blob: 9a812b866060ff73b9ba05c3e87e4a494ef9d8e9 [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
mohammad16038481e742018-03-18 13:57:31 +0200289static void psa_operation_init(void *operation,
290 psa_algorithm_t alg)
291{
292 if( PSA_ALG_IS_MAC(alg) )
293 {
294 if ( ((psa_mac_operation_t*)operation)->alg != 0 ) //restart
295 {
296 ((psa_mac_operation_t*)operation)->alg = 0;
297 ((psa_mac_operation_t*)operation)->iv_required = 0;
298 }
299 else
300 {
301 ((psa_mac_operation_t*)operation)->alg = alg;
302 ((psa_mac_operation_t*)operation)->iv_required = 1;
303 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100304
mohammad16038481e742018-03-18 13:57:31 +0200305 ((psa_mac_operation_t*)operation)->key_set = 0;
306 ((psa_mac_operation_t*)operation)->iv_set = 0;
307 ((psa_mac_operation_t*)operation)->has_input = 0;
308 ((psa_mac_operation_t*)operation)->mac_size = 0;
309 }
310 else if( PSA_ALG_IS_CIPHER(alg) )
311 {
312 if ( ((psa_cipher_operation_t*)operation)->alg != 0 ) //restart
313 ((psa_cipher_operation_t*)operation)->alg = 0;
314 else
315 ((psa_cipher_operation_t*)operation)->alg = alg;
316
317 ((psa_cipher_operation_t*)operation)->key_set = 0;
318 ((psa_cipher_operation_t*)operation)->iv_set = 0;
319 ((psa_cipher_operation_t*)operation)->iv_size = 0;
320 ((psa_cipher_operation_t*)operation)->block_size = 0;
321 }
322}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100323
324/****************************************************************/
325/* Key management */
326/****************************************************************/
327
328psa_status_t psa_import_key(psa_key_slot_t key,
329 psa_key_type_t type,
330 const uint8_t *data,
331 size_t data_length)
332{
333 key_slot_t *slot;
334
335 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 slot = &global_data.key_slots[key];
338 if( slot->type != PSA_KEY_TYPE_NONE )
339 return( PSA_ERROR_OCCUPIED_SLOT );
340
Gilles Peskine8c9def32018-02-08 10:02:12 +0100341 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100342 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100343 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100344 if( data_length > SIZE_MAX / 8 )
345 return( PSA_ERROR_NOT_SUPPORTED );
346 slot->data.raw.data = mbedtls_calloc( 1, data_length );
347 if( slot->data.raw.data == NULL )
348 return( PSA_ERROR_INSUFFICIENT_MEMORY );
349 memcpy( slot->data.raw.data, data, data_length );
350 slot->data.raw.bytes = data_length;
351 }
352 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100353#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100354 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
355 type == PSA_KEY_TYPE_RSA_KEYPAIR ||
356 PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357 {
358 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100359 mbedtls_pk_context pk;
360 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100361 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
362 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
363 else
364 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100365 if( ret != 0 )
366 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100367 switch( mbedtls_pk_get_type( &pk ) )
368 {
369#if defined(MBEDTLS_RSA_C)
370 case MBEDTLS_PK_RSA:
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100371 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
372 type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine969ac722018-01-28 18:16:59 +0100373 slot->data.rsa = pk.pk_ctx;
374 else
375 return( PSA_ERROR_INVALID_ARGUMENT );
376 break;
377#endif /* MBEDTLS_RSA_C */
378#if defined(MBEDTLS_ECP_C)
379 case MBEDTLS_PK_ECKEY:
380 if( PSA_KEY_TYPE_IS_ECC( type ) )
381 {
382 // TODO: check curve
383 slot->data.ecp = pk.pk_ctx;
384 }
385 else
386 return( PSA_ERROR_INVALID_ARGUMENT );
387 break;
388#endif /* MBEDTLS_ECP_C */
389 default:
390 return( PSA_ERROR_INVALID_ARGUMENT );
391 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100392 }
393 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100394#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100395 {
396 return( PSA_ERROR_NOT_SUPPORTED );
397 }
398
399 slot->type = type;
400 return( PSA_SUCCESS );
401}
402
403psa_status_t psa_destroy_key(psa_key_slot_t key)
404{
405 key_slot_t *slot;
406
407 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
408 return( PSA_ERROR_INVALID_ARGUMENT );
409 slot = &global_data.key_slots[key];
410 if( slot->type == PSA_KEY_TYPE_NONE )
411 return( PSA_ERROR_EMPTY_SLOT );
412
Gilles Peskine8c9def32018-02-08 10:02:12 +0100413 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100414 {
415 mbedtls_free( slot->data.raw.data );
416 }
417 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100418#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100419 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
420 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100421 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100422 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100423 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100424 }
425 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100426#endif /* defined(MBEDTLS_RSA_C) */
427#if defined(MBEDTLS_ECP_C)
428 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
429 {
430 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100431 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100432 }
433 else
434#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100435 {
436 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100437 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100438 return( PSA_ERROR_TAMPERING_DETECTED );
439 }
440
441 mbedtls_zeroize( slot, sizeof( *slot ) );
442 return( PSA_SUCCESS );
443}
444
445psa_status_t psa_get_key_information(psa_key_slot_t key,
446 psa_key_type_t *type,
447 size_t *bits)
448{
449 key_slot_t *slot;
450
451 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100452 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100453 slot = &global_data.key_slots[key];
454 if( type != NULL )
455 *type = slot->type;
456 if( bits != NULL )
457 *bits = 0;
458 if( slot->type == PSA_KEY_TYPE_NONE )
459 return( PSA_ERROR_EMPTY_SLOT );
460
Gilles Peskine8c9def32018-02-08 10:02:12 +0100461 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100462 {
463 if( bits != NULL )
464 *bits = slot->data.raw.bytes * 8;
465 }
466 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100467#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100468 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
469 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100470 {
471 if( bits != NULL )
Gilles Peskine969ac722018-01-28 18:16:59 +0100472 *bits = mbedtls_rsa_get_bitlen( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100473 }
474 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100475#endif /* defined(MBEDTLS_RSA_C) */
476#if defined(MBEDTLS_ECP_C)
477 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
478 {
479 if( bits != NULL )
480 *bits = slot->data.ecp->grp.pbits;
481 }
482 else
483#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100484 {
485 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100486 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 return( PSA_ERROR_TAMPERING_DETECTED );
488 }
489
490 return( PSA_SUCCESS );
491}
492
493psa_status_t psa_export_key(psa_key_slot_t key,
494 uint8_t *data,
495 size_t data_size,
496 size_t *data_length)
497{
498 key_slot_t *slot;
499
500 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100501 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100502 slot = &global_data.key_slots[key];
503 if( slot->type == PSA_KEY_TYPE_NONE )
504 return( PSA_ERROR_EMPTY_SLOT );
505
mohammad160306e79202018-03-28 13:17:44 +0300506 if( !( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) )
507 return( PSA_ERROR_NOT_PERMITTED );
508
Gilles Peskine8c9def32018-02-08 10:02:12 +0100509 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510 {
511 if( slot->data.raw.bytes > data_size )
512 return( PSA_ERROR_BUFFER_TOO_SMALL );
513 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
514 *data_length = slot->data.raw.bytes;
515 return( PSA_SUCCESS );
516 }
517 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100518#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100519 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
520 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100521 PSA_KEY_TYPE_IS_ECC( slot->type ) )
522 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100523 mbedtls_pk_context pk;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100524 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100525 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100526 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
527 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine969ac722018-01-28 18:16:59 +0100528 {
529 pk.pk_info = &mbedtls_rsa_info;
530 pk.pk_ctx = slot->data.rsa;
531 }
532 else
533 {
534 pk.pk_info = &mbedtls_eckey_info;
535 pk.pk_ctx = slot->data.ecp;
536 }
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100537 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
538 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
539 else
540 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541 if( ret < 0 )
542 return( mbedtls_to_psa_error( ret ) );
543 *data_length = ret;
544 return( PSA_SUCCESS );
545 }
546 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100547#endif /* defined(MBEDTLS_PK_WRITE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100549 /* This shouldn't happen in the reference implementation, but
550 it is valid for a special-purpose implementation to omit
551 support for exporting certain key types. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 return( PSA_ERROR_NOT_SUPPORTED );
553 }
554}
555
556
557
558/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100559/* Message digests */
560/****************************************************************/
561
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100562static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100563{
564 switch( alg )
565 {
566#if defined(MBEDTLS_MD2_C)
567 case PSA_ALG_MD2:
568 return( &mbedtls_md2_info );
569#endif
570#if defined(MBEDTLS_MD4_C)
571 case PSA_ALG_MD4:
572 return( &mbedtls_md4_info );
573#endif
574#if defined(MBEDTLS_MD5_C)
575 case PSA_ALG_MD5:
576 return( &mbedtls_md5_info );
577#endif
578#if defined(MBEDTLS_RIPEMD160_C)
579 case PSA_ALG_RIPEMD160:
580 return( &mbedtls_ripemd160_info );
581#endif
582#if defined(MBEDTLS_SHA1_C)
583 case PSA_ALG_SHA_1:
584 return( &mbedtls_sha1_info );
585#endif
586#if defined(MBEDTLS_SHA256_C)
587 case PSA_ALG_SHA_224:
588 return( &mbedtls_sha224_info );
589 case PSA_ALG_SHA_256:
590 return( &mbedtls_sha256_info );
591#endif
592#if defined(MBEDTLS_SHA512_C)
593 case PSA_ALG_SHA_384:
594 return( &mbedtls_sha384_info );
595 case PSA_ALG_SHA_512:
596 return( &mbedtls_sha512_info );
597#endif
598 default:
599 return( NULL );
600 }
601}
602
603#if 0
604static psa_algorithm_t mbedtls_md_alg_to_psa( mbedtls_md_type_t md_alg )
605{
606 switch( md_alg )
607 {
608 case MBEDTLS_MD_NONE:
609 return( 0 );
610 case MBEDTLS_MD_MD2:
611 return( PSA_ALG_MD2 );
612 case MBEDTLS_MD_MD4:
613 return( PSA_ALG_MD4 );
614 case MBEDTLS_MD_MD5:
615 return( PSA_ALG_MD5 );
616 case MBEDTLS_MD_SHA1:
617 return( PSA_ALG_SHA_1 );
618 case MBEDTLS_MD_SHA224:
619 return( PSA_ALG_SHA_224 );
620 case MBEDTLS_MD_SHA256:
621 return( PSA_ALG_SHA_256 );
622 case MBEDTLS_MD_SHA384:
623 return( PSA_ALG_SHA_384 );
624 case MBEDTLS_MD_SHA512:
625 return( PSA_ALG_SHA_512 );
626 case MBEDTLS_MD_RIPEMD160:
627 return( PSA_ALG_RIPEMD160 );
628 default:
Gilles Peskine47c1bc02018-03-20 17:55:04 +0100629 return( 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +0100630 }
631}
632#endif
633
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100634psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
635{
636 switch( operation->alg )
637 {
638#if defined(MBEDTLS_MD2_C)
639 case PSA_ALG_MD2:
640 mbedtls_md2_free( &operation->ctx.md2 );
641 break;
642#endif
643#if defined(MBEDTLS_MD4_C)
644 case PSA_ALG_MD4:
645 mbedtls_md4_free( &operation->ctx.md4 );
646 break;
647#endif
648#if defined(MBEDTLS_MD5_C)
649 case PSA_ALG_MD5:
650 mbedtls_md5_free( &operation->ctx.md5 );
651 break;
652#endif
653#if defined(MBEDTLS_RIPEMD160_C)
654 case PSA_ALG_RIPEMD160:
655 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
656 break;
657#endif
658#if defined(MBEDTLS_SHA1_C)
659 case PSA_ALG_SHA_1:
660 mbedtls_sha1_free( &operation->ctx.sha1 );
661 break;
662#endif
663#if defined(MBEDTLS_SHA256_C)
664 case PSA_ALG_SHA_224:
665 case PSA_ALG_SHA_256:
666 mbedtls_sha256_free( &operation->ctx.sha256 );
667 break;
668#endif
669#if defined(MBEDTLS_SHA512_C)
670 case PSA_ALG_SHA_384:
671 case PSA_ALG_SHA_512:
672 mbedtls_sha512_free( &operation->ctx.sha512 );
673 break;
674#endif
675 default:
676 return( PSA_ERROR_NOT_SUPPORTED );
677 }
678 operation->alg = 0;
679 return( PSA_SUCCESS );
680}
681
682psa_status_t psa_hash_start( psa_hash_operation_t *operation,
683 psa_algorithm_t alg )
684{
685 int ret;
686 operation->alg = 0;
687 switch( alg )
688 {
689#if defined(MBEDTLS_MD2_C)
690 case PSA_ALG_MD2:
691 mbedtls_md2_init( &operation->ctx.md2 );
692 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
693 break;
694#endif
695#if defined(MBEDTLS_MD4_C)
696 case PSA_ALG_MD4:
697 mbedtls_md4_init( &operation->ctx.md4 );
698 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
699 break;
700#endif
701#if defined(MBEDTLS_MD5_C)
702 case PSA_ALG_MD5:
703 mbedtls_md5_init( &operation->ctx.md5 );
704 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
705 break;
706#endif
707#if defined(MBEDTLS_RIPEMD160_C)
708 case PSA_ALG_RIPEMD160:
709 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
710 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
711 break;
712#endif
713#if defined(MBEDTLS_SHA1_C)
714 case PSA_ALG_SHA_1:
715 mbedtls_sha1_init( &operation->ctx.sha1 );
716 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
717 break;
718#endif
719#if defined(MBEDTLS_SHA256_C)
720 case PSA_ALG_SHA_224:
721 mbedtls_sha256_init( &operation->ctx.sha256 );
722 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
723 break;
724 case PSA_ALG_SHA_256:
725 mbedtls_sha256_init( &operation->ctx.sha256 );
726 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
727 break;
728#endif
729#if defined(MBEDTLS_SHA512_C)
730 case PSA_ALG_SHA_384:
731 mbedtls_sha512_init( &operation->ctx.sha512 );
732 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
733 break;
734 case PSA_ALG_SHA_512:
735 mbedtls_sha512_init( &operation->ctx.sha512 );
736 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
737 break;
738#endif
739 default:
740 return( PSA_ERROR_NOT_SUPPORTED );
741 }
742 if( ret == 0 )
743 operation->alg = alg;
744 else
745 psa_hash_abort( operation );
746 return( mbedtls_to_psa_error( ret ) );
747}
748
749psa_status_t psa_hash_update( psa_hash_operation_t *operation,
750 const uint8_t *input,
751 size_t input_length )
752{
753 int ret;
754 switch( operation->alg )
755 {
756#if defined(MBEDTLS_MD2_C)
757 case PSA_ALG_MD2:
758 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
759 input, input_length );
760 break;
761#endif
762#if defined(MBEDTLS_MD4_C)
763 case PSA_ALG_MD4:
764 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
765 input, input_length );
766 break;
767#endif
768#if defined(MBEDTLS_MD5_C)
769 case PSA_ALG_MD5:
770 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
771 input, input_length );
772 break;
773#endif
774#if defined(MBEDTLS_RIPEMD160_C)
775 case PSA_ALG_RIPEMD160:
776 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
777 input, input_length );
778 break;
779#endif
780#if defined(MBEDTLS_SHA1_C)
781 case PSA_ALG_SHA_1:
782 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
783 input, input_length );
784 break;
785#endif
786#if defined(MBEDTLS_SHA256_C)
787 case PSA_ALG_SHA_224:
788 case PSA_ALG_SHA_256:
789 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
790 input, input_length );
791 break;
792#endif
793#if defined(MBEDTLS_SHA512_C)
794 case PSA_ALG_SHA_384:
795 case PSA_ALG_SHA_512:
796 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
797 input, input_length );
798 break;
799#endif
800 default:
801 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
802 break;
803 }
804 if( ret != 0 )
805 psa_hash_abort( operation );
806 return( mbedtls_to_psa_error( ret ) );
807}
808
809psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
810 uint8_t *hash,
811 size_t hash_size,
812 size_t *hash_length )
813{
814 int ret;
815 size_t actual_hash_length = PSA_HASH_FINAL_SIZE( operation->alg );
816
817 /* Fill the output buffer with something that isn't a valid hash
818 * (barring an attack on the hash and deliberately-crafted input),
819 * in case the caller doesn't check the return status properly. */
820 *hash_length = actual_hash_length;
821 memset( hash, '!', hash_size );
822
823 if( hash_size < actual_hash_length )
824 return( PSA_ERROR_BUFFER_TOO_SMALL );
825
826 switch( operation->alg )
827 {
828#if defined(MBEDTLS_MD2_C)
829 case PSA_ALG_MD2:
830 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
831 break;
832#endif
833#if defined(MBEDTLS_MD4_C)
834 case PSA_ALG_MD4:
835 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
836 break;
837#endif
838#if defined(MBEDTLS_MD5_C)
839 case PSA_ALG_MD5:
840 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
841 break;
842#endif
843#if defined(MBEDTLS_RIPEMD160_C)
844 case PSA_ALG_RIPEMD160:
845 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
846 break;
847#endif
848#if defined(MBEDTLS_SHA1_C)
849 case PSA_ALG_SHA_1:
850 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
851 break;
852#endif
853#if defined(MBEDTLS_SHA256_C)
854 case PSA_ALG_SHA_224:
855 case PSA_ALG_SHA_256:
856 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
857 break;
858#endif
859#if defined(MBEDTLS_SHA512_C)
860 case PSA_ALG_SHA_384:
861 case PSA_ALG_SHA_512:
862 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
863 break;
864#endif
865 default:
866 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
867 break;
868 }
869
870 if( ret == 0 )
871 {
872 return( psa_hash_abort( operation ) );
873 }
874 else
875 {
876 psa_hash_abort( operation );
877 return( mbedtls_to_psa_error( ret ) );
878 }
879}
880
881psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
882 const uint8_t *hash,
883 size_t hash_length)
884{
885 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
886 size_t actual_hash_length;
887 psa_status_t status = psa_hash_finish( operation,
888 actual_hash, sizeof( actual_hash ),
889 &actual_hash_length );
890 if( status != PSA_SUCCESS )
891 return( status );
892 if( actual_hash_length != hash_length )
893 return( PSA_ERROR_INVALID_SIGNATURE );
894 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
895 return( PSA_ERROR_INVALID_SIGNATURE );
896 return( PSA_SUCCESS );
897}
898
899
900
901
902/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +0100903/* MAC */
904/****************************************************************/
905
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100906static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +0100907 psa_algorithm_t alg,
908 psa_key_type_t key_type,
909 size_t key_bits )
910{
911 mbedtls_cipher_id_t cipher_id;
912 mbedtls_cipher_mode_t mode;
913
914 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
915 {
mohammad16038481e742018-03-18 13:57:31 +0200916 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
917 {
918 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
919 }
Gilles Peskine8c9def32018-02-08 10:02:12 +0100920 switch( alg )
921 {
922 case PSA_ALG_STREAM_CIPHER:
923 mode = MBEDTLS_MODE_STREAM;
924 break;
925 case PSA_ALG_CBC_BASE:
926 mode = MBEDTLS_MODE_CBC;
927 break;
928 case PSA_ALG_CFB_BASE:
929 mode = MBEDTLS_MODE_CFB;
930 break;
931 case PSA_ALG_OFB_BASE:
932 mode = MBEDTLS_MODE_OFB;
933 break;
934 case PSA_ALG_CTR:
935 mode = MBEDTLS_MODE_CTR;
936 break;
937 case PSA_ALG_CCM:
938 mode = MBEDTLS_MODE_CCM;
939 break;
940 case PSA_ALG_GCM:
941 mode = MBEDTLS_MODE_GCM;
942 break;
943 default:
944 return( NULL );
945 }
946 }
947 else if( alg == PSA_ALG_CMAC )
948 mode = MBEDTLS_MODE_ECB;
949 else if( alg == PSA_ALG_GMAC )
950 mode = MBEDTLS_MODE_GCM;
951 else
952 return( NULL );
953
954 switch( key_type )
955 {
956 case PSA_KEY_TYPE_AES:
957 cipher_id = MBEDTLS_CIPHER_ID_AES;
958 break;
959 case PSA_KEY_TYPE_DES:
960 if( key_bits == 64 )
961 cipher_id = MBEDTLS_CIPHER_ID_DES;
962 else
963 cipher_id = MBEDTLS_CIPHER_ID_3DES;
964 break;
965 case PSA_KEY_TYPE_CAMELLIA:
966 cipher_id = MBEDTLS_CIPHER_ID_CAMELLIA;
967 break;
968 case PSA_KEY_TYPE_ARC4:
969 cipher_id = MBEDTLS_CIPHER_ID_ARC4;
970 break;
971 default:
972 return( NULL );
973 }
974
975 return( mbedtls_cipher_info_from_values( cipher_id, key_bits, mode ) );
976}
977
978psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
979{
980 switch( operation->alg )
981 {
982#if defined(MBEDTLS_CMAC_C)
983 case PSA_ALG_CMAC:
984 mbedtls_cipher_free( &operation->ctx.cmac );
985 break;
986#endif /* MBEDTLS_CMAC_C */
987 default:
988#if defined(MBEDTLS_MD_C)
989 if( PSA_ALG_IS_HMAC( operation->alg ) )
990 mbedtls_md_free( &operation->ctx.hmac );
991 else
992#endif /* MBEDTLS_MD_C */
993 return( PSA_ERROR_NOT_SUPPORTED );
994 }
mohammad16038481e742018-03-18 13:57:31 +0200995 psa_operation_init(operation, 0);
Gilles Peskine8c9def32018-02-08 10:02:12 +0100996 return( PSA_SUCCESS );
997}
998
999psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1000 psa_key_slot_t key,
1001 psa_algorithm_t alg )
1002{
1003 int ret = MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
1004 psa_status_t status;
1005 key_slot_t *slot;
1006 psa_key_type_t key_type;
1007 size_t key_bits;
1008 const mbedtls_cipher_info_t *cipher_info = NULL;
1009
mohammad16038481e742018-03-18 13:57:31 +02001010 psa_operation_init(operation, alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001011
1012 status = psa_get_key_information( key, &key_type, &key_bits );
1013 if( status != PSA_SUCCESS )
1014 return( status );
1015 slot = &global_data.key_slots[key];
1016
mohammad16036df908f2018-04-02 08:34:15 -07001017 if ( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
1018 operation->key_usage_sign = 1;
1019
1020 if ( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1021 operation->key_usage_verify = 1;
1022
Gilles Peskine8c9def32018-02-08 10:02:12 +01001023 if( ! PSA_ALG_IS_HMAC( alg ) )
1024 {
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001025 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001026 if( cipher_info == NULL )
1027 return( PSA_ERROR_NOT_SUPPORTED );
1028 operation->mac_size = cipher_info->block_size;
1029 }
1030 switch( alg )
1031 {
1032#if defined(MBEDTLS_CMAC_C)
1033 case PSA_ALG_CMAC:
1034 operation->iv_required = 0;
1035 mbedtls_cipher_init( &operation->ctx.cmac );
1036 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1037 if( ret != 0 )
1038 break;
1039 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1040 slot->data.raw.data,
1041 key_bits );
1042 break;
1043#endif /* MBEDTLS_CMAC_C */
1044 default:
1045#if defined(MBEDTLS_MD_C)
1046 if( PSA_ALG_IS_HMAC( alg ) )
1047 {
1048 const mbedtls_md_info_t *md_info =
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001049 mbedtls_md_info_from_psa( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001050 if( md_info == NULL )
1051 return( PSA_ERROR_NOT_SUPPORTED );
1052 if( key_type != PSA_KEY_TYPE_HMAC )
1053 return( PSA_ERROR_INVALID_ARGUMENT );
1054 operation->iv_required = 0;
1055 operation->mac_size = mbedtls_md_get_size( md_info );
1056 mbedtls_md_init( &operation->ctx.hmac );
1057 ret = mbedtls_md_setup( &operation->ctx.hmac, md_info, 1 );
1058 if( ret != 0 )
1059 break;
1060 ret = mbedtls_md_hmac_starts( &operation->ctx.hmac,
1061 slot->data.raw.data,
1062 slot->data.raw.bytes );
1063 break;
1064 }
1065 else
1066#endif /* MBEDTLS_MD_C */
1067 return( PSA_ERROR_NOT_SUPPORTED );
1068 }
1069
1070 /* If we reach this point, then the algorithm-specific part of the
1071 * context has at least been initialized, and may contain data that
1072 * needs to be wiped on error. */
1073 operation->alg = alg;
1074 if( ret != 0 )
1075 {
1076 psa_mac_abort( operation );
1077 return( mbedtls_to_psa_error( ret ) );
1078 }
1079 operation->key_set = 1;
Gilles Peskine47c1bc02018-03-20 17:55:04 +01001080 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001081}
1082
1083psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1084 const uint8_t *input,
1085 size_t input_length )
1086{
1087 int ret;
1088 if( ! operation->key_set )
1089 return( PSA_ERROR_BAD_STATE );
1090 if( operation->iv_required && ! operation->iv_set )
1091 return( PSA_ERROR_BAD_STATE );
1092 operation->has_input = 1;
1093
1094 switch( operation->alg )
1095 {
1096#if defined(MBEDTLS_CMAC_C)
1097 case PSA_ALG_CMAC:
1098 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1099 input, input_length );
1100 break;
1101#endif /* MBEDTLS_CMAC_C */
1102 default:
1103#if defined(MBEDTLS_MD_C)
1104 if( PSA_ALG_IS_HMAC( operation->alg ) )
1105 {
1106 ret = mbedtls_md_hmac_update( &operation->ctx.hmac,
1107 input, input_length );
1108 }
1109 else
1110#endif /* MBEDTLS_MD_C */
1111 {
1112 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1113 }
1114 break;
1115 }
1116 if( ret != 0 )
1117 psa_mac_abort( operation );
1118 return( mbedtls_to_psa_error( ret ) );
1119}
1120
mohammad16036df908f2018-04-02 08:34:15 -07001121static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01001122 uint8_t *mac,
1123 size_t mac_size,
1124 size_t *mac_length )
1125{
1126 int ret;
1127 if( ! operation->key_set )
1128 return( PSA_ERROR_BAD_STATE );
1129 if( operation->iv_required && ! operation->iv_set )
1130 return( PSA_ERROR_BAD_STATE );
1131
1132 /* Fill the output buffer with something that isn't a valid mac
1133 * (barring an attack on the mac and deliberately-crafted input),
1134 * in case the caller doesn't check the return status properly. */
1135 *mac_length = operation->mac_size;
1136 memset( mac, '!', mac_size );
1137
1138 if( mac_size < operation->mac_size )
1139 return( PSA_ERROR_BUFFER_TOO_SMALL );
1140
1141 switch( operation->alg )
1142 {
1143#if defined(MBEDTLS_CMAC_C)
1144 case PSA_ALG_CMAC:
1145 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1146 break;
1147#endif /* MBEDTLS_CMAC_C */
1148 default:
1149#if defined(MBEDTLS_MD_C)
1150 if( PSA_ALG_IS_HMAC( operation->alg ) )
1151 {
1152 ret = mbedtls_md_hmac_finish( &operation->ctx.hmac, mac );
1153 }
1154 else
1155#endif /* MBEDTLS_MD_C */
1156 {
1157 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1158 }
1159 break;
1160 }
1161
1162 if( ret == 0 )
1163 {
1164 return( psa_mac_abort( operation ) );
1165 }
1166 else
1167 {
1168 psa_mac_abort( operation );
1169 return( mbedtls_to_psa_error( ret ) );
1170 }
1171}
1172
mohammad16036df908f2018-04-02 08:34:15 -07001173psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1174 uint8_t *mac,
1175 size_t mac_size,
1176 size_t *mac_length )
1177{
1178 if( !( operation->key_usage_sign ) )
1179 return( PSA_ERROR_NOT_PERMITTED );
1180
1181 return( psa_mac_finish_internal(operation, mac, mac_size, mac_length ) );
1182}
1183
Gilles Peskine8c9def32018-02-08 10:02:12 +01001184#define MBEDTLS_PSA_MAC_MAX_SIZE \
1185 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1186 MBEDTLS_MD_MAX_SIZE : \
1187 MBEDTLS_MAX_BLOCK_LENGTH )
1188psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1189 const uint8_t *mac,
1190 size_t mac_length )
1191{
1192 uint8_t actual_mac[MBEDTLS_PSA_MAC_MAX_SIZE];
1193 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001194 psa_status_t status;
1195
1196 if( !( operation->key_usage_verify ) )
1197 return( PSA_ERROR_NOT_PERMITTED );
1198
1199 status = psa_mac_finish_internal( operation,
1200 actual_mac, sizeof( actual_mac ),
1201 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001202 if( status != PSA_SUCCESS )
1203 return( status );
1204 if( actual_mac_length != mac_length )
1205 return( PSA_ERROR_INVALID_SIGNATURE );
1206 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1207 return( PSA_ERROR_INVALID_SIGNATURE );
1208 return( PSA_SUCCESS );
1209}
1210
1211
Gilles Peskine20035e32018-02-03 22:44:14 +01001212
1213
1214/****************************************************************/
1215/* Asymmetric cryptography */
1216/****************************************************************/
1217
1218psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1219 psa_algorithm_t alg,
1220 const uint8_t *hash,
1221 size_t hash_length,
1222 const uint8_t *salt,
1223 size_t salt_length,
1224 uint8_t *signature,
1225 size_t signature_size,
1226 size_t *signature_length)
1227{
1228 key_slot_t *slot;
1229
Gilles Peskine93aa0332018-02-03 23:58:03 +01001230 *signature_length = 0;
1231 (void) salt;
1232 (void) salt_length;
1233
Gilles Peskine20035e32018-02-03 22:44:14 +01001234 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1235 return( PSA_ERROR_EMPTY_SLOT );
1236 slot = &global_data.key_slots[key];
1237 if( slot->type == PSA_KEY_TYPE_NONE )
1238 return( PSA_ERROR_EMPTY_SLOT );
1239 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1240 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001241 if( !( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
1242 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001243
Gilles Peskine20035e32018-02-03 22:44:14 +01001244#if defined(MBEDTLS_RSA_C)
1245 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1246 {
1247 mbedtls_rsa_context *rsa = slot->data.rsa;
1248 int ret;
1249 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001250 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001251 mbedtls_md_type_t md_alg =
1252 hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1253 if( md_alg == MBEDTLS_MD_NONE )
1254 {
1255#if SIZE_MAX > UINT_MAX
1256 if( hash_length > UINT_MAX )
1257 return( PSA_ERROR_INVALID_ARGUMENT );
1258#endif
1259 }
1260 else
1261 {
1262 if( mbedtls_md_get_size( md_info ) != hash_length )
1263 return( PSA_ERROR_INVALID_ARGUMENT );
1264 if( md_info == NULL )
1265 return( PSA_ERROR_NOT_SUPPORTED );
1266 }
1267 if( signature_size < rsa->len )
1268 return( PSA_ERROR_BUFFER_TOO_SMALL );
1269#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001270 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001271 {
1272 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1273 MBEDTLS_MD_NONE );
1274 ret = mbedtls_rsa_pkcs1_sign( rsa,
1275 mbedtls_ctr_drbg_random,
1276 &global_data.ctr_drbg,
1277 MBEDTLS_RSA_PRIVATE,
1278 md_alg, hash_length, hash,
1279 signature );
1280 }
1281 else
1282#endif /* MBEDTLS_PKCS1_V15 */
1283#if defined(MBEDTLS_PKCS1_V21)
1284 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1285 {
1286 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1287 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1288 mbedtls_ctr_drbg_random,
1289 &global_data.ctr_drbg,
1290 MBEDTLS_RSA_PRIVATE,
1291 md_alg, hash_length, hash,
1292 signature );
1293 }
1294 else
1295#endif /* MBEDTLS_PKCS1_V21 */
1296 {
1297 return( PSA_ERROR_INVALID_ARGUMENT );
1298 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001299 if( ret == 0 )
1300 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001301 return( mbedtls_to_psa_error( ret ) );
1302 }
1303 else
1304#endif /* defined(MBEDTLS_RSA_C) */
1305#if defined(MBEDTLS_ECP_C)
1306 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1307 {
1308 // TODO
1309 return( PSA_ERROR_NOT_SUPPORTED );
1310 }
1311 else
1312#endif /* defined(MBEDTLS_ECP_C) */
1313 {
1314 return( PSA_ERROR_NOT_SUPPORTED );
1315 }
1316}
1317
1318
mohammad1603503973b2018-03-12 15:59:30 +02001319/****************************************************************/
1320/* Symmetric cryptography */
1321/****************************************************************/
1322
mohammad16038481e742018-03-18 13:57:31 +02001323static psa_status_t psa_setup(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001324 psa_key_slot_t key,
mohammad16038481e742018-03-18 13:57:31 +02001325 psa_algorithm_t alg, mbedtls_operation_t cipher_operation)
mohammad1603503973b2018-03-12 15:59:30 +02001326{
1327 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1328 psa_status_t status;
1329 key_slot_t *slot;
1330 psa_key_type_t key_type;
1331 size_t key_bits;
1332 const mbedtls_cipher_info_t *cipher_info = NULL;
mohammad16038481e742018-03-18 13:57:31 +02001333 psa_algorithm_t padding_mode = PSA_ALG_BLOCK_CIPHER_PAD_NONE;
1334 mbedtls_cipher_padding_t mode = MBEDTLS_PADDING_NONE;
mohammad1603503973b2018-03-12 15:59:30 +02001335
mohammad16038481e742018-03-18 13:57:31 +02001336 psa_operation_init(operation, alg);
mohammad1603503973b2018-03-12 15:59:30 +02001337
1338 status = psa_get_key_information( key, &key_type, &key_bits );
1339 if( status != PSA_SUCCESS )
1340 return( status );
1341 slot = &global_data.key_slots[key];
1342
1343 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits );
1344 if( cipher_info == NULL )
1345 return( PSA_ERROR_NOT_SUPPORTED );
1346
1347 operation->block_size = cipher_info->block_size;
1348
1349 mbedtls_cipher_init( &operation->ctx.cipher );
1350 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
1351 if (ret != 0)
1352 {
1353 psa_cipher_abort( operation );
1354 return( mbedtls_to_psa_error( ret ) );
1355 }
1356
1357 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data,
mohammad16038481e742018-03-18 13:57:31 +02001358 key_bits, cipher_operation );
mohammad1603503973b2018-03-12 15:59:30 +02001359 if (ret != 0)
1360 {
1361 psa_cipher_abort( operation );
1362 return( mbedtls_to_psa_error( ret ) );
1363 }
1364
mohammad16038481e742018-03-18 13:57:31 +02001365#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1366 if (( alg & PSA_ALG_CBC_BASE) == PSA_ALG_CBC_BASE)
1367 {
1368 padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1369
1370 switch (padding_mode)
1371 {
1372 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
1373 mode = MBEDTLS_PADDING_PKCS7;
1374 break;
1375 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
1376 mode = MBEDTLS_PADDING_NONE;
1377 break;
1378 default:
1379 return ( PSA_ERROR_INVALID_PADDING );
1380 }
1381 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
1382 if (ret != 0)
1383 return( mbedtls_to_psa_error( ret ) );
1384 }
1385#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
1386
mohammad1603503973b2018-03-12 15:59:30 +02001387 operation->key_set = 1;
1388 operation->alg = alg;
mohammad16038481e742018-03-18 13:57:31 +02001389 operation->block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type);
1390 if ( PSA_ALG_IS_BLOCK_CIPHER(alg) )
1391 {
1392 operation->iv_size = operation->block_size;
1393 }
mohammad1603503973b2018-03-12 15:59:30 +02001394
1395 return ( PSA_SUCCESS );
1396}
1397
mohammad16038481e742018-03-18 13:57:31 +02001398psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation,
1399 psa_key_slot_t key,
1400 psa_algorithm_t alg)
1401{
1402 return psa_setup(operation, key, alg, MBEDTLS_ENCRYPT);
1403}
1404
1405psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
1406 psa_key_slot_t key,
1407 psa_algorithm_t alg)
1408{
1409 return psa_setup(operation, key, alg, MBEDTLS_DECRYPT);
1410}
1411
1412psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation,
1413 unsigned char *iv,
mohammad1603503973b2018-03-12 15:59:30 +02001414 size_t iv_size,
1415 size_t *iv_length)
1416{
1417 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
mohammad16038481e742018-03-18 13:57:31 +02001418 if (iv_size < operation->iv_size)
1419 return ( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02001420
mohammad16038481e742018-03-18 13:57:31 +02001421 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, operation->iv_size);
mohammad1603503973b2018-03-12 15:59:30 +02001422 if (ret != 0)
1423 {
1424 return( mbedtls_to_psa_error( ret ) );
1425 }
1426
mohammad16038481e742018-03-18 13:57:31 +02001427 *iv_length = operation->iv_size;
1428
1429 return psa_encrypt_set_iv( operation, iv, *iv_length);
mohammad1603503973b2018-03-12 15:59:30 +02001430}
1431
1432psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
1433 const unsigned char *iv,
1434 size_t iv_length)
1435{
1436 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1437
1438 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
1439 if (ret != 0)
1440 {
1441 psa_cipher_abort( operation );
1442 return( mbedtls_to_psa_error( ret ) );
1443 }
1444
1445 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02001446
1447 return ( PSA_SUCCESS );
1448}
1449
1450psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1451 const uint8_t *input,
1452 size_t input_length,
1453 unsigned char *output,
1454 size_t output_size,
1455 size_t *output_length)
1456{
1457 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1458
mohammad160382759612018-03-12 18:16:40 +02001459 if ( output_size < input_length )
1460 return ( PSA_ERROR_BUFFER_TOO_SMALL );
1461
mohammad1603503973b2018-03-12 15:59:30 +02001462 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
1463 input_length, output, output_length );
1464 if (ret != 0)
1465 {
1466 psa_cipher_abort( operation );
1467 return( mbedtls_to_psa_error( ret ) );
1468 }
1469
1470 return ( PSA_SUCCESS );
1471}
1472
1473psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
1474 uint8_t *output,
1475 size_t output_size,
1476 size_t *output_length)
1477{
1478 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
mohammad160382759612018-03-12 18:16:40 +02001479
1480 if ( output_size < operation->block_size )
1481 return ( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad1603503973b2018-03-12 15:59:30 +02001482
1483 if( ! operation->key_set )
1484 return( PSA_ERROR_BAD_STATE );
1485 if( ! operation->iv_set )
1486 return( PSA_ERROR_BAD_STATE );
1487
1488 ret = mbedtls_cipher_finish( &operation->ctx.cipher, output,
1489 output_length );
1490 if (ret != 0)
1491 {
1492 psa_cipher_abort( operation );
1493 return( mbedtls_to_psa_error( ret ) );
1494 }
1495
1496 return ( PSA_SUCCESS );
1497}
1498
1499psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
1500{
1501 mbedtls_cipher_free( &operation->ctx.cipher );
1502
mohammad16038481e742018-03-18 13:57:31 +02001503 psa_operation_init(operation, 0);
mohammad1603503973b2018-03-12 15:59:30 +02001504
1505 return ( PSA_SUCCESS );
1506}
1507
Gilles Peskinea0655c32018-04-30 17:06:50 +02001508
mohammad16038cc1cee2018-03-28 01:21:33 +03001509/****************************************************************/
1510/* Key Policy */
1511/****************************************************************/
1512
1513void psa_key_policy_init(psa_key_policy_t *policy)
1514{
mohammad16036df908f2018-04-02 08:34:15 -07001515 memset( policy, 0, sizeof( psa_key_policy_t ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03001516}
1517
1518void psa_key_policy_set_usage(psa_key_policy_t *policy,
1519 psa_key_usage_t usage,
1520 psa_algorithm_t alg)
1521{
mohammad16034eed7572018-03-28 05:14:59 -07001522 policy->usage = usage;
1523 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03001524}
1525
1526psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy)
1527{
mohammad16036df908f2018-04-02 08:34:15 -07001528 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03001529}
1530
1531psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy)
1532{
mohammad16036df908f2018-04-02 08:34:15 -07001533 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03001534}
1535
1536psa_status_t psa_set_key_policy(psa_key_slot_t key,
1537 const psa_key_policy_t *policy)
1538{
1539 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03001540
1541 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
1542 return( PSA_ERROR_INVALID_ARGUMENT );
1543
1544 slot = &global_data.key_slots[key];
1545 if( slot->type != PSA_KEY_TYPE_NONE )
1546 return( PSA_ERROR_OCCUPIED_SLOT );
1547
mohammad16036df908f2018-04-02 08:34:15 -07001548 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_ENCRYPT
1549 | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN
1550 | PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07001551 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03001552
mohammad16036df908f2018-04-02 08:34:15 -07001553 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03001554
1555 return( PSA_SUCCESS );
1556}
1557
1558psa_status_t psa_get_key_policy(psa_key_slot_t key,
1559 psa_key_policy_t *policy)
1560{
1561 key_slot_t *slot;
1562
1563 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
1564 return( PSA_ERROR_INVALID_ARGUMENT );
1565
1566 slot = &global_data.key_slots[key];
mohammad16038cc1cee2018-03-28 01:21:33 +03001567
mohammad16036df908f2018-04-02 08:34:15 -07001568 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03001569
1570 return( PSA_SUCCESS );
1571}
Gilles Peskine20035e32018-02-03 22:44:14 +01001572
Gilles Peskinea0655c32018-04-30 17:06:50 +02001573
1574
mohammad1603804cd712018-03-20 22:44:08 +02001575/****************************************************************/
1576/* Key Lifetime */
1577/****************************************************************/
1578
1579psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1580 psa_key_lifetime_t *lifetime)
1581{
1582 key_slot_t *slot;
1583
1584 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1585 return( PSA_ERROR_INVALID_ARGUMENT );
1586
1587 slot = &global_data.key_slots[key];
mohammad1603804cd712018-03-20 22:44:08 +02001588
1589 *lifetime = slot->lifetime;
1590
1591 return( PSA_SUCCESS );
1592}
1593
1594psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
1595 const psa_key_lifetime_t lifetime)
1596{
1597 key_slot_t *slot;
1598
1599 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1600 return( PSA_ERROR_INVALID_ARGUMENT );
1601
mohammad1603ba178512018-03-21 04:35:20 -07001602 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
1603 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
1604 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
1605 return( PSA_ERROR_INVALID_ARGUMENT );
1606
mohammad1603804cd712018-03-20 22:44:08 +02001607 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03001608 if( slot->type != PSA_KEY_TYPE_NONE )
1609 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02001610
mohammad1603ba178512018-03-21 04:35:20 -07001611 if ( lifetime != PSA_KEY_LIFETIME_VOLATILE )
1612 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603804cd712018-03-20 22:44:08 +02001613
mohammad1603060ad8a2018-03-20 14:28:38 -07001614 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02001615
1616 return( PSA_SUCCESS );
1617}
1618
Gilles Peskine20035e32018-02-03 22:44:14 +01001619
Gilles Peskinea0655c32018-04-30 17:06:50 +02001620
Gilles Peskine20035e32018-02-03 22:44:14 +01001621/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001622/* Module setup */
1623/****************************************************************/
1624
Gilles Peskinee59236f2018-01-27 23:32:46 +01001625void mbedtls_psa_crypto_free( void )
1626{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001627 size_t key;
1628 for( key = 1; key < MBEDTLS_PSA_KEY_SLOT_COUNT; key++ )
1629 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001630 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
1631 mbedtls_entropy_free( &global_data.entropy );
1632 mbedtls_zeroize( &global_data, sizeof( global_data ) );
1633}
1634
1635psa_status_t psa_crypto_init( void )
1636{
1637 int ret;
1638 const unsigned char drbg_seed[] = "PSA";
1639
1640 if( global_data.initialized != 0 )
1641 return( PSA_SUCCESS );
1642
1643 mbedtls_zeroize( &global_data, sizeof( global_data ) );
1644 mbedtls_entropy_init( &global_data.entropy );
1645 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
1646
1647 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
1648 mbedtls_entropy_func,
1649 &global_data.entropy,
1650 drbg_seed, sizeof( drbg_seed ) - 1 );
1651 if( ret != 0 )
1652 goto exit;
1653
Gilles Peskinee4ebc122018-03-07 14:16:44 +01001654 global_data.initialized = 1;
1655
Gilles Peskinee59236f2018-01-27 23:32:46 +01001656exit:
1657 if( ret != 0 )
1658 mbedtls_psa_crypto_free( );
1659 return( mbedtls_to_psa_error( ret ) );
1660}
1661
1662#endif /* MBEDTLS_PSA_CRYPTO_C */