blob: 438f0f04da7563f9b7508e7ab7a62d350fe9e166 [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
itayzafrir5c753392018-05-08 11:18:38 +0300284 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300285 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300286 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300287 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
288 return( PSA_ERROR_BUFFER_TOO_SMALL );
289 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
290 return( PSA_ERROR_NOT_SUPPORTED );
291 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
292 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
293 return( PSA_ERROR_INVALID_SIGNATURE );
294 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
295 return( PSA_ERROR_INSUFFICIENT_MEMORY );
296 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300298
Gilles Peskinee59236f2018-01-27 23:32:46 +0100299 default:
300 return( PSA_ERROR_UNKNOWN_ERROR );
301 }
302}
303
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100304/****************************************************************/
305/* Key management */
306/****************************************************************/
307
308psa_status_t psa_import_key(psa_key_slot_t key,
309 psa_key_type_t type,
310 const uint8_t *data,
311 size_t data_length)
312{
313 key_slot_t *slot;
314
315 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
316 return( PSA_ERROR_INVALID_ARGUMENT );
317 slot = &global_data.key_slots[key];
318 if( slot->type != PSA_KEY_TYPE_NONE )
319 return( PSA_ERROR_OCCUPIED_SLOT );
320
Gilles Peskine8c9def32018-02-08 10:02:12 +0100321 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100322 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100323 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324 if( data_length > SIZE_MAX / 8 )
325 return( PSA_ERROR_NOT_SUPPORTED );
326 slot->data.raw.data = mbedtls_calloc( 1, data_length );
327 if( slot->data.raw.data == NULL )
328 return( PSA_ERROR_INSUFFICIENT_MEMORY );
329 memcpy( slot->data.raw.data, data, data_length );
330 slot->data.raw.bytes = data_length;
331 }
332 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100333#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100334 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
335 type == PSA_KEY_TYPE_RSA_KEYPAIR ||
336 PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100337 {
338 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100339 mbedtls_pk_context pk;
340 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100341 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
342 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
343 else
344 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100345 if( ret != 0 )
346 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100347 switch( mbedtls_pk_get_type( &pk ) )
348 {
349#if defined(MBEDTLS_RSA_C)
350 case MBEDTLS_PK_RSA:
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100351 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
352 type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine969ac722018-01-28 18:16:59 +0100353 slot->data.rsa = pk.pk_ctx;
354 else
355 return( PSA_ERROR_INVALID_ARGUMENT );
356 break;
357#endif /* MBEDTLS_RSA_C */
358#if defined(MBEDTLS_ECP_C)
359 case MBEDTLS_PK_ECKEY:
360 if( PSA_KEY_TYPE_IS_ECC( type ) )
361 {
362 // TODO: check curve
363 slot->data.ecp = pk.pk_ctx;
364 }
365 else
366 return( PSA_ERROR_INVALID_ARGUMENT );
367 break;
368#endif /* MBEDTLS_ECP_C */
369 default:
370 return( PSA_ERROR_INVALID_ARGUMENT );
371 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100372 }
373 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100374#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100375 {
376 return( PSA_ERROR_NOT_SUPPORTED );
377 }
378
379 slot->type = type;
380 return( PSA_SUCCESS );
381}
382
383psa_status_t psa_destroy_key(psa_key_slot_t key)
384{
385 key_slot_t *slot;
386
387 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
388 return( PSA_ERROR_INVALID_ARGUMENT );
389 slot = &global_data.key_slots[key];
390 if( slot->type == PSA_KEY_TYPE_NONE )
391 return( PSA_ERROR_EMPTY_SLOT );
392
Gilles Peskine8c9def32018-02-08 10:02:12 +0100393 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100394 {
395 mbedtls_free( slot->data.raw.data );
396 }
397 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100398#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100399 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
400 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100401 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100402 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100403 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100404 }
405 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100406#endif /* defined(MBEDTLS_RSA_C) */
407#if defined(MBEDTLS_ECP_C)
408 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
409 {
410 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100411 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100412 }
413 else
414#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415 {
416 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100417 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100418 return( PSA_ERROR_TAMPERING_DETECTED );
419 }
420
421 mbedtls_zeroize( slot, sizeof( *slot ) );
422 return( PSA_SUCCESS );
423}
424
425psa_status_t psa_get_key_information(psa_key_slot_t key,
426 psa_key_type_t *type,
427 size_t *bits)
428{
429 key_slot_t *slot;
430
431 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100432 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100433 slot = &global_data.key_slots[key];
434 if( type != NULL )
435 *type = slot->type;
436 if( bits != NULL )
437 *bits = 0;
438 if( slot->type == PSA_KEY_TYPE_NONE )
439 return( PSA_ERROR_EMPTY_SLOT );
440
Gilles Peskine8c9def32018-02-08 10:02:12 +0100441 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100442 {
443 if( bits != NULL )
444 *bits = slot->data.raw.bytes * 8;
445 }
446 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100447#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100448 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
449 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100450 {
451 if( bits != NULL )
Gilles Peskine969ac722018-01-28 18:16:59 +0100452 *bits = mbedtls_rsa_get_bitlen( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100453 }
454 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100455#endif /* defined(MBEDTLS_RSA_C) */
456#if defined(MBEDTLS_ECP_C)
457 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
458 {
459 if( bits != NULL )
460 *bits = slot->data.ecp->grp.pbits;
461 }
462 else
463#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100464 {
465 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100466 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100467 return( PSA_ERROR_TAMPERING_DETECTED );
468 }
469
470 return( PSA_SUCCESS );
471}
472
Moran Pekera998bc62018-04-16 18:16:20 +0300473static psa_status_t psa_internal_export_key(psa_key_slot_t key,
474 uint8_t *data,
475 size_t data_size,
476 size_t *data_length,
477 int export_public_key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100478{
479 key_slot_t *slot;
480
481 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100482 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100483 slot = &global_data.key_slots[key];
484 if( slot->type == PSA_KEY_TYPE_NONE )
485 return( PSA_ERROR_EMPTY_SLOT );
486
Moran Peker87567632018-06-04 18:41:37 +0300487 if( export_public_key && ( !( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) ) ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300488 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300489
Moran Peker87567632018-06-04 18:41:37 +0300490 if( ( !export_public_key ) && ( !( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ) ) &&
491 ( !( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) ) )
492 return( PSA_ERROR_NOT_PERMITTED );
493
Gilles Peskine8c9def32018-02-08 10:02:12 +0100494 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100495 {
496 if( slot->data.raw.bytes > data_size )
497 return( PSA_ERROR_BUFFER_TOO_SMALL );
498 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
499 *data_length = slot->data.raw.bytes;
500 return( PSA_SUCCESS );
501 }
502 else
Moran Peker17e36e12018-05-02 12:55:20 +0300503 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100504#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100505 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
Moran Pekera998bc62018-04-16 18:16:20 +0300506 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
507 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100508 {
Moran Pekera998bc62018-04-16 18:16:20 +0300509 mbedtls_pk_context pk;
510 int ret;
511 mbedtls_pk_init( &pk );
512 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
513 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
514 {
515 pk.pk_info = &mbedtls_rsa_info;
516 pk.pk_ctx = slot->data.rsa;
517 }
518 else
519 {
520 pk.pk_info = &mbedtls_eckey_info;
521 pk.pk_ctx = slot->data.ecp;
522 }
Moran Pekerd7326592018-05-29 16:56:39 +0300523 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300524 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300525 else
526 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300527 if( ret < 0 )
Moran Pekera998bc62018-04-16 18:16:20 +0300528 return( mbedtls_to_psa_error( ret ) );
529 *data_length = ret;
530 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100531 }
532 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100533#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300534 {
535 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200536 it is valid for a special-purpose implementation to omit
537 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300538 return( PSA_ERROR_NOT_SUPPORTED );
539 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100540 }
541}
542
Moran Pekera998bc62018-04-16 18:16:20 +0300543psa_status_t psa_export_key(psa_key_slot_t key,
544 uint8_t *data,
545 size_t data_size,
546 size_t *data_length)
547{
548 return psa_internal_export_key( key, data, data_size,
549 data_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100550}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551
552
Moran Pekerdd4ea382018-04-03 15:30:03 +0300553psa_status_t psa_export_public_key(psa_key_slot_t key,
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300554 uint8_t *data,
555 size_t data_size,
556 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +0300557{
Moran Pekera998bc62018-04-16 18:16:20 +0300558 return psa_internal_export_key( key, data, data_size,
559 data_length, 1 );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300560}
561
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100563/* Message digests */
564/****************************************************************/
565
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100566static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100567{
568 switch( alg )
569 {
570#if defined(MBEDTLS_MD2_C)
571 case PSA_ALG_MD2:
572 return( &mbedtls_md2_info );
573#endif
574#if defined(MBEDTLS_MD4_C)
575 case PSA_ALG_MD4:
576 return( &mbedtls_md4_info );
577#endif
578#if defined(MBEDTLS_MD5_C)
579 case PSA_ALG_MD5:
580 return( &mbedtls_md5_info );
581#endif
582#if defined(MBEDTLS_RIPEMD160_C)
583 case PSA_ALG_RIPEMD160:
584 return( &mbedtls_ripemd160_info );
585#endif
586#if defined(MBEDTLS_SHA1_C)
587 case PSA_ALG_SHA_1:
588 return( &mbedtls_sha1_info );
589#endif
590#if defined(MBEDTLS_SHA256_C)
591 case PSA_ALG_SHA_224:
592 return( &mbedtls_sha224_info );
593 case PSA_ALG_SHA_256:
594 return( &mbedtls_sha256_info );
595#endif
596#if defined(MBEDTLS_SHA512_C)
597 case PSA_ALG_SHA_384:
598 return( &mbedtls_sha384_info );
599 case PSA_ALG_SHA_512:
600 return( &mbedtls_sha512_info );
601#endif
602 default:
603 return( NULL );
604 }
605}
606
607#if 0
608static psa_algorithm_t mbedtls_md_alg_to_psa( mbedtls_md_type_t md_alg )
609{
610 switch( md_alg )
611 {
612 case MBEDTLS_MD_NONE:
613 return( 0 );
614 case MBEDTLS_MD_MD2:
615 return( PSA_ALG_MD2 );
616 case MBEDTLS_MD_MD4:
617 return( PSA_ALG_MD4 );
618 case MBEDTLS_MD_MD5:
619 return( PSA_ALG_MD5 );
620 case MBEDTLS_MD_SHA1:
621 return( PSA_ALG_SHA_1 );
622 case MBEDTLS_MD_SHA224:
623 return( PSA_ALG_SHA_224 );
624 case MBEDTLS_MD_SHA256:
625 return( PSA_ALG_SHA_256 );
626 case MBEDTLS_MD_SHA384:
627 return( PSA_ALG_SHA_384 );
628 case MBEDTLS_MD_SHA512:
629 return( PSA_ALG_SHA_512 );
630 case MBEDTLS_MD_RIPEMD160:
631 return( PSA_ALG_RIPEMD160 );
632 default:
Gilles Peskine47c1bc02018-03-20 17:55:04 +0100633 return( 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +0100634 }
635}
636#endif
637
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100638psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
639{
640 switch( operation->alg )
641 {
642#if defined(MBEDTLS_MD2_C)
643 case PSA_ALG_MD2:
644 mbedtls_md2_free( &operation->ctx.md2 );
645 break;
646#endif
647#if defined(MBEDTLS_MD4_C)
648 case PSA_ALG_MD4:
649 mbedtls_md4_free( &operation->ctx.md4 );
650 break;
651#endif
652#if defined(MBEDTLS_MD5_C)
653 case PSA_ALG_MD5:
654 mbedtls_md5_free( &operation->ctx.md5 );
655 break;
656#endif
657#if defined(MBEDTLS_RIPEMD160_C)
658 case PSA_ALG_RIPEMD160:
659 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
660 break;
661#endif
662#if defined(MBEDTLS_SHA1_C)
663 case PSA_ALG_SHA_1:
664 mbedtls_sha1_free( &operation->ctx.sha1 );
665 break;
666#endif
667#if defined(MBEDTLS_SHA256_C)
668 case PSA_ALG_SHA_224:
669 case PSA_ALG_SHA_256:
670 mbedtls_sha256_free( &operation->ctx.sha256 );
671 break;
672#endif
673#if defined(MBEDTLS_SHA512_C)
674 case PSA_ALG_SHA_384:
675 case PSA_ALG_SHA_512:
676 mbedtls_sha512_free( &operation->ctx.sha512 );
677 break;
678#endif
679 default:
680 return( PSA_ERROR_NOT_SUPPORTED );
681 }
682 operation->alg = 0;
683 return( PSA_SUCCESS );
684}
685
686psa_status_t psa_hash_start( psa_hash_operation_t *operation,
687 psa_algorithm_t alg )
688{
689 int ret;
690 operation->alg = 0;
691 switch( alg )
692 {
693#if defined(MBEDTLS_MD2_C)
694 case PSA_ALG_MD2:
695 mbedtls_md2_init( &operation->ctx.md2 );
696 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
697 break;
698#endif
699#if defined(MBEDTLS_MD4_C)
700 case PSA_ALG_MD4:
701 mbedtls_md4_init( &operation->ctx.md4 );
702 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
703 break;
704#endif
705#if defined(MBEDTLS_MD5_C)
706 case PSA_ALG_MD5:
707 mbedtls_md5_init( &operation->ctx.md5 );
708 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
709 break;
710#endif
711#if defined(MBEDTLS_RIPEMD160_C)
712 case PSA_ALG_RIPEMD160:
713 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
714 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
715 break;
716#endif
717#if defined(MBEDTLS_SHA1_C)
718 case PSA_ALG_SHA_1:
719 mbedtls_sha1_init( &operation->ctx.sha1 );
720 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
721 break;
722#endif
723#if defined(MBEDTLS_SHA256_C)
724 case PSA_ALG_SHA_224:
725 mbedtls_sha256_init( &operation->ctx.sha256 );
726 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
727 break;
728 case PSA_ALG_SHA_256:
729 mbedtls_sha256_init( &operation->ctx.sha256 );
730 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
731 break;
732#endif
733#if defined(MBEDTLS_SHA512_C)
734 case PSA_ALG_SHA_384:
735 mbedtls_sha512_init( &operation->ctx.sha512 );
736 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
737 break;
738 case PSA_ALG_SHA_512:
739 mbedtls_sha512_init( &operation->ctx.sha512 );
740 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
741 break;
742#endif
743 default:
744 return( PSA_ERROR_NOT_SUPPORTED );
745 }
746 if( ret == 0 )
747 operation->alg = alg;
748 else
749 psa_hash_abort( operation );
750 return( mbedtls_to_psa_error( ret ) );
751}
752
753psa_status_t psa_hash_update( psa_hash_operation_t *operation,
754 const uint8_t *input,
755 size_t input_length )
756{
757 int ret;
758 switch( operation->alg )
759 {
760#if defined(MBEDTLS_MD2_C)
761 case PSA_ALG_MD2:
762 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
763 input, input_length );
764 break;
765#endif
766#if defined(MBEDTLS_MD4_C)
767 case PSA_ALG_MD4:
768 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
769 input, input_length );
770 break;
771#endif
772#if defined(MBEDTLS_MD5_C)
773 case PSA_ALG_MD5:
774 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
775 input, input_length );
776 break;
777#endif
778#if defined(MBEDTLS_RIPEMD160_C)
779 case PSA_ALG_RIPEMD160:
780 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
781 input, input_length );
782 break;
783#endif
784#if defined(MBEDTLS_SHA1_C)
785 case PSA_ALG_SHA_1:
786 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
787 input, input_length );
788 break;
789#endif
790#if defined(MBEDTLS_SHA256_C)
791 case PSA_ALG_SHA_224:
792 case PSA_ALG_SHA_256:
793 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
794 input, input_length );
795 break;
796#endif
797#if defined(MBEDTLS_SHA512_C)
798 case PSA_ALG_SHA_384:
799 case PSA_ALG_SHA_512:
800 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
801 input, input_length );
802 break;
803#endif
804 default:
805 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
806 break;
807 }
808 if( ret != 0 )
809 psa_hash_abort( operation );
810 return( mbedtls_to_psa_error( ret ) );
811}
812
813psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
814 uint8_t *hash,
815 size_t hash_size,
816 size_t *hash_length )
817{
818 int ret;
819 size_t actual_hash_length = PSA_HASH_FINAL_SIZE( operation->alg );
820
821 /* Fill the output buffer with something that isn't a valid hash
822 * (barring an attack on the hash and deliberately-crafted input),
823 * in case the caller doesn't check the return status properly. */
824 *hash_length = actual_hash_length;
825 memset( hash, '!', hash_size );
826
827 if( hash_size < actual_hash_length )
828 return( PSA_ERROR_BUFFER_TOO_SMALL );
829
830 switch( operation->alg )
831 {
832#if defined(MBEDTLS_MD2_C)
833 case PSA_ALG_MD2:
834 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
835 break;
836#endif
837#if defined(MBEDTLS_MD4_C)
838 case PSA_ALG_MD4:
839 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
840 break;
841#endif
842#if defined(MBEDTLS_MD5_C)
843 case PSA_ALG_MD5:
844 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
845 break;
846#endif
847#if defined(MBEDTLS_RIPEMD160_C)
848 case PSA_ALG_RIPEMD160:
849 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
850 break;
851#endif
852#if defined(MBEDTLS_SHA1_C)
853 case PSA_ALG_SHA_1:
854 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
855 break;
856#endif
857#if defined(MBEDTLS_SHA256_C)
858 case PSA_ALG_SHA_224:
859 case PSA_ALG_SHA_256:
860 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
861 break;
862#endif
863#if defined(MBEDTLS_SHA512_C)
864 case PSA_ALG_SHA_384:
865 case PSA_ALG_SHA_512:
866 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
867 break;
868#endif
869 default:
870 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
871 break;
872 }
873
874 if( ret == 0 )
875 {
876 return( psa_hash_abort( operation ) );
877 }
878 else
879 {
880 psa_hash_abort( operation );
881 return( mbedtls_to_psa_error( ret ) );
882 }
883}
884
885psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
886 const uint8_t *hash,
887 size_t hash_length)
888{
889 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
890 size_t actual_hash_length;
891 psa_status_t status = psa_hash_finish( operation,
892 actual_hash, sizeof( actual_hash ),
893 &actual_hash_length );
894 if( status != PSA_SUCCESS )
895 return( status );
896 if( actual_hash_length != hash_length )
897 return( PSA_ERROR_INVALID_SIGNATURE );
898 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
899 return( PSA_ERROR_INVALID_SIGNATURE );
900 return( PSA_SUCCESS );
901}
902
903
904
905
906/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +0100907/* MAC */
908/****************************************************************/
909
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100910static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +0100911 psa_algorithm_t alg,
912 psa_key_type_t key_type,
913 size_t key_bits )
914{
915 mbedtls_cipher_id_t cipher_id;
916 mbedtls_cipher_mode_t mode;
917
918 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
919 {
920 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +0200921 {
922 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
923 }
Gilles Peskine8c9def32018-02-08 10:02:12 +0100924 switch( alg )
925 {
926 case PSA_ALG_STREAM_CIPHER:
927 mode = MBEDTLS_MODE_STREAM;
928 break;
929 case PSA_ALG_CBC_BASE:
930 mode = MBEDTLS_MODE_CBC;
931 break;
932 case PSA_ALG_CFB_BASE:
933 mode = MBEDTLS_MODE_CFB;
934 break;
935 case PSA_ALG_OFB_BASE:
936 mode = MBEDTLS_MODE_OFB;
937 break;
938 case PSA_ALG_CTR:
939 mode = MBEDTLS_MODE_CTR;
940 break;
941 case PSA_ALG_CCM:
942 mode = MBEDTLS_MODE_CCM;
943 break;
944 case PSA_ALG_GCM:
945 mode = MBEDTLS_MODE_GCM;
946 break;
947 default:
948 return( NULL );
949 }
950 }
951 else if( alg == PSA_ALG_CMAC )
952 mode = MBEDTLS_MODE_ECB;
953 else if( alg == PSA_ALG_GMAC )
954 mode = MBEDTLS_MODE_GCM;
955 else
956 return( NULL );
957
958 switch( key_type )
959 {
960 case PSA_KEY_TYPE_AES:
961 cipher_id = MBEDTLS_CIPHER_ID_AES;
962 break;
963 case PSA_KEY_TYPE_DES:
964 if( key_bits == 64 )
965 cipher_id = MBEDTLS_CIPHER_ID_DES;
966 else
967 cipher_id = MBEDTLS_CIPHER_ID_3DES;
968 break;
969 case PSA_KEY_TYPE_CAMELLIA:
970 cipher_id = MBEDTLS_CIPHER_ID_CAMELLIA;
971 break;
972 case PSA_KEY_TYPE_ARC4:
973 cipher_id = MBEDTLS_CIPHER_ID_ARC4;
974 break;
975 default:
976 return( NULL );
977 }
978
979 return( mbedtls_cipher_info_from_values( cipher_id, key_bits, mode ) );
980}
981
982psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
983{
984 switch( operation->alg )
985 {
986#if defined(MBEDTLS_CMAC_C)
987 case PSA_ALG_CMAC:
988 mbedtls_cipher_free( &operation->ctx.cmac );
989 break;
990#endif /* MBEDTLS_CMAC_C */
991 default:
992#if defined(MBEDTLS_MD_C)
993 if( PSA_ALG_IS_HMAC( operation->alg ) )
994 mbedtls_md_free( &operation->ctx.hmac );
995 else
996#endif /* MBEDTLS_MD_C */
997 return( PSA_ERROR_NOT_SUPPORTED );
998 }
Moran Peker41deec42018-04-04 15:43:05 +0300999
Gilles Peskine8c9def32018-02-08 10:02:12 +01001000 operation->alg = 0;
1001 operation->key_set = 0;
1002 operation->iv_set = 0;
1003 operation->iv_required = 0;
1004 operation->has_input = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001005
Gilles Peskine8c9def32018-02-08 10:02:12 +01001006 return( PSA_SUCCESS );
1007}
1008
1009psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1010 psa_key_slot_t key,
1011 psa_algorithm_t alg )
1012{
1013 int ret = MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
1014 psa_status_t status;
1015 key_slot_t *slot;
1016 psa_key_type_t key_type;
1017 size_t key_bits;
1018 const mbedtls_cipher_info_t *cipher_info = NULL;
1019
1020 operation->alg = 0;
1021 operation->key_set = 0;
1022 operation->iv_set = 0;
1023 operation->iv_required = 1;
1024 operation->has_input = 0;
1025
1026 status = psa_get_key_information( key, &key_type, &key_bits );
1027 if( status != PSA_SUCCESS )
1028 return( status );
1029 slot = &global_data.key_slots[key];
1030
Moran Pekerd7326592018-05-29 16:56:39 +03001031 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001032 operation->key_usage_sign = 1;
1033
Moran Pekerd7326592018-05-29 16:56:39 +03001034 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001035 operation->key_usage_verify = 1;
1036
Gilles Peskine8c9def32018-02-08 10:02:12 +01001037 if( ! PSA_ALG_IS_HMAC( alg ) )
1038 {
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001039 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001040 if( cipher_info == NULL )
1041 return( PSA_ERROR_NOT_SUPPORTED );
1042 operation->mac_size = cipher_info->block_size;
1043 }
1044 switch( alg )
1045 {
1046#if defined(MBEDTLS_CMAC_C)
1047 case PSA_ALG_CMAC:
1048 operation->iv_required = 0;
1049 mbedtls_cipher_init( &operation->ctx.cmac );
1050 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1051 if( ret != 0 )
1052 break;
1053 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1054 slot->data.raw.data,
1055 key_bits );
1056 break;
1057#endif /* MBEDTLS_CMAC_C */
1058 default:
1059#if defined(MBEDTLS_MD_C)
1060 if( PSA_ALG_IS_HMAC( alg ) )
1061 {
1062 const mbedtls_md_info_t *md_info =
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001063 mbedtls_md_info_from_psa( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001064 if( md_info == NULL )
1065 return( PSA_ERROR_NOT_SUPPORTED );
1066 if( key_type != PSA_KEY_TYPE_HMAC )
1067 return( PSA_ERROR_INVALID_ARGUMENT );
1068 operation->iv_required = 0;
1069 operation->mac_size = mbedtls_md_get_size( md_info );
1070 mbedtls_md_init( &operation->ctx.hmac );
1071 ret = mbedtls_md_setup( &operation->ctx.hmac, md_info, 1 );
1072 if( ret != 0 )
1073 break;
1074 ret = mbedtls_md_hmac_starts( &operation->ctx.hmac,
1075 slot->data.raw.data,
1076 slot->data.raw.bytes );
1077 break;
1078 }
1079 else
1080#endif /* MBEDTLS_MD_C */
1081 return( PSA_ERROR_NOT_SUPPORTED );
1082 }
1083
1084 /* If we reach this point, then the algorithm-specific part of the
1085 * context has at least been initialized, and may contain data that
1086 * needs to be wiped on error. */
1087 operation->alg = alg;
1088 if( ret != 0 )
1089 {
1090 psa_mac_abort( operation );
1091 return( mbedtls_to_psa_error( ret ) );
1092 }
1093 operation->key_set = 1;
Gilles Peskine47c1bc02018-03-20 17:55:04 +01001094 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001095}
1096
1097psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1098 const uint8_t *input,
1099 size_t input_length )
1100{
1101 int ret;
1102 if( ! operation->key_set )
1103 return( PSA_ERROR_BAD_STATE );
1104 if( operation->iv_required && ! operation->iv_set )
1105 return( PSA_ERROR_BAD_STATE );
1106 operation->has_input = 1;
1107
1108 switch( operation->alg )
1109 {
1110#if defined(MBEDTLS_CMAC_C)
1111 case PSA_ALG_CMAC:
1112 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1113 input, input_length );
1114 break;
1115#endif /* MBEDTLS_CMAC_C */
1116 default:
1117#if defined(MBEDTLS_MD_C)
1118 if( PSA_ALG_IS_HMAC( operation->alg ) )
1119 {
1120 ret = mbedtls_md_hmac_update( &operation->ctx.hmac,
1121 input, input_length );
1122 }
1123 else
1124#endif /* MBEDTLS_MD_C */
1125 {
1126 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1127 }
1128 break;
1129 }
1130 if( ret != 0 )
1131 psa_mac_abort( operation );
1132 return( mbedtls_to_psa_error( ret ) );
1133}
1134
mohammad16036df908f2018-04-02 08:34:15 -07001135static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01001136 uint8_t *mac,
1137 size_t mac_size,
1138 size_t *mac_length )
1139{
1140 int ret;
1141 if( ! operation->key_set )
1142 return( PSA_ERROR_BAD_STATE );
1143 if( operation->iv_required && ! operation->iv_set )
1144 return( PSA_ERROR_BAD_STATE );
1145
1146 /* Fill the output buffer with something that isn't a valid mac
1147 * (barring an attack on the mac and deliberately-crafted input),
1148 * in case the caller doesn't check the return status properly. */
1149 *mac_length = operation->mac_size;
1150 memset( mac, '!', mac_size );
1151
1152 if( mac_size < operation->mac_size )
1153 return( PSA_ERROR_BUFFER_TOO_SMALL );
1154
1155 switch( operation->alg )
1156 {
1157#if defined(MBEDTLS_CMAC_C)
1158 case PSA_ALG_CMAC:
1159 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1160 break;
1161#endif /* MBEDTLS_CMAC_C */
1162 default:
1163#if defined(MBEDTLS_MD_C)
1164 if( PSA_ALG_IS_HMAC( operation->alg ) )
1165 {
1166 ret = mbedtls_md_hmac_finish( &operation->ctx.hmac, mac );
1167 }
1168 else
1169#endif /* MBEDTLS_MD_C */
1170 {
1171 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1172 }
1173 break;
1174 }
1175
1176 if( ret == 0 )
1177 {
1178 return( psa_mac_abort( operation ) );
1179 }
1180 else
1181 {
1182 psa_mac_abort( operation );
1183 return( mbedtls_to_psa_error( ret ) );
1184 }
1185}
1186
mohammad16036df908f2018-04-02 08:34:15 -07001187psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1188 uint8_t *mac,
1189 size_t mac_size,
1190 size_t *mac_length )
1191{
1192 if( !( operation->key_usage_sign ) )
1193 return( PSA_ERROR_NOT_PERMITTED );
1194
1195 return( psa_mac_finish_internal(operation, mac, mac_size, mac_length ) );
1196}
1197
Gilles Peskine8c9def32018-02-08 10:02:12 +01001198#define MBEDTLS_PSA_MAC_MAX_SIZE \
1199 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1200 MBEDTLS_MD_MAX_SIZE : \
1201 MBEDTLS_MAX_BLOCK_LENGTH )
1202psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1203 const uint8_t *mac,
1204 size_t mac_length )
1205{
1206 uint8_t actual_mac[MBEDTLS_PSA_MAC_MAX_SIZE];
1207 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001208 psa_status_t status;
1209
1210 if( !( operation->key_usage_verify ) )
1211 return( PSA_ERROR_NOT_PERMITTED );
1212
1213 status = psa_mac_finish_internal( operation,
1214 actual_mac, sizeof( actual_mac ),
1215 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001216 if( status != PSA_SUCCESS )
1217 return( status );
1218 if( actual_mac_length != mac_length )
1219 return( PSA_ERROR_INVALID_SIGNATURE );
1220 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1221 return( PSA_ERROR_INVALID_SIGNATURE );
1222 return( PSA_SUCCESS );
1223}
1224
1225
Gilles Peskine20035e32018-02-03 22:44:14 +01001226
1227
1228/****************************************************************/
1229/* Asymmetric cryptography */
1230/****************************************************************/
1231
1232psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1233 psa_algorithm_t alg,
1234 const uint8_t *hash,
1235 size_t hash_length,
1236 const uint8_t *salt,
1237 size_t salt_length,
1238 uint8_t *signature,
1239 size_t signature_size,
1240 size_t *signature_length)
1241{
1242 key_slot_t *slot;
1243
Gilles Peskine93aa0332018-02-03 23:58:03 +01001244 *signature_length = 0;
1245 (void) salt;
1246 (void) salt_length;
1247
Gilles Peskine20035e32018-02-03 22:44:14 +01001248 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1249 return( PSA_ERROR_EMPTY_SLOT );
1250 slot = &global_data.key_slots[key];
1251 if( slot->type == PSA_KEY_TYPE_NONE )
1252 return( PSA_ERROR_EMPTY_SLOT );
1253 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1254 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001255 if( !( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
1256 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001257
Gilles Peskine20035e32018-02-03 22:44:14 +01001258#if defined(MBEDTLS_RSA_C)
1259 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1260 {
1261 mbedtls_rsa_context *rsa = slot->data.rsa;
1262 int ret;
1263 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001264 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001265 mbedtls_md_type_t md_alg =
1266 hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1267 if( md_alg == MBEDTLS_MD_NONE )
1268 {
1269#if SIZE_MAX > UINT_MAX
1270 if( hash_length > UINT_MAX )
1271 return( PSA_ERROR_INVALID_ARGUMENT );
1272#endif
1273 }
1274 else
1275 {
1276 if( mbedtls_md_get_size( md_info ) != hash_length )
1277 return( PSA_ERROR_INVALID_ARGUMENT );
1278 if( md_info == NULL )
1279 return( PSA_ERROR_NOT_SUPPORTED );
1280 }
1281 if( signature_size < rsa->len )
1282 return( PSA_ERROR_BUFFER_TOO_SMALL );
1283#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001284 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001285 {
1286 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1287 MBEDTLS_MD_NONE );
1288 ret = mbedtls_rsa_pkcs1_sign( rsa,
1289 mbedtls_ctr_drbg_random,
1290 &global_data.ctr_drbg,
1291 MBEDTLS_RSA_PRIVATE,
1292 md_alg, hash_length, hash,
1293 signature );
1294 }
1295 else
1296#endif /* MBEDTLS_PKCS1_V15 */
1297#if defined(MBEDTLS_PKCS1_V21)
1298 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1299 {
1300 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1301 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1302 mbedtls_ctr_drbg_random,
1303 &global_data.ctr_drbg,
1304 MBEDTLS_RSA_PRIVATE,
1305 md_alg, hash_length, hash,
1306 signature );
1307 }
1308 else
1309#endif /* MBEDTLS_PKCS1_V21 */
1310 {
1311 return( PSA_ERROR_INVALID_ARGUMENT );
1312 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001313 if( ret == 0 )
1314 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001315 return( mbedtls_to_psa_error( ret ) );
1316 }
1317 else
1318#endif /* defined(MBEDTLS_RSA_C) */
1319#if defined(MBEDTLS_ECP_C)
1320 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1321 {
itayzafrir5c753392018-05-08 11:18:38 +03001322 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1323 int ret;
1324 const mbedtls_md_info_t *md_info;
1325 mbedtls_md_type_t md_alg;
1326 if( signature_size < PSA_ECDSA_SIGNATURE_SIZE( ecdsa->grp.pbits ) )
1327 return( PSA_ERROR_BUFFER_TOO_SMALL );
1328 md_info = mbedtls_md_info_from_psa( alg );
1329 md_alg = mbedtls_md_get_type( md_info );
1330 ret = mbedtls_ecdsa_write_signature( ecdsa, md_alg, hash, hash_length,
1331 signature, signature_length, mbedtls_ctr_drbg_random,
1332 &global_data.ctr_drbg );
1333 return( mbedtls_to_psa_error( ret ) );
1334 }
1335 else
1336#endif /* defined(MBEDTLS_ECP_C) */
1337 {
Gilles Peskine20035e32018-02-03 22:44:14 +01001338 return( PSA_ERROR_NOT_SUPPORTED );
1339 }
itayzafrir5c753392018-05-08 11:18:38 +03001340}
1341
1342psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1343 psa_algorithm_t alg,
1344 const uint8_t *hash,
1345 size_t hash_length,
1346 const uint8_t *salt,
1347 size_t salt_length,
1348 uint8_t *signature,
1349 size_t signature_size )
1350{
1351 key_slot_t *slot;
1352 (void) salt;
1353 (void) salt_length;
1354
1355 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1356 return( PSA_ERROR_INVALID_ARGUMENT );
1357 slot = &global_data.key_slots[key];
1358 if( slot->type == PSA_KEY_TYPE_NONE )
1359 return( PSA_ERROR_EMPTY_SLOT );
1360 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1361 return( PSA_ERROR_INVALID_ARGUMENT );
1362 if( !( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
1363 return( PSA_ERROR_NOT_PERMITTED );
1364
1365#if defined(MBEDTLS_ECP_C)
1366 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1367 {
1368 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1369 int ret;
1370 (void) alg;
1371 ret = mbedtls_ecdsa_read_signature( ecdsa, hash, hash_length, signature,
1372 signature_size );
1373 return( mbedtls_to_psa_error( ret ) );
1374 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001375 else
1376#endif /* defined(MBEDTLS_ECP_C) */
1377 {
1378 return( PSA_ERROR_NOT_SUPPORTED );
1379 }
1380}
1381
1382
mohammad1603503973b2018-03-12 15:59:30 +02001383/****************************************************************/
1384/* Symmetric cryptography */
1385/****************************************************************/
1386
Gilles Peskinee553c652018-06-04 16:22:46 +02001387static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
1388 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02001389 psa_algorithm_t alg,
1390 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02001391{
1392 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1393 psa_status_t status;
1394 key_slot_t *slot;
1395 psa_key_type_t key_type;
1396 size_t key_bits;
1397 const mbedtls_cipher_info_t *cipher_info = NULL;
1398
Moran Peker41deec42018-04-04 15:43:05 +03001399 operation->alg = alg;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001400 operation->key_set = 0;
1401 operation->iv_set = 0;
1402 operation->iv_required = 1;
1403 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001404 operation->block_size = 0;
mohammad1603503973b2018-03-12 15:59:30 +02001405
1406 status = psa_get_key_information( key, &key_type, &key_bits );
1407 if( status != PSA_SUCCESS )
1408 return( status );
1409 slot = &global_data.key_slots[key];
1410
1411 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits );
1412 if( cipher_info == NULL )
1413 return( PSA_ERROR_NOT_SUPPORTED );
1414
mohammad1603503973b2018-03-12 15:59:30 +02001415 mbedtls_cipher_init( &operation->ctx.cipher );
1416 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03001417 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001418 {
1419 psa_cipher_abort( operation );
1420 return( mbedtls_to_psa_error( ret ) );
1421 }
1422
1423 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data,
Gilles Peskinee553c652018-06-04 16:22:46 +02001424 key_bits, cipher_operation );
Moran Peker41deec42018-04-04 15:43:05 +03001425 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001426 {
1427 psa_cipher_abort( operation );
1428 return( mbedtls_to_psa_error( ret ) );
1429 }
1430
mohammad16038481e742018-03-18 13:57:31 +02001431#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03001432 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02001433 {
Gilles Peskine53514202018-06-06 15:11:46 +02001434 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1435 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02001436
Moran Pekera28258c2018-05-29 16:25:04 +03001437 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02001438 {
1439 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
1440 mode = MBEDTLS_PADDING_PKCS7;
1441 break;
1442 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
1443 mode = MBEDTLS_PADDING_NONE;
1444 break;
1445 default:
Moran Pekerae382792018-05-31 14:06:17 +03001446 psa_cipher_abort( operation );
1447 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02001448 }
1449 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03001450 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03001451 {
1452 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02001453 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03001454 }
mohammad16038481e742018-03-18 13:57:31 +02001455 }
1456#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
1457
mohammad1603503973b2018-03-12 15:59:30 +02001458 operation->key_set = 1;
1459 operation->alg = alg;
Gilles Peskine7e928852018-06-04 16:23:10 +02001460 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
1461 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
1462 1 );
Moran Peker41deec42018-04-04 15:43:05 +03001463 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || ( alg == PSA_ALG_CTR ) )
mohammad16038481e742018-03-18 13:57:31 +02001464 {
mohammad160389e0f462018-04-12 08:48:45 +03001465 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02001466 }
mohammad1603503973b2018-03-12 15:59:30 +02001467
Moran Peker395db872018-05-31 14:07:14 +03001468 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001469}
1470
Gilles Peskinee553c652018-06-04 16:22:46 +02001471psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
1472 psa_key_slot_t key,
1473 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02001474{
Moran Pekera28258c2018-05-29 16:25:04 +03001475 return psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT );
mohammad16038481e742018-03-18 13:57:31 +02001476}
1477
Gilles Peskinee553c652018-06-04 16:22:46 +02001478psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
1479 psa_key_slot_t key,
1480 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02001481{
Moran Pekera28258c2018-05-29 16:25:04 +03001482 return psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT );
mohammad16038481e742018-03-18 13:57:31 +02001483}
1484
Gilles Peskinee553c652018-06-04 16:22:46 +02001485psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
1486 unsigned char *iv,
1487 size_t iv_size,
1488 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02001489{
Moran Peker41deec42018-04-04 15:43:05 +03001490 int ret = PSA_SUCCESS;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001491 if( operation->iv_set || !( operation->iv_required ) )
Moran Peker41deec42018-04-04 15:43:05 +03001492 return( PSA_ERROR_BAD_STATE );
1493 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02001494 {
Moran Peker41deec42018-04-04 15:43:05 +03001495 ret = PSA_ERROR_BUFFER_TOO_SMALL;
1496 goto exit;
1497 }
Gilles Peskine7e928852018-06-04 16:23:10 +02001498 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
1499 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03001500 if( ret != 0 )
1501 {
1502 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02001503 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02001504 }
Gilles Peskinee553c652018-06-04 16:22:46 +02001505
mohammad16038481e742018-03-18 13:57:31 +02001506 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03001507 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03001508
Moran Peker395db872018-05-31 14:07:14 +03001509exit:
1510 if( ret != PSA_SUCCESS )
1511 psa_cipher_abort( operation );
1512 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02001513}
1514
Gilles Peskinee553c652018-06-04 16:22:46 +02001515psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
1516 const unsigned char *iv,
1517 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02001518{
Moran Peker41deec42018-04-04 15:43:05 +03001519 int ret = PSA_SUCCESS;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001520 if( operation->iv_set || !( operation->iv_required ) )
Moran Peker41deec42018-04-04 15:43:05 +03001521 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03001522 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03001523 {
Moran Pekerae382792018-05-31 14:06:17 +03001524 psa_cipher_abort( operation );
1525 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03001526 }
mohammad1603503973b2018-03-12 15:59:30 +02001527 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03001528 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001529 {
1530 psa_cipher_abort( operation );
1531 return( mbedtls_to_psa_error( ret ) );
1532 }
1533
1534 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02001535
Moran Peker395db872018-05-31 14:07:14 +03001536 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001537}
1538
Gilles Peskinee553c652018-06-04 16:22:46 +02001539psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
1540 const uint8_t *input,
1541 size_t input_length,
1542 unsigned char *output,
1543 size_t output_size,
1544 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02001545{
1546 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02001547 size_t expected_output_size;
1548 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
1549 {
1550 /* Take the unprocessed partial block left over from previous
1551 * update calls, if any, plus the input to this call. Remove
1552 * the last partial block, if any. You get the data that will be
1553 * output in this call. */
1554 expected_output_size =
1555 ( operation->ctx.cipher.unprocessed_len + input_length )
1556 / operation->block_size * operation->block_size;
1557 }
1558 else
1559 {
1560 expected_output_size = input_length;
1561 }
1562 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03001563 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02001564
mohammad1603503973b2018-03-12 15:59:30 +02001565 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02001566 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03001567 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001568 {
1569 psa_cipher_abort( operation );
1570 return( mbedtls_to_psa_error( ret ) );
1571 }
1572
Moran Peker395db872018-05-31 14:07:14 +03001573 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001574}
1575
Gilles Peskinee553c652018-06-04 16:22:46 +02001576psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
1577 uint8_t *output,
1578 size_t output_size,
1579 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02001580{
1581 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02001582 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03001583
mohammad1603503973b2018-03-12 15:59:30 +02001584 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03001585 {
Moran Peker7cb22b82018-06-05 11:40:02 +03001586 psa_cipher_abort( operation );
1587 return( PSA_ERROR_BAD_STATE );
1588 }
1589 if( operation->iv_required && ! operation->iv_set )
1590 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001591 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03001592 return( PSA_ERROR_BAD_STATE );
1593 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001594 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
1595 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03001596 {
Gilles Peskine53514202018-06-06 15:11:46 +02001597 psa_algorithm_t padding_mode =
1598 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03001599 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02001600 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001601 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03001602 return( PSA_ERROR_TAMPERING_DETECTED );
1603 }
Gilles Peskine53514202018-06-06 15:11:46 +02001604 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03001605 {
1606 if( operation->ctx.cipher.unprocessed_len != 0 )
1607 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001608 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03001609 return( PSA_ERROR_INVALID_ARGUMENT );
1610 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02001611 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03001612 }
1613
1614 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02001615 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03001616 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001617 {
1618 psa_cipher_abort( operation );
1619 return( mbedtls_to_psa_error( ret ) );
1620 }
Gilles Peskinee553c652018-06-04 16:22:46 +02001621 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03001622 memcpy( output, temp_output_buffer, *output_length );
1623 else
1624 {
1625 psa_cipher_abort( operation );
1626 return( PSA_ERROR_BUFFER_TOO_SMALL );
1627 }
mohammad1603503973b2018-03-12 15:59:30 +02001628
Moran Peker4c80d832018-04-22 20:15:31 +03001629 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001630}
1631
Gilles Peskinee553c652018-06-04 16:22:46 +02001632psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
1633{
mohammad1603503973b2018-03-12 15:59:30 +02001634 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02001635
Moran Peker41deec42018-04-04 15:43:05 +03001636 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001637 operation->key_set = 0;
1638 operation->iv_set = 0;
1639 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001640 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001641 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001642
Moran Peker395db872018-05-31 14:07:14 +03001643 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001644}
1645
Gilles Peskinea0655c32018-04-30 17:06:50 +02001646
mohammad16038cc1cee2018-03-28 01:21:33 +03001647/****************************************************************/
1648/* Key Policy */
1649/****************************************************************/
1650
1651void psa_key_policy_init(psa_key_policy_t *policy)
1652{
mohammad16036df908f2018-04-02 08:34:15 -07001653 memset( policy, 0, sizeof( psa_key_policy_t ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03001654}
1655
1656void psa_key_policy_set_usage(psa_key_policy_t *policy,
1657 psa_key_usage_t usage,
1658 psa_algorithm_t alg)
1659{
mohammad16034eed7572018-03-28 05:14:59 -07001660 policy->usage = usage;
1661 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03001662}
1663
1664psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy)
1665{
mohammad16036df908f2018-04-02 08:34:15 -07001666 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03001667}
1668
1669psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy)
1670{
mohammad16036df908f2018-04-02 08:34:15 -07001671 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03001672}
1673
1674psa_status_t psa_set_key_policy(psa_key_slot_t key,
1675 const psa_key_policy_t *policy)
1676{
1677 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03001678
1679 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
1680 return( PSA_ERROR_INVALID_ARGUMENT );
1681
1682 slot = &global_data.key_slots[key];
1683 if( slot->type != PSA_KEY_TYPE_NONE )
1684 return( PSA_ERROR_OCCUPIED_SLOT );
1685
mohammad16036df908f2018-04-02 08:34:15 -07001686 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_ENCRYPT
1687 | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN
1688 | PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07001689 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03001690
mohammad16036df908f2018-04-02 08:34:15 -07001691 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03001692
1693 return( PSA_SUCCESS );
1694}
1695
1696psa_status_t psa_get_key_policy(psa_key_slot_t key,
1697 psa_key_policy_t *policy)
1698{
1699 key_slot_t *slot;
1700
1701 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
1702 return( PSA_ERROR_INVALID_ARGUMENT );
1703
1704 slot = &global_data.key_slots[key];
mohammad16038cc1cee2018-03-28 01:21:33 +03001705
mohammad16036df908f2018-04-02 08:34:15 -07001706 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03001707
1708 return( PSA_SUCCESS );
1709}
Gilles Peskine20035e32018-02-03 22:44:14 +01001710
Gilles Peskinea0655c32018-04-30 17:06:50 +02001711
1712
mohammad1603804cd712018-03-20 22:44:08 +02001713/****************************************************************/
1714/* Key Lifetime */
1715/****************************************************************/
1716
1717psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1718 psa_key_lifetime_t *lifetime)
1719{
1720 key_slot_t *slot;
1721
1722 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1723 return( PSA_ERROR_INVALID_ARGUMENT );
1724
1725 slot = &global_data.key_slots[key];
mohammad1603804cd712018-03-20 22:44:08 +02001726
1727 *lifetime = slot->lifetime;
1728
1729 return( PSA_SUCCESS );
1730}
1731
1732psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
1733 const psa_key_lifetime_t lifetime)
1734{
1735 key_slot_t *slot;
1736
1737 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1738 return( PSA_ERROR_INVALID_ARGUMENT );
1739
mohammad1603ba178512018-03-21 04:35:20 -07001740 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
1741 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
1742 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
1743 return( PSA_ERROR_INVALID_ARGUMENT );
1744
mohammad1603804cd712018-03-20 22:44:08 +02001745 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03001746 if( slot->type != PSA_KEY_TYPE_NONE )
1747 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02001748
Moran Pekerd7326592018-05-29 16:56:39 +03001749 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07001750 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603804cd712018-03-20 22:44:08 +02001751
mohammad1603060ad8a2018-03-20 14:28:38 -07001752 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02001753
1754 return( PSA_SUCCESS );
1755}
1756
Gilles Peskine20035e32018-02-03 22:44:14 +01001757
Gilles Peskinea0655c32018-04-30 17:06:50 +02001758
Gilles Peskine20035e32018-02-03 22:44:14 +01001759/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001760/* Module setup */
1761/****************************************************************/
1762
Gilles Peskinee59236f2018-01-27 23:32:46 +01001763void mbedtls_psa_crypto_free( void )
1764{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001765 size_t key;
1766 for( key = 1; key < MBEDTLS_PSA_KEY_SLOT_COUNT; key++ )
1767 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001768 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
1769 mbedtls_entropy_free( &global_data.entropy );
1770 mbedtls_zeroize( &global_data, sizeof( global_data ) );
1771}
1772
1773psa_status_t psa_crypto_init( void )
1774{
1775 int ret;
1776 const unsigned char drbg_seed[] = "PSA";
1777
1778 if( global_data.initialized != 0 )
1779 return( PSA_SUCCESS );
1780
1781 mbedtls_zeroize( &global_data, sizeof( global_data ) );
1782 mbedtls_entropy_init( &global_data.entropy );
1783 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
1784
1785 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
1786 mbedtls_entropy_func,
1787 &global_data.entropy,
1788 drbg_seed, sizeof( drbg_seed ) - 1 );
1789 if( ret != 0 )
1790 goto exit;
1791
Gilles Peskinee4ebc122018-03-07 14:16:44 +01001792 global_data.initialized = 1;
1793
Gilles Peskinee59236f2018-01-27 23:32:46 +01001794exit:
1795 if( ret != 0 )
1796 mbedtls_psa_crypto_free( );
1797 return( mbedtls_to_psa_error( ret ) );
1798}
1799
1800#endif /* MBEDTLS_PSA_CRYPTO_C */