blob: adcadf3f568dfb41e231396684b558a9f086db8a [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 Peskine2d277862018-06-18 15:41:12 +0200119typedef struct
120{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100121 int initialized;
122 mbedtls_entropy_context entropy;
123 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200124 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100125} psa_global_data_t;
126
127static psa_global_data_t global_data;
128
129static psa_status_t mbedtls_to_psa_error( int ret )
130{
Gilles Peskinea5905292018-02-07 20:59:33 +0100131 /* If there's both a high-level code and low-level code, dispatch on
132 * the high-level code. */
133 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100134 {
135 case 0:
136 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100137
138 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
139 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
140 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
141 return( PSA_ERROR_NOT_SUPPORTED );
142 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
145 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
Gilles Peskine9a944802018-06-21 09:35:35 +0200148 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
149 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
150 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
151 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
152 case MBEDTLS_ERR_ASN1_INVALID_DATA:
153 return( PSA_ERROR_INVALID_ARGUMENT );
154 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
155 return( PSA_ERROR_INSUFFICIENT_MEMORY );
156 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
157 return( PSA_ERROR_BUFFER_TOO_SMALL );
158
Gilles Peskinea5905292018-02-07 20:59:33 +0100159 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
160 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
161 return( PSA_ERROR_NOT_SUPPORTED );
162 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
163 return( PSA_ERROR_HARDWARE_FAILURE );
164
165 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
166 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
171 case MBEDTLS_ERR_CCM_BAD_INPUT:
172 return( PSA_ERROR_INVALID_ARGUMENT );
173 case MBEDTLS_ERR_CCM_AUTH_FAILED:
174 return( PSA_ERROR_INVALID_SIGNATURE );
175 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
178 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
179 return( PSA_ERROR_NOT_SUPPORTED );
180 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
181 return( PSA_ERROR_INVALID_ARGUMENT );
182 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
183 return( PSA_ERROR_INSUFFICIENT_MEMORY );
184 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
185 return( PSA_ERROR_INVALID_PADDING );
186 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
187 return( PSA_ERROR_BAD_STATE );
188 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
189 return( PSA_ERROR_INVALID_SIGNATURE );
190 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
191 return( PSA_ERROR_TAMPERING_DETECTED );
192 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
193 return( PSA_ERROR_HARDWARE_FAILURE );
194
195 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
196 return( PSA_ERROR_HARDWARE_FAILURE );
197
198 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
199 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
200 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
201 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
202 return( PSA_ERROR_NOT_SUPPORTED );
203 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
204 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
205
206 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
Gilles Peskinee59236f2018-01-27 23:32:46 +0100211 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
212 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
213 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100215
216 case MBEDTLS_ERR_GCM_AUTH_FAILED:
217 return( PSA_ERROR_INVALID_SIGNATURE );
218 case MBEDTLS_ERR_GCM_BAD_INPUT:
219 return( PSA_ERROR_NOT_SUPPORTED );
220 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
221 return( PSA_ERROR_HARDWARE_FAILURE );
222
223 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
224 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
225 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
228 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
229 return( PSA_ERROR_NOT_SUPPORTED );
230 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
231 return( PSA_ERROR_INVALID_ARGUMENT );
232 case MBEDTLS_ERR_MD_ALLOC_FAILED:
233 return( PSA_ERROR_INSUFFICIENT_MEMORY );
234 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
235 return( PSA_ERROR_STORAGE_FAILURE );
236 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
237 return( PSA_ERROR_HARDWARE_FAILURE );
238
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100239 case MBEDTLS_ERR_PK_ALLOC_FAILED:
240 return( PSA_ERROR_INSUFFICIENT_MEMORY );
241 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
242 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
243 return( PSA_ERROR_INVALID_ARGUMENT );
244 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100246 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
247 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
250 return( PSA_ERROR_NOT_SUPPORTED );
251 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
252 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
253 return( PSA_ERROR_NOT_PERMITTED );
254 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
255 return( PSA_ERROR_INVALID_ARGUMENT );
256 case MBEDTLS_ERR_PK_INVALID_ALG:
257 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
258 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
259 return( PSA_ERROR_NOT_SUPPORTED );
260 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
261 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100262 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
263 return( PSA_ERROR_HARDWARE_FAILURE );
264
265 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
266 return( PSA_ERROR_HARDWARE_FAILURE );
267
268 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_RSA_INVALID_PADDING:
271 return( PSA_ERROR_INVALID_PADDING );
272 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
273 return( PSA_ERROR_HARDWARE_FAILURE );
274 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
277 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
278 return( PSA_ERROR_TAMPERING_DETECTED );
279 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
280 return( PSA_ERROR_INVALID_SIGNATURE );
281 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
282 return( PSA_ERROR_BUFFER_TOO_SMALL );
283 case MBEDTLS_ERR_RSA_RNG_FAILED:
284 return( PSA_ERROR_INSUFFICIENT_MEMORY );
285 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
286 return( PSA_ERROR_NOT_SUPPORTED );
287 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
288 return( PSA_ERROR_HARDWARE_FAILURE );
289
290 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
291 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
292 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
293 return( PSA_ERROR_HARDWARE_FAILURE );
294
295 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
itayzafrir5c753392018-05-08 11:18:38 +0300300 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300301 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300302 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300303 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
304 return( PSA_ERROR_BUFFER_TOO_SMALL );
305 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
306 return( PSA_ERROR_NOT_SUPPORTED );
307 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
308 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
309 return( PSA_ERROR_INVALID_SIGNATURE );
310 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
311 return( PSA_ERROR_INSUFFICIENT_MEMORY );
312 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300314
Gilles Peskinee59236f2018-01-27 23:32:46 +0100315 default:
316 return( PSA_ERROR_UNKNOWN_ERROR );
317 }
318}
319
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200320
321
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100322/****************************************************************/
323/* Key management */
324/****************************************************************/
325
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200326static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
327{
328 switch( grpid )
329 {
330 case MBEDTLS_ECP_DP_SECP192R1:
331 return( PSA_ECC_CURVE_SECP192R1 );
332 case MBEDTLS_ECP_DP_SECP224R1:
333 return( PSA_ECC_CURVE_SECP224R1 );
334 case MBEDTLS_ECP_DP_SECP256R1:
335 return( PSA_ECC_CURVE_SECP256R1 );
336 case MBEDTLS_ECP_DP_SECP384R1:
337 return( PSA_ECC_CURVE_SECP384R1 );
338 case MBEDTLS_ECP_DP_SECP521R1:
339 return( PSA_ECC_CURVE_SECP521R1 );
340 case MBEDTLS_ECP_DP_BP256R1:
341 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
342 case MBEDTLS_ECP_DP_BP384R1:
343 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
344 case MBEDTLS_ECP_DP_BP512R1:
345 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
346 case MBEDTLS_ECP_DP_CURVE25519:
347 return( PSA_ECC_CURVE_CURVE25519 );
348 case MBEDTLS_ECP_DP_SECP192K1:
349 return( PSA_ECC_CURVE_SECP192K1 );
350 case MBEDTLS_ECP_DP_SECP224K1:
351 return( PSA_ECC_CURVE_SECP224K1 );
352 case MBEDTLS_ECP_DP_SECP256K1:
353 return( PSA_ECC_CURVE_SECP256K1 );
354 case MBEDTLS_ECP_DP_CURVE448:
355 return( PSA_ECC_CURVE_CURVE448 );
356 default:
357 return( 0 );
358 }
359}
360
Gilles Peskine12313cd2018-06-20 00:20:32 +0200361static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
362{
363 switch( curve )
364 {
365 case PSA_ECC_CURVE_SECP192R1:
366 return( MBEDTLS_ECP_DP_SECP192R1 );
367 case PSA_ECC_CURVE_SECP224R1:
368 return( MBEDTLS_ECP_DP_SECP224R1 );
369 case PSA_ECC_CURVE_SECP256R1:
370 return( MBEDTLS_ECP_DP_SECP256R1 );
371 case PSA_ECC_CURVE_SECP384R1:
372 return( MBEDTLS_ECP_DP_SECP384R1 );
373 case PSA_ECC_CURVE_SECP521R1:
374 return( MBEDTLS_ECP_DP_SECP521R1 );
375 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
376 return( MBEDTLS_ECP_DP_BP256R1 );
377 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
378 return( MBEDTLS_ECP_DP_BP384R1 );
379 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
380 return( MBEDTLS_ECP_DP_BP512R1 );
381 case PSA_ECC_CURVE_CURVE25519:
382 return( MBEDTLS_ECP_DP_CURVE25519 );
383 case PSA_ECC_CURVE_SECP192K1:
384 return( MBEDTLS_ECP_DP_SECP192K1 );
385 case PSA_ECC_CURVE_SECP224K1:
386 return( MBEDTLS_ECP_DP_SECP224K1 );
387 case PSA_ECC_CURVE_SECP256K1:
388 return( MBEDTLS_ECP_DP_SECP256K1 );
389 case PSA_ECC_CURVE_CURVE448:
390 return( MBEDTLS_ECP_DP_CURVE448 );
391 default:
392 return( MBEDTLS_ECP_DP_NONE );
393 }
394}
395
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200396static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
397 size_t bits,
398 struct raw_data *raw )
399{
400 /* Check that the bit size is acceptable for the key type */
401 switch( type )
402 {
403 case PSA_KEY_TYPE_RAW_DATA:
404#if defined(MBEDTLS_MD_C)
405 case PSA_KEY_TYPE_HMAC:
406#endif
407 break;
408#if defined(MBEDTLS_AES_C)
409 case PSA_KEY_TYPE_AES:
410 if( bits != 128 && bits != 192 && bits != 256 )
411 return( PSA_ERROR_INVALID_ARGUMENT );
412 break;
413#endif
414#if defined(MBEDTLS_CAMELLIA_C)
415 case PSA_KEY_TYPE_CAMELLIA:
416 if( bits != 128 && bits != 192 && bits != 256 )
417 return( PSA_ERROR_INVALID_ARGUMENT );
418 break;
419#endif
420#if defined(MBEDTLS_DES_C)
421 case PSA_KEY_TYPE_DES:
422 if( bits != 64 && bits != 128 && bits != 192 )
423 return( PSA_ERROR_INVALID_ARGUMENT );
424 break;
425#endif
426#if defined(MBEDTLS_ARC4_C)
427 case PSA_KEY_TYPE_ARC4:
428 if( bits < 8 || bits > 2048 )
429 return( PSA_ERROR_INVALID_ARGUMENT );
430 break;
431#endif
432 default:
433 return( PSA_ERROR_NOT_SUPPORTED );
434 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200435 if( bits % 8 != 0 )
436 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200437
438 /* Allocate memory for the key */
439 raw->bytes = PSA_BITS_TO_BYTES( bits );
440 raw->data = mbedtls_calloc( 1, raw->bytes );
441 if( raw->data == NULL )
442 {
443 raw->bytes = 0;
444 return( PSA_ERROR_INSUFFICIENT_MEMORY );
445 }
446 return( PSA_SUCCESS );
447}
448
Gilles Peskine2d277862018-06-18 15:41:12 +0200449psa_status_t psa_import_key( psa_key_slot_t key,
450 psa_key_type_t type,
451 const uint8_t *data,
452 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100453{
454 key_slot_t *slot;
455
Gilles Peskine828ed142018-06-18 23:25:51 +0200456 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100457 return( PSA_ERROR_INVALID_ARGUMENT );
458 slot = &global_data.key_slots[key];
459 if( slot->type != PSA_KEY_TYPE_NONE )
460 return( PSA_ERROR_OCCUPIED_SLOT );
461
Gilles Peskine8c9def32018-02-08 10:02:12 +0100462 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100463 {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200464 psa_status_t status;
Gilles Peskine6d912132018-03-07 16:41:37 +0100465 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100466 if( data_length > SIZE_MAX / 8 )
467 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200468 status = prepare_raw_data_slot( type,
469 PSA_BYTES_TO_BITS( data_length ),
470 &slot->data.raw );
471 if( status != PSA_SUCCESS )
472 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100473 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100474 }
475 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100476#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100477 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
478 type == PSA_KEY_TYPE_RSA_KEYPAIR ||
479 PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100480 {
481 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100482 mbedtls_pk_context pk;
483 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100484 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
485 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
486 else
487 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100488 if( ret != 0 )
489 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100490 switch( mbedtls_pk_get_type( &pk ) )
491 {
492#if defined(MBEDTLS_RSA_C)
493 case MBEDTLS_PK_RSA:
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100494 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
495 type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200496 slot->data.rsa = mbedtls_pk_rsa( pk );
Gilles Peskine969ac722018-01-28 18:16:59 +0100497 else
498 return( PSA_ERROR_INVALID_ARGUMENT );
499 break;
500#endif /* MBEDTLS_RSA_C */
501#if defined(MBEDTLS_ECP_C)
502 case MBEDTLS_PK_ECKEY:
503 if( PSA_KEY_TYPE_IS_ECC( type ) )
504 {
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200505 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
506 psa_ecc_curve_t actual_curve =
507 mbedtls_ecc_group_to_psa( ecp->grp.id );
508 psa_ecc_curve_t expected_curve =
509 PSA_KEY_TYPE_GET_CURVE( type );
510 if( actual_curve != expected_curve )
511 return( PSA_ERROR_INVALID_ARGUMENT );
512 slot->data.ecp = ecp;
Gilles Peskine969ac722018-01-28 18:16:59 +0100513 }
514 else
515 return( PSA_ERROR_INVALID_ARGUMENT );
516 break;
517#endif /* MBEDTLS_ECP_C */
518 default:
519 return( PSA_ERROR_INVALID_ARGUMENT );
520 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100521 }
522 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100523#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100524 {
525 return( PSA_ERROR_NOT_SUPPORTED );
526 }
527
528 slot->type = type;
529 return( PSA_SUCCESS );
530}
531
Gilles Peskine2d277862018-06-18 15:41:12 +0200532psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100533{
534 key_slot_t *slot;
535
Gilles Peskine828ed142018-06-18 23:25:51 +0200536 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100537 return( PSA_ERROR_INVALID_ARGUMENT );
538 slot = &global_data.key_slots[key];
539 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200540 {
541 /* No key material to clean, but do zeroize the slot below to wipe
542 * metadata such as policies. */
543 }
544 else if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 {
546 mbedtls_free( slot->data.raw.data );
547 }
548 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100549#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100550 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
551 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100553 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100554 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100555 }
556 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100557#endif /* defined(MBEDTLS_RSA_C) */
558#if defined(MBEDTLS_ECP_C)
559 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
560 {
561 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100562 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100563 }
564 else
565#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100566 {
567 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100568 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569 return( PSA_ERROR_TAMPERING_DETECTED );
570 }
571
572 mbedtls_zeroize( slot, sizeof( *slot ) );
573 return( PSA_SUCCESS );
574}
575
Gilles Peskine2d277862018-06-18 15:41:12 +0200576psa_status_t psa_get_key_information( psa_key_slot_t key,
577 psa_key_type_t *type,
578 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100579{
580 key_slot_t *slot;
581
Gilles Peskine828ed142018-06-18 23:25:51 +0200582 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100583 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584 slot = &global_data.key_slots[key];
585 if( type != NULL )
586 *type = slot->type;
587 if( bits != NULL )
588 *bits = 0;
589 if( slot->type == PSA_KEY_TYPE_NONE )
590 return( PSA_ERROR_EMPTY_SLOT );
591
Gilles Peskine8c9def32018-02-08 10:02:12 +0100592 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100593 {
594 if( bits != NULL )
595 *bits = slot->data.raw.bytes * 8;
596 }
597 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100598#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100599 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
600 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 {
602 if( bits != NULL )
Gilles Peskine969ac722018-01-28 18:16:59 +0100603 *bits = mbedtls_rsa_get_bitlen( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100604 }
605 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100606#endif /* defined(MBEDTLS_RSA_C) */
607#if defined(MBEDTLS_ECP_C)
608 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
609 {
610 if( bits != NULL )
611 *bits = slot->data.ecp->grp.pbits;
612 }
613 else
614#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100615 {
616 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100617 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618 return( PSA_ERROR_TAMPERING_DETECTED );
619 }
620
621 return( PSA_SUCCESS );
622}
623
Gilles Peskine2d277862018-06-18 15:41:12 +0200624static psa_status_t psa_internal_export_key( psa_key_slot_t key,
625 uint8_t *data,
626 size_t data_size,
627 size_t *data_length,
628 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100629{
630 key_slot_t *slot;
631
Gilles Peskine828ed142018-06-18 23:25:51 +0200632 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100633 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634 slot = &global_data.key_slots[key];
635 if( slot->type == PSA_KEY_TYPE_NONE )
636 return( PSA_ERROR_EMPTY_SLOT );
637
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200638 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300639 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300640
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200641 if( ! export_public_key &&
642 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) &&
643 ( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) == 0 )
Moran Peker87567632018-06-04 18:41:37 +0300644 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine2d277862018-06-18 15:41:12 +0200645
Gilles Peskine8c9def32018-02-08 10:02:12 +0100646 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647 {
648 if( slot->data.raw.bytes > data_size )
649 return( PSA_ERROR_BUFFER_TOO_SMALL );
650 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
651 *data_length = slot->data.raw.bytes;
652 return( PSA_SUCCESS );
653 }
654 else
Moran Peker17e36e12018-05-02 12:55:20 +0300655 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100656#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100657 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
Moran Pekera998bc62018-04-16 18:16:20 +0300658 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
659 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100660 {
Moran Pekera998bc62018-04-16 18:16:20 +0300661 mbedtls_pk_context pk;
662 int ret;
663 mbedtls_pk_init( &pk );
664 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
665 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
666 {
667 pk.pk_info = &mbedtls_rsa_info;
668 pk.pk_ctx = slot->data.rsa;
669 }
670 else
671 {
672 pk.pk_info = &mbedtls_eckey_info;
673 pk.pk_ctx = slot->data.ecp;
674 }
Moran Pekerd7326592018-05-29 16:56:39 +0300675 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300676 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300677 else
678 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300679 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200680 {
681 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300682 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200683 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200684 /* The mbedtls_pk_xxx functions write to the end of the buffer.
685 * Move the data to the beginning and erase remaining data
686 * at the original location. */
687 if( 2 * (size_t) ret <= data_size )
688 {
689 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200690 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200691 }
692 else if( (size_t) ret < data_size )
693 {
694 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200695 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200696 }
Moran Pekera998bc62018-04-16 18:16:20 +0300697 *data_length = ret;
698 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100699 }
700 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100701#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300702 {
703 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200704 it is valid for a special-purpose implementation to omit
705 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300706 return( PSA_ERROR_NOT_SUPPORTED );
707 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 }
709}
710
Gilles Peskine2d277862018-06-18 15:41:12 +0200711psa_status_t psa_export_key( psa_key_slot_t key,
712 uint8_t *data,
713 size_t data_size,
714 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300715{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200716 return( psa_internal_export_key( key, data, data_size,
717 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100718}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100719
Gilles Peskine2d277862018-06-18 15:41:12 +0200720psa_status_t psa_export_public_key( psa_key_slot_t key,
721 uint8_t *data,
722 size_t data_size,
723 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300724{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200725 return( psa_internal_export_key( key, data, data_size,
726 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300727}
728
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200729
730
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100731/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100732/* Message digests */
733/****************************************************************/
734
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100735static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100736{
737 switch( alg )
738 {
739#if defined(MBEDTLS_MD2_C)
740 case PSA_ALG_MD2:
741 return( &mbedtls_md2_info );
742#endif
743#if defined(MBEDTLS_MD4_C)
744 case PSA_ALG_MD4:
745 return( &mbedtls_md4_info );
746#endif
747#if defined(MBEDTLS_MD5_C)
748 case PSA_ALG_MD5:
749 return( &mbedtls_md5_info );
750#endif
751#if defined(MBEDTLS_RIPEMD160_C)
752 case PSA_ALG_RIPEMD160:
753 return( &mbedtls_ripemd160_info );
754#endif
755#if defined(MBEDTLS_SHA1_C)
756 case PSA_ALG_SHA_1:
757 return( &mbedtls_sha1_info );
758#endif
759#if defined(MBEDTLS_SHA256_C)
760 case PSA_ALG_SHA_224:
761 return( &mbedtls_sha224_info );
762 case PSA_ALG_SHA_256:
763 return( &mbedtls_sha256_info );
764#endif
765#if defined(MBEDTLS_SHA512_C)
766 case PSA_ALG_SHA_384:
767 return( &mbedtls_sha384_info );
768 case PSA_ALG_SHA_512:
769 return( &mbedtls_sha512_info );
770#endif
771 default:
772 return( NULL );
773 }
774}
775
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100776psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
777{
778 switch( operation->alg )
779 {
780#if defined(MBEDTLS_MD2_C)
781 case PSA_ALG_MD2:
782 mbedtls_md2_free( &operation->ctx.md2 );
783 break;
784#endif
785#if defined(MBEDTLS_MD4_C)
786 case PSA_ALG_MD4:
787 mbedtls_md4_free( &operation->ctx.md4 );
788 break;
789#endif
790#if defined(MBEDTLS_MD5_C)
791 case PSA_ALG_MD5:
792 mbedtls_md5_free( &operation->ctx.md5 );
793 break;
794#endif
795#if defined(MBEDTLS_RIPEMD160_C)
796 case PSA_ALG_RIPEMD160:
797 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
798 break;
799#endif
800#if defined(MBEDTLS_SHA1_C)
801 case PSA_ALG_SHA_1:
802 mbedtls_sha1_free( &operation->ctx.sha1 );
803 break;
804#endif
805#if defined(MBEDTLS_SHA256_C)
806 case PSA_ALG_SHA_224:
807 case PSA_ALG_SHA_256:
808 mbedtls_sha256_free( &operation->ctx.sha256 );
809 break;
810#endif
811#if defined(MBEDTLS_SHA512_C)
812 case PSA_ALG_SHA_384:
813 case PSA_ALG_SHA_512:
814 mbedtls_sha512_free( &operation->ctx.sha512 );
815 break;
816#endif
817 default:
818 return( PSA_ERROR_NOT_SUPPORTED );
819 }
820 operation->alg = 0;
821 return( PSA_SUCCESS );
822}
823
824psa_status_t psa_hash_start( psa_hash_operation_t *operation,
825 psa_algorithm_t alg )
826{
827 int ret;
828 operation->alg = 0;
829 switch( alg )
830 {
831#if defined(MBEDTLS_MD2_C)
832 case PSA_ALG_MD2:
833 mbedtls_md2_init( &operation->ctx.md2 );
834 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
835 break;
836#endif
837#if defined(MBEDTLS_MD4_C)
838 case PSA_ALG_MD4:
839 mbedtls_md4_init( &operation->ctx.md4 );
840 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
841 break;
842#endif
843#if defined(MBEDTLS_MD5_C)
844 case PSA_ALG_MD5:
845 mbedtls_md5_init( &operation->ctx.md5 );
846 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
847 break;
848#endif
849#if defined(MBEDTLS_RIPEMD160_C)
850 case PSA_ALG_RIPEMD160:
851 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
852 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
853 break;
854#endif
855#if defined(MBEDTLS_SHA1_C)
856 case PSA_ALG_SHA_1:
857 mbedtls_sha1_init( &operation->ctx.sha1 );
858 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
859 break;
860#endif
861#if defined(MBEDTLS_SHA256_C)
862 case PSA_ALG_SHA_224:
863 mbedtls_sha256_init( &operation->ctx.sha256 );
864 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
865 break;
866 case PSA_ALG_SHA_256:
867 mbedtls_sha256_init( &operation->ctx.sha256 );
868 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
869 break;
870#endif
871#if defined(MBEDTLS_SHA512_C)
872 case PSA_ALG_SHA_384:
873 mbedtls_sha512_init( &operation->ctx.sha512 );
874 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
875 break;
876 case PSA_ALG_SHA_512:
877 mbedtls_sha512_init( &operation->ctx.sha512 );
878 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
879 break;
880#endif
881 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +0200882 return( PSA_ALG_IS_HASH( alg ) ?
883 PSA_ERROR_NOT_SUPPORTED :
884 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100885 }
886 if( ret == 0 )
887 operation->alg = alg;
888 else
889 psa_hash_abort( operation );
890 return( mbedtls_to_psa_error( ret ) );
891}
892
893psa_status_t psa_hash_update( psa_hash_operation_t *operation,
894 const uint8_t *input,
895 size_t input_length )
896{
897 int ret;
898 switch( operation->alg )
899 {
900#if defined(MBEDTLS_MD2_C)
901 case PSA_ALG_MD2:
902 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
903 input, input_length );
904 break;
905#endif
906#if defined(MBEDTLS_MD4_C)
907 case PSA_ALG_MD4:
908 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
909 input, input_length );
910 break;
911#endif
912#if defined(MBEDTLS_MD5_C)
913 case PSA_ALG_MD5:
914 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
915 input, input_length );
916 break;
917#endif
918#if defined(MBEDTLS_RIPEMD160_C)
919 case PSA_ALG_RIPEMD160:
920 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
921 input, input_length );
922 break;
923#endif
924#if defined(MBEDTLS_SHA1_C)
925 case PSA_ALG_SHA_1:
926 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
927 input, input_length );
928 break;
929#endif
930#if defined(MBEDTLS_SHA256_C)
931 case PSA_ALG_SHA_224:
932 case PSA_ALG_SHA_256:
933 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
934 input, input_length );
935 break;
936#endif
937#if defined(MBEDTLS_SHA512_C)
938 case PSA_ALG_SHA_384:
939 case PSA_ALG_SHA_512:
940 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
941 input, input_length );
942 break;
943#endif
944 default:
945 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
946 break;
947 }
948 if( ret != 0 )
949 psa_hash_abort( operation );
950 return( mbedtls_to_psa_error( ret ) );
951}
952
953psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
954 uint8_t *hash,
955 size_t hash_size,
956 size_t *hash_length )
957{
958 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200959 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100960
961 /* Fill the output buffer with something that isn't a valid hash
962 * (barring an attack on the hash and deliberately-crafted input),
963 * in case the caller doesn't check the return status properly. */
964 *hash_length = actual_hash_length;
965 memset( hash, '!', hash_size );
966
967 if( hash_size < actual_hash_length )
968 return( PSA_ERROR_BUFFER_TOO_SMALL );
969
970 switch( operation->alg )
971 {
972#if defined(MBEDTLS_MD2_C)
973 case PSA_ALG_MD2:
974 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
975 break;
976#endif
977#if defined(MBEDTLS_MD4_C)
978 case PSA_ALG_MD4:
979 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
980 break;
981#endif
982#if defined(MBEDTLS_MD5_C)
983 case PSA_ALG_MD5:
984 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
985 break;
986#endif
987#if defined(MBEDTLS_RIPEMD160_C)
988 case PSA_ALG_RIPEMD160:
989 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
990 break;
991#endif
992#if defined(MBEDTLS_SHA1_C)
993 case PSA_ALG_SHA_1:
994 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
995 break;
996#endif
997#if defined(MBEDTLS_SHA256_C)
998 case PSA_ALG_SHA_224:
999 case PSA_ALG_SHA_256:
1000 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1001 break;
1002#endif
1003#if defined(MBEDTLS_SHA512_C)
1004 case PSA_ALG_SHA_384:
1005 case PSA_ALG_SHA_512:
1006 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1007 break;
1008#endif
1009 default:
1010 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1011 break;
1012 }
1013
1014 if( ret == 0 )
1015 {
1016 return( psa_hash_abort( operation ) );
1017 }
1018 else
1019 {
1020 psa_hash_abort( operation );
1021 return( mbedtls_to_psa_error( ret ) );
1022 }
1023}
1024
Gilles Peskine2d277862018-06-18 15:41:12 +02001025psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1026 const uint8_t *hash,
1027 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001028{
1029 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1030 size_t actual_hash_length;
1031 psa_status_t status = psa_hash_finish( operation,
1032 actual_hash, sizeof( actual_hash ),
1033 &actual_hash_length );
1034 if( status != PSA_SUCCESS )
1035 return( status );
1036 if( actual_hash_length != hash_length )
1037 return( PSA_ERROR_INVALID_SIGNATURE );
1038 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1039 return( PSA_ERROR_INVALID_SIGNATURE );
1040 return( PSA_SUCCESS );
1041}
1042
1043
1044
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001045/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001046/* MAC */
1047/****************************************************************/
1048
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001049static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001050 psa_algorithm_t alg,
1051 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001052 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001053 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001054{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001055 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001056 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001057
1058 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1059 {
1060 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001061 {
1062 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1063 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001064
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001065 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001066 {
1067 case PSA_ALG_STREAM_CIPHER:
1068 mode = MBEDTLS_MODE_STREAM;
1069 break;
1070 case PSA_ALG_CBC_BASE:
1071 mode = MBEDTLS_MODE_CBC;
1072 break;
1073 case PSA_ALG_CFB_BASE:
1074 mode = MBEDTLS_MODE_CFB;
1075 break;
1076 case PSA_ALG_OFB_BASE:
1077 mode = MBEDTLS_MODE_OFB;
1078 break;
1079 case PSA_ALG_CTR:
1080 mode = MBEDTLS_MODE_CTR;
1081 break;
1082 case PSA_ALG_CCM:
1083 mode = MBEDTLS_MODE_CCM;
1084 break;
1085 case PSA_ALG_GCM:
1086 mode = MBEDTLS_MODE_GCM;
1087 break;
1088 default:
1089 return( NULL );
1090 }
1091 }
1092 else if( alg == PSA_ALG_CMAC )
1093 mode = MBEDTLS_MODE_ECB;
1094 else if( alg == PSA_ALG_GMAC )
1095 mode = MBEDTLS_MODE_GCM;
1096 else
1097 return( NULL );
1098
1099 switch( key_type )
1100 {
1101 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001102 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001103 break;
1104 case PSA_KEY_TYPE_DES:
1105 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001106 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001107 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001108 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001109 break;
1110 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001111 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001112 break;
1113 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001114 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001115 break;
1116 default:
1117 return( NULL );
1118 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001119 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001120 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001121
mohammad1603f4f0d612018-06-03 15:04:51 +03001122 return( mbedtls_cipher_info_from_values( cipher_id_tmp, key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001123}
1124
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001125static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001126{
Gilles Peskine2d277862018-06-18 15:41:12 +02001127 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001128 {
1129 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001130 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001131 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001132 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001133 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001134 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001135 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001136 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001137 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001138 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001139 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001140 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001141 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001142 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001143 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001144 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001145 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001146 return( 128 );
1147 default:
1148 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001149 }
1150}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001151
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001152/* Initialize the MAC operation structure. Once this function has been
1153 * called, psa_mac_abort can run and will do the right thing. */
1154static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1155 psa_algorithm_t alg )
1156{
1157 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1158
1159 operation->alg = alg;
1160 operation->key_set = 0;
1161 operation->iv_set = 0;
1162 operation->iv_required = 0;
1163 operation->has_input = 0;
1164 operation->key_usage_sign = 0;
1165 operation->key_usage_verify = 0;
1166
1167#if defined(MBEDTLS_CMAC_C)
1168 if( alg == PSA_ALG_CMAC )
1169 {
1170 operation->iv_required = 0;
1171 mbedtls_cipher_init( &operation->ctx.cmac );
1172 status = PSA_SUCCESS;
1173 }
1174 else
1175#endif /* MBEDTLS_CMAC_C */
1176#if defined(MBEDTLS_MD_C)
1177 if( PSA_ALG_IS_HMAC( operation->alg ) )
1178 {
1179 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1180 PSA_ALG_HMAC_HASH( alg ) );
1181 }
1182 else
1183#endif /* MBEDTLS_MD_C */
1184 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001185 if( ! PSA_ALG_IS_MAC( alg ) )
1186 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001187 }
1188
1189 if( status != PSA_SUCCESS )
1190 memset( operation, 0, sizeof( *operation ) );
1191 return( status );
1192}
1193
Gilles Peskine8c9def32018-02-08 10:02:12 +01001194psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1195{
1196 switch( operation->alg )
1197 {
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001198 case 0:
1199 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001200#if defined(MBEDTLS_CMAC_C)
1201 case PSA_ALG_CMAC:
1202 mbedtls_cipher_free( &operation->ctx.cmac );
1203 break;
1204#endif /* MBEDTLS_CMAC_C */
1205 default:
1206#if defined(MBEDTLS_MD_C)
1207 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001208 {
Gilles Peskine99bc6492018-06-11 17:13:00 +02001209 unsigned int block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001210 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001211
Gilles Peskine99bc6492018-06-11 17:13:00 +02001212 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001213 return( PSA_ERROR_NOT_SUPPORTED );
1214
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001215 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001216 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001217 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001218 else
1219#endif /* MBEDTLS_MD_C */
1220 return( PSA_ERROR_NOT_SUPPORTED );
1221 }
Moran Peker41deec42018-04-04 15:43:05 +03001222
Gilles Peskine8c9def32018-02-08 10:02:12 +01001223 operation->alg = 0;
1224 operation->key_set = 0;
1225 operation->iv_set = 0;
1226 operation->iv_required = 0;
1227 operation->has_input = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001228 operation->key_usage_sign = 0;
1229 operation->key_usage_verify = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001230
Gilles Peskine8c9def32018-02-08 10:02:12 +01001231 return( PSA_SUCCESS );
1232}
1233
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001234#if defined(MBEDTLS_CMAC_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001235static int psa_cmac_start( psa_mac_operation_t *operation,
1236 size_t key_bits,
1237 key_slot_t *slot,
1238 const mbedtls_cipher_info_t *cipher_info )
1239{
1240 int ret;
1241
1242 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001243
1244 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1245 if( ret != 0 )
1246 return( ret );
1247
1248 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1249 slot->data.raw.data,
1250 key_bits );
1251 return( ret );
1252}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001253#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001254
Gilles Peskine248051a2018-06-20 16:09:38 +02001255#if defined(MBEDTLS_MD_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001256static int psa_hmac_start( psa_mac_operation_t *operation,
1257 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001258 key_slot_t *slot,
1259 psa_algorithm_t alg )
1260{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001261 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001262 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001263 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001264 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001265 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001266 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001267 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001268 size_t key_length = slot->data.raw.bytes;
1269 psa_status_t status;
1270
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001271 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001272 return( PSA_ERROR_NOT_SUPPORTED );
1273
1274 if( key_type != PSA_KEY_TYPE_HMAC )
1275 return( PSA_ERROR_INVALID_ARGUMENT );
1276
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001277 operation->mac_size = digest_size;
1278
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001279 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001280 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001281 {
1282 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001283 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001284 if( status != PSA_SUCCESS )
1285 return( status );
1286 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001287 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001288 if( status != PSA_SUCCESS )
1289 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001290 }
Gilles Peskined223b522018-06-11 18:12:58 +02001291 else
1292 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001293
Gilles Peskined223b522018-06-11 18:12:58 +02001294 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1295 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001296 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001297 ipad[i] ^= 0x36;
1298 memset( ipad + key_length, 0x36, block_size - key_length );
1299
1300 /* Copy the key material from ipad to opad, flipping the requisite bits,
1301 * and filling the rest of opad with the requisite constant. */
1302 for( i = 0; i < key_length; i++ )
1303 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1304 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001305
1306 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1307 PSA_ALG_HMAC_HASH( alg ) );
1308 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001309 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001310
1311 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1312 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001313
1314cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001315 mbedtls_zeroize( ipad, key_length );
1316 /* opad is in the context. It needs to stay in memory if this function
1317 * succeeds, and it will be wiped by psa_mac_abort() called from
1318 * psa_mac_start in the error case. */
1319
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001320 return( status );
1321}
Gilles Peskine248051a2018-06-20 16:09:38 +02001322#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001323
Gilles Peskine8c9def32018-02-08 10:02:12 +01001324psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1325 psa_key_slot_t key,
1326 psa_algorithm_t alg )
1327{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001328 psa_status_t status;
1329 key_slot_t *slot;
1330 psa_key_type_t key_type;
1331 size_t key_bits;
Gilles Peskine828ed142018-06-18 23:25:51 +02001332 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001333
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001334 status = psa_mac_init( operation, alg );
1335 if( status != PSA_SUCCESS )
1336 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001337
1338 status = psa_get_key_information( key, &key_type, &key_bits );
1339 if( status != PSA_SUCCESS )
1340 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001341
Gilles Peskine8c9def32018-02-08 10:02:12 +01001342 slot = &global_data.key_slots[key];
Gilles Peskine99bc6492018-06-11 17:13:00 +02001343 if( slot->type == PSA_KEY_TYPE_NONE )
1344 return( PSA_ERROR_EMPTY_SLOT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001345
Moran Pekerd7326592018-05-29 16:56:39 +03001346 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001347 operation->key_usage_sign = 1;
1348
Moran Pekerd7326592018-05-29 16:56:39 +03001349 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001350 operation->key_usage_verify = 1;
1351
Gilles Peskine8c9def32018-02-08 10:02:12 +01001352 if( ! PSA_ALG_IS_HMAC( alg ) )
1353 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001354 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001355 if( cipher_info == NULL )
1356 return( PSA_ERROR_NOT_SUPPORTED );
1357 operation->mac_size = cipher_info->block_size;
1358 }
1359 switch( alg )
1360 {
1361#if defined(MBEDTLS_CMAC_C)
1362 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001363 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1364 key_bits,
1365 slot,
1366 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001367 break;
1368#endif /* MBEDTLS_CMAC_C */
1369 default:
1370#if defined(MBEDTLS_MD_C)
1371 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001372 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001373 else
1374#endif /* MBEDTLS_MD_C */
1375 return( PSA_ERROR_NOT_SUPPORTED );
1376 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001377
Gilles Peskine8c9def32018-02-08 10:02:12 +01001378 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001379 * context may contain data that needs to be wiped on error. */
1380 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001381 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001382 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001384 else
1385 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001386 operation->key_set = 1;
1387 }
1388 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001389}
1390
1391psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1392 const uint8_t *input,
1393 size_t input_length )
1394{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001395 int ret = 0 ;
1396 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001397 if( ! operation->key_set )
1398 return( PSA_ERROR_BAD_STATE );
1399 if( operation->iv_required && ! operation->iv_set )
1400 return( PSA_ERROR_BAD_STATE );
1401 operation->has_input = 1;
1402
1403 switch( operation->alg )
1404 {
1405#if defined(MBEDTLS_CMAC_C)
1406 case PSA_ALG_CMAC:
1407 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1408 input, input_length );
1409 break;
1410#endif /* MBEDTLS_CMAC_C */
1411 default:
1412#if defined(MBEDTLS_MD_C)
1413 if( PSA_ALG_IS_HMAC( operation->alg ) )
1414 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001415 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001416 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001417 }
1418 else
1419#endif /* MBEDTLS_MD_C */
1420 {
1421 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1422 }
1423 break;
1424 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001425 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001426 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001427 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001428 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001429 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001430 }
1431
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001432 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001433}
1434
mohammad16036df908f2018-04-02 08:34:15 -07001435static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001436 uint8_t *mac,
1437 size_t mac_size,
1438 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001439{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001440 int ret = 0;
1441 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001442 if( ! operation->key_set )
1443 return( PSA_ERROR_BAD_STATE );
1444 if( operation->iv_required && ! operation->iv_set )
1445 return( PSA_ERROR_BAD_STATE );
1446
1447 /* Fill the output buffer with something that isn't a valid mac
1448 * (barring an attack on the mac and deliberately-crafted input),
1449 * in case the caller doesn't check the return status properly. */
1450 *mac_length = operation->mac_size;
1451 memset( mac, '!', mac_size );
1452
1453 if( mac_size < operation->mac_size )
1454 return( PSA_ERROR_BUFFER_TOO_SMALL );
1455
1456 switch( operation->alg )
1457 {
1458#if defined(MBEDTLS_CMAC_C)
1459 case PSA_ALG_CMAC:
1460 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1461 break;
1462#endif /* MBEDTLS_CMAC_C */
1463 default:
1464#if defined(MBEDTLS_MD_C)
1465 if( PSA_ALG_IS_HMAC( operation->alg ) )
1466 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001467 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001468 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001469 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001470 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001471 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001472
Gilles Peskine99bc6492018-06-11 17:13:00 +02001473 if( block_size == 0 )
1474 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001475
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001476 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001477 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001478 if( status != PSA_SUCCESS )
1479 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001480 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001481
1482 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1483 PSA_ALG_HMAC_HASH( operation->alg ) );
1484 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001485 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001486
1487 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001488 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001489 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001490 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001491
1492 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001493 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001494 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001495 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001496
1497 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1498 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001499 hmac_cleanup:
1500 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001501 }
1502 else
1503#endif /* MBEDTLS_MD_C */
1504 {
1505 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1506 }
1507 break;
1508 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001509cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001510
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001511 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001512 {
1513 return( psa_mac_abort( operation ) );
1514 }
1515 else
1516 {
1517 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001518 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001519 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001520
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001521 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001522 }
1523}
1524
mohammad16036df908f2018-04-02 08:34:15 -07001525psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1526 uint8_t *mac,
1527 size_t mac_size,
1528 size_t *mac_length )
1529{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001530 if( ! operation->key_usage_sign )
mohammad16036df908f2018-04-02 08:34:15 -07001531 return( PSA_ERROR_NOT_PERMITTED );
1532
Gilles Peskine99bc6492018-06-11 17:13:00 +02001533 return( psa_mac_finish_internal( operation, mac,
1534 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001535}
1536
Gilles Peskine828ed142018-06-18 23:25:51 +02001537#define PSA_MAC_MAX_SIZE \
Gilles Peskine2d277862018-06-18 15:41:12 +02001538 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1539 MBEDTLS_MD_MAX_SIZE : \
Gilles Peskine8c9def32018-02-08 10:02:12 +01001540 MBEDTLS_MAX_BLOCK_LENGTH )
1541psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1542 const uint8_t *mac,
1543 size_t mac_length )
1544{
Gilles Peskine828ed142018-06-18 23:25:51 +02001545 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001546 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001547 psa_status_t status;
1548
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001549 if( ! operation->key_usage_verify )
mohammad16036df908f2018-04-02 08:34:15 -07001550 return( PSA_ERROR_NOT_PERMITTED );
1551
1552 status = psa_mac_finish_internal( operation,
1553 actual_mac, sizeof( actual_mac ),
1554 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001555 if( status != PSA_SUCCESS )
1556 return( status );
1557 if( actual_mac_length != mac_length )
1558 return( PSA_ERROR_INVALID_SIGNATURE );
1559 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1560 return( PSA_ERROR_INVALID_SIGNATURE );
1561 return( PSA_SUCCESS );
1562}
1563
1564
Gilles Peskine20035e32018-02-03 22:44:14 +01001565
Gilles Peskine20035e32018-02-03 22:44:14 +01001566/****************************************************************/
1567/* Asymmetric cryptography */
1568/****************************************************************/
1569
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001570/* Decode the hash algorithm from alg and store the mbedtls encoding in
1571 * md_alg. Verify that the hash length is consistent. */
1572static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1573 size_t hash_length,
1574 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001575{
Gilles Peskine61b91d42018-06-08 16:09:36 +02001576 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
1577 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1578 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1579 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001580 {
1581#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001582 if( hash_length > UINT_MAX )
1583 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001584#endif
1585 }
1586 else
1587 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001588 if( mbedtls_md_get_size( md_info ) != hash_length )
1589 return( PSA_ERROR_INVALID_ARGUMENT );
1590 if( md_info == NULL )
1591 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001592 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001593 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001594}
1595
Gilles Peskine61b91d42018-06-08 16:09:36 +02001596psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1597 psa_algorithm_t alg,
1598 const uint8_t *hash,
1599 size_t hash_length,
1600 const uint8_t *salt,
1601 size_t salt_length,
1602 uint8_t *signature,
1603 size_t signature_size,
1604 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001605{
1606 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001607 psa_status_t status;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001608 *signature_length = 0;
1609 (void) salt;
1610 (void) salt_length;
1611
Gilles Peskine828ed142018-06-18 23:25:51 +02001612 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine20035e32018-02-03 22:44:14 +01001613 return( PSA_ERROR_EMPTY_SLOT );
1614 slot = &global_data.key_slots[key];
1615 if( slot->type == PSA_KEY_TYPE_NONE )
1616 return( PSA_ERROR_EMPTY_SLOT );
1617 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1618 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001619 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
mohammad160306e79202018-03-28 13:17:44 +03001620 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001621
Gilles Peskine20035e32018-02-03 22:44:14 +01001622#if defined(MBEDTLS_RSA_C)
1623 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1624 {
1625 mbedtls_rsa_context *rsa = slot->data.rsa;
1626 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001627 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001628 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001629 if( status != PSA_SUCCESS )
1630 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001631
Gilles Peskine20035e32018-02-03 22:44:14 +01001632 if( signature_size < rsa->len )
1633 return( PSA_ERROR_BUFFER_TOO_SMALL );
1634#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001635 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001636 {
1637 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1638 MBEDTLS_MD_NONE );
1639 ret = mbedtls_rsa_pkcs1_sign( rsa,
1640 mbedtls_ctr_drbg_random,
1641 &global_data.ctr_drbg,
1642 MBEDTLS_RSA_PRIVATE,
1643 md_alg, hash_length, hash,
1644 signature );
1645 }
1646 else
1647#endif /* MBEDTLS_PKCS1_V15 */
1648#if defined(MBEDTLS_PKCS1_V21)
1649 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1650 {
1651 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1652 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1653 mbedtls_ctr_drbg_random,
1654 &global_data.ctr_drbg,
1655 MBEDTLS_RSA_PRIVATE,
1656 md_alg, hash_length, hash,
1657 signature );
1658 }
1659 else
1660#endif /* MBEDTLS_PKCS1_V21 */
1661 {
1662 return( PSA_ERROR_INVALID_ARGUMENT );
1663 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001664 if( ret == 0 )
1665 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001666 return( mbedtls_to_psa_error( ret ) );
1667 }
1668 else
1669#endif /* defined(MBEDTLS_RSA_C) */
1670#if defined(MBEDTLS_ECP_C)
1671 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1672 {
itayzafrir5c753392018-05-08 11:18:38 +03001673 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1674 int ret;
1675 const mbedtls_md_info_t *md_info;
1676 mbedtls_md_type_t md_alg;
1677 if( signature_size < PSA_ECDSA_SIGNATURE_SIZE( ecdsa->grp.pbits ) )
1678 return( PSA_ERROR_BUFFER_TOO_SMALL );
1679 md_info = mbedtls_md_info_from_psa( alg );
1680 md_alg = mbedtls_md_get_type( md_info );
1681 ret = mbedtls_ecdsa_write_signature( ecdsa, md_alg, hash, hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001682 signature, signature_length,
1683 mbedtls_ctr_drbg_random,
1684 &global_data.ctr_drbg );
itayzafrir5c753392018-05-08 11:18:38 +03001685 return( mbedtls_to_psa_error( ret ) );
1686 }
1687 else
1688#endif /* defined(MBEDTLS_ECP_C) */
1689 {
Gilles Peskine20035e32018-02-03 22:44:14 +01001690 return( PSA_ERROR_NOT_SUPPORTED );
1691 }
itayzafrir5c753392018-05-08 11:18:38 +03001692}
1693
1694psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1695 psa_algorithm_t alg,
1696 const uint8_t *hash,
1697 size_t hash_length,
1698 const uint8_t *salt,
1699 size_t salt_length,
1700 uint8_t *signature,
1701 size_t signature_size )
1702{
1703 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001704 psa_status_t status;
itayzafrir5c753392018-05-08 11:18:38 +03001705 (void) salt;
1706 (void) salt_length;
1707
Gilles Peskine828ed142018-06-18 23:25:51 +02001708 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
itayzafrir5c753392018-05-08 11:18:38 +03001709 return( PSA_ERROR_INVALID_ARGUMENT );
1710 slot = &global_data.key_slots[key];
1711 if( slot->type == PSA_KEY_TYPE_NONE )
1712 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001713 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001714 return( PSA_ERROR_NOT_PERMITTED );
1715
Gilles Peskine61b91d42018-06-08 16:09:36 +02001716#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001717 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1718 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001719 {
1720 mbedtls_rsa_context *rsa = slot->data.rsa;
1721 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001722 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001723 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001724 if( status != PSA_SUCCESS )
1725 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001726
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001727 if( signature_size < rsa->len )
1728 return( PSA_ERROR_BUFFER_TOO_SMALL );
1729#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001730 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001731 {
1732 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1733 MBEDTLS_MD_NONE );
1734
1735 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001736 mbedtls_ctr_drbg_random,
1737 &global_data.ctr_drbg,
1738 MBEDTLS_RSA_PUBLIC,
1739 md_alg,
1740 hash_length,
1741 hash,
1742 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001743
1744 }
1745 else
1746#endif /* MBEDTLS_PKCS1_V15 */
1747#if defined(MBEDTLS_PKCS1_V21)
1748 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1749 {
Gilles Peskinebeb49482018-06-08 17:44:35 +02001750 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1751 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1752 mbedtls_ctr_drbg_random,
1753 &global_data.ctr_drbg,
1754 MBEDTLS_RSA_PUBLIC,
1755 md_alg, hash_length, hash,
1756 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001757 }
1758 else
1759#endif /* MBEDTLS_PKCS1_V21 */
1760 {
1761 return( PSA_ERROR_INVALID_ARGUMENT );
1762 }
1763 return( mbedtls_to_psa_error( ret ) );
1764 }
1765 else
1766#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001767#if defined(MBEDTLS_ECP_C)
1768 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1769 {
1770 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1771 int ret;
Gilles Peskine2d277862018-06-18 15:41:12 +02001772 (void) alg;
Gilles Peskine61b91d42018-06-08 16:09:36 +02001773 ret = mbedtls_ecdsa_read_signature( ecdsa, hash, hash_length,
1774 signature, signature_size );
itayzafrir5c753392018-05-08 11:18:38 +03001775 return( mbedtls_to_psa_error( ret ) );
1776 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001777 else
1778#endif /* defined(MBEDTLS_ECP_C) */
1779 {
1780 return( PSA_ERROR_NOT_SUPPORTED );
1781 }
1782}
1783
Gilles Peskine61b91d42018-06-08 16:09:36 +02001784psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
1785 psa_algorithm_t alg,
1786 const uint8_t *input,
1787 size_t input_length,
1788 const uint8_t *salt,
1789 size_t salt_length,
1790 uint8_t *output,
1791 size_t output_size,
1792 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001793{
1794 key_slot_t *slot;
1795 (void) salt;
1796 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001797 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001798
Gilles Peskine828ed142018-06-18 23:25:51 +02001799 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03001800 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001801 slot = &global_data.key_slots[key];
1802 if( slot->type == PSA_KEY_TYPE_NONE )
1803 return( PSA_ERROR_EMPTY_SLOT );
1804 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1805 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001806 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
1807 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001808
1809#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001810 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1811 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001812 {
1813 mbedtls_rsa_context *rsa = slot->data.rsa;
1814 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001815 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02001816 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001817#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001818 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001819 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001820 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
1821 mbedtls_ctr_drbg_random,
1822 &global_data.ctr_drbg,
1823 MBEDTLS_RSA_PUBLIC,
1824 input_length,
1825 input,
1826 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001827 }
1828 else
1829#endif /* MBEDTLS_PKCS1_V15 */
1830#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001831 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001832 {
1833 return( PSA_ERROR_NOT_SUPPORTED );
1834 }
1835 else
1836#endif /* MBEDTLS_PKCS1_V21 */
1837 {
1838 return( PSA_ERROR_INVALID_ARGUMENT );
1839 }
1840 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001841 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001842 return( mbedtls_to_psa_error( ret ) );
1843 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001844 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001845#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001846 {
1847 return( PSA_ERROR_NOT_SUPPORTED );
1848 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001849}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001850
Gilles Peskine61b91d42018-06-08 16:09:36 +02001851psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
1852 psa_algorithm_t alg,
1853 const uint8_t *input,
1854 size_t input_length,
1855 const uint8_t *salt,
1856 size_t salt_length,
1857 uint8_t *output,
1858 size_t output_size,
1859 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001860{
1861 key_slot_t *slot;
1862 (void) salt;
1863 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001864 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001865
Gilles Peskine828ed142018-06-18 23:25:51 +02001866 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001867 return( PSA_ERROR_EMPTY_SLOT );
1868 slot = &global_data.key_slots[key];
1869 if( slot->type == PSA_KEY_TYPE_NONE )
1870 return( PSA_ERROR_EMPTY_SLOT );
1871 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1872 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001873 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
1874 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001875
1876#if defined(MBEDTLS_RSA_C)
1877 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1878 {
1879 mbedtls_rsa_context *rsa = slot->data.rsa;
1880 int ret;
1881
Gilles Peskinec4def2f2018-06-08 17:53:48 +02001882 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001883 return( PSA_ERROR_INVALID_ARGUMENT );
1884
1885#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001886 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001887 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001888 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
1889 mbedtls_ctr_drbg_random,
1890 &global_data.ctr_drbg,
1891 MBEDTLS_RSA_PRIVATE,
1892 output_length,
1893 input,
1894 output,
1895 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001896 }
1897 else
1898#endif /* MBEDTLS_PKCS1_V15 */
1899#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001900 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001901 {
1902 return( PSA_ERROR_NOT_SUPPORTED );
1903 }
1904 else
1905#endif /* MBEDTLS_PKCS1_V21 */
1906 {
1907 return( PSA_ERROR_INVALID_ARGUMENT );
1908 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001909
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001910 return( mbedtls_to_psa_error( ret ) );
1911 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001912 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001913#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001914 {
1915 return( PSA_ERROR_NOT_SUPPORTED );
1916 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001917}
Gilles Peskine20035e32018-02-03 22:44:14 +01001918
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001919
1920
mohammad1603503973b2018-03-12 15:59:30 +02001921/****************************************************************/
1922/* Symmetric cryptography */
1923/****************************************************************/
1924
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001925/* Initialize the cipher operation structure. Once this function has been
1926 * called, psa_cipher_abort can run and will do the right thing. */
1927static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
1928 psa_algorithm_t alg )
1929{
Gilles Peskinec06e0712018-06-20 16:21:04 +02001930 if( ! PSA_ALG_IS_CIPHER( alg ) )
1931 {
1932 memset( operation, 0, sizeof( *operation ) );
1933 return( PSA_ERROR_INVALID_ARGUMENT );
1934 }
1935
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001936 operation->alg = alg;
1937 operation->key_set = 0;
1938 operation->iv_set = 0;
1939 operation->iv_required = 1;
1940 operation->iv_size = 0;
1941 operation->block_size = 0;
1942 mbedtls_cipher_init( &operation->ctx.cipher );
1943 return( PSA_SUCCESS );
1944}
1945
Gilles Peskinee553c652018-06-04 16:22:46 +02001946static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
1947 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02001948 psa_algorithm_t alg,
1949 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02001950{
1951 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1952 psa_status_t status;
1953 key_slot_t *slot;
1954 psa_key_type_t key_type;
1955 size_t key_bits;
1956 const mbedtls_cipher_info_t *cipher_info = NULL;
1957
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001958 status = psa_cipher_init( operation, alg );
1959 if( status != PSA_SUCCESS )
1960 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02001961
1962 status = psa_get_key_information( key, &key_type, &key_bits );
1963 if( status != PSA_SUCCESS )
1964 return( status );
1965 slot = &global_data.key_slots[key];
1966
Gilles Peskinebb1072f2018-06-08 18:46:05 +02001967 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02001968 if( cipher_info == NULL )
1969 return( PSA_ERROR_NOT_SUPPORTED );
1970
mohammad1603503973b2018-03-12 15:59:30 +02001971 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03001972 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001973 {
1974 psa_cipher_abort( operation );
1975 return( mbedtls_to_psa_error( ret ) );
1976 }
1977
1978 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data,
Gilles Peskinee553c652018-06-04 16:22:46 +02001979 key_bits, cipher_operation );
Moran Peker41deec42018-04-04 15:43:05 +03001980 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001981 {
1982 psa_cipher_abort( operation );
1983 return( mbedtls_to_psa_error( ret ) );
1984 }
1985
mohammad16038481e742018-03-18 13:57:31 +02001986#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03001987 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02001988 {
Gilles Peskine53514202018-06-06 15:11:46 +02001989 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1990 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02001991
Moran Pekera28258c2018-05-29 16:25:04 +03001992 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02001993 {
1994 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
1995 mode = MBEDTLS_PADDING_PKCS7;
1996 break;
1997 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
1998 mode = MBEDTLS_PADDING_NONE;
1999 break;
2000 default:
Moran Pekerae382792018-05-31 14:06:17 +03002001 psa_cipher_abort( operation );
2002 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002003 }
2004 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002005 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002006 {
2007 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002008 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002009 }
mohammad16038481e742018-03-18 13:57:31 +02002010 }
2011#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2012
mohammad1603503973b2018-03-12 15:59:30 +02002013 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002014 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
2015 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
2016 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002017 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002018 {
mohammad160389e0f462018-04-12 08:48:45 +03002019 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02002020 }
mohammad1603503973b2018-03-12 15:59:30 +02002021
Moran Peker395db872018-05-31 14:07:14 +03002022 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002023}
2024
Gilles Peskinee553c652018-06-04 16:22:46 +02002025psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
2026 psa_key_slot_t key,
2027 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002028{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002029 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002030}
2031
Gilles Peskinee553c652018-06-04 16:22:46 +02002032psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
2033 psa_key_slot_t key,
2034 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002035{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002036 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002037}
2038
Gilles Peskinee553c652018-06-04 16:22:46 +02002039psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
2040 unsigned char *iv,
2041 size_t iv_size,
2042 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002043{
Moran Peker41deec42018-04-04 15:43:05 +03002044 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002045 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002046 return( PSA_ERROR_BAD_STATE );
2047 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002048 {
Moran Peker41deec42018-04-04 15:43:05 +03002049 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2050 goto exit;
2051 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002052 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2053 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002054 if( ret != 0 )
2055 {
2056 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002057 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002058 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002059
mohammad16038481e742018-03-18 13:57:31 +02002060 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03002061 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002062
Moran Peker395db872018-05-31 14:07:14 +03002063exit:
2064 if( ret != PSA_SUCCESS )
2065 psa_cipher_abort( operation );
2066 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002067}
2068
Gilles Peskinee553c652018-06-04 16:22:46 +02002069psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2070 const unsigned char *iv,
2071 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002072{
Moran Peker41deec42018-04-04 15:43:05 +03002073 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002074 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002075 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002076 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002077 {
Moran Pekerae382792018-05-31 14:06:17 +03002078 psa_cipher_abort( operation );
2079 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002080 }
mohammad1603503973b2018-03-12 15:59:30 +02002081 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002082 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002083 {
2084 psa_cipher_abort( operation );
2085 return( mbedtls_to_psa_error( ret ) );
2086 }
2087
2088 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002089
Moran Peker395db872018-05-31 14:07:14 +03002090 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002091}
2092
Gilles Peskinee553c652018-06-04 16:22:46 +02002093psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2094 const uint8_t *input,
2095 size_t input_length,
2096 unsigned char *output,
2097 size_t output_size,
2098 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002099{
2100 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002101 size_t expected_output_size;
2102 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2103 {
2104 /* Take the unprocessed partial block left over from previous
2105 * update calls, if any, plus the input to this call. Remove
2106 * the last partial block, if any. You get the data that will be
2107 * output in this call. */
2108 expected_output_size =
2109 ( operation->ctx.cipher.unprocessed_len + input_length )
2110 / operation->block_size * operation->block_size;
2111 }
2112 else
2113 {
2114 expected_output_size = input_length;
2115 }
2116 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002117 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002118
mohammad1603503973b2018-03-12 15:59:30 +02002119 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002120 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002121 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002122 {
2123 psa_cipher_abort( operation );
2124 return( mbedtls_to_psa_error( ret ) );
2125 }
2126
Moran Peker395db872018-05-31 14:07:14 +03002127 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002128}
2129
Gilles Peskinee553c652018-06-04 16:22:46 +02002130psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2131 uint8_t *output,
2132 size_t output_size,
2133 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002134{
2135 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002136 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002137
mohammad1603503973b2018-03-12 15:59:30 +02002138 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002139 {
Moran Peker7cb22b82018-06-05 11:40:02 +03002140 psa_cipher_abort( operation );
2141 return( PSA_ERROR_BAD_STATE );
2142 }
2143 if( operation->iv_required && ! operation->iv_set )
2144 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002145 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002146 return( PSA_ERROR_BAD_STATE );
2147 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002148 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2149 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002150 {
Gilles Peskine53514202018-06-06 15:11:46 +02002151 psa_algorithm_t padding_mode =
2152 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002153 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002154 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002155 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002156 return( PSA_ERROR_TAMPERING_DETECTED );
2157 }
Gilles Peskine53514202018-06-06 15:11:46 +02002158 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002159 {
2160 if( operation->ctx.cipher.unprocessed_len != 0 )
2161 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002162 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002163 return( PSA_ERROR_INVALID_ARGUMENT );
2164 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002165 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002166 }
2167
2168 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002169 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002170 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002171 {
2172 psa_cipher_abort( operation );
2173 return( mbedtls_to_psa_error( ret ) );
2174 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002175 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002176 memcpy( output, temp_output_buffer, *output_length );
2177 else
2178 {
2179 psa_cipher_abort( operation );
2180 return( PSA_ERROR_BUFFER_TOO_SMALL );
2181 }
mohammad1603503973b2018-03-12 15:59:30 +02002182
Moran Peker4c80d832018-04-22 20:15:31 +03002183 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002184}
2185
Gilles Peskinee553c652018-06-04 16:22:46 +02002186psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2187{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002188 if( operation->alg == 0 )
2189 return( PSA_SUCCESS );
2190
mohammad1603503973b2018-03-12 15:59:30 +02002191 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002192
Moran Peker41deec42018-04-04 15:43:05 +03002193 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002194 operation->key_set = 0;
2195 operation->iv_set = 0;
2196 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002197 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002198 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002199
Moran Peker395db872018-05-31 14:07:14 +03002200 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002201}
2202
Gilles Peskinea0655c32018-04-30 17:06:50 +02002203
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002204
mohammad16038cc1cee2018-03-28 01:21:33 +03002205/****************************************************************/
2206/* Key Policy */
2207/****************************************************************/
2208
Gilles Peskine2d277862018-06-18 15:41:12 +02002209void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002210{
Gilles Peskine803ce742018-06-18 16:07:14 +02002211 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002212}
2213
Gilles Peskine2d277862018-06-18 15:41:12 +02002214void psa_key_policy_set_usage( psa_key_policy_t *policy,
2215 psa_key_usage_t usage,
2216 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002217{
mohammad16034eed7572018-03-28 05:14:59 -07002218 policy->usage = usage;
2219 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002220}
2221
Gilles Peskine2d277862018-06-18 15:41:12 +02002222psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002223{
mohammad16036df908f2018-04-02 08:34:15 -07002224 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002225}
2226
Gilles Peskine2d277862018-06-18 15:41:12 +02002227psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002228{
mohammad16036df908f2018-04-02 08:34:15 -07002229 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002230}
2231
Gilles Peskine2d277862018-06-18 15:41:12 +02002232psa_status_t psa_set_key_policy( psa_key_slot_t key,
2233 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002234{
2235 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002236
Gilles Peskine828ed142018-06-18 23:25:51 +02002237 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002238 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002239
mohammad16038cc1cee2018-03-28 01:21:33 +03002240 slot = &global_data.key_slots[key];
2241 if( slot->type != PSA_KEY_TYPE_NONE )
2242 return( PSA_ERROR_OCCUPIED_SLOT );
2243
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002244 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2245 PSA_KEY_USAGE_ENCRYPT |
2246 PSA_KEY_USAGE_DECRYPT |
2247 PSA_KEY_USAGE_SIGN |
2248 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002249 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002250
mohammad16036df908f2018-04-02 08:34:15 -07002251 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002252
2253 return( PSA_SUCCESS );
2254}
2255
Gilles Peskine2d277862018-06-18 15:41:12 +02002256psa_status_t psa_get_key_policy( psa_key_slot_t key,
2257 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002258{
2259 key_slot_t *slot;
2260
Gilles Peskine828ed142018-06-18 23:25:51 +02002261 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002262 return( PSA_ERROR_INVALID_ARGUMENT );
2263
2264 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002265
mohammad16036df908f2018-04-02 08:34:15 -07002266 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002267
2268 return( PSA_SUCCESS );
2269}
Gilles Peskine20035e32018-02-03 22:44:14 +01002270
Gilles Peskinea0655c32018-04-30 17:06:50 +02002271
2272
mohammad1603804cd712018-03-20 22:44:08 +02002273/****************************************************************/
2274/* Key Lifetime */
2275/****************************************************************/
2276
Gilles Peskine2d277862018-06-18 15:41:12 +02002277psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2278 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002279{
2280 key_slot_t *slot;
2281
Gilles Peskine828ed142018-06-18 23:25:51 +02002282 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002283 return( PSA_ERROR_INVALID_ARGUMENT );
2284
2285 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002286
mohammad1603804cd712018-03-20 22:44:08 +02002287 *lifetime = slot->lifetime;
2288
2289 return( PSA_SUCCESS );
2290}
2291
Gilles Peskine2d277862018-06-18 15:41:12 +02002292psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
2293 const psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002294{
2295 key_slot_t *slot;
2296
Gilles Peskine828ed142018-06-18 23:25:51 +02002297 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002298 return( PSA_ERROR_INVALID_ARGUMENT );
2299
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002300 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2301 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002302 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2303 return( PSA_ERROR_INVALID_ARGUMENT );
2304
mohammad1603804cd712018-03-20 22:44:08 +02002305 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002306 if( slot->type != PSA_KEY_TYPE_NONE )
2307 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002308
Moran Pekerd7326592018-05-29 16:56:39 +03002309 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002310 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002311
mohammad1603060ad8a2018-03-20 14:28:38 -07002312 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002313
2314 return( PSA_SUCCESS );
2315}
2316
Gilles Peskine20035e32018-02-03 22:44:14 +01002317
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002318
mohammad16035955c982018-04-26 00:53:03 +03002319/****************************************************************/
2320/* AEAD */
2321/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002322
mohammad16035955c982018-04-26 00:53:03 +03002323psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2324 psa_algorithm_t alg,
2325 const uint8_t *nonce,
2326 size_t nonce_length,
2327 const uint8_t *additional_data,
2328 size_t additional_data_length,
2329 const uint8_t *plaintext,
2330 size_t plaintext_length,
2331 uint8_t *ciphertext,
2332 size_t ciphertext_size,
2333 size_t *ciphertext_length )
2334{
2335 int ret;
2336 psa_status_t status;
2337 key_slot_t *slot;
2338 psa_key_type_t key_type;
2339 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002340 uint8_t *tag;
2341 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002342 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002343 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002344
mohammad1603f08a5502018-06-03 15:05:47 +03002345 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002346
mohammad16035955c982018-04-26 00:53:03 +03002347 status = psa_get_key_information( key, &key_type, &key_bits );
2348 if( status != PSA_SUCCESS )
2349 return( status );
2350 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002351 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002352 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002353
mohammad16035ed06212018-06-06 13:09:34 +03002354 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2355 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002356 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002357 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002358
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002359 if( ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) == 0 )
mohammad1603f14394b2018-06-04 14:33:19 +03002360 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002361
Gilles Peskine2d277862018-06-18 15:41:12 +02002362 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2363 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002364 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002365
mohammad16035955c982018-04-26 00:53:03 +03002366 if( alg == PSA_ALG_GCM )
2367 {
2368 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002369 tag_length = 16;
2370
mohammad160396910d82018-06-04 14:33:00 +03002371 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2372 return( PSA_ERROR_INVALID_ARGUMENT );
2373
mohammad160315223a82018-06-03 17:19:55 +03002374 //make sure we have place to hold the tag in the ciphertext buffer
2375 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002376 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002377
2378 //update the tag pointer to point to the end of the ciphertext_length
2379 tag = ciphertext + plaintext_length;
2380
mohammad16035955c982018-04-26 00:53:03 +03002381 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002382 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002383 slot->data.raw.data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002384 key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002385 if( ret != 0 )
2386 {
2387 mbedtls_gcm_free( &gcm );
2388 return( mbedtls_to_psa_error( ret ) );
2389 }
2390 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002391 plaintext_length, nonce,
2392 nonce_length, additional_data,
2393 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002394 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002395 mbedtls_gcm_free( &gcm );
2396 }
2397 else if( alg == PSA_ALG_CCM )
2398 {
2399 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002400 tag_length = 16;
2401
mohammad160396910d82018-06-04 14:33:00 +03002402 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2403 return( PSA_ERROR_INVALID_ARGUMENT );
2404
mohammad160347ddf3d2018-04-26 01:11:21 +03002405 if( nonce_length < 7 || nonce_length > 13 )
2406 return( PSA_ERROR_INVALID_ARGUMENT );
2407
mohammad160315223a82018-06-03 17:19:55 +03002408 //make sure we have place to hold the tag in the ciphertext buffer
2409 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002410 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002411
2412 //update the tag pointer to point to the end of the ciphertext_length
2413 tag = ciphertext + plaintext_length;
2414
mohammad16035955c982018-04-26 00:53:03 +03002415 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002416 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002417 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002418 if( ret != 0 )
2419 {
2420 mbedtls_ccm_free( &ccm );
2421 return( mbedtls_to_psa_error( ret ) );
2422 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002423 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002424 nonce, nonce_length,
2425 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002426 additional_data_length,
2427 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002428 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002429 mbedtls_ccm_free( &ccm );
2430 }
mohammad16035c8845f2018-05-09 05:40:09 -07002431 else
2432 {
mohammad1603554faad2018-06-03 15:07:38 +03002433 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002434 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002435
mohammad160315223a82018-06-03 17:19:55 +03002436 if( ret != 0 )
2437 {
2438 memset( ciphertext, 0, ciphertext_size );
2439 return( mbedtls_to_psa_error( ret ) );
2440 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002441
mohammad160315223a82018-06-03 17:19:55 +03002442 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002443 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002444}
2445
Gilles Peskineee652a32018-06-01 19:23:52 +02002446/* Locate the tag in a ciphertext buffer containing the encrypted data
2447 * followed by the tag. Return the length of the part preceding the tag in
2448 * *plaintext_length. This is the size of the plaintext in modes where
2449 * the encrypted data has the same size as the plaintext, such as
2450 * CCM and GCM. */
2451static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2452 const uint8_t *ciphertext,
2453 size_t ciphertext_length,
2454 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002455 const uint8_t **p_tag )
2456{
2457 size_t payload_length;
2458 if( tag_length > ciphertext_length )
2459 return( PSA_ERROR_INVALID_ARGUMENT );
2460 payload_length = ciphertext_length - tag_length;
2461 if( payload_length > plaintext_size )
2462 return( PSA_ERROR_BUFFER_TOO_SMALL );
2463 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002464 return( PSA_SUCCESS );
2465}
2466
mohammad16035955c982018-04-26 00:53:03 +03002467psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2468 psa_algorithm_t alg,
2469 const uint8_t *nonce,
2470 size_t nonce_length,
2471 const uint8_t *additional_data,
2472 size_t additional_data_length,
2473 const uint8_t *ciphertext,
2474 size_t ciphertext_length,
2475 uint8_t *plaintext,
2476 size_t plaintext_size,
2477 size_t *plaintext_length )
2478{
2479 int ret;
2480 psa_status_t status;
2481 key_slot_t *slot;
2482 psa_key_type_t key_type;
2483 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002484 const uint8_t *tag;
2485 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002486 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002487 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002488
Gilles Peskineee652a32018-06-01 19:23:52 +02002489 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002490
mohammad16035955c982018-04-26 00:53:03 +03002491 status = psa_get_key_information( key, &key_type, &key_bits );
2492 if( status != PSA_SUCCESS )
2493 return( status );
2494 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002495 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002496 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002497
mohammad16035ed06212018-06-06 13:09:34 +03002498 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2499 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002500 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002501 return( PSA_ERROR_NOT_SUPPORTED );
2502
mohammad1603f14394b2018-06-04 14:33:19 +03002503 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2504 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002505
Gilles Peskine2d277862018-06-18 15:41:12 +02002506 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2507 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002508 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002509
mohammad16035955c982018-04-26 00:53:03 +03002510 if( alg == PSA_ALG_GCM )
2511 {
2512 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002513
Gilles Peskineee652a32018-06-01 19:23:52 +02002514 tag_length = 16;
2515 status = psa_aead_unpadded_locate_tag( tag_length,
2516 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002517 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002518 if( status != PSA_SUCCESS )
2519 return( status );
2520
mohammad16035955c982018-04-26 00:53:03 +03002521 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002522 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002523 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002524 if( ret != 0 )
2525 {
2526 mbedtls_gcm_free( &gcm );
2527 return( mbedtls_to_psa_error( ret ) );
2528 }
mohammad16035955c982018-04-26 00:53:03 +03002529
Gilles Peskineee652a32018-06-01 19:23:52 +02002530 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002531 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002532 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002533 additional_data,
2534 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002535 tag, tag_length,
2536 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002537 mbedtls_gcm_free( &gcm );
2538 }
2539 else if( alg == PSA_ALG_CCM )
2540 {
2541 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002542
mohammad160347ddf3d2018-04-26 01:11:21 +03002543 if( nonce_length < 7 || nonce_length > 13 )
2544 return( PSA_ERROR_INVALID_ARGUMENT );
2545
mohammad16039375f842018-06-03 14:28:24 +03002546 tag_length = 16;
2547 status = psa_aead_unpadded_locate_tag( tag_length,
2548 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002549 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002550 if( status != PSA_SUCCESS )
2551 return( status );
2552
mohammad16035955c982018-04-26 00:53:03 +03002553 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002554 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002555 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002556 if( ret != 0 )
2557 {
2558 mbedtls_ccm_free( &ccm );
2559 return( mbedtls_to_psa_error( ret ) );
2560 }
mohammad160360a64d02018-06-03 17:20:42 +03002561 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002562 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002563 additional_data,
2564 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002565 ciphertext, plaintext,
2566 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002567 mbedtls_ccm_free( &ccm );
2568 }
mohammad160339574652018-06-01 04:39:53 -07002569 else
2570 {
mohammad1603554faad2018-06-03 15:07:38 +03002571 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002572 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002573
Gilles Peskineee652a32018-06-01 19:23:52 +02002574 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002575 memset( plaintext, 0, plaintext_size );
2576 else
2577 *plaintext_length = ciphertext_length - tag_length;
2578
Gilles Peskineee652a32018-06-01 19:23:52 +02002579 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002580}
2581
Gilles Peskinea0655c32018-04-30 17:06:50 +02002582
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002583
Gilles Peskine20035e32018-02-03 22:44:14 +01002584/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002585/* Key generation */
2586/****************************************************************/
2587
2588psa_status_t psa_generate_random( uint8_t *output,
2589 size_t output_size )
2590{
2591 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2592 output, output_size );
2593 return( mbedtls_to_psa_error( ret ) );
2594}
2595
2596psa_status_t psa_generate_key( psa_key_slot_t key,
2597 psa_key_type_t type,
2598 size_t bits,
2599 const void *parameters,
2600 size_t parameters_size )
2601{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002602 key_slot_t *slot;
2603
2604 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
2605 return( PSA_ERROR_INVALID_ARGUMENT );
2606 slot = &global_data.key_slots[key];
2607 if( slot->type != PSA_KEY_TYPE_NONE )
2608 return( PSA_ERROR_OCCUPIED_SLOT );
2609 if( parameters == NULL && parameters_size != 0 )
2610 return( PSA_ERROR_INVALID_ARGUMENT );
2611
2612 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
2613 {
2614 psa_status_t status = prepare_raw_data_slot( type, bits,
2615 &slot->data.raw );
2616 if( status != PSA_SUCCESS )
2617 return( status );
2618 status = psa_generate_random( slot->data.raw.data,
2619 slot->data.raw.bytes );
2620 if( status != PSA_SUCCESS )
2621 {
2622 mbedtls_free( slot->data.raw.data );
2623 return( status );
2624 }
2625#if defined(MBEDTLS_DES_C)
2626 if( type == PSA_KEY_TYPE_DES )
2627 {
2628 mbedtls_des_key_set_parity( slot->data.raw.data );
2629 if( slot->data.raw.bytes >= 16 )
2630 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2631 if( slot->data.raw.bytes == 24 )
2632 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2633 }
2634#endif /* MBEDTLS_DES_C */
2635 }
2636 else
2637
2638#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2639 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2640 {
2641 mbedtls_rsa_context *rsa;
2642 int ret;
2643 int exponent = 65537;
2644 if( parameters != NULL )
2645 {
2646 const unsigned *p = parameters;
2647 if( parameters_size != sizeof( *p ) )
2648 return( PSA_ERROR_INVALID_ARGUMENT );
2649 if( *p > INT_MAX )
2650 return( PSA_ERROR_INVALID_ARGUMENT );
2651 exponent = *p;
2652 }
2653 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
2654 if( rsa == NULL )
2655 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2656 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
2657 ret = mbedtls_rsa_gen_key( rsa,
2658 mbedtls_ctr_drbg_random,
2659 &global_data.ctr_drbg,
2660 bits,
2661 exponent );
2662 if( ret != 0 )
2663 {
2664 mbedtls_rsa_free( rsa );
2665 mbedtls_free( rsa );
2666 return( mbedtls_to_psa_error( ret ) );
2667 }
2668 slot->data.rsa = rsa;
2669 }
2670 else
2671#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
2672
2673#if defined(MBEDTLS_ECP_C)
2674 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
2675 {
2676 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
2677 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
2678 const mbedtls_ecp_curve_info *curve_info =
2679 mbedtls_ecp_curve_info_from_grp_id( grp_id );
2680 mbedtls_ecp_keypair *ecp;
2681 int ret;
2682 if( parameters != NULL )
2683 return( PSA_ERROR_NOT_SUPPORTED );
2684 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
2685 return( PSA_ERROR_NOT_SUPPORTED );
2686 if( curve_info->bit_size != bits )
2687 return( PSA_ERROR_INVALID_ARGUMENT );
2688 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
2689 if( ecp == NULL )
2690 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2691 mbedtls_ecp_keypair_init( ecp );
2692 ret = mbedtls_ecp_gen_key( grp_id, ecp,
2693 mbedtls_ctr_drbg_random,
2694 &global_data.ctr_drbg );
2695 if( ret != 0 )
2696 {
2697 mbedtls_ecp_keypair_free( ecp );
2698 mbedtls_free( ecp );
2699 return( mbedtls_to_psa_error( ret ) );
2700 }
2701 slot->data.ecp = ecp;
2702 }
2703 else
2704#endif /* MBEDTLS_ECP_C */
2705
2706 return( PSA_ERROR_NOT_SUPPORTED );
2707
2708 slot->type = type;
2709 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02002710}
2711
2712
2713/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002714/* Module setup */
2715/****************************************************************/
2716
Gilles Peskinee59236f2018-01-27 23:32:46 +01002717void mbedtls_psa_crypto_free( void )
2718{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002719 size_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02002720 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002721 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002722 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2723 mbedtls_entropy_free( &global_data.entropy );
2724 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2725}
2726
2727psa_status_t psa_crypto_init( void )
2728{
2729 int ret;
2730 const unsigned char drbg_seed[] = "PSA";
2731
2732 if( global_data.initialized != 0 )
2733 return( PSA_SUCCESS );
2734
2735 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2736 mbedtls_entropy_init( &global_data.entropy );
2737 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2738
2739 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2740 mbedtls_entropy_func,
2741 &global_data.entropy,
2742 drbg_seed, sizeof( drbg_seed ) - 1 );
2743 if( ret != 0 )
2744 goto exit;
2745
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002746 global_data.initialized = 1;
2747
Gilles Peskinee59236f2018-01-27 23:32:46 +01002748exit:
2749 if( ret != 0 )
2750 mbedtls_psa_crypto_free( );
2751 return( mbedtls_to_psa_error( ret ) );
2752}
2753
2754#endif /* MBEDTLS_PSA_CRYPTO_C */