blob: 37befc0e5ca954653aa0b008d33dbb02d16bb85e [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. */
95#define MBEDTLS_PSA_KEY_SLOT_COUNT 32
96
97typedef struct {
98 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +030099 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200100 psa_key_lifetime_t lifetime;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101 union {
102 struct raw_data {
103 uint8_t *data;
104 size_t bytes;
105 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100106#if defined(MBEDTLS_RSA_C)
107 mbedtls_rsa_context *rsa;
108#endif /* MBEDTLS_RSA_C */
109#if defined(MBEDTLS_ECP_C)
110 mbedtls_ecp_keypair *ecp;
111#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100112 } data;
113} key_slot_t;
114
Gilles Peskinee59236f2018-01-27 23:32:46 +0100115typedef struct {
116 int initialized;
117 mbedtls_entropy_context entropy;
118 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100119 key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120} psa_global_data_t;
121
122static psa_global_data_t global_data;
123
124static psa_status_t mbedtls_to_psa_error( int ret )
125{
Gilles Peskinea5905292018-02-07 20:59:33 +0100126 /* If there's both a high-level code and low-level code, dispatch on
127 * the high-level code. */
128 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129 {
130 case 0:
131 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100132
133 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
134 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
135 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
136 return( PSA_ERROR_NOT_SUPPORTED );
137 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
138 return( PSA_ERROR_HARDWARE_FAILURE );
139
140 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
141 return( PSA_ERROR_HARDWARE_FAILURE );
142
143 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
144 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
145 return( PSA_ERROR_NOT_SUPPORTED );
146 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
147 return( PSA_ERROR_HARDWARE_FAILURE );
148
149 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
150 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
151 return( PSA_ERROR_NOT_SUPPORTED );
152 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
153 return( PSA_ERROR_HARDWARE_FAILURE );
154
155 case MBEDTLS_ERR_CCM_BAD_INPUT:
156 return( PSA_ERROR_INVALID_ARGUMENT );
157 case MBEDTLS_ERR_CCM_AUTH_FAILED:
158 return( PSA_ERROR_INVALID_SIGNATURE );
159 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
160 return( PSA_ERROR_HARDWARE_FAILURE );
161
162 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
163 return( PSA_ERROR_NOT_SUPPORTED );
164 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
165 return( PSA_ERROR_INVALID_ARGUMENT );
166 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
167 return( PSA_ERROR_INSUFFICIENT_MEMORY );
168 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
169 return( PSA_ERROR_INVALID_PADDING );
170 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
171 return( PSA_ERROR_BAD_STATE );
172 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
173 return( PSA_ERROR_INVALID_SIGNATURE );
174 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
175 return( PSA_ERROR_TAMPERING_DETECTED );
176 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
179 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
182 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
183 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
184 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
185 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
186 return( PSA_ERROR_NOT_SUPPORTED );
187 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
188 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
189
190 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
191 return( PSA_ERROR_NOT_SUPPORTED );
192 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
193 return( PSA_ERROR_HARDWARE_FAILURE );
194
Gilles Peskinee59236f2018-01-27 23:32:46 +0100195 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
196 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
197 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100199
200 case MBEDTLS_ERR_GCM_AUTH_FAILED:
201 return( PSA_ERROR_INVALID_SIGNATURE );
202 case MBEDTLS_ERR_GCM_BAD_INPUT:
203 return( PSA_ERROR_NOT_SUPPORTED );
204 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
205 return( PSA_ERROR_HARDWARE_FAILURE );
206
207 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
208 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
209 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
213 return( PSA_ERROR_NOT_SUPPORTED );
214 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
215 return( PSA_ERROR_INVALID_ARGUMENT );
216 case MBEDTLS_ERR_MD_ALLOC_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_MEMORY );
218 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
219 return( PSA_ERROR_STORAGE_FAILURE );
220 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
221 return( PSA_ERROR_HARDWARE_FAILURE );
222
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100223 case MBEDTLS_ERR_PK_ALLOC_FAILED:
224 return( PSA_ERROR_INSUFFICIENT_MEMORY );
225 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
226 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
227 return( PSA_ERROR_INVALID_ARGUMENT );
228 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100230 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
231 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
232 return( PSA_ERROR_INVALID_ARGUMENT );
233 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
234 return( PSA_ERROR_NOT_SUPPORTED );
235 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
236 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
237 return( PSA_ERROR_NOT_PERMITTED );
238 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
239 return( PSA_ERROR_INVALID_ARGUMENT );
240 case MBEDTLS_ERR_PK_INVALID_ALG:
241 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
242 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
243 return( PSA_ERROR_NOT_SUPPORTED );
244 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
245 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100246 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
247 return( PSA_ERROR_HARDWARE_FAILURE );
248
249 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
250 return( PSA_ERROR_HARDWARE_FAILURE );
251
252 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
253 return( PSA_ERROR_INVALID_ARGUMENT );
254 case MBEDTLS_ERR_RSA_INVALID_PADDING:
255 return( PSA_ERROR_INVALID_PADDING );
256 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
257 return( PSA_ERROR_HARDWARE_FAILURE );
258 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
261 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
262 return( PSA_ERROR_TAMPERING_DETECTED );
263 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
264 return( PSA_ERROR_INVALID_SIGNATURE );
265 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
266 return( PSA_ERROR_BUFFER_TOO_SMALL );
267 case MBEDTLS_ERR_RSA_RNG_FAILED:
268 return( PSA_ERROR_INSUFFICIENT_MEMORY );
269 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
270 return( PSA_ERROR_NOT_SUPPORTED );
271 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
272 return( PSA_ERROR_HARDWARE_FAILURE );
273
274 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
275 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
276 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
277 return( PSA_ERROR_HARDWARE_FAILURE );
278
279 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
282 return( PSA_ERROR_HARDWARE_FAILURE );
283
Gilles Peskinee59236f2018-01-27 23:32:46 +0100284 default:
285 return( PSA_ERROR_UNKNOWN_ERROR );
286 }
287}
288
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100289
290
291/****************************************************************/
292/* Key management */
293/****************************************************************/
294
295psa_status_t psa_import_key(psa_key_slot_t key,
296 psa_key_type_t type,
297 const uint8_t *data,
298 size_t data_length)
299{
300 key_slot_t *slot;
301
302 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 slot = &global_data.key_slots[key];
305 if( slot->type != PSA_KEY_TYPE_NONE )
306 return( PSA_ERROR_OCCUPIED_SLOT );
307
Gilles Peskine8c9def32018-02-08 10:02:12 +0100308 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100310 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 if( data_length > SIZE_MAX / 8 )
312 return( PSA_ERROR_NOT_SUPPORTED );
313 slot->data.raw.data = mbedtls_calloc( 1, data_length );
314 if( slot->data.raw.data == NULL )
315 return( PSA_ERROR_INSUFFICIENT_MEMORY );
316 memcpy( slot->data.raw.data, data, data_length );
317 slot->data.raw.bytes = data_length;
318 }
319 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100320#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100321 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
322 type == PSA_KEY_TYPE_RSA_KEYPAIR ||
323 PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324 {
325 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100326 mbedtls_pk_context pk;
327 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100328 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
329 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
330 else
331 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100332 if( ret != 0 )
333 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100334 switch( mbedtls_pk_get_type( &pk ) )
335 {
336#if defined(MBEDTLS_RSA_C)
337 case MBEDTLS_PK_RSA:
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100338 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
339 type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine969ac722018-01-28 18:16:59 +0100340 slot->data.rsa = pk.pk_ctx;
341 else
342 return( PSA_ERROR_INVALID_ARGUMENT );
343 break;
344#endif /* MBEDTLS_RSA_C */
345#if defined(MBEDTLS_ECP_C)
346 case MBEDTLS_PK_ECKEY:
347 if( PSA_KEY_TYPE_IS_ECC( type ) )
348 {
349 // TODO: check curve
350 slot->data.ecp = pk.pk_ctx;
351 }
352 else
353 return( PSA_ERROR_INVALID_ARGUMENT );
354 break;
355#endif /* MBEDTLS_ECP_C */
356 default:
357 return( PSA_ERROR_INVALID_ARGUMENT );
358 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359 }
360 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100361#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362 {
363 return( PSA_ERROR_NOT_SUPPORTED );
364 }
365
366 slot->type = type;
367 return( PSA_SUCCESS );
368}
369
370psa_status_t psa_destroy_key(psa_key_slot_t key)
371{
372 key_slot_t *slot;
373
374 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
375 return( PSA_ERROR_INVALID_ARGUMENT );
376 slot = &global_data.key_slots[key];
377 if( slot->type == PSA_KEY_TYPE_NONE )
378 return( PSA_ERROR_EMPTY_SLOT );
379
Gilles Peskine8c9def32018-02-08 10:02:12 +0100380 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100381 {
382 mbedtls_free( slot->data.raw.data );
383 }
384 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100385#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100386 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
387 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100388 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100389 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100390 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100391 }
392 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100393#endif /* defined(MBEDTLS_RSA_C) */
394#if defined(MBEDTLS_ECP_C)
395 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
396 {
397 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100398 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100399 }
400 else
401#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100402 {
403 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100404 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100405 return( PSA_ERROR_TAMPERING_DETECTED );
406 }
407
408 mbedtls_zeroize( slot, sizeof( *slot ) );
409 return( PSA_SUCCESS );
410}
411
412psa_status_t psa_get_key_information(psa_key_slot_t key,
413 psa_key_type_t *type,
414 size_t *bits)
415{
416 key_slot_t *slot;
417
418 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100419 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100420 slot = &global_data.key_slots[key];
421 if( type != NULL )
422 *type = slot->type;
423 if( bits != NULL )
424 *bits = 0;
425 if( slot->type == PSA_KEY_TYPE_NONE )
426 return( PSA_ERROR_EMPTY_SLOT );
427
Gilles Peskine8c9def32018-02-08 10:02:12 +0100428 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100429 {
430 if( bits != NULL )
431 *bits = slot->data.raw.bytes * 8;
432 }
433 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100434#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100435 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
436 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100437 {
438 if( bits != NULL )
Gilles Peskine969ac722018-01-28 18:16:59 +0100439 *bits = mbedtls_rsa_get_bitlen( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100440 }
441 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100442#endif /* defined(MBEDTLS_RSA_C) */
443#if defined(MBEDTLS_ECP_C)
444 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
445 {
446 if( bits != NULL )
447 *bits = slot->data.ecp->grp.pbits;
448 }
449 else
450#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100451 {
452 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100453 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100454 return( PSA_ERROR_TAMPERING_DETECTED );
455 }
456
457 return( PSA_SUCCESS );
458}
459
460psa_status_t psa_export_key(psa_key_slot_t key,
461 uint8_t *data,
462 size_t data_size,
463 size_t *data_length)
464{
465 key_slot_t *slot;
466
467 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100468 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100469 slot = &global_data.key_slots[key];
470 if( slot->type == PSA_KEY_TYPE_NONE )
471 return( PSA_ERROR_EMPTY_SLOT );
472
mohammad160306e79202018-03-28 13:17:44 +0300473 if( !( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) )
474 return( PSA_ERROR_NOT_PERMITTED );
475
Gilles Peskine8c9def32018-02-08 10:02:12 +0100476 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100477 {
478 if( slot->data.raw.bytes > data_size )
479 return( PSA_ERROR_BUFFER_TOO_SMALL );
480 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
481 *data_length = slot->data.raw.bytes;
482 return( PSA_SUCCESS );
483 }
484 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100485#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100486 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
487 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100488 PSA_KEY_TYPE_IS_ECC( slot->type ) )
489 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100490 mbedtls_pk_context pk;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100491 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100492 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100493 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
494 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine969ac722018-01-28 18:16:59 +0100495 {
496 pk.pk_info = &mbedtls_rsa_info;
497 pk.pk_ctx = slot->data.rsa;
498 }
499 else
500 {
501 pk.pk_info = &mbedtls_eckey_info;
502 pk.pk_ctx = slot->data.ecp;
503 }
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100504 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
505 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
506 else
507 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100508 if( ret < 0 )
509 return( mbedtls_to_psa_error( ret ) );
510 *data_length = ret;
511 return( PSA_SUCCESS );
512 }
513 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100514#endif /* defined(MBEDTLS_PK_WRITE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100516 /* This shouldn't happen in the reference implementation, but
517 it is valid for a special-purpose implementation to omit
518 support for exporting certain key types. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100519 return( PSA_ERROR_NOT_SUPPORTED );
520 }
521}
522
523
524
525/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100526/* Message digests */
527/****************************************************************/
528
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100529static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100530{
531 switch( alg )
532 {
533#if defined(MBEDTLS_MD2_C)
534 case PSA_ALG_MD2:
535 return( &mbedtls_md2_info );
536#endif
537#if defined(MBEDTLS_MD4_C)
538 case PSA_ALG_MD4:
539 return( &mbedtls_md4_info );
540#endif
541#if defined(MBEDTLS_MD5_C)
542 case PSA_ALG_MD5:
543 return( &mbedtls_md5_info );
544#endif
545#if defined(MBEDTLS_RIPEMD160_C)
546 case PSA_ALG_RIPEMD160:
547 return( &mbedtls_ripemd160_info );
548#endif
549#if defined(MBEDTLS_SHA1_C)
550 case PSA_ALG_SHA_1:
551 return( &mbedtls_sha1_info );
552#endif
553#if defined(MBEDTLS_SHA256_C)
554 case PSA_ALG_SHA_224:
555 return( &mbedtls_sha224_info );
556 case PSA_ALG_SHA_256:
557 return( &mbedtls_sha256_info );
558#endif
559#if defined(MBEDTLS_SHA512_C)
560 case PSA_ALG_SHA_384:
561 return( &mbedtls_sha384_info );
562 case PSA_ALG_SHA_512:
563 return( &mbedtls_sha512_info );
564#endif
565 default:
566 return( NULL );
567 }
568}
569
570#if 0
571static psa_algorithm_t mbedtls_md_alg_to_psa( mbedtls_md_type_t md_alg )
572{
573 switch( md_alg )
574 {
575 case MBEDTLS_MD_NONE:
576 return( 0 );
577 case MBEDTLS_MD_MD2:
578 return( PSA_ALG_MD2 );
579 case MBEDTLS_MD_MD4:
580 return( PSA_ALG_MD4 );
581 case MBEDTLS_MD_MD5:
582 return( PSA_ALG_MD5 );
583 case MBEDTLS_MD_SHA1:
584 return( PSA_ALG_SHA_1 );
585 case MBEDTLS_MD_SHA224:
586 return( PSA_ALG_SHA_224 );
587 case MBEDTLS_MD_SHA256:
588 return( PSA_ALG_SHA_256 );
589 case MBEDTLS_MD_SHA384:
590 return( PSA_ALG_SHA_384 );
591 case MBEDTLS_MD_SHA512:
592 return( PSA_ALG_SHA_512 );
593 case MBEDTLS_MD_RIPEMD160:
594 return( PSA_ALG_RIPEMD160 );
595 default:
Gilles Peskine47c1bc02018-03-20 17:55:04 +0100596 return( 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +0100597 }
598}
599#endif
600
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100601psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
602{
603 switch( operation->alg )
604 {
605#if defined(MBEDTLS_MD2_C)
606 case PSA_ALG_MD2:
607 mbedtls_md2_free( &operation->ctx.md2 );
608 break;
609#endif
610#if defined(MBEDTLS_MD4_C)
611 case PSA_ALG_MD4:
612 mbedtls_md4_free( &operation->ctx.md4 );
613 break;
614#endif
615#if defined(MBEDTLS_MD5_C)
616 case PSA_ALG_MD5:
617 mbedtls_md5_free( &operation->ctx.md5 );
618 break;
619#endif
620#if defined(MBEDTLS_RIPEMD160_C)
621 case PSA_ALG_RIPEMD160:
622 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
623 break;
624#endif
625#if defined(MBEDTLS_SHA1_C)
626 case PSA_ALG_SHA_1:
627 mbedtls_sha1_free( &operation->ctx.sha1 );
628 break;
629#endif
630#if defined(MBEDTLS_SHA256_C)
631 case PSA_ALG_SHA_224:
632 case PSA_ALG_SHA_256:
633 mbedtls_sha256_free( &operation->ctx.sha256 );
634 break;
635#endif
636#if defined(MBEDTLS_SHA512_C)
637 case PSA_ALG_SHA_384:
638 case PSA_ALG_SHA_512:
639 mbedtls_sha512_free( &operation->ctx.sha512 );
640 break;
641#endif
642 default:
643 return( PSA_ERROR_NOT_SUPPORTED );
644 }
645 operation->alg = 0;
646 return( PSA_SUCCESS );
647}
648
649psa_status_t psa_hash_start( psa_hash_operation_t *operation,
650 psa_algorithm_t alg )
651{
652 int ret;
653 operation->alg = 0;
654 switch( alg )
655 {
656#if defined(MBEDTLS_MD2_C)
657 case PSA_ALG_MD2:
658 mbedtls_md2_init( &operation->ctx.md2 );
659 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
660 break;
661#endif
662#if defined(MBEDTLS_MD4_C)
663 case PSA_ALG_MD4:
664 mbedtls_md4_init( &operation->ctx.md4 );
665 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
666 break;
667#endif
668#if defined(MBEDTLS_MD5_C)
669 case PSA_ALG_MD5:
670 mbedtls_md5_init( &operation->ctx.md5 );
671 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
672 break;
673#endif
674#if defined(MBEDTLS_RIPEMD160_C)
675 case PSA_ALG_RIPEMD160:
676 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
677 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
678 break;
679#endif
680#if defined(MBEDTLS_SHA1_C)
681 case PSA_ALG_SHA_1:
682 mbedtls_sha1_init( &operation->ctx.sha1 );
683 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
684 break;
685#endif
686#if defined(MBEDTLS_SHA256_C)
687 case PSA_ALG_SHA_224:
688 mbedtls_sha256_init( &operation->ctx.sha256 );
689 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
690 break;
691 case PSA_ALG_SHA_256:
692 mbedtls_sha256_init( &operation->ctx.sha256 );
693 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
694 break;
695#endif
696#if defined(MBEDTLS_SHA512_C)
697 case PSA_ALG_SHA_384:
698 mbedtls_sha512_init( &operation->ctx.sha512 );
699 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
700 break;
701 case PSA_ALG_SHA_512:
702 mbedtls_sha512_init( &operation->ctx.sha512 );
703 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
704 break;
705#endif
706 default:
707 return( PSA_ERROR_NOT_SUPPORTED );
708 }
709 if( ret == 0 )
710 operation->alg = alg;
711 else
712 psa_hash_abort( operation );
713 return( mbedtls_to_psa_error( ret ) );
714}
715
716psa_status_t psa_hash_update( psa_hash_operation_t *operation,
717 const uint8_t *input,
718 size_t input_length )
719{
720 int ret;
721 switch( operation->alg )
722 {
723#if defined(MBEDTLS_MD2_C)
724 case PSA_ALG_MD2:
725 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
726 input, input_length );
727 break;
728#endif
729#if defined(MBEDTLS_MD4_C)
730 case PSA_ALG_MD4:
731 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
732 input, input_length );
733 break;
734#endif
735#if defined(MBEDTLS_MD5_C)
736 case PSA_ALG_MD5:
737 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
738 input, input_length );
739 break;
740#endif
741#if defined(MBEDTLS_RIPEMD160_C)
742 case PSA_ALG_RIPEMD160:
743 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
744 input, input_length );
745 break;
746#endif
747#if defined(MBEDTLS_SHA1_C)
748 case PSA_ALG_SHA_1:
749 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
750 input, input_length );
751 break;
752#endif
753#if defined(MBEDTLS_SHA256_C)
754 case PSA_ALG_SHA_224:
755 case PSA_ALG_SHA_256:
756 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
757 input, input_length );
758 break;
759#endif
760#if defined(MBEDTLS_SHA512_C)
761 case PSA_ALG_SHA_384:
762 case PSA_ALG_SHA_512:
763 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
764 input, input_length );
765 break;
766#endif
767 default:
768 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
769 break;
770 }
771 if( ret != 0 )
772 psa_hash_abort( operation );
773 return( mbedtls_to_psa_error( ret ) );
774}
775
776psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
777 uint8_t *hash,
778 size_t hash_size,
779 size_t *hash_length )
780{
781 int ret;
782 size_t actual_hash_length = PSA_HASH_FINAL_SIZE( operation->alg );
783
784 /* Fill the output buffer with something that isn't a valid hash
785 * (barring an attack on the hash and deliberately-crafted input),
786 * in case the caller doesn't check the return status properly. */
787 *hash_length = actual_hash_length;
788 memset( hash, '!', hash_size );
789
790 if( hash_size < actual_hash_length )
791 return( PSA_ERROR_BUFFER_TOO_SMALL );
792
793 switch( operation->alg )
794 {
795#if defined(MBEDTLS_MD2_C)
796 case PSA_ALG_MD2:
797 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
798 break;
799#endif
800#if defined(MBEDTLS_MD4_C)
801 case PSA_ALG_MD4:
802 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
803 break;
804#endif
805#if defined(MBEDTLS_MD5_C)
806 case PSA_ALG_MD5:
807 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
808 break;
809#endif
810#if defined(MBEDTLS_RIPEMD160_C)
811 case PSA_ALG_RIPEMD160:
812 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
813 break;
814#endif
815#if defined(MBEDTLS_SHA1_C)
816 case PSA_ALG_SHA_1:
817 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
818 break;
819#endif
820#if defined(MBEDTLS_SHA256_C)
821 case PSA_ALG_SHA_224:
822 case PSA_ALG_SHA_256:
823 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
824 break;
825#endif
826#if defined(MBEDTLS_SHA512_C)
827 case PSA_ALG_SHA_384:
828 case PSA_ALG_SHA_512:
829 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
830 break;
831#endif
832 default:
833 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
834 break;
835 }
836
837 if( ret == 0 )
838 {
839 return( psa_hash_abort( operation ) );
840 }
841 else
842 {
843 psa_hash_abort( operation );
844 return( mbedtls_to_psa_error( ret ) );
845 }
846}
847
848psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
849 const uint8_t *hash,
850 size_t hash_length)
851{
852 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
853 size_t actual_hash_length;
854 psa_status_t status = psa_hash_finish( operation,
855 actual_hash, sizeof( actual_hash ),
856 &actual_hash_length );
857 if( status != PSA_SUCCESS )
858 return( status );
859 if( actual_hash_length != hash_length )
860 return( PSA_ERROR_INVALID_SIGNATURE );
861 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
862 return( PSA_ERROR_INVALID_SIGNATURE );
863 return( PSA_SUCCESS );
864}
865
866
867
868
869/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +0100870/* MAC */
871/****************************************************************/
872
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100873static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +0100874 psa_algorithm_t alg,
875 psa_key_type_t key_type,
876 size_t key_bits )
877{
878 mbedtls_cipher_id_t cipher_id;
879 mbedtls_cipher_mode_t mode;
880
881 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
882 {
Gilles Peskine8c9def32018-02-08 10:02:12 +0100883 switch( alg )
884 {
885 case PSA_ALG_STREAM_CIPHER:
886 mode = MBEDTLS_MODE_STREAM;
887 break;
mohammad1603990a18c2018-03-14 15:15:33 +0200888 case PSA_ALG_ECB_BASE:
889 mode = MBEDTLS_MODE_ECB;
890 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100891 case PSA_ALG_CBC_BASE:
892 mode = MBEDTLS_MODE_CBC;
893 break;
894 case PSA_ALG_CFB_BASE:
895 mode = MBEDTLS_MODE_CFB;
896 break;
897 case PSA_ALG_OFB_BASE:
898 mode = MBEDTLS_MODE_OFB;
899 break;
900 case PSA_ALG_CTR:
901 mode = MBEDTLS_MODE_CTR;
902 break;
903 case PSA_ALG_CCM:
904 mode = MBEDTLS_MODE_CCM;
905 break;
906 case PSA_ALG_GCM:
907 mode = MBEDTLS_MODE_GCM;
908 break;
909 default:
910 return( NULL );
911 }
912 }
913 else if( alg == PSA_ALG_CMAC )
914 mode = MBEDTLS_MODE_ECB;
915 else if( alg == PSA_ALG_GMAC )
916 mode = MBEDTLS_MODE_GCM;
917 else
918 return( NULL );
919
920 switch( key_type )
921 {
922 case PSA_KEY_TYPE_AES:
923 cipher_id = MBEDTLS_CIPHER_ID_AES;
924 break;
925 case PSA_KEY_TYPE_DES:
926 if( key_bits == 64 )
927 cipher_id = MBEDTLS_CIPHER_ID_DES;
928 else
929 cipher_id = MBEDTLS_CIPHER_ID_3DES;
930 break;
931 case PSA_KEY_TYPE_CAMELLIA:
932 cipher_id = MBEDTLS_CIPHER_ID_CAMELLIA;
933 break;
934 case PSA_KEY_TYPE_ARC4:
935 cipher_id = MBEDTLS_CIPHER_ID_ARC4;
936 break;
937 default:
938 return( NULL );
939 }
940
941 return( mbedtls_cipher_info_from_values( cipher_id, key_bits, mode ) );
942}
943
944psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
945{
946 switch( operation->alg )
947 {
948#if defined(MBEDTLS_CMAC_C)
949 case PSA_ALG_CMAC:
950 mbedtls_cipher_free( &operation->ctx.cmac );
951 break;
952#endif /* MBEDTLS_CMAC_C */
953 default:
954#if defined(MBEDTLS_MD_C)
955 if( PSA_ALG_IS_HMAC( operation->alg ) )
956 mbedtls_md_free( &operation->ctx.hmac );
957 else
958#endif /* MBEDTLS_MD_C */
959 return( PSA_ERROR_NOT_SUPPORTED );
960 }
961 operation->alg = 0;
962 operation->key_set = 0;
963 operation->iv_set = 0;
964 operation->iv_required = 0;
965 operation->has_input = 0;
966 return( PSA_SUCCESS );
967}
968
969psa_status_t psa_mac_start( psa_mac_operation_t *operation,
970 psa_key_slot_t key,
971 psa_algorithm_t alg )
972{
973 int ret = MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
974 psa_status_t status;
975 key_slot_t *slot;
976 psa_key_type_t key_type;
977 size_t key_bits;
978 const mbedtls_cipher_info_t *cipher_info = NULL;
979
980 operation->alg = 0;
981 operation->key_set = 0;
982 operation->iv_set = 0;
983 operation->iv_required = 1;
984 operation->has_input = 0;
985
986 status = psa_get_key_information( key, &key_type, &key_bits );
987 if( status != PSA_SUCCESS )
988 return( status );
989 slot = &global_data.key_slots[key];
990
mohammad16036df908f2018-04-02 08:34:15 -0700991 if ( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
992 operation->key_usage_sign = 1;
993
994 if ( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
995 operation->key_usage_verify = 1;
996
Gilles Peskine8c9def32018-02-08 10:02:12 +0100997 if( ! PSA_ALG_IS_HMAC( alg ) )
998 {
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100999 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001000 if( cipher_info == NULL )
1001 return( PSA_ERROR_NOT_SUPPORTED );
1002 operation->mac_size = cipher_info->block_size;
1003 }
1004 switch( alg )
1005 {
1006#if defined(MBEDTLS_CMAC_C)
1007 case PSA_ALG_CMAC:
1008 operation->iv_required = 0;
1009 mbedtls_cipher_init( &operation->ctx.cmac );
1010 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1011 if( ret != 0 )
1012 break;
1013 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1014 slot->data.raw.data,
1015 key_bits );
1016 break;
1017#endif /* MBEDTLS_CMAC_C */
1018 default:
1019#if defined(MBEDTLS_MD_C)
1020 if( PSA_ALG_IS_HMAC( alg ) )
1021 {
1022 const mbedtls_md_info_t *md_info =
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001023 mbedtls_md_info_from_psa( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001024 if( md_info == NULL )
1025 return( PSA_ERROR_NOT_SUPPORTED );
1026 if( key_type != PSA_KEY_TYPE_HMAC )
1027 return( PSA_ERROR_INVALID_ARGUMENT );
1028 operation->iv_required = 0;
1029 operation->mac_size = mbedtls_md_get_size( md_info );
1030 mbedtls_md_init( &operation->ctx.hmac );
1031 ret = mbedtls_md_setup( &operation->ctx.hmac, md_info, 1 );
1032 if( ret != 0 )
1033 break;
1034 ret = mbedtls_md_hmac_starts( &operation->ctx.hmac,
1035 slot->data.raw.data,
1036 slot->data.raw.bytes );
1037 break;
1038 }
1039 else
1040#endif /* MBEDTLS_MD_C */
1041 return( PSA_ERROR_NOT_SUPPORTED );
1042 }
1043
1044 /* If we reach this point, then the algorithm-specific part of the
1045 * context has at least been initialized, and may contain data that
1046 * needs to be wiped on error. */
1047 operation->alg = alg;
1048 if( ret != 0 )
1049 {
1050 psa_mac_abort( operation );
1051 return( mbedtls_to_psa_error( ret ) );
1052 }
1053 operation->key_set = 1;
Gilles Peskine47c1bc02018-03-20 17:55:04 +01001054 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001055}
1056
1057psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1058 const uint8_t *input,
1059 size_t input_length )
1060{
1061 int ret;
1062 if( ! operation->key_set )
1063 return( PSA_ERROR_BAD_STATE );
1064 if( operation->iv_required && ! operation->iv_set )
1065 return( PSA_ERROR_BAD_STATE );
1066 operation->has_input = 1;
1067
1068 switch( operation->alg )
1069 {
1070#if defined(MBEDTLS_CMAC_C)
1071 case PSA_ALG_CMAC:
1072 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1073 input, input_length );
1074 break;
1075#endif /* MBEDTLS_CMAC_C */
1076 default:
1077#if defined(MBEDTLS_MD_C)
1078 if( PSA_ALG_IS_HMAC( operation->alg ) )
1079 {
1080 ret = mbedtls_md_hmac_update( &operation->ctx.hmac,
1081 input, input_length );
1082 }
1083 else
1084#endif /* MBEDTLS_MD_C */
1085 {
1086 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1087 }
1088 break;
1089 }
1090 if( ret != 0 )
1091 psa_mac_abort( operation );
1092 return( mbedtls_to_psa_error( ret ) );
1093}
1094
mohammad16036df908f2018-04-02 08:34:15 -07001095static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01001096 uint8_t *mac,
1097 size_t mac_size,
1098 size_t *mac_length )
1099{
1100 int ret;
1101 if( ! operation->key_set )
1102 return( PSA_ERROR_BAD_STATE );
1103 if( operation->iv_required && ! operation->iv_set )
1104 return( PSA_ERROR_BAD_STATE );
1105
1106 /* Fill the output buffer with something that isn't a valid mac
1107 * (barring an attack on the mac and deliberately-crafted input),
1108 * in case the caller doesn't check the return status properly. */
1109 *mac_length = operation->mac_size;
1110 memset( mac, '!', mac_size );
1111
1112 if( mac_size < operation->mac_size )
1113 return( PSA_ERROR_BUFFER_TOO_SMALL );
1114
1115 switch( operation->alg )
1116 {
1117#if defined(MBEDTLS_CMAC_C)
1118 case PSA_ALG_CMAC:
1119 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1120 break;
1121#endif /* MBEDTLS_CMAC_C */
1122 default:
1123#if defined(MBEDTLS_MD_C)
1124 if( PSA_ALG_IS_HMAC( operation->alg ) )
1125 {
1126 ret = mbedtls_md_hmac_finish( &operation->ctx.hmac, mac );
1127 }
1128 else
1129#endif /* MBEDTLS_MD_C */
1130 {
1131 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1132 }
1133 break;
1134 }
1135
1136 if( ret == 0 )
1137 {
1138 return( psa_mac_abort( operation ) );
1139 }
1140 else
1141 {
1142 psa_mac_abort( operation );
1143 return( mbedtls_to_psa_error( ret ) );
1144 }
1145}
1146
mohammad16036df908f2018-04-02 08:34:15 -07001147psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1148 uint8_t *mac,
1149 size_t mac_size,
1150 size_t *mac_length )
1151{
1152 if( !( operation->key_usage_sign ) )
1153 return( PSA_ERROR_NOT_PERMITTED );
1154
1155 return( psa_mac_finish_internal(operation, mac, mac_size, mac_length ) );
1156}
1157
Gilles Peskine8c9def32018-02-08 10:02:12 +01001158#define MBEDTLS_PSA_MAC_MAX_SIZE \
1159 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1160 MBEDTLS_MD_MAX_SIZE : \
1161 MBEDTLS_MAX_BLOCK_LENGTH )
1162psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1163 const uint8_t *mac,
1164 size_t mac_length )
1165{
1166 uint8_t actual_mac[MBEDTLS_PSA_MAC_MAX_SIZE];
1167 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001168 psa_status_t status;
1169
1170 if( !( operation->key_usage_verify ) )
1171 return( PSA_ERROR_NOT_PERMITTED );
1172
1173 status = psa_mac_finish_internal( operation,
1174 actual_mac, sizeof( actual_mac ),
1175 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001176 if( status != PSA_SUCCESS )
1177 return( status );
1178 if( actual_mac_length != mac_length )
1179 return( PSA_ERROR_INVALID_SIGNATURE );
1180 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1181 return( PSA_ERROR_INVALID_SIGNATURE );
1182 return( PSA_SUCCESS );
1183}
1184
1185
Gilles Peskine20035e32018-02-03 22:44:14 +01001186
1187
1188/****************************************************************/
1189/* Asymmetric cryptography */
1190/****************************************************************/
1191
1192psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1193 psa_algorithm_t alg,
1194 const uint8_t *hash,
1195 size_t hash_length,
1196 const uint8_t *salt,
1197 size_t salt_length,
1198 uint8_t *signature,
1199 size_t signature_size,
1200 size_t *signature_length)
1201{
1202 key_slot_t *slot;
1203
Gilles Peskine93aa0332018-02-03 23:58:03 +01001204 *signature_length = 0;
1205 (void) salt;
1206 (void) salt_length;
1207
Gilles Peskine20035e32018-02-03 22:44:14 +01001208 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1209 return( PSA_ERROR_EMPTY_SLOT );
1210 slot = &global_data.key_slots[key];
1211 if( slot->type == PSA_KEY_TYPE_NONE )
1212 return( PSA_ERROR_EMPTY_SLOT );
1213 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1214 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001215 if( !( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
1216 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001217
Gilles Peskine20035e32018-02-03 22:44:14 +01001218#if defined(MBEDTLS_RSA_C)
1219 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1220 {
1221 mbedtls_rsa_context *rsa = slot->data.rsa;
1222 int ret;
1223 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001224 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001225 mbedtls_md_type_t md_alg =
1226 hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1227 if( md_alg == MBEDTLS_MD_NONE )
1228 {
1229#if SIZE_MAX > UINT_MAX
1230 if( hash_length > UINT_MAX )
1231 return( PSA_ERROR_INVALID_ARGUMENT );
1232#endif
1233 }
1234 else
1235 {
1236 if( mbedtls_md_get_size( md_info ) != hash_length )
1237 return( PSA_ERROR_INVALID_ARGUMENT );
1238 if( md_info == NULL )
1239 return( PSA_ERROR_NOT_SUPPORTED );
1240 }
1241 if( signature_size < rsa->len )
1242 return( PSA_ERROR_BUFFER_TOO_SMALL );
1243#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001244 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001245 {
1246 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1247 MBEDTLS_MD_NONE );
1248 ret = mbedtls_rsa_pkcs1_sign( rsa,
1249 mbedtls_ctr_drbg_random,
1250 &global_data.ctr_drbg,
1251 MBEDTLS_RSA_PRIVATE,
1252 md_alg, hash_length, hash,
1253 signature );
1254 }
1255 else
1256#endif /* MBEDTLS_PKCS1_V15 */
1257#if defined(MBEDTLS_PKCS1_V21)
1258 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1259 {
1260 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1261 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1262 mbedtls_ctr_drbg_random,
1263 &global_data.ctr_drbg,
1264 MBEDTLS_RSA_PRIVATE,
1265 md_alg, hash_length, hash,
1266 signature );
1267 }
1268 else
1269#endif /* MBEDTLS_PKCS1_V21 */
1270 {
1271 return( PSA_ERROR_INVALID_ARGUMENT );
1272 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001273 if( ret == 0 )
1274 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001275 return( mbedtls_to_psa_error( ret ) );
1276 }
1277 else
1278#endif /* defined(MBEDTLS_RSA_C) */
1279#if defined(MBEDTLS_ECP_C)
1280 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1281 {
1282 // TODO
1283 return( PSA_ERROR_NOT_SUPPORTED );
1284 }
1285 else
1286#endif /* defined(MBEDTLS_ECP_C) */
1287 {
1288 return( PSA_ERROR_NOT_SUPPORTED );
1289 }
1290}
1291
1292
mohammad1603503973b2018-03-12 15:59:30 +02001293/****************************************************************/
1294/* Symmetric cryptography */
1295/****************************************************************/
1296
1297psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
1298 psa_key_slot_t key,
mohammad160382759612018-03-12 18:16:40 +02001299 psa_algorithm_t alg)
mohammad1603503973b2018-03-12 15:59:30 +02001300{
1301 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1302 psa_status_t status;
1303 key_slot_t *slot;
1304 psa_key_type_t key_type;
1305 size_t key_bits;
1306 const mbedtls_cipher_info_t *cipher_info = NULL;
1307
1308 operation->alg = 0;
1309 operation->key_set = 0;
1310 operation->iv_set = 0;
1311 operation->block_size = 0;
1312 operation->iv_size = 0;
1313
1314 status = psa_get_key_information( key, &key_type, &key_bits );
1315 if( status != PSA_SUCCESS )
1316 return( status );
1317 slot = &global_data.key_slots[key];
1318
1319 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits );
1320 if( cipher_info == NULL )
1321 return( PSA_ERROR_NOT_SUPPORTED );
1322
1323 operation->block_size = cipher_info->block_size;
1324
1325 mbedtls_cipher_init( &operation->ctx.cipher );
1326 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
1327 if (ret != 0)
1328 {
1329 psa_cipher_abort( operation );
1330 return( mbedtls_to_psa_error( ret ) );
1331 }
1332
1333 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data,
1334 key_bits, MBEDTLS_DECRYPT );
1335 if (ret != 0)
1336 {
1337 psa_cipher_abort( operation );
1338 return( mbedtls_to_psa_error( ret ) );
1339 }
1340
1341 operation->key_set = 1;
1342 operation->alg = alg;
1343
1344 return ( PSA_SUCCESS );
1345}
1346
mohammad160382759612018-03-12 18:16:40 +02001347psa_status_t psa_encrypt_generate_iv(unsigned char *iv,
mohammad1603503973b2018-03-12 15:59:30 +02001348 size_t iv_size,
1349 size_t *iv_length)
1350{
1351 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
mohammad160382759612018-03-12 18:16:40 +02001352
mohammad1603503973b2018-03-12 15:59:30 +02001353 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, iv_size);
1354 if (ret != 0)
1355 {
1356 return( mbedtls_to_psa_error( ret ) );
1357 }
1358
1359 *iv_length = iv_size;
mohammad160382759612018-03-12 18:16:40 +02001360 return ( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001361}
1362
1363psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
1364 const unsigned char *iv,
1365 size_t iv_length)
1366{
1367 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1368
1369 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
1370 if (ret != 0)
1371 {
1372 psa_cipher_abort( operation );
1373 return( mbedtls_to_psa_error( ret ) );
1374 }
1375
1376 operation->iv_set = 1;
1377 operation->iv_size = iv_length;
1378
1379 return ( PSA_SUCCESS );
1380}
1381
1382psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1383 const uint8_t *input,
1384 size_t input_length,
1385 unsigned char *output,
1386 size_t output_size,
1387 size_t *output_length)
1388{
1389 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1390
mohammad160382759612018-03-12 18:16:40 +02001391 if ( output_size < input_length )
1392 return ( PSA_ERROR_BUFFER_TOO_SMALL );
1393
mohammad1603503973b2018-03-12 15:59:30 +02001394 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
1395 input_length, output, output_length );
1396 if (ret != 0)
1397 {
1398 psa_cipher_abort( operation );
1399 return( mbedtls_to_psa_error( ret ) );
1400 }
1401
1402 return ( PSA_SUCCESS );
1403}
1404
1405psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
1406 uint8_t *output,
1407 size_t output_size,
1408 size_t *output_length)
1409{
1410 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
mohammad160382759612018-03-12 18:16:40 +02001411
1412 if ( output_size < operation->block_size )
1413 return ( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad1603503973b2018-03-12 15:59:30 +02001414
1415 if( ! operation->key_set )
1416 return( PSA_ERROR_BAD_STATE );
1417 if( ! operation->iv_set )
1418 return( PSA_ERROR_BAD_STATE );
1419
1420 ret = mbedtls_cipher_finish( &operation->ctx.cipher, output,
1421 output_length );
1422 if (ret != 0)
1423 {
1424 psa_cipher_abort( operation );
1425 return( mbedtls_to_psa_error( ret ) );
1426 }
1427
1428 return ( PSA_SUCCESS );
1429}
1430
1431psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
1432{
1433 mbedtls_cipher_free( &operation->ctx.cipher );
1434
1435 operation->alg = 0;
1436 operation->key_set = 0;
1437 operation->iv_set = 0;
1438 operation->block_size = 0;
1439 operation->iv_size = 0;
1440
1441 return ( PSA_SUCCESS );
1442}
1443
Gilles Peskinea0655c32018-04-30 17:06:50 +02001444
mohammad16038cc1cee2018-03-28 01:21:33 +03001445/****************************************************************/
1446/* Key Policy */
1447/****************************************************************/
1448
1449void psa_key_policy_init(psa_key_policy_t *policy)
1450{
mohammad16036df908f2018-04-02 08:34:15 -07001451 memset( policy, 0, sizeof( psa_key_policy_t ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03001452}
1453
1454void psa_key_policy_set_usage(psa_key_policy_t *policy,
1455 psa_key_usage_t usage,
1456 psa_algorithm_t alg)
1457{
mohammad16034eed7572018-03-28 05:14:59 -07001458 policy->usage = usage;
1459 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03001460}
1461
1462psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy)
1463{
mohammad16036df908f2018-04-02 08:34:15 -07001464 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03001465}
1466
1467psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy)
1468{
mohammad16036df908f2018-04-02 08:34:15 -07001469 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03001470}
1471
1472psa_status_t psa_set_key_policy(psa_key_slot_t key,
1473 const psa_key_policy_t *policy)
1474{
1475 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03001476
1477 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
1478 return( PSA_ERROR_INVALID_ARGUMENT );
1479
1480 slot = &global_data.key_slots[key];
1481 if( slot->type != PSA_KEY_TYPE_NONE )
1482 return( PSA_ERROR_OCCUPIED_SLOT );
1483
mohammad16036df908f2018-04-02 08:34:15 -07001484 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_ENCRYPT
1485 | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN
1486 | PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07001487 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03001488
mohammad16036df908f2018-04-02 08:34:15 -07001489 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03001490
1491 return( PSA_SUCCESS );
1492}
1493
1494psa_status_t psa_get_key_policy(psa_key_slot_t key,
1495 psa_key_policy_t *policy)
1496{
1497 key_slot_t *slot;
1498
1499 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
1500 return( PSA_ERROR_INVALID_ARGUMENT );
1501
1502 slot = &global_data.key_slots[key];
mohammad16038cc1cee2018-03-28 01:21:33 +03001503
mohammad16036df908f2018-04-02 08:34:15 -07001504 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03001505
1506 return( PSA_SUCCESS );
1507}
Gilles Peskine20035e32018-02-03 22:44:14 +01001508
Gilles Peskinea0655c32018-04-30 17:06:50 +02001509
1510
mohammad1603804cd712018-03-20 22:44:08 +02001511/****************************************************************/
1512/* Key Lifetime */
1513/****************************************************************/
1514
1515psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1516 psa_key_lifetime_t *lifetime)
1517{
1518 key_slot_t *slot;
1519
1520 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1521 return( PSA_ERROR_INVALID_ARGUMENT );
1522
1523 slot = &global_data.key_slots[key];
mohammad1603804cd712018-03-20 22:44:08 +02001524
1525 *lifetime = slot->lifetime;
1526
1527 return( PSA_SUCCESS );
1528}
1529
1530psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
1531 const psa_key_lifetime_t lifetime)
1532{
1533 key_slot_t *slot;
1534
1535 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1536 return( PSA_ERROR_INVALID_ARGUMENT );
1537
mohammad1603ba178512018-03-21 04:35:20 -07001538 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
1539 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
1540 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
1541 return( PSA_ERROR_INVALID_ARGUMENT );
1542
mohammad1603804cd712018-03-20 22:44:08 +02001543 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03001544 if( slot->type != PSA_KEY_TYPE_NONE )
1545 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02001546
mohammad1603ba178512018-03-21 04:35:20 -07001547 if ( lifetime != PSA_KEY_LIFETIME_VOLATILE )
1548 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603804cd712018-03-20 22:44:08 +02001549
mohammad1603060ad8a2018-03-20 14:28:38 -07001550 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02001551
1552 return( PSA_SUCCESS );
1553}
1554
Gilles Peskine20035e32018-02-03 22:44:14 +01001555
Gilles Peskinea0655c32018-04-30 17:06:50 +02001556
Gilles Peskine20035e32018-02-03 22:44:14 +01001557/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001558/* Module setup */
1559/****************************************************************/
1560
Gilles Peskinee59236f2018-01-27 23:32:46 +01001561void mbedtls_psa_crypto_free( void )
1562{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001563 size_t key;
1564 for( key = 1; key < MBEDTLS_PSA_KEY_SLOT_COUNT; key++ )
1565 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001566 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
1567 mbedtls_entropy_free( &global_data.entropy );
1568 mbedtls_zeroize( &global_data, sizeof( global_data ) );
1569}
1570
1571psa_status_t psa_crypto_init( void )
1572{
1573 int ret;
1574 const unsigned char drbg_seed[] = "PSA";
1575
1576 if( global_data.initialized != 0 )
1577 return( PSA_SUCCESS );
1578
1579 mbedtls_zeroize( &global_data, sizeof( global_data ) );
1580 mbedtls_entropy_init( &global_data.entropy );
1581 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
1582
1583 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
1584 mbedtls_entropy_func,
1585 &global_data.entropy,
1586 drbg_seed, sizeof( drbg_seed ) - 1 );
1587 if( ret != 0 )
1588 goto exit;
1589
Gilles Peskinee4ebc122018-03-07 14:16:44 +01001590 global_data.initialized = 1;
1591
Gilles Peskinee59236f2018-01-27 23:32:46 +01001592exit:
1593 if( ret != 0 )
1594 mbedtls_psa_crypto_free( );
1595 return( mbedtls_to_psa_error( ret ) );
1596}
1597
1598#endif /* MBEDTLS_PSA_CRYPTO_C */