blob: 4a256988be8a056159d34f91b8d472bb33560fc5 [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 )
Gilles Peskine154bd952018-04-19 08:38:16 +0200391 {
392 /* No key material to clean, but do zeroize the slot below to wipe
393 * metadata such as policies. */
394 }
395 else if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100396 {
397 mbedtls_free( slot->data.raw.data );
398 }
399 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100400#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100401 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
402 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100403 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100404 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100405 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100406 }
407 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100408#endif /* defined(MBEDTLS_RSA_C) */
409#if defined(MBEDTLS_ECP_C)
410 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
411 {
412 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100413 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100414 }
415 else
416#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100417 {
418 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100419 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100420 return( PSA_ERROR_TAMPERING_DETECTED );
421 }
422
423 mbedtls_zeroize( slot, sizeof( *slot ) );
424 return( PSA_SUCCESS );
425}
426
427psa_status_t psa_get_key_information(psa_key_slot_t key,
428 psa_key_type_t *type,
429 size_t *bits)
430{
431 key_slot_t *slot;
432
433 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100434 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100435 slot = &global_data.key_slots[key];
436 if( type != NULL )
437 *type = slot->type;
438 if( bits != NULL )
439 *bits = 0;
440 if( slot->type == PSA_KEY_TYPE_NONE )
441 return( PSA_ERROR_EMPTY_SLOT );
442
Gilles Peskine8c9def32018-02-08 10:02:12 +0100443 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100444 {
445 if( bits != NULL )
446 *bits = slot->data.raw.bytes * 8;
447 }
448 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100449#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100450 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
451 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100452 {
453 if( bits != NULL )
Gilles Peskine969ac722018-01-28 18:16:59 +0100454 *bits = mbedtls_rsa_get_bitlen( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100455 }
456 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100457#endif /* defined(MBEDTLS_RSA_C) */
458#if defined(MBEDTLS_ECP_C)
459 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
460 {
461 if( bits != NULL )
462 *bits = slot->data.ecp->grp.pbits;
463 }
464 else
465#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100466 {
467 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100468 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100469 return( PSA_ERROR_TAMPERING_DETECTED );
470 }
471
472 return( PSA_SUCCESS );
473}
474
Moran Pekera998bc62018-04-16 18:16:20 +0300475static psa_status_t psa_internal_export_key(psa_key_slot_t key,
476 uint8_t *data,
477 size_t data_size,
478 size_t *data_length,
479 int export_public_key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100480{
481 key_slot_t *slot;
482
483 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100484 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100485 slot = &global_data.key_slots[key];
486 if( slot->type == PSA_KEY_TYPE_NONE )
487 return( PSA_ERROR_EMPTY_SLOT );
488
Moran Peker87567632018-06-04 18:41:37 +0300489 if( export_public_key && ( !( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) ) ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300490 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300491
Moran Peker87567632018-06-04 18:41:37 +0300492 if( ( !export_public_key ) && ( !( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ) ) &&
493 ( !( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) ) )
494 return( PSA_ERROR_NOT_PERMITTED );
495
Gilles Peskine8c9def32018-02-08 10:02:12 +0100496 if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 {
498 if( slot->data.raw.bytes > data_size )
499 return( PSA_ERROR_BUFFER_TOO_SMALL );
500 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
501 *data_length = slot->data.raw.bytes;
502 return( PSA_SUCCESS );
503 }
504 else
Moran Peker17e36e12018-05-02 12:55:20 +0300505 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100506#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100507 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
Moran Pekera998bc62018-04-16 18:16:20 +0300508 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
509 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100510 {
Moran Pekera998bc62018-04-16 18:16:20 +0300511 mbedtls_pk_context pk;
512 int ret;
513 mbedtls_pk_init( &pk );
514 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
515 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
516 {
517 pk.pk_info = &mbedtls_rsa_info;
518 pk.pk_ctx = slot->data.rsa;
519 }
520 else
521 {
522 pk.pk_info = &mbedtls_eckey_info;
523 pk.pk_ctx = slot->data.ecp;
524 }
Moran Pekerd7326592018-05-29 16:56:39 +0300525 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300526 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300527 else
528 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300529 if( ret < 0 )
Moran Pekera998bc62018-04-16 18:16:20 +0300530 return( mbedtls_to_psa_error( ret ) );
531 *data_length = ret;
532 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100533 }
534 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100535#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300536 {
537 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200538 it is valid for a special-purpose implementation to omit
539 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300540 return( PSA_ERROR_NOT_SUPPORTED );
541 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 }
543}
544
Moran Pekera998bc62018-04-16 18:16:20 +0300545psa_status_t psa_export_key(psa_key_slot_t key,
546 uint8_t *data,
547 size_t data_size,
548 size_t *data_length)
549{
550 return psa_internal_export_key( key, data, data_size,
551 data_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553
554
Moran Pekerdd4ea382018-04-03 15:30:03 +0300555psa_status_t psa_export_public_key(psa_key_slot_t key,
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300556 uint8_t *data,
557 size_t data_size,
558 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +0300559{
Moran Pekera998bc62018-04-16 18:16:20 +0300560 return psa_internal_export_key( key, data, data_size,
561 data_length, 1 );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300562}
563
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100565/* Message digests */
566/****************************************************************/
567
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100568static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100569{
570 switch( alg )
571 {
572#if defined(MBEDTLS_MD2_C)
573 case PSA_ALG_MD2:
574 return( &mbedtls_md2_info );
575#endif
576#if defined(MBEDTLS_MD4_C)
577 case PSA_ALG_MD4:
578 return( &mbedtls_md4_info );
579#endif
580#if defined(MBEDTLS_MD5_C)
581 case PSA_ALG_MD5:
582 return( &mbedtls_md5_info );
583#endif
584#if defined(MBEDTLS_RIPEMD160_C)
585 case PSA_ALG_RIPEMD160:
586 return( &mbedtls_ripemd160_info );
587#endif
588#if defined(MBEDTLS_SHA1_C)
589 case PSA_ALG_SHA_1:
590 return( &mbedtls_sha1_info );
591#endif
592#if defined(MBEDTLS_SHA256_C)
593 case PSA_ALG_SHA_224:
594 return( &mbedtls_sha224_info );
595 case PSA_ALG_SHA_256:
596 return( &mbedtls_sha256_info );
597#endif
598#if defined(MBEDTLS_SHA512_C)
599 case PSA_ALG_SHA_384:
600 return( &mbedtls_sha384_info );
601 case PSA_ALG_SHA_512:
602 return( &mbedtls_sha512_info );
603#endif
604 default:
605 return( NULL );
606 }
607}
608
609#if 0
610static psa_algorithm_t mbedtls_md_alg_to_psa( mbedtls_md_type_t md_alg )
611{
612 switch( md_alg )
613 {
614 case MBEDTLS_MD_NONE:
615 return( 0 );
616 case MBEDTLS_MD_MD2:
617 return( PSA_ALG_MD2 );
618 case MBEDTLS_MD_MD4:
619 return( PSA_ALG_MD4 );
620 case MBEDTLS_MD_MD5:
621 return( PSA_ALG_MD5 );
622 case MBEDTLS_MD_SHA1:
623 return( PSA_ALG_SHA_1 );
624 case MBEDTLS_MD_SHA224:
625 return( PSA_ALG_SHA_224 );
626 case MBEDTLS_MD_SHA256:
627 return( PSA_ALG_SHA_256 );
628 case MBEDTLS_MD_SHA384:
629 return( PSA_ALG_SHA_384 );
630 case MBEDTLS_MD_SHA512:
631 return( PSA_ALG_SHA_512 );
632 case MBEDTLS_MD_RIPEMD160:
633 return( PSA_ALG_RIPEMD160 );
634 default:
Gilles Peskine47c1bc02018-03-20 17:55:04 +0100635 return( 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +0100636 }
637}
638#endif
639
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100640psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
641{
642 switch( operation->alg )
643 {
644#if defined(MBEDTLS_MD2_C)
645 case PSA_ALG_MD2:
646 mbedtls_md2_free( &operation->ctx.md2 );
647 break;
648#endif
649#if defined(MBEDTLS_MD4_C)
650 case PSA_ALG_MD4:
651 mbedtls_md4_free( &operation->ctx.md4 );
652 break;
653#endif
654#if defined(MBEDTLS_MD5_C)
655 case PSA_ALG_MD5:
656 mbedtls_md5_free( &operation->ctx.md5 );
657 break;
658#endif
659#if defined(MBEDTLS_RIPEMD160_C)
660 case PSA_ALG_RIPEMD160:
661 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
662 break;
663#endif
664#if defined(MBEDTLS_SHA1_C)
665 case PSA_ALG_SHA_1:
666 mbedtls_sha1_free( &operation->ctx.sha1 );
667 break;
668#endif
669#if defined(MBEDTLS_SHA256_C)
670 case PSA_ALG_SHA_224:
671 case PSA_ALG_SHA_256:
672 mbedtls_sha256_free( &operation->ctx.sha256 );
673 break;
674#endif
675#if defined(MBEDTLS_SHA512_C)
676 case PSA_ALG_SHA_384:
677 case PSA_ALG_SHA_512:
678 mbedtls_sha512_free( &operation->ctx.sha512 );
679 break;
680#endif
681 default:
682 return( PSA_ERROR_NOT_SUPPORTED );
683 }
684 operation->alg = 0;
685 return( PSA_SUCCESS );
686}
687
688psa_status_t psa_hash_start( psa_hash_operation_t *operation,
689 psa_algorithm_t alg )
690{
691 int ret;
692 operation->alg = 0;
693 switch( alg )
694 {
695#if defined(MBEDTLS_MD2_C)
696 case PSA_ALG_MD2:
697 mbedtls_md2_init( &operation->ctx.md2 );
698 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
699 break;
700#endif
701#if defined(MBEDTLS_MD4_C)
702 case PSA_ALG_MD4:
703 mbedtls_md4_init( &operation->ctx.md4 );
704 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
705 break;
706#endif
707#if defined(MBEDTLS_MD5_C)
708 case PSA_ALG_MD5:
709 mbedtls_md5_init( &operation->ctx.md5 );
710 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
711 break;
712#endif
713#if defined(MBEDTLS_RIPEMD160_C)
714 case PSA_ALG_RIPEMD160:
715 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
716 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
717 break;
718#endif
719#if defined(MBEDTLS_SHA1_C)
720 case PSA_ALG_SHA_1:
721 mbedtls_sha1_init( &operation->ctx.sha1 );
722 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
723 break;
724#endif
725#if defined(MBEDTLS_SHA256_C)
726 case PSA_ALG_SHA_224:
727 mbedtls_sha256_init( &operation->ctx.sha256 );
728 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
729 break;
730 case PSA_ALG_SHA_256:
731 mbedtls_sha256_init( &operation->ctx.sha256 );
732 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
733 break;
734#endif
735#if defined(MBEDTLS_SHA512_C)
736 case PSA_ALG_SHA_384:
737 mbedtls_sha512_init( &operation->ctx.sha512 );
738 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
739 break;
740 case PSA_ALG_SHA_512:
741 mbedtls_sha512_init( &operation->ctx.sha512 );
742 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
743 break;
744#endif
745 default:
746 return( PSA_ERROR_NOT_SUPPORTED );
747 }
748 if( ret == 0 )
749 operation->alg = alg;
750 else
751 psa_hash_abort( operation );
752 return( mbedtls_to_psa_error( ret ) );
753}
754
755psa_status_t psa_hash_update( psa_hash_operation_t *operation,
756 const uint8_t *input,
757 size_t input_length )
758{
759 int ret;
760 switch( operation->alg )
761 {
762#if defined(MBEDTLS_MD2_C)
763 case PSA_ALG_MD2:
764 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
765 input, input_length );
766 break;
767#endif
768#if defined(MBEDTLS_MD4_C)
769 case PSA_ALG_MD4:
770 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
771 input, input_length );
772 break;
773#endif
774#if defined(MBEDTLS_MD5_C)
775 case PSA_ALG_MD5:
776 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
777 input, input_length );
778 break;
779#endif
780#if defined(MBEDTLS_RIPEMD160_C)
781 case PSA_ALG_RIPEMD160:
782 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
783 input, input_length );
784 break;
785#endif
786#if defined(MBEDTLS_SHA1_C)
787 case PSA_ALG_SHA_1:
788 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
789 input, input_length );
790 break;
791#endif
792#if defined(MBEDTLS_SHA256_C)
793 case PSA_ALG_SHA_224:
794 case PSA_ALG_SHA_256:
795 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
796 input, input_length );
797 break;
798#endif
799#if defined(MBEDTLS_SHA512_C)
800 case PSA_ALG_SHA_384:
801 case PSA_ALG_SHA_512:
802 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
803 input, input_length );
804 break;
805#endif
806 default:
807 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
808 break;
809 }
810 if( ret != 0 )
811 psa_hash_abort( operation );
812 return( mbedtls_to_psa_error( ret ) );
813}
814
815psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
816 uint8_t *hash,
817 size_t hash_size,
818 size_t *hash_length )
819{
820 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200821 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100822
823 /* Fill the output buffer with something that isn't a valid hash
824 * (barring an attack on the hash and deliberately-crafted input),
825 * in case the caller doesn't check the return status properly. */
826 *hash_length = actual_hash_length;
827 memset( hash, '!', hash_size );
828
829 if( hash_size < actual_hash_length )
830 return( PSA_ERROR_BUFFER_TOO_SMALL );
831
832 switch( operation->alg )
833 {
834#if defined(MBEDTLS_MD2_C)
835 case PSA_ALG_MD2:
836 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
837 break;
838#endif
839#if defined(MBEDTLS_MD4_C)
840 case PSA_ALG_MD4:
841 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
842 break;
843#endif
844#if defined(MBEDTLS_MD5_C)
845 case PSA_ALG_MD5:
846 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
847 break;
848#endif
849#if defined(MBEDTLS_RIPEMD160_C)
850 case PSA_ALG_RIPEMD160:
851 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
852 break;
853#endif
854#if defined(MBEDTLS_SHA1_C)
855 case PSA_ALG_SHA_1:
856 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
857 break;
858#endif
859#if defined(MBEDTLS_SHA256_C)
860 case PSA_ALG_SHA_224:
861 case PSA_ALG_SHA_256:
862 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
863 break;
864#endif
865#if defined(MBEDTLS_SHA512_C)
866 case PSA_ALG_SHA_384:
867 case PSA_ALG_SHA_512:
868 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
869 break;
870#endif
871 default:
872 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
873 break;
874 }
875
876 if( ret == 0 )
877 {
878 return( psa_hash_abort( operation ) );
879 }
880 else
881 {
882 psa_hash_abort( operation );
883 return( mbedtls_to_psa_error( ret ) );
884 }
885}
886
887psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
888 const uint8_t *hash,
889 size_t hash_length)
890{
891 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
892 size_t actual_hash_length;
893 psa_status_t status = psa_hash_finish( operation,
894 actual_hash, sizeof( actual_hash ),
895 &actual_hash_length );
896 if( status != PSA_SUCCESS )
897 return( status );
898 if( actual_hash_length != hash_length )
899 return( PSA_ERROR_INVALID_SIGNATURE );
900 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
901 return( PSA_ERROR_INVALID_SIGNATURE );
902 return( PSA_SUCCESS );
903}
904
905
906
907
908/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +0100909/* MAC */
910/****************************************************************/
911
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100912static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +0100913 psa_algorithm_t alg,
914 psa_key_type_t key_type,
mohammad1603f4f0d612018-06-03 15:04:51 +0300915 size_t key_bits,
916 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100917{
Gilles Peskine8c9def32018-02-08 10:02:12 +0100918 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +0300919 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100920
921 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
922 {
923 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +0200924 {
925 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
926 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +0200927
Nir Sonnenscheine9664c32018-06-17 14:41:30 +0300928 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100929 {
930 case PSA_ALG_STREAM_CIPHER:
931 mode = MBEDTLS_MODE_STREAM;
932 break;
933 case PSA_ALG_CBC_BASE:
934 mode = MBEDTLS_MODE_CBC;
935 break;
936 case PSA_ALG_CFB_BASE:
937 mode = MBEDTLS_MODE_CFB;
938 break;
939 case PSA_ALG_OFB_BASE:
940 mode = MBEDTLS_MODE_OFB;
941 break;
942 case PSA_ALG_CTR:
943 mode = MBEDTLS_MODE_CTR;
944 break;
945 case PSA_ALG_CCM:
946 mode = MBEDTLS_MODE_CCM;
947 break;
948 case PSA_ALG_GCM:
949 mode = MBEDTLS_MODE_GCM;
950 break;
951 default:
952 return( NULL );
953 }
954 }
955 else if( alg == PSA_ALG_CMAC )
956 mode = MBEDTLS_MODE_ECB;
957 else if( alg == PSA_ALG_GMAC )
958 mode = MBEDTLS_MODE_GCM;
959 else
960 return( NULL );
961
962 switch( key_type )
963 {
964 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +0300965 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100966 break;
967 case PSA_KEY_TYPE_DES:
968 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +0300969 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100970 else
mohammad1603f4f0d612018-06-03 15:04:51 +0300971 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100972 break;
973 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +0300974 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100975 break;
976 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +0300977 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100978 break;
979 default:
980 return( NULL );
981 }
mohammad1603f4f0d612018-06-03 15:04:51 +0300982 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +0300983 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100984
mohammad1603f4f0d612018-06-03 15:04:51 +0300985 return( mbedtls_cipher_info_from_values( cipher_id_tmp, key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100986}
987
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +0300988static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +0300989{
990 switch(alg)
991 {
992 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +0300993 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +0300994 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +0300995 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +0300996 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +0300997 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +0300998 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +0300999 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001000 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001001 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001002 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001003 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001004 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001005 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001006 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001007 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001008 case PSA_ALG_SHA_512:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001009 return ( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001010 default:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001011 return ( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001012 }
1013}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001014
Gilles Peskine8c9def32018-02-08 10:02:12 +01001015psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1016{
1017 switch( operation->alg )
1018 {
1019#if defined(MBEDTLS_CMAC_C)
1020 case PSA_ALG_CMAC:
1021 mbedtls_cipher_free( &operation->ctx.cmac );
1022 break;
1023#endif /* MBEDTLS_CMAC_C */
1024 default:
1025#if defined(MBEDTLS_MD_C)
1026 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001027 {
Gilles Peskine99bc6492018-06-11 17:13:00 +02001028 unsigned int block_size =
Nir Sonnenschein96272412018-06-17 14:41:10 +03001029 psa_get_hash_block_size( ( PSA_ALG_HMAC_HASH( operation->alg ) ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001030
Gilles Peskine99bc6492018-06-11 17:13:00 +02001031 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001032 return( PSA_ERROR_NOT_SUPPORTED );
1033
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001034 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001035 mbedtls_zeroize( operation->ctx.hmac.opad,
Nir Sonnenschein35dfbf42018-06-07 16:20:17 +03001036 block_size);
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001037 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001038 else
1039#endif /* MBEDTLS_MD_C */
1040 return( PSA_ERROR_NOT_SUPPORTED );
1041 }
Moran Peker41deec42018-04-04 15:43:05 +03001042
Gilles Peskine8c9def32018-02-08 10:02:12 +01001043 operation->alg = 0;
1044 operation->key_set = 0;
1045 operation->iv_set = 0;
1046 operation->iv_required = 0;
1047 operation->has_input = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001048
Gilles Peskine8c9def32018-02-08 10:02:12 +01001049 return( PSA_SUCCESS );
1050}
1051
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001052static int psa_cmac_start( psa_mac_operation_t *operation,
1053 size_t key_bits,
1054 key_slot_t *slot,
1055 const mbedtls_cipher_info_t *cipher_info )
1056{
1057 int ret;
1058
1059 operation->mac_size = cipher_info->block_size;
1060 operation->iv_required = 0;
1061 mbedtls_cipher_init( &operation->ctx.cmac );
1062
1063 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1064 if( ret != 0 )
1065 return( ret );
1066
1067 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1068 slot->data.raw.data,
1069 key_bits );
1070 return( ret );
1071}
1072
1073static int psa_hmac_start( psa_mac_operation_t *operation,
1074 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001075 key_slot_t *slot,
1076 psa_algorithm_t alg )
1077{
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001078 unsigned char ipad[PSA_CRYPTO_MD_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001079 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001080 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001081 size_t block_size =
Nir Sonnenschein96272412018-06-17 14:41:10 +03001082 psa_get_hash_block_size( ( PSA_ALG_HMAC_HASH( alg ) ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001083 unsigned int digest_size =
Nir Sonnenscheincaec7f02018-06-14 15:34:50 +03001084 PSA_HASH_SIZE( ( PSA_ALG_HMAC_HASH( alg ) ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001085 size_t key_length = slot->data.raw.bytes;
1086 psa_status_t status;
1087
1088 if( ( block_size == 0 ) || ( digest_size == 0 ) )
1089 return( PSA_ERROR_NOT_SUPPORTED );
1090
1091 if( key_type != PSA_KEY_TYPE_HMAC )
1092 return( PSA_ERROR_INVALID_ARGUMENT );
1093
1094 operation->iv_required = 0;
1095 operation->mac_size = digest_size;
1096
1097 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1098 PSA_ALG_HMAC_HASH( alg ) );
1099 if( status != PSA_SUCCESS )
1100 return( status );
1101
Gilles Peskined223b522018-06-11 18:12:58 +02001102 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001103 {
1104 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001105 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001106 if( status != PSA_SUCCESS )
1107 return( status );
1108 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001109 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001110 if( status != PSA_SUCCESS )
1111 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001112 }
Gilles Peskined223b522018-06-11 18:12:58 +02001113 else
1114 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001115
Gilles Peskined223b522018-06-11 18:12:58 +02001116 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1117 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001118 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001119 ipad[i] ^= 0x36;
1120 memset( ipad + key_length, 0x36, block_size - key_length );
1121
1122 /* Copy the key material from ipad to opad, flipping the requisite bits,
1123 * and filling the rest of opad with the requisite constant. */
1124 for( i = 0; i < key_length; i++ )
1125 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1126 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001127
1128 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1129 PSA_ALG_HMAC_HASH( alg ) );
1130 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001131 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001132
1133 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1134 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001135
1136cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001137 mbedtls_zeroize( ipad, key_length );
1138 /* opad is in the context. It needs to stay in memory if this function
1139 * succeeds, and it will be wiped by psa_mac_abort() called from
1140 * psa_mac_start in the error case. */
1141
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001142 return( status );
1143}
1144
Gilles Peskine8c9def32018-02-08 10:02:12 +01001145psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1146 psa_key_slot_t key,
1147 psa_algorithm_t alg )
1148{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001149 psa_status_t status;
1150 key_slot_t *slot;
1151 psa_key_type_t key_type;
1152 size_t key_bits;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001153 const mbedtls_cipher_info_t *cipher_info;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001154
1155 operation->alg = 0;
1156 operation->key_set = 0;
1157 operation->iv_set = 0;
1158 operation->iv_required = 1;
1159 operation->has_input = 0;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001160 operation->key_usage_sign = 0;
1161 operation->key_usage_verify = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001162
1163 status = psa_get_key_information( key, &key_type, &key_bits );
1164 if( status != PSA_SUCCESS )
1165 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001166
Gilles Peskine8c9def32018-02-08 10:02:12 +01001167 slot = &global_data.key_slots[key];
Gilles Peskine99bc6492018-06-11 17:13:00 +02001168 if( slot->type == PSA_KEY_TYPE_NONE )
1169 return( PSA_ERROR_EMPTY_SLOT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001170
Moran Pekerd7326592018-05-29 16:56:39 +03001171 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001172 operation->key_usage_sign = 1;
1173
Moran Pekerd7326592018-05-29 16:56:39 +03001174 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001175 operation->key_usage_verify = 1;
1176
Gilles Peskine8c9def32018-02-08 10:02:12 +01001177 if( ! PSA_ALG_IS_HMAC( alg ) )
1178 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001179 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001180 if( cipher_info == NULL )
1181 return( PSA_ERROR_NOT_SUPPORTED );
1182 operation->mac_size = cipher_info->block_size;
1183 }
1184 switch( alg )
1185 {
1186#if defined(MBEDTLS_CMAC_C)
1187 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001188 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1189 key_bits,
1190 slot,
1191 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001192 break;
1193#endif /* MBEDTLS_CMAC_C */
1194 default:
1195#if defined(MBEDTLS_MD_C)
1196 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001197 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001198 else
1199#endif /* MBEDTLS_MD_C */
1200 return( PSA_ERROR_NOT_SUPPORTED );
1201 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001202
Gilles Peskine8c9def32018-02-08 10:02:12 +01001203 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001204
1205 * context may contain data that needs to be wiped on error. */
1206 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001207 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001208 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001209 }
Gilles Peskine99bc6492018-06-11 17:13:00 +02001210
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001211 else
1212 {
1213 operation->alg = alg;
1214 operation->key_set = 1;
1215 }
1216 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001217}
1218
1219psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1220 const uint8_t *input,
1221 size_t input_length )
1222{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001223 int ret = 0 ;
1224 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001225 if( ! operation->key_set )
1226 return( PSA_ERROR_BAD_STATE );
1227 if( operation->iv_required && ! operation->iv_set )
1228 return( PSA_ERROR_BAD_STATE );
1229 operation->has_input = 1;
1230
1231 switch( operation->alg )
1232 {
1233#if defined(MBEDTLS_CMAC_C)
1234 case PSA_ALG_CMAC:
1235 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1236 input, input_length );
1237 break;
1238#endif /* MBEDTLS_CMAC_C */
1239 default:
1240#if defined(MBEDTLS_MD_C)
1241 if( PSA_ALG_IS_HMAC( operation->alg ) )
1242 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001243 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1244 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001245 }
1246 else
1247#endif /* MBEDTLS_MD_C */
1248 {
1249 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1250 }
1251 break;
1252 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001253 if ( ( ret != 0 ) || ( status != PSA_SUCCESS ) )
1254 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001255 psa_mac_abort( operation );
1256 if ( ret != 0 )
1257 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001258 }
1259
1260 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001261}
1262
mohammad16036df908f2018-04-02 08:34:15 -07001263static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01001264 uint8_t *mac,
1265 size_t mac_size,
1266 size_t *mac_length )
1267{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001268 int ret = 0;
1269 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001270 if( ! operation->key_set )
1271 return( PSA_ERROR_BAD_STATE );
1272 if( operation->iv_required && ! operation->iv_set )
1273 return( PSA_ERROR_BAD_STATE );
1274
1275 /* Fill the output buffer with something that isn't a valid mac
1276 * (barring an attack on the mac and deliberately-crafted input),
1277 * in case the caller doesn't check the return status properly. */
1278 *mac_length = operation->mac_size;
1279 memset( mac, '!', mac_size );
1280
1281 if( mac_size < operation->mac_size )
1282 return( PSA_ERROR_BUFFER_TOO_SMALL );
1283
1284 switch( operation->alg )
1285 {
1286#if defined(MBEDTLS_CMAC_C)
1287 case PSA_ALG_CMAC:
1288 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1289 break;
1290#endif /* MBEDTLS_CMAC_C */
1291 default:
1292#if defined(MBEDTLS_MD_C)
1293 if( PSA_ALG_IS_HMAC( operation->alg ) )
1294 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001295 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001296 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001297 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001298 size_t block_size =
1299 psa_get_hash_block_size( ( PSA_ALG_HMAC_HASH( operation->alg ) ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001300
Gilles Peskine99bc6492018-06-11 17:13:00 +02001301 if( block_size == 0 )
1302 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001303
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001304 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001305 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001306 if( status != PSA_SUCCESS )
1307 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001308 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001309
1310 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1311 PSA_ALG_HMAC_HASH( operation->alg ) );
1312 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001313 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001314
1315 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001316 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001317 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001318 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001319
1320 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
1321 hash_size);
1322 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001323 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001324
1325 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1326 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001327 hmac_cleanup:
1328 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001329 }
1330 else
1331#endif /* MBEDTLS_MD_C */
1332 {
1333 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1334 }
1335 break;
1336 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001337cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001338
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001339 if( ( ret == 0 ) && (status == PSA_SUCCESS) )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001340 {
1341 return( psa_mac_abort( operation ) );
1342 }
1343 else
1344 {
1345 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001346 if( ret != 0 )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001347 status = mbedtls_to_psa_error(ret);
1348
1349 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001350 }
1351}
1352
mohammad16036df908f2018-04-02 08:34:15 -07001353psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1354 uint8_t *mac,
1355 size_t mac_size,
1356 size_t *mac_length )
1357{
1358 if( !( operation->key_usage_sign ) )
1359 return( PSA_ERROR_NOT_PERMITTED );
1360
Gilles Peskine99bc6492018-06-11 17:13:00 +02001361 return( psa_mac_finish_internal( operation, mac,
1362 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001363}
1364
Gilles Peskine8c9def32018-02-08 10:02:12 +01001365#define MBEDTLS_PSA_MAC_MAX_SIZE \
1366 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1367 MBEDTLS_MD_MAX_SIZE : \
1368 MBEDTLS_MAX_BLOCK_LENGTH )
1369psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1370 const uint8_t *mac,
1371 size_t mac_length )
1372{
1373 uint8_t actual_mac[MBEDTLS_PSA_MAC_MAX_SIZE];
1374 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001375 psa_status_t status;
1376
1377 if( !( operation->key_usage_verify ) )
1378 return( PSA_ERROR_NOT_PERMITTED );
1379
1380 status = psa_mac_finish_internal( operation,
1381 actual_mac, sizeof( actual_mac ),
1382 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383 if( status != PSA_SUCCESS )
1384 return( status );
1385 if( actual_mac_length != mac_length )
1386 return( PSA_ERROR_INVALID_SIGNATURE );
1387 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1388 return( PSA_ERROR_INVALID_SIGNATURE );
1389 return( PSA_SUCCESS );
1390}
1391
1392
Gilles Peskine20035e32018-02-03 22:44:14 +01001393
1394
1395/****************************************************************/
1396/* Asymmetric cryptography */
1397/****************************************************************/
1398
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001399/* Decode the hash algorithm from alg and store the mbedtls encoding in
1400 * md_alg. Verify that the hash length is consistent. */
1401static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1402 size_t hash_length,
1403 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001404{
Gilles Peskine61b91d42018-06-08 16:09:36 +02001405 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
1406 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1407 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1408 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001409 {
1410#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001411 if( hash_length > UINT_MAX )
1412 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001413#endif
1414 }
1415 else
1416 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001417 if( mbedtls_md_get_size( md_info ) != hash_length )
1418 return( PSA_ERROR_INVALID_ARGUMENT );
1419 if( md_info == NULL )
1420 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001421 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001422 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001423}
1424
Gilles Peskine61b91d42018-06-08 16:09:36 +02001425psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1426 psa_algorithm_t alg,
1427 const uint8_t *hash,
1428 size_t hash_length,
1429 const uint8_t *salt,
1430 size_t salt_length,
1431 uint8_t *signature,
1432 size_t signature_size,
1433 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001434{
1435 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001436 psa_status_t status;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001437 *signature_length = 0;
1438 (void) salt;
1439 (void) salt_length;
1440
Gilles Peskine20035e32018-02-03 22:44:14 +01001441 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1442 return( PSA_ERROR_EMPTY_SLOT );
1443 slot = &global_data.key_slots[key];
1444 if( slot->type == PSA_KEY_TYPE_NONE )
1445 return( PSA_ERROR_EMPTY_SLOT );
1446 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1447 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001448 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
mohammad160306e79202018-03-28 13:17:44 +03001449 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001450
Gilles Peskine20035e32018-02-03 22:44:14 +01001451#if defined(MBEDTLS_RSA_C)
1452 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1453 {
1454 mbedtls_rsa_context *rsa = slot->data.rsa;
1455 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001456 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001457 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001458 if( status != PSA_SUCCESS )
1459 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001460
Gilles Peskine20035e32018-02-03 22:44:14 +01001461 if( signature_size < rsa->len )
1462 return( PSA_ERROR_BUFFER_TOO_SMALL );
1463#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001464 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001465 {
1466 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1467 MBEDTLS_MD_NONE );
1468 ret = mbedtls_rsa_pkcs1_sign( rsa,
1469 mbedtls_ctr_drbg_random,
1470 &global_data.ctr_drbg,
1471 MBEDTLS_RSA_PRIVATE,
1472 md_alg, hash_length, hash,
1473 signature );
1474 }
1475 else
1476#endif /* MBEDTLS_PKCS1_V15 */
1477#if defined(MBEDTLS_PKCS1_V21)
1478 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1479 {
1480 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1481 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1482 mbedtls_ctr_drbg_random,
1483 &global_data.ctr_drbg,
1484 MBEDTLS_RSA_PRIVATE,
1485 md_alg, hash_length, hash,
1486 signature );
1487 }
1488 else
1489#endif /* MBEDTLS_PKCS1_V21 */
1490 {
1491 return( PSA_ERROR_INVALID_ARGUMENT );
1492 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001493 if( ret == 0 )
1494 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001495 return( mbedtls_to_psa_error( ret ) );
1496 }
1497 else
1498#endif /* defined(MBEDTLS_RSA_C) */
1499#if defined(MBEDTLS_ECP_C)
1500 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1501 {
itayzafrir5c753392018-05-08 11:18:38 +03001502 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1503 int ret;
1504 const mbedtls_md_info_t *md_info;
1505 mbedtls_md_type_t md_alg;
1506 if( signature_size < PSA_ECDSA_SIGNATURE_SIZE( ecdsa->grp.pbits ) )
1507 return( PSA_ERROR_BUFFER_TOO_SMALL );
1508 md_info = mbedtls_md_info_from_psa( alg );
1509 md_alg = mbedtls_md_get_type( md_info );
1510 ret = mbedtls_ecdsa_write_signature( ecdsa, md_alg, hash, hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001511 signature, signature_length,
1512 mbedtls_ctr_drbg_random,
1513 &global_data.ctr_drbg );
itayzafrir5c753392018-05-08 11:18:38 +03001514 return( mbedtls_to_psa_error( ret ) );
1515 }
1516 else
1517#endif /* defined(MBEDTLS_ECP_C) */
1518 {
Gilles Peskine20035e32018-02-03 22:44:14 +01001519 return( PSA_ERROR_NOT_SUPPORTED );
1520 }
itayzafrir5c753392018-05-08 11:18:38 +03001521}
1522
1523psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1524 psa_algorithm_t alg,
1525 const uint8_t *hash,
1526 size_t hash_length,
1527 const uint8_t *salt,
1528 size_t salt_length,
1529 uint8_t *signature,
1530 size_t signature_size )
1531{
1532 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001533 psa_status_t status;
itayzafrir5c753392018-05-08 11:18:38 +03001534 (void) salt;
1535 (void) salt_length;
1536
1537 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1538 return( PSA_ERROR_INVALID_ARGUMENT );
1539 slot = &global_data.key_slots[key];
1540 if( slot->type == PSA_KEY_TYPE_NONE )
1541 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001542 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001543 return( PSA_ERROR_NOT_PERMITTED );
1544
Gilles Peskine61b91d42018-06-08 16:09:36 +02001545#if defined(MBEDTLS_RSA_C)
Nir Sonnenschein1c2a7ea2018-06-05 15:01:42 +03001546 if( ( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) ||
1547 ( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001548 {
1549 mbedtls_rsa_context *rsa = slot->data.rsa;
1550 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001551 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001552 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001553 if( status != PSA_SUCCESS )
1554 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001555
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001556 if( signature_size < rsa->len )
1557 return( PSA_ERROR_BUFFER_TOO_SMALL );
1558#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001559 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001560 {
1561 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1562 MBEDTLS_MD_NONE );
1563
1564 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001565 mbedtls_ctr_drbg_random,
1566 &global_data.ctr_drbg,
1567 MBEDTLS_RSA_PUBLIC,
1568 md_alg,
1569 hash_length,
1570 hash,
1571 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001572
1573 }
1574 else
1575#endif /* MBEDTLS_PKCS1_V15 */
1576#if defined(MBEDTLS_PKCS1_V21)
1577 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1578 {
Gilles Peskinebeb49482018-06-08 17:44:35 +02001579 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1580 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1581 mbedtls_ctr_drbg_random,
1582 &global_data.ctr_drbg,
1583 MBEDTLS_RSA_PUBLIC,
1584 md_alg, hash_length, hash,
1585 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001586 }
1587 else
1588#endif /* MBEDTLS_PKCS1_V21 */
1589 {
1590 return( PSA_ERROR_INVALID_ARGUMENT );
1591 }
1592 return( mbedtls_to_psa_error( ret ) );
1593 }
1594 else
1595#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001596#if defined(MBEDTLS_ECP_C)
1597 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1598 {
1599 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1600 int ret;
Nir Sonnenscheinc4602912018-06-04 16:42:18 +03001601 (void)alg;
Gilles Peskine61b91d42018-06-08 16:09:36 +02001602 ret = mbedtls_ecdsa_read_signature( ecdsa, hash, hash_length,
1603 signature, signature_size );
itayzafrir5c753392018-05-08 11:18:38 +03001604 return( mbedtls_to_psa_error( ret ) );
1605 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001606 else
1607#endif /* defined(MBEDTLS_ECP_C) */
1608 {
1609 return( PSA_ERROR_NOT_SUPPORTED );
1610 }
1611}
1612
Gilles Peskine61b91d42018-06-08 16:09:36 +02001613psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
1614 psa_algorithm_t alg,
1615 const uint8_t *input,
1616 size_t input_length,
1617 const uint8_t *salt,
1618 size_t salt_length,
1619 uint8_t *output,
1620 size_t output_size,
1621 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001622{
1623 key_slot_t *slot;
1624 (void) salt;
1625 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001626 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001627
1628 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03001629 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001630 slot = &global_data.key_slots[key];
1631 if( slot->type == PSA_KEY_TYPE_NONE )
1632 return( PSA_ERROR_EMPTY_SLOT );
1633 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1634 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001635 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
1636 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001637
1638#if defined(MBEDTLS_RSA_C)
Nir Sonnenschein1c2a7ea2018-06-05 15:01:42 +03001639 if( ( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) ||
Gilles Peskine61b91d42018-06-08 16:09:36 +02001640 ( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001641 {
1642 mbedtls_rsa_context *rsa = slot->data.rsa;
1643 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001644 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02001645 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001646#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001647 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001648 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001649 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
1650 mbedtls_ctr_drbg_random,
1651 &global_data.ctr_drbg,
1652 MBEDTLS_RSA_PUBLIC,
1653 input_length,
1654 input,
1655 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001656 }
1657 else
1658#endif /* MBEDTLS_PKCS1_V15 */
1659#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001660 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001661 {
1662 return( PSA_ERROR_NOT_SUPPORTED );
1663 }
1664 else
1665#endif /* MBEDTLS_PKCS1_V21 */
1666 {
1667 return( PSA_ERROR_INVALID_ARGUMENT );
1668 }
1669 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001670 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001671 return( mbedtls_to_psa_error( ret ) );
1672 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001673 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001674#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001675 {
1676 return( PSA_ERROR_NOT_SUPPORTED );
1677 }
1678
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001679}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001680
Gilles Peskine61b91d42018-06-08 16:09:36 +02001681psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
1682 psa_algorithm_t alg,
1683 const uint8_t *input,
1684 size_t input_length,
1685 const uint8_t *salt,
1686 size_t salt_length,
1687 uint8_t *output,
1688 size_t output_size,
1689 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001690{
1691 key_slot_t *slot;
1692 (void) salt;
1693 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001694 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001695
1696 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1697 return( PSA_ERROR_EMPTY_SLOT );
1698 slot = &global_data.key_slots[key];
1699 if( slot->type == PSA_KEY_TYPE_NONE )
1700 return( PSA_ERROR_EMPTY_SLOT );
1701 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1702 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001703 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
1704 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001705
1706#if defined(MBEDTLS_RSA_C)
1707 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1708 {
1709 mbedtls_rsa_context *rsa = slot->data.rsa;
1710 int ret;
1711
Gilles Peskinec4def2f2018-06-08 17:53:48 +02001712 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001713 return( PSA_ERROR_INVALID_ARGUMENT );
1714
1715#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001716 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001717 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001718 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
1719 mbedtls_ctr_drbg_random,
1720 &global_data.ctr_drbg,
1721 MBEDTLS_RSA_PRIVATE,
1722 output_length,
1723 input,
1724 output,
1725 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001726 }
1727 else
1728#endif /* MBEDTLS_PKCS1_V15 */
1729#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001730 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001731 {
1732 return( PSA_ERROR_NOT_SUPPORTED );
1733 }
1734 else
1735#endif /* MBEDTLS_PKCS1_V21 */
1736 {
1737 return( PSA_ERROR_INVALID_ARGUMENT );
1738 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001739
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001740 return( mbedtls_to_psa_error( ret ) );
1741 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001742 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001743#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001744 {
1745 return( PSA_ERROR_NOT_SUPPORTED );
1746 }
1747
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001748}
Gilles Peskine20035e32018-02-03 22:44:14 +01001749
mohammad1603503973b2018-03-12 15:59:30 +02001750/****************************************************************/
1751/* Symmetric cryptography */
1752/****************************************************************/
1753
Gilles Peskinee553c652018-06-04 16:22:46 +02001754static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
1755 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02001756 psa_algorithm_t alg,
1757 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02001758{
1759 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1760 psa_status_t status;
1761 key_slot_t *slot;
1762 psa_key_type_t key_type;
1763 size_t key_bits;
1764 const mbedtls_cipher_info_t *cipher_info = NULL;
1765
Moran Peker41deec42018-04-04 15:43:05 +03001766 operation->alg = alg;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001767 operation->key_set = 0;
1768 operation->iv_set = 0;
1769 operation->iv_required = 1;
1770 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001771 operation->block_size = 0;
mohammad1603503973b2018-03-12 15:59:30 +02001772
1773 status = psa_get_key_information( key, &key_type, &key_bits );
1774 if( status != PSA_SUCCESS )
1775 return( status );
1776 slot = &global_data.key_slots[key];
1777
Gilles Peskinebb1072f2018-06-08 18:46:05 +02001778 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02001779 if( cipher_info == NULL )
1780 return( PSA_ERROR_NOT_SUPPORTED );
1781
mohammad1603503973b2018-03-12 15:59:30 +02001782 mbedtls_cipher_init( &operation->ctx.cipher );
1783 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03001784 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001785 {
1786 psa_cipher_abort( operation );
1787 return( mbedtls_to_psa_error( ret ) );
1788 }
1789
1790 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data,
Gilles Peskinee553c652018-06-04 16:22:46 +02001791 key_bits, cipher_operation );
Moran Peker41deec42018-04-04 15:43:05 +03001792 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001793 {
1794 psa_cipher_abort( operation );
1795 return( mbedtls_to_psa_error( ret ) );
1796 }
1797
mohammad16038481e742018-03-18 13:57:31 +02001798#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03001799 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02001800 {
Gilles Peskine53514202018-06-06 15:11:46 +02001801 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1802 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02001803
Moran Pekera28258c2018-05-29 16:25:04 +03001804 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02001805 {
1806 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
1807 mode = MBEDTLS_PADDING_PKCS7;
1808 break;
1809 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
1810 mode = MBEDTLS_PADDING_NONE;
1811 break;
1812 default:
Moran Pekerae382792018-05-31 14:06:17 +03001813 psa_cipher_abort( operation );
1814 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02001815 }
1816 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03001817 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03001818 {
1819 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02001820 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03001821 }
mohammad16038481e742018-03-18 13:57:31 +02001822 }
1823#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
1824
mohammad1603503973b2018-03-12 15:59:30 +02001825 operation->key_set = 1;
1826 operation->alg = alg;
Gilles Peskine7e928852018-06-04 16:23:10 +02001827 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
1828 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
1829 1 );
Moran Peker41deec42018-04-04 15:43:05 +03001830 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || ( alg == PSA_ALG_CTR ) )
mohammad16038481e742018-03-18 13:57:31 +02001831 {
mohammad160389e0f462018-04-12 08:48:45 +03001832 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02001833 }
mohammad1603503973b2018-03-12 15:59:30 +02001834
Moran Peker395db872018-05-31 14:07:14 +03001835 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001836}
1837
Gilles Peskinee553c652018-06-04 16:22:46 +02001838psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
1839 psa_key_slot_t key,
1840 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02001841{
Moran Pekera28258c2018-05-29 16:25:04 +03001842 return psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT );
mohammad16038481e742018-03-18 13:57:31 +02001843}
1844
Gilles Peskinee553c652018-06-04 16:22:46 +02001845psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
1846 psa_key_slot_t key,
1847 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02001848{
Moran Pekera28258c2018-05-29 16:25:04 +03001849 return psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT );
mohammad16038481e742018-03-18 13:57:31 +02001850}
1851
Gilles Peskinee553c652018-06-04 16:22:46 +02001852psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
1853 unsigned char *iv,
1854 size_t iv_size,
1855 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02001856{
Moran Peker41deec42018-04-04 15:43:05 +03001857 int ret = PSA_SUCCESS;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001858 if( operation->iv_set || !( operation->iv_required ) )
Moran Peker41deec42018-04-04 15:43:05 +03001859 return( PSA_ERROR_BAD_STATE );
1860 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02001861 {
Moran Peker41deec42018-04-04 15:43:05 +03001862 ret = PSA_ERROR_BUFFER_TOO_SMALL;
1863 goto exit;
1864 }
Gilles Peskine7e928852018-06-04 16:23:10 +02001865 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
1866 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03001867 if( ret != 0 )
1868 {
1869 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02001870 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02001871 }
Gilles Peskinee553c652018-06-04 16:22:46 +02001872
mohammad16038481e742018-03-18 13:57:31 +02001873 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03001874 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03001875
Moran Peker395db872018-05-31 14:07:14 +03001876exit:
1877 if( ret != PSA_SUCCESS )
1878 psa_cipher_abort( operation );
1879 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02001880}
1881
Gilles Peskinee553c652018-06-04 16:22:46 +02001882psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
1883 const unsigned char *iv,
1884 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02001885{
Moran Peker41deec42018-04-04 15:43:05 +03001886 int ret = PSA_SUCCESS;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001887 if( operation->iv_set || !( operation->iv_required ) )
Moran Peker41deec42018-04-04 15:43:05 +03001888 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03001889 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03001890 {
Moran Pekerae382792018-05-31 14:06:17 +03001891 psa_cipher_abort( operation );
1892 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03001893 }
mohammad1603503973b2018-03-12 15:59:30 +02001894 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03001895 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001896 {
1897 psa_cipher_abort( operation );
1898 return( mbedtls_to_psa_error( ret ) );
1899 }
1900
1901 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02001902
Moran Peker395db872018-05-31 14:07:14 +03001903 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001904}
1905
Gilles Peskinee553c652018-06-04 16:22:46 +02001906psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
1907 const uint8_t *input,
1908 size_t input_length,
1909 unsigned char *output,
1910 size_t output_size,
1911 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02001912{
1913 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02001914 size_t expected_output_size;
1915 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
1916 {
1917 /* Take the unprocessed partial block left over from previous
1918 * update calls, if any, plus the input to this call. Remove
1919 * the last partial block, if any. You get the data that will be
1920 * output in this call. */
1921 expected_output_size =
1922 ( operation->ctx.cipher.unprocessed_len + input_length )
1923 / operation->block_size * operation->block_size;
1924 }
1925 else
1926 {
1927 expected_output_size = input_length;
1928 }
1929 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03001930 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02001931
mohammad1603503973b2018-03-12 15:59:30 +02001932 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02001933 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03001934 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001935 {
1936 psa_cipher_abort( operation );
1937 return( mbedtls_to_psa_error( ret ) );
1938 }
1939
Moran Peker395db872018-05-31 14:07:14 +03001940 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001941}
1942
Gilles Peskinee553c652018-06-04 16:22:46 +02001943psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
1944 uint8_t *output,
1945 size_t output_size,
1946 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02001947{
1948 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02001949 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03001950
mohammad1603503973b2018-03-12 15:59:30 +02001951 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03001952 {
Moran Peker7cb22b82018-06-05 11:40:02 +03001953 psa_cipher_abort( operation );
1954 return( PSA_ERROR_BAD_STATE );
1955 }
1956 if( operation->iv_required && ! operation->iv_set )
1957 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001958 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03001959 return( PSA_ERROR_BAD_STATE );
1960 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001961 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
1962 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03001963 {
Gilles Peskine53514202018-06-06 15:11:46 +02001964 psa_algorithm_t padding_mode =
1965 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03001966 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02001967 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001968 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03001969 return( PSA_ERROR_TAMPERING_DETECTED );
1970 }
Gilles Peskine53514202018-06-06 15:11:46 +02001971 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03001972 {
1973 if( operation->ctx.cipher.unprocessed_len != 0 )
1974 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001975 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03001976 return( PSA_ERROR_INVALID_ARGUMENT );
1977 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02001978 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03001979 }
1980
1981 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02001982 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03001983 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001984 {
1985 psa_cipher_abort( operation );
1986 return( mbedtls_to_psa_error( ret ) );
1987 }
Gilles Peskinee553c652018-06-04 16:22:46 +02001988 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03001989 memcpy( output, temp_output_buffer, *output_length );
1990 else
1991 {
1992 psa_cipher_abort( operation );
1993 return( PSA_ERROR_BUFFER_TOO_SMALL );
1994 }
mohammad1603503973b2018-03-12 15:59:30 +02001995
Moran Peker4c80d832018-04-22 20:15:31 +03001996 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001997}
1998
Gilles Peskinee553c652018-06-04 16:22:46 +02001999psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2000{
mohammad1603503973b2018-03-12 15:59:30 +02002001 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002002
Moran Peker41deec42018-04-04 15:43:05 +03002003 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002004 operation->key_set = 0;
2005 operation->iv_set = 0;
2006 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002007 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002008 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002009
Moran Peker395db872018-05-31 14:07:14 +03002010 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002011}
2012
Gilles Peskinea0655c32018-04-30 17:06:50 +02002013
mohammad16038cc1cee2018-03-28 01:21:33 +03002014/****************************************************************/
2015/* Key Policy */
2016/****************************************************************/
2017
2018void psa_key_policy_init(psa_key_policy_t *policy)
2019{
mohammad16036df908f2018-04-02 08:34:15 -07002020 memset( policy, 0, sizeof( psa_key_policy_t ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002021}
2022
2023void psa_key_policy_set_usage(psa_key_policy_t *policy,
2024 psa_key_usage_t usage,
2025 psa_algorithm_t alg)
2026{
mohammad16034eed7572018-03-28 05:14:59 -07002027 policy->usage = usage;
2028 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002029}
2030
2031psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy)
2032{
mohammad16036df908f2018-04-02 08:34:15 -07002033 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002034}
2035
2036psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy)
2037{
mohammad16036df908f2018-04-02 08:34:15 -07002038 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002039}
2040
2041psa_status_t psa_set_key_policy(psa_key_slot_t key,
2042 const psa_key_policy_t *policy)
2043{
2044 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002045
2046 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
2047 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002048
mohammad16038cc1cee2018-03-28 01:21:33 +03002049 slot = &global_data.key_slots[key];
2050 if( slot->type != PSA_KEY_TYPE_NONE )
2051 return( PSA_ERROR_OCCUPIED_SLOT );
2052
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002053 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_ENCRYPT
2054 | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN
mohammad16036df908f2018-04-02 08:34:15 -07002055 | PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002056 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002057
mohammad16036df908f2018-04-02 08:34:15 -07002058 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002059
2060 return( PSA_SUCCESS );
2061}
2062
2063psa_status_t psa_get_key_policy(psa_key_slot_t key,
2064 psa_key_policy_t *policy)
2065{
2066 key_slot_t *slot;
2067
2068 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
2069 return( PSA_ERROR_INVALID_ARGUMENT );
2070
2071 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002072
mohammad16036df908f2018-04-02 08:34:15 -07002073 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002074
2075 return( PSA_SUCCESS );
2076}
Gilles Peskine20035e32018-02-03 22:44:14 +01002077
Gilles Peskinea0655c32018-04-30 17:06:50 +02002078
2079
mohammad1603804cd712018-03-20 22:44:08 +02002080/****************************************************************/
2081/* Key Lifetime */
2082/****************************************************************/
2083
2084psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
2085 psa_key_lifetime_t *lifetime)
2086{
2087 key_slot_t *slot;
2088
2089 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
2090 return( PSA_ERROR_INVALID_ARGUMENT );
2091
2092 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002093
mohammad1603804cd712018-03-20 22:44:08 +02002094 *lifetime = slot->lifetime;
2095
2096 return( PSA_SUCCESS );
2097}
2098
2099psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
2100 const psa_key_lifetime_t lifetime)
2101{
2102 key_slot_t *slot;
2103
2104 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
2105 return( PSA_ERROR_INVALID_ARGUMENT );
2106
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002107 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2108 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002109 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2110 return( PSA_ERROR_INVALID_ARGUMENT );
2111
mohammad1603804cd712018-03-20 22:44:08 +02002112 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002113 if( slot->type != PSA_KEY_TYPE_NONE )
2114 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002115
Moran Pekerd7326592018-05-29 16:56:39 +03002116 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002117 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002118
mohammad1603060ad8a2018-03-20 14:28:38 -07002119 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002120
2121 return( PSA_SUCCESS );
2122}
2123
Gilles Peskine20035e32018-02-03 22:44:14 +01002124
mohammad16035955c982018-04-26 00:53:03 +03002125/****************************************************************/
2126/* AEAD */
2127/****************************************************************/
2128psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2129 psa_algorithm_t alg,
2130 const uint8_t *nonce,
2131 size_t nonce_length,
2132 const uint8_t *additional_data,
2133 size_t additional_data_length,
2134 const uint8_t *plaintext,
2135 size_t plaintext_length,
2136 uint8_t *ciphertext,
2137 size_t ciphertext_size,
2138 size_t *ciphertext_length )
2139{
2140 int ret;
2141 psa_status_t status;
2142 key_slot_t *slot;
2143 psa_key_type_t key_type;
2144 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002145 uint8_t *tag;
2146 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002147 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002148 const mbedtls_cipher_info_t *cipher_info = NULL;
2149
mohammad1603f08a5502018-06-03 15:05:47 +03002150 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002151
mohammad16035955c982018-04-26 00:53:03 +03002152 status = psa_get_key_information( key, &key_type, &key_bits );
2153 if( status != PSA_SUCCESS )
2154 return( status );
2155 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002156 if( slot->type == PSA_KEY_TYPE_NONE )
2157 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002158
mohammad16035ed06212018-06-06 13:09:34 +03002159 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2160 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002161 if( cipher_info == NULL )
2162 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002163
mohammad1603f14394b2018-06-04 14:33:19 +03002164 if( !( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
2165 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002166
mohammad16036b4d98c2018-06-06 13:19:51 +03002167 if ( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
mohammad16035ed06212018-06-06 13:09:34 +03002168 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002169 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002170
mohammad16035955c982018-04-26 00:53:03 +03002171 if( alg == PSA_ALG_GCM )
2172 {
2173 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002174 tag_length = 16;
2175
mohammad160396910d82018-06-04 14:33:00 +03002176 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2177 return( PSA_ERROR_INVALID_ARGUMENT );
2178
mohammad160315223a82018-06-03 17:19:55 +03002179 //make sure we have place to hold the tag in the ciphertext buffer
2180 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002181 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002182
2183 //update the tag pointer to point to the end of the ciphertext_length
2184 tag = ciphertext + plaintext_length;
2185
mohammad16035955c982018-04-26 00:53:03 +03002186 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002187 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002188 slot->data.raw.data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002189 key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002190 if( ret != 0 )
2191 {
2192 mbedtls_gcm_free( &gcm );
2193 return( mbedtls_to_psa_error( ret ) );
2194 }
2195 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002196 plaintext_length, nonce,
2197 nonce_length, additional_data,
2198 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002199 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002200 mbedtls_gcm_free( &gcm );
2201 }
2202 else if( alg == PSA_ALG_CCM )
2203 {
2204 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002205 tag_length = 16;
2206
mohammad160396910d82018-06-04 14:33:00 +03002207 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2208 return( PSA_ERROR_INVALID_ARGUMENT );
2209
mohammad160347ddf3d2018-04-26 01:11:21 +03002210 if( nonce_length < 7 || nonce_length > 13 )
2211 return( PSA_ERROR_INVALID_ARGUMENT );
2212
mohammad160315223a82018-06-03 17:19:55 +03002213 //make sure we have place to hold the tag in the ciphertext buffer
2214 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002215 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002216
2217 //update the tag pointer to point to the end of the ciphertext_length
2218 tag = ciphertext + plaintext_length;
2219
2220
2221
mohammad16035955c982018-04-26 00:53:03 +03002222 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002223 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002224 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002225 if( ret != 0 )
2226 {
2227 mbedtls_ccm_free( &ccm );
2228 return( mbedtls_to_psa_error( ret ) );
2229 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002230 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
2231 nonce, nonce_length, additional_data,
2232 additional_data_length,
2233 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002234 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002235 mbedtls_ccm_free( &ccm );
2236 }
mohammad16035c8845f2018-05-09 05:40:09 -07002237 else
2238 {
mohammad1603554faad2018-06-03 15:07:38 +03002239 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002240 }
mohammad160315223a82018-06-03 17:19:55 +03002241
2242 if( ret != 0 )
2243 {
2244 memset( ciphertext, 0, ciphertext_size );
2245 return( mbedtls_to_psa_error( ret ) );
2246 }
2247
2248 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002249 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002250}
2251
Gilles Peskineee652a32018-06-01 19:23:52 +02002252/* Locate the tag in a ciphertext buffer containing the encrypted data
2253 * followed by the tag. Return the length of the part preceding the tag in
2254 * *plaintext_length. This is the size of the plaintext in modes where
2255 * the encrypted data has the same size as the plaintext, such as
2256 * CCM and GCM. */
2257static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2258 const uint8_t *ciphertext,
2259 size_t ciphertext_length,
2260 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002261 const uint8_t **p_tag )
2262{
2263 size_t payload_length;
2264 if( tag_length > ciphertext_length )
2265 return( PSA_ERROR_INVALID_ARGUMENT );
2266 payload_length = ciphertext_length - tag_length;
2267 if( payload_length > plaintext_size )
2268 return( PSA_ERROR_BUFFER_TOO_SMALL );
2269 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002270 return( PSA_SUCCESS );
2271}
2272
mohammad16035955c982018-04-26 00:53:03 +03002273psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2274 psa_algorithm_t alg,
2275 const uint8_t *nonce,
2276 size_t nonce_length,
2277 const uint8_t *additional_data,
2278 size_t additional_data_length,
2279 const uint8_t *ciphertext,
2280 size_t ciphertext_length,
2281 uint8_t *plaintext,
2282 size_t plaintext_size,
2283 size_t *plaintext_length )
2284{
2285 int ret;
2286 psa_status_t status;
2287 key_slot_t *slot;
2288 psa_key_type_t key_type;
2289 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002290 const uint8_t *tag;
2291 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002292 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002293 const mbedtls_cipher_info_t *cipher_info = NULL;
2294
Gilles Peskineee652a32018-06-01 19:23:52 +02002295 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002296
mohammad16035955c982018-04-26 00:53:03 +03002297 status = psa_get_key_information( key, &key_type, &key_bits );
2298 if( status != PSA_SUCCESS )
2299 return( status );
2300 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002301 if( slot->type == PSA_KEY_TYPE_NONE )
2302 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002303
mohammad16035ed06212018-06-06 13:09:34 +03002304 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2305 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002306 if( cipher_info == NULL )
2307 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603f14394b2018-06-04 14:33:19 +03002308
2309 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2310 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002311
mohammad1603fc614b12018-06-07 01:43:52 +03002312 if ( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2313 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002314 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002315
mohammad16035955c982018-04-26 00:53:03 +03002316 if( alg == PSA_ALG_GCM )
2317 {
2318 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002319
Gilles Peskineee652a32018-06-01 19:23:52 +02002320 tag_length = 16;
2321 status = psa_aead_unpadded_locate_tag( tag_length,
2322 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002323 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002324 if( status != PSA_SUCCESS )
2325 return( status );
2326
mohammad16035955c982018-04-26 00:53:03 +03002327 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002328 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002329 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002330 if( ret != 0 )
2331 {
2332 mbedtls_gcm_free( &gcm );
2333 return( mbedtls_to_psa_error( ret ) );
2334 }
mohammad16035955c982018-04-26 00:53:03 +03002335
Gilles Peskineee652a32018-06-01 19:23:52 +02002336 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002337 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002338 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002339 additional_data,
2340 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002341 tag, tag_length,
2342 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002343 mbedtls_gcm_free( &gcm );
2344 }
2345 else if( alg == PSA_ALG_CCM )
2346 {
2347 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002348
mohammad160347ddf3d2018-04-26 01:11:21 +03002349 if( nonce_length < 7 || nonce_length > 13 )
2350 return( PSA_ERROR_INVALID_ARGUMENT );
2351
mohammad16039375f842018-06-03 14:28:24 +03002352 tag_length = 16;
2353 status = psa_aead_unpadded_locate_tag( tag_length,
2354 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002355 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002356 if( status != PSA_SUCCESS )
2357 return( status );
2358
mohammad16035955c982018-04-26 00:53:03 +03002359 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002360 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002361 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002362 if( ret != 0 )
2363 {
2364 mbedtls_ccm_free( &ccm );
2365 return( mbedtls_to_psa_error( ret ) );
2366 }
mohammad160360a64d02018-06-03 17:20:42 +03002367 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002368 nonce, nonce_length,
2369 additional_data, additional_data_length,
2370 ciphertext, plaintext,
2371 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002372 mbedtls_ccm_free( &ccm );
2373 }
mohammad160339574652018-06-01 04:39:53 -07002374 else
2375 {
mohammad1603554faad2018-06-03 15:07:38 +03002376 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002377 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002378
Gilles Peskineee652a32018-06-01 19:23:52 +02002379 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002380 memset( plaintext, 0, plaintext_size );
2381 else
2382 *plaintext_length = ciphertext_length - tag_length;
2383
Gilles Peskineee652a32018-06-01 19:23:52 +02002384 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002385}
2386
Gilles Peskinea0655c32018-04-30 17:06:50 +02002387
Gilles Peskine20035e32018-02-03 22:44:14 +01002388/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002389/* Module setup */
2390/****************************************************************/
2391
Gilles Peskinee59236f2018-01-27 23:32:46 +01002392void mbedtls_psa_crypto_free( void )
2393{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002394 size_t key;
2395 for( key = 1; key < MBEDTLS_PSA_KEY_SLOT_COUNT; key++ )
2396 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002397 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2398 mbedtls_entropy_free( &global_data.entropy );
2399 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2400}
2401
2402psa_status_t psa_crypto_init( void )
2403{
2404 int ret;
2405 const unsigned char drbg_seed[] = "PSA";
2406
2407 if( global_data.initialized != 0 )
2408 return( PSA_SUCCESS );
2409
2410 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2411 mbedtls_entropy_init( &global_data.entropy );
2412 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2413
2414 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2415 mbedtls_entropy_func,
2416 &global_data.entropy,
2417 drbg_seed, sizeof( drbg_seed ) - 1 );
2418 if( ret != 0 )
2419 goto exit;
2420
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002421 global_data.initialized = 1;
2422
Gilles Peskinee59236f2018-01-27 23:32:46 +01002423exit:
2424 if( ret != 0 )
2425 mbedtls_psa_crypto_free( );
2426 return( mbedtls_to_psa_error( ret ) );
2427}
2428
2429#endif /* MBEDTLS_PSA_CRYPTO_C */