blob: 81da8cef01e606c12b400cf361ab7336b29870c4 [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;
99 union {
100 struct raw_data {
101 uint8_t *data;
102 size_t bytes;
103 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100104#if defined(MBEDTLS_RSA_C)
105 mbedtls_rsa_context *rsa;
106#endif /* MBEDTLS_RSA_C */
107#if defined(MBEDTLS_ECP_C)
108 mbedtls_ecp_keypair *ecp;
109#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100110 } data;
111} key_slot_t;
112
Gilles Peskinee59236f2018-01-27 23:32:46 +0100113typedef struct {
114 int initialized;
115 mbedtls_entropy_context entropy;
116 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100117 key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
122static psa_status_t mbedtls_to_psa_error( int ret )
123{
Gilles Peskinea5905292018-02-07 20:59:33 +0100124 /* If there's both a high-level code and low-level code, dispatch on
125 * the high-level code. */
126 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100127 {
128 case 0:
129 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100130
131 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
132 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
133 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
134 return( PSA_ERROR_NOT_SUPPORTED );
135 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
136 return( PSA_ERROR_HARDWARE_FAILURE );
137
138 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
139 return( PSA_ERROR_HARDWARE_FAILURE );
140
141 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
142 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
148 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
149 return( PSA_ERROR_NOT_SUPPORTED );
150 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
151 return( PSA_ERROR_HARDWARE_FAILURE );
152
153 case MBEDTLS_ERR_CCM_BAD_INPUT:
154 return( PSA_ERROR_INVALID_ARGUMENT );
155 case MBEDTLS_ERR_CCM_AUTH_FAILED:
156 return( PSA_ERROR_INVALID_SIGNATURE );
157 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
158 return( PSA_ERROR_HARDWARE_FAILURE );
159
160 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
161 return( PSA_ERROR_NOT_SUPPORTED );
162 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
163 return( PSA_ERROR_INVALID_ARGUMENT );
164 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
165 return( PSA_ERROR_INSUFFICIENT_MEMORY );
166 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
167 return( PSA_ERROR_INVALID_PADDING );
168 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
169 return( PSA_ERROR_BAD_STATE );
170 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
171 return( PSA_ERROR_INVALID_SIGNATURE );
172 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
173 return( PSA_ERROR_TAMPERING_DETECTED );
174 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
175 return( PSA_ERROR_HARDWARE_FAILURE );
176
177 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
181 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
182 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
183 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
186 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
187
188 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
189 return( PSA_ERROR_NOT_SUPPORTED );
190 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
191 return( PSA_ERROR_HARDWARE_FAILURE );
192
Gilles Peskinee59236f2018-01-27 23:32:46 +0100193 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
194 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
195 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
196 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100197
198 case MBEDTLS_ERR_GCM_AUTH_FAILED:
199 return( PSA_ERROR_INVALID_SIGNATURE );
200 case MBEDTLS_ERR_GCM_BAD_INPUT:
201 return( PSA_ERROR_NOT_SUPPORTED );
202 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
203 return( PSA_ERROR_HARDWARE_FAILURE );
204
205 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
206 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
207 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
210 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
211 return( PSA_ERROR_NOT_SUPPORTED );
212 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
213 return( PSA_ERROR_INVALID_ARGUMENT );
214 case MBEDTLS_ERR_MD_ALLOC_FAILED:
215 return( PSA_ERROR_INSUFFICIENT_MEMORY );
216 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
217 return( PSA_ERROR_STORAGE_FAILURE );
218 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100221 case MBEDTLS_ERR_PK_ALLOC_FAILED:
222 return( PSA_ERROR_INSUFFICIENT_MEMORY );
223 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
224 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
225 return( PSA_ERROR_INVALID_ARGUMENT );
226 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100228 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
229 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
230 return( PSA_ERROR_INVALID_ARGUMENT );
231 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
232 return( PSA_ERROR_NOT_SUPPORTED );
233 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
234 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
235 return( PSA_ERROR_NOT_PERMITTED );
236 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
237 return( PSA_ERROR_INVALID_ARGUMENT );
238 case MBEDTLS_ERR_PK_INVALID_ALG:
239 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
240 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
241 return( PSA_ERROR_NOT_SUPPORTED );
242 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
243 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100244 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
247 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
248 return( PSA_ERROR_HARDWARE_FAILURE );
249
250 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
251 return( PSA_ERROR_INVALID_ARGUMENT );
252 case MBEDTLS_ERR_RSA_INVALID_PADDING:
253 return( PSA_ERROR_INVALID_PADDING );
254 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
259 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
260 return( PSA_ERROR_TAMPERING_DETECTED );
261 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
262 return( PSA_ERROR_INVALID_SIGNATURE );
263 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
264 return( PSA_ERROR_BUFFER_TOO_SMALL );
265 case MBEDTLS_ERR_RSA_RNG_FAILED:
266 return( PSA_ERROR_INSUFFICIENT_MEMORY );
267 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
268 return( PSA_ERROR_NOT_SUPPORTED );
269 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
270 return( PSA_ERROR_HARDWARE_FAILURE );
271
272 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
273 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
274 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
275 return( PSA_ERROR_HARDWARE_FAILURE );
276
277 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
280 return( PSA_ERROR_HARDWARE_FAILURE );
281
Gilles Peskinee59236f2018-01-27 23:32:46 +0100282 default:
283 return( PSA_ERROR_UNKNOWN_ERROR );
284 }
285}
286
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100287
288
289/****************************************************************/
290/* Key management */
291/****************************************************************/
292
293psa_status_t psa_import_key(psa_key_slot_t key,
294 psa_key_type_t type,
295 const uint8_t *data,
296 size_t data_length)
297{
298 key_slot_t *slot;
299
300 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
301 return( PSA_ERROR_INVALID_ARGUMENT );
302 slot = &global_data.key_slots[key];
303 if( slot->type != PSA_KEY_TYPE_NONE )
304 return( PSA_ERROR_OCCUPIED_SLOT );
305
Gilles Peskine8c9def32018-02-08 10:02:12 +0100306 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100307 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100308 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 if( data_length > SIZE_MAX / 8 )
310 return( PSA_ERROR_NOT_SUPPORTED );
311 slot->data.raw.data = mbedtls_calloc( 1, data_length );
312 if( slot->data.raw.data == NULL )
313 return( PSA_ERROR_INSUFFICIENT_MEMORY );
314 memcpy( slot->data.raw.data, data, data_length );
315 slot->data.raw.bytes = data_length;
316 }
317 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100318#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100319 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
320 type == PSA_KEY_TYPE_RSA_KEYPAIR ||
321 PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100322 {
323 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100324 mbedtls_pk_context pk;
325 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100326 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
327 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
328 else
329 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100330 if( ret != 0 )
331 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100332 switch( mbedtls_pk_get_type( &pk ) )
333 {
334#if defined(MBEDTLS_RSA_C)
335 case MBEDTLS_PK_RSA:
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100336 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
337 type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine969ac722018-01-28 18:16:59 +0100338 slot->data.rsa = pk.pk_ctx;
339 else
340 return( PSA_ERROR_INVALID_ARGUMENT );
341 break;
342#endif /* MBEDTLS_RSA_C */
343#if defined(MBEDTLS_ECP_C)
344 case MBEDTLS_PK_ECKEY:
345 if( PSA_KEY_TYPE_IS_ECC( type ) )
346 {
347 // TODO: check curve
348 slot->data.ecp = pk.pk_ctx;
349 }
350 else
351 return( PSA_ERROR_INVALID_ARGUMENT );
352 break;
353#endif /* MBEDTLS_ECP_C */
354 default:
355 return( PSA_ERROR_INVALID_ARGUMENT );
356 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357 }
358 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100359#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100360 {
361 return( PSA_ERROR_NOT_SUPPORTED );
362 }
363
364 slot->type = type;
365 return( PSA_SUCCESS );
366}
367
368psa_status_t psa_destroy_key(psa_key_slot_t key)
369{
370 key_slot_t *slot;
371
372 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
373 return( PSA_ERROR_INVALID_ARGUMENT );
374 slot = &global_data.key_slots[key];
375 if( slot->type == PSA_KEY_TYPE_NONE )
376 return( PSA_ERROR_EMPTY_SLOT );
377
Gilles Peskine8c9def32018-02-08 10:02:12 +0100378 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100379 {
380 mbedtls_free( slot->data.raw.data );
381 }
382 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100383#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100384 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
385 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100387 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100388 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100389 }
390 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100391#endif /* defined(MBEDTLS_RSA_C) */
392#if defined(MBEDTLS_ECP_C)
393 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
394 {
395 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100396 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100397 }
398 else
399#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100400 {
401 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100402 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100403 return( PSA_ERROR_TAMPERING_DETECTED );
404 }
405
406 mbedtls_zeroize( slot, sizeof( *slot ) );
407 return( PSA_SUCCESS );
408}
409
410psa_status_t psa_get_key_information(psa_key_slot_t key,
411 psa_key_type_t *type,
412 size_t *bits)
413{
414 key_slot_t *slot;
415
416 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100417 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100418 slot = &global_data.key_slots[key];
419 if( type != NULL )
420 *type = slot->type;
421 if( bits != NULL )
422 *bits = 0;
423 if( slot->type == PSA_KEY_TYPE_NONE )
424 return( PSA_ERROR_EMPTY_SLOT );
425
Gilles Peskine8c9def32018-02-08 10:02:12 +0100426 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100427 {
428 if( bits != NULL )
429 *bits = slot->data.raw.bytes * 8;
430 }
431 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100432#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100433 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
434 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100435 {
436 if( bits != NULL )
Gilles Peskine969ac722018-01-28 18:16:59 +0100437 *bits = mbedtls_rsa_get_bitlen( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100438 }
439 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100440#endif /* defined(MBEDTLS_RSA_C) */
441#if defined(MBEDTLS_ECP_C)
442 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
443 {
444 if( bits != NULL )
445 *bits = slot->data.ecp->grp.pbits;
446 }
447 else
448#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100449 {
450 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100451 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100452 return( PSA_ERROR_TAMPERING_DETECTED );
453 }
454
455 return( PSA_SUCCESS );
456}
457
458psa_status_t psa_export_key(psa_key_slot_t key,
459 uint8_t *data,
460 size_t data_size,
461 size_t *data_length)
462{
463 key_slot_t *slot;
464
465 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100466 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100467 slot = &global_data.key_slots[key];
468 if( slot->type == PSA_KEY_TYPE_NONE )
469 return( PSA_ERROR_EMPTY_SLOT );
470
Gilles Peskine8c9def32018-02-08 10:02:12 +0100471 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100472 {
473 if( slot->data.raw.bytes > data_size )
474 return( PSA_ERROR_BUFFER_TOO_SMALL );
475 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
476 *data_length = slot->data.raw.bytes;
477 return( PSA_SUCCESS );
478 }
479 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100480#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100481 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
482 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100483 PSA_KEY_TYPE_IS_ECC( slot->type ) )
484 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100485 mbedtls_pk_context pk;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100486 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100487 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100488 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
489 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine969ac722018-01-28 18:16:59 +0100490 {
491 pk.pk_info = &mbedtls_rsa_info;
492 pk.pk_ctx = slot->data.rsa;
493 }
494 else
495 {
496 pk.pk_info = &mbedtls_eckey_info;
497 pk.pk_ctx = slot->data.ecp;
498 }
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100499 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
500 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
501 else
502 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100503 if( ret < 0 )
504 return( mbedtls_to_psa_error( ret ) );
505 *data_length = ret;
506 return( PSA_SUCCESS );
507 }
508 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100509#endif /* defined(MBEDTLS_PK_WRITE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100511 /* This shouldn't happen in the reference implementation, but
512 it is valid for a special-purpose implementation to omit
513 support for exporting certain key types. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100514 return( PSA_ERROR_NOT_SUPPORTED );
515 }
516}
517
518
519
520/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100521/* Message digests */
522/****************************************************************/
523
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100524static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100525{
526 switch( alg )
527 {
528#if defined(MBEDTLS_MD2_C)
529 case PSA_ALG_MD2:
530 return( &mbedtls_md2_info );
531#endif
532#if defined(MBEDTLS_MD4_C)
533 case PSA_ALG_MD4:
534 return( &mbedtls_md4_info );
535#endif
536#if defined(MBEDTLS_MD5_C)
537 case PSA_ALG_MD5:
538 return( &mbedtls_md5_info );
539#endif
540#if defined(MBEDTLS_RIPEMD160_C)
541 case PSA_ALG_RIPEMD160:
542 return( &mbedtls_ripemd160_info );
543#endif
544#if defined(MBEDTLS_SHA1_C)
545 case PSA_ALG_SHA_1:
546 return( &mbedtls_sha1_info );
547#endif
548#if defined(MBEDTLS_SHA256_C)
549 case PSA_ALG_SHA_224:
550 return( &mbedtls_sha224_info );
551 case PSA_ALG_SHA_256:
552 return( &mbedtls_sha256_info );
553#endif
554#if defined(MBEDTLS_SHA512_C)
555 case PSA_ALG_SHA_384:
556 return( &mbedtls_sha384_info );
557 case PSA_ALG_SHA_512:
558 return( &mbedtls_sha512_info );
559#endif
560 default:
561 return( NULL );
562 }
563}
564
565#if 0
566static psa_algorithm_t mbedtls_md_alg_to_psa( mbedtls_md_type_t md_alg )
567{
568 switch( md_alg )
569 {
570 case MBEDTLS_MD_NONE:
571 return( 0 );
572 case MBEDTLS_MD_MD2:
573 return( PSA_ALG_MD2 );
574 case MBEDTLS_MD_MD4:
575 return( PSA_ALG_MD4 );
576 case MBEDTLS_MD_MD5:
577 return( PSA_ALG_MD5 );
578 case MBEDTLS_MD_SHA1:
579 return( PSA_ALG_SHA_1 );
580 case MBEDTLS_MD_SHA224:
581 return( PSA_ALG_SHA_224 );
582 case MBEDTLS_MD_SHA256:
583 return( PSA_ALG_SHA_256 );
584 case MBEDTLS_MD_SHA384:
585 return( PSA_ALG_SHA_384 );
586 case MBEDTLS_MD_SHA512:
587 return( PSA_ALG_SHA_512 );
588 case MBEDTLS_MD_RIPEMD160:
589 return( PSA_ALG_RIPEMD160 );
590 default:
591 return( MBEDTLS_MD_NOT_SUPPORTED );
592 }
593}
594#endif
595
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100596psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
597{
598 switch( operation->alg )
599 {
600#if defined(MBEDTLS_MD2_C)
601 case PSA_ALG_MD2:
602 mbedtls_md2_free( &operation->ctx.md2 );
603 break;
604#endif
605#if defined(MBEDTLS_MD4_C)
606 case PSA_ALG_MD4:
607 mbedtls_md4_free( &operation->ctx.md4 );
608 break;
609#endif
610#if defined(MBEDTLS_MD5_C)
611 case PSA_ALG_MD5:
612 mbedtls_md5_free( &operation->ctx.md5 );
613 break;
614#endif
615#if defined(MBEDTLS_RIPEMD160_C)
616 case PSA_ALG_RIPEMD160:
617 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
618 break;
619#endif
620#if defined(MBEDTLS_SHA1_C)
621 case PSA_ALG_SHA_1:
622 mbedtls_sha1_free( &operation->ctx.sha1 );
623 break;
624#endif
625#if defined(MBEDTLS_SHA256_C)
626 case PSA_ALG_SHA_224:
627 case PSA_ALG_SHA_256:
628 mbedtls_sha256_free( &operation->ctx.sha256 );
629 break;
630#endif
631#if defined(MBEDTLS_SHA512_C)
632 case PSA_ALG_SHA_384:
633 case PSA_ALG_SHA_512:
634 mbedtls_sha512_free( &operation->ctx.sha512 );
635 break;
636#endif
637 default:
638 return( PSA_ERROR_NOT_SUPPORTED );
639 }
640 operation->alg = 0;
641 return( PSA_SUCCESS );
642}
643
644psa_status_t psa_hash_start( psa_hash_operation_t *operation,
645 psa_algorithm_t alg )
646{
647 int ret;
648 operation->alg = 0;
649 switch( alg )
650 {
651#if defined(MBEDTLS_MD2_C)
652 case PSA_ALG_MD2:
653 mbedtls_md2_init( &operation->ctx.md2 );
654 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
655 break;
656#endif
657#if defined(MBEDTLS_MD4_C)
658 case PSA_ALG_MD4:
659 mbedtls_md4_init( &operation->ctx.md4 );
660 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
661 break;
662#endif
663#if defined(MBEDTLS_MD5_C)
664 case PSA_ALG_MD5:
665 mbedtls_md5_init( &operation->ctx.md5 );
666 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
667 break;
668#endif
669#if defined(MBEDTLS_RIPEMD160_C)
670 case PSA_ALG_RIPEMD160:
671 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
672 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
673 break;
674#endif
675#if defined(MBEDTLS_SHA1_C)
676 case PSA_ALG_SHA_1:
677 mbedtls_sha1_init( &operation->ctx.sha1 );
678 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
679 break;
680#endif
681#if defined(MBEDTLS_SHA256_C)
682 case PSA_ALG_SHA_224:
683 mbedtls_sha256_init( &operation->ctx.sha256 );
684 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
685 break;
686 case PSA_ALG_SHA_256:
687 mbedtls_sha256_init( &operation->ctx.sha256 );
688 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
689 break;
690#endif
691#if defined(MBEDTLS_SHA512_C)
692 case PSA_ALG_SHA_384:
693 mbedtls_sha512_init( &operation->ctx.sha512 );
694 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
695 break;
696 case PSA_ALG_SHA_512:
697 mbedtls_sha512_init( &operation->ctx.sha512 );
698 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
699 break;
700#endif
701 default:
702 return( PSA_ERROR_NOT_SUPPORTED );
703 }
704 if( ret == 0 )
705 operation->alg = alg;
706 else
707 psa_hash_abort( operation );
708 return( mbedtls_to_psa_error( ret ) );
709}
710
711psa_status_t psa_hash_update( psa_hash_operation_t *operation,
712 const uint8_t *input,
713 size_t input_length )
714{
715 int ret;
716 switch( operation->alg )
717 {
718#if defined(MBEDTLS_MD2_C)
719 case PSA_ALG_MD2:
720 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
721 input, input_length );
722 break;
723#endif
724#if defined(MBEDTLS_MD4_C)
725 case PSA_ALG_MD4:
726 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
727 input, input_length );
728 break;
729#endif
730#if defined(MBEDTLS_MD5_C)
731 case PSA_ALG_MD5:
732 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
733 input, input_length );
734 break;
735#endif
736#if defined(MBEDTLS_RIPEMD160_C)
737 case PSA_ALG_RIPEMD160:
738 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
739 input, input_length );
740 break;
741#endif
742#if defined(MBEDTLS_SHA1_C)
743 case PSA_ALG_SHA_1:
744 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
745 input, input_length );
746 break;
747#endif
748#if defined(MBEDTLS_SHA256_C)
749 case PSA_ALG_SHA_224:
750 case PSA_ALG_SHA_256:
751 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
752 input, input_length );
753 break;
754#endif
755#if defined(MBEDTLS_SHA512_C)
756 case PSA_ALG_SHA_384:
757 case PSA_ALG_SHA_512:
758 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
759 input, input_length );
760 break;
761#endif
762 default:
763 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
764 break;
765 }
766 if( ret != 0 )
767 psa_hash_abort( operation );
768 return( mbedtls_to_psa_error( ret ) );
769}
770
771psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
772 uint8_t *hash,
773 size_t hash_size,
774 size_t *hash_length )
775{
776 int ret;
777 size_t actual_hash_length = PSA_HASH_FINAL_SIZE( operation->alg );
778
779 /* Fill the output buffer with something that isn't a valid hash
780 * (barring an attack on the hash and deliberately-crafted input),
781 * in case the caller doesn't check the return status properly. */
782 *hash_length = actual_hash_length;
783 memset( hash, '!', hash_size );
784
785 if( hash_size < actual_hash_length )
786 return( PSA_ERROR_BUFFER_TOO_SMALL );
787
788 switch( operation->alg )
789 {
790#if defined(MBEDTLS_MD2_C)
791 case PSA_ALG_MD2:
792 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
793 break;
794#endif
795#if defined(MBEDTLS_MD4_C)
796 case PSA_ALG_MD4:
797 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
798 break;
799#endif
800#if defined(MBEDTLS_MD5_C)
801 case PSA_ALG_MD5:
802 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
803 break;
804#endif
805#if defined(MBEDTLS_RIPEMD160_C)
806 case PSA_ALG_RIPEMD160:
807 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
808 break;
809#endif
810#if defined(MBEDTLS_SHA1_C)
811 case PSA_ALG_SHA_1:
812 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
813 break;
814#endif
815#if defined(MBEDTLS_SHA256_C)
816 case PSA_ALG_SHA_224:
817 case PSA_ALG_SHA_256:
818 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
819 break;
820#endif
821#if defined(MBEDTLS_SHA512_C)
822 case PSA_ALG_SHA_384:
823 case PSA_ALG_SHA_512:
824 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
825 break;
826#endif
827 default:
828 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
829 break;
830 }
831
832 if( ret == 0 )
833 {
834 return( psa_hash_abort( operation ) );
835 }
836 else
837 {
838 psa_hash_abort( operation );
839 return( mbedtls_to_psa_error( ret ) );
840 }
841}
842
843psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
844 const uint8_t *hash,
845 size_t hash_length)
846{
847 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
848 size_t actual_hash_length;
849 psa_status_t status = psa_hash_finish( operation,
850 actual_hash, sizeof( actual_hash ),
851 &actual_hash_length );
852 if( status != PSA_SUCCESS )
853 return( status );
854 if( actual_hash_length != hash_length )
855 return( PSA_ERROR_INVALID_SIGNATURE );
856 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
857 return( PSA_ERROR_INVALID_SIGNATURE );
858 return( PSA_SUCCESS );
859}
860
861
862
863
864/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +0100865/* MAC */
866/****************************************************************/
867
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100868static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +0100869 psa_algorithm_t alg,
870 psa_key_type_t key_type,
871 size_t key_bits )
872{
873 mbedtls_cipher_id_t cipher_id;
874 mbedtls_cipher_mode_t mode;
875
876 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
877 {
878 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
879 alg &= ~PSA_ALG_BLOCK_CIPHER_MODE_MASK;
880 switch( alg )
881 {
882 case PSA_ALG_STREAM_CIPHER:
883 mode = MBEDTLS_MODE_STREAM;
884 break;
885 case PSA_ALG_CBC_BASE:
886 mode = MBEDTLS_MODE_CBC;
887 break;
888 case PSA_ALG_CFB_BASE:
889 mode = MBEDTLS_MODE_CFB;
890 break;
891 case PSA_ALG_OFB_BASE:
892 mode = MBEDTLS_MODE_OFB;
893 break;
894 case PSA_ALG_CTR:
895 mode = MBEDTLS_MODE_CTR;
896 break;
897 case PSA_ALG_CCM:
898 mode = MBEDTLS_MODE_CCM;
899 break;
900 case PSA_ALG_GCM:
901 mode = MBEDTLS_MODE_GCM;
902 break;
903 default:
904 return( NULL );
905 }
906 }
907 else if( alg == PSA_ALG_CMAC )
908 mode = MBEDTLS_MODE_ECB;
909 else if( alg == PSA_ALG_GMAC )
910 mode = MBEDTLS_MODE_GCM;
911 else
912 return( NULL );
913
914 switch( key_type )
915 {
916 case PSA_KEY_TYPE_AES:
917 cipher_id = MBEDTLS_CIPHER_ID_AES;
918 break;
919 case PSA_KEY_TYPE_DES:
920 if( key_bits == 64 )
921 cipher_id = MBEDTLS_CIPHER_ID_DES;
922 else
923 cipher_id = MBEDTLS_CIPHER_ID_3DES;
924 break;
925 case PSA_KEY_TYPE_CAMELLIA:
926 cipher_id = MBEDTLS_CIPHER_ID_CAMELLIA;
927 break;
928 case PSA_KEY_TYPE_ARC4:
929 cipher_id = MBEDTLS_CIPHER_ID_ARC4;
930 break;
931 default:
932 return( NULL );
933 }
934
935 return( mbedtls_cipher_info_from_values( cipher_id, key_bits, mode ) );
936}
937
938psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
939{
940 switch( operation->alg )
941 {
942#if defined(MBEDTLS_CMAC_C)
943 case PSA_ALG_CMAC:
944 mbedtls_cipher_free( &operation->ctx.cmac );
945 break;
946#endif /* MBEDTLS_CMAC_C */
947 default:
948#if defined(MBEDTLS_MD_C)
949 if( PSA_ALG_IS_HMAC( operation->alg ) )
950 mbedtls_md_free( &operation->ctx.hmac );
951 else
952#endif /* MBEDTLS_MD_C */
953 return( PSA_ERROR_NOT_SUPPORTED );
954 }
955 operation->alg = 0;
956 operation->key_set = 0;
957 operation->iv_set = 0;
958 operation->iv_required = 0;
959 operation->has_input = 0;
960 return( PSA_SUCCESS );
961}
962
963psa_status_t psa_mac_start( psa_mac_operation_t *operation,
964 psa_key_slot_t key,
965 psa_algorithm_t alg )
966{
967 int ret = MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
968 psa_status_t status;
969 key_slot_t *slot;
970 psa_key_type_t key_type;
971 size_t key_bits;
972 const mbedtls_cipher_info_t *cipher_info = NULL;
973
974 operation->alg = 0;
975 operation->key_set = 0;
976 operation->iv_set = 0;
977 operation->iv_required = 1;
978 operation->has_input = 0;
979
980 status = psa_get_key_information( key, &key_type, &key_bits );
981 if( status != PSA_SUCCESS )
982 return( status );
983 slot = &global_data.key_slots[key];
984
985 if( ! PSA_ALG_IS_HMAC( alg ) )
986 {
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100987 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100988 if( cipher_info == NULL )
989 return( PSA_ERROR_NOT_SUPPORTED );
990 operation->mac_size = cipher_info->block_size;
991 }
992 switch( alg )
993 {
994#if defined(MBEDTLS_CMAC_C)
995 case PSA_ALG_CMAC:
996 operation->iv_required = 0;
997 mbedtls_cipher_init( &operation->ctx.cmac );
998 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
999 if( ret != 0 )
1000 break;
1001 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1002 slot->data.raw.data,
1003 key_bits );
1004 break;
1005#endif /* MBEDTLS_CMAC_C */
1006 default:
1007#if defined(MBEDTLS_MD_C)
1008 if( PSA_ALG_IS_HMAC( alg ) )
1009 {
1010 const mbedtls_md_info_t *md_info =
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001011 mbedtls_md_info_from_psa( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001012 if( md_info == NULL )
1013 return( PSA_ERROR_NOT_SUPPORTED );
1014 if( key_type != PSA_KEY_TYPE_HMAC )
1015 return( PSA_ERROR_INVALID_ARGUMENT );
1016 operation->iv_required = 0;
1017 operation->mac_size = mbedtls_md_get_size( md_info );
1018 mbedtls_md_init( &operation->ctx.hmac );
1019 ret = mbedtls_md_setup( &operation->ctx.hmac, md_info, 1 );
1020 if( ret != 0 )
1021 break;
1022 ret = mbedtls_md_hmac_starts( &operation->ctx.hmac,
1023 slot->data.raw.data,
1024 slot->data.raw.bytes );
1025 break;
1026 }
1027 else
1028#endif /* MBEDTLS_MD_C */
1029 return( PSA_ERROR_NOT_SUPPORTED );
1030 }
1031
1032 /* If we reach this point, then the algorithm-specific part of the
1033 * context has at least been initialized, and may contain data that
1034 * needs to be wiped on error. */
1035 operation->alg = alg;
1036 if( ret != 0 )
1037 {
1038 psa_mac_abort( operation );
1039 return( mbedtls_to_psa_error( ret ) );
1040 }
1041 operation->key_set = 1;
1042 return( 0 );
1043}
1044
1045psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1046 const uint8_t *input,
1047 size_t input_length )
1048{
1049 int ret;
1050 if( ! operation->key_set )
1051 return( PSA_ERROR_BAD_STATE );
1052 if( operation->iv_required && ! operation->iv_set )
1053 return( PSA_ERROR_BAD_STATE );
1054 operation->has_input = 1;
1055
1056 switch( operation->alg )
1057 {
1058#if defined(MBEDTLS_CMAC_C)
1059 case PSA_ALG_CMAC:
1060 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1061 input, input_length );
1062 break;
1063#endif /* MBEDTLS_CMAC_C */
1064 default:
1065#if defined(MBEDTLS_MD_C)
1066 if( PSA_ALG_IS_HMAC( operation->alg ) )
1067 {
1068 ret = mbedtls_md_hmac_update( &operation->ctx.hmac,
1069 input, input_length );
1070 }
1071 else
1072#endif /* MBEDTLS_MD_C */
1073 {
1074 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1075 }
1076 break;
1077 }
1078 if( ret != 0 )
1079 psa_mac_abort( operation );
1080 return( mbedtls_to_psa_error( ret ) );
1081}
1082
1083psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1084 uint8_t *mac,
1085 size_t mac_size,
1086 size_t *mac_length )
1087{
1088 int ret;
1089 if( ! operation->key_set )
1090 return( PSA_ERROR_BAD_STATE );
1091 if( operation->iv_required && ! operation->iv_set )
1092 return( PSA_ERROR_BAD_STATE );
1093
1094 /* Fill the output buffer with something that isn't a valid mac
1095 * (barring an attack on the mac and deliberately-crafted input),
1096 * in case the caller doesn't check the return status properly. */
1097 *mac_length = operation->mac_size;
1098 memset( mac, '!', mac_size );
1099
1100 if( mac_size < operation->mac_size )
1101 return( PSA_ERROR_BUFFER_TOO_SMALL );
1102
1103 switch( operation->alg )
1104 {
1105#if defined(MBEDTLS_CMAC_C)
1106 case PSA_ALG_CMAC:
1107 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1108 break;
1109#endif /* MBEDTLS_CMAC_C */
1110 default:
1111#if defined(MBEDTLS_MD_C)
1112 if( PSA_ALG_IS_HMAC( operation->alg ) )
1113 {
1114 ret = mbedtls_md_hmac_finish( &operation->ctx.hmac, mac );
1115 }
1116 else
1117#endif /* MBEDTLS_MD_C */
1118 {
1119 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1120 }
1121 break;
1122 }
1123
1124 if( ret == 0 )
1125 {
1126 return( psa_mac_abort( operation ) );
1127 }
1128 else
1129 {
1130 psa_mac_abort( operation );
1131 return( mbedtls_to_psa_error( ret ) );
1132 }
1133}
1134
1135#define MBEDTLS_PSA_MAC_MAX_SIZE \
1136 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1137 MBEDTLS_MD_MAX_SIZE : \
1138 MBEDTLS_MAX_BLOCK_LENGTH )
1139psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1140 const uint8_t *mac,
1141 size_t mac_length )
1142{
1143 uint8_t actual_mac[MBEDTLS_PSA_MAC_MAX_SIZE];
1144 size_t actual_mac_length;
1145 psa_status_t status = psa_mac_finish( operation,
1146 actual_mac, sizeof( actual_mac ),
1147 &actual_mac_length );
1148 if( status != PSA_SUCCESS )
1149 return( status );
1150 if( actual_mac_length != mac_length )
1151 return( PSA_ERROR_INVALID_SIGNATURE );
1152 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1153 return( PSA_ERROR_INVALID_SIGNATURE );
1154 return( PSA_SUCCESS );
1155}
1156
1157
Gilles Peskine20035e32018-02-03 22:44:14 +01001158
1159
1160/****************************************************************/
1161/* Asymmetric cryptography */
1162/****************************************************************/
1163
1164psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1165 psa_algorithm_t alg,
1166 const uint8_t *hash,
1167 size_t hash_length,
1168 const uint8_t *salt,
1169 size_t salt_length,
1170 uint8_t *signature,
1171 size_t signature_size,
1172 size_t *signature_length)
1173{
1174 key_slot_t *slot;
1175
Gilles Peskine93aa0332018-02-03 23:58:03 +01001176 *signature_length = 0;
1177 (void) salt;
1178 (void) salt_length;
1179
Gilles Peskine20035e32018-02-03 22:44:14 +01001180 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1181 return( PSA_ERROR_EMPTY_SLOT );
1182 slot = &global_data.key_slots[key];
1183 if( slot->type == PSA_KEY_TYPE_NONE )
1184 return( PSA_ERROR_EMPTY_SLOT );
1185 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1186 return( PSA_ERROR_INVALID_ARGUMENT );
1187
Gilles Peskine20035e32018-02-03 22:44:14 +01001188#if defined(MBEDTLS_RSA_C)
1189 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1190 {
1191 mbedtls_rsa_context *rsa = slot->data.rsa;
1192 int ret;
1193 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001194 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001195 mbedtls_md_type_t md_alg =
1196 hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1197 if( md_alg == MBEDTLS_MD_NONE )
1198 {
1199#if SIZE_MAX > UINT_MAX
1200 if( hash_length > UINT_MAX )
1201 return( PSA_ERROR_INVALID_ARGUMENT );
1202#endif
1203 }
1204 else
1205 {
1206 if( mbedtls_md_get_size( md_info ) != hash_length )
1207 return( PSA_ERROR_INVALID_ARGUMENT );
1208 if( md_info == NULL )
1209 return( PSA_ERROR_NOT_SUPPORTED );
1210 }
1211 if( signature_size < rsa->len )
1212 return( PSA_ERROR_BUFFER_TOO_SMALL );
1213#if defined(MBEDTLS_PKCS1_V15)
1214 if( PSA_ALG_IS_RSA_PKCS1V15( alg ) )
1215 {
1216 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1217 MBEDTLS_MD_NONE );
1218 ret = mbedtls_rsa_pkcs1_sign( rsa,
1219 mbedtls_ctr_drbg_random,
1220 &global_data.ctr_drbg,
1221 MBEDTLS_RSA_PRIVATE,
1222 md_alg, hash_length, hash,
1223 signature );
1224 }
1225 else
1226#endif /* MBEDTLS_PKCS1_V15 */
1227#if defined(MBEDTLS_PKCS1_V21)
1228 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1229 {
1230 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1231 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1232 mbedtls_ctr_drbg_random,
1233 &global_data.ctr_drbg,
1234 MBEDTLS_RSA_PRIVATE,
1235 md_alg, hash_length, hash,
1236 signature );
1237 }
1238 else
1239#endif /* MBEDTLS_PKCS1_V21 */
1240 {
1241 return( PSA_ERROR_INVALID_ARGUMENT );
1242 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001243 if( ret == 0 )
1244 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001245 return( mbedtls_to_psa_error( ret ) );
1246 }
1247 else
1248#endif /* defined(MBEDTLS_RSA_C) */
1249#if defined(MBEDTLS_ECP_C)
1250 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1251 {
1252 // TODO
1253 return( PSA_ERROR_NOT_SUPPORTED );
1254 }
1255 else
1256#endif /* defined(MBEDTLS_ECP_C) */
1257 {
1258 return( PSA_ERROR_NOT_SUPPORTED );
1259 }
1260}
1261
1262
1263
1264/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001265/* Module setup */
1266/****************************************************************/
1267
Gilles Peskinee59236f2018-01-27 23:32:46 +01001268void mbedtls_psa_crypto_free( void )
1269{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001270 size_t key;
1271 for( key = 1; key < MBEDTLS_PSA_KEY_SLOT_COUNT; key++ )
1272 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001273 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
1274 mbedtls_entropy_free( &global_data.entropy );
1275 mbedtls_zeroize( &global_data, sizeof( global_data ) );
1276}
1277
1278psa_status_t psa_crypto_init( void )
1279{
1280 int ret;
1281 const unsigned char drbg_seed[] = "PSA";
1282
1283 if( global_data.initialized != 0 )
1284 return( PSA_SUCCESS );
1285
1286 mbedtls_zeroize( &global_data, sizeof( global_data ) );
1287 mbedtls_entropy_init( &global_data.entropy );
1288 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
1289
1290 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
1291 mbedtls_entropy_func,
1292 &global_data.entropy,
1293 drbg_seed, sizeof( drbg_seed ) - 1 );
1294 if( ret != 0 )
1295 goto exit;
1296
Gilles Peskinee4ebc122018-03-07 14:16:44 +01001297 global_data.initialized = 1;
1298
Gilles Peskinee59236f2018-01-27 23:32:46 +01001299exit:
1300 if( ret != 0 )
1301 mbedtls_psa_crypto_free( );
1302 return( mbedtls_to_psa_error( ret ) );
1303}
1304
1305#endif /* MBEDTLS_PSA_CRYPTO_C */