blob: 90b43549c9c752357828984d0555432f8b8abdfc [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:
Gilles Peskinec06e0712018-06-20 16:21:04 +0200868 return( PSA_ALG_IS_HASH( alg ) ?
869 PSA_ERROR_NOT_SUPPORTED :
870 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100871 }
872 if( ret == 0 )
873 operation->alg = alg;
874 else
875 psa_hash_abort( operation );
876 return( mbedtls_to_psa_error( ret ) );
877}
878
879psa_status_t psa_hash_update( psa_hash_operation_t *operation,
880 const uint8_t *input,
881 size_t input_length )
882{
883 int ret;
884 switch( operation->alg )
885 {
886#if defined(MBEDTLS_MD2_C)
887 case PSA_ALG_MD2:
888 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
889 input, input_length );
890 break;
891#endif
892#if defined(MBEDTLS_MD4_C)
893 case PSA_ALG_MD4:
894 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
895 input, input_length );
896 break;
897#endif
898#if defined(MBEDTLS_MD5_C)
899 case PSA_ALG_MD5:
900 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
901 input, input_length );
902 break;
903#endif
904#if defined(MBEDTLS_RIPEMD160_C)
905 case PSA_ALG_RIPEMD160:
906 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
907 input, input_length );
908 break;
909#endif
910#if defined(MBEDTLS_SHA1_C)
911 case PSA_ALG_SHA_1:
912 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
913 input, input_length );
914 break;
915#endif
916#if defined(MBEDTLS_SHA256_C)
917 case PSA_ALG_SHA_224:
918 case PSA_ALG_SHA_256:
919 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
920 input, input_length );
921 break;
922#endif
923#if defined(MBEDTLS_SHA512_C)
924 case PSA_ALG_SHA_384:
925 case PSA_ALG_SHA_512:
926 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
927 input, input_length );
928 break;
929#endif
930 default:
931 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
932 break;
933 }
934 if( ret != 0 )
935 psa_hash_abort( operation );
936 return( mbedtls_to_psa_error( ret ) );
937}
938
939psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
940 uint8_t *hash,
941 size_t hash_size,
942 size_t *hash_length )
943{
944 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200945 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100946
947 /* Fill the output buffer with something that isn't a valid hash
948 * (barring an attack on the hash and deliberately-crafted input),
949 * in case the caller doesn't check the return status properly. */
950 *hash_length = actual_hash_length;
951 memset( hash, '!', hash_size );
952
953 if( hash_size < actual_hash_length )
954 return( PSA_ERROR_BUFFER_TOO_SMALL );
955
956 switch( operation->alg )
957 {
958#if defined(MBEDTLS_MD2_C)
959 case PSA_ALG_MD2:
960 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
961 break;
962#endif
963#if defined(MBEDTLS_MD4_C)
964 case PSA_ALG_MD4:
965 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
966 break;
967#endif
968#if defined(MBEDTLS_MD5_C)
969 case PSA_ALG_MD5:
970 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
971 break;
972#endif
973#if defined(MBEDTLS_RIPEMD160_C)
974 case PSA_ALG_RIPEMD160:
975 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
976 break;
977#endif
978#if defined(MBEDTLS_SHA1_C)
979 case PSA_ALG_SHA_1:
980 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
981 break;
982#endif
983#if defined(MBEDTLS_SHA256_C)
984 case PSA_ALG_SHA_224:
985 case PSA_ALG_SHA_256:
986 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
987 break;
988#endif
989#if defined(MBEDTLS_SHA512_C)
990 case PSA_ALG_SHA_384:
991 case PSA_ALG_SHA_512:
992 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
993 break;
994#endif
995 default:
996 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
997 break;
998 }
999
1000 if( ret == 0 )
1001 {
1002 return( psa_hash_abort( operation ) );
1003 }
1004 else
1005 {
1006 psa_hash_abort( operation );
1007 return( mbedtls_to_psa_error( ret ) );
1008 }
1009}
1010
Gilles Peskine2d277862018-06-18 15:41:12 +02001011psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1012 const uint8_t *hash,
1013 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001014{
1015 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1016 size_t actual_hash_length;
1017 psa_status_t status = psa_hash_finish( operation,
1018 actual_hash, sizeof( actual_hash ),
1019 &actual_hash_length );
1020 if( status != PSA_SUCCESS )
1021 return( status );
1022 if( actual_hash_length != hash_length )
1023 return( PSA_ERROR_INVALID_SIGNATURE );
1024 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1025 return( PSA_ERROR_INVALID_SIGNATURE );
1026 return( PSA_SUCCESS );
1027}
1028
1029
1030
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001031/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001032/* MAC */
1033/****************************************************************/
1034
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001035static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001036 psa_algorithm_t alg,
1037 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001038 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001039 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001040{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001041 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001042 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001043
1044 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1045 {
1046 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001047 {
1048 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1049 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001050
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001051 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001052 {
1053 case PSA_ALG_STREAM_CIPHER:
1054 mode = MBEDTLS_MODE_STREAM;
1055 break;
1056 case PSA_ALG_CBC_BASE:
1057 mode = MBEDTLS_MODE_CBC;
1058 break;
1059 case PSA_ALG_CFB_BASE:
1060 mode = MBEDTLS_MODE_CFB;
1061 break;
1062 case PSA_ALG_OFB_BASE:
1063 mode = MBEDTLS_MODE_OFB;
1064 break;
1065 case PSA_ALG_CTR:
1066 mode = MBEDTLS_MODE_CTR;
1067 break;
1068 case PSA_ALG_CCM:
1069 mode = MBEDTLS_MODE_CCM;
1070 break;
1071 case PSA_ALG_GCM:
1072 mode = MBEDTLS_MODE_GCM;
1073 break;
1074 default:
1075 return( NULL );
1076 }
1077 }
1078 else if( alg == PSA_ALG_CMAC )
1079 mode = MBEDTLS_MODE_ECB;
1080 else if( alg == PSA_ALG_GMAC )
1081 mode = MBEDTLS_MODE_GCM;
1082 else
1083 return( NULL );
1084
1085 switch( key_type )
1086 {
1087 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001088 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001089 break;
1090 case PSA_KEY_TYPE_DES:
1091 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001092 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001093 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001094 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001095 break;
1096 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001097 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001098 break;
1099 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001100 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001101 break;
1102 default:
1103 return( NULL );
1104 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001105 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001106 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001107
mohammad1603f4f0d612018-06-03 15:04:51 +03001108 return( mbedtls_cipher_info_from_values( cipher_id_tmp, key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001109}
1110
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001111static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001112{
Gilles Peskine2d277862018-06-18 15:41:12 +02001113 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001114 {
1115 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001116 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001117 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001118 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001119 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001120 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001121 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001122 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001123 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001124 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001125 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001126 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001127 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001128 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001129 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001130 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001131 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001132 return( 128 );
1133 default:
1134 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001135 }
1136}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001137
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001138/* Initialize the MAC operation structure. Once this function has been
1139 * called, psa_mac_abort can run and will do the right thing. */
1140static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1141 psa_algorithm_t alg )
1142{
1143 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1144
1145 operation->alg = alg;
1146 operation->key_set = 0;
1147 operation->iv_set = 0;
1148 operation->iv_required = 0;
1149 operation->has_input = 0;
1150 operation->key_usage_sign = 0;
1151 operation->key_usage_verify = 0;
1152
1153#if defined(MBEDTLS_CMAC_C)
1154 if( alg == PSA_ALG_CMAC )
1155 {
1156 operation->iv_required = 0;
1157 mbedtls_cipher_init( &operation->ctx.cmac );
1158 status = PSA_SUCCESS;
1159 }
1160 else
1161#endif /* MBEDTLS_CMAC_C */
1162#if defined(MBEDTLS_MD_C)
1163 if( PSA_ALG_IS_HMAC( operation->alg ) )
1164 {
1165 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1166 PSA_ALG_HMAC_HASH( alg ) );
1167 }
1168 else
1169#endif /* MBEDTLS_MD_C */
1170 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001171 if( ! PSA_ALG_IS_MAC( alg ) )
1172 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001173 }
1174
1175 if( status != PSA_SUCCESS )
1176 memset( operation, 0, sizeof( *operation ) );
1177 return( status );
1178}
1179
Gilles Peskine8c9def32018-02-08 10:02:12 +01001180psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1181{
1182 switch( operation->alg )
1183 {
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001184 case 0:
1185 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001186#if defined(MBEDTLS_CMAC_C)
1187 case PSA_ALG_CMAC:
1188 mbedtls_cipher_free( &operation->ctx.cmac );
1189 break;
1190#endif /* MBEDTLS_CMAC_C */
1191 default:
1192#if defined(MBEDTLS_MD_C)
1193 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001194 {
Gilles Peskine99bc6492018-06-11 17:13:00 +02001195 unsigned int block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001196 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001197
Gilles Peskine99bc6492018-06-11 17:13:00 +02001198 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001199 return( PSA_ERROR_NOT_SUPPORTED );
1200
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001201 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001202 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001203 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001204 else
1205#endif /* MBEDTLS_MD_C */
1206 return( PSA_ERROR_NOT_SUPPORTED );
1207 }
Moran Peker41deec42018-04-04 15:43:05 +03001208
Gilles Peskine8c9def32018-02-08 10:02:12 +01001209 operation->alg = 0;
1210 operation->key_set = 0;
1211 operation->iv_set = 0;
1212 operation->iv_required = 0;
1213 operation->has_input = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001214 operation->key_usage_sign = 0;
1215 operation->key_usage_verify = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001216
Gilles Peskine8c9def32018-02-08 10:02:12 +01001217 return( PSA_SUCCESS );
1218}
1219
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001220#if defined(MBEDTLS_CMAC_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001221static int psa_cmac_start( psa_mac_operation_t *operation,
1222 size_t key_bits,
1223 key_slot_t *slot,
1224 const mbedtls_cipher_info_t *cipher_info )
1225{
1226 int ret;
1227
1228 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001229
1230 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1231 if( ret != 0 )
1232 return( ret );
1233
1234 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1235 slot->data.raw.data,
1236 key_bits );
1237 return( ret );
1238}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001239#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001240
Gilles Peskine248051a2018-06-20 16:09:38 +02001241#if defined(MBEDTLS_MD_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001242static int psa_hmac_start( psa_mac_operation_t *operation,
1243 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001244 key_slot_t *slot,
1245 psa_algorithm_t alg )
1246{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001247 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001248 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001249 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001250 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001251 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001252 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001253 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001254 size_t key_length = slot->data.raw.bytes;
1255 psa_status_t status;
1256
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001257 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001258 return( PSA_ERROR_NOT_SUPPORTED );
1259
1260 if( key_type != PSA_KEY_TYPE_HMAC )
1261 return( PSA_ERROR_INVALID_ARGUMENT );
1262
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001263 operation->mac_size = digest_size;
1264
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001265 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001266 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001267 {
1268 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001269 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001270 if( status != PSA_SUCCESS )
1271 return( status );
1272 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001273 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001274 if( status != PSA_SUCCESS )
1275 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001276 }
Gilles Peskined223b522018-06-11 18:12:58 +02001277 else
1278 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001279
Gilles Peskined223b522018-06-11 18:12:58 +02001280 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1281 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001282 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001283 ipad[i] ^= 0x36;
1284 memset( ipad + key_length, 0x36, block_size - key_length );
1285
1286 /* Copy the key material from ipad to opad, flipping the requisite bits,
1287 * and filling the rest of opad with the requisite constant. */
1288 for( i = 0; i < key_length; i++ )
1289 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1290 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001291
1292 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1293 PSA_ALG_HMAC_HASH( alg ) );
1294 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001295 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001296
1297 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1298 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001299
1300cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001301 mbedtls_zeroize( ipad, key_length );
1302 /* opad is in the context. It needs to stay in memory if this function
1303 * succeeds, and it will be wiped by psa_mac_abort() called from
1304 * psa_mac_start in the error case. */
1305
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001306 return( status );
1307}
Gilles Peskine248051a2018-06-20 16:09:38 +02001308#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001309
Gilles Peskine8c9def32018-02-08 10:02:12 +01001310psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1311 psa_key_slot_t key,
1312 psa_algorithm_t alg )
1313{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001314 psa_status_t status;
1315 key_slot_t *slot;
1316 psa_key_type_t key_type;
1317 size_t key_bits;
Gilles Peskine828ed142018-06-18 23:25:51 +02001318 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001319
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001320 status = psa_mac_init( operation, alg );
1321 if( status != PSA_SUCCESS )
1322 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001323
1324 status = psa_get_key_information( key, &key_type, &key_bits );
1325 if( status != PSA_SUCCESS )
1326 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001327
Gilles Peskine8c9def32018-02-08 10:02:12 +01001328 slot = &global_data.key_slots[key];
Gilles Peskine99bc6492018-06-11 17:13:00 +02001329 if( slot->type == PSA_KEY_TYPE_NONE )
1330 return( PSA_ERROR_EMPTY_SLOT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001331
Moran Pekerd7326592018-05-29 16:56:39 +03001332 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001333 operation->key_usage_sign = 1;
1334
Moran Pekerd7326592018-05-29 16:56:39 +03001335 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001336 operation->key_usage_verify = 1;
1337
Gilles Peskine8c9def32018-02-08 10:02:12 +01001338 if( ! PSA_ALG_IS_HMAC( alg ) )
1339 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001340 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001341 if( cipher_info == NULL )
1342 return( PSA_ERROR_NOT_SUPPORTED );
1343 operation->mac_size = cipher_info->block_size;
1344 }
1345 switch( alg )
1346 {
1347#if defined(MBEDTLS_CMAC_C)
1348 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001349 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1350 key_bits,
1351 slot,
1352 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001353 break;
1354#endif /* MBEDTLS_CMAC_C */
1355 default:
1356#if defined(MBEDTLS_MD_C)
1357 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001358 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001359 else
1360#endif /* MBEDTLS_MD_C */
1361 return( PSA_ERROR_NOT_SUPPORTED );
1362 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001363
Gilles Peskine8c9def32018-02-08 10:02:12 +01001364 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001365 * context may contain data that needs to be wiped on error. */
1366 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001367 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001368 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001369 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001370 else
1371 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001372 operation->key_set = 1;
1373 }
1374 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001375}
1376
1377psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1378 const uint8_t *input,
1379 size_t input_length )
1380{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001381 int ret = 0 ;
1382 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383 if( ! operation->key_set )
1384 return( PSA_ERROR_BAD_STATE );
1385 if( operation->iv_required && ! operation->iv_set )
1386 return( PSA_ERROR_BAD_STATE );
1387 operation->has_input = 1;
1388
1389 switch( operation->alg )
1390 {
1391#if defined(MBEDTLS_CMAC_C)
1392 case PSA_ALG_CMAC:
1393 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1394 input, input_length );
1395 break;
1396#endif /* MBEDTLS_CMAC_C */
1397 default:
1398#if defined(MBEDTLS_MD_C)
1399 if( PSA_ALG_IS_HMAC( operation->alg ) )
1400 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001401 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001402 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001403 }
1404 else
1405#endif /* MBEDTLS_MD_C */
1406 {
1407 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1408 }
1409 break;
1410 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001411 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001412 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001413 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001414 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001415 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001416 }
1417
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001418 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001419}
1420
mohammad16036df908f2018-04-02 08:34:15 -07001421static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001422 uint8_t *mac,
1423 size_t mac_size,
1424 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001425{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001426 int ret = 0;
1427 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001428 if( ! operation->key_set )
1429 return( PSA_ERROR_BAD_STATE );
1430 if( operation->iv_required && ! operation->iv_set )
1431 return( PSA_ERROR_BAD_STATE );
1432
1433 /* Fill the output buffer with something that isn't a valid mac
1434 * (barring an attack on the mac and deliberately-crafted input),
1435 * in case the caller doesn't check the return status properly. */
1436 *mac_length = operation->mac_size;
1437 memset( mac, '!', mac_size );
1438
1439 if( mac_size < operation->mac_size )
1440 return( PSA_ERROR_BUFFER_TOO_SMALL );
1441
1442 switch( operation->alg )
1443 {
1444#if defined(MBEDTLS_CMAC_C)
1445 case PSA_ALG_CMAC:
1446 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1447 break;
1448#endif /* MBEDTLS_CMAC_C */
1449 default:
1450#if defined(MBEDTLS_MD_C)
1451 if( PSA_ALG_IS_HMAC( operation->alg ) )
1452 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001453 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001454 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001455 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001456 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001457 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001458
Gilles Peskine99bc6492018-06-11 17:13:00 +02001459 if( block_size == 0 )
1460 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001461
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001462 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001463 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001464 if( status != PSA_SUCCESS )
1465 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001466 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001467
1468 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1469 PSA_ALG_HMAC_HASH( operation->alg ) );
1470 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001471 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001472
1473 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001474 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001475 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001476 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001477
1478 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001479 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001480 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001481 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001482
1483 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1484 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001485 hmac_cleanup:
1486 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001487 }
1488 else
1489#endif /* MBEDTLS_MD_C */
1490 {
1491 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1492 }
1493 break;
1494 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001495cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001496
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001497 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001498 {
1499 return( psa_mac_abort( operation ) );
1500 }
1501 else
1502 {
1503 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001504 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001505 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001506
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001507 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001508 }
1509}
1510
mohammad16036df908f2018-04-02 08:34:15 -07001511psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1512 uint8_t *mac,
1513 size_t mac_size,
1514 size_t *mac_length )
1515{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001516 if( ! operation->key_usage_sign )
mohammad16036df908f2018-04-02 08:34:15 -07001517 return( PSA_ERROR_NOT_PERMITTED );
1518
Gilles Peskine99bc6492018-06-11 17:13:00 +02001519 return( psa_mac_finish_internal( operation, mac,
1520 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001521}
1522
Gilles Peskine828ed142018-06-18 23:25:51 +02001523#define PSA_MAC_MAX_SIZE \
Gilles Peskine2d277862018-06-18 15:41:12 +02001524 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1525 MBEDTLS_MD_MAX_SIZE : \
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526 MBEDTLS_MAX_BLOCK_LENGTH )
1527psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1528 const uint8_t *mac,
1529 size_t mac_length )
1530{
Gilles Peskine828ed142018-06-18 23:25:51 +02001531 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001532 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001533 psa_status_t status;
1534
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001535 if( ! operation->key_usage_verify )
mohammad16036df908f2018-04-02 08:34:15 -07001536 return( PSA_ERROR_NOT_PERMITTED );
1537
1538 status = psa_mac_finish_internal( operation,
1539 actual_mac, sizeof( actual_mac ),
1540 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001541 if( status != PSA_SUCCESS )
1542 return( status );
1543 if( actual_mac_length != mac_length )
1544 return( PSA_ERROR_INVALID_SIGNATURE );
1545 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1546 return( PSA_ERROR_INVALID_SIGNATURE );
1547 return( PSA_SUCCESS );
1548}
1549
1550
Gilles Peskine20035e32018-02-03 22:44:14 +01001551
Gilles Peskine20035e32018-02-03 22:44:14 +01001552/****************************************************************/
1553/* Asymmetric cryptography */
1554/****************************************************************/
1555
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001556/* Decode the hash algorithm from alg and store the mbedtls encoding in
1557 * md_alg. Verify that the hash length is consistent. */
1558static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1559 size_t hash_length,
1560 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001561{
Gilles Peskine61b91d42018-06-08 16:09:36 +02001562 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
1563 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1564 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1565 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001566 {
1567#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001568 if( hash_length > UINT_MAX )
1569 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001570#endif
1571 }
1572 else
1573 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001574 if( mbedtls_md_get_size( md_info ) != hash_length )
1575 return( PSA_ERROR_INVALID_ARGUMENT );
1576 if( md_info == NULL )
1577 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001578 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001579 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001580}
1581
Gilles Peskine61b91d42018-06-08 16:09:36 +02001582psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1583 psa_algorithm_t alg,
1584 const uint8_t *hash,
1585 size_t hash_length,
1586 const uint8_t *salt,
1587 size_t salt_length,
1588 uint8_t *signature,
1589 size_t signature_size,
1590 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001591{
1592 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001593 psa_status_t status;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001594 *signature_length = 0;
1595 (void) salt;
1596 (void) salt_length;
1597
Gilles Peskine828ed142018-06-18 23:25:51 +02001598 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine20035e32018-02-03 22:44:14 +01001599 return( PSA_ERROR_EMPTY_SLOT );
1600 slot = &global_data.key_slots[key];
1601 if( slot->type == PSA_KEY_TYPE_NONE )
1602 return( PSA_ERROR_EMPTY_SLOT );
1603 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1604 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001605 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
mohammad160306e79202018-03-28 13:17:44 +03001606 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001607
Gilles Peskine20035e32018-02-03 22:44:14 +01001608#if defined(MBEDTLS_RSA_C)
1609 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1610 {
1611 mbedtls_rsa_context *rsa = slot->data.rsa;
1612 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001613 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001614 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001615 if( status != PSA_SUCCESS )
1616 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001617
Gilles Peskine20035e32018-02-03 22:44:14 +01001618 if( signature_size < rsa->len )
1619 return( PSA_ERROR_BUFFER_TOO_SMALL );
1620#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001621 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001622 {
1623 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1624 MBEDTLS_MD_NONE );
1625 ret = mbedtls_rsa_pkcs1_sign( rsa,
1626 mbedtls_ctr_drbg_random,
1627 &global_data.ctr_drbg,
1628 MBEDTLS_RSA_PRIVATE,
1629 md_alg, hash_length, hash,
1630 signature );
1631 }
1632 else
1633#endif /* MBEDTLS_PKCS1_V15 */
1634#if defined(MBEDTLS_PKCS1_V21)
1635 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1636 {
1637 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1638 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1639 mbedtls_ctr_drbg_random,
1640 &global_data.ctr_drbg,
1641 MBEDTLS_RSA_PRIVATE,
1642 md_alg, hash_length, hash,
1643 signature );
1644 }
1645 else
1646#endif /* MBEDTLS_PKCS1_V21 */
1647 {
1648 return( PSA_ERROR_INVALID_ARGUMENT );
1649 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001650 if( ret == 0 )
1651 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001652 return( mbedtls_to_psa_error( ret ) );
1653 }
1654 else
1655#endif /* defined(MBEDTLS_RSA_C) */
1656#if defined(MBEDTLS_ECP_C)
1657 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1658 {
itayzafrir5c753392018-05-08 11:18:38 +03001659 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1660 int ret;
1661 const mbedtls_md_info_t *md_info;
1662 mbedtls_md_type_t md_alg;
1663 if( signature_size < PSA_ECDSA_SIGNATURE_SIZE( ecdsa->grp.pbits ) )
1664 return( PSA_ERROR_BUFFER_TOO_SMALL );
1665 md_info = mbedtls_md_info_from_psa( alg );
1666 md_alg = mbedtls_md_get_type( md_info );
1667 ret = mbedtls_ecdsa_write_signature( ecdsa, md_alg, hash, hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001668 signature, signature_length,
1669 mbedtls_ctr_drbg_random,
1670 &global_data.ctr_drbg );
itayzafrir5c753392018-05-08 11:18:38 +03001671 return( mbedtls_to_psa_error( ret ) );
1672 }
1673 else
1674#endif /* defined(MBEDTLS_ECP_C) */
1675 {
Gilles Peskine20035e32018-02-03 22:44:14 +01001676 return( PSA_ERROR_NOT_SUPPORTED );
1677 }
itayzafrir5c753392018-05-08 11:18:38 +03001678}
1679
1680psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1681 psa_algorithm_t alg,
1682 const uint8_t *hash,
1683 size_t hash_length,
1684 const uint8_t *salt,
1685 size_t salt_length,
1686 uint8_t *signature,
1687 size_t signature_size )
1688{
1689 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001690 psa_status_t status;
itayzafrir5c753392018-05-08 11:18:38 +03001691 (void) salt;
1692 (void) salt_length;
1693
Gilles Peskine828ed142018-06-18 23:25:51 +02001694 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
itayzafrir5c753392018-05-08 11:18:38 +03001695 return( PSA_ERROR_INVALID_ARGUMENT );
1696 slot = &global_data.key_slots[key];
1697 if( slot->type == PSA_KEY_TYPE_NONE )
1698 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001699 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001700 return( PSA_ERROR_NOT_PERMITTED );
1701
Gilles Peskine61b91d42018-06-08 16:09:36 +02001702#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001703 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1704 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001705 {
1706 mbedtls_rsa_context *rsa = slot->data.rsa;
1707 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001708 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001709 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001710 if( status != PSA_SUCCESS )
1711 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001712
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001713 if( signature_size < rsa->len )
1714 return( PSA_ERROR_BUFFER_TOO_SMALL );
1715#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001716 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001717 {
1718 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1719 MBEDTLS_MD_NONE );
1720
1721 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001722 mbedtls_ctr_drbg_random,
1723 &global_data.ctr_drbg,
1724 MBEDTLS_RSA_PUBLIC,
1725 md_alg,
1726 hash_length,
1727 hash,
1728 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001729
1730 }
1731 else
1732#endif /* MBEDTLS_PKCS1_V15 */
1733#if defined(MBEDTLS_PKCS1_V21)
1734 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1735 {
Gilles Peskinebeb49482018-06-08 17:44:35 +02001736 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1737 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1738 mbedtls_ctr_drbg_random,
1739 &global_data.ctr_drbg,
1740 MBEDTLS_RSA_PUBLIC,
1741 md_alg, hash_length, hash,
1742 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001743 }
1744 else
1745#endif /* MBEDTLS_PKCS1_V21 */
1746 {
1747 return( PSA_ERROR_INVALID_ARGUMENT );
1748 }
1749 return( mbedtls_to_psa_error( ret ) );
1750 }
1751 else
1752#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001753#if defined(MBEDTLS_ECP_C)
1754 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1755 {
1756 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1757 int ret;
Gilles Peskine2d277862018-06-18 15:41:12 +02001758 (void) alg;
Gilles Peskine61b91d42018-06-08 16:09:36 +02001759 ret = mbedtls_ecdsa_read_signature( ecdsa, hash, hash_length,
1760 signature, signature_size );
itayzafrir5c753392018-05-08 11:18:38 +03001761 return( mbedtls_to_psa_error( ret ) );
1762 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001763 else
1764#endif /* defined(MBEDTLS_ECP_C) */
1765 {
1766 return( PSA_ERROR_NOT_SUPPORTED );
1767 }
1768}
1769
Gilles Peskine61b91d42018-06-08 16:09:36 +02001770psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
1771 psa_algorithm_t alg,
1772 const uint8_t *input,
1773 size_t input_length,
1774 const uint8_t *salt,
1775 size_t salt_length,
1776 uint8_t *output,
1777 size_t output_size,
1778 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001779{
1780 key_slot_t *slot;
1781 (void) salt;
1782 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001783 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001784
Gilles Peskine828ed142018-06-18 23:25:51 +02001785 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03001786 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001787 slot = &global_data.key_slots[key];
1788 if( slot->type == PSA_KEY_TYPE_NONE )
1789 return( PSA_ERROR_EMPTY_SLOT );
1790 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1791 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001792 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
1793 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001794
1795#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001796 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1797 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001798 {
1799 mbedtls_rsa_context *rsa = slot->data.rsa;
1800 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001801 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02001802 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001803#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001804 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001805 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001806 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
1807 mbedtls_ctr_drbg_random,
1808 &global_data.ctr_drbg,
1809 MBEDTLS_RSA_PUBLIC,
1810 input_length,
1811 input,
1812 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001813 }
1814 else
1815#endif /* MBEDTLS_PKCS1_V15 */
1816#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001817 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001818 {
1819 return( PSA_ERROR_NOT_SUPPORTED );
1820 }
1821 else
1822#endif /* MBEDTLS_PKCS1_V21 */
1823 {
1824 return( PSA_ERROR_INVALID_ARGUMENT );
1825 }
1826 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001827 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001828 return( mbedtls_to_psa_error( ret ) );
1829 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001830 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001831#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001832 {
1833 return( PSA_ERROR_NOT_SUPPORTED );
1834 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001835}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001836
Gilles Peskine61b91d42018-06-08 16:09:36 +02001837psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
1838 psa_algorithm_t alg,
1839 const uint8_t *input,
1840 size_t input_length,
1841 const uint8_t *salt,
1842 size_t salt_length,
1843 uint8_t *output,
1844 size_t output_size,
1845 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001846{
1847 key_slot_t *slot;
1848 (void) salt;
1849 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001850 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001851
Gilles Peskine828ed142018-06-18 23:25:51 +02001852 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001853 return( PSA_ERROR_EMPTY_SLOT );
1854 slot = &global_data.key_slots[key];
1855 if( slot->type == PSA_KEY_TYPE_NONE )
1856 return( PSA_ERROR_EMPTY_SLOT );
1857 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1858 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001859 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
1860 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001861
1862#if defined(MBEDTLS_RSA_C)
1863 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1864 {
1865 mbedtls_rsa_context *rsa = slot->data.rsa;
1866 int ret;
1867
Gilles Peskinec4def2f2018-06-08 17:53:48 +02001868 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001869 return( PSA_ERROR_INVALID_ARGUMENT );
1870
1871#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001872 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001873 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001874 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
1875 mbedtls_ctr_drbg_random,
1876 &global_data.ctr_drbg,
1877 MBEDTLS_RSA_PRIVATE,
1878 output_length,
1879 input,
1880 output,
1881 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001882 }
1883 else
1884#endif /* MBEDTLS_PKCS1_V15 */
1885#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001886 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001887 {
1888 return( PSA_ERROR_NOT_SUPPORTED );
1889 }
1890 else
1891#endif /* MBEDTLS_PKCS1_V21 */
1892 {
1893 return( PSA_ERROR_INVALID_ARGUMENT );
1894 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001895
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001896 return( mbedtls_to_psa_error( ret ) );
1897 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001898 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001899#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001900 {
1901 return( PSA_ERROR_NOT_SUPPORTED );
1902 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001903}
Gilles Peskine20035e32018-02-03 22:44:14 +01001904
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001905
1906
mohammad1603503973b2018-03-12 15:59:30 +02001907/****************************************************************/
1908/* Symmetric cryptography */
1909/****************************************************************/
1910
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001911/* Initialize the cipher operation structure. Once this function has been
1912 * called, psa_cipher_abort can run and will do the right thing. */
1913static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
1914 psa_algorithm_t alg )
1915{
Gilles Peskinec06e0712018-06-20 16:21:04 +02001916 if( ! PSA_ALG_IS_CIPHER( alg ) )
1917 {
1918 memset( operation, 0, sizeof( *operation ) );
1919 return( PSA_ERROR_INVALID_ARGUMENT );
1920 }
1921
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001922 operation->alg = alg;
1923 operation->key_set = 0;
1924 operation->iv_set = 0;
1925 operation->iv_required = 1;
1926 operation->iv_size = 0;
1927 operation->block_size = 0;
1928 mbedtls_cipher_init( &operation->ctx.cipher );
1929 return( PSA_SUCCESS );
1930}
1931
Gilles Peskinee553c652018-06-04 16:22:46 +02001932static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
1933 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02001934 psa_algorithm_t alg,
1935 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02001936{
1937 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1938 psa_status_t status;
1939 key_slot_t *slot;
1940 psa_key_type_t key_type;
1941 size_t key_bits;
1942 const mbedtls_cipher_info_t *cipher_info = NULL;
1943
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001944 status = psa_cipher_init( operation, alg );
1945 if( status != PSA_SUCCESS )
1946 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02001947
1948 status = psa_get_key_information( key, &key_type, &key_bits );
1949 if( status != PSA_SUCCESS )
1950 return( status );
1951 slot = &global_data.key_slots[key];
1952
Gilles Peskinebb1072f2018-06-08 18:46:05 +02001953 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02001954 if( cipher_info == NULL )
1955 return( PSA_ERROR_NOT_SUPPORTED );
1956
mohammad1603503973b2018-03-12 15:59:30 +02001957 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03001958 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001959 {
1960 psa_cipher_abort( operation );
1961 return( mbedtls_to_psa_error( ret ) );
1962 }
1963
1964 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data,
Gilles Peskinee553c652018-06-04 16:22:46 +02001965 key_bits, cipher_operation );
Moran Peker41deec42018-04-04 15:43:05 +03001966 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001967 {
1968 psa_cipher_abort( operation );
1969 return( mbedtls_to_psa_error( ret ) );
1970 }
1971
mohammad16038481e742018-03-18 13:57:31 +02001972#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03001973 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02001974 {
Gilles Peskine53514202018-06-06 15:11:46 +02001975 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1976 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02001977
Moran Pekera28258c2018-05-29 16:25:04 +03001978 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02001979 {
1980 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
1981 mode = MBEDTLS_PADDING_PKCS7;
1982 break;
1983 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
1984 mode = MBEDTLS_PADDING_NONE;
1985 break;
1986 default:
Moran Pekerae382792018-05-31 14:06:17 +03001987 psa_cipher_abort( operation );
1988 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02001989 }
1990 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03001991 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03001992 {
1993 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02001994 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03001995 }
mohammad16038481e742018-03-18 13:57:31 +02001996 }
1997#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
1998
mohammad1603503973b2018-03-12 15:59:30 +02001999 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002000 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
2001 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
2002 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002003 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002004 {
mohammad160389e0f462018-04-12 08:48:45 +03002005 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02002006 }
mohammad1603503973b2018-03-12 15:59:30 +02002007
Moran Peker395db872018-05-31 14:07:14 +03002008 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002009}
2010
Gilles Peskinee553c652018-06-04 16:22:46 +02002011psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
2012 psa_key_slot_t key,
2013 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002014{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002015 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002016}
2017
Gilles Peskinee553c652018-06-04 16:22:46 +02002018psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
2019 psa_key_slot_t key,
2020 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002021{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002022 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002023}
2024
Gilles Peskinee553c652018-06-04 16:22:46 +02002025psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
2026 unsigned char *iv,
2027 size_t iv_size,
2028 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002029{
Moran Peker41deec42018-04-04 15:43:05 +03002030 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002031 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002032 return( PSA_ERROR_BAD_STATE );
2033 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002034 {
Moran Peker41deec42018-04-04 15:43:05 +03002035 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2036 goto exit;
2037 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002038 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2039 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002040 if( ret != 0 )
2041 {
2042 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002043 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002044 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002045
mohammad16038481e742018-03-18 13:57:31 +02002046 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03002047 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002048
Moran Peker395db872018-05-31 14:07:14 +03002049exit:
2050 if( ret != PSA_SUCCESS )
2051 psa_cipher_abort( operation );
2052 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002053}
2054
Gilles Peskinee553c652018-06-04 16:22:46 +02002055psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2056 const unsigned char *iv,
2057 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002058{
Moran Peker41deec42018-04-04 15:43:05 +03002059 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002060 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002061 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002062 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002063 {
Moran Pekerae382792018-05-31 14:06:17 +03002064 psa_cipher_abort( operation );
2065 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002066 }
mohammad1603503973b2018-03-12 15:59:30 +02002067 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002068 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002069 {
2070 psa_cipher_abort( operation );
2071 return( mbedtls_to_psa_error( ret ) );
2072 }
2073
2074 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002075
Moran Peker395db872018-05-31 14:07:14 +03002076 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002077}
2078
Gilles Peskinee553c652018-06-04 16:22:46 +02002079psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2080 const uint8_t *input,
2081 size_t input_length,
2082 unsigned char *output,
2083 size_t output_size,
2084 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002085{
2086 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002087 size_t expected_output_size;
2088 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2089 {
2090 /* Take the unprocessed partial block left over from previous
2091 * update calls, if any, plus the input to this call. Remove
2092 * the last partial block, if any. You get the data that will be
2093 * output in this call. */
2094 expected_output_size =
2095 ( operation->ctx.cipher.unprocessed_len + input_length )
2096 / operation->block_size * operation->block_size;
2097 }
2098 else
2099 {
2100 expected_output_size = input_length;
2101 }
2102 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002103 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002104
mohammad1603503973b2018-03-12 15:59:30 +02002105 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002106 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002107 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002108 {
2109 psa_cipher_abort( operation );
2110 return( mbedtls_to_psa_error( ret ) );
2111 }
2112
Moran Peker395db872018-05-31 14:07:14 +03002113 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002114}
2115
Gilles Peskinee553c652018-06-04 16:22:46 +02002116psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2117 uint8_t *output,
2118 size_t output_size,
2119 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002120{
2121 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002122 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002123
mohammad1603503973b2018-03-12 15:59:30 +02002124 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002125 {
Moran Peker7cb22b82018-06-05 11:40:02 +03002126 psa_cipher_abort( operation );
2127 return( PSA_ERROR_BAD_STATE );
2128 }
2129 if( operation->iv_required && ! operation->iv_set )
2130 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002131 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002132 return( PSA_ERROR_BAD_STATE );
2133 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002134 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2135 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002136 {
Gilles Peskine53514202018-06-06 15:11:46 +02002137 psa_algorithm_t padding_mode =
2138 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002139 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002140 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002141 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002142 return( PSA_ERROR_TAMPERING_DETECTED );
2143 }
Gilles Peskine53514202018-06-06 15:11:46 +02002144 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002145 {
2146 if( operation->ctx.cipher.unprocessed_len != 0 )
2147 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002148 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002149 return( PSA_ERROR_INVALID_ARGUMENT );
2150 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002151 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002152 }
2153
2154 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002155 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002156 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002157 {
2158 psa_cipher_abort( operation );
2159 return( mbedtls_to_psa_error( ret ) );
2160 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002161 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002162 memcpy( output, temp_output_buffer, *output_length );
2163 else
2164 {
2165 psa_cipher_abort( operation );
2166 return( PSA_ERROR_BUFFER_TOO_SMALL );
2167 }
mohammad1603503973b2018-03-12 15:59:30 +02002168
Moran Peker4c80d832018-04-22 20:15:31 +03002169 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002170}
2171
Gilles Peskinee553c652018-06-04 16:22:46 +02002172psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2173{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002174 if( operation->alg == 0 )
2175 return( PSA_SUCCESS );
2176
mohammad1603503973b2018-03-12 15:59:30 +02002177 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002178
Moran Peker41deec42018-04-04 15:43:05 +03002179 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002180 operation->key_set = 0;
2181 operation->iv_set = 0;
2182 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002183 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002184 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002185
Moran Peker395db872018-05-31 14:07:14 +03002186 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002187}
2188
Gilles Peskinea0655c32018-04-30 17:06:50 +02002189
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002190
mohammad16038cc1cee2018-03-28 01:21:33 +03002191/****************************************************************/
2192/* Key Policy */
2193/****************************************************************/
2194
Gilles Peskine2d277862018-06-18 15:41:12 +02002195void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002196{
Gilles Peskine803ce742018-06-18 16:07:14 +02002197 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002198}
2199
Gilles Peskine2d277862018-06-18 15:41:12 +02002200void psa_key_policy_set_usage( psa_key_policy_t *policy,
2201 psa_key_usage_t usage,
2202 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002203{
mohammad16034eed7572018-03-28 05:14:59 -07002204 policy->usage = usage;
2205 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002206}
2207
Gilles Peskine2d277862018-06-18 15:41:12 +02002208psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002209{
mohammad16036df908f2018-04-02 08:34:15 -07002210 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002211}
2212
Gilles Peskine2d277862018-06-18 15:41:12 +02002213psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002214{
mohammad16036df908f2018-04-02 08:34:15 -07002215 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002216}
2217
Gilles Peskine2d277862018-06-18 15:41:12 +02002218psa_status_t psa_set_key_policy( psa_key_slot_t key,
2219 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002220{
2221 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002222
Gilles Peskine828ed142018-06-18 23:25:51 +02002223 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002224 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002225
mohammad16038cc1cee2018-03-28 01:21:33 +03002226 slot = &global_data.key_slots[key];
2227 if( slot->type != PSA_KEY_TYPE_NONE )
2228 return( PSA_ERROR_OCCUPIED_SLOT );
2229
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002230 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2231 PSA_KEY_USAGE_ENCRYPT |
2232 PSA_KEY_USAGE_DECRYPT |
2233 PSA_KEY_USAGE_SIGN |
2234 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002235 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002236
mohammad16036df908f2018-04-02 08:34:15 -07002237 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002238
2239 return( PSA_SUCCESS );
2240}
2241
Gilles Peskine2d277862018-06-18 15:41:12 +02002242psa_status_t psa_get_key_policy( psa_key_slot_t key,
2243 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002244{
2245 key_slot_t *slot;
2246
Gilles Peskine828ed142018-06-18 23:25:51 +02002247 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002248 return( PSA_ERROR_INVALID_ARGUMENT );
2249
2250 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002251
mohammad16036df908f2018-04-02 08:34:15 -07002252 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002253
2254 return( PSA_SUCCESS );
2255}
Gilles Peskine20035e32018-02-03 22:44:14 +01002256
Gilles Peskinea0655c32018-04-30 17:06:50 +02002257
2258
mohammad1603804cd712018-03-20 22:44:08 +02002259/****************************************************************/
2260/* Key Lifetime */
2261/****************************************************************/
2262
Gilles Peskine2d277862018-06-18 15:41:12 +02002263psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2264 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002265{
2266 key_slot_t *slot;
2267
Gilles Peskine828ed142018-06-18 23:25:51 +02002268 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002269 return( PSA_ERROR_INVALID_ARGUMENT );
2270
2271 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002272
mohammad1603804cd712018-03-20 22:44:08 +02002273 *lifetime = slot->lifetime;
2274
2275 return( PSA_SUCCESS );
2276}
2277
Gilles Peskine2d277862018-06-18 15:41:12 +02002278psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
2279 const psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002280{
2281 key_slot_t *slot;
2282
Gilles Peskine828ed142018-06-18 23:25:51 +02002283 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002284 return( PSA_ERROR_INVALID_ARGUMENT );
2285
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002286 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2287 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002288 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2289 return( PSA_ERROR_INVALID_ARGUMENT );
2290
mohammad1603804cd712018-03-20 22:44:08 +02002291 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002292 if( slot->type != PSA_KEY_TYPE_NONE )
2293 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002294
Moran Pekerd7326592018-05-29 16:56:39 +03002295 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002296 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002297
mohammad1603060ad8a2018-03-20 14:28:38 -07002298 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002299
2300 return( PSA_SUCCESS );
2301}
2302
Gilles Peskine20035e32018-02-03 22:44:14 +01002303
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002304
mohammad16035955c982018-04-26 00:53:03 +03002305/****************************************************************/
2306/* AEAD */
2307/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002308
mohammad16035955c982018-04-26 00:53:03 +03002309psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2310 psa_algorithm_t alg,
2311 const uint8_t *nonce,
2312 size_t nonce_length,
2313 const uint8_t *additional_data,
2314 size_t additional_data_length,
2315 const uint8_t *plaintext,
2316 size_t plaintext_length,
2317 uint8_t *ciphertext,
2318 size_t ciphertext_size,
2319 size_t *ciphertext_length )
2320{
2321 int ret;
2322 psa_status_t status;
2323 key_slot_t *slot;
2324 psa_key_type_t key_type;
2325 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002326 uint8_t *tag;
2327 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002328 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002329 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002330
mohammad1603f08a5502018-06-03 15:05:47 +03002331 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002332
mohammad16035955c982018-04-26 00:53:03 +03002333 status = psa_get_key_information( key, &key_type, &key_bits );
2334 if( status != PSA_SUCCESS )
2335 return( status );
2336 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002337 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002338 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002339
mohammad16035ed06212018-06-06 13:09:34 +03002340 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2341 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002342 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002343 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002344
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002345 if( ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) == 0 )
mohammad1603f14394b2018-06-04 14:33:19 +03002346 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002347
Gilles Peskine2d277862018-06-18 15:41:12 +02002348 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2349 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002350 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002351
mohammad16035955c982018-04-26 00:53:03 +03002352 if( alg == PSA_ALG_GCM )
2353 {
2354 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002355 tag_length = 16;
2356
mohammad160396910d82018-06-04 14:33:00 +03002357 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2358 return( PSA_ERROR_INVALID_ARGUMENT );
2359
mohammad160315223a82018-06-03 17:19:55 +03002360 //make sure we have place to hold the tag in the ciphertext buffer
2361 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002362 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002363
2364 //update the tag pointer to point to the end of the ciphertext_length
2365 tag = ciphertext + plaintext_length;
2366
mohammad16035955c982018-04-26 00:53:03 +03002367 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002368 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002369 slot->data.raw.data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002370 key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002371 if( ret != 0 )
2372 {
2373 mbedtls_gcm_free( &gcm );
2374 return( mbedtls_to_psa_error( ret ) );
2375 }
2376 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002377 plaintext_length, nonce,
2378 nonce_length, additional_data,
2379 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002380 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002381 mbedtls_gcm_free( &gcm );
2382 }
2383 else if( alg == PSA_ALG_CCM )
2384 {
2385 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002386 tag_length = 16;
2387
mohammad160396910d82018-06-04 14:33:00 +03002388 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2389 return( PSA_ERROR_INVALID_ARGUMENT );
2390
mohammad160347ddf3d2018-04-26 01:11:21 +03002391 if( nonce_length < 7 || nonce_length > 13 )
2392 return( PSA_ERROR_INVALID_ARGUMENT );
2393
mohammad160315223a82018-06-03 17:19:55 +03002394 //make sure we have place to hold the tag in the ciphertext buffer
2395 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002396 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002397
2398 //update the tag pointer to point to the end of the ciphertext_length
2399 tag = ciphertext + plaintext_length;
2400
mohammad16035955c982018-04-26 00:53:03 +03002401 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002402 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002403 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002404 if( ret != 0 )
2405 {
2406 mbedtls_ccm_free( &ccm );
2407 return( mbedtls_to_psa_error( ret ) );
2408 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002409 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002410 nonce, nonce_length,
2411 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002412 additional_data_length,
2413 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002414 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002415 mbedtls_ccm_free( &ccm );
2416 }
mohammad16035c8845f2018-05-09 05:40:09 -07002417 else
2418 {
mohammad1603554faad2018-06-03 15:07:38 +03002419 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002420 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002421
mohammad160315223a82018-06-03 17:19:55 +03002422 if( ret != 0 )
2423 {
2424 memset( ciphertext, 0, ciphertext_size );
2425 return( mbedtls_to_psa_error( ret ) );
2426 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002427
mohammad160315223a82018-06-03 17:19:55 +03002428 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002429 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002430}
2431
Gilles Peskineee652a32018-06-01 19:23:52 +02002432/* Locate the tag in a ciphertext buffer containing the encrypted data
2433 * followed by the tag. Return the length of the part preceding the tag in
2434 * *plaintext_length. This is the size of the plaintext in modes where
2435 * the encrypted data has the same size as the plaintext, such as
2436 * CCM and GCM. */
2437static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2438 const uint8_t *ciphertext,
2439 size_t ciphertext_length,
2440 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002441 const uint8_t **p_tag )
2442{
2443 size_t payload_length;
2444 if( tag_length > ciphertext_length )
2445 return( PSA_ERROR_INVALID_ARGUMENT );
2446 payload_length = ciphertext_length - tag_length;
2447 if( payload_length > plaintext_size )
2448 return( PSA_ERROR_BUFFER_TOO_SMALL );
2449 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002450 return( PSA_SUCCESS );
2451}
2452
mohammad16035955c982018-04-26 00:53:03 +03002453psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2454 psa_algorithm_t alg,
2455 const uint8_t *nonce,
2456 size_t nonce_length,
2457 const uint8_t *additional_data,
2458 size_t additional_data_length,
2459 const uint8_t *ciphertext,
2460 size_t ciphertext_length,
2461 uint8_t *plaintext,
2462 size_t plaintext_size,
2463 size_t *plaintext_length )
2464{
2465 int ret;
2466 psa_status_t status;
2467 key_slot_t *slot;
2468 psa_key_type_t key_type;
2469 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002470 const uint8_t *tag;
2471 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002472 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002473 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002474
Gilles Peskineee652a32018-06-01 19:23:52 +02002475 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002476
mohammad16035955c982018-04-26 00:53:03 +03002477 status = psa_get_key_information( key, &key_type, &key_bits );
2478 if( status != PSA_SUCCESS )
2479 return( status );
2480 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002481 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002482 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002483
mohammad16035ed06212018-06-06 13:09:34 +03002484 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2485 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002486 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002487 return( PSA_ERROR_NOT_SUPPORTED );
2488
mohammad1603f14394b2018-06-04 14:33:19 +03002489 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2490 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002491
Gilles Peskine2d277862018-06-18 15:41:12 +02002492 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2493 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002494 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002495
mohammad16035955c982018-04-26 00:53:03 +03002496 if( alg == PSA_ALG_GCM )
2497 {
2498 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002499
Gilles Peskineee652a32018-06-01 19:23:52 +02002500 tag_length = 16;
2501 status = psa_aead_unpadded_locate_tag( tag_length,
2502 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002503 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002504 if( status != PSA_SUCCESS )
2505 return( status );
2506
mohammad16035955c982018-04-26 00:53:03 +03002507 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002508 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002509 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002510 if( ret != 0 )
2511 {
2512 mbedtls_gcm_free( &gcm );
2513 return( mbedtls_to_psa_error( ret ) );
2514 }
mohammad16035955c982018-04-26 00:53:03 +03002515
Gilles Peskineee652a32018-06-01 19:23:52 +02002516 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002517 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002518 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002519 additional_data,
2520 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002521 tag, tag_length,
2522 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002523 mbedtls_gcm_free( &gcm );
2524 }
2525 else if( alg == PSA_ALG_CCM )
2526 {
2527 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002528
mohammad160347ddf3d2018-04-26 01:11:21 +03002529 if( nonce_length < 7 || nonce_length > 13 )
2530 return( PSA_ERROR_INVALID_ARGUMENT );
2531
mohammad16039375f842018-06-03 14:28:24 +03002532 tag_length = 16;
2533 status = psa_aead_unpadded_locate_tag( tag_length,
2534 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002535 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002536 if( status != PSA_SUCCESS )
2537 return( status );
2538
mohammad16035955c982018-04-26 00:53:03 +03002539 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002540 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002541 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002542 if( ret != 0 )
2543 {
2544 mbedtls_ccm_free( &ccm );
2545 return( mbedtls_to_psa_error( ret ) );
2546 }
mohammad160360a64d02018-06-03 17:20:42 +03002547 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002548 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002549 additional_data,
2550 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002551 ciphertext, plaintext,
2552 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002553 mbedtls_ccm_free( &ccm );
2554 }
mohammad160339574652018-06-01 04:39:53 -07002555 else
2556 {
mohammad1603554faad2018-06-03 15:07:38 +03002557 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002558 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002559
Gilles Peskineee652a32018-06-01 19:23:52 +02002560 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002561 memset( plaintext, 0, plaintext_size );
2562 else
2563 *plaintext_length = ciphertext_length - tag_length;
2564
Gilles Peskineee652a32018-06-01 19:23:52 +02002565 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002566}
2567
Gilles Peskinea0655c32018-04-30 17:06:50 +02002568
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002569
Gilles Peskine20035e32018-02-03 22:44:14 +01002570/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002571/* Key generation */
2572/****************************************************************/
2573
2574psa_status_t psa_generate_random( uint8_t *output,
2575 size_t output_size )
2576{
2577 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2578 output, output_size );
2579 return( mbedtls_to_psa_error( ret ) );
2580}
2581
2582psa_status_t psa_generate_key( psa_key_slot_t key,
2583 psa_key_type_t type,
2584 size_t bits,
2585 const void *parameters,
2586 size_t parameters_size )
2587{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002588 key_slot_t *slot;
2589
2590 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
2591 return( PSA_ERROR_INVALID_ARGUMENT );
2592 slot = &global_data.key_slots[key];
2593 if( slot->type != PSA_KEY_TYPE_NONE )
2594 return( PSA_ERROR_OCCUPIED_SLOT );
2595 if( parameters == NULL && parameters_size != 0 )
2596 return( PSA_ERROR_INVALID_ARGUMENT );
2597
2598 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
2599 {
2600 psa_status_t status = prepare_raw_data_slot( type, bits,
2601 &slot->data.raw );
2602 if( status != PSA_SUCCESS )
2603 return( status );
2604 status = psa_generate_random( slot->data.raw.data,
2605 slot->data.raw.bytes );
2606 if( status != PSA_SUCCESS )
2607 {
2608 mbedtls_free( slot->data.raw.data );
2609 return( status );
2610 }
2611#if defined(MBEDTLS_DES_C)
2612 if( type == PSA_KEY_TYPE_DES )
2613 {
2614 mbedtls_des_key_set_parity( slot->data.raw.data );
2615 if( slot->data.raw.bytes >= 16 )
2616 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2617 if( slot->data.raw.bytes == 24 )
2618 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2619 }
2620#endif /* MBEDTLS_DES_C */
2621 }
2622 else
2623
2624#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2625 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2626 {
2627 mbedtls_rsa_context *rsa;
2628 int ret;
2629 int exponent = 65537;
2630 if( parameters != NULL )
2631 {
2632 const unsigned *p = parameters;
2633 if( parameters_size != sizeof( *p ) )
2634 return( PSA_ERROR_INVALID_ARGUMENT );
2635 if( *p > INT_MAX )
2636 return( PSA_ERROR_INVALID_ARGUMENT );
2637 exponent = *p;
2638 }
2639 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
2640 if( rsa == NULL )
2641 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2642 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
2643 ret = mbedtls_rsa_gen_key( rsa,
2644 mbedtls_ctr_drbg_random,
2645 &global_data.ctr_drbg,
2646 bits,
2647 exponent );
2648 if( ret != 0 )
2649 {
2650 mbedtls_rsa_free( rsa );
2651 mbedtls_free( rsa );
2652 return( mbedtls_to_psa_error( ret ) );
2653 }
2654 slot->data.rsa = rsa;
2655 }
2656 else
2657#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
2658
2659#if defined(MBEDTLS_ECP_C)
2660 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
2661 {
2662 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
2663 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
2664 const mbedtls_ecp_curve_info *curve_info =
2665 mbedtls_ecp_curve_info_from_grp_id( grp_id );
2666 mbedtls_ecp_keypair *ecp;
2667 int ret;
2668 if( parameters != NULL )
2669 return( PSA_ERROR_NOT_SUPPORTED );
2670 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
2671 return( PSA_ERROR_NOT_SUPPORTED );
2672 if( curve_info->bit_size != bits )
2673 return( PSA_ERROR_INVALID_ARGUMENT );
2674 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
2675 if( ecp == NULL )
2676 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2677 mbedtls_ecp_keypair_init( ecp );
2678 ret = mbedtls_ecp_gen_key( grp_id, ecp,
2679 mbedtls_ctr_drbg_random,
2680 &global_data.ctr_drbg );
2681 if( ret != 0 )
2682 {
2683 mbedtls_ecp_keypair_free( ecp );
2684 mbedtls_free( ecp );
2685 return( mbedtls_to_psa_error( ret ) );
2686 }
2687 slot->data.ecp = ecp;
2688 }
2689 else
2690#endif /* MBEDTLS_ECP_C */
2691
2692 return( PSA_ERROR_NOT_SUPPORTED );
2693
2694 slot->type = type;
2695 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02002696}
2697
2698
2699/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002700/* Module setup */
2701/****************************************************************/
2702
Gilles Peskinee59236f2018-01-27 23:32:46 +01002703void mbedtls_psa_crypto_free( void )
2704{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002705 size_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02002706 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002707 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002708 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2709 mbedtls_entropy_free( &global_data.entropy );
2710 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2711}
2712
2713psa_status_t psa_crypto_init( void )
2714{
2715 int ret;
2716 const unsigned char drbg_seed[] = "PSA";
2717
2718 if( global_data.initialized != 0 )
2719 return( PSA_SUCCESS );
2720
2721 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2722 mbedtls_entropy_init( &global_data.entropy );
2723 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2724
2725 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2726 mbedtls_entropy_func,
2727 &global_data.entropy,
2728 drbg_seed, sizeof( drbg_seed ) - 1 );
2729 if( ret != 0 )
2730 goto exit;
2731
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002732 global_data.initialized = 1;
2733
Gilles Peskinee59236f2018-01-27 23:32:46 +01002734exit:
2735 if( ret != 0 )
2736 mbedtls_psa_crypto_free( );
2737 return( mbedtls_to_psa_error( ret ) );
2738}
2739
2740#endif /* MBEDTLS_PSA_CRYPTO_C */