blob: fc73b2cf2fb6270082a1a7d4069dd3d3a33ed1b9 [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 {
787#if defined(MBEDTLS_MD2_C)
788 case PSA_ALG_MD2:
789 mbedtls_md2_free( &operation->ctx.md2 );
790 break;
791#endif
792#if defined(MBEDTLS_MD4_C)
793 case PSA_ALG_MD4:
794 mbedtls_md4_free( &operation->ctx.md4 );
795 break;
796#endif
797#if defined(MBEDTLS_MD5_C)
798 case PSA_ALG_MD5:
799 mbedtls_md5_free( &operation->ctx.md5 );
800 break;
801#endif
802#if defined(MBEDTLS_RIPEMD160_C)
803 case PSA_ALG_RIPEMD160:
804 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
805 break;
806#endif
807#if defined(MBEDTLS_SHA1_C)
808 case PSA_ALG_SHA_1:
809 mbedtls_sha1_free( &operation->ctx.sha1 );
810 break;
811#endif
812#if defined(MBEDTLS_SHA256_C)
813 case PSA_ALG_SHA_224:
814 case PSA_ALG_SHA_256:
815 mbedtls_sha256_free( &operation->ctx.sha256 );
816 break;
817#endif
818#if defined(MBEDTLS_SHA512_C)
819 case PSA_ALG_SHA_384:
820 case PSA_ALG_SHA_512:
821 mbedtls_sha512_free( &operation->ctx.sha512 );
822 break;
823#endif
824 default:
825 return( PSA_ERROR_NOT_SUPPORTED );
826 }
827 operation->alg = 0;
828 return( PSA_SUCCESS );
829}
830
831psa_status_t psa_hash_start( psa_hash_operation_t *operation,
832 psa_algorithm_t alg )
833{
834 int ret;
835 operation->alg = 0;
836 switch( alg )
837 {
838#if defined(MBEDTLS_MD2_C)
839 case PSA_ALG_MD2:
840 mbedtls_md2_init( &operation->ctx.md2 );
841 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
842 break;
843#endif
844#if defined(MBEDTLS_MD4_C)
845 case PSA_ALG_MD4:
846 mbedtls_md4_init( &operation->ctx.md4 );
847 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
848 break;
849#endif
850#if defined(MBEDTLS_MD5_C)
851 case PSA_ALG_MD5:
852 mbedtls_md5_init( &operation->ctx.md5 );
853 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
854 break;
855#endif
856#if defined(MBEDTLS_RIPEMD160_C)
857 case PSA_ALG_RIPEMD160:
858 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
859 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
860 break;
861#endif
862#if defined(MBEDTLS_SHA1_C)
863 case PSA_ALG_SHA_1:
864 mbedtls_sha1_init( &operation->ctx.sha1 );
865 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
866 break;
867#endif
868#if defined(MBEDTLS_SHA256_C)
869 case PSA_ALG_SHA_224:
870 mbedtls_sha256_init( &operation->ctx.sha256 );
871 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
872 break;
873 case PSA_ALG_SHA_256:
874 mbedtls_sha256_init( &operation->ctx.sha256 );
875 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
876 break;
877#endif
878#if defined(MBEDTLS_SHA512_C)
879 case PSA_ALG_SHA_384:
880 mbedtls_sha512_init( &operation->ctx.sha512 );
881 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
882 break;
883 case PSA_ALG_SHA_512:
884 mbedtls_sha512_init( &operation->ctx.sha512 );
885 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
886 break;
887#endif
888 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +0200889 return( PSA_ALG_IS_HASH( alg ) ?
890 PSA_ERROR_NOT_SUPPORTED :
891 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100892 }
893 if( ret == 0 )
894 operation->alg = alg;
895 else
896 psa_hash_abort( operation );
897 return( mbedtls_to_psa_error( ret ) );
898}
899
900psa_status_t psa_hash_update( psa_hash_operation_t *operation,
901 const uint8_t *input,
902 size_t input_length )
903{
904 int ret;
905 switch( operation->alg )
906 {
907#if defined(MBEDTLS_MD2_C)
908 case PSA_ALG_MD2:
909 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
910 input, input_length );
911 break;
912#endif
913#if defined(MBEDTLS_MD4_C)
914 case PSA_ALG_MD4:
915 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
916 input, input_length );
917 break;
918#endif
919#if defined(MBEDTLS_MD5_C)
920 case PSA_ALG_MD5:
921 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
922 input, input_length );
923 break;
924#endif
925#if defined(MBEDTLS_RIPEMD160_C)
926 case PSA_ALG_RIPEMD160:
927 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
928 input, input_length );
929 break;
930#endif
931#if defined(MBEDTLS_SHA1_C)
932 case PSA_ALG_SHA_1:
933 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
934 input, input_length );
935 break;
936#endif
937#if defined(MBEDTLS_SHA256_C)
938 case PSA_ALG_SHA_224:
939 case PSA_ALG_SHA_256:
940 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
941 input, input_length );
942 break;
943#endif
944#if defined(MBEDTLS_SHA512_C)
945 case PSA_ALG_SHA_384:
946 case PSA_ALG_SHA_512:
947 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
948 input, input_length );
949 break;
950#endif
951 default:
952 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
953 break;
954 }
955 if( ret != 0 )
956 psa_hash_abort( operation );
957 return( mbedtls_to_psa_error( ret ) );
958}
959
960psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
961 uint8_t *hash,
962 size_t hash_size,
963 size_t *hash_length )
964{
965 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200966 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100967
968 /* Fill the output buffer with something that isn't a valid hash
969 * (barring an attack on the hash and deliberately-crafted input),
970 * in case the caller doesn't check the return status properly. */
971 *hash_length = actual_hash_length;
972 memset( hash, '!', hash_size );
973
974 if( hash_size < actual_hash_length )
975 return( PSA_ERROR_BUFFER_TOO_SMALL );
976
977 switch( operation->alg )
978 {
979#if defined(MBEDTLS_MD2_C)
980 case PSA_ALG_MD2:
981 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
982 break;
983#endif
984#if defined(MBEDTLS_MD4_C)
985 case PSA_ALG_MD4:
986 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
987 break;
988#endif
989#if defined(MBEDTLS_MD5_C)
990 case PSA_ALG_MD5:
991 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
992 break;
993#endif
994#if defined(MBEDTLS_RIPEMD160_C)
995 case PSA_ALG_RIPEMD160:
996 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
997 break;
998#endif
999#if defined(MBEDTLS_SHA1_C)
1000 case PSA_ALG_SHA_1:
1001 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1002 break;
1003#endif
1004#if defined(MBEDTLS_SHA256_C)
1005 case PSA_ALG_SHA_224:
1006 case PSA_ALG_SHA_256:
1007 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1008 break;
1009#endif
1010#if defined(MBEDTLS_SHA512_C)
1011 case PSA_ALG_SHA_384:
1012 case PSA_ALG_SHA_512:
1013 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1014 break;
1015#endif
1016 default:
1017 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1018 break;
1019 }
1020
1021 if( ret == 0 )
1022 {
1023 return( psa_hash_abort( operation ) );
1024 }
1025 else
1026 {
1027 psa_hash_abort( operation );
1028 return( mbedtls_to_psa_error( ret ) );
1029 }
1030}
1031
Gilles Peskine2d277862018-06-18 15:41:12 +02001032psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1033 const uint8_t *hash,
1034 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001035{
1036 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1037 size_t actual_hash_length;
1038 psa_status_t status = psa_hash_finish( operation,
1039 actual_hash, sizeof( actual_hash ),
1040 &actual_hash_length );
1041 if( status != PSA_SUCCESS )
1042 return( status );
1043 if( actual_hash_length != hash_length )
1044 return( PSA_ERROR_INVALID_SIGNATURE );
1045 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1046 return( PSA_ERROR_INVALID_SIGNATURE );
1047 return( PSA_SUCCESS );
1048}
1049
1050
1051
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001052/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001053/* MAC */
1054/****************************************************************/
1055
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001056static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001057 psa_algorithm_t alg,
1058 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001059 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001060 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001061{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001062 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001063 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001064
1065 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1066 {
1067 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001068 {
1069 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1070 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001071
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001072 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001073 {
1074 case PSA_ALG_STREAM_CIPHER:
1075 mode = MBEDTLS_MODE_STREAM;
1076 break;
1077 case PSA_ALG_CBC_BASE:
1078 mode = MBEDTLS_MODE_CBC;
1079 break;
1080 case PSA_ALG_CFB_BASE:
1081 mode = MBEDTLS_MODE_CFB;
1082 break;
1083 case PSA_ALG_OFB_BASE:
1084 mode = MBEDTLS_MODE_OFB;
1085 break;
1086 case PSA_ALG_CTR:
1087 mode = MBEDTLS_MODE_CTR;
1088 break;
1089 case PSA_ALG_CCM:
1090 mode = MBEDTLS_MODE_CCM;
1091 break;
1092 case PSA_ALG_GCM:
1093 mode = MBEDTLS_MODE_GCM;
1094 break;
1095 default:
1096 return( NULL );
1097 }
1098 }
1099 else if( alg == PSA_ALG_CMAC )
1100 mode = MBEDTLS_MODE_ECB;
1101 else if( alg == PSA_ALG_GMAC )
1102 mode = MBEDTLS_MODE_GCM;
1103 else
1104 return( NULL );
1105
1106 switch( key_type )
1107 {
1108 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001109 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001110 break;
1111 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001112 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1113 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001114 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001115 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001116 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001117 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001118 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1119 * but two-key Triple-DES is functionally three-key Triple-DES
1120 * with K1=K3, so that's how we present it to mbedtls. */
1121 if( key_bits == 128 )
1122 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001123 break;
1124 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001125 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001126 break;
1127 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001128 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001129 break;
1130 default:
1131 return( NULL );
1132 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001133 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001134 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001135
mohammad1603f4f0d612018-06-03 15:04:51 +03001136 return( mbedtls_cipher_info_from_values( cipher_id_tmp, key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001137}
1138
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001139static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001140{
Gilles Peskine2d277862018-06-18 15:41:12 +02001141 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001142 {
1143 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001144 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001145 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001146 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001147 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001148 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001149 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001150 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001151 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001152 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001153 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001154 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001155 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001156 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001157 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001158 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001159 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001160 return( 128 );
1161 default:
1162 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001163 }
1164}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001165
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001166/* Initialize the MAC operation structure. Once this function has been
1167 * called, psa_mac_abort can run and will do the right thing. */
1168static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1169 psa_algorithm_t alg )
1170{
1171 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1172
1173 operation->alg = alg;
1174 operation->key_set = 0;
1175 operation->iv_set = 0;
1176 operation->iv_required = 0;
1177 operation->has_input = 0;
1178 operation->key_usage_sign = 0;
1179 operation->key_usage_verify = 0;
1180
1181#if defined(MBEDTLS_CMAC_C)
1182 if( alg == PSA_ALG_CMAC )
1183 {
1184 operation->iv_required = 0;
1185 mbedtls_cipher_init( &operation->ctx.cmac );
1186 status = PSA_SUCCESS;
1187 }
1188 else
1189#endif /* MBEDTLS_CMAC_C */
1190#if defined(MBEDTLS_MD_C)
1191 if( PSA_ALG_IS_HMAC( operation->alg ) )
1192 {
1193 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1194 PSA_ALG_HMAC_HASH( alg ) );
1195 }
1196 else
1197#endif /* MBEDTLS_MD_C */
1198 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001199 if( ! PSA_ALG_IS_MAC( alg ) )
1200 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001201 }
1202
1203 if( status != PSA_SUCCESS )
1204 memset( operation, 0, sizeof( *operation ) );
1205 return( status );
1206}
1207
Gilles Peskine8c9def32018-02-08 10:02:12 +01001208psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1209{
1210 switch( operation->alg )
1211 {
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001212 case 0:
1213 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001214#if defined(MBEDTLS_CMAC_C)
1215 case PSA_ALG_CMAC:
1216 mbedtls_cipher_free( &operation->ctx.cmac );
1217 break;
1218#endif /* MBEDTLS_CMAC_C */
1219 default:
1220#if defined(MBEDTLS_MD_C)
1221 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001222 {
Gilles Peskine99bc6492018-06-11 17:13:00 +02001223 unsigned int block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001224 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001225
Gilles Peskine99bc6492018-06-11 17:13:00 +02001226 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001227 return( PSA_ERROR_NOT_SUPPORTED );
1228
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001229 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001230 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001231 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001232 else
1233#endif /* MBEDTLS_MD_C */
1234 return( PSA_ERROR_NOT_SUPPORTED );
1235 }
Moran Peker41deec42018-04-04 15:43:05 +03001236
Gilles Peskine8c9def32018-02-08 10:02:12 +01001237 operation->alg = 0;
1238 operation->key_set = 0;
1239 operation->iv_set = 0;
1240 operation->iv_required = 0;
1241 operation->has_input = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001242 operation->key_usage_sign = 0;
1243 operation->key_usage_verify = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001244
Gilles Peskine8c9def32018-02-08 10:02:12 +01001245 return( PSA_SUCCESS );
1246}
1247
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001248#if defined(MBEDTLS_CMAC_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001249static int psa_cmac_start( psa_mac_operation_t *operation,
1250 size_t key_bits,
1251 key_slot_t *slot,
1252 const mbedtls_cipher_info_t *cipher_info )
1253{
1254 int ret;
1255
1256 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001257
1258 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1259 if( ret != 0 )
1260 return( ret );
1261
1262 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1263 slot->data.raw.data,
1264 key_bits );
1265 return( ret );
1266}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001267#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001268
Gilles Peskine248051a2018-06-20 16:09:38 +02001269#if defined(MBEDTLS_MD_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001270static int psa_hmac_start( psa_mac_operation_t *operation,
1271 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001272 key_slot_t *slot,
1273 psa_algorithm_t alg )
1274{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001275 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001276 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001277 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001278 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001279 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001280 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001281 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001282 size_t key_length = slot->data.raw.bytes;
1283 psa_status_t status;
1284
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001285 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001286 return( PSA_ERROR_NOT_SUPPORTED );
1287
1288 if( key_type != PSA_KEY_TYPE_HMAC )
1289 return( PSA_ERROR_INVALID_ARGUMENT );
1290
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001291 operation->mac_size = digest_size;
1292
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001293 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001294 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001295 {
1296 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001297 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001298 if( status != PSA_SUCCESS )
1299 return( status );
1300 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001301 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001302 if( status != PSA_SUCCESS )
1303 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001304 }
Gilles Peskined223b522018-06-11 18:12:58 +02001305 else
1306 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001307
Gilles Peskined223b522018-06-11 18:12:58 +02001308 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1309 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001310 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001311 ipad[i] ^= 0x36;
1312 memset( ipad + key_length, 0x36, block_size - key_length );
1313
1314 /* Copy the key material from ipad to opad, flipping the requisite bits,
1315 * and filling the rest of opad with the requisite constant. */
1316 for( i = 0; i < key_length; i++ )
1317 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1318 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001319
1320 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1321 PSA_ALG_HMAC_HASH( alg ) );
1322 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001323 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001324
1325 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1326 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001327
1328cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001329 mbedtls_zeroize( ipad, key_length );
1330 /* opad is in the context. It needs to stay in memory if this function
1331 * succeeds, and it will be wiped by psa_mac_abort() called from
1332 * psa_mac_start in the error case. */
1333
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001334 return( status );
1335}
Gilles Peskine248051a2018-06-20 16:09:38 +02001336#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001337
Gilles Peskine8c9def32018-02-08 10:02:12 +01001338psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1339 psa_key_slot_t key,
1340 psa_algorithm_t alg )
1341{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001342 psa_status_t status;
1343 key_slot_t *slot;
1344 psa_key_type_t key_type;
1345 size_t key_bits;
Gilles Peskine828ed142018-06-18 23:25:51 +02001346 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001347
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001348 status = psa_mac_init( operation, alg );
1349 if( status != PSA_SUCCESS )
1350 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001351
1352 status = psa_get_key_information( key, &key_type, &key_bits );
1353 if( status != PSA_SUCCESS )
1354 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001355
Gilles Peskine8c9def32018-02-08 10:02:12 +01001356 slot = &global_data.key_slots[key];
Gilles Peskine99bc6492018-06-11 17:13:00 +02001357 if( slot->type == PSA_KEY_TYPE_NONE )
1358 return( PSA_ERROR_EMPTY_SLOT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001359
Moran Pekerd7326592018-05-29 16:56:39 +03001360 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001361 operation->key_usage_sign = 1;
1362
Moran Pekerd7326592018-05-29 16:56:39 +03001363 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001364 operation->key_usage_verify = 1;
1365
Gilles Peskine8c9def32018-02-08 10:02:12 +01001366 if( ! PSA_ALG_IS_HMAC( alg ) )
1367 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001368 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001369 if( cipher_info == NULL )
1370 return( PSA_ERROR_NOT_SUPPORTED );
1371 operation->mac_size = cipher_info->block_size;
1372 }
1373 switch( alg )
1374 {
1375#if defined(MBEDTLS_CMAC_C)
1376 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001377 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1378 key_bits,
1379 slot,
1380 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001381 break;
1382#endif /* MBEDTLS_CMAC_C */
1383 default:
1384#if defined(MBEDTLS_MD_C)
1385 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001386 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001387 else
1388#endif /* MBEDTLS_MD_C */
1389 return( PSA_ERROR_NOT_SUPPORTED );
1390 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001391
Gilles Peskine8c9def32018-02-08 10:02:12 +01001392 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001393 * context may contain data that needs to be wiped on error. */
1394 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001395 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001396 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001397 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001398 else
1399 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001400 operation->key_set = 1;
1401 }
1402 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001403}
1404
1405psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1406 const uint8_t *input,
1407 size_t input_length )
1408{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001409 int ret = 0 ;
1410 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001411 if( ! operation->key_set )
1412 return( PSA_ERROR_BAD_STATE );
1413 if( operation->iv_required && ! operation->iv_set )
1414 return( PSA_ERROR_BAD_STATE );
1415 operation->has_input = 1;
1416
1417 switch( operation->alg )
1418 {
1419#if defined(MBEDTLS_CMAC_C)
1420 case PSA_ALG_CMAC:
1421 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1422 input, input_length );
1423 break;
1424#endif /* MBEDTLS_CMAC_C */
1425 default:
1426#if defined(MBEDTLS_MD_C)
1427 if( PSA_ALG_IS_HMAC( operation->alg ) )
1428 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001429 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001430 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001431 }
1432 else
1433#endif /* MBEDTLS_MD_C */
1434 {
1435 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1436 }
1437 break;
1438 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001439 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001440 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001441 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001442 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001443 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001444 }
1445
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001446 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001447}
1448
mohammad16036df908f2018-04-02 08:34:15 -07001449static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001450 uint8_t *mac,
1451 size_t mac_size,
1452 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001453{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001454 int ret = 0;
1455 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001456 if( ! operation->key_set )
1457 return( PSA_ERROR_BAD_STATE );
1458 if( operation->iv_required && ! operation->iv_set )
1459 return( PSA_ERROR_BAD_STATE );
1460
1461 /* Fill the output buffer with something that isn't a valid mac
1462 * (barring an attack on the mac and deliberately-crafted input),
1463 * in case the caller doesn't check the return status properly. */
1464 *mac_length = operation->mac_size;
1465 memset( mac, '!', mac_size );
1466
1467 if( mac_size < operation->mac_size )
1468 return( PSA_ERROR_BUFFER_TOO_SMALL );
1469
1470 switch( operation->alg )
1471 {
1472#if defined(MBEDTLS_CMAC_C)
1473 case PSA_ALG_CMAC:
1474 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1475 break;
1476#endif /* MBEDTLS_CMAC_C */
1477 default:
1478#if defined(MBEDTLS_MD_C)
1479 if( PSA_ALG_IS_HMAC( operation->alg ) )
1480 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001481 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001482 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001483 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001484 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001485 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001486
Gilles Peskine99bc6492018-06-11 17:13:00 +02001487 if( block_size == 0 )
1488 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001489
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001490 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001491 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001492 if( status != PSA_SUCCESS )
1493 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001494 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001495
1496 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1497 PSA_ALG_HMAC_HASH( operation->alg ) );
1498 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001499 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001500
1501 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001502 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001503 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001504 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001505
1506 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001507 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001508 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001509 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001510
1511 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1512 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001513 hmac_cleanup:
1514 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001515 }
1516 else
1517#endif /* MBEDTLS_MD_C */
1518 {
1519 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1520 }
1521 break;
1522 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001523cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001524
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001525 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526 {
1527 return( psa_mac_abort( operation ) );
1528 }
1529 else
1530 {
1531 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001532 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001533 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001534
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001535 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001536 }
1537}
1538
mohammad16036df908f2018-04-02 08:34:15 -07001539psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1540 uint8_t *mac,
1541 size_t mac_size,
1542 size_t *mac_length )
1543{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001544 if( ! operation->key_usage_sign )
mohammad16036df908f2018-04-02 08:34:15 -07001545 return( PSA_ERROR_NOT_PERMITTED );
1546
Gilles Peskine99bc6492018-06-11 17:13:00 +02001547 return( psa_mac_finish_internal( operation, mac,
1548 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001549}
1550
Gilles Peskine828ed142018-06-18 23:25:51 +02001551#define PSA_MAC_MAX_SIZE \
Gilles Peskine2d277862018-06-18 15:41:12 +02001552 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1553 MBEDTLS_MD_MAX_SIZE : \
Gilles Peskine8c9def32018-02-08 10:02:12 +01001554 MBEDTLS_MAX_BLOCK_LENGTH )
1555psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1556 const uint8_t *mac,
1557 size_t mac_length )
1558{
Gilles Peskine828ed142018-06-18 23:25:51 +02001559 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001560 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001561 psa_status_t status;
1562
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001563 if( ! operation->key_usage_verify )
mohammad16036df908f2018-04-02 08:34:15 -07001564 return( PSA_ERROR_NOT_PERMITTED );
1565
1566 status = psa_mac_finish_internal( operation,
1567 actual_mac, sizeof( actual_mac ),
1568 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001569 if( status != PSA_SUCCESS )
1570 return( status );
1571 if( actual_mac_length != mac_length )
1572 return( PSA_ERROR_INVALID_SIGNATURE );
1573 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1574 return( PSA_ERROR_INVALID_SIGNATURE );
1575 return( PSA_SUCCESS );
1576}
1577
1578
Gilles Peskine20035e32018-02-03 22:44:14 +01001579
Gilles Peskine20035e32018-02-03 22:44:14 +01001580/****************************************************************/
1581/* Asymmetric cryptography */
1582/****************************************************************/
1583
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001584/* Decode the hash algorithm from alg and store the mbedtls encoding in
1585 * md_alg. Verify that the hash length is consistent. */
1586static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1587 size_t hash_length,
1588 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001589{
Gilles Peskine61b91d42018-06-08 16:09:36 +02001590 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
1591 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1592 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1593 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001594 {
1595#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001596 if( hash_length > UINT_MAX )
1597 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001598#endif
1599 }
1600 else
1601 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001602 if( mbedtls_md_get_size( md_info ) != hash_length )
1603 return( PSA_ERROR_INVALID_ARGUMENT );
1604 if( md_info == NULL )
1605 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001606 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001607 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001608}
1609
Gilles Peskine61b91d42018-06-08 16:09:36 +02001610psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1611 psa_algorithm_t alg,
1612 const uint8_t *hash,
1613 size_t hash_length,
1614 const uint8_t *salt,
1615 size_t salt_length,
1616 uint8_t *signature,
1617 size_t signature_size,
1618 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001619{
1620 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001621 psa_status_t status;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001622 *signature_length = 0;
1623 (void) salt;
1624 (void) salt_length;
1625
Gilles Peskine828ed142018-06-18 23:25:51 +02001626 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine20035e32018-02-03 22:44:14 +01001627 return( PSA_ERROR_EMPTY_SLOT );
1628 slot = &global_data.key_slots[key];
1629 if( slot->type == PSA_KEY_TYPE_NONE )
1630 return( PSA_ERROR_EMPTY_SLOT );
1631 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1632 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001633 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
mohammad160306e79202018-03-28 13:17:44 +03001634 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001635
Gilles Peskine20035e32018-02-03 22:44:14 +01001636#if defined(MBEDTLS_RSA_C)
1637 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1638 {
1639 mbedtls_rsa_context *rsa = slot->data.rsa;
1640 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001641 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001642 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001643 if( status != PSA_SUCCESS )
1644 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001645
Gilles Peskine20035e32018-02-03 22:44:14 +01001646 if( signature_size < rsa->len )
1647 return( PSA_ERROR_BUFFER_TOO_SMALL );
1648#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001649 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001650 {
1651 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1652 MBEDTLS_MD_NONE );
1653 ret = mbedtls_rsa_pkcs1_sign( rsa,
1654 mbedtls_ctr_drbg_random,
1655 &global_data.ctr_drbg,
1656 MBEDTLS_RSA_PRIVATE,
1657 md_alg, hash_length, hash,
1658 signature );
1659 }
1660 else
1661#endif /* MBEDTLS_PKCS1_V15 */
1662#if defined(MBEDTLS_PKCS1_V21)
1663 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1664 {
1665 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1666 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1667 mbedtls_ctr_drbg_random,
1668 &global_data.ctr_drbg,
1669 MBEDTLS_RSA_PRIVATE,
1670 md_alg, hash_length, hash,
1671 signature );
1672 }
1673 else
1674#endif /* MBEDTLS_PKCS1_V21 */
1675 {
1676 return( PSA_ERROR_INVALID_ARGUMENT );
1677 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001678 if( ret == 0 )
1679 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001680 return( mbedtls_to_psa_error( ret ) );
1681 }
1682 else
1683#endif /* defined(MBEDTLS_RSA_C) */
1684#if defined(MBEDTLS_ECP_C)
1685 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1686 {
itayzafrir5c753392018-05-08 11:18:38 +03001687 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1688 int ret;
1689 const mbedtls_md_info_t *md_info;
1690 mbedtls_md_type_t md_alg;
1691 if( signature_size < PSA_ECDSA_SIGNATURE_SIZE( ecdsa->grp.pbits ) )
1692 return( PSA_ERROR_BUFFER_TOO_SMALL );
1693 md_info = mbedtls_md_info_from_psa( alg );
1694 md_alg = mbedtls_md_get_type( md_info );
1695 ret = mbedtls_ecdsa_write_signature( ecdsa, md_alg, hash, hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001696 signature, signature_length,
1697 mbedtls_ctr_drbg_random,
1698 &global_data.ctr_drbg );
itayzafrir5c753392018-05-08 11:18:38 +03001699 return( mbedtls_to_psa_error( ret ) );
1700 }
1701 else
1702#endif /* defined(MBEDTLS_ECP_C) */
1703 {
Gilles Peskine20035e32018-02-03 22:44:14 +01001704 return( PSA_ERROR_NOT_SUPPORTED );
1705 }
itayzafrir5c753392018-05-08 11:18:38 +03001706}
1707
1708psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1709 psa_algorithm_t alg,
1710 const uint8_t *hash,
1711 size_t hash_length,
1712 const uint8_t *salt,
1713 size_t salt_length,
1714 uint8_t *signature,
1715 size_t signature_size )
1716{
1717 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001718 psa_status_t status;
itayzafrir5c753392018-05-08 11:18:38 +03001719 (void) salt;
1720 (void) salt_length;
1721
Gilles Peskine828ed142018-06-18 23:25:51 +02001722 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
itayzafrir5c753392018-05-08 11:18:38 +03001723 return( PSA_ERROR_INVALID_ARGUMENT );
1724 slot = &global_data.key_slots[key];
1725 if( slot->type == PSA_KEY_TYPE_NONE )
1726 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001727 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001728 return( PSA_ERROR_NOT_PERMITTED );
1729
Gilles Peskine61b91d42018-06-08 16:09:36 +02001730#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001731 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1732 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001733 {
1734 mbedtls_rsa_context *rsa = slot->data.rsa;
1735 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001736 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001737 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001738 if( status != PSA_SUCCESS )
1739 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001740
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001741 if( signature_size < rsa->len )
1742 return( PSA_ERROR_BUFFER_TOO_SMALL );
1743#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001744 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001745 {
1746 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1747 MBEDTLS_MD_NONE );
1748
1749 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001750 mbedtls_ctr_drbg_random,
1751 &global_data.ctr_drbg,
1752 MBEDTLS_RSA_PUBLIC,
1753 md_alg,
1754 hash_length,
1755 hash,
1756 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001757
1758 }
1759 else
1760#endif /* MBEDTLS_PKCS1_V15 */
1761#if defined(MBEDTLS_PKCS1_V21)
1762 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1763 {
Gilles Peskinebeb49482018-06-08 17:44:35 +02001764 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1765 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1766 mbedtls_ctr_drbg_random,
1767 &global_data.ctr_drbg,
1768 MBEDTLS_RSA_PUBLIC,
1769 md_alg, hash_length, hash,
1770 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001771 }
1772 else
1773#endif /* MBEDTLS_PKCS1_V21 */
1774 {
1775 return( PSA_ERROR_INVALID_ARGUMENT );
1776 }
1777 return( mbedtls_to_psa_error( ret ) );
1778 }
1779 else
1780#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001781#if defined(MBEDTLS_ECP_C)
1782 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1783 {
1784 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1785 int ret;
Gilles Peskine2d277862018-06-18 15:41:12 +02001786 (void) alg;
Gilles Peskine61b91d42018-06-08 16:09:36 +02001787 ret = mbedtls_ecdsa_read_signature( ecdsa, hash, hash_length,
1788 signature, signature_size );
itayzafrir5c753392018-05-08 11:18:38 +03001789 return( mbedtls_to_psa_error( ret ) );
1790 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001791 else
1792#endif /* defined(MBEDTLS_ECP_C) */
1793 {
1794 return( PSA_ERROR_NOT_SUPPORTED );
1795 }
1796}
1797
Gilles Peskine61b91d42018-06-08 16:09:36 +02001798psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
1799 psa_algorithm_t alg,
1800 const uint8_t *input,
1801 size_t input_length,
1802 const uint8_t *salt,
1803 size_t salt_length,
1804 uint8_t *output,
1805 size_t output_size,
1806 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001807{
1808 key_slot_t *slot;
1809 (void) salt;
1810 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001811 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001812
Gilles Peskine828ed142018-06-18 23:25:51 +02001813 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03001814 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001815 slot = &global_data.key_slots[key];
1816 if( slot->type == PSA_KEY_TYPE_NONE )
1817 return( PSA_ERROR_EMPTY_SLOT );
1818 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1819 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001820 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
1821 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001822
1823#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001824 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1825 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001826 {
1827 mbedtls_rsa_context *rsa = slot->data.rsa;
1828 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001829 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02001830 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001831#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001832 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001833 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001834 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
1835 mbedtls_ctr_drbg_random,
1836 &global_data.ctr_drbg,
1837 MBEDTLS_RSA_PUBLIC,
1838 input_length,
1839 input,
1840 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001841 }
1842 else
1843#endif /* MBEDTLS_PKCS1_V15 */
1844#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001845 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001846 {
1847 return( PSA_ERROR_NOT_SUPPORTED );
1848 }
1849 else
1850#endif /* MBEDTLS_PKCS1_V21 */
1851 {
1852 return( PSA_ERROR_INVALID_ARGUMENT );
1853 }
1854 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001855 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001856 return( mbedtls_to_psa_error( ret ) );
1857 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001858 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001859#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001860 {
1861 return( PSA_ERROR_NOT_SUPPORTED );
1862 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001863}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001864
Gilles Peskine61b91d42018-06-08 16:09:36 +02001865psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
1866 psa_algorithm_t alg,
1867 const uint8_t *input,
1868 size_t input_length,
1869 const uint8_t *salt,
1870 size_t salt_length,
1871 uint8_t *output,
1872 size_t output_size,
1873 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001874{
1875 key_slot_t *slot;
1876 (void) salt;
1877 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001878 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001879
Gilles Peskine828ed142018-06-18 23:25:51 +02001880 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001881 return( PSA_ERROR_EMPTY_SLOT );
1882 slot = &global_data.key_slots[key];
1883 if( slot->type == PSA_KEY_TYPE_NONE )
1884 return( PSA_ERROR_EMPTY_SLOT );
1885 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1886 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001887 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
1888 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001889
1890#if defined(MBEDTLS_RSA_C)
1891 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1892 {
1893 mbedtls_rsa_context *rsa = slot->data.rsa;
1894 int ret;
1895
Gilles Peskinec4def2f2018-06-08 17:53:48 +02001896 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001897 return( PSA_ERROR_INVALID_ARGUMENT );
1898
1899#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001900 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001901 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001902 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
1903 mbedtls_ctr_drbg_random,
1904 &global_data.ctr_drbg,
1905 MBEDTLS_RSA_PRIVATE,
1906 output_length,
1907 input,
1908 output,
1909 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001910 }
1911 else
1912#endif /* MBEDTLS_PKCS1_V15 */
1913#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001914 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001915 {
1916 return( PSA_ERROR_NOT_SUPPORTED );
1917 }
1918 else
1919#endif /* MBEDTLS_PKCS1_V21 */
1920 {
1921 return( PSA_ERROR_INVALID_ARGUMENT );
1922 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001923
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001924 return( mbedtls_to_psa_error( ret ) );
1925 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001926 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001927#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001928 {
1929 return( PSA_ERROR_NOT_SUPPORTED );
1930 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001931}
Gilles Peskine20035e32018-02-03 22:44:14 +01001932
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001933
1934
mohammad1603503973b2018-03-12 15:59:30 +02001935/****************************************************************/
1936/* Symmetric cryptography */
1937/****************************************************************/
1938
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001939/* Initialize the cipher operation structure. Once this function has been
1940 * called, psa_cipher_abort can run and will do the right thing. */
1941static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
1942 psa_algorithm_t alg )
1943{
Gilles Peskinec06e0712018-06-20 16:21:04 +02001944 if( ! PSA_ALG_IS_CIPHER( alg ) )
1945 {
1946 memset( operation, 0, sizeof( *operation ) );
1947 return( PSA_ERROR_INVALID_ARGUMENT );
1948 }
1949
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001950 operation->alg = alg;
1951 operation->key_set = 0;
1952 operation->iv_set = 0;
1953 operation->iv_required = 1;
1954 operation->iv_size = 0;
1955 operation->block_size = 0;
1956 mbedtls_cipher_init( &operation->ctx.cipher );
1957 return( PSA_SUCCESS );
1958}
1959
Gilles Peskinee553c652018-06-04 16:22:46 +02001960static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
1961 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02001962 psa_algorithm_t alg,
1963 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02001964{
1965 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1966 psa_status_t status;
1967 key_slot_t *slot;
1968 psa_key_type_t key_type;
1969 size_t key_bits;
1970 const mbedtls_cipher_info_t *cipher_info = NULL;
1971
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001972 status = psa_cipher_init( operation, alg );
1973 if( status != PSA_SUCCESS )
1974 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02001975
1976 status = psa_get_key_information( key, &key_type, &key_bits );
1977 if( status != PSA_SUCCESS )
1978 return( status );
1979 slot = &global_data.key_slots[key];
1980
Gilles Peskinebb1072f2018-06-08 18:46:05 +02001981 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02001982 if( cipher_info == NULL )
1983 return( PSA_ERROR_NOT_SUPPORTED );
1984
mohammad1603503973b2018-03-12 15:59:30 +02001985 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03001986 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001987 {
1988 psa_cipher_abort( operation );
1989 return( mbedtls_to_psa_error( ret ) );
1990 }
1991
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001992#if defined(MBEDTLS_DES_C)
1993 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
1994 {
1995 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
1996 unsigned char keys[24];
1997 memcpy( keys, slot->data.raw.data, 16 );
1998 memcpy( keys + 16, slot->data.raw.data, 8 );
1999 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2000 keys,
2001 192, cipher_operation );
2002 }
2003 else
2004#endif
2005 {
2006 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2007 slot->data.raw.data,
2008 key_bits, cipher_operation );
2009 }
Moran Peker41deec42018-04-04 15:43:05 +03002010 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002011 {
2012 psa_cipher_abort( operation );
2013 return( mbedtls_to_psa_error( ret ) );
2014 }
2015
mohammad16038481e742018-03-18 13:57:31 +02002016#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002017 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002018 {
Gilles Peskine53514202018-06-06 15:11:46 +02002019 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2020 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002021
Moran Pekera28258c2018-05-29 16:25:04 +03002022 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002023 {
2024 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2025 mode = MBEDTLS_PADDING_PKCS7;
2026 break;
2027 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2028 mode = MBEDTLS_PADDING_NONE;
2029 break;
2030 default:
Moran Pekerae382792018-05-31 14:06:17 +03002031 psa_cipher_abort( operation );
2032 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002033 }
2034 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002035 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002036 {
2037 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002038 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002039 }
mohammad16038481e742018-03-18 13:57:31 +02002040 }
2041#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2042
mohammad1603503973b2018-03-12 15:59:30 +02002043 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002044 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
2045 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
2046 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002047 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002048 {
mohammad160389e0f462018-04-12 08:48:45 +03002049 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02002050 }
mohammad1603503973b2018-03-12 15:59:30 +02002051
Moran Peker395db872018-05-31 14:07:14 +03002052 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002053}
2054
Gilles Peskinee553c652018-06-04 16:22:46 +02002055psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
2056 psa_key_slot_t key,
2057 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002058{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002059 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002060}
2061
Gilles Peskinee553c652018-06-04 16:22:46 +02002062psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
2063 psa_key_slot_t key,
2064 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002065{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002066 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002067}
2068
Gilles Peskinee553c652018-06-04 16:22:46 +02002069psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
2070 unsigned char *iv,
2071 size_t iv_size,
2072 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002073{
Moran Peker41deec42018-04-04 15:43:05 +03002074 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002075 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002076 return( PSA_ERROR_BAD_STATE );
2077 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002078 {
Moran Peker41deec42018-04-04 15:43:05 +03002079 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2080 goto exit;
2081 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002082 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2083 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002084 if( ret != 0 )
2085 {
2086 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002087 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002088 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002089
mohammad16038481e742018-03-18 13:57:31 +02002090 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03002091 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002092
Moran Peker395db872018-05-31 14:07:14 +03002093exit:
2094 if( ret != PSA_SUCCESS )
2095 psa_cipher_abort( operation );
2096 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002097}
2098
Gilles Peskinee553c652018-06-04 16:22:46 +02002099psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2100 const unsigned char *iv,
2101 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002102{
Moran Peker41deec42018-04-04 15:43:05 +03002103 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002104 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002105 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002106 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002107 {
Moran Pekerae382792018-05-31 14:06:17 +03002108 psa_cipher_abort( operation );
2109 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002110 }
mohammad1603503973b2018-03-12 15:59:30 +02002111 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002112 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002113 {
2114 psa_cipher_abort( operation );
2115 return( mbedtls_to_psa_error( ret ) );
2116 }
2117
2118 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002119
Moran Peker395db872018-05-31 14:07:14 +03002120 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002121}
2122
Gilles Peskinee553c652018-06-04 16:22:46 +02002123psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2124 const uint8_t *input,
2125 size_t input_length,
2126 unsigned char *output,
2127 size_t output_size,
2128 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002129{
2130 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002131 size_t expected_output_size;
2132 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2133 {
2134 /* Take the unprocessed partial block left over from previous
2135 * update calls, if any, plus the input to this call. Remove
2136 * the last partial block, if any. You get the data that will be
2137 * output in this call. */
2138 expected_output_size =
2139 ( operation->ctx.cipher.unprocessed_len + input_length )
2140 / operation->block_size * operation->block_size;
2141 }
2142 else
2143 {
2144 expected_output_size = input_length;
2145 }
2146 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002147 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002148
mohammad1603503973b2018-03-12 15:59:30 +02002149 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002150 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002151 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002152 {
2153 psa_cipher_abort( operation );
2154 return( mbedtls_to_psa_error( ret ) );
2155 }
2156
Moran Peker395db872018-05-31 14:07:14 +03002157 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002158}
2159
Gilles Peskinee553c652018-06-04 16:22:46 +02002160psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2161 uint8_t *output,
2162 size_t output_size,
2163 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002164{
2165 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002166 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002167
mohammad1603503973b2018-03-12 15:59:30 +02002168 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002169 {
Moran Peker7cb22b82018-06-05 11:40:02 +03002170 psa_cipher_abort( operation );
2171 return( PSA_ERROR_BAD_STATE );
2172 }
2173 if( operation->iv_required && ! operation->iv_set )
2174 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002175 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002176 return( PSA_ERROR_BAD_STATE );
2177 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002178 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2179 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002180 {
Gilles Peskine53514202018-06-06 15:11:46 +02002181 psa_algorithm_t padding_mode =
2182 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002183 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002184 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002185 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002186 return( PSA_ERROR_TAMPERING_DETECTED );
2187 }
Gilles Peskine53514202018-06-06 15:11:46 +02002188 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002189 {
2190 if( operation->ctx.cipher.unprocessed_len != 0 )
2191 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002192 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002193 return( PSA_ERROR_INVALID_ARGUMENT );
2194 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002195 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002196 }
2197
2198 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002199 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002200 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002201 {
2202 psa_cipher_abort( operation );
2203 return( mbedtls_to_psa_error( ret ) );
2204 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002205 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002206 memcpy( output, temp_output_buffer, *output_length );
2207 else
2208 {
2209 psa_cipher_abort( operation );
2210 return( PSA_ERROR_BUFFER_TOO_SMALL );
2211 }
mohammad1603503973b2018-03-12 15:59:30 +02002212
Moran Peker4c80d832018-04-22 20:15:31 +03002213 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002214}
2215
Gilles Peskinee553c652018-06-04 16:22:46 +02002216psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2217{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002218 if( operation->alg == 0 )
2219 return( PSA_SUCCESS );
2220
mohammad1603503973b2018-03-12 15:59:30 +02002221 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002222
Moran Peker41deec42018-04-04 15:43:05 +03002223 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002224 operation->key_set = 0;
2225 operation->iv_set = 0;
2226 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002227 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002228 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002229
Moran Peker395db872018-05-31 14:07:14 +03002230 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002231}
2232
Gilles Peskinea0655c32018-04-30 17:06:50 +02002233
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002234
mohammad16038cc1cee2018-03-28 01:21:33 +03002235/****************************************************************/
2236/* Key Policy */
2237/****************************************************************/
2238
Gilles Peskine2d277862018-06-18 15:41:12 +02002239void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002240{
Gilles Peskine803ce742018-06-18 16:07:14 +02002241 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002242}
2243
Gilles Peskine2d277862018-06-18 15:41:12 +02002244void psa_key_policy_set_usage( psa_key_policy_t *policy,
2245 psa_key_usage_t usage,
2246 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002247{
mohammad16034eed7572018-03-28 05:14:59 -07002248 policy->usage = usage;
2249 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002250}
2251
Gilles Peskine2d277862018-06-18 15:41:12 +02002252psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002253{
mohammad16036df908f2018-04-02 08:34:15 -07002254 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002255}
2256
Gilles Peskine2d277862018-06-18 15:41:12 +02002257psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002258{
mohammad16036df908f2018-04-02 08:34:15 -07002259 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002260}
2261
Gilles Peskine2d277862018-06-18 15:41:12 +02002262psa_status_t psa_set_key_policy( psa_key_slot_t key,
2263 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002264{
2265 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002266
Gilles Peskine828ed142018-06-18 23:25:51 +02002267 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002268 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002269
mohammad16038cc1cee2018-03-28 01:21:33 +03002270 slot = &global_data.key_slots[key];
2271 if( slot->type != PSA_KEY_TYPE_NONE )
2272 return( PSA_ERROR_OCCUPIED_SLOT );
2273
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002274 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2275 PSA_KEY_USAGE_ENCRYPT |
2276 PSA_KEY_USAGE_DECRYPT |
2277 PSA_KEY_USAGE_SIGN |
2278 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002279 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002280
mohammad16036df908f2018-04-02 08:34:15 -07002281 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002282
2283 return( PSA_SUCCESS );
2284}
2285
Gilles Peskine2d277862018-06-18 15:41:12 +02002286psa_status_t psa_get_key_policy( psa_key_slot_t key,
2287 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002288{
2289 key_slot_t *slot;
2290
Gilles Peskine828ed142018-06-18 23:25:51 +02002291 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002292 return( PSA_ERROR_INVALID_ARGUMENT );
2293
2294 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002295
mohammad16036df908f2018-04-02 08:34:15 -07002296 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002297
2298 return( PSA_SUCCESS );
2299}
Gilles Peskine20035e32018-02-03 22:44:14 +01002300
Gilles Peskinea0655c32018-04-30 17:06:50 +02002301
2302
mohammad1603804cd712018-03-20 22:44:08 +02002303/****************************************************************/
2304/* Key Lifetime */
2305/****************************************************************/
2306
Gilles Peskine2d277862018-06-18 15:41:12 +02002307psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2308 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002309{
2310 key_slot_t *slot;
2311
Gilles Peskine828ed142018-06-18 23:25:51 +02002312 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002313 return( PSA_ERROR_INVALID_ARGUMENT );
2314
2315 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002316
mohammad1603804cd712018-03-20 22:44:08 +02002317 *lifetime = slot->lifetime;
2318
2319 return( PSA_SUCCESS );
2320}
2321
Gilles Peskine2d277862018-06-18 15:41:12 +02002322psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
2323 const psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002324{
2325 key_slot_t *slot;
2326
Gilles Peskine828ed142018-06-18 23:25:51 +02002327 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002328 return( PSA_ERROR_INVALID_ARGUMENT );
2329
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002330 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2331 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002332 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2333 return( PSA_ERROR_INVALID_ARGUMENT );
2334
mohammad1603804cd712018-03-20 22:44:08 +02002335 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002336 if( slot->type != PSA_KEY_TYPE_NONE )
2337 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002338
Moran Pekerd7326592018-05-29 16:56:39 +03002339 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002340 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002341
mohammad1603060ad8a2018-03-20 14:28:38 -07002342 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002343
2344 return( PSA_SUCCESS );
2345}
2346
Gilles Peskine20035e32018-02-03 22:44:14 +01002347
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002348
mohammad16035955c982018-04-26 00:53:03 +03002349/****************************************************************/
2350/* AEAD */
2351/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002352
mohammad16035955c982018-04-26 00:53:03 +03002353psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2354 psa_algorithm_t alg,
2355 const uint8_t *nonce,
2356 size_t nonce_length,
2357 const uint8_t *additional_data,
2358 size_t additional_data_length,
2359 const uint8_t *plaintext,
2360 size_t plaintext_length,
2361 uint8_t *ciphertext,
2362 size_t ciphertext_size,
2363 size_t *ciphertext_length )
2364{
2365 int ret;
2366 psa_status_t status;
2367 key_slot_t *slot;
2368 psa_key_type_t key_type;
2369 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002370 uint8_t *tag;
2371 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002372 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002373 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002374
mohammad1603f08a5502018-06-03 15:05:47 +03002375 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002376
mohammad16035955c982018-04-26 00:53:03 +03002377 status = psa_get_key_information( key, &key_type, &key_bits );
2378 if( status != PSA_SUCCESS )
2379 return( status );
2380 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002381 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002382 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002383
mohammad16035ed06212018-06-06 13:09:34 +03002384 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2385 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002386 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002387 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002388
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002389 if( ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) == 0 )
mohammad1603f14394b2018-06-04 14:33:19 +03002390 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002391
Gilles Peskine2d277862018-06-18 15:41:12 +02002392 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2393 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002394 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002395
mohammad16035955c982018-04-26 00:53:03 +03002396 if( alg == PSA_ALG_GCM )
2397 {
2398 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002399 tag_length = 16;
2400
mohammad160396910d82018-06-04 14:33:00 +03002401 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2402 return( PSA_ERROR_INVALID_ARGUMENT );
2403
mohammad160315223a82018-06-03 17:19:55 +03002404 //make sure we have place to hold the tag in the ciphertext buffer
2405 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002406 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002407
2408 //update the tag pointer to point to the end of the ciphertext_length
2409 tag = ciphertext + plaintext_length;
2410
mohammad16035955c982018-04-26 00:53:03 +03002411 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002412 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002413 slot->data.raw.data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002414 key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002415 if( ret != 0 )
2416 {
2417 mbedtls_gcm_free( &gcm );
2418 return( mbedtls_to_psa_error( ret ) );
2419 }
2420 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002421 plaintext_length, nonce,
2422 nonce_length, additional_data,
2423 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002424 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002425 mbedtls_gcm_free( &gcm );
2426 }
2427 else if( alg == PSA_ALG_CCM )
2428 {
2429 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002430 tag_length = 16;
2431
mohammad160396910d82018-06-04 14:33:00 +03002432 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2433 return( PSA_ERROR_INVALID_ARGUMENT );
2434
mohammad160347ddf3d2018-04-26 01:11:21 +03002435 if( nonce_length < 7 || nonce_length > 13 )
2436 return( PSA_ERROR_INVALID_ARGUMENT );
2437
mohammad160315223a82018-06-03 17:19:55 +03002438 //make sure we have place to hold the tag in the ciphertext buffer
2439 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002440 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002441
2442 //update the tag pointer to point to the end of the ciphertext_length
2443 tag = ciphertext + plaintext_length;
2444
mohammad16035955c982018-04-26 00:53:03 +03002445 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002446 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002447 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002448 if( ret != 0 )
2449 {
2450 mbedtls_ccm_free( &ccm );
2451 return( mbedtls_to_psa_error( ret ) );
2452 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002453 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002454 nonce, nonce_length,
2455 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002456 additional_data_length,
2457 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002458 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002459 mbedtls_ccm_free( &ccm );
2460 }
mohammad16035c8845f2018-05-09 05:40:09 -07002461 else
2462 {
mohammad1603554faad2018-06-03 15:07:38 +03002463 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002464 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002465
mohammad160315223a82018-06-03 17:19:55 +03002466 if( ret != 0 )
2467 {
2468 memset( ciphertext, 0, ciphertext_size );
2469 return( mbedtls_to_psa_error( ret ) );
2470 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002471
mohammad160315223a82018-06-03 17:19:55 +03002472 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002473 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002474}
2475
Gilles Peskineee652a32018-06-01 19:23:52 +02002476/* Locate the tag in a ciphertext buffer containing the encrypted data
2477 * followed by the tag. Return the length of the part preceding the tag in
2478 * *plaintext_length. This is the size of the plaintext in modes where
2479 * the encrypted data has the same size as the plaintext, such as
2480 * CCM and GCM. */
2481static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2482 const uint8_t *ciphertext,
2483 size_t ciphertext_length,
2484 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002485 const uint8_t **p_tag )
2486{
2487 size_t payload_length;
2488 if( tag_length > ciphertext_length )
2489 return( PSA_ERROR_INVALID_ARGUMENT );
2490 payload_length = ciphertext_length - tag_length;
2491 if( payload_length > plaintext_size )
2492 return( PSA_ERROR_BUFFER_TOO_SMALL );
2493 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002494 return( PSA_SUCCESS );
2495}
2496
mohammad16035955c982018-04-26 00:53:03 +03002497psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2498 psa_algorithm_t alg,
2499 const uint8_t *nonce,
2500 size_t nonce_length,
2501 const uint8_t *additional_data,
2502 size_t additional_data_length,
2503 const uint8_t *ciphertext,
2504 size_t ciphertext_length,
2505 uint8_t *plaintext,
2506 size_t plaintext_size,
2507 size_t *plaintext_length )
2508{
2509 int ret;
2510 psa_status_t status;
2511 key_slot_t *slot;
2512 psa_key_type_t key_type;
2513 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002514 const uint8_t *tag;
2515 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002516 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002517 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002518
Gilles Peskineee652a32018-06-01 19:23:52 +02002519 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002520
mohammad16035955c982018-04-26 00:53:03 +03002521 status = psa_get_key_information( key, &key_type, &key_bits );
2522 if( status != PSA_SUCCESS )
2523 return( status );
2524 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002525 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002526 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002527
mohammad16035ed06212018-06-06 13:09:34 +03002528 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2529 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002530 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002531 return( PSA_ERROR_NOT_SUPPORTED );
2532
mohammad1603f14394b2018-06-04 14:33:19 +03002533 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2534 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002535
Gilles Peskine2d277862018-06-18 15:41:12 +02002536 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2537 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002538 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002539
mohammad16035955c982018-04-26 00:53:03 +03002540 if( alg == PSA_ALG_GCM )
2541 {
2542 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002543
Gilles Peskineee652a32018-06-01 19:23:52 +02002544 tag_length = 16;
2545 status = psa_aead_unpadded_locate_tag( tag_length,
2546 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002547 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002548 if( status != PSA_SUCCESS )
2549 return( status );
2550
mohammad16035955c982018-04-26 00:53:03 +03002551 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002552 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002553 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002554 if( ret != 0 )
2555 {
2556 mbedtls_gcm_free( &gcm );
2557 return( mbedtls_to_psa_error( ret ) );
2558 }
mohammad16035955c982018-04-26 00:53:03 +03002559
Gilles Peskineee652a32018-06-01 19:23:52 +02002560 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002561 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002562 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002563 additional_data,
2564 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002565 tag, tag_length,
2566 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002567 mbedtls_gcm_free( &gcm );
2568 }
2569 else if( alg == PSA_ALG_CCM )
2570 {
2571 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002572
mohammad160347ddf3d2018-04-26 01:11:21 +03002573 if( nonce_length < 7 || nonce_length > 13 )
2574 return( PSA_ERROR_INVALID_ARGUMENT );
2575
mohammad16039375f842018-06-03 14:28:24 +03002576 tag_length = 16;
2577 status = psa_aead_unpadded_locate_tag( tag_length,
2578 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002579 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002580 if( status != PSA_SUCCESS )
2581 return( status );
2582
mohammad16035955c982018-04-26 00:53:03 +03002583 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002584 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002585 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002586 if( ret != 0 )
2587 {
2588 mbedtls_ccm_free( &ccm );
2589 return( mbedtls_to_psa_error( ret ) );
2590 }
mohammad160360a64d02018-06-03 17:20:42 +03002591 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002592 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002593 additional_data,
2594 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002595 ciphertext, plaintext,
2596 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002597 mbedtls_ccm_free( &ccm );
2598 }
mohammad160339574652018-06-01 04:39:53 -07002599 else
2600 {
mohammad1603554faad2018-06-03 15:07:38 +03002601 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002602 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002603
Gilles Peskineee652a32018-06-01 19:23:52 +02002604 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002605 memset( plaintext, 0, plaintext_size );
2606 else
2607 *plaintext_length = ciphertext_length - tag_length;
2608
Gilles Peskineee652a32018-06-01 19:23:52 +02002609 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002610}
2611
Gilles Peskinea0655c32018-04-30 17:06:50 +02002612
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002613
Gilles Peskine20035e32018-02-03 22:44:14 +01002614/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002615/* Key generation */
2616/****************************************************************/
2617
2618psa_status_t psa_generate_random( uint8_t *output,
2619 size_t output_size )
2620{
2621 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2622 output, output_size );
2623 return( mbedtls_to_psa_error( ret ) );
2624}
2625
2626psa_status_t psa_generate_key( psa_key_slot_t key,
2627 psa_key_type_t type,
2628 size_t bits,
2629 const void *parameters,
2630 size_t parameters_size )
2631{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002632 key_slot_t *slot;
2633
2634 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
2635 return( PSA_ERROR_INVALID_ARGUMENT );
2636 slot = &global_data.key_slots[key];
2637 if( slot->type != PSA_KEY_TYPE_NONE )
2638 return( PSA_ERROR_OCCUPIED_SLOT );
2639 if( parameters == NULL && parameters_size != 0 )
2640 return( PSA_ERROR_INVALID_ARGUMENT );
2641
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002642 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002643 {
2644 psa_status_t status = prepare_raw_data_slot( type, bits,
2645 &slot->data.raw );
2646 if( status != PSA_SUCCESS )
2647 return( status );
2648 status = psa_generate_random( slot->data.raw.data,
2649 slot->data.raw.bytes );
2650 if( status != PSA_SUCCESS )
2651 {
2652 mbedtls_free( slot->data.raw.data );
2653 return( status );
2654 }
2655#if defined(MBEDTLS_DES_C)
2656 if( type == PSA_KEY_TYPE_DES )
2657 {
2658 mbedtls_des_key_set_parity( slot->data.raw.data );
2659 if( slot->data.raw.bytes >= 16 )
2660 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2661 if( slot->data.raw.bytes == 24 )
2662 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2663 }
2664#endif /* MBEDTLS_DES_C */
2665 }
2666 else
2667
2668#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2669 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2670 {
2671 mbedtls_rsa_context *rsa;
2672 int ret;
2673 int exponent = 65537;
2674 if( parameters != NULL )
2675 {
2676 const unsigned *p = parameters;
2677 if( parameters_size != sizeof( *p ) )
2678 return( PSA_ERROR_INVALID_ARGUMENT );
2679 if( *p > INT_MAX )
2680 return( PSA_ERROR_INVALID_ARGUMENT );
2681 exponent = *p;
2682 }
2683 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
2684 if( rsa == NULL )
2685 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2686 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
2687 ret = mbedtls_rsa_gen_key( rsa,
2688 mbedtls_ctr_drbg_random,
2689 &global_data.ctr_drbg,
2690 bits,
2691 exponent );
2692 if( ret != 0 )
2693 {
2694 mbedtls_rsa_free( rsa );
2695 mbedtls_free( rsa );
2696 return( mbedtls_to_psa_error( ret ) );
2697 }
2698 slot->data.rsa = rsa;
2699 }
2700 else
2701#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
2702
2703#if defined(MBEDTLS_ECP_C)
2704 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
2705 {
2706 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
2707 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
2708 const mbedtls_ecp_curve_info *curve_info =
2709 mbedtls_ecp_curve_info_from_grp_id( grp_id );
2710 mbedtls_ecp_keypair *ecp;
2711 int ret;
2712 if( parameters != NULL )
2713 return( PSA_ERROR_NOT_SUPPORTED );
2714 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
2715 return( PSA_ERROR_NOT_SUPPORTED );
2716 if( curve_info->bit_size != bits )
2717 return( PSA_ERROR_INVALID_ARGUMENT );
2718 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
2719 if( ecp == NULL )
2720 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2721 mbedtls_ecp_keypair_init( ecp );
2722 ret = mbedtls_ecp_gen_key( grp_id, ecp,
2723 mbedtls_ctr_drbg_random,
2724 &global_data.ctr_drbg );
2725 if( ret != 0 )
2726 {
2727 mbedtls_ecp_keypair_free( ecp );
2728 mbedtls_free( ecp );
2729 return( mbedtls_to_psa_error( ret ) );
2730 }
2731 slot->data.ecp = ecp;
2732 }
2733 else
2734#endif /* MBEDTLS_ECP_C */
2735
2736 return( PSA_ERROR_NOT_SUPPORTED );
2737
2738 slot->type = type;
2739 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02002740}
2741
2742
2743/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002744/* Module setup */
2745/****************************************************************/
2746
Gilles Peskinee59236f2018-01-27 23:32:46 +01002747void mbedtls_psa_crypto_free( void )
2748{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002749 size_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02002750 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002751 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002752 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2753 mbedtls_entropy_free( &global_data.entropy );
2754 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2755}
2756
2757psa_status_t psa_crypto_init( void )
2758{
2759 int ret;
2760 const unsigned char drbg_seed[] = "PSA";
2761
2762 if( global_data.initialized != 0 )
2763 return( PSA_SUCCESS );
2764
2765 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2766 mbedtls_entropy_init( &global_data.entropy );
2767 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2768
2769 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2770 mbedtls_entropy_func,
2771 &global_data.entropy,
2772 drbg_seed, sizeof( drbg_seed ) - 1 );
2773 if( ret != 0 )
2774 goto exit;
2775
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002776 global_data.initialized = 1;
2777
Gilles Peskinee59236f2018-01-27 23:32:46 +01002778exit:
2779 if( ret != 0 )
2780 mbedtls_psa_crypto_free( );
2781 return( mbedtls_to_psa_error( ret ) );
2782}
2783
2784#endif /* MBEDTLS_PSA_CRYPTO_C */