blob: d170505c3f1433d53dcfecffcfea3a63b4dc6e8b [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;
888 case PSA_ALG_CBC_BASE:
889 mode = MBEDTLS_MODE_CBC;
890 break;
891 case PSA_ALG_CFB_BASE:
892 mode = MBEDTLS_MODE_CFB;
893 break;
894 case PSA_ALG_OFB_BASE:
895 mode = MBEDTLS_MODE_OFB;
896 break;
897 case PSA_ALG_CTR:
898 mode = MBEDTLS_MODE_CTR;
899 break;
900 case PSA_ALG_CCM:
901 mode = MBEDTLS_MODE_CCM;
902 break;
903 case PSA_ALG_GCM:
904 mode = MBEDTLS_MODE_GCM;
905 break;
906 default:
907 return( NULL );
908 }
909 }
910 else if( alg == PSA_ALG_CMAC )
911 mode = MBEDTLS_MODE_ECB;
912 else if( alg == PSA_ALG_GMAC )
913 mode = MBEDTLS_MODE_GCM;
914 else
915 return( NULL );
916
917 switch( key_type )
918 {
919 case PSA_KEY_TYPE_AES:
920 cipher_id = MBEDTLS_CIPHER_ID_AES;
921 break;
922 case PSA_KEY_TYPE_DES:
923 if( key_bits == 64 )
924 cipher_id = MBEDTLS_CIPHER_ID_DES;
925 else
926 cipher_id = MBEDTLS_CIPHER_ID_3DES;
927 break;
928 case PSA_KEY_TYPE_CAMELLIA:
929 cipher_id = MBEDTLS_CIPHER_ID_CAMELLIA;
930 break;
931 case PSA_KEY_TYPE_ARC4:
932 cipher_id = MBEDTLS_CIPHER_ID_ARC4;
933 break;
934 default:
935 return( NULL );
936 }
937
938 return( mbedtls_cipher_info_from_values( cipher_id, key_bits, mode ) );
939}
940
941psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
942{
943 switch( operation->alg )
944 {
945#if defined(MBEDTLS_CMAC_C)
946 case PSA_ALG_CMAC:
947 mbedtls_cipher_free( &operation->ctx.cmac );
948 break;
949#endif /* MBEDTLS_CMAC_C */
950 default:
951#if defined(MBEDTLS_MD_C)
952 if( PSA_ALG_IS_HMAC( operation->alg ) )
953 mbedtls_md_free( &operation->ctx.hmac );
954 else
955#endif /* MBEDTLS_MD_C */
956 return( PSA_ERROR_NOT_SUPPORTED );
957 }
958 operation->alg = 0;
959 operation->key_set = 0;
960 operation->iv_set = 0;
961 operation->iv_required = 0;
962 operation->has_input = 0;
963 return( PSA_SUCCESS );
964}
965
966psa_status_t psa_mac_start( psa_mac_operation_t *operation,
967 psa_key_slot_t key,
968 psa_algorithm_t alg )
969{
970 int ret = MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
971 psa_status_t status;
972 key_slot_t *slot;
973 psa_key_type_t key_type;
974 size_t key_bits;
975 const mbedtls_cipher_info_t *cipher_info = NULL;
976
977 operation->alg = 0;
978 operation->key_set = 0;
979 operation->iv_set = 0;
980 operation->iv_required = 1;
981 operation->has_input = 0;
982
983 status = psa_get_key_information( key, &key_type, &key_bits );
984 if( status != PSA_SUCCESS )
985 return( status );
986 slot = &global_data.key_slots[key];
987
mohammad16036df908f2018-04-02 08:34:15 -0700988 if ( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
989 operation->key_usage_sign = 1;
990
991 if ( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
992 operation->key_usage_verify = 1;
993
Gilles Peskine8c9def32018-02-08 10:02:12 +0100994 if( ! PSA_ALG_IS_HMAC( alg ) )
995 {
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100996 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100997 if( cipher_info == NULL )
998 return( PSA_ERROR_NOT_SUPPORTED );
999 operation->mac_size = cipher_info->block_size;
1000 }
1001 switch( alg )
1002 {
1003#if defined(MBEDTLS_CMAC_C)
1004 case PSA_ALG_CMAC:
1005 operation->iv_required = 0;
1006 mbedtls_cipher_init( &operation->ctx.cmac );
1007 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1008 if( ret != 0 )
1009 break;
1010 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1011 slot->data.raw.data,
1012 key_bits );
1013 break;
1014#endif /* MBEDTLS_CMAC_C */
1015 default:
1016#if defined(MBEDTLS_MD_C)
1017 if( PSA_ALG_IS_HMAC( alg ) )
1018 {
1019 const mbedtls_md_info_t *md_info =
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001020 mbedtls_md_info_from_psa( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001021 if( md_info == NULL )
1022 return( PSA_ERROR_NOT_SUPPORTED );
1023 if( key_type != PSA_KEY_TYPE_HMAC )
1024 return( PSA_ERROR_INVALID_ARGUMENT );
1025 operation->iv_required = 0;
1026 operation->mac_size = mbedtls_md_get_size( md_info );
1027 mbedtls_md_init( &operation->ctx.hmac );
1028 ret = mbedtls_md_setup( &operation->ctx.hmac, md_info, 1 );
1029 if( ret != 0 )
1030 break;
1031 ret = mbedtls_md_hmac_starts( &operation->ctx.hmac,
1032 slot->data.raw.data,
1033 slot->data.raw.bytes );
1034 break;
1035 }
1036 else
1037#endif /* MBEDTLS_MD_C */
1038 return( PSA_ERROR_NOT_SUPPORTED );
1039 }
1040
1041 /* If we reach this point, then the algorithm-specific part of the
1042 * context has at least been initialized, and may contain data that
1043 * needs to be wiped on error. */
1044 operation->alg = alg;
1045 if( ret != 0 )
1046 {
1047 psa_mac_abort( operation );
1048 return( mbedtls_to_psa_error( ret ) );
1049 }
1050 operation->key_set = 1;
Gilles Peskine47c1bc02018-03-20 17:55:04 +01001051 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001052}
1053
1054psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1055 const uint8_t *input,
1056 size_t input_length )
1057{
1058 int ret;
1059 if( ! operation->key_set )
1060 return( PSA_ERROR_BAD_STATE );
1061 if( operation->iv_required && ! operation->iv_set )
1062 return( PSA_ERROR_BAD_STATE );
1063 operation->has_input = 1;
1064
1065 switch( operation->alg )
1066 {
1067#if defined(MBEDTLS_CMAC_C)
1068 case PSA_ALG_CMAC:
1069 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1070 input, input_length );
1071 break;
1072#endif /* MBEDTLS_CMAC_C */
1073 default:
1074#if defined(MBEDTLS_MD_C)
1075 if( PSA_ALG_IS_HMAC( operation->alg ) )
1076 {
1077 ret = mbedtls_md_hmac_update( &operation->ctx.hmac,
1078 input, input_length );
1079 }
1080 else
1081#endif /* MBEDTLS_MD_C */
1082 {
1083 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1084 }
1085 break;
1086 }
1087 if( ret != 0 )
1088 psa_mac_abort( operation );
1089 return( mbedtls_to_psa_error( ret ) );
1090}
1091
mohammad16036df908f2018-04-02 08:34:15 -07001092static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01001093 uint8_t *mac,
1094 size_t mac_size,
1095 size_t *mac_length )
1096{
1097 int ret;
1098 if( ! operation->key_set )
1099 return( PSA_ERROR_BAD_STATE );
1100 if( operation->iv_required && ! operation->iv_set )
1101 return( PSA_ERROR_BAD_STATE );
1102
1103 /* Fill the output buffer with something that isn't a valid mac
1104 * (barring an attack on the mac and deliberately-crafted input),
1105 * in case the caller doesn't check the return status properly. */
1106 *mac_length = operation->mac_size;
1107 memset( mac, '!', mac_size );
1108
1109 if( mac_size < operation->mac_size )
1110 return( PSA_ERROR_BUFFER_TOO_SMALL );
1111
1112 switch( operation->alg )
1113 {
1114#if defined(MBEDTLS_CMAC_C)
1115 case PSA_ALG_CMAC:
1116 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1117 break;
1118#endif /* MBEDTLS_CMAC_C */
1119 default:
1120#if defined(MBEDTLS_MD_C)
1121 if( PSA_ALG_IS_HMAC( operation->alg ) )
1122 {
1123 ret = mbedtls_md_hmac_finish( &operation->ctx.hmac, mac );
1124 }
1125 else
1126#endif /* MBEDTLS_MD_C */
1127 {
1128 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1129 }
1130 break;
1131 }
1132
1133 if( ret == 0 )
1134 {
1135 return( psa_mac_abort( operation ) );
1136 }
1137 else
1138 {
1139 psa_mac_abort( operation );
1140 return( mbedtls_to_psa_error( ret ) );
1141 }
1142}
1143
mohammad16036df908f2018-04-02 08:34:15 -07001144psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1145 uint8_t *mac,
1146 size_t mac_size,
1147 size_t *mac_length )
1148{
1149 if( !( operation->key_usage_sign ) )
1150 return( PSA_ERROR_NOT_PERMITTED );
1151
1152 return( psa_mac_finish_internal(operation, mac, mac_size, mac_length ) );
1153}
1154
Gilles Peskine8c9def32018-02-08 10:02:12 +01001155#define MBEDTLS_PSA_MAC_MAX_SIZE \
1156 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1157 MBEDTLS_MD_MAX_SIZE : \
1158 MBEDTLS_MAX_BLOCK_LENGTH )
1159psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1160 const uint8_t *mac,
1161 size_t mac_length )
1162{
1163 uint8_t actual_mac[MBEDTLS_PSA_MAC_MAX_SIZE];
1164 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001165 psa_status_t status;
1166
1167 if( !( operation->key_usage_verify ) )
1168 return( PSA_ERROR_NOT_PERMITTED );
1169
1170 status = psa_mac_finish_internal( operation,
1171 actual_mac, sizeof( actual_mac ),
1172 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001173 if( status != PSA_SUCCESS )
1174 return( status );
1175 if( actual_mac_length != mac_length )
1176 return( PSA_ERROR_INVALID_SIGNATURE );
1177 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1178 return( PSA_ERROR_INVALID_SIGNATURE );
1179 return( PSA_SUCCESS );
1180}
1181
1182
Gilles Peskine20035e32018-02-03 22:44:14 +01001183
1184
1185/****************************************************************/
1186/* Asymmetric cryptography */
1187/****************************************************************/
1188
1189psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1190 psa_algorithm_t alg,
1191 const uint8_t *hash,
1192 size_t hash_length,
1193 const uint8_t *salt,
1194 size_t salt_length,
1195 uint8_t *signature,
1196 size_t signature_size,
1197 size_t *signature_length)
1198{
1199 key_slot_t *slot;
1200
Gilles Peskine93aa0332018-02-03 23:58:03 +01001201 *signature_length = 0;
1202 (void) salt;
1203 (void) salt_length;
1204
Gilles Peskine20035e32018-02-03 22:44:14 +01001205 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1206 return( PSA_ERROR_EMPTY_SLOT );
1207 slot = &global_data.key_slots[key];
1208 if( slot->type == PSA_KEY_TYPE_NONE )
1209 return( PSA_ERROR_EMPTY_SLOT );
1210 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1211 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001212 if( !( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
1213 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001214
Gilles Peskine20035e32018-02-03 22:44:14 +01001215#if defined(MBEDTLS_RSA_C)
1216 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1217 {
1218 mbedtls_rsa_context *rsa = slot->data.rsa;
1219 int ret;
1220 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001221 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001222 mbedtls_md_type_t md_alg =
1223 hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1224 if( md_alg == MBEDTLS_MD_NONE )
1225 {
1226#if SIZE_MAX > UINT_MAX
1227 if( hash_length > UINT_MAX )
1228 return( PSA_ERROR_INVALID_ARGUMENT );
1229#endif
1230 }
1231 else
1232 {
1233 if( mbedtls_md_get_size( md_info ) != hash_length )
1234 return( PSA_ERROR_INVALID_ARGUMENT );
1235 if( md_info == NULL )
1236 return( PSA_ERROR_NOT_SUPPORTED );
1237 }
1238 if( signature_size < rsa->len )
1239 return( PSA_ERROR_BUFFER_TOO_SMALL );
1240#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001241 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001242 {
1243 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1244 MBEDTLS_MD_NONE );
1245 ret = mbedtls_rsa_pkcs1_sign( rsa,
1246 mbedtls_ctr_drbg_random,
1247 &global_data.ctr_drbg,
1248 MBEDTLS_RSA_PRIVATE,
1249 md_alg, hash_length, hash,
1250 signature );
1251 }
1252 else
1253#endif /* MBEDTLS_PKCS1_V15 */
1254#if defined(MBEDTLS_PKCS1_V21)
1255 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1256 {
1257 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1258 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1259 mbedtls_ctr_drbg_random,
1260 &global_data.ctr_drbg,
1261 MBEDTLS_RSA_PRIVATE,
1262 md_alg, hash_length, hash,
1263 signature );
1264 }
1265 else
1266#endif /* MBEDTLS_PKCS1_V21 */
1267 {
1268 return( PSA_ERROR_INVALID_ARGUMENT );
1269 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001270 if( ret == 0 )
1271 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001272 return( mbedtls_to_psa_error( ret ) );
1273 }
1274 else
1275#endif /* defined(MBEDTLS_RSA_C) */
1276#if defined(MBEDTLS_ECP_C)
1277 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1278 {
1279 // TODO
1280 return( PSA_ERROR_NOT_SUPPORTED );
1281 }
1282 else
1283#endif /* defined(MBEDTLS_ECP_C) */
1284 {
1285 return( PSA_ERROR_NOT_SUPPORTED );
1286 }
1287}
1288
1289
mohammad1603503973b2018-03-12 15:59:30 +02001290/****************************************************************/
1291/* Symmetric cryptography */
1292/****************************************************************/
1293
1294psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
1295 psa_key_slot_t key,
mohammad160382759612018-03-12 18:16:40 +02001296 psa_algorithm_t alg)
mohammad1603503973b2018-03-12 15:59:30 +02001297{
1298 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1299 psa_status_t status;
1300 key_slot_t *slot;
1301 psa_key_type_t key_type;
1302 size_t key_bits;
1303 const mbedtls_cipher_info_t *cipher_info = NULL;
1304
1305 operation->alg = 0;
1306 operation->key_set = 0;
1307 operation->iv_set = 0;
1308 operation->block_size = 0;
1309 operation->iv_size = 0;
1310
1311 status = psa_get_key_information( key, &key_type, &key_bits );
1312 if( status != PSA_SUCCESS )
1313 return( status );
1314 slot = &global_data.key_slots[key];
1315
1316 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits );
1317 if( cipher_info == NULL )
1318 return( PSA_ERROR_NOT_SUPPORTED );
1319
1320 operation->block_size = cipher_info->block_size;
1321
1322 mbedtls_cipher_init( &operation->ctx.cipher );
1323 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
1324 if (ret != 0)
1325 {
1326 psa_cipher_abort( operation );
1327 return( mbedtls_to_psa_error( ret ) );
1328 }
1329
1330 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data,
1331 key_bits, MBEDTLS_DECRYPT );
1332 if (ret != 0)
1333 {
1334 psa_cipher_abort( operation );
1335 return( mbedtls_to_psa_error( ret ) );
1336 }
1337
1338 operation->key_set = 1;
1339 operation->alg = alg;
1340
1341 return ( PSA_SUCCESS );
1342}
1343
mohammad160382759612018-03-12 18:16:40 +02001344psa_status_t psa_encrypt_generate_iv(unsigned char *iv,
mohammad1603503973b2018-03-12 15:59:30 +02001345 size_t iv_size,
1346 size_t *iv_length)
1347{
1348 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
mohammad160382759612018-03-12 18:16:40 +02001349
mohammad1603503973b2018-03-12 15:59:30 +02001350 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, iv_size);
1351 if (ret != 0)
1352 {
1353 return( mbedtls_to_psa_error( ret ) );
1354 }
1355
1356 *iv_length = iv_size;
mohammad160382759612018-03-12 18:16:40 +02001357 return ( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001358}
1359
1360psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
1361 const unsigned char *iv,
1362 size_t iv_length)
1363{
1364 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1365
1366 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
1367 if (ret != 0)
1368 {
1369 psa_cipher_abort( operation );
1370 return( mbedtls_to_psa_error( ret ) );
1371 }
1372
1373 operation->iv_set = 1;
1374 operation->iv_size = iv_length;
1375
1376 return ( PSA_SUCCESS );
1377}
1378
1379psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1380 const uint8_t *input,
1381 size_t input_length,
1382 unsigned char *output,
1383 size_t output_size,
1384 size_t *output_length)
1385{
1386 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1387
mohammad160382759612018-03-12 18:16:40 +02001388 if ( output_size < input_length )
1389 return ( PSA_ERROR_BUFFER_TOO_SMALL );
1390
mohammad1603503973b2018-03-12 15:59:30 +02001391 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
1392 input_length, output, output_length );
1393 if (ret != 0)
1394 {
1395 psa_cipher_abort( operation );
1396 return( mbedtls_to_psa_error( ret ) );
1397 }
1398
1399 return ( PSA_SUCCESS );
1400}
1401
1402psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
1403 uint8_t *output,
1404 size_t output_size,
1405 size_t *output_length)
1406{
1407 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
mohammad160382759612018-03-12 18:16:40 +02001408
1409 if ( output_size < operation->block_size )
1410 return ( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad1603503973b2018-03-12 15:59:30 +02001411
1412 if( ! operation->key_set )
1413 return( PSA_ERROR_BAD_STATE );
1414 if( ! operation->iv_set )
1415 return( PSA_ERROR_BAD_STATE );
1416
1417 ret = mbedtls_cipher_finish( &operation->ctx.cipher, output,
1418 output_length );
1419 if (ret != 0)
1420 {
1421 psa_cipher_abort( operation );
1422 return( mbedtls_to_psa_error( ret ) );
1423 }
1424
1425 return ( PSA_SUCCESS );
1426}
1427
1428psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
1429{
1430 mbedtls_cipher_free( &operation->ctx.cipher );
1431
1432 operation->alg = 0;
1433 operation->key_set = 0;
1434 operation->iv_set = 0;
1435 operation->block_size = 0;
1436 operation->iv_size = 0;
1437
1438 return ( PSA_SUCCESS );
1439}
1440
Gilles Peskinea0655c32018-04-30 17:06:50 +02001441
mohammad16038cc1cee2018-03-28 01:21:33 +03001442/****************************************************************/
1443/* Key Policy */
1444/****************************************************************/
1445
1446void psa_key_policy_init(psa_key_policy_t *policy)
1447{
mohammad16036df908f2018-04-02 08:34:15 -07001448 memset( policy, 0, sizeof( psa_key_policy_t ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03001449}
1450
1451void psa_key_policy_set_usage(psa_key_policy_t *policy,
1452 psa_key_usage_t usage,
1453 psa_algorithm_t alg)
1454{
mohammad16034eed7572018-03-28 05:14:59 -07001455 policy->usage = usage;
1456 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03001457}
1458
1459psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy)
1460{
mohammad16036df908f2018-04-02 08:34:15 -07001461 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03001462}
1463
1464psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy)
1465{
mohammad16036df908f2018-04-02 08:34:15 -07001466 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03001467}
1468
1469psa_status_t psa_set_key_policy(psa_key_slot_t key,
1470 const psa_key_policy_t *policy)
1471{
1472 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03001473
1474 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
1475 return( PSA_ERROR_INVALID_ARGUMENT );
1476
1477 slot = &global_data.key_slots[key];
1478 if( slot->type != PSA_KEY_TYPE_NONE )
1479 return( PSA_ERROR_OCCUPIED_SLOT );
1480
mohammad16036df908f2018-04-02 08:34:15 -07001481 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_ENCRYPT
1482 | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN
1483 | PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07001484 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03001485
mohammad16036df908f2018-04-02 08:34:15 -07001486 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03001487
1488 return( PSA_SUCCESS );
1489}
1490
1491psa_status_t psa_get_key_policy(psa_key_slot_t key,
1492 psa_key_policy_t *policy)
1493{
1494 key_slot_t *slot;
1495
1496 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
1497 return( PSA_ERROR_INVALID_ARGUMENT );
1498
1499 slot = &global_data.key_slots[key];
mohammad16038cc1cee2018-03-28 01:21:33 +03001500
mohammad16036df908f2018-04-02 08:34:15 -07001501 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03001502
1503 return( PSA_SUCCESS );
1504}
Gilles Peskine20035e32018-02-03 22:44:14 +01001505
Gilles Peskinea0655c32018-04-30 17:06:50 +02001506
1507
mohammad1603804cd712018-03-20 22:44:08 +02001508/****************************************************************/
1509/* Key Lifetime */
1510/****************************************************************/
1511
1512psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1513 psa_key_lifetime_t *lifetime)
1514{
1515 key_slot_t *slot;
1516
1517 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1518 return( PSA_ERROR_INVALID_ARGUMENT );
1519
1520 slot = &global_data.key_slots[key];
mohammad1603804cd712018-03-20 22:44:08 +02001521
1522 *lifetime = slot->lifetime;
1523
1524 return( PSA_SUCCESS );
1525}
1526
1527psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
1528 const psa_key_lifetime_t lifetime)
1529{
1530 key_slot_t *slot;
1531
1532 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1533 return( PSA_ERROR_INVALID_ARGUMENT );
1534
mohammad1603ba178512018-03-21 04:35:20 -07001535 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
1536 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
1537 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
1538 return( PSA_ERROR_INVALID_ARGUMENT );
1539
mohammad1603804cd712018-03-20 22:44:08 +02001540 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03001541 if( slot->type != PSA_KEY_TYPE_NONE )
1542 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02001543
mohammad1603ba178512018-03-21 04:35:20 -07001544 if ( lifetime != PSA_KEY_LIFETIME_VOLATILE )
1545 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603804cd712018-03-20 22:44:08 +02001546
mohammad1603060ad8a2018-03-20 14:28:38 -07001547 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02001548
1549 return( PSA_SUCCESS );
1550}
1551
Gilles Peskine20035e32018-02-03 22:44:14 +01001552
Gilles Peskinea0655c32018-04-30 17:06:50 +02001553
Gilles Peskine20035e32018-02-03 22:44:14 +01001554/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001555/* Module setup */
1556/****************************************************************/
1557
Gilles Peskinee59236f2018-01-27 23:32:46 +01001558void mbedtls_psa_crypto_free( void )
1559{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001560 size_t key;
1561 for( key = 1; key < MBEDTLS_PSA_KEY_SLOT_COUNT; key++ )
1562 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001563 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
1564 mbedtls_entropy_free( &global_data.entropy );
1565 mbedtls_zeroize( &global_data, sizeof( global_data ) );
1566}
1567
1568psa_status_t psa_crypto_init( void )
1569{
1570 int ret;
1571 const unsigned char drbg_seed[] = "PSA";
1572
1573 if( global_data.initialized != 0 )
1574 return( PSA_SUCCESS );
1575
1576 mbedtls_zeroize( &global_data, sizeof( global_data ) );
1577 mbedtls_entropy_init( &global_data.entropy );
1578 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
1579
1580 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
1581 mbedtls_entropy_func,
1582 &global_data.entropy,
1583 drbg_seed, sizeof( drbg_seed ) - 1 );
1584 if( ret != 0 )
1585 goto exit;
1586
Gilles Peskinee4ebc122018-03-07 14:16:44 +01001587 global_data.initialized = 1;
1588
Gilles Peskinee59236f2018-01-27 23:32:46 +01001589exit:
1590 if( ret != 0 )
1591 mbedtls_psa_crypto_free( );
1592 return( mbedtls_to_psa_error( ret ) );
1593}
1594
1595#endif /* MBEDTLS_PSA_CRYPTO_C */