blob: a610af364a820a5e0a5c4c3ac1799d126930009a [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:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001105 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1106 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001107 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001108 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001109 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001110 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001111 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1112 * but two-key Triple-DES is functionally three-key Triple-DES
1113 * with K1=K3, so that's how we present it to mbedtls. */
1114 if( key_bits == 128 )
1115 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001116 break;
1117 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001118 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001119 break;
1120 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001121 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001122 break;
1123 default:
1124 return( NULL );
1125 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001126 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001127 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001128
mohammad1603f4f0d612018-06-03 15:04:51 +03001129 return( mbedtls_cipher_info_from_values( cipher_id_tmp, key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001130}
1131
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001132static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001133{
Gilles Peskine2d277862018-06-18 15:41:12 +02001134 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001135 {
1136 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001137 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001138 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001139 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001140 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001141 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001142 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001143 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001144 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001145 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001146 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001147 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001148 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001149 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001150 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001151 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001152 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001153 return( 128 );
1154 default:
1155 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001156 }
1157}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001158
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001159/* Initialize the MAC operation structure. Once this function has been
1160 * called, psa_mac_abort can run and will do the right thing. */
1161static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1162 psa_algorithm_t alg )
1163{
1164 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1165
1166 operation->alg = alg;
1167 operation->key_set = 0;
1168 operation->iv_set = 0;
1169 operation->iv_required = 0;
1170 operation->has_input = 0;
1171 operation->key_usage_sign = 0;
1172 operation->key_usage_verify = 0;
1173
1174#if defined(MBEDTLS_CMAC_C)
1175 if( alg == PSA_ALG_CMAC )
1176 {
1177 operation->iv_required = 0;
1178 mbedtls_cipher_init( &operation->ctx.cmac );
1179 status = PSA_SUCCESS;
1180 }
1181 else
1182#endif /* MBEDTLS_CMAC_C */
1183#if defined(MBEDTLS_MD_C)
1184 if( PSA_ALG_IS_HMAC( operation->alg ) )
1185 {
1186 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1187 PSA_ALG_HMAC_HASH( alg ) );
1188 }
1189 else
1190#endif /* MBEDTLS_MD_C */
1191 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001192 if( ! PSA_ALG_IS_MAC( alg ) )
1193 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001194 }
1195
1196 if( status != PSA_SUCCESS )
1197 memset( operation, 0, sizeof( *operation ) );
1198 return( status );
1199}
1200
Gilles Peskine8c9def32018-02-08 10:02:12 +01001201psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1202{
1203 switch( operation->alg )
1204 {
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001205 case 0:
1206 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001207#if defined(MBEDTLS_CMAC_C)
1208 case PSA_ALG_CMAC:
1209 mbedtls_cipher_free( &operation->ctx.cmac );
1210 break;
1211#endif /* MBEDTLS_CMAC_C */
1212 default:
1213#if defined(MBEDTLS_MD_C)
1214 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001215 {
Gilles Peskine99bc6492018-06-11 17:13:00 +02001216 unsigned int block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001217 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001218
Gilles Peskine99bc6492018-06-11 17:13:00 +02001219 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001220 return( PSA_ERROR_NOT_SUPPORTED );
1221
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001222 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001223 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001224 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001225 else
1226#endif /* MBEDTLS_MD_C */
1227 return( PSA_ERROR_NOT_SUPPORTED );
1228 }
Moran Peker41deec42018-04-04 15:43:05 +03001229
Gilles Peskine8c9def32018-02-08 10:02:12 +01001230 operation->alg = 0;
1231 operation->key_set = 0;
1232 operation->iv_set = 0;
1233 operation->iv_required = 0;
1234 operation->has_input = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001235 operation->key_usage_sign = 0;
1236 operation->key_usage_verify = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001237
Gilles Peskine8c9def32018-02-08 10:02:12 +01001238 return( PSA_SUCCESS );
1239}
1240
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001241#if defined(MBEDTLS_CMAC_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001242static int psa_cmac_start( psa_mac_operation_t *operation,
1243 size_t key_bits,
1244 key_slot_t *slot,
1245 const mbedtls_cipher_info_t *cipher_info )
1246{
1247 int ret;
1248
1249 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001250
1251 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1252 if( ret != 0 )
1253 return( ret );
1254
1255 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1256 slot->data.raw.data,
1257 key_bits );
1258 return( ret );
1259}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001260#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001261
Gilles Peskine248051a2018-06-20 16:09:38 +02001262#if defined(MBEDTLS_MD_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001263static int psa_hmac_start( psa_mac_operation_t *operation,
1264 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001265 key_slot_t *slot,
1266 psa_algorithm_t alg )
1267{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001268 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001269 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001270 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001271 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001272 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001273 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001274 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001275 size_t key_length = slot->data.raw.bytes;
1276 psa_status_t status;
1277
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001278 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001279 return( PSA_ERROR_NOT_SUPPORTED );
1280
1281 if( key_type != PSA_KEY_TYPE_HMAC )
1282 return( PSA_ERROR_INVALID_ARGUMENT );
1283
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001284 operation->mac_size = digest_size;
1285
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001286 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001287 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001288 {
1289 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001290 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001291 if( status != PSA_SUCCESS )
1292 return( status );
1293 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001294 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001295 if( status != PSA_SUCCESS )
1296 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001297 }
Gilles Peskined223b522018-06-11 18:12:58 +02001298 else
1299 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001300
Gilles Peskined223b522018-06-11 18:12:58 +02001301 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1302 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001303 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001304 ipad[i] ^= 0x36;
1305 memset( ipad + key_length, 0x36, block_size - key_length );
1306
1307 /* Copy the key material from ipad to opad, flipping the requisite bits,
1308 * and filling the rest of opad with the requisite constant. */
1309 for( i = 0; i < key_length; i++ )
1310 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1311 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001312
1313 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1314 PSA_ALG_HMAC_HASH( alg ) );
1315 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001316 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001317
1318 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1319 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001320
1321cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001322 mbedtls_zeroize( ipad, key_length );
1323 /* opad is in the context. It needs to stay in memory if this function
1324 * succeeds, and it will be wiped by psa_mac_abort() called from
1325 * psa_mac_start in the error case. */
1326
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001327 return( status );
1328}
Gilles Peskine248051a2018-06-20 16:09:38 +02001329#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001330
Gilles Peskine8c9def32018-02-08 10:02:12 +01001331psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1332 psa_key_slot_t key,
1333 psa_algorithm_t alg )
1334{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001335 psa_status_t status;
1336 key_slot_t *slot;
1337 psa_key_type_t key_type;
1338 size_t key_bits;
Gilles Peskine828ed142018-06-18 23:25:51 +02001339 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001340
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001341 status = psa_mac_init( operation, alg );
1342 if( status != PSA_SUCCESS )
1343 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001344
1345 status = psa_get_key_information( key, &key_type, &key_bits );
1346 if( status != PSA_SUCCESS )
1347 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001348
Gilles Peskine8c9def32018-02-08 10:02:12 +01001349 slot = &global_data.key_slots[key];
Gilles Peskine99bc6492018-06-11 17:13:00 +02001350 if( slot->type == PSA_KEY_TYPE_NONE )
1351 return( PSA_ERROR_EMPTY_SLOT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001352
Moran Pekerd7326592018-05-29 16:56:39 +03001353 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001354 operation->key_usage_sign = 1;
1355
Moran Pekerd7326592018-05-29 16:56:39 +03001356 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001357 operation->key_usage_verify = 1;
1358
Gilles Peskine8c9def32018-02-08 10:02:12 +01001359 if( ! PSA_ALG_IS_HMAC( alg ) )
1360 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001361 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001362 if( cipher_info == NULL )
1363 return( PSA_ERROR_NOT_SUPPORTED );
1364 operation->mac_size = cipher_info->block_size;
1365 }
1366 switch( alg )
1367 {
1368#if defined(MBEDTLS_CMAC_C)
1369 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001370 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1371 key_bits,
1372 slot,
1373 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001374 break;
1375#endif /* MBEDTLS_CMAC_C */
1376 default:
1377#if defined(MBEDTLS_MD_C)
1378 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001379 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001380 else
1381#endif /* MBEDTLS_MD_C */
1382 return( PSA_ERROR_NOT_SUPPORTED );
1383 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001384
Gilles Peskine8c9def32018-02-08 10:02:12 +01001385 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001386 * context may contain data that needs to be wiped on error. */
1387 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001388 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001389 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001390 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001391 else
1392 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001393 operation->key_set = 1;
1394 }
1395 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001396}
1397
1398psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1399 const uint8_t *input,
1400 size_t input_length )
1401{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001402 int ret = 0 ;
1403 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001404 if( ! operation->key_set )
1405 return( PSA_ERROR_BAD_STATE );
1406 if( operation->iv_required && ! operation->iv_set )
1407 return( PSA_ERROR_BAD_STATE );
1408 operation->has_input = 1;
1409
1410 switch( operation->alg )
1411 {
1412#if defined(MBEDTLS_CMAC_C)
1413 case PSA_ALG_CMAC:
1414 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1415 input, input_length );
1416 break;
1417#endif /* MBEDTLS_CMAC_C */
1418 default:
1419#if defined(MBEDTLS_MD_C)
1420 if( PSA_ALG_IS_HMAC( operation->alg ) )
1421 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001422 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001423 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001424 }
1425 else
1426#endif /* MBEDTLS_MD_C */
1427 {
1428 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1429 }
1430 break;
1431 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001432 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001433 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001434 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001435 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001436 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001437 }
1438
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001439 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001440}
1441
mohammad16036df908f2018-04-02 08:34:15 -07001442static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001443 uint8_t *mac,
1444 size_t mac_size,
1445 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001446{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001447 int ret = 0;
1448 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001449 if( ! operation->key_set )
1450 return( PSA_ERROR_BAD_STATE );
1451 if( operation->iv_required && ! operation->iv_set )
1452 return( PSA_ERROR_BAD_STATE );
1453
1454 /* Fill the output buffer with something that isn't a valid mac
1455 * (barring an attack on the mac and deliberately-crafted input),
1456 * in case the caller doesn't check the return status properly. */
1457 *mac_length = operation->mac_size;
1458 memset( mac, '!', mac_size );
1459
1460 if( mac_size < operation->mac_size )
1461 return( PSA_ERROR_BUFFER_TOO_SMALL );
1462
1463 switch( operation->alg )
1464 {
1465#if defined(MBEDTLS_CMAC_C)
1466 case PSA_ALG_CMAC:
1467 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1468 break;
1469#endif /* MBEDTLS_CMAC_C */
1470 default:
1471#if defined(MBEDTLS_MD_C)
1472 if( PSA_ALG_IS_HMAC( operation->alg ) )
1473 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001474 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001475 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001476 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001477 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001478 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001479
Gilles Peskine99bc6492018-06-11 17:13:00 +02001480 if( block_size == 0 )
1481 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001482
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001483 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001484 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001485 if( status != PSA_SUCCESS )
1486 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001487 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001488
1489 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1490 PSA_ALG_HMAC_HASH( operation->alg ) );
1491 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001492 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001493
1494 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001495 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001496 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001497 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001498
1499 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001500 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001501 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001502 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001503
1504 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1505 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001506 hmac_cleanup:
1507 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001508 }
1509 else
1510#endif /* MBEDTLS_MD_C */
1511 {
1512 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1513 }
1514 break;
1515 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001516cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001517
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001518 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001519 {
1520 return( psa_mac_abort( operation ) );
1521 }
1522 else
1523 {
1524 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001525 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001526 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001527
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001528 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001529 }
1530}
1531
mohammad16036df908f2018-04-02 08:34:15 -07001532psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1533 uint8_t *mac,
1534 size_t mac_size,
1535 size_t *mac_length )
1536{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001537 if( ! operation->key_usage_sign )
mohammad16036df908f2018-04-02 08:34:15 -07001538 return( PSA_ERROR_NOT_PERMITTED );
1539
Gilles Peskine99bc6492018-06-11 17:13:00 +02001540 return( psa_mac_finish_internal( operation, mac,
1541 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001542}
1543
Gilles Peskine828ed142018-06-18 23:25:51 +02001544#define PSA_MAC_MAX_SIZE \
Gilles Peskine2d277862018-06-18 15:41:12 +02001545 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1546 MBEDTLS_MD_MAX_SIZE : \
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547 MBEDTLS_MAX_BLOCK_LENGTH )
1548psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1549 const uint8_t *mac,
1550 size_t mac_length )
1551{
Gilles Peskine828ed142018-06-18 23:25:51 +02001552 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001553 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001554 psa_status_t status;
1555
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001556 if( ! operation->key_usage_verify )
mohammad16036df908f2018-04-02 08:34:15 -07001557 return( PSA_ERROR_NOT_PERMITTED );
1558
1559 status = psa_mac_finish_internal( operation,
1560 actual_mac, sizeof( actual_mac ),
1561 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001562 if( status != PSA_SUCCESS )
1563 return( status );
1564 if( actual_mac_length != mac_length )
1565 return( PSA_ERROR_INVALID_SIGNATURE );
1566 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1567 return( PSA_ERROR_INVALID_SIGNATURE );
1568 return( PSA_SUCCESS );
1569}
1570
1571
Gilles Peskine20035e32018-02-03 22:44:14 +01001572
Gilles Peskine20035e32018-02-03 22:44:14 +01001573/****************************************************************/
1574/* Asymmetric cryptography */
1575/****************************************************************/
1576
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001577/* Decode the hash algorithm from alg and store the mbedtls encoding in
1578 * md_alg. Verify that the hash length is consistent. */
1579static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1580 size_t hash_length,
1581 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001582{
Gilles Peskine61b91d42018-06-08 16:09:36 +02001583 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
1584 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1585 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1586 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001587 {
1588#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001589 if( hash_length > UINT_MAX )
1590 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001591#endif
1592 }
1593 else
1594 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001595 if( mbedtls_md_get_size( md_info ) != hash_length )
1596 return( PSA_ERROR_INVALID_ARGUMENT );
1597 if( md_info == NULL )
1598 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001599 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001600 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001601}
1602
Gilles Peskine61b91d42018-06-08 16:09:36 +02001603psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1604 psa_algorithm_t alg,
1605 const uint8_t *hash,
1606 size_t hash_length,
1607 const uint8_t *salt,
1608 size_t salt_length,
1609 uint8_t *signature,
1610 size_t signature_size,
1611 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001612{
1613 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001614 psa_status_t status;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001615 *signature_length = 0;
1616 (void) salt;
1617 (void) salt_length;
1618
Gilles Peskine828ed142018-06-18 23:25:51 +02001619 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine20035e32018-02-03 22:44:14 +01001620 return( PSA_ERROR_EMPTY_SLOT );
1621 slot = &global_data.key_slots[key];
1622 if( slot->type == PSA_KEY_TYPE_NONE )
1623 return( PSA_ERROR_EMPTY_SLOT );
1624 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1625 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001626 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
mohammad160306e79202018-03-28 13:17:44 +03001627 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001628
Gilles Peskine20035e32018-02-03 22:44:14 +01001629#if defined(MBEDTLS_RSA_C)
1630 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1631 {
1632 mbedtls_rsa_context *rsa = slot->data.rsa;
1633 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001634 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001635 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001636 if( status != PSA_SUCCESS )
1637 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001638
Gilles Peskine20035e32018-02-03 22:44:14 +01001639 if( signature_size < rsa->len )
1640 return( PSA_ERROR_BUFFER_TOO_SMALL );
1641#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001642 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001643 {
1644 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1645 MBEDTLS_MD_NONE );
1646 ret = mbedtls_rsa_pkcs1_sign( rsa,
1647 mbedtls_ctr_drbg_random,
1648 &global_data.ctr_drbg,
1649 MBEDTLS_RSA_PRIVATE,
1650 md_alg, hash_length, hash,
1651 signature );
1652 }
1653 else
1654#endif /* MBEDTLS_PKCS1_V15 */
1655#if defined(MBEDTLS_PKCS1_V21)
1656 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1657 {
1658 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1659 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1660 mbedtls_ctr_drbg_random,
1661 &global_data.ctr_drbg,
1662 MBEDTLS_RSA_PRIVATE,
1663 md_alg, hash_length, hash,
1664 signature );
1665 }
1666 else
1667#endif /* MBEDTLS_PKCS1_V21 */
1668 {
1669 return( PSA_ERROR_INVALID_ARGUMENT );
1670 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001671 if( ret == 0 )
1672 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001673 return( mbedtls_to_psa_error( ret ) );
1674 }
1675 else
1676#endif /* defined(MBEDTLS_RSA_C) */
1677#if defined(MBEDTLS_ECP_C)
1678 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1679 {
itayzafrir5c753392018-05-08 11:18:38 +03001680 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1681 int ret;
1682 const mbedtls_md_info_t *md_info;
1683 mbedtls_md_type_t md_alg;
1684 if( signature_size < PSA_ECDSA_SIGNATURE_SIZE( ecdsa->grp.pbits ) )
1685 return( PSA_ERROR_BUFFER_TOO_SMALL );
1686 md_info = mbedtls_md_info_from_psa( alg );
1687 md_alg = mbedtls_md_get_type( md_info );
1688 ret = mbedtls_ecdsa_write_signature( ecdsa, md_alg, hash, hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001689 signature, signature_length,
1690 mbedtls_ctr_drbg_random,
1691 &global_data.ctr_drbg );
itayzafrir5c753392018-05-08 11:18:38 +03001692 return( mbedtls_to_psa_error( ret ) );
1693 }
1694 else
1695#endif /* defined(MBEDTLS_ECP_C) */
1696 {
Gilles Peskine20035e32018-02-03 22:44:14 +01001697 return( PSA_ERROR_NOT_SUPPORTED );
1698 }
itayzafrir5c753392018-05-08 11:18:38 +03001699}
1700
1701psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1702 psa_algorithm_t alg,
1703 const uint8_t *hash,
1704 size_t hash_length,
1705 const uint8_t *salt,
1706 size_t salt_length,
1707 uint8_t *signature,
1708 size_t signature_size )
1709{
1710 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001711 psa_status_t status;
itayzafrir5c753392018-05-08 11:18:38 +03001712 (void) salt;
1713 (void) salt_length;
1714
Gilles Peskine828ed142018-06-18 23:25:51 +02001715 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
itayzafrir5c753392018-05-08 11:18:38 +03001716 return( PSA_ERROR_INVALID_ARGUMENT );
1717 slot = &global_data.key_slots[key];
1718 if( slot->type == PSA_KEY_TYPE_NONE )
1719 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001720 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001721 return( PSA_ERROR_NOT_PERMITTED );
1722
Gilles Peskine61b91d42018-06-08 16:09:36 +02001723#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001724 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1725 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001726 {
1727 mbedtls_rsa_context *rsa = slot->data.rsa;
1728 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001729 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001730 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001731 if( status != PSA_SUCCESS )
1732 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001733
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001734 if( signature_size < rsa->len )
1735 return( PSA_ERROR_BUFFER_TOO_SMALL );
1736#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001737 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001738 {
1739 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1740 MBEDTLS_MD_NONE );
1741
1742 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001743 mbedtls_ctr_drbg_random,
1744 &global_data.ctr_drbg,
1745 MBEDTLS_RSA_PUBLIC,
1746 md_alg,
1747 hash_length,
1748 hash,
1749 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001750
1751 }
1752 else
1753#endif /* MBEDTLS_PKCS1_V15 */
1754#if defined(MBEDTLS_PKCS1_V21)
1755 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1756 {
Gilles Peskinebeb49482018-06-08 17:44:35 +02001757 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1758 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1759 mbedtls_ctr_drbg_random,
1760 &global_data.ctr_drbg,
1761 MBEDTLS_RSA_PUBLIC,
1762 md_alg, hash_length, hash,
1763 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001764 }
1765 else
1766#endif /* MBEDTLS_PKCS1_V21 */
1767 {
1768 return( PSA_ERROR_INVALID_ARGUMENT );
1769 }
1770 return( mbedtls_to_psa_error( ret ) );
1771 }
1772 else
1773#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001774#if defined(MBEDTLS_ECP_C)
1775 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1776 {
1777 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1778 int ret;
Gilles Peskine2d277862018-06-18 15:41:12 +02001779 (void) alg;
Gilles Peskine61b91d42018-06-08 16:09:36 +02001780 ret = mbedtls_ecdsa_read_signature( ecdsa, hash, hash_length,
1781 signature, signature_size );
itayzafrir5c753392018-05-08 11:18:38 +03001782 return( mbedtls_to_psa_error( ret ) );
1783 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001784 else
1785#endif /* defined(MBEDTLS_ECP_C) */
1786 {
1787 return( PSA_ERROR_NOT_SUPPORTED );
1788 }
1789}
1790
Gilles Peskine61b91d42018-06-08 16:09:36 +02001791psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
1792 psa_algorithm_t alg,
1793 const uint8_t *input,
1794 size_t input_length,
1795 const uint8_t *salt,
1796 size_t salt_length,
1797 uint8_t *output,
1798 size_t output_size,
1799 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001800{
1801 key_slot_t *slot;
1802 (void) salt;
1803 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001804 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001805
Gilles Peskine828ed142018-06-18 23:25:51 +02001806 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03001807 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001808 slot = &global_data.key_slots[key];
1809 if( slot->type == PSA_KEY_TYPE_NONE )
1810 return( PSA_ERROR_EMPTY_SLOT );
1811 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1812 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001813 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
1814 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001815
1816#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001817 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1818 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001819 {
1820 mbedtls_rsa_context *rsa = slot->data.rsa;
1821 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001822 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02001823 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001824#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001825 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001826 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001827 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
1828 mbedtls_ctr_drbg_random,
1829 &global_data.ctr_drbg,
1830 MBEDTLS_RSA_PUBLIC,
1831 input_length,
1832 input,
1833 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001834 }
1835 else
1836#endif /* MBEDTLS_PKCS1_V15 */
1837#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001838 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001839 {
1840 return( PSA_ERROR_NOT_SUPPORTED );
1841 }
1842 else
1843#endif /* MBEDTLS_PKCS1_V21 */
1844 {
1845 return( PSA_ERROR_INVALID_ARGUMENT );
1846 }
1847 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001848 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001849 return( mbedtls_to_psa_error( ret ) );
1850 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001851 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001852#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001853 {
1854 return( PSA_ERROR_NOT_SUPPORTED );
1855 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001856}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001857
Gilles Peskine61b91d42018-06-08 16:09:36 +02001858psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
1859 psa_algorithm_t alg,
1860 const uint8_t *input,
1861 size_t input_length,
1862 const uint8_t *salt,
1863 size_t salt_length,
1864 uint8_t *output,
1865 size_t output_size,
1866 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001867{
1868 key_slot_t *slot;
1869 (void) salt;
1870 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001871 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001872
Gilles Peskine828ed142018-06-18 23:25:51 +02001873 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001874 return( PSA_ERROR_EMPTY_SLOT );
1875 slot = &global_data.key_slots[key];
1876 if( slot->type == PSA_KEY_TYPE_NONE )
1877 return( PSA_ERROR_EMPTY_SLOT );
1878 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1879 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001880 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
1881 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001882
1883#if defined(MBEDTLS_RSA_C)
1884 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1885 {
1886 mbedtls_rsa_context *rsa = slot->data.rsa;
1887 int ret;
1888
Gilles Peskinec4def2f2018-06-08 17:53:48 +02001889 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001890 return( PSA_ERROR_INVALID_ARGUMENT );
1891
1892#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001893 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001894 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001895 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
1896 mbedtls_ctr_drbg_random,
1897 &global_data.ctr_drbg,
1898 MBEDTLS_RSA_PRIVATE,
1899 output_length,
1900 input,
1901 output,
1902 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001903 }
1904 else
1905#endif /* MBEDTLS_PKCS1_V15 */
1906#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001907 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001908 {
1909 return( PSA_ERROR_NOT_SUPPORTED );
1910 }
1911 else
1912#endif /* MBEDTLS_PKCS1_V21 */
1913 {
1914 return( PSA_ERROR_INVALID_ARGUMENT );
1915 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001916
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001917 return( mbedtls_to_psa_error( ret ) );
1918 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001919 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001920#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001921 {
1922 return( PSA_ERROR_NOT_SUPPORTED );
1923 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001924}
Gilles Peskine20035e32018-02-03 22:44:14 +01001925
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001926
1927
mohammad1603503973b2018-03-12 15:59:30 +02001928/****************************************************************/
1929/* Symmetric cryptography */
1930/****************************************************************/
1931
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001932/* Initialize the cipher operation structure. Once this function has been
1933 * called, psa_cipher_abort can run and will do the right thing. */
1934static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
1935 psa_algorithm_t alg )
1936{
Gilles Peskinec06e0712018-06-20 16:21:04 +02001937 if( ! PSA_ALG_IS_CIPHER( alg ) )
1938 {
1939 memset( operation, 0, sizeof( *operation ) );
1940 return( PSA_ERROR_INVALID_ARGUMENT );
1941 }
1942
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001943 operation->alg = alg;
1944 operation->key_set = 0;
1945 operation->iv_set = 0;
1946 operation->iv_required = 1;
1947 operation->iv_size = 0;
1948 operation->block_size = 0;
1949 mbedtls_cipher_init( &operation->ctx.cipher );
1950 return( PSA_SUCCESS );
1951}
1952
Gilles Peskinee553c652018-06-04 16:22:46 +02001953static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
1954 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02001955 psa_algorithm_t alg,
1956 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02001957{
1958 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1959 psa_status_t status;
1960 key_slot_t *slot;
1961 psa_key_type_t key_type;
1962 size_t key_bits;
1963 const mbedtls_cipher_info_t *cipher_info = NULL;
1964
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001965 status = psa_cipher_init( operation, alg );
1966 if( status != PSA_SUCCESS )
1967 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02001968
1969 status = psa_get_key_information( key, &key_type, &key_bits );
1970 if( status != PSA_SUCCESS )
1971 return( status );
1972 slot = &global_data.key_slots[key];
1973
Gilles Peskinebb1072f2018-06-08 18:46:05 +02001974 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02001975 if( cipher_info == NULL )
1976 return( PSA_ERROR_NOT_SUPPORTED );
1977
mohammad1603503973b2018-03-12 15:59:30 +02001978 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03001979 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001980 {
1981 psa_cipher_abort( operation );
1982 return( mbedtls_to_psa_error( ret ) );
1983 }
1984
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001985#if defined(MBEDTLS_DES_C)
1986 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
1987 {
1988 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
1989 unsigned char keys[24];
1990 memcpy( keys, slot->data.raw.data, 16 );
1991 memcpy( keys + 16, slot->data.raw.data, 8 );
1992 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
1993 keys,
1994 192, cipher_operation );
1995 }
1996 else
1997#endif
1998 {
1999 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2000 slot->data.raw.data,
2001 key_bits, cipher_operation );
2002 }
Moran Peker41deec42018-04-04 15:43:05 +03002003 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002004 {
2005 psa_cipher_abort( operation );
2006 return( mbedtls_to_psa_error( ret ) );
2007 }
2008
mohammad16038481e742018-03-18 13:57:31 +02002009#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002010 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002011 {
Gilles Peskine53514202018-06-06 15:11:46 +02002012 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2013 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002014
Moran Pekera28258c2018-05-29 16:25:04 +03002015 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002016 {
2017 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2018 mode = MBEDTLS_PADDING_PKCS7;
2019 break;
2020 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2021 mode = MBEDTLS_PADDING_NONE;
2022 break;
2023 default:
Moran Pekerae382792018-05-31 14:06:17 +03002024 psa_cipher_abort( operation );
2025 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002026 }
2027 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002028 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002029 {
2030 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002031 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002032 }
mohammad16038481e742018-03-18 13:57:31 +02002033 }
2034#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2035
mohammad1603503973b2018-03-12 15:59:30 +02002036 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002037 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
2038 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
2039 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002040 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002041 {
mohammad160389e0f462018-04-12 08:48:45 +03002042 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02002043 }
mohammad1603503973b2018-03-12 15:59:30 +02002044
Moran Peker395db872018-05-31 14:07:14 +03002045 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002046}
2047
Gilles Peskinee553c652018-06-04 16:22:46 +02002048psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
2049 psa_key_slot_t key,
2050 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002051{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002052 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002053}
2054
Gilles Peskinee553c652018-06-04 16:22:46 +02002055psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
2056 psa_key_slot_t key,
2057 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002058{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002059 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002060}
2061
Gilles Peskinee553c652018-06-04 16:22:46 +02002062psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
2063 unsigned char *iv,
2064 size_t iv_size,
2065 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002066{
Moran Peker41deec42018-04-04 15:43:05 +03002067 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002068 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002069 return( PSA_ERROR_BAD_STATE );
2070 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002071 {
Moran Peker41deec42018-04-04 15:43:05 +03002072 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2073 goto exit;
2074 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002075 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2076 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002077 if( ret != 0 )
2078 {
2079 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002080 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002081 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002082
mohammad16038481e742018-03-18 13:57:31 +02002083 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03002084 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002085
Moran Peker395db872018-05-31 14:07:14 +03002086exit:
2087 if( ret != PSA_SUCCESS )
2088 psa_cipher_abort( operation );
2089 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002090}
2091
Gilles Peskinee553c652018-06-04 16:22:46 +02002092psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2093 const unsigned char *iv,
2094 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002095{
Moran Peker41deec42018-04-04 15:43:05 +03002096 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002097 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002098 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002099 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002100 {
Moran Pekerae382792018-05-31 14:06:17 +03002101 psa_cipher_abort( operation );
2102 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002103 }
mohammad1603503973b2018-03-12 15:59:30 +02002104 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002105 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002106 {
2107 psa_cipher_abort( operation );
2108 return( mbedtls_to_psa_error( ret ) );
2109 }
2110
2111 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002112
Moran Peker395db872018-05-31 14:07:14 +03002113 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002114}
2115
Gilles Peskinee553c652018-06-04 16:22:46 +02002116psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2117 const uint8_t *input,
2118 size_t input_length,
2119 unsigned char *output,
2120 size_t output_size,
2121 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002122{
2123 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002124 size_t expected_output_size;
2125 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2126 {
2127 /* Take the unprocessed partial block left over from previous
2128 * update calls, if any, plus the input to this call. Remove
2129 * the last partial block, if any. You get the data that will be
2130 * output in this call. */
2131 expected_output_size =
2132 ( operation->ctx.cipher.unprocessed_len + input_length )
2133 / operation->block_size * operation->block_size;
2134 }
2135 else
2136 {
2137 expected_output_size = input_length;
2138 }
2139 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002140 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002141
mohammad1603503973b2018-03-12 15:59:30 +02002142 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002143 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002144 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002145 {
2146 psa_cipher_abort( operation );
2147 return( mbedtls_to_psa_error( ret ) );
2148 }
2149
Moran Peker395db872018-05-31 14:07:14 +03002150 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002151}
2152
Gilles Peskinee553c652018-06-04 16:22:46 +02002153psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2154 uint8_t *output,
2155 size_t output_size,
2156 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002157{
2158 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002159 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002160
mohammad1603503973b2018-03-12 15:59:30 +02002161 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002162 {
Moran Peker7cb22b82018-06-05 11:40:02 +03002163 psa_cipher_abort( operation );
2164 return( PSA_ERROR_BAD_STATE );
2165 }
2166 if( operation->iv_required && ! operation->iv_set )
2167 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002168 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002169 return( PSA_ERROR_BAD_STATE );
2170 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002171 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2172 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002173 {
Gilles Peskine53514202018-06-06 15:11:46 +02002174 psa_algorithm_t padding_mode =
2175 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002176 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002177 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002178 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002179 return( PSA_ERROR_TAMPERING_DETECTED );
2180 }
Gilles Peskine53514202018-06-06 15:11:46 +02002181 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002182 {
2183 if( operation->ctx.cipher.unprocessed_len != 0 )
2184 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002185 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002186 return( PSA_ERROR_INVALID_ARGUMENT );
2187 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002188 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002189 }
2190
2191 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002192 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002193 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002194 {
2195 psa_cipher_abort( operation );
2196 return( mbedtls_to_psa_error( ret ) );
2197 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002198 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002199 memcpy( output, temp_output_buffer, *output_length );
2200 else
2201 {
2202 psa_cipher_abort( operation );
2203 return( PSA_ERROR_BUFFER_TOO_SMALL );
2204 }
mohammad1603503973b2018-03-12 15:59:30 +02002205
Moran Peker4c80d832018-04-22 20:15:31 +03002206 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002207}
2208
Gilles Peskinee553c652018-06-04 16:22:46 +02002209psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2210{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002211 if( operation->alg == 0 )
2212 return( PSA_SUCCESS );
2213
mohammad1603503973b2018-03-12 15:59:30 +02002214 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002215
Moran Peker41deec42018-04-04 15:43:05 +03002216 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002217 operation->key_set = 0;
2218 operation->iv_set = 0;
2219 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002220 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002221 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002222
Moran Peker395db872018-05-31 14:07:14 +03002223 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002224}
2225
Gilles Peskinea0655c32018-04-30 17:06:50 +02002226
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002227
mohammad16038cc1cee2018-03-28 01:21:33 +03002228/****************************************************************/
2229/* Key Policy */
2230/****************************************************************/
2231
Gilles Peskine2d277862018-06-18 15:41:12 +02002232void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002233{
Gilles Peskine803ce742018-06-18 16:07:14 +02002234 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002235}
2236
Gilles Peskine2d277862018-06-18 15:41:12 +02002237void psa_key_policy_set_usage( psa_key_policy_t *policy,
2238 psa_key_usage_t usage,
2239 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002240{
mohammad16034eed7572018-03-28 05:14:59 -07002241 policy->usage = usage;
2242 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002243}
2244
Gilles Peskine2d277862018-06-18 15:41:12 +02002245psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002246{
mohammad16036df908f2018-04-02 08:34:15 -07002247 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002248}
2249
Gilles Peskine2d277862018-06-18 15:41:12 +02002250psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002251{
mohammad16036df908f2018-04-02 08:34:15 -07002252 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002253}
2254
Gilles Peskine2d277862018-06-18 15:41:12 +02002255psa_status_t psa_set_key_policy( psa_key_slot_t key,
2256 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002257{
2258 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002259
Gilles Peskine828ed142018-06-18 23:25:51 +02002260 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002261 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002262
mohammad16038cc1cee2018-03-28 01:21:33 +03002263 slot = &global_data.key_slots[key];
2264 if( slot->type != PSA_KEY_TYPE_NONE )
2265 return( PSA_ERROR_OCCUPIED_SLOT );
2266
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002267 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2268 PSA_KEY_USAGE_ENCRYPT |
2269 PSA_KEY_USAGE_DECRYPT |
2270 PSA_KEY_USAGE_SIGN |
2271 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002272 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002273
mohammad16036df908f2018-04-02 08:34:15 -07002274 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002275
2276 return( PSA_SUCCESS );
2277}
2278
Gilles Peskine2d277862018-06-18 15:41:12 +02002279psa_status_t psa_get_key_policy( psa_key_slot_t key,
2280 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002281{
2282 key_slot_t *slot;
2283
Gilles Peskine828ed142018-06-18 23:25:51 +02002284 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002285 return( PSA_ERROR_INVALID_ARGUMENT );
2286
2287 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002288
mohammad16036df908f2018-04-02 08:34:15 -07002289 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002290
2291 return( PSA_SUCCESS );
2292}
Gilles Peskine20035e32018-02-03 22:44:14 +01002293
Gilles Peskinea0655c32018-04-30 17:06:50 +02002294
2295
mohammad1603804cd712018-03-20 22:44:08 +02002296/****************************************************************/
2297/* Key Lifetime */
2298/****************************************************************/
2299
Gilles Peskine2d277862018-06-18 15:41:12 +02002300psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2301 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002302{
2303 key_slot_t *slot;
2304
Gilles Peskine828ed142018-06-18 23:25:51 +02002305 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002306 return( PSA_ERROR_INVALID_ARGUMENT );
2307
2308 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002309
mohammad1603804cd712018-03-20 22:44:08 +02002310 *lifetime = slot->lifetime;
2311
2312 return( PSA_SUCCESS );
2313}
2314
Gilles Peskine2d277862018-06-18 15:41:12 +02002315psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
2316 const psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002317{
2318 key_slot_t *slot;
2319
Gilles Peskine828ed142018-06-18 23:25:51 +02002320 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002321 return( PSA_ERROR_INVALID_ARGUMENT );
2322
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002323 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2324 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002325 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2326 return( PSA_ERROR_INVALID_ARGUMENT );
2327
mohammad1603804cd712018-03-20 22:44:08 +02002328 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002329 if( slot->type != PSA_KEY_TYPE_NONE )
2330 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002331
Moran Pekerd7326592018-05-29 16:56:39 +03002332 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002333 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002334
mohammad1603060ad8a2018-03-20 14:28:38 -07002335 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002336
2337 return( PSA_SUCCESS );
2338}
2339
Gilles Peskine20035e32018-02-03 22:44:14 +01002340
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002341
mohammad16035955c982018-04-26 00:53:03 +03002342/****************************************************************/
2343/* AEAD */
2344/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002345
mohammad16035955c982018-04-26 00:53:03 +03002346psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2347 psa_algorithm_t alg,
2348 const uint8_t *nonce,
2349 size_t nonce_length,
2350 const uint8_t *additional_data,
2351 size_t additional_data_length,
2352 const uint8_t *plaintext,
2353 size_t plaintext_length,
2354 uint8_t *ciphertext,
2355 size_t ciphertext_size,
2356 size_t *ciphertext_length )
2357{
2358 int ret;
2359 psa_status_t status;
2360 key_slot_t *slot;
2361 psa_key_type_t key_type;
2362 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002363 uint8_t *tag;
2364 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002365 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002366 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002367
mohammad1603f08a5502018-06-03 15:05:47 +03002368 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002369
mohammad16035955c982018-04-26 00:53:03 +03002370 status = psa_get_key_information( key, &key_type, &key_bits );
2371 if( status != PSA_SUCCESS )
2372 return( status );
2373 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002374 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002375 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002376
mohammad16035ed06212018-06-06 13:09:34 +03002377 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2378 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002379 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002380 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002381
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002382 if( ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) == 0 )
mohammad1603f14394b2018-06-04 14:33:19 +03002383 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002384
Gilles Peskine2d277862018-06-18 15:41:12 +02002385 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2386 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002387 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002388
mohammad16035955c982018-04-26 00:53:03 +03002389 if( alg == PSA_ALG_GCM )
2390 {
2391 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002392 tag_length = 16;
2393
mohammad160396910d82018-06-04 14:33:00 +03002394 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2395 return( PSA_ERROR_INVALID_ARGUMENT );
2396
mohammad160315223a82018-06-03 17:19:55 +03002397 //make sure we have place to hold the tag in the ciphertext buffer
2398 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002399 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002400
2401 //update the tag pointer to point to the end of the ciphertext_length
2402 tag = ciphertext + plaintext_length;
2403
mohammad16035955c982018-04-26 00:53:03 +03002404 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002405 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002406 slot->data.raw.data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002407 key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002408 if( ret != 0 )
2409 {
2410 mbedtls_gcm_free( &gcm );
2411 return( mbedtls_to_psa_error( ret ) );
2412 }
2413 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002414 plaintext_length, nonce,
2415 nonce_length, additional_data,
2416 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002417 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002418 mbedtls_gcm_free( &gcm );
2419 }
2420 else if( alg == PSA_ALG_CCM )
2421 {
2422 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002423 tag_length = 16;
2424
mohammad160396910d82018-06-04 14:33:00 +03002425 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2426 return( PSA_ERROR_INVALID_ARGUMENT );
2427
mohammad160347ddf3d2018-04-26 01:11:21 +03002428 if( nonce_length < 7 || nonce_length > 13 )
2429 return( PSA_ERROR_INVALID_ARGUMENT );
2430
mohammad160315223a82018-06-03 17:19:55 +03002431 //make sure we have place to hold the tag in the ciphertext buffer
2432 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002433 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002434
2435 //update the tag pointer to point to the end of the ciphertext_length
2436 tag = ciphertext + plaintext_length;
2437
mohammad16035955c982018-04-26 00:53:03 +03002438 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002439 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002440 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002441 if( ret != 0 )
2442 {
2443 mbedtls_ccm_free( &ccm );
2444 return( mbedtls_to_psa_error( ret ) );
2445 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002446 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002447 nonce, nonce_length,
2448 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002449 additional_data_length,
2450 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002451 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002452 mbedtls_ccm_free( &ccm );
2453 }
mohammad16035c8845f2018-05-09 05:40:09 -07002454 else
2455 {
mohammad1603554faad2018-06-03 15:07:38 +03002456 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002457 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002458
mohammad160315223a82018-06-03 17:19:55 +03002459 if( ret != 0 )
2460 {
2461 memset( ciphertext, 0, ciphertext_size );
2462 return( mbedtls_to_psa_error( ret ) );
2463 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002464
mohammad160315223a82018-06-03 17:19:55 +03002465 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002466 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002467}
2468
Gilles Peskineee652a32018-06-01 19:23:52 +02002469/* Locate the tag in a ciphertext buffer containing the encrypted data
2470 * followed by the tag. Return the length of the part preceding the tag in
2471 * *plaintext_length. This is the size of the plaintext in modes where
2472 * the encrypted data has the same size as the plaintext, such as
2473 * CCM and GCM. */
2474static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2475 const uint8_t *ciphertext,
2476 size_t ciphertext_length,
2477 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002478 const uint8_t **p_tag )
2479{
2480 size_t payload_length;
2481 if( tag_length > ciphertext_length )
2482 return( PSA_ERROR_INVALID_ARGUMENT );
2483 payload_length = ciphertext_length - tag_length;
2484 if( payload_length > plaintext_size )
2485 return( PSA_ERROR_BUFFER_TOO_SMALL );
2486 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002487 return( PSA_SUCCESS );
2488}
2489
mohammad16035955c982018-04-26 00:53:03 +03002490psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2491 psa_algorithm_t alg,
2492 const uint8_t *nonce,
2493 size_t nonce_length,
2494 const uint8_t *additional_data,
2495 size_t additional_data_length,
2496 const uint8_t *ciphertext,
2497 size_t ciphertext_length,
2498 uint8_t *plaintext,
2499 size_t plaintext_size,
2500 size_t *plaintext_length )
2501{
2502 int ret;
2503 psa_status_t status;
2504 key_slot_t *slot;
2505 psa_key_type_t key_type;
2506 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002507 const uint8_t *tag;
2508 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002509 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002510 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002511
Gilles Peskineee652a32018-06-01 19:23:52 +02002512 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002513
mohammad16035955c982018-04-26 00:53:03 +03002514 status = psa_get_key_information( key, &key_type, &key_bits );
2515 if( status != PSA_SUCCESS )
2516 return( status );
2517 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002518 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002519 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002520
mohammad16035ed06212018-06-06 13:09:34 +03002521 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2522 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002523 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002524 return( PSA_ERROR_NOT_SUPPORTED );
2525
mohammad1603f14394b2018-06-04 14:33:19 +03002526 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2527 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002528
Gilles Peskine2d277862018-06-18 15:41:12 +02002529 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2530 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002531 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002532
mohammad16035955c982018-04-26 00:53:03 +03002533 if( alg == PSA_ALG_GCM )
2534 {
2535 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002536
Gilles Peskineee652a32018-06-01 19:23:52 +02002537 tag_length = 16;
2538 status = psa_aead_unpadded_locate_tag( tag_length,
2539 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002540 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002541 if( status != PSA_SUCCESS )
2542 return( status );
2543
mohammad16035955c982018-04-26 00:53:03 +03002544 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002545 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002546 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002547 if( ret != 0 )
2548 {
2549 mbedtls_gcm_free( &gcm );
2550 return( mbedtls_to_psa_error( ret ) );
2551 }
mohammad16035955c982018-04-26 00:53:03 +03002552
Gilles Peskineee652a32018-06-01 19:23:52 +02002553 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002554 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002555 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002556 additional_data,
2557 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002558 tag, tag_length,
2559 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002560 mbedtls_gcm_free( &gcm );
2561 }
2562 else if( alg == PSA_ALG_CCM )
2563 {
2564 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002565
mohammad160347ddf3d2018-04-26 01:11:21 +03002566 if( nonce_length < 7 || nonce_length > 13 )
2567 return( PSA_ERROR_INVALID_ARGUMENT );
2568
mohammad16039375f842018-06-03 14:28:24 +03002569 tag_length = 16;
2570 status = psa_aead_unpadded_locate_tag( tag_length,
2571 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002572 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002573 if( status != PSA_SUCCESS )
2574 return( status );
2575
mohammad16035955c982018-04-26 00:53:03 +03002576 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002577 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002578 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002579 if( ret != 0 )
2580 {
2581 mbedtls_ccm_free( &ccm );
2582 return( mbedtls_to_psa_error( ret ) );
2583 }
mohammad160360a64d02018-06-03 17:20:42 +03002584 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002585 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002586 additional_data,
2587 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002588 ciphertext, plaintext,
2589 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002590 mbedtls_ccm_free( &ccm );
2591 }
mohammad160339574652018-06-01 04:39:53 -07002592 else
2593 {
mohammad1603554faad2018-06-03 15:07:38 +03002594 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002595 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002596
Gilles Peskineee652a32018-06-01 19:23:52 +02002597 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002598 memset( plaintext, 0, plaintext_size );
2599 else
2600 *plaintext_length = ciphertext_length - tag_length;
2601
Gilles Peskineee652a32018-06-01 19:23:52 +02002602 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002603}
2604
Gilles Peskinea0655c32018-04-30 17:06:50 +02002605
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002606
Gilles Peskine20035e32018-02-03 22:44:14 +01002607/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002608/* Key generation */
2609/****************************************************************/
2610
2611psa_status_t psa_generate_random( uint8_t *output,
2612 size_t output_size )
2613{
2614 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2615 output, output_size );
2616 return( mbedtls_to_psa_error( ret ) );
2617}
2618
2619psa_status_t psa_generate_key( psa_key_slot_t key,
2620 psa_key_type_t type,
2621 size_t bits,
2622 const void *parameters,
2623 size_t parameters_size )
2624{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002625 key_slot_t *slot;
2626
2627 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
2628 return( PSA_ERROR_INVALID_ARGUMENT );
2629 slot = &global_data.key_slots[key];
2630 if( slot->type != PSA_KEY_TYPE_NONE )
2631 return( PSA_ERROR_OCCUPIED_SLOT );
2632 if( parameters == NULL && parameters_size != 0 )
2633 return( PSA_ERROR_INVALID_ARGUMENT );
2634
2635 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
2636 {
2637 psa_status_t status = prepare_raw_data_slot( type, bits,
2638 &slot->data.raw );
2639 if( status != PSA_SUCCESS )
2640 return( status );
2641 status = psa_generate_random( slot->data.raw.data,
2642 slot->data.raw.bytes );
2643 if( status != PSA_SUCCESS )
2644 {
2645 mbedtls_free( slot->data.raw.data );
2646 return( status );
2647 }
2648#if defined(MBEDTLS_DES_C)
2649 if( type == PSA_KEY_TYPE_DES )
2650 {
2651 mbedtls_des_key_set_parity( slot->data.raw.data );
2652 if( slot->data.raw.bytes >= 16 )
2653 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2654 if( slot->data.raw.bytes == 24 )
2655 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2656 }
2657#endif /* MBEDTLS_DES_C */
2658 }
2659 else
2660
2661#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2662 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2663 {
2664 mbedtls_rsa_context *rsa;
2665 int ret;
2666 int exponent = 65537;
2667 if( parameters != NULL )
2668 {
2669 const unsigned *p = parameters;
2670 if( parameters_size != sizeof( *p ) )
2671 return( PSA_ERROR_INVALID_ARGUMENT );
2672 if( *p > INT_MAX )
2673 return( PSA_ERROR_INVALID_ARGUMENT );
2674 exponent = *p;
2675 }
2676 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
2677 if( rsa == NULL )
2678 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2679 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
2680 ret = mbedtls_rsa_gen_key( rsa,
2681 mbedtls_ctr_drbg_random,
2682 &global_data.ctr_drbg,
2683 bits,
2684 exponent );
2685 if( ret != 0 )
2686 {
2687 mbedtls_rsa_free( rsa );
2688 mbedtls_free( rsa );
2689 return( mbedtls_to_psa_error( ret ) );
2690 }
2691 slot->data.rsa = rsa;
2692 }
2693 else
2694#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
2695
2696#if defined(MBEDTLS_ECP_C)
2697 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
2698 {
2699 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
2700 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
2701 const mbedtls_ecp_curve_info *curve_info =
2702 mbedtls_ecp_curve_info_from_grp_id( grp_id );
2703 mbedtls_ecp_keypair *ecp;
2704 int ret;
2705 if( parameters != NULL )
2706 return( PSA_ERROR_NOT_SUPPORTED );
2707 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
2708 return( PSA_ERROR_NOT_SUPPORTED );
2709 if( curve_info->bit_size != bits )
2710 return( PSA_ERROR_INVALID_ARGUMENT );
2711 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
2712 if( ecp == NULL )
2713 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2714 mbedtls_ecp_keypair_init( ecp );
2715 ret = mbedtls_ecp_gen_key( grp_id, ecp,
2716 mbedtls_ctr_drbg_random,
2717 &global_data.ctr_drbg );
2718 if( ret != 0 )
2719 {
2720 mbedtls_ecp_keypair_free( ecp );
2721 mbedtls_free( ecp );
2722 return( mbedtls_to_psa_error( ret ) );
2723 }
2724 slot->data.ecp = ecp;
2725 }
2726 else
2727#endif /* MBEDTLS_ECP_C */
2728
2729 return( PSA_ERROR_NOT_SUPPORTED );
2730
2731 slot->type = type;
2732 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02002733}
2734
2735
2736/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002737/* Module setup */
2738/****************************************************************/
2739
Gilles Peskinee59236f2018-01-27 23:32:46 +01002740void mbedtls_psa_crypto_free( void )
2741{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002742 size_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02002743 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002744 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002745 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2746 mbedtls_entropy_free( &global_data.entropy );
2747 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2748}
2749
2750psa_status_t psa_crypto_init( void )
2751{
2752 int ret;
2753 const unsigned char drbg_seed[] = "PSA";
2754
2755 if( global_data.initialized != 0 )
2756 return( PSA_SUCCESS );
2757
2758 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2759 mbedtls_entropy_init( &global_data.entropy );
2760 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2761
2762 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2763 mbedtls_entropy_func,
2764 &global_data.entropy,
2765 drbg_seed, sizeof( drbg_seed ) - 1 );
2766 if( ret != 0 )
2767 goto exit;
2768
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002769 global_data.initialized = 1;
2770
Gilles Peskinee59236f2018-01-27 23:32:46 +01002771exit:
2772 if( ret != 0 )
2773 mbedtls_psa_crypto_free( );
2774 return( mbedtls_to_psa_error( ret ) );
2775}
2776
2777#endif /* MBEDTLS_PSA_CRYPTO_C */