blob: d75226cc6900613f58d443ebe5874c54b0a23e07 [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"
42#include "mbedtls/blowfish.h"
43#include "mbedtls/camellia.h"
44#include "mbedtls/cipher.h"
45#include "mbedtls/ccm.h"
46#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010047#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010048#include "mbedtls/des.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010049#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010050#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/error.h"
52#include "mbedtls/gcm.h"
53#include "mbedtls/md2.h"
54#include "mbedtls/md4.h"
55#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010056#include "mbedtls/md.h"
57#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010058#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010059#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010061#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/sha1.h"
63#include "mbedtls/sha256.h"
64#include "mbedtls/sha512.h"
65#include "mbedtls/xtea.h"
66
Gilles Peskinee59236f2018-01-27 23:32:46 +010067
68
69/* Implementation that should never be optimized out by the compiler */
70static void mbedtls_zeroize( void *v, size_t n )
71{
72 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
73}
74
Gilles Peskine9ef733f2018-02-07 21:05:37 +010075/* constant-time buffer comparison */
76static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
77{
78 size_t i;
79 unsigned char diff = 0;
80
81 for( i = 0; i < n; i++ )
82 diff |= a[i] ^ b[i];
83
84 return( diff );
85}
86
87
88
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010089/****************************************************************/
90/* Global data, support functions and library management */
91/****************************************************************/
92
93/* Number of key slots (plus one because 0 is not used).
94 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +020095#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010096
Gilles Peskine2d277862018-06-18 15:41:12 +020097typedef struct
98{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010099 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300100 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200101 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200102 union
103 {
104 struct raw_data
105 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100106 uint8_t *data;
107 size_t bytes;
108 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100109#if defined(MBEDTLS_RSA_C)
110 mbedtls_rsa_context *rsa;
111#endif /* MBEDTLS_RSA_C */
112#if defined(MBEDTLS_ECP_C)
113 mbedtls_ecp_keypair *ecp;
114#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100115 } data;
116} key_slot_t;
117
Gilles Peskine2d277862018-06-18 15:41:12 +0200118typedef struct
119{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120 int initialized;
121 mbedtls_entropy_context entropy;
122 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200123 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100124} psa_global_data_t;
125
126static psa_global_data_t global_data;
127
128static psa_status_t mbedtls_to_psa_error( int ret )
129{
Gilles Peskinea5905292018-02-07 20:59:33 +0100130 /* If there's both a high-level code and low-level code, dispatch on
131 * the high-level code. */
132 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133 {
134 case 0:
135 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100136
137 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
138 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
139 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
140 return( PSA_ERROR_NOT_SUPPORTED );
141 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
142 return( PSA_ERROR_HARDWARE_FAILURE );
143
144 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
148 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
149 return( PSA_ERROR_NOT_SUPPORTED );
150 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
151 return( PSA_ERROR_HARDWARE_FAILURE );
152
153 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
154 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
155 return( PSA_ERROR_NOT_SUPPORTED );
156 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
157 return( PSA_ERROR_HARDWARE_FAILURE );
158
159 case MBEDTLS_ERR_CCM_BAD_INPUT:
160 return( PSA_ERROR_INVALID_ARGUMENT );
161 case MBEDTLS_ERR_CCM_AUTH_FAILED:
162 return( PSA_ERROR_INVALID_SIGNATURE );
163 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
164 return( PSA_ERROR_HARDWARE_FAILURE );
165
166 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
169 return( PSA_ERROR_INVALID_ARGUMENT );
170 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
171 return( PSA_ERROR_INSUFFICIENT_MEMORY );
172 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
173 return( PSA_ERROR_INVALID_PADDING );
174 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
175 return( PSA_ERROR_BAD_STATE );
176 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
177 return( PSA_ERROR_INVALID_SIGNATURE );
178 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
179 return( PSA_ERROR_TAMPERING_DETECTED );
180 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
181 return( PSA_ERROR_HARDWARE_FAILURE );
182
183 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
186 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
187 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
188 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
189 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
190 return( PSA_ERROR_NOT_SUPPORTED );
191 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
192 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
193
194 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
195 return( PSA_ERROR_NOT_SUPPORTED );
196 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
197 return( PSA_ERROR_HARDWARE_FAILURE );
198
Gilles Peskinee59236f2018-01-27 23:32:46 +0100199 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
200 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
201 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
202 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100203
204 case MBEDTLS_ERR_GCM_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_GCM_BAD_INPUT:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
211 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
212 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
213 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
219 return( PSA_ERROR_INVALID_ARGUMENT );
220 case MBEDTLS_ERR_MD_ALLOC_FAILED:
221 return( PSA_ERROR_INSUFFICIENT_MEMORY );
222 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
223 return( PSA_ERROR_STORAGE_FAILURE );
224 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
225 return( PSA_ERROR_HARDWARE_FAILURE );
226
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100227 case MBEDTLS_ERR_PK_ALLOC_FAILED:
228 return( PSA_ERROR_INSUFFICIENT_MEMORY );
229 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
230 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
231 return( PSA_ERROR_INVALID_ARGUMENT );
232 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100233 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100234 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
235 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
236 return( PSA_ERROR_INVALID_ARGUMENT );
237 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
238 return( PSA_ERROR_NOT_SUPPORTED );
239 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
240 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
241 return( PSA_ERROR_NOT_PERMITTED );
242 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
243 return( PSA_ERROR_INVALID_ARGUMENT );
244 case MBEDTLS_ERR_PK_INVALID_ALG:
245 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
246 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
247 return( PSA_ERROR_NOT_SUPPORTED );
248 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
249 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100250 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
251 return( PSA_ERROR_HARDWARE_FAILURE );
252
253 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
256 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_RSA_INVALID_PADDING:
259 return( PSA_ERROR_INVALID_PADDING );
260 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
261 return( PSA_ERROR_HARDWARE_FAILURE );
262 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
265 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
266 return( PSA_ERROR_TAMPERING_DETECTED );
267 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
268 return( PSA_ERROR_INVALID_SIGNATURE );
269 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
270 return( PSA_ERROR_BUFFER_TOO_SMALL );
271 case MBEDTLS_ERR_RSA_RNG_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
274 return( PSA_ERROR_NOT_SUPPORTED );
275 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
276 return( PSA_ERROR_HARDWARE_FAILURE );
277
278 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
279 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
280 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
281 return( PSA_ERROR_HARDWARE_FAILURE );
282
283 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
286 return( PSA_ERROR_HARDWARE_FAILURE );
287
itayzafrir5c753392018-05-08 11:18:38 +0300288 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300289 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300290 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300291 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
292 return( PSA_ERROR_BUFFER_TOO_SMALL );
293 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
296 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
297 return( PSA_ERROR_INVALID_SIGNATURE );
298 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
299 return( PSA_ERROR_INSUFFICIENT_MEMORY );
300 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300302
Gilles Peskinee59236f2018-01-27 23:32:46 +0100303 default:
304 return( PSA_ERROR_UNKNOWN_ERROR );
305 }
306}
307
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200308
309
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100310/****************************************************************/
311/* Key management */
312/****************************************************************/
313
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200314static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
315{
316 switch( grpid )
317 {
318 case MBEDTLS_ECP_DP_SECP192R1:
319 return( PSA_ECC_CURVE_SECP192R1 );
320 case MBEDTLS_ECP_DP_SECP224R1:
321 return( PSA_ECC_CURVE_SECP224R1 );
322 case MBEDTLS_ECP_DP_SECP256R1:
323 return( PSA_ECC_CURVE_SECP256R1 );
324 case MBEDTLS_ECP_DP_SECP384R1:
325 return( PSA_ECC_CURVE_SECP384R1 );
326 case MBEDTLS_ECP_DP_SECP521R1:
327 return( PSA_ECC_CURVE_SECP521R1 );
328 case MBEDTLS_ECP_DP_BP256R1:
329 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
330 case MBEDTLS_ECP_DP_BP384R1:
331 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
332 case MBEDTLS_ECP_DP_BP512R1:
333 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
334 case MBEDTLS_ECP_DP_CURVE25519:
335 return( PSA_ECC_CURVE_CURVE25519 );
336 case MBEDTLS_ECP_DP_SECP192K1:
337 return( PSA_ECC_CURVE_SECP192K1 );
338 case MBEDTLS_ECP_DP_SECP224K1:
339 return( PSA_ECC_CURVE_SECP224K1 );
340 case MBEDTLS_ECP_DP_SECP256K1:
341 return( PSA_ECC_CURVE_SECP256K1 );
342 case MBEDTLS_ECP_DP_CURVE448:
343 return( PSA_ECC_CURVE_CURVE448 );
344 default:
345 return( 0 );
346 }
347}
348
Gilles Peskine12313cd2018-06-20 00:20:32 +0200349static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
350{
351 switch( curve )
352 {
353 case PSA_ECC_CURVE_SECP192R1:
354 return( MBEDTLS_ECP_DP_SECP192R1 );
355 case PSA_ECC_CURVE_SECP224R1:
356 return( MBEDTLS_ECP_DP_SECP224R1 );
357 case PSA_ECC_CURVE_SECP256R1:
358 return( MBEDTLS_ECP_DP_SECP256R1 );
359 case PSA_ECC_CURVE_SECP384R1:
360 return( MBEDTLS_ECP_DP_SECP384R1 );
361 case PSA_ECC_CURVE_SECP521R1:
362 return( MBEDTLS_ECP_DP_SECP521R1 );
363 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
364 return( MBEDTLS_ECP_DP_BP256R1 );
365 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
366 return( MBEDTLS_ECP_DP_BP384R1 );
367 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
368 return( MBEDTLS_ECP_DP_BP512R1 );
369 case PSA_ECC_CURVE_CURVE25519:
370 return( MBEDTLS_ECP_DP_CURVE25519 );
371 case PSA_ECC_CURVE_SECP192K1:
372 return( MBEDTLS_ECP_DP_SECP192K1 );
373 case PSA_ECC_CURVE_SECP224K1:
374 return( MBEDTLS_ECP_DP_SECP224K1 );
375 case PSA_ECC_CURVE_SECP256K1:
376 return( MBEDTLS_ECP_DP_SECP256K1 );
377 case PSA_ECC_CURVE_CURVE448:
378 return( MBEDTLS_ECP_DP_CURVE448 );
379 default:
380 return( MBEDTLS_ECP_DP_NONE );
381 }
382}
383
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200384static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
385 size_t bits,
386 struct raw_data *raw )
387{
388 /* Check that the bit size is acceptable for the key type */
389 switch( type )
390 {
391 case PSA_KEY_TYPE_RAW_DATA:
392#if defined(MBEDTLS_MD_C)
393 case PSA_KEY_TYPE_HMAC:
394#endif
395 break;
396#if defined(MBEDTLS_AES_C)
397 case PSA_KEY_TYPE_AES:
398 if( bits != 128 && bits != 192 && bits != 256 )
399 return( PSA_ERROR_INVALID_ARGUMENT );
400 break;
401#endif
402#if defined(MBEDTLS_CAMELLIA_C)
403 case PSA_KEY_TYPE_CAMELLIA:
404 if( bits != 128 && bits != 192 && bits != 256 )
405 return( PSA_ERROR_INVALID_ARGUMENT );
406 break;
407#endif
408#if defined(MBEDTLS_DES_C)
409 case PSA_KEY_TYPE_DES:
410 if( bits != 64 && bits != 128 && bits != 192 )
411 return( PSA_ERROR_INVALID_ARGUMENT );
412 break;
413#endif
414#if defined(MBEDTLS_ARC4_C)
415 case PSA_KEY_TYPE_ARC4:
416 if( bits < 8 || bits > 2048 )
417 return( PSA_ERROR_INVALID_ARGUMENT );
418 break;
419#endif
420 default:
421 return( PSA_ERROR_NOT_SUPPORTED );
422 }
423
424 /* Allocate memory for the key */
425 raw->bytes = PSA_BITS_TO_BYTES( bits );
426 raw->data = mbedtls_calloc( 1, raw->bytes );
427 if( raw->data == NULL )
428 {
429 raw->bytes = 0;
430 return( PSA_ERROR_INSUFFICIENT_MEMORY );
431 }
432 return( PSA_SUCCESS );
433}
434
Gilles Peskine2d277862018-06-18 15:41:12 +0200435psa_status_t psa_import_key( psa_key_slot_t key,
436 psa_key_type_t type,
437 const uint8_t *data,
438 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100439{
440 key_slot_t *slot;
441
Gilles Peskine828ed142018-06-18 23:25:51 +0200442 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100443 return( PSA_ERROR_INVALID_ARGUMENT );
444 slot = &global_data.key_slots[key];
445 if( slot->type != PSA_KEY_TYPE_NONE )
446 return( PSA_ERROR_OCCUPIED_SLOT );
447
Gilles Peskine8c9def32018-02-08 10:02:12 +0100448 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100449 {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450 psa_status_t status;
Gilles Peskine6d912132018-03-07 16:41:37 +0100451 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100452 if( data_length > SIZE_MAX / 8 )
453 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200454 status = prepare_raw_data_slot( type,
455 PSA_BYTES_TO_BITS( data_length ),
456 &slot->data.raw );
457 if( status != PSA_SUCCESS )
458 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100459 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100460 }
461 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100462#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100463 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
464 type == PSA_KEY_TYPE_RSA_KEYPAIR ||
465 PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100466 {
467 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100468 mbedtls_pk_context pk;
469 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100470 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
471 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
472 else
473 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100474 if( ret != 0 )
475 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100476 switch( mbedtls_pk_get_type( &pk ) )
477 {
478#if defined(MBEDTLS_RSA_C)
479 case MBEDTLS_PK_RSA:
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100480 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
481 type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200482 slot->data.rsa = mbedtls_pk_rsa( pk );
Gilles Peskine969ac722018-01-28 18:16:59 +0100483 else
484 return( PSA_ERROR_INVALID_ARGUMENT );
485 break;
486#endif /* MBEDTLS_RSA_C */
487#if defined(MBEDTLS_ECP_C)
488 case MBEDTLS_PK_ECKEY:
489 if( PSA_KEY_TYPE_IS_ECC( type ) )
490 {
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200491 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
492 psa_ecc_curve_t actual_curve =
493 mbedtls_ecc_group_to_psa( ecp->grp.id );
494 psa_ecc_curve_t expected_curve =
495 PSA_KEY_TYPE_GET_CURVE( type );
496 if( actual_curve != expected_curve )
497 return( PSA_ERROR_INVALID_ARGUMENT );
498 slot->data.ecp = ecp;
Gilles Peskine969ac722018-01-28 18:16:59 +0100499 }
500 else
501 return( PSA_ERROR_INVALID_ARGUMENT );
502 break;
503#endif /* MBEDTLS_ECP_C */
504 default:
505 return( PSA_ERROR_INVALID_ARGUMENT );
506 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 }
508 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100509#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510 {
511 return( PSA_ERROR_NOT_SUPPORTED );
512 }
513
514 slot->type = type;
515 return( PSA_SUCCESS );
516}
517
Gilles Peskine2d277862018-06-18 15:41:12 +0200518psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100519{
520 key_slot_t *slot;
521
Gilles Peskine828ed142018-06-18 23:25:51 +0200522 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100523 return( PSA_ERROR_INVALID_ARGUMENT );
524 slot = &global_data.key_slots[key];
525 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200526 {
527 /* No key material to clean, but do zeroize the slot below to wipe
528 * metadata such as policies. */
529 }
530 else if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100531 {
532 mbedtls_free( slot->data.raw.data );
533 }
534 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100535#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100536 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
537 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100538 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100539 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100540 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541 }
542 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100543#endif /* defined(MBEDTLS_RSA_C) */
544#if defined(MBEDTLS_ECP_C)
545 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
546 {
547 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100548 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100549 }
550 else
551#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 {
553 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100554 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100555 return( PSA_ERROR_TAMPERING_DETECTED );
556 }
557
558 mbedtls_zeroize( slot, sizeof( *slot ) );
559 return( PSA_SUCCESS );
560}
561
Gilles Peskine2d277862018-06-18 15:41:12 +0200562psa_status_t psa_get_key_information( psa_key_slot_t key,
563 psa_key_type_t *type,
564 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565{
566 key_slot_t *slot;
567
Gilles Peskine828ed142018-06-18 23:25:51 +0200568 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100569 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570 slot = &global_data.key_slots[key];
571 if( type != NULL )
572 *type = slot->type;
573 if( bits != NULL )
574 *bits = 0;
575 if( slot->type == PSA_KEY_TYPE_NONE )
576 return( PSA_ERROR_EMPTY_SLOT );
577
Gilles Peskine8c9def32018-02-08 10:02:12 +0100578 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100579 {
580 if( bits != NULL )
581 *bits = slot->data.raw.bytes * 8;
582 }
583 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100584#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100585 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
586 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100587 {
588 if( bits != NULL )
Gilles Peskine969ac722018-01-28 18:16:59 +0100589 *bits = mbedtls_rsa_get_bitlen( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100590 }
591 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100592#endif /* defined(MBEDTLS_RSA_C) */
593#if defined(MBEDTLS_ECP_C)
594 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
595 {
596 if( bits != NULL )
597 *bits = slot->data.ecp->grp.pbits;
598 }
599 else
600#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 {
602 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100603 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100604 return( PSA_ERROR_TAMPERING_DETECTED );
605 }
606
607 return( PSA_SUCCESS );
608}
609
Gilles Peskine2d277862018-06-18 15:41:12 +0200610static psa_status_t psa_internal_export_key( psa_key_slot_t key,
611 uint8_t *data,
612 size_t data_size,
613 size_t *data_length,
614 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100615{
616 key_slot_t *slot;
617
Gilles Peskine828ed142018-06-18 23:25:51 +0200618 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100619 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100620 slot = &global_data.key_slots[key];
621 if( slot->type == PSA_KEY_TYPE_NONE )
622 return( PSA_ERROR_EMPTY_SLOT );
623
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200624 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300625 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300626
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200627 if( ! export_public_key &&
628 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) &&
629 ( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) == 0 )
Moran Peker87567632018-06-04 18:41:37 +0300630 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine2d277862018-06-18 15:41:12 +0200631
Gilles Peskine8c9def32018-02-08 10:02:12 +0100632 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100633 {
634 if( slot->data.raw.bytes > data_size )
635 return( PSA_ERROR_BUFFER_TOO_SMALL );
636 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
637 *data_length = slot->data.raw.bytes;
638 return( PSA_SUCCESS );
639 }
640 else
Moran Peker17e36e12018-05-02 12:55:20 +0300641 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100642#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100643 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
Moran Pekera998bc62018-04-16 18:16:20 +0300644 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
645 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100646 {
Moran Pekera998bc62018-04-16 18:16:20 +0300647 mbedtls_pk_context pk;
648 int ret;
649 mbedtls_pk_init( &pk );
650 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
651 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
652 {
653 pk.pk_info = &mbedtls_rsa_info;
654 pk.pk_ctx = slot->data.rsa;
655 }
656 else
657 {
658 pk.pk_info = &mbedtls_eckey_info;
659 pk.pk_ctx = slot->data.ecp;
660 }
Moran Pekerd7326592018-05-29 16:56:39 +0300661 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300662 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300663 else
664 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300665 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200666 {
667 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300668 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200669 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200670 /* The mbedtls_pk_xxx functions write to the end of the buffer.
671 * Move the data to the beginning and erase remaining data
672 * at the original location. */
673 if( 2 * (size_t) ret <= data_size )
674 {
675 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200676 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200677 }
678 else if( (size_t) ret < data_size )
679 {
680 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200681 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200682 }
Moran Pekera998bc62018-04-16 18:16:20 +0300683 *data_length = ret;
684 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100685 }
686 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100687#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300688 {
689 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200690 it is valid for a special-purpose implementation to omit
691 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300692 return( PSA_ERROR_NOT_SUPPORTED );
693 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100694 }
695}
696
Gilles Peskine2d277862018-06-18 15:41:12 +0200697psa_status_t psa_export_key( psa_key_slot_t key,
698 uint8_t *data,
699 size_t data_size,
700 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300701{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200702 return( psa_internal_export_key( key, data, data_size,
703 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100705
Gilles Peskine2d277862018-06-18 15:41:12 +0200706psa_status_t psa_export_public_key( psa_key_slot_t key,
707 uint8_t *data,
708 size_t data_size,
709 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300710{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200711 return( psa_internal_export_key( key, data, data_size,
712 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300713}
714
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200715
716
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100717/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100718/* Message digests */
719/****************************************************************/
720
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100721static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100722{
723 switch( alg )
724 {
725#if defined(MBEDTLS_MD2_C)
726 case PSA_ALG_MD2:
727 return( &mbedtls_md2_info );
728#endif
729#if defined(MBEDTLS_MD4_C)
730 case PSA_ALG_MD4:
731 return( &mbedtls_md4_info );
732#endif
733#if defined(MBEDTLS_MD5_C)
734 case PSA_ALG_MD5:
735 return( &mbedtls_md5_info );
736#endif
737#if defined(MBEDTLS_RIPEMD160_C)
738 case PSA_ALG_RIPEMD160:
739 return( &mbedtls_ripemd160_info );
740#endif
741#if defined(MBEDTLS_SHA1_C)
742 case PSA_ALG_SHA_1:
743 return( &mbedtls_sha1_info );
744#endif
745#if defined(MBEDTLS_SHA256_C)
746 case PSA_ALG_SHA_224:
747 return( &mbedtls_sha224_info );
748 case PSA_ALG_SHA_256:
749 return( &mbedtls_sha256_info );
750#endif
751#if defined(MBEDTLS_SHA512_C)
752 case PSA_ALG_SHA_384:
753 return( &mbedtls_sha384_info );
754 case PSA_ALG_SHA_512:
755 return( &mbedtls_sha512_info );
756#endif
757 default:
758 return( NULL );
759 }
760}
761
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100762psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
763{
764 switch( operation->alg )
765 {
766#if defined(MBEDTLS_MD2_C)
767 case PSA_ALG_MD2:
768 mbedtls_md2_free( &operation->ctx.md2 );
769 break;
770#endif
771#if defined(MBEDTLS_MD4_C)
772 case PSA_ALG_MD4:
773 mbedtls_md4_free( &operation->ctx.md4 );
774 break;
775#endif
776#if defined(MBEDTLS_MD5_C)
777 case PSA_ALG_MD5:
778 mbedtls_md5_free( &operation->ctx.md5 );
779 break;
780#endif
781#if defined(MBEDTLS_RIPEMD160_C)
782 case PSA_ALG_RIPEMD160:
783 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
784 break;
785#endif
786#if defined(MBEDTLS_SHA1_C)
787 case PSA_ALG_SHA_1:
788 mbedtls_sha1_free( &operation->ctx.sha1 );
789 break;
790#endif
791#if defined(MBEDTLS_SHA256_C)
792 case PSA_ALG_SHA_224:
793 case PSA_ALG_SHA_256:
794 mbedtls_sha256_free( &operation->ctx.sha256 );
795 break;
796#endif
797#if defined(MBEDTLS_SHA512_C)
798 case PSA_ALG_SHA_384:
799 case PSA_ALG_SHA_512:
800 mbedtls_sha512_free( &operation->ctx.sha512 );
801 break;
802#endif
803 default:
804 return( PSA_ERROR_NOT_SUPPORTED );
805 }
806 operation->alg = 0;
807 return( PSA_SUCCESS );
808}
809
810psa_status_t psa_hash_start( psa_hash_operation_t *operation,
811 psa_algorithm_t alg )
812{
813 int ret;
814 operation->alg = 0;
815 switch( alg )
816 {
817#if defined(MBEDTLS_MD2_C)
818 case PSA_ALG_MD2:
819 mbedtls_md2_init( &operation->ctx.md2 );
820 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
821 break;
822#endif
823#if defined(MBEDTLS_MD4_C)
824 case PSA_ALG_MD4:
825 mbedtls_md4_init( &operation->ctx.md4 );
826 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
827 break;
828#endif
829#if defined(MBEDTLS_MD5_C)
830 case PSA_ALG_MD5:
831 mbedtls_md5_init( &operation->ctx.md5 );
832 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
833 break;
834#endif
835#if defined(MBEDTLS_RIPEMD160_C)
836 case PSA_ALG_RIPEMD160:
837 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
838 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
839 break;
840#endif
841#if defined(MBEDTLS_SHA1_C)
842 case PSA_ALG_SHA_1:
843 mbedtls_sha1_init( &operation->ctx.sha1 );
844 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
845 break;
846#endif
847#if defined(MBEDTLS_SHA256_C)
848 case PSA_ALG_SHA_224:
849 mbedtls_sha256_init( &operation->ctx.sha256 );
850 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
851 break;
852 case PSA_ALG_SHA_256:
853 mbedtls_sha256_init( &operation->ctx.sha256 );
854 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
855 break;
856#endif
857#if defined(MBEDTLS_SHA512_C)
858 case PSA_ALG_SHA_384:
859 mbedtls_sha512_init( &operation->ctx.sha512 );
860 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
861 break;
862 case PSA_ALG_SHA_512:
863 mbedtls_sha512_init( &operation->ctx.sha512 );
864 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
865 break;
866#endif
867 default:
868 return( PSA_ERROR_NOT_SUPPORTED );
869 }
870 if( ret == 0 )
871 operation->alg = alg;
872 else
873 psa_hash_abort( operation );
874 return( mbedtls_to_psa_error( ret ) );
875}
876
877psa_status_t psa_hash_update( psa_hash_operation_t *operation,
878 const uint8_t *input,
879 size_t input_length )
880{
881 int ret;
882 switch( operation->alg )
883 {
884#if defined(MBEDTLS_MD2_C)
885 case PSA_ALG_MD2:
886 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
887 input, input_length );
888 break;
889#endif
890#if defined(MBEDTLS_MD4_C)
891 case PSA_ALG_MD4:
892 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
893 input, input_length );
894 break;
895#endif
896#if defined(MBEDTLS_MD5_C)
897 case PSA_ALG_MD5:
898 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
899 input, input_length );
900 break;
901#endif
902#if defined(MBEDTLS_RIPEMD160_C)
903 case PSA_ALG_RIPEMD160:
904 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
905 input, input_length );
906 break;
907#endif
908#if defined(MBEDTLS_SHA1_C)
909 case PSA_ALG_SHA_1:
910 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
911 input, input_length );
912 break;
913#endif
914#if defined(MBEDTLS_SHA256_C)
915 case PSA_ALG_SHA_224:
916 case PSA_ALG_SHA_256:
917 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
918 input, input_length );
919 break;
920#endif
921#if defined(MBEDTLS_SHA512_C)
922 case PSA_ALG_SHA_384:
923 case PSA_ALG_SHA_512:
924 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
925 input, input_length );
926 break;
927#endif
928 default:
929 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
930 break;
931 }
932 if( ret != 0 )
933 psa_hash_abort( operation );
934 return( mbedtls_to_psa_error( ret ) );
935}
936
937psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
938 uint8_t *hash,
939 size_t hash_size,
940 size_t *hash_length )
941{
942 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200943 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100944
945 /* Fill the output buffer with something that isn't a valid hash
946 * (barring an attack on the hash and deliberately-crafted input),
947 * in case the caller doesn't check the return status properly. */
948 *hash_length = actual_hash_length;
949 memset( hash, '!', hash_size );
950
951 if( hash_size < actual_hash_length )
952 return( PSA_ERROR_BUFFER_TOO_SMALL );
953
954 switch( operation->alg )
955 {
956#if defined(MBEDTLS_MD2_C)
957 case PSA_ALG_MD2:
958 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
959 break;
960#endif
961#if defined(MBEDTLS_MD4_C)
962 case PSA_ALG_MD4:
963 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
964 break;
965#endif
966#if defined(MBEDTLS_MD5_C)
967 case PSA_ALG_MD5:
968 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
969 break;
970#endif
971#if defined(MBEDTLS_RIPEMD160_C)
972 case PSA_ALG_RIPEMD160:
973 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
974 break;
975#endif
976#if defined(MBEDTLS_SHA1_C)
977 case PSA_ALG_SHA_1:
978 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
979 break;
980#endif
981#if defined(MBEDTLS_SHA256_C)
982 case PSA_ALG_SHA_224:
983 case PSA_ALG_SHA_256:
984 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
985 break;
986#endif
987#if defined(MBEDTLS_SHA512_C)
988 case PSA_ALG_SHA_384:
989 case PSA_ALG_SHA_512:
990 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
991 break;
992#endif
993 default:
994 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
995 break;
996 }
997
998 if( ret == 0 )
999 {
1000 return( psa_hash_abort( operation ) );
1001 }
1002 else
1003 {
1004 psa_hash_abort( operation );
1005 return( mbedtls_to_psa_error( ret ) );
1006 }
1007}
1008
Gilles Peskine2d277862018-06-18 15:41:12 +02001009psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1010 const uint8_t *hash,
1011 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001012{
1013 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1014 size_t actual_hash_length;
1015 psa_status_t status = psa_hash_finish( operation,
1016 actual_hash, sizeof( actual_hash ),
1017 &actual_hash_length );
1018 if( status != PSA_SUCCESS )
1019 return( status );
1020 if( actual_hash_length != hash_length )
1021 return( PSA_ERROR_INVALID_SIGNATURE );
1022 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1023 return( PSA_ERROR_INVALID_SIGNATURE );
1024 return( PSA_SUCCESS );
1025}
1026
1027
1028
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001029/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001030/* MAC */
1031/****************************************************************/
1032
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001033static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001034 psa_algorithm_t alg,
1035 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001036 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001037 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001038{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001039 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001040 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001041
1042 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1043 {
1044 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001045 {
1046 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1047 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001048
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001049 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001050 {
1051 case PSA_ALG_STREAM_CIPHER:
1052 mode = MBEDTLS_MODE_STREAM;
1053 break;
1054 case PSA_ALG_CBC_BASE:
1055 mode = MBEDTLS_MODE_CBC;
1056 break;
1057 case PSA_ALG_CFB_BASE:
1058 mode = MBEDTLS_MODE_CFB;
1059 break;
1060 case PSA_ALG_OFB_BASE:
1061 mode = MBEDTLS_MODE_OFB;
1062 break;
1063 case PSA_ALG_CTR:
1064 mode = MBEDTLS_MODE_CTR;
1065 break;
1066 case PSA_ALG_CCM:
1067 mode = MBEDTLS_MODE_CCM;
1068 break;
1069 case PSA_ALG_GCM:
1070 mode = MBEDTLS_MODE_GCM;
1071 break;
1072 default:
1073 return( NULL );
1074 }
1075 }
1076 else if( alg == PSA_ALG_CMAC )
1077 mode = MBEDTLS_MODE_ECB;
1078 else if( alg == PSA_ALG_GMAC )
1079 mode = MBEDTLS_MODE_GCM;
1080 else
1081 return( NULL );
1082
1083 switch( key_type )
1084 {
1085 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001086 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001087 break;
1088 case PSA_KEY_TYPE_DES:
1089 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001090 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001091 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001092 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001093 break;
1094 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001095 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001096 break;
1097 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001098 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001099 break;
1100 default:
1101 return( NULL );
1102 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001103 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001104 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001105
mohammad1603f4f0d612018-06-03 15:04:51 +03001106 return( mbedtls_cipher_info_from_values( cipher_id_tmp, key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001107}
1108
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001109static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001110{
Gilles Peskine2d277862018-06-18 15:41:12 +02001111 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001112 {
1113 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001114 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001115 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001116 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001117 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001118 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001119 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001120 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001121 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001122 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001123 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001124 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001125 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001126 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001127 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001128 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001129 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001130 return( 128 );
1131 default:
1132 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001133 }
1134}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001135
Gilles Peskine8c9def32018-02-08 10:02:12 +01001136psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1137{
1138 switch( operation->alg )
1139 {
1140#if defined(MBEDTLS_CMAC_C)
1141 case PSA_ALG_CMAC:
1142 mbedtls_cipher_free( &operation->ctx.cmac );
1143 break;
1144#endif /* MBEDTLS_CMAC_C */
1145 default:
1146#if defined(MBEDTLS_MD_C)
1147 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001148 {
Gilles Peskine99bc6492018-06-11 17:13:00 +02001149 unsigned int block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001150 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001151
Gilles Peskine99bc6492018-06-11 17:13:00 +02001152 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001153 return( PSA_ERROR_NOT_SUPPORTED );
1154
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001155 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001156 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001157 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001158 else
1159#endif /* MBEDTLS_MD_C */
1160 return( PSA_ERROR_NOT_SUPPORTED );
1161 }
Moran Peker41deec42018-04-04 15:43:05 +03001162
Gilles Peskine8c9def32018-02-08 10:02:12 +01001163 operation->alg = 0;
1164 operation->key_set = 0;
1165 operation->iv_set = 0;
1166 operation->iv_required = 0;
1167 operation->has_input = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001168
Gilles Peskine8c9def32018-02-08 10:02:12 +01001169 return( PSA_SUCCESS );
1170}
1171
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001172#if defined(MBEDTLS_CMAC_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001173static int psa_cmac_start( psa_mac_operation_t *operation,
1174 size_t key_bits,
1175 key_slot_t *slot,
1176 const mbedtls_cipher_info_t *cipher_info )
1177{
1178 int ret;
1179
1180 operation->mac_size = cipher_info->block_size;
1181 operation->iv_required = 0;
1182 mbedtls_cipher_init( &operation->ctx.cmac );
1183
1184 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1185 if( ret != 0 )
1186 return( ret );
1187
1188 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1189 slot->data.raw.data,
1190 key_bits );
1191 return( ret );
1192}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001193#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001194
1195static int psa_hmac_start( psa_mac_operation_t *operation,
1196 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001197 key_slot_t *slot,
1198 psa_algorithm_t alg )
1199{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001200 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001201 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001202 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001203 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001204 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001205 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001206 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001207 size_t key_length = slot->data.raw.bytes;
1208 psa_status_t status;
1209
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001210 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001211 return( PSA_ERROR_NOT_SUPPORTED );
1212
1213 if( key_type != PSA_KEY_TYPE_HMAC )
1214 return( PSA_ERROR_INVALID_ARGUMENT );
1215
1216 operation->iv_required = 0;
1217 operation->mac_size = digest_size;
1218
1219 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1220 PSA_ALG_HMAC_HASH( alg ) );
1221 if( status != PSA_SUCCESS )
1222 return( status );
1223
Gilles Peskined223b522018-06-11 18:12:58 +02001224 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001225 {
1226 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001227 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001228 if( status != PSA_SUCCESS )
1229 return( status );
1230 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001231 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001232 if( status != PSA_SUCCESS )
1233 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001234 }
Gilles Peskined223b522018-06-11 18:12:58 +02001235 else
1236 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001237
Gilles Peskined223b522018-06-11 18:12:58 +02001238 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1239 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001240 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001241 ipad[i] ^= 0x36;
1242 memset( ipad + key_length, 0x36, block_size - key_length );
1243
1244 /* Copy the key material from ipad to opad, flipping the requisite bits,
1245 * and filling the rest of opad with the requisite constant. */
1246 for( i = 0; i < key_length; i++ )
1247 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1248 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001249
1250 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1251 PSA_ALG_HMAC_HASH( alg ) );
1252 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001253 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001254
1255 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1256 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001257
1258cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001259 mbedtls_zeroize( ipad, key_length );
1260 /* opad is in the context. It needs to stay in memory if this function
1261 * succeeds, and it will be wiped by psa_mac_abort() called from
1262 * psa_mac_start in the error case. */
1263
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001264 return( status );
1265}
1266
Gilles Peskine8c9def32018-02-08 10:02:12 +01001267psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1268 psa_key_slot_t key,
1269 psa_algorithm_t alg )
1270{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001271 psa_status_t status;
1272 key_slot_t *slot;
1273 psa_key_type_t key_type;
1274 size_t key_bits;
Gilles Peskine828ed142018-06-18 23:25:51 +02001275 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001276
1277 operation->alg = 0;
1278 operation->key_set = 0;
1279 operation->iv_set = 0;
1280 operation->iv_required = 1;
1281 operation->has_input = 0;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001282 operation->key_usage_sign = 0;
1283 operation->key_usage_verify = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001284
1285 status = psa_get_key_information( key, &key_type, &key_bits );
1286 if( status != PSA_SUCCESS )
1287 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001288
Gilles Peskine8c9def32018-02-08 10:02:12 +01001289 slot = &global_data.key_slots[key];
Gilles Peskine99bc6492018-06-11 17:13:00 +02001290 if( slot->type == PSA_KEY_TYPE_NONE )
1291 return( PSA_ERROR_EMPTY_SLOT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001292
Moran Pekerd7326592018-05-29 16:56:39 +03001293 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001294 operation->key_usage_sign = 1;
1295
Moran Pekerd7326592018-05-29 16:56:39 +03001296 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001297 operation->key_usage_verify = 1;
1298
Gilles Peskine8c9def32018-02-08 10:02:12 +01001299 if( ! PSA_ALG_IS_HMAC( alg ) )
1300 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001301 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001302 if( cipher_info == NULL )
1303 return( PSA_ERROR_NOT_SUPPORTED );
1304 operation->mac_size = cipher_info->block_size;
1305 }
1306 switch( alg )
1307 {
1308#if defined(MBEDTLS_CMAC_C)
1309 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001310 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1311 key_bits,
1312 slot,
1313 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001314 break;
1315#endif /* MBEDTLS_CMAC_C */
1316 default:
1317#if defined(MBEDTLS_MD_C)
1318 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001319 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001320 else
1321#endif /* MBEDTLS_MD_C */
1322 return( PSA_ERROR_NOT_SUPPORTED );
1323 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001324
Gilles Peskine8c9def32018-02-08 10:02:12 +01001325 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001326
1327 * context may contain data that needs to be wiped on error. */
1328 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001329 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001330 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001331 }
Gilles Peskine99bc6492018-06-11 17:13:00 +02001332
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001333 else
1334 {
1335 operation->alg = alg;
1336 operation->key_set = 1;
1337 }
1338 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001339}
1340
1341psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1342 const uint8_t *input,
1343 size_t input_length )
1344{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001345 int ret = 0 ;
1346 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001347 if( ! operation->key_set )
1348 return( PSA_ERROR_BAD_STATE );
1349 if( operation->iv_required && ! operation->iv_set )
1350 return( PSA_ERROR_BAD_STATE );
1351 operation->has_input = 1;
1352
1353 switch( operation->alg )
1354 {
1355#if defined(MBEDTLS_CMAC_C)
1356 case PSA_ALG_CMAC:
1357 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1358 input, input_length );
1359 break;
1360#endif /* MBEDTLS_CMAC_C */
1361 default:
1362#if defined(MBEDTLS_MD_C)
1363 if( PSA_ALG_IS_HMAC( operation->alg ) )
1364 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001365 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001366 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001367 }
1368 else
1369#endif /* MBEDTLS_MD_C */
1370 {
1371 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1372 }
1373 break;
1374 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001375 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001376 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001377 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001378 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001379 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001380 }
1381
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001382 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383}
1384
mohammad16036df908f2018-04-02 08:34:15 -07001385static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001386 uint8_t *mac,
1387 size_t mac_size,
1388 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001389{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001390 int ret = 0;
1391 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001392 if( ! operation->key_set )
1393 return( PSA_ERROR_BAD_STATE );
1394 if( operation->iv_required && ! operation->iv_set )
1395 return( PSA_ERROR_BAD_STATE );
1396
1397 /* Fill the output buffer with something that isn't a valid mac
1398 * (barring an attack on the mac and deliberately-crafted input),
1399 * in case the caller doesn't check the return status properly. */
1400 *mac_length = operation->mac_size;
1401 memset( mac, '!', mac_size );
1402
1403 if( mac_size < operation->mac_size )
1404 return( PSA_ERROR_BUFFER_TOO_SMALL );
1405
1406 switch( operation->alg )
1407 {
1408#if defined(MBEDTLS_CMAC_C)
1409 case PSA_ALG_CMAC:
1410 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1411 break;
1412#endif /* MBEDTLS_CMAC_C */
1413 default:
1414#if defined(MBEDTLS_MD_C)
1415 if( PSA_ALG_IS_HMAC( operation->alg ) )
1416 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001417 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001418 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001419 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001420 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001421 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001422
Gilles Peskine99bc6492018-06-11 17:13:00 +02001423 if( block_size == 0 )
1424 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001425
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001426 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001427 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001428 if( status != PSA_SUCCESS )
1429 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001430 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001431
1432 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1433 PSA_ALG_HMAC_HASH( operation->alg ) );
1434 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001435 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001436
1437 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001438 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001439 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001440 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001441
1442 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001443 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001444 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001445 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001446
1447 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1448 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001449 hmac_cleanup:
1450 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001451 }
1452 else
1453#endif /* MBEDTLS_MD_C */
1454 {
1455 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1456 }
1457 break;
1458 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001459cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001460
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001461 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001462 {
1463 return( psa_mac_abort( operation ) );
1464 }
1465 else
1466 {
1467 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001468 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001469 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001470
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001471 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001472 }
1473}
1474
mohammad16036df908f2018-04-02 08:34:15 -07001475psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1476 uint8_t *mac,
1477 size_t mac_size,
1478 size_t *mac_length )
1479{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001480 if( ! operation->key_usage_sign )
mohammad16036df908f2018-04-02 08:34:15 -07001481 return( PSA_ERROR_NOT_PERMITTED );
1482
Gilles Peskine99bc6492018-06-11 17:13:00 +02001483 return( psa_mac_finish_internal( operation, mac,
1484 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001485}
1486
Gilles Peskine828ed142018-06-18 23:25:51 +02001487#define PSA_MAC_MAX_SIZE \
Gilles Peskine2d277862018-06-18 15:41:12 +02001488 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1489 MBEDTLS_MD_MAX_SIZE : \
Gilles Peskine8c9def32018-02-08 10:02:12 +01001490 MBEDTLS_MAX_BLOCK_LENGTH )
1491psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1492 const uint8_t *mac,
1493 size_t mac_length )
1494{
Gilles Peskine828ed142018-06-18 23:25:51 +02001495 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001496 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001497 psa_status_t status;
1498
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001499 if( ! operation->key_usage_verify )
mohammad16036df908f2018-04-02 08:34:15 -07001500 return( PSA_ERROR_NOT_PERMITTED );
1501
1502 status = psa_mac_finish_internal( operation,
1503 actual_mac, sizeof( actual_mac ),
1504 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001505 if( status != PSA_SUCCESS )
1506 return( status );
1507 if( actual_mac_length != mac_length )
1508 return( PSA_ERROR_INVALID_SIGNATURE );
1509 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1510 return( PSA_ERROR_INVALID_SIGNATURE );
1511 return( PSA_SUCCESS );
1512}
1513
1514
Gilles Peskine20035e32018-02-03 22:44:14 +01001515
Gilles Peskine20035e32018-02-03 22:44:14 +01001516/****************************************************************/
1517/* Asymmetric cryptography */
1518/****************************************************************/
1519
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001520/* Decode the hash algorithm from alg and store the mbedtls encoding in
1521 * md_alg. Verify that the hash length is consistent. */
1522static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1523 size_t hash_length,
1524 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001525{
Gilles Peskine61b91d42018-06-08 16:09:36 +02001526 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
1527 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1528 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1529 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001530 {
1531#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001532 if( hash_length > UINT_MAX )
1533 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001534#endif
1535 }
1536 else
1537 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001538 if( mbedtls_md_get_size( md_info ) != hash_length )
1539 return( PSA_ERROR_INVALID_ARGUMENT );
1540 if( md_info == NULL )
1541 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001542 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001543 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001544}
1545
Gilles Peskine61b91d42018-06-08 16:09:36 +02001546psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1547 psa_algorithm_t alg,
1548 const uint8_t *hash,
1549 size_t hash_length,
1550 const uint8_t *salt,
1551 size_t salt_length,
1552 uint8_t *signature,
1553 size_t signature_size,
1554 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001555{
1556 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001557 psa_status_t status;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001558 *signature_length = 0;
1559 (void) salt;
1560 (void) salt_length;
1561
Gilles Peskine828ed142018-06-18 23:25:51 +02001562 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine20035e32018-02-03 22:44:14 +01001563 return( PSA_ERROR_EMPTY_SLOT );
1564 slot = &global_data.key_slots[key];
1565 if( slot->type == PSA_KEY_TYPE_NONE )
1566 return( PSA_ERROR_EMPTY_SLOT );
1567 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1568 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001569 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
mohammad160306e79202018-03-28 13:17:44 +03001570 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001571
Gilles Peskine20035e32018-02-03 22:44:14 +01001572#if defined(MBEDTLS_RSA_C)
1573 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1574 {
1575 mbedtls_rsa_context *rsa = slot->data.rsa;
1576 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001577 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001578 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001579 if( status != PSA_SUCCESS )
1580 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001581
Gilles Peskine20035e32018-02-03 22:44:14 +01001582 if( signature_size < rsa->len )
1583 return( PSA_ERROR_BUFFER_TOO_SMALL );
1584#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001585 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001586 {
1587 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1588 MBEDTLS_MD_NONE );
1589 ret = mbedtls_rsa_pkcs1_sign( rsa,
1590 mbedtls_ctr_drbg_random,
1591 &global_data.ctr_drbg,
1592 MBEDTLS_RSA_PRIVATE,
1593 md_alg, hash_length, hash,
1594 signature );
1595 }
1596 else
1597#endif /* MBEDTLS_PKCS1_V15 */
1598#if defined(MBEDTLS_PKCS1_V21)
1599 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1600 {
1601 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1602 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1603 mbedtls_ctr_drbg_random,
1604 &global_data.ctr_drbg,
1605 MBEDTLS_RSA_PRIVATE,
1606 md_alg, hash_length, hash,
1607 signature );
1608 }
1609 else
1610#endif /* MBEDTLS_PKCS1_V21 */
1611 {
1612 return( PSA_ERROR_INVALID_ARGUMENT );
1613 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001614 if( ret == 0 )
1615 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001616 return( mbedtls_to_psa_error( ret ) );
1617 }
1618 else
1619#endif /* defined(MBEDTLS_RSA_C) */
1620#if defined(MBEDTLS_ECP_C)
1621 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1622 {
itayzafrir5c753392018-05-08 11:18:38 +03001623 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1624 int ret;
1625 const mbedtls_md_info_t *md_info;
1626 mbedtls_md_type_t md_alg;
1627 if( signature_size < PSA_ECDSA_SIGNATURE_SIZE( ecdsa->grp.pbits ) )
1628 return( PSA_ERROR_BUFFER_TOO_SMALL );
1629 md_info = mbedtls_md_info_from_psa( alg );
1630 md_alg = mbedtls_md_get_type( md_info );
1631 ret = mbedtls_ecdsa_write_signature( ecdsa, md_alg, hash, hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001632 signature, signature_length,
1633 mbedtls_ctr_drbg_random,
1634 &global_data.ctr_drbg );
itayzafrir5c753392018-05-08 11:18:38 +03001635 return( mbedtls_to_psa_error( ret ) );
1636 }
1637 else
1638#endif /* defined(MBEDTLS_ECP_C) */
1639 {
Gilles Peskine20035e32018-02-03 22:44:14 +01001640 return( PSA_ERROR_NOT_SUPPORTED );
1641 }
itayzafrir5c753392018-05-08 11:18:38 +03001642}
1643
1644psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1645 psa_algorithm_t alg,
1646 const uint8_t *hash,
1647 size_t hash_length,
1648 const uint8_t *salt,
1649 size_t salt_length,
1650 uint8_t *signature,
1651 size_t signature_size )
1652{
1653 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001654 psa_status_t status;
itayzafrir5c753392018-05-08 11:18:38 +03001655 (void) salt;
1656 (void) salt_length;
1657
Gilles Peskine828ed142018-06-18 23:25:51 +02001658 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
itayzafrir5c753392018-05-08 11:18:38 +03001659 return( PSA_ERROR_INVALID_ARGUMENT );
1660 slot = &global_data.key_slots[key];
1661 if( slot->type == PSA_KEY_TYPE_NONE )
1662 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001663 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001664 return( PSA_ERROR_NOT_PERMITTED );
1665
Gilles Peskine61b91d42018-06-08 16:09:36 +02001666#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001667 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1668 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001669 {
1670 mbedtls_rsa_context *rsa = slot->data.rsa;
1671 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001672 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001673 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001674 if( status != PSA_SUCCESS )
1675 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001676
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001677 if( signature_size < rsa->len )
1678 return( PSA_ERROR_BUFFER_TOO_SMALL );
1679#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001680 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001681 {
1682 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1683 MBEDTLS_MD_NONE );
1684
1685 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001686 mbedtls_ctr_drbg_random,
1687 &global_data.ctr_drbg,
1688 MBEDTLS_RSA_PUBLIC,
1689 md_alg,
1690 hash_length,
1691 hash,
1692 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001693
1694 }
1695 else
1696#endif /* MBEDTLS_PKCS1_V15 */
1697#if defined(MBEDTLS_PKCS1_V21)
1698 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1699 {
Gilles Peskinebeb49482018-06-08 17:44:35 +02001700 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1701 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1702 mbedtls_ctr_drbg_random,
1703 &global_data.ctr_drbg,
1704 MBEDTLS_RSA_PUBLIC,
1705 md_alg, hash_length, hash,
1706 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001707 }
1708 else
1709#endif /* MBEDTLS_PKCS1_V21 */
1710 {
1711 return( PSA_ERROR_INVALID_ARGUMENT );
1712 }
1713 return( mbedtls_to_psa_error( ret ) );
1714 }
1715 else
1716#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001717#if defined(MBEDTLS_ECP_C)
1718 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1719 {
1720 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1721 int ret;
Gilles Peskine2d277862018-06-18 15:41:12 +02001722 (void) alg;
Gilles Peskine61b91d42018-06-08 16:09:36 +02001723 ret = mbedtls_ecdsa_read_signature( ecdsa, hash, hash_length,
1724 signature, signature_size );
itayzafrir5c753392018-05-08 11:18:38 +03001725 return( mbedtls_to_psa_error( ret ) );
1726 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001727 else
1728#endif /* defined(MBEDTLS_ECP_C) */
1729 {
1730 return( PSA_ERROR_NOT_SUPPORTED );
1731 }
1732}
1733
Gilles Peskine61b91d42018-06-08 16:09:36 +02001734psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
1735 psa_algorithm_t alg,
1736 const uint8_t *input,
1737 size_t input_length,
1738 const uint8_t *salt,
1739 size_t salt_length,
1740 uint8_t *output,
1741 size_t output_size,
1742 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001743{
1744 key_slot_t *slot;
1745 (void) salt;
1746 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001747 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001748
Gilles Peskine828ed142018-06-18 23:25:51 +02001749 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03001750 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001751 slot = &global_data.key_slots[key];
1752 if( slot->type == PSA_KEY_TYPE_NONE )
1753 return( PSA_ERROR_EMPTY_SLOT );
1754 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1755 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001756 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
1757 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001758
1759#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001760 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1761 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001762 {
1763 mbedtls_rsa_context *rsa = slot->data.rsa;
1764 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001765 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02001766 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001767#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001768 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001769 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001770 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
1771 mbedtls_ctr_drbg_random,
1772 &global_data.ctr_drbg,
1773 MBEDTLS_RSA_PUBLIC,
1774 input_length,
1775 input,
1776 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001777 }
1778 else
1779#endif /* MBEDTLS_PKCS1_V15 */
1780#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001781 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001782 {
1783 return( PSA_ERROR_NOT_SUPPORTED );
1784 }
1785 else
1786#endif /* MBEDTLS_PKCS1_V21 */
1787 {
1788 return( PSA_ERROR_INVALID_ARGUMENT );
1789 }
1790 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001791 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001792 return( mbedtls_to_psa_error( ret ) );
1793 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001794 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001795#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001796 {
1797 return( PSA_ERROR_NOT_SUPPORTED );
1798 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001799}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001800
Gilles Peskine61b91d42018-06-08 16:09:36 +02001801psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
1802 psa_algorithm_t alg,
1803 const uint8_t *input,
1804 size_t input_length,
1805 const uint8_t *salt,
1806 size_t salt_length,
1807 uint8_t *output,
1808 size_t output_size,
1809 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001810{
1811 key_slot_t *slot;
1812 (void) salt;
1813 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001814 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001815
Gilles Peskine828ed142018-06-18 23:25:51 +02001816 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001817 return( PSA_ERROR_EMPTY_SLOT );
1818 slot = &global_data.key_slots[key];
1819 if( slot->type == PSA_KEY_TYPE_NONE )
1820 return( PSA_ERROR_EMPTY_SLOT );
1821 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1822 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001823 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
1824 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001825
1826#if defined(MBEDTLS_RSA_C)
1827 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1828 {
1829 mbedtls_rsa_context *rsa = slot->data.rsa;
1830 int ret;
1831
Gilles Peskinec4def2f2018-06-08 17:53:48 +02001832 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001833 return( PSA_ERROR_INVALID_ARGUMENT );
1834
1835#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001836 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001837 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001838 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
1839 mbedtls_ctr_drbg_random,
1840 &global_data.ctr_drbg,
1841 MBEDTLS_RSA_PRIVATE,
1842 output_length,
1843 input,
1844 output,
1845 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001846 }
1847 else
1848#endif /* MBEDTLS_PKCS1_V15 */
1849#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001850 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001851 {
1852 return( PSA_ERROR_NOT_SUPPORTED );
1853 }
1854 else
1855#endif /* MBEDTLS_PKCS1_V21 */
1856 {
1857 return( PSA_ERROR_INVALID_ARGUMENT );
1858 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001859
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001860 return( mbedtls_to_psa_error( ret ) );
1861 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001862 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001863#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001864 {
1865 return( PSA_ERROR_NOT_SUPPORTED );
1866 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001867}
Gilles Peskine20035e32018-02-03 22:44:14 +01001868
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001869
1870
mohammad1603503973b2018-03-12 15:59:30 +02001871/****************************************************************/
1872/* Symmetric cryptography */
1873/****************************************************************/
1874
Gilles Peskinee553c652018-06-04 16:22:46 +02001875static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
1876 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02001877 psa_algorithm_t alg,
1878 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02001879{
1880 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1881 psa_status_t status;
1882 key_slot_t *slot;
1883 psa_key_type_t key_type;
1884 size_t key_bits;
1885 const mbedtls_cipher_info_t *cipher_info = NULL;
1886
Moran Peker41deec42018-04-04 15:43:05 +03001887 operation->alg = alg;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001888 operation->key_set = 0;
1889 operation->iv_set = 0;
1890 operation->iv_required = 1;
1891 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001892 operation->block_size = 0;
mohammad1603503973b2018-03-12 15:59:30 +02001893
1894 status = psa_get_key_information( key, &key_type, &key_bits );
1895 if( status != PSA_SUCCESS )
1896 return( status );
1897 slot = &global_data.key_slots[key];
1898
Gilles Peskinebb1072f2018-06-08 18:46:05 +02001899 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02001900 if( cipher_info == NULL )
1901 return( PSA_ERROR_NOT_SUPPORTED );
1902
mohammad1603503973b2018-03-12 15:59:30 +02001903 mbedtls_cipher_init( &operation->ctx.cipher );
1904 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03001905 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001906 {
1907 psa_cipher_abort( operation );
1908 return( mbedtls_to_psa_error( ret ) );
1909 }
1910
1911 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data,
Gilles Peskinee553c652018-06-04 16:22:46 +02001912 key_bits, cipher_operation );
Moran Peker41deec42018-04-04 15:43:05 +03001913 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001914 {
1915 psa_cipher_abort( operation );
1916 return( mbedtls_to_psa_error( ret ) );
1917 }
1918
mohammad16038481e742018-03-18 13:57:31 +02001919#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03001920 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02001921 {
Gilles Peskine53514202018-06-06 15:11:46 +02001922 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1923 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02001924
Moran Pekera28258c2018-05-29 16:25:04 +03001925 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02001926 {
1927 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
1928 mode = MBEDTLS_PADDING_PKCS7;
1929 break;
1930 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
1931 mode = MBEDTLS_PADDING_NONE;
1932 break;
1933 default:
Moran Pekerae382792018-05-31 14:06:17 +03001934 psa_cipher_abort( operation );
1935 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02001936 }
1937 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03001938 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03001939 {
1940 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02001941 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03001942 }
mohammad16038481e742018-03-18 13:57:31 +02001943 }
1944#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
1945
mohammad1603503973b2018-03-12 15:59:30 +02001946 operation->key_set = 1;
1947 operation->alg = alg;
Gilles Peskine7e928852018-06-04 16:23:10 +02001948 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
1949 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
1950 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001951 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02001952 {
mohammad160389e0f462018-04-12 08:48:45 +03001953 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02001954 }
mohammad1603503973b2018-03-12 15:59:30 +02001955
Moran Peker395db872018-05-31 14:07:14 +03001956 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001957}
1958
Gilles Peskinee553c652018-06-04 16:22:46 +02001959psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
1960 psa_key_slot_t key,
1961 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02001962{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001963 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02001964}
1965
Gilles Peskinee553c652018-06-04 16:22:46 +02001966psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
1967 psa_key_slot_t key,
1968 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02001969{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001970 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02001971}
1972
Gilles Peskinee553c652018-06-04 16:22:46 +02001973psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
1974 unsigned char *iv,
1975 size_t iv_size,
1976 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02001977{
Moran Peker41deec42018-04-04 15:43:05 +03001978 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001979 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03001980 return( PSA_ERROR_BAD_STATE );
1981 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02001982 {
Moran Peker41deec42018-04-04 15:43:05 +03001983 ret = PSA_ERROR_BUFFER_TOO_SMALL;
1984 goto exit;
1985 }
Gilles Peskine7e928852018-06-04 16:23:10 +02001986 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
1987 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03001988 if( ret != 0 )
1989 {
1990 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02001991 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02001992 }
Gilles Peskinee553c652018-06-04 16:22:46 +02001993
mohammad16038481e742018-03-18 13:57:31 +02001994 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03001995 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03001996
Moran Peker395db872018-05-31 14:07:14 +03001997exit:
1998 if( ret != PSA_SUCCESS )
1999 psa_cipher_abort( operation );
2000 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002001}
2002
Gilles Peskinee553c652018-06-04 16:22:46 +02002003psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2004 const unsigned char *iv,
2005 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002006{
Moran Peker41deec42018-04-04 15:43:05 +03002007 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002008 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002009 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002010 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002011 {
Moran Pekerae382792018-05-31 14:06:17 +03002012 psa_cipher_abort( operation );
2013 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002014 }
mohammad1603503973b2018-03-12 15:59:30 +02002015 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002016 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002017 {
2018 psa_cipher_abort( operation );
2019 return( mbedtls_to_psa_error( ret ) );
2020 }
2021
2022 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002023
Moran Peker395db872018-05-31 14:07:14 +03002024 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002025}
2026
Gilles Peskinee553c652018-06-04 16:22:46 +02002027psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2028 const uint8_t *input,
2029 size_t input_length,
2030 unsigned char *output,
2031 size_t output_size,
2032 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002033{
2034 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002035 size_t expected_output_size;
2036 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2037 {
2038 /* Take the unprocessed partial block left over from previous
2039 * update calls, if any, plus the input to this call. Remove
2040 * the last partial block, if any. You get the data that will be
2041 * output in this call. */
2042 expected_output_size =
2043 ( operation->ctx.cipher.unprocessed_len + input_length )
2044 / operation->block_size * operation->block_size;
2045 }
2046 else
2047 {
2048 expected_output_size = input_length;
2049 }
2050 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002051 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002052
mohammad1603503973b2018-03-12 15:59:30 +02002053 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002054 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002055 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002056 {
2057 psa_cipher_abort( operation );
2058 return( mbedtls_to_psa_error( ret ) );
2059 }
2060
Moran Peker395db872018-05-31 14:07:14 +03002061 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002062}
2063
Gilles Peskinee553c652018-06-04 16:22:46 +02002064psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2065 uint8_t *output,
2066 size_t output_size,
2067 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002068{
2069 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002070 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002071
mohammad1603503973b2018-03-12 15:59:30 +02002072 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002073 {
Moran Peker7cb22b82018-06-05 11:40:02 +03002074 psa_cipher_abort( operation );
2075 return( PSA_ERROR_BAD_STATE );
2076 }
2077 if( operation->iv_required && ! operation->iv_set )
2078 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002079 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002080 return( PSA_ERROR_BAD_STATE );
2081 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002082 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2083 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002084 {
Gilles Peskine53514202018-06-06 15:11:46 +02002085 psa_algorithm_t padding_mode =
2086 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002087 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002088 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002089 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002090 return( PSA_ERROR_TAMPERING_DETECTED );
2091 }
Gilles Peskine53514202018-06-06 15:11:46 +02002092 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002093 {
2094 if( operation->ctx.cipher.unprocessed_len != 0 )
2095 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002096 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002097 return( PSA_ERROR_INVALID_ARGUMENT );
2098 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002099 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002100 }
2101
2102 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002103 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002104 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002105 {
2106 psa_cipher_abort( operation );
2107 return( mbedtls_to_psa_error( ret ) );
2108 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002109 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002110 memcpy( output, temp_output_buffer, *output_length );
2111 else
2112 {
2113 psa_cipher_abort( operation );
2114 return( PSA_ERROR_BUFFER_TOO_SMALL );
2115 }
mohammad1603503973b2018-03-12 15:59:30 +02002116
Moran Peker4c80d832018-04-22 20:15:31 +03002117 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002118}
2119
Gilles Peskinee553c652018-06-04 16:22:46 +02002120psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2121{
mohammad1603503973b2018-03-12 15:59:30 +02002122 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002123
Moran Peker41deec42018-04-04 15:43:05 +03002124 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002125 operation->key_set = 0;
2126 operation->iv_set = 0;
2127 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002128 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002129 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002130
Moran Peker395db872018-05-31 14:07:14 +03002131 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002132}
2133
Gilles Peskinea0655c32018-04-30 17:06:50 +02002134
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002135
mohammad16038cc1cee2018-03-28 01:21:33 +03002136/****************************************************************/
2137/* Key Policy */
2138/****************************************************************/
2139
Gilles Peskine2d277862018-06-18 15:41:12 +02002140void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002141{
Gilles Peskine803ce742018-06-18 16:07:14 +02002142 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002143}
2144
Gilles Peskine2d277862018-06-18 15:41:12 +02002145void psa_key_policy_set_usage( psa_key_policy_t *policy,
2146 psa_key_usage_t usage,
2147 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002148{
mohammad16034eed7572018-03-28 05:14:59 -07002149 policy->usage = usage;
2150 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002151}
2152
Gilles Peskine2d277862018-06-18 15:41:12 +02002153psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002154{
mohammad16036df908f2018-04-02 08:34:15 -07002155 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002156}
2157
Gilles Peskine2d277862018-06-18 15:41:12 +02002158psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002159{
mohammad16036df908f2018-04-02 08:34:15 -07002160 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002161}
2162
Gilles Peskine2d277862018-06-18 15:41:12 +02002163psa_status_t psa_set_key_policy( psa_key_slot_t key,
2164 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002165{
2166 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002167
Gilles Peskine828ed142018-06-18 23:25:51 +02002168 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002169 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002170
mohammad16038cc1cee2018-03-28 01:21:33 +03002171 slot = &global_data.key_slots[key];
2172 if( slot->type != PSA_KEY_TYPE_NONE )
2173 return( PSA_ERROR_OCCUPIED_SLOT );
2174
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002175 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2176 PSA_KEY_USAGE_ENCRYPT |
2177 PSA_KEY_USAGE_DECRYPT |
2178 PSA_KEY_USAGE_SIGN |
2179 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002180 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002181
mohammad16036df908f2018-04-02 08:34:15 -07002182 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002183
2184 return( PSA_SUCCESS );
2185}
2186
Gilles Peskine2d277862018-06-18 15:41:12 +02002187psa_status_t psa_get_key_policy( psa_key_slot_t key,
2188 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002189{
2190 key_slot_t *slot;
2191
Gilles Peskine828ed142018-06-18 23:25:51 +02002192 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002193 return( PSA_ERROR_INVALID_ARGUMENT );
2194
2195 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002196
mohammad16036df908f2018-04-02 08:34:15 -07002197 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002198
2199 return( PSA_SUCCESS );
2200}
Gilles Peskine20035e32018-02-03 22:44:14 +01002201
Gilles Peskinea0655c32018-04-30 17:06:50 +02002202
2203
mohammad1603804cd712018-03-20 22:44:08 +02002204/****************************************************************/
2205/* Key Lifetime */
2206/****************************************************************/
2207
Gilles Peskine2d277862018-06-18 15:41:12 +02002208psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2209 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002210{
2211 key_slot_t *slot;
2212
Gilles Peskine828ed142018-06-18 23:25:51 +02002213 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002214 return( PSA_ERROR_INVALID_ARGUMENT );
2215
2216 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002217
mohammad1603804cd712018-03-20 22:44:08 +02002218 *lifetime = slot->lifetime;
2219
2220 return( PSA_SUCCESS );
2221}
2222
Gilles Peskine2d277862018-06-18 15:41:12 +02002223psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
2224 const psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002225{
2226 key_slot_t *slot;
2227
Gilles Peskine828ed142018-06-18 23:25:51 +02002228 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002229 return( PSA_ERROR_INVALID_ARGUMENT );
2230
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002231 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2232 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002233 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2234 return( PSA_ERROR_INVALID_ARGUMENT );
2235
mohammad1603804cd712018-03-20 22:44:08 +02002236 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002237 if( slot->type != PSA_KEY_TYPE_NONE )
2238 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002239
Moran Pekerd7326592018-05-29 16:56:39 +03002240 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002241 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002242
mohammad1603060ad8a2018-03-20 14:28:38 -07002243 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002244
2245 return( PSA_SUCCESS );
2246}
2247
Gilles Peskine20035e32018-02-03 22:44:14 +01002248
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002249
mohammad16035955c982018-04-26 00:53:03 +03002250/****************************************************************/
2251/* AEAD */
2252/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002253
mohammad16035955c982018-04-26 00:53:03 +03002254psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2255 psa_algorithm_t alg,
2256 const uint8_t *nonce,
2257 size_t nonce_length,
2258 const uint8_t *additional_data,
2259 size_t additional_data_length,
2260 const uint8_t *plaintext,
2261 size_t plaintext_length,
2262 uint8_t *ciphertext,
2263 size_t ciphertext_size,
2264 size_t *ciphertext_length )
2265{
2266 int ret;
2267 psa_status_t status;
2268 key_slot_t *slot;
2269 psa_key_type_t key_type;
2270 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002271 uint8_t *tag;
2272 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002273 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002274 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002275
mohammad1603f08a5502018-06-03 15:05:47 +03002276 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002277
mohammad16035955c982018-04-26 00:53:03 +03002278 status = psa_get_key_information( key, &key_type, &key_bits );
2279 if( status != PSA_SUCCESS )
2280 return( status );
2281 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002282 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002283 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002284
mohammad16035ed06212018-06-06 13:09:34 +03002285 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2286 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002287 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002288 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002289
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002290 if( ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) == 0 )
mohammad1603f14394b2018-06-04 14:33:19 +03002291 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002292
Gilles Peskine2d277862018-06-18 15:41:12 +02002293 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2294 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002295 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002296
mohammad16035955c982018-04-26 00:53:03 +03002297 if( alg == PSA_ALG_GCM )
2298 {
2299 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002300 tag_length = 16;
2301
mohammad160396910d82018-06-04 14:33:00 +03002302 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2303 return( PSA_ERROR_INVALID_ARGUMENT );
2304
mohammad160315223a82018-06-03 17:19:55 +03002305 //make sure we have place to hold the tag in the ciphertext buffer
2306 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002307 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002308
2309 //update the tag pointer to point to the end of the ciphertext_length
2310 tag = ciphertext + plaintext_length;
2311
mohammad16035955c982018-04-26 00:53:03 +03002312 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002313 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002314 slot->data.raw.data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002315 key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002316 if( ret != 0 )
2317 {
2318 mbedtls_gcm_free( &gcm );
2319 return( mbedtls_to_psa_error( ret ) );
2320 }
2321 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002322 plaintext_length, nonce,
2323 nonce_length, additional_data,
2324 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002325 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002326 mbedtls_gcm_free( &gcm );
2327 }
2328 else if( alg == PSA_ALG_CCM )
2329 {
2330 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002331 tag_length = 16;
2332
mohammad160396910d82018-06-04 14:33:00 +03002333 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2334 return( PSA_ERROR_INVALID_ARGUMENT );
2335
mohammad160347ddf3d2018-04-26 01:11:21 +03002336 if( nonce_length < 7 || nonce_length > 13 )
2337 return( PSA_ERROR_INVALID_ARGUMENT );
2338
mohammad160315223a82018-06-03 17:19:55 +03002339 //make sure we have place to hold the tag in the ciphertext buffer
2340 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002341 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002342
2343 //update the tag pointer to point to the end of the ciphertext_length
2344 tag = ciphertext + plaintext_length;
2345
mohammad16035955c982018-04-26 00:53:03 +03002346 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002347 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002348 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002349 if( ret != 0 )
2350 {
2351 mbedtls_ccm_free( &ccm );
2352 return( mbedtls_to_psa_error( ret ) );
2353 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002354 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002355 nonce, nonce_length,
2356 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002357 additional_data_length,
2358 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002359 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002360 mbedtls_ccm_free( &ccm );
2361 }
mohammad16035c8845f2018-05-09 05:40:09 -07002362 else
2363 {
mohammad1603554faad2018-06-03 15:07:38 +03002364 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002365 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002366
mohammad160315223a82018-06-03 17:19:55 +03002367 if( ret != 0 )
2368 {
2369 memset( ciphertext, 0, ciphertext_size );
2370 return( mbedtls_to_psa_error( ret ) );
2371 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002372
mohammad160315223a82018-06-03 17:19:55 +03002373 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002374 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002375}
2376
Gilles Peskineee652a32018-06-01 19:23:52 +02002377/* Locate the tag in a ciphertext buffer containing the encrypted data
2378 * followed by the tag. Return the length of the part preceding the tag in
2379 * *plaintext_length. This is the size of the plaintext in modes where
2380 * the encrypted data has the same size as the plaintext, such as
2381 * CCM and GCM. */
2382static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2383 const uint8_t *ciphertext,
2384 size_t ciphertext_length,
2385 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002386 const uint8_t **p_tag )
2387{
2388 size_t payload_length;
2389 if( tag_length > ciphertext_length )
2390 return( PSA_ERROR_INVALID_ARGUMENT );
2391 payload_length = ciphertext_length - tag_length;
2392 if( payload_length > plaintext_size )
2393 return( PSA_ERROR_BUFFER_TOO_SMALL );
2394 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002395 return( PSA_SUCCESS );
2396}
2397
mohammad16035955c982018-04-26 00:53:03 +03002398psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2399 psa_algorithm_t alg,
2400 const uint8_t *nonce,
2401 size_t nonce_length,
2402 const uint8_t *additional_data,
2403 size_t additional_data_length,
2404 const uint8_t *ciphertext,
2405 size_t ciphertext_length,
2406 uint8_t *plaintext,
2407 size_t plaintext_size,
2408 size_t *plaintext_length )
2409{
2410 int ret;
2411 psa_status_t status;
2412 key_slot_t *slot;
2413 psa_key_type_t key_type;
2414 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002415 const uint8_t *tag;
2416 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002417 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002418 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002419
Gilles Peskineee652a32018-06-01 19:23:52 +02002420 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002421
mohammad16035955c982018-04-26 00:53:03 +03002422 status = psa_get_key_information( key, &key_type, &key_bits );
2423 if( status != PSA_SUCCESS )
2424 return( status );
2425 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002426 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002427 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002428
mohammad16035ed06212018-06-06 13:09:34 +03002429 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2430 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002431 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002432 return( PSA_ERROR_NOT_SUPPORTED );
2433
mohammad1603f14394b2018-06-04 14:33:19 +03002434 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2435 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002436
Gilles Peskine2d277862018-06-18 15:41:12 +02002437 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2438 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002439 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002440
mohammad16035955c982018-04-26 00:53:03 +03002441 if( alg == PSA_ALG_GCM )
2442 {
2443 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002444
Gilles Peskineee652a32018-06-01 19:23:52 +02002445 tag_length = 16;
2446 status = psa_aead_unpadded_locate_tag( tag_length,
2447 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002448 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002449 if( status != PSA_SUCCESS )
2450 return( status );
2451
mohammad16035955c982018-04-26 00:53:03 +03002452 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002453 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002454 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002455 if( ret != 0 )
2456 {
2457 mbedtls_gcm_free( &gcm );
2458 return( mbedtls_to_psa_error( ret ) );
2459 }
mohammad16035955c982018-04-26 00:53:03 +03002460
Gilles Peskineee652a32018-06-01 19:23:52 +02002461 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002462 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002463 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002464 additional_data,
2465 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002466 tag, tag_length,
2467 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002468 mbedtls_gcm_free( &gcm );
2469 }
2470 else if( alg == PSA_ALG_CCM )
2471 {
2472 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002473
mohammad160347ddf3d2018-04-26 01:11:21 +03002474 if( nonce_length < 7 || nonce_length > 13 )
2475 return( PSA_ERROR_INVALID_ARGUMENT );
2476
mohammad16039375f842018-06-03 14:28:24 +03002477 tag_length = 16;
2478 status = psa_aead_unpadded_locate_tag( tag_length,
2479 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002480 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002481 if( status != PSA_SUCCESS )
2482 return( status );
2483
mohammad16035955c982018-04-26 00:53:03 +03002484 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002485 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002486 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002487 if( ret != 0 )
2488 {
2489 mbedtls_ccm_free( &ccm );
2490 return( mbedtls_to_psa_error( ret ) );
2491 }
mohammad160360a64d02018-06-03 17:20:42 +03002492 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002493 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002494 additional_data,
2495 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002496 ciphertext, plaintext,
2497 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002498 mbedtls_ccm_free( &ccm );
2499 }
mohammad160339574652018-06-01 04:39:53 -07002500 else
2501 {
mohammad1603554faad2018-06-03 15:07:38 +03002502 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002503 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002504
Gilles Peskineee652a32018-06-01 19:23:52 +02002505 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002506 memset( plaintext, 0, plaintext_size );
2507 else
2508 *plaintext_length = ciphertext_length - tag_length;
2509
Gilles Peskineee652a32018-06-01 19:23:52 +02002510 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002511}
2512
Gilles Peskinea0655c32018-04-30 17:06:50 +02002513
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002514
Gilles Peskine20035e32018-02-03 22:44:14 +01002515/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002516/* Key generation */
2517/****************************************************************/
2518
2519psa_status_t psa_generate_random( uint8_t *output,
2520 size_t output_size )
2521{
2522 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2523 output, output_size );
2524 return( mbedtls_to_psa_error( ret ) );
2525}
2526
2527psa_status_t psa_generate_key( psa_key_slot_t key,
2528 psa_key_type_t type,
2529 size_t bits,
2530 const void *parameters,
2531 size_t parameters_size )
2532{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002533 key_slot_t *slot;
2534
2535 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
2536 return( PSA_ERROR_INVALID_ARGUMENT );
2537 slot = &global_data.key_slots[key];
2538 if( slot->type != PSA_KEY_TYPE_NONE )
2539 return( PSA_ERROR_OCCUPIED_SLOT );
2540 if( parameters == NULL && parameters_size != 0 )
2541 return( PSA_ERROR_INVALID_ARGUMENT );
2542
2543 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
2544 {
2545 psa_status_t status = prepare_raw_data_slot( type, bits,
2546 &slot->data.raw );
2547 if( status != PSA_SUCCESS )
2548 return( status );
2549 status = psa_generate_random( slot->data.raw.data,
2550 slot->data.raw.bytes );
2551 if( status != PSA_SUCCESS )
2552 {
2553 mbedtls_free( slot->data.raw.data );
2554 return( status );
2555 }
2556#if defined(MBEDTLS_DES_C)
2557 if( type == PSA_KEY_TYPE_DES )
2558 {
2559 mbedtls_des_key_set_parity( slot->data.raw.data );
2560 if( slot->data.raw.bytes >= 16 )
2561 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2562 if( slot->data.raw.bytes == 24 )
2563 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2564 }
2565#endif /* MBEDTLS_DES_C */
2566 }
2567 else
2568
2569#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2570 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2571 {
2572 mbedtls_rsa_context *rsa;
2573 int ret;
2574 int exponent = 65537;
2575 if( parameters != NULL )
2576 {
2577 const unsigned *p = parameters;
2578 if( parameters_size != sizeof( *p ) )
2579 return( PSA_ERROR_INVALID_ARGUMENT );
2580 if( *p > INT_MAX )
2581 return( PSA_ERROR_INVALID_ARGUMENT );
2582 exponent = *p;
2583 }
2584 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
2585 if( rsa == NULL )
2586 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2587 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
2588 ret = mbedtls_rsa_gen_key( rsa,
2589 mbedtls_ctr_drbg_random,
2590 &global_data.ctr_drbg,
2591 bits,
2592 exponent );
2593 if( ret != 0 )
2594 {
2595 mbedtls_rsa_free( rsa );
2596 mbedtls_free( rsa );
2597 return( mbedtls_to_psa_error( ret ) );
2598 }
2599 slot->data.rsa = rsa;
2600 }
2601 else
2602#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
2603
2604#if defined(MBEDTLS_ECP_C)
2605 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
2606 {
2607 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
2608 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
2609 const mbedtls_ecp_curve_info *curve_info =
2610 mbedtls_ecp_curve_info_from_grp_id( grp_id );
2611 mbedtls_ecp_keypair *ecp;
2612 int ret;
2613 if( parameters != NULL )
2614 return( PSA_ERROR_NOT_SUPPORTED );
2615 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
2616 return( PSA_ERROR_NOT_SUPPORTED );
2617 if( curve_info->bit_size != bits )
2618 return( PSA_ERROR_INVALID_ARGUMENT );
2619 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
2620 if( ecp == NULL )
2621 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2622 mbedtls_ecp_keypair_init( ecp );
2623 ret = mbedtls_ecp_gen_key( grp_id, ecp,
2624 mbedtls_ctr_drbg_random,
2625 &global_data.ctr_drbg );
2626 if( ret != 0 )
2627 {
2628 mbedtls_ecp_keypair_free( ecp );
2629 mbedtls_free( ecp );
2630 return( mbedtls_to_psa_error( ret ) );
2631 }
2632 slot->data.ecp = ecp;
2633 }
2634 else
2635#endif /* MBEDTLS_ECP_C */
2636
2637 return( PSA_ERROR_NOT_SUPPORTED );
2638
2639 slot->type = type;
2640 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02002641}
2642
2643
2644/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002645/* Module setup */
2646/****************************************************************/
2647
Gilles Peskinee59236f2018-01-27 23:32:46 +01002648void mbedtls_psa_crypto_free( void )
2649{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002650 size_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02002651 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002652 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002653 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2654 mbedtls_entropy_free( &global_data.entropy );
2655 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2656}
2657
2658psa_status_t psa_crypto_init( void )
2659{
2660 int ret;
2661 const unsigned char drbg_seed[] = "PSA";
2662
2663 if( global_data.initialized != 0 )
2664 return( PSA_SUCCESS );
2665
2666 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2667 mbedtls_entropy_init( &global_data.entropy );
2668 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2669
2670 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2671 mbedtls_entropy_func,
2672 &global_data.entropy,
2673 drbg_seed, sizeof( drbg_seed ) - 1 );
2674 if( ret != 0 )
2675 goto exit;
2676
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002677 global_data.initialized = 1;
2678
Gilles Peskinee59236f2018-01-27 23:32:46 +01002679exit:
2680 if( ret != 0 )
2681 mbedtls_psa_crypto_free( );
2682 return( mbedtls_to_psa_error( ret ) );
2683}
2684
2685#endif /* MBEDTLS_PSA_CRYPTO_C */