blob: 18126f638dc1e750080ae1b4def4719cf85b3203 [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 Peskine8c9def32018-02-08 10:02:12 +0100927 switch( alg )
928 {
929 case PSA_ALG_STREAM_CIPHER:
930 mode = MBEDTLS_MODE_STREAM;
931 break;
932 case PSA_ALG_CBC_BASE:
933 mode = MBEDTLS_MODE_CBC;
934 break;
935 case PSA_ALG_CFB_BASE:
936 mode = MBEDTLS_MODE_CFB;
937 break;
938 case PSA_ALG_OFB_BASE:
939 mode = MBEDTLS_MODE_OFB;
940 break;
941 case PSA_ALG_CTR:
942 mode = MBEDTLS_MODE_CTR;
943 break;
944 case PSA_ALG_CCM:
945 mode = MBEDTLS_MODE_CCM;
946 break;
947 case PSA_ALG_GCM:
948 mode = MBEDTLS_MODE_GCM;
949 break;
950 default:
951 return( NULL );
952 }
953 }
954 else if( alg == PSA_ALG_CMAC )
955 mode = MBEDTLS_MODE_ECB;
956 else if( alg == PSA_ALG_GMAC )
957 mode = MBEDTLS_MODE_GCM;
958 else
959 return( NULL );
960
961 switch( key_type )
962 {
963 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +0300964 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100965 break;
966 case PSA_KEY_TYPE_DES:
967 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +0300968 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100969 else
mohammad1603f4f0d612018-06-03 15:04:51 +0300970 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100971 break;
972 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +0300973 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100974 break;
975 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +0300976 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100977 break;
978 default:
979 return( NULL );
980 }
mohammad1603f4f0d612018-06-03 15:04:51 +0300981 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +0300982 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100983
mohammad1603f4f0d612018-06-03 15:04:51 +0300984 return( mbedtls_cipher_info_from_values( cipher_id_tmp, key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100985}
986
987psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
988{
989 switch( operation->alg )
990 {
991#if defined(MBEDTLS_CMAC_C)
992 case PSA_ALG_CMAC:
993 mbedtls_cipher_free( &operation->ctx.cmac );
994 break;
995#endif /* MBEDTLS_CMAC_C */
996 default:
997#if defined(MBEDTLS_MD_C)
998 if( PSA_ALG_IS_HMAC( operation->alg ) )
999 mbedtls_md_free( &operation->ctx.hmac );
1000 else
1001#endif /* MBEDTLS_MD_C */
1002 return( PSA_ERROR_NOT_SUPPORTED );
1003 }
Moran Peker41deec42018-04-04 15:43:05 +03001004
Gilles Peskine8c9def32018-02-08 10:02:12 +01001005 operation->alg = 0;
1006 operation->key_set = 0;
1007 operation->iv_set = 0;
1008 operation->iv_required = 0;
1009 operation->has_input = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001010
Gilles Peskine8c9def32018-02-08 10:02:12 +01001011 return( PSA_SUCCESS );
1012}
1013
1014psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1015 psa_key_slot_t key,
1016 psa_algorithm_t alg )
1017{
1018 int ret = MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
1019 psa_status_t status;
1020 key_slot_t *slot;
1021 psa_key_type_t key_type;
1022 size_t key_bits;
1023 const mbedtls_cipher_info_t *cipher_info = NULL;
1024
1025 operation->alg = 0;
1026 operation->key_set = 0;
1027 operation->iv_set = 0;
1028 operation->iv_required = 1;
1029 operation->has_input = 0;
1030
1031 status = psa_get_key_information( key, &key_type, &key_bits );
1032 if( status != PSA_SUCCESS )
1033 return( status );
1034 slot = &global_data.key_slots[key];
1035
Moran Pekerd7326592018-05-29 16:56:39 +03001036 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001037 operation->key_usage_sign = 1;
1038
Moran Pekerd7326592018-05-29 16:56:39 +03001039 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001040 operation->key_usage_verify = 1;
1041
Gilles Peskine8c9def32018-02-08 10:02:12 +01001042 if( ! PSA_ALG_IS_HMAC( alg ) )
1043 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001044 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001045 if( cipher_info == NULL )
1046 return( PSA_ERROR_NOT_SUPPORTED );
1047 operation->mac_size = cipher_info->block_size;
1048 }
1049 switch( alg )
1050 {
1051#if defined(MBEDTLS_CMAC_C)
1052 case PSA_ALG_CMAC:
1053 operation->iv_required = 0;
1054 mbedtls_cipher_init( &operation->ctx.cmac );
1055 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1056 if( ret != 0 )
1057 break;
1058 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1059 slot->data.raw.data,
1060 key_bits );
1061 break;
1062#endif /* MBEDTLS_CMAC_C */
1063 default:
1064#if defined(MBEDTLS_MD_C)
1065 if( PSA_ALG_IS_HMAC( alg ) )
1066 {
1067 const mbedtls_md_info_t *md_info =
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001068 mbedtls_md_info_from_psa( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001069 if( md_info == NULL )
1070 return( PSA_ERROR_NOT_SUPPORTED );
1071 if( key_type != PSA_KEY_TYPE_HMAC )
1072 return( PSA_ERROR_INVALID_ARGUMENT );
1073 operation->iv_required = 0;
1074 operation->mac_size = mbedtls_md_get_size( md_info );
1075 mbedtls_md_init( &operation->ctx.hmac );
1076 ret = mbedtls_md_setup( &operation->ctx.hmac, md_info, 1 );
1077 if( ret != 0 )
1078 break;
1079 ret = mbedtls_md_hmac_starts( &operation->ctx.hmac,
1080 slot->data.raw.data,
1081 slot->data.raw.bytes );
1082 break;
1083 }
1084 else
1085#endif /* MBEDTLS_MD_C */
1086 return( PSA_ERROR_NOT_SUPPORTED );
1087 }
1088
1089 /* If we reach this point, then the algorithm-specific part of the
1090 * context has at least been initialized, and may contain data that
1091 * needs to be wiped on error. */
1092 operation->alg = alg;
1093 if( ret != 0 )
1094 {
1095 psa_mac_abort( operation );
1096 return( mbedtls_to_psa_error( ret ) );
1097 }
1098 operation->key_set = 1;
Gilles Peskine47c1bc02018-03-20 17:55:04 +01001099 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001100}
1101
1102psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1103 const uint8_t *input,
1104 size_t input_length )
1105{
1106 int ret;
1107 if( ! operation->key_set )
1108 return( PSA_ERROR_BAD_STATE );
1109 if( operation->iv_required && ! operation->iv_set )
1110 return( PSA_ERROR_BAD_STATE );
1111 operation->has_input = 1;
1112
1113 switch( operation->alg )
1114 {
1115#if defined(MBEDTLS_CMAC_C)
1116 case PSA_ALG_CMAC:
1117 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1118 input, input_length );
1119 break;
1120#endif /* MBEDTLS_CMAC_C */
1121 default:
1122#if defined(MBEDTLS_MD_C)
1123 if( PSA_ALG_IS_HMAC( operation->alg ) )
1124 {
1125 ret = mbedtls_md_hmac_update( &operation->ctx.hmac,
1126 input, input_length );
1127 }
1128 else
1129#endif /* MBEDTLS_MD_C */
1130 {
1131 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1132 }
1133 break;
1134 }
1135 if( ret != 0 )
1136 psa_mac_abort( operation );
1137 return( mbedtls_to_psa_error( ret ) );
1138}
1139
mohammad16036df908f2018-04-02 08:34:15 -07001140static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01001141 uint8_t *mac,
1142 size_t mac_size,
1143 size_t *mac_length )
1144{
1145 int ret;
1146 if( ! operation->key_set )
1147 return( PSA_ERROR_BAD_STATE );
1148 if( operation->iv_required && ! operation->iv_set )
1149 return( PSA_ERROR_BAD_STATE );
1150
1151 /* Fill the output buffer with something that isn't a valid mac
1152 * (barring an attack on the mac and deliberately-crafted input),
1153 * in case the caller doesn't check the return status properly. */
1154 *mac_length = operation->mac_size;
1155 memset( mac, '!', mac_size );
1156
1157 if( mac_size < operation->mac_size )
1158 return( PSA_ERROR_BUFFER_TOO_SMALL );
1159
1160 switch( operation->alg )
1161 {
1162#if defined(MBEDTLS_CMAC_C)
1163 case PSA_ALG_CMAC:
1164 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1165 break;
1166#endif /* MBEDTLS_CMAC_C */
1167 default:
1168#if defined(MBEDTLS_MD_C)
1169 if( PSA_ALG_IS_HMAC( operation->alg ) )
1170 {
1171 ret = mbedtls_md_hmac_finish( &operation->ctx.hmac, mac );
1172 }
1173 else
1174#endif /* MBEDTLS_MD_C */
1175 {
1176 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1177 }
1178 break;
1179 }
1180
1181 if( ret == 0 )
1182 {
1183 return( psa_mac_abort( operation ) );
1184 }
1185 else
1186 {
1187 psa_mac_abort( operation );
1188 return( mbedtls_to_psa_error( ret ) );
1189 }
1190}
1191
mohammad16036df908f2018-04-02 08:34:15 -07001192psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1193 uint8_t *mac,
1194 size_t mac_size,
1195 size_t *mac_length )
1196{
1197 if( !( operation->key_usage_sign ) )
1198 return( PSA_ERROR_NOT_PERMITTED );
1199
1200 return( psa_mac_finish_internal(operation, mac, mac_size, mac_length ) );
1201}
1202
Gilles Peskine8c9def32018-02-08 10:02:12 +01001203#define MBEDTLS_PSA_MAC_MAX_SIZE \
1204 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1205 MBEDTLS_MD_MAX_SIZE : \
1206 MBEDTLS_MAX_BLOCK_LENGTH )
1207psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1208 const uint8_t *mac,
1209 size_t mac_length )
1210{
1211 uint8_t actual_mac[MBEDTLS_PSA_MAC_MAX_SIZE];
1212 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001213 psa_status_t status;
1214
1215 if( !( operation->key_usage_verify ) )
1216 return( PSA_ERROR_NOT_PERMITTED );
1217
1218 status = psa_mac_finish_internal( operation,
1219 actual_mac, sizeof( actual_mac ),
1220 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001221 if( status != PSA_SUCCESS )
1222 return( status );
1223 if( actual_mac_length != mac_length )
1224 return( PSA_ERROR_INVALID_SIGNATURE );
1225 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1226 return( PSA_ERROR_INVALID_SIGNATURE );
1227 return( PSA_SUCCESS );
1228}
1229
1230
Gilles Peskine20035e32018-02-03 22:44:14 +01001231
1232
1233/****************************************************************/
1234/* Asymmetric cryptography */
1235/****************************************************************/
1236
1237psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1238 psa_algorithm_t alg,
1239 const uint8_t *hash,
1240 size_t hash_length,
1241 const uint8_t *salt,
1242 size_t salt_length,
1243 uint8_t *signature,
1244 size_t signature_size,
1245 size_t *signature_length)
1246{
1247 key_slot_t *slot;
1248
Gilles Peskine93aa0332018-02-03 23:58:03 +01001249 *signature_length = 0;
1250 (void) salt;
1251 (void) salt_length;
1252
Gilles Peskine20035e32018-02-03 22:44:14 +01001253 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1254 return( PSA_ERROR_EMPTY_SLOT );
1255 slot = &global_data.key_slots[key];
1256 if( slot->type == PSA_KEY_TYPE_NONE )
1257 return( PSA_ERROR_EMPTY_SLOT );
1258 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1259 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001260 if( !( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
1261 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001262
Gilles Peskine20035e32018-02-03 22:44:14 +01001263#if defined(MBEDTLS_RSA_C)
1264 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1265 {
1266 mbedtls_rsa_context *rsa = slot->data.rsa;
1267 int ret;
1268 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001269 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001270 mbedtls_md_type_t md_alg =
1271 hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1272 if( md_alg == MBEDTLS_MD_NONE )
1273 {
1274#if SIZE_MAX > UINT_MAX
1275 if( hash_length > UINT_MAX )
1276 return( PSA_ERROR_INVALID_ARGUMENT );
1277#endif
1278 }
1279 else
1280 {
1281 if( mbedtls_md_get_size( md_info ) != hash_length )
1282 return( PSA_ERROR_INVALID_ARGUMENT );
1283 if( md_info == NULL )
1284 return( PSA_ERROR_NOT_SUPPORTED );
1285 }
1286 if( signature_size < rsa->len )
1287 return( PSA_ERROR_BUFFER_TOO_SMALL );
1288#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001289 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001290 {
1291 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1292 MBEDTLS_MD_NONE );
1293 ret = mbedtls_rsa_pkcs1_sign( rsa,
1294 mbedtls_ctr_drbg_random,
1295 &global_data.ctr_drbg,
1296 MBEDTLS_RSA_PRIVATE,
1297 md_alg, hash_length, hash,
1298 signature );
1299 }
1300 else
1301#endif /* MBEDTLS_PKCS1_V15 */
1302#if defined(MBEDTLS_PKCS1_V21)
1303 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1304 {
1305 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1306 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1307 mbedtls_ctr_drbg_random,
1308 &global_data.ctr_drbg,
1309 MBEDTLS_RSA_PRIVATE,
1310 md_alg, hash_length, hash,
1311 signature );
1312 }
1313 else
1314#endif /* MBEDTLS_PKCS1_V21 */
1315 {
1316 return( PSA_ERROR_INVALID_ARGUMENT );
1317 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001318 if( ret == 0 )
1319 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001320 return( mbedtls_to_psa_error( ret ) );
1321 }
1322 else
1323#endif /* defined(MBEDTLS_RSA_C) */
1324#if defined(MBEDTLS_ECP_C)
1325 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1326 {
itayzafrir5c753392018-05-08 11:18:38 +03001327 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1328 int ret;
1329 const mbedtls_md_info_t *md_info;
1330 mbedtls_md_type_t md_alg;
1331 if( signature_size < PSA_ECDSA_SIGNATURE_SIZE( ecdsa->grp.pbits ) )
1332 return( PSA_ERROR_BUFFER_TOO_SMALL );
1333 md_info = mbedtls_md_info_from_psa( alg );
1334 md_alg = mbedtls_md_get_type( md_info );
1335 ret = mbedtls_ecdsa_write_signature( ecdsa, md_alg, hash, hash_length,
1336 signature, signature_length, mbedtls_ctr_drbg_random,
1337 &global_data.ctr_drbg );
1338 return( mbedtls_to_psa_error( ret ) );
1339 }
1340 else
1341#endif /* defined(MBEDTLS_ECP_C) */
1342 {
Gilles Peskine20035e32018-02-03 22:44:14 +01001343 return( PSA_ERROR_NOT_SUPPORTED );
1344 }
itayzafrir5c753392018-05-08 11:18:38 +03001345}
1346
1347psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1348 psa_algorithm_t alg,
1349 const uint8_t *hash,
1350 size_t hash_length,
1351 const uint8_t *salt,
1352 size_t salt_length,
1353 uint8_t *signature,
1354 size_t signature_size )
1355{
1356 key_slot_t *slot;
1357 (void) salt;
1358 (void) salt_length;
1359
1360 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1361 return( PSA_ERROR_INVALID_ARGUMENT );
1362 slot = &global_data.key_slots[key];
1363 if( slot->type == PSA_KEY_TYPE_NONE )
1364 return( PSA_ERROR_EMPTY_SLOT );
1365 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1366 return( PSA_ERROR_INVALID_ARGUMENT );
1367 if( !( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
1368 return( PSA_ERROR_NOT_PERMITTED );
1369
1370#if defined(MBEDTLS_ECP_C)
1371 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1372 {
1373 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1374 int ret;
1375 (void) alg;
1376 ret = mbedtls_ecdsa_read_signature( ecdsa, hash, hash_length, signature,
1377 signature_size );
1378 return( mbedtls_to_psa_error( ret ) );
1379 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001380 else
1381#endif /* defined(MBEDTLS_ECP_C) */
1382 {
1383 return( PSA_ERROR_NOT_SUPPORTED );
1384 }
1385}
1386
1387
mohammad1603503973b2018-03-12 15:59:30 +02001388/****************************************************************/
1389/* Symmetric cryptography */
1390/****************************************************************/
1391
Gilles Peskinee553c652018-06-04 16:22:46 +02001392static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
1393 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02001394 psa_algorithm_t alg,
1395 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02001396{
1397 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1398 psa_status_t status;
1399 key_slot_t *slot;
1400 psa_key_type_t key_type;
1401 size_t key_bits;
1402 const mbedtls_cipher_info_t *cipher_info = NULL;
1403
Moran Peker41deec42018-04-04 15:43:05 +03001404 operation->alg = alg;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001405 operation->key_set = 0;
1406 operation->iv_set = 0;
1407 operation->iv_required = 1;
1408 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001409 operation->block_size = 0;
mohammad1603503973b2018-03-12 15:59:30 +02001410
1411 status = psa_get_key_information( key, &key_type, &key_bits );
1412 if( status != PSA_SUCCESS )
1413 return( status );
1414 slot = &global_data.key_slots[key];
1415
Gilles Peskinebb1072f2018-06-08 18:46:05 +02001416 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02001417 if( cipher_info == NULL )
1418 return( PSA_ERROR_NOT_SUPPORTED );
1419
mohammad1603503973b2018-03-12 15:59:30 +02001420 mbedtls_cipher_init( &operation->ctx.cipher );
1421 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03001422 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001423 {
1424 psa_cipher_abort( operation );
1425 return( mbedtls_to_psa_error( ret ) );
1426 }
1427
1428 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data,
Gilles Peskinee553c652018-06-04 16:22:46 +02001429 key_bits, cipher_operation );
Moran Peker41deec42018-04-04 15:43:05 +03001430 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001431 {
1432 psa_cipher_abort( operation );
1433 return( mbedtls_to_psa_error( ret ) );
1434 }
1435
mohammad16038481e742018-03-18 13:57:31 +02001436#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03001437 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02001438 {
Gilles Peskine53514202018-06-06 15:11:46 +02001439 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1440 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02001441
Moran Pekera28258c2018-05-29 16:25:04 +03001442 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02001443 {
1444 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
1445 mode = MBEDTLS_PADDING_PKCS7;
1446 break;
1447 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
1448 mode = MBEDTLS_PADDING_NONE;
1449 break;
1450 default:
Moran Pekerae382792018-05-31 14:06:17 +03001451 psa_cipher_abort( operation );
1452 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02001453 }
1454 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03001455 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03001456 {
1457 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02001458 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03001459 }
mohammad16038481e742018-03-18 13:57:31 +02001460 }
1461#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
1462
mohammad1603503973b2018-03-12 15:59:30 +02001463 operation->key_set = 1;
1464 operation->alg = alg;
Gilles Peskine7e928852018-06-04 16:23:10 +02001465 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
1466 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
1467 1 );
Moran Peker41deec42018-04-04 15:43:05 +03001468 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || ( alg == PSA_ALG_CTR ) )
mohammad16038481e742018-03-18 13:57:31 +02001469 {
mohammad160389e0f462018-04-12 08:48:45 +03001470 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02001471 }
mohammad1603503973b2018-03-12 15:59:30 +02001472
Moran Peker395db872018-05-31 14:07:14 +03001473 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001474}
1475
Gilles Peskinee553c652018-06-04 16:22:46 +02001476psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
1477 psa_key_slot_t key,
1478 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02001479{
Moran Pekera28258c2018-05-29 16:25:04 +03001480 return psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT );
mohammad16038481e742018-03-18 13:57:31 +02001481}
1482
Gilles Peskinee553c652018-06-04 16:22:46 +02001483psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
1484 psa_key_slot_t key,
1485 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02001486{
Moran Pekera28258c2018-05-29 16:25:04 +03001487 return psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT );
mohammad16038481e742018-03-18 13:57:31 +02001488}
1489
Gilles Peskinee553c652018-06-04 16:22:46 +02001490psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
1491 unsigned char *iv,
1492 size_t iv_size,
1493 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02001494{
Moran Peker41deec42018-04-04 15:43:05 +03001495 int ret = PSA_SUCCESS;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001496 if( operation->iv_set || !( operation->iv_required ) )
Moran Peker41deec42018-04-04 15:43:05 +03001497 return( PSA_ERROR_BAD_STATE );
1498 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02001499 {
Moran Peker41deec42018-04-04 15:43:05 +03001500 ret = PSA_ERROR_BUFFER_TOO_SMALL;
1501 goto exit;
1502 }
Gilles Peskine7e928852018-06-04 16:23:10 +02001503 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
1504 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03001505 if( ret != 0 )
1506 {
1507 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02001508 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02001509 }
Gilles Peskinee553c652018-06-04 16:22:46 +02001510
mohammad16038481e742018-03-18 13:57:31 +02001511 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03001512 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03001513
Moran Peker395db872018-05-31 14:07:14 +03001514exit:
1515 if( ret != PSA_SUCCESS )
1516 psa_cipher_abort( operation );
1517 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02001518}
1519
Gilles Peskinee553c652018-06-04 16:22:46 +02001520psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
1521 const unsigned char *iv,
1522 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02001523{
Moran Peker41deec42018-04-04 15:43:05 +03001524 int ret = PSA_SUCCESS;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001525 if( operation->iv_set || !( operation->iv_required ) )
Moran Peker41deec42018-04-04 15:43:05 +03001526 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03001527 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03001528 {
Moran Pekerae382792018-05-31 14:06:17 +03001529 psa_cipher_abort( operation );
1530 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03001531 }
mohammad1603503973b2018-03-12 15:59:30 +02001532 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03001533 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001534 {
1535 psa_cipher_abort( operation );
1536 return( mbedtls_to_psa_error( ret ) );
1537 }
1538
1539 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02001540
Moran Peker395db872018-05-31 14:07:14 +03001541 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001542}
1543
Gilles Peskinee553c652018-06-04 16:22:46 +02001544psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
1545 const uint8_t *input,
1546 size_t input_length,
1547 unsigned char *output,
1548 size_t output_size,
1549 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02001550{
1551 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02001552 size_t expected_output_size;
1553 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
1554 {
1555 /* Take the unprocessed partial block left over from previous
1556 * update calls, if any, plus the input to this call. Remove
1557 * the last partial block, if any. You get the data that will be
1558 * output in this call. */
1559 expected_output_size =
1560 ( operation->ctx.cipher.unprocessed_len + input_length )
1561 / operation->block_size * operation->block_size;
1562 }
1563 else
1564 {
1565 expected_output_size = input_length;
1566 }
1567 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03001568 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02001569
mohammad1603503973b2018-03-12 15:59:30 +02001570 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02001571 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03001572 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001573 {
1574 psa_cipher_abort( operation );
1575 return( mbedtls_to_psa_error( ret ) );
1576 }
1577
Moran Peker395db872018-05-31 14:07:14 +03001578 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001579}
1580
Gilles Peskinee553c652018-06-04 16:22:46 +02001581psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
1582 uint8_t *output,
1583 size_t output_size,
1584 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02001585{
1586 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02001587 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03001588
mohammad1603503973b2018-03-12 15:59:30 +02001589 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03001590 {
Moran Peker7cb22b82018-06-05 11:40:02 +03001591 psa_cipher_abort( operation );
1592 return( PSA_ERROR_BAD_STATE );
1593 }
1594 if( operation->iv_required && ! operation->iv_set )
1595 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001596 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03001597 return( PSA_ERROR_BAD_STATE );
1598 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001599 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
1600 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03001601 {
Gilles Peskine53514202018-06-06 15:11:46 +02001602 psa_algorithm_t padding_mode =
1603 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03001604 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02001605 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001606 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03001607 return( PSA_ERROR_TAMPERING_DETECTED );
1608 }
Gilles Peskine53514202018-06-06 15:11:46 +02001609 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03001610 {
1611 if( operation->ctx.cipher.unprocessed_len != 0 )
1612 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001613 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03001614 return( PSA_ERROR_INVALID_ARGUMENT );
1615 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02001616 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03001617 }
1618
1619 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02001620 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03001621 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001622 {
1623 psa_cipher_abort( operation );
1624 return( mbedtls_to_psa_error( ret ) );
1625 }
Gilles Peskinee553c652018-06-04 16:22:46 +02001626 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03001627 memcpy( output, temp_output_buffer, *output_length );
1628 else
1629 {
1630 psa_cipher_abort( operation );
1631 return( PSA_ERROR_BUFFER_TOO_SMALL );
1632 }
mohammad1603503973b2018-03-12 15:59:30 +02001633
Moran Peker4c80d832018-04-22 20:15:31 +03001634 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001635}
1636
Gilles Peskinee553c652018-06-04 16:22:46 +02001637psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
1638{
mohammad1603503973b2018-03-12 15:59:30 +02001639 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02001640
Moran Peker41deec42018-04-04 15:43:05 +03001641 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001642 operation->key_set = 0;
1643 operation->iv_set = 0;
1644 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001645 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001646 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001647
Moran Peker395db872018-05-31 14:07:14 +03001648 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001649}
1650
Gilles Peskinea0655c32018-04-30 17:06:50 +02001651
mohammad16038cc1cee2018-03-28 01:21:33 +03001652/****************************************************************/
1653/* Key Policy */
1654/****************************************************************/
1655
1656void psa_key_policy_init(psa_key_policy_t *policy)
1657{
mohammad16036df908f2018-04-02 08:34:15 -07001658 memset( policy, 0, sizeof( psa_key_policy_t ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03001659}
1660
1661void psa_key_policy_set_usage(psa_key_policy_t *policy,
1662 psa_key_usage_t usage,
1663 psa_algorithm_t alg)
1664{
mohammad16034eed7572018-03-28 05:14:59 -07001665 policy->usage = usage;
1666 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03001667}
1668
1669psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy)
1670{
mohammad16036df908f2018-04-02 08:34:15 -07001671 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03001672}
1673
1674psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy)
1675{
mohammad16036df908f2018-04-02 08:34:15 -07001676 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03001677}
1678
1679psa_status_t psa_set_key_policy(psa_key_slot_t key,
1680 const psa_key_policy_t *policy)
1681{
1682 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03001683
1684 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
1685 return( PSA_ERROR_INVALID_ARGUMENT );
1686
1687 slot = &global_data.key_slots[key];
1688 if( slot->type != PSA_KEY_TYPE_NONE )
1689 return( PSA_ERROR_OCCUPIED_SLOT );
1690
mohammad16036df908f2018-04-02 08:34:15 -07001691 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_ENCRYPT
1692 | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN
1693 | PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07001694 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03001695
mohammad16036df908f2018-04-02 08:34:15 -07001696 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03001697
1698 return( PSA_SUCCESS );
1699}
1700
1701psa_status_t psa_get_key_policy(psa_key_slot_t key,
1702 psa_key_policy_t *policy)
1703{
1704 key_slot_t *slot;
1705
1706 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
1707 return( PSA_ERROR_INVALID_ARGUMENT );
1708
1709 slot = &global_data.key_slots[key];
mohammad16038cc1cee2018-03-28 01:21:33 +03001710
mohammad16036df908f2018-04-02 08:34:15 -07001711 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03001712
1713 return( PSA_SUCCESS );
1714}
Gilles Peskine20035e32018-02-03 22:44:14 +01001715
Gilles Peskinea0655c32018-04-30 17:06:50 +02001716
1717
mohammad1603804cd712018-03-20 22:44:08 +02001718/****************************************************************/
1719/* Key Lifetime */
1720/****************************************************************/
1721
1722psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1723 psa_key_lifetime_t *lifetime)
1724{
1725 key_slot_t *slot;
1726
1727 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1728 return( PSA_ERROR_INVALID_ARGUMENT );
1729
1730 slot = &global_data.key_slots[key];
mohammad1603804cd712018-03-20 22:44:08 +02001731
1732 *lifetime = slot->lifetime;
1733
1734 return( PSA_SUCCESS );
1735}
1736
1737psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
1738 const psa_key_lifetime_t lifetime)
1739{
1740 key_slot_t *slot;
1741
1742 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1743 return( PSA_ERROR_INVALID_ARGUMENT );
1744
mohammad1603ba178512018-03-21 04:35:20 -07001745 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
1746 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
1747 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
1748 return( PSA_ERROR_INVALID_ARGUMENT );
1749
mohammad1603804cd712018-03-20 22:44:08 +02001750 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03001751 if( slot->type != PSA_KEY_TYPE_NONE )
1752 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02001753
Moran Pekerd7326592018-05-29 16:56:39 +03001754 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07001755 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603804cd712018-03-20 22:44:08 +02001756
mohammad1603060ad8a2018-03-20 14:28:38 -07001757 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02001758
1759 return( PSA_SUCCESS );
1760}
1761
Gilles Peskine20035e32018-02-03 22:44:14 +01001762
mohammad16035955c982018-04-26 00:53:03 +03001763/****************************************************************/
1764/* AEAD */
1765/****************************************************************/
1766psa_status_t psa_aead_encrypt( psa_key_slot_t key,
1767 psa_algorithm_t alg,
1768 const uint8_t *nonce,
1769 size_t nonce_length,
1770 const uint8_t *additional_data,
1771 size_t additional_data_length,
1772 const uint8_t *plaintext,
1773 size_t plaintext_length,
1774 uint8_t *ciphertext,
1775 size_t ciphertext_size,
1776 size_t *ciphertext_length )
1777{
1778 int ret;
1779 psa_status_t status;
1780 key_slot_t *slot;
1781 psa_key_type_t key_type;
1782 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03001783 uint8_t *tag;
1784 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07001785 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03001786 const mbedtls_cipher_info_t *cipher_info = NULL;
1787
mohammad1603f08a5502018-06-03 15:05:47 +03001788 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07001789
mohammad16035955c982018-04-26 00:53:03 +03001790 status = psa_get_key_information( key, &key_type, &key_bits );
1791 if( status != PSA_SUCCESS )
1792 return( status );
1793 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03001794 if( slot->type == PSA_KEY_TYPE_NONE )
1795 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03001796
mohammad16035ed06212018-06-06 13:09:34 +03001797 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
1798 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03001799 if( cipher_info == NULL )
1800 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07001801
mohammad1603f14394b2018-06-04 14:33:19 +03001802 if( !( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
1803 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03001804
mohammad16036b4d98c2018-06-06 13:19:51 +03001805 if ( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
mohammad16035ed06212018-06-06 13:09:34 +03001806 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03001807 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03001808
mohammad16035955c982018-04-26 00:53:03 +03001809 if( alg == PSA_ALG_GCM )
1810 {
1811 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03001812 tag_length = 16;
1813
mohammad160396910d82018-06-04 14:33:00 +03001814 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
1815 return( PSA_ERROR_INVALID_ARGUMENT );
1816
mohammad160315223a82018-06-03 17:19:55 +03001817 //make sure we have place to hold the tag in the ciphertext buffer
1818 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03001819 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03001820
1821 //update the tag pointer to point to the end of the ciphertext_length
1822 tag = ciphertext + plaintext_length;
1823
mohammad16035955c982018-04-26 00:53:03 +03001824 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02001825 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03001826 slot->data.raw.data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02001827 key_bits );
mohammad16035955c982018-04-26 00:53:03 +03001828 if( ret != 0 )
1829 {
1830 mbedtls_gcm_free( &gcm );
1831 return( mbedtls_to_psa_error( ret ) );
1832 }
1833 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02001834 plaintext_length, nonce,
1835 nonce_length, additional_data,
1836 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03001837 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03001838 mbedtls_gcm_free( &gcm );
1839 }
1840 else if( alg == PSA_ALG_CCM )
1841 {
1842 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03001843 tag_length = 16;
1844
mohammad160396910d82018-06-04 14:33:00 +03001845 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
1846 return( PSA_ERROR_INVALID_ARGUMENT );
1847
mohammad160347ddf3d2018-04-26 01:11:21 +03001848 if( nonce_length < 7 || nonce_length > 13 )
1849 return( PSA_ERROR_INVALID_ARGUMENT );
1850
mohammad160315223a82018-06-03 17:19:55 +03001851 //make sure we have place to hold the tag in the ciphertext buffer
1852 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03001853 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03001854
1855 //update the tag pointer to point to the end of the ciphertext_length
1856 tag = ciphertext + plaintext_length;
1857
1858
1859
mohammad16035955c982018-04-26 00:53:03 +03001860 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02001861 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03001862 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03001863 if( ret != 0 )
1864 {
1865 mbedtls_ccm_free( &ccm );
1866 return( mbedtls_to_psa_error( ret ) );
1867 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02001868 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
1869 nonce, nonce_length, additional_data,
1870 additional_data_length,
1871 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03001872 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03001873 mbedtls_ccm_free( &ccm );
1874 }
mohammad16035c8845f2018-05-09 05:40:09 -07001875 else
1876 {
mohammad1603554faad2018-06-03 15:07:38 +03001877 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07001878 }
mohammad160315223a82018-06-03 17:19:55 +03001879
1880 if( ret != 0 )
1881 {
1882 memset( ciphertext, 0, ciphertext_size );
1883 return( mbedtls_to_psa_error( ret ) );
1884 }
1885
1886 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03001887 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03001888}
1889
Gilles Peskineee652a32018-06-01 19:23:52 +02001890/* Locate the tag in a ciphertext buffer containing the encrypted data
1891 * followed by the tag. Return the length of the part preceding the tag in
1892 * *plaintext_length. This is the size of the plaintext in modes where
1893 * the encrypted data has the same size as the plaintext, such as
1894 * CCM and GCM. */
1895static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
1896 const uint8_t *ciphertext,
1897 size_t ciphertext_length,
1898 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02001899 const uint8_t **p_tag )
1900{
1901 size_t payload_length;
1902 if( tag_length > ciphertext_length )
1903 return( PSA_ERROR_INVALID_ARGUMENT );
1904 payload_length = ciphertext_length - tag_length;
1905 if( payload_length > plaintext_size )
1906 return( PSA_ERROR_BUFFER_TOO_SMALL );
1907 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02001908 return( PSA_SUCCESS );
1909}
1910
mohammad16035955c982018-04-26 00:53:03 +03001911psa_status_t psa_aead_decrypt( psa_key_slot_t key,
1912 psa_algorithm_t alg,
1913 const uint8_t *nonce,
1914 size_t nonce_length,
1915 const uint8_t *additional_data,
1916 size_t additional_data_length,
1917 const uint8_t *ciphertext,
1918 size_t ciphertext_length,
1919 uint8_t *plaintext,
1920 size_t plaintext_size,
1921 size_t *plaintext_length )
1922{
1923 int ret;
1924 psa_status_t status;
1925 key_slot_t *slot;
1926 psa_key_type_t key_type;
1927 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02001928 const uint8_t *tag;
1929 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07001930 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03001931 const mbedtls_cipher_info_t *cipher_info = NULL;
1932
Gilles Peskineee652a32018-06-01 19:23:52 +02001933 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03001934
mohammad16035955c982018-04-26 00:53:03 +03001935 status = psa_get_key_information( key, &key_type, &key_bits );
1936 if( status != PSA_SUCCESS )
1937 return( status );
1938 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03001939 if( slot->type == PSA_KEY_TYPE_NONE )
1940 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03001941
mohammad16035ed06212018-06-06 13:09:34 +03001942 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
1943 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03001944 if( cipher_info == NULL )
1945 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603f14394b2018-06-04 14:33:19 +03001946
1947 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
1948 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03001949
mohammad1603fc614b12018-06-07 01:43:52 +03001950 if ( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
1951 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03001952 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03001953
mohammad16035955c982018-04-26 00:53:03 +03001954 if( alg == PSA_ALG_GCM )
1955 {
1956 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03001957
Gilles Peskineee652a32018-06-01 19:23:52 +02001958 tag_length = 16;
1959 status = psa_aead_unpadded_locate_tag( tag_length,
1960 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03001961 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02001962 if( status != PSA_SUCCESS )
1963 return( status );
1964
mohammad16035955c982018-04-26 00:53:03 +03001965 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02001966 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03001967 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03001968 if( ret != 0 )
1969 {
1970 mbedtls_gcm_free( &gcm );
1971 return( mbedtls_to_psa_error( ret ) );
1972 }
mohammad16035955c982018-04-26 00:53:03 +03001973
Gilles Peskineee652a32018-06-01 19:23:52 +02001974 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03001975 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02001976 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03001977 additional_data,
1978 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02001979 tag, tag_length,
1980 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03001981 mbedtls_gcm_free( &gcm );
1982 }
1983 else if( alg == PSA_ALG_CCM )
1984 {
1985 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02001986
mohammad160347ddf3d2018-04-26 01:11:21 +03001987 if( nonce_length < 7 || nonce_length > 13 )
1988 return( PSA_ERROR_INVALID_ARGUMENT );
1989
mohammad16039375f842018-06-03 14:28:24 +03001990 tag_length = 16;
1991 status = psa_aead_unpadded_locate_tag( tag_length,
1992 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03001993 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03001994 if( status != PSA_SUCCESS )
1995 return( status );
1996
mohammad16035955c982018-04-26 00:53:03 +03001997 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02001998 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03001999 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002000 if( ret != 0 )
2001 {
2002 mbedtls_ccm_free( &ccm );
2003 return( mbedtls_to_psa_error( ret ) );
2004 }
mohammad160360a64d02018-06-03 17:20:42 +03002005 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002006 nonce, nonce_length,
2007 additional_data, additional_data_length,
2008 ciphertext, plaintext,
2009 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002010 mbedtls_ccm_free( &ccm );
2011 }
mohammad160339574652018-06-01 04:39:53 -07002012 else
2013 {
mohammad1603554faad2018-06-03 15:07:38 +03002014 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002015 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002016
Gilles Peskineee652a32018-06-01 19:23:52 +02002017 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002018 memset( plaintext, 0, plaintext_size );
2019 else
2020 *plaintext_length = ciphertext_length - tag_length;
2021
Gilles Peskineee652a32018-06-01 19:23:52 +02002022 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002023}
2024
Gilles Peskinea0655c32018-04-30 17:06:50 +02002025
Gilles Peskine20035e32018-02-03 22:44:14 +01002026/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002027/* Module setup */
2028/****************************************************************/
2029
Gilles Peskinee59236f2018-01-27 23:32:46 +01002030void mbedtls_psa_crypto_free( void )
2031{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002032 size_t key;
2033 for( key = 1; key < MBEDTLS_PSA_KEY_SLOT_COUNT; key++ )
2034 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002035 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2036 mbedtls_entropy_free( &global_data.entropy );
2037 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2038}
2039
2040psa_status_t psa_crypto_init( void )
2041{
2042 int ret;
2043 const unsigned char drbg_seed[] = "PSA";
2044
2045 if( global_data.initialized != 0 )
2046 return( PSA_SUCCESS );
2047
2048 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2049 mbedtls_entropy_init( &global_data.entropy );
2050 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2051
2052 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2053 mbedtls_entropy_func,
2054 &global_data.entropy,
2055 drbg_seed, sizeof( drbg_seed ) - 1 );
2056 if( ret != 0 )
2057 goto exit;
2058
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002059 global_data.initialized = 1;
2060
Gilles Peskinee59236f2018-01-27 23:32:46 +01002061exit:
2062 if( ret != 0 )
2063 mbedtls_psa_crypto_free( );
2064 return( mbedtls_to_psa_error( ret ) );
2065}
2066
2067#endif /* MBEDTLS_PSA_CRYPTO_C */