blob: 29a541faa510010a63e35c1b41e050137f4caee3 [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
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +0300987
988static psa_status_t get_block_size_from_hash_algorithm( psa_algorithm_t alg, unsigned int *block_size, unsigned int *digest_size)
989{
990 *block_size = 0;
991 *digest_size = 0;
992
993 switch( PSA_ALG_HMAC_HASH( alg ) )
994 {
995#if defined(MBEDTLS_MD2_C)
996 case PSA_ALG_MD2:
997 *block_size = 16;
998 *digest_size = 16;
999 break;
1000#endif
1001#if defined(MBEDTLS_MD4_C)
1002 case PSA_ALG_MD4:
1003 *block_size = 64;
1004 *digest_size = 16;
1005 break;
1006#endif
1007#if defined(MBEDTLS_MD5_C)
1008 case PSA_ALG_MD5:
1009 *block_size = 64;
1010 *digest_size = 16;
1011 break;
1012#endif
1013#if defined(MBEDTLS_RIPEMD160_C)
1014 case PSA_ALG_RIPEMD160:
1015 *block_size = 64;
1016 *digest_size = 20;
1017 break;
1018#endif
1019#if defined(MBEDTLS_SHA1_C)
1020 case PSA_ALG_SHA_1:
1021 *block_size = 64;
1022 *digest_size = 20;
1023 break;
1024#endif
1025#if defined(MBEDTLS_SHA256_C)
1026 case PSA_ALG_SHA_224:
1027 *block_size = 64;
1028 *digest_size = 28;
1029 break;
1030 case PSA_ALG_SHA_256:
1031 *block_size = 64;
1032 *digest_size = 32;
1033 break;
1034#endif
1035#if defined(MBEDTLS_SHA512_C)
1036 case PSA_ALG_SHA_384:
1037 *block_size = 128;
1038 *digest_size = 48;
1039 break;
1040 case PSA_ALG_SHA_512:
1041 *block_size = 128;
1042 *digest_size = 64;
1043 break;
1044#endif
1045 default:
1046 return( PSA_ERROR_NOT_SUPPORTED );
1047 }
1048return ( PSA_SUCCESS );
1049}
1050
1051
Gilles Peskine8c9def32018-02-08 10:02:12 +01001052psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1053{
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001054 psa_status_t status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001055 switch( operation->alg )
1056 {
1057#if defined(MBEDTLS_CMAC_C)
1058 case PSA_ALG_CMAC:
1059 mbedtls_cipher_free( &operation->ctx.cmac );
1060 break;
1061#endif /* MBEDTLS_CMAC_C */
1062 default:
1063#if defined(MBEDTLS_MD_C)
1064 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001065 {
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001066 unsigned int block_size = 0;
1067 unsigned int digest_size = 0;
1068 status = get_block_size_from_hash_algorithm( operation->alg,
1069 &block_size, &digest_size);
1070 if( status != PSA_SUCCESS )
1071 return( status );
1072
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001073 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
1074 if ( operation->ctx.hmac.hmac_ctx != NULL )
1075 {
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001076 mbedtls_zeroize( operation->ctx.hmac.hmac_ctx,
Nir Sonnenschein35dfbf42018-06-07 16:20:17 +03001077 block_size);
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001078 }
1079 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001080 else
1081#endif /* MBEDTLS_MD_C */
1082 return( PSA_ERROR_NOT_SUPPORTED );
1083 }
Moran Peker41deec42018-04-04 15:43:05 +03001084
Gilles Peskine8c9def32018-02-08 10:02:12 +01001085 operation->alg = 0;
1086 operation->key_set = 0;
1087 operation->iv_set = 0;
1088 operation->iv_required = 0;
1089 operation->has_input = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001090
Gilles Peskine8c9def32018-02-08 10:02:12 +01001091 return( PSA_SUCCESS );
1092}
1093
1094psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1095 psa_key_slot_t key,
1096 psa_algorithm_t alg )
1097{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001098 int ret = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001099 psa_status_t status;
1100 key_slot_t *slot;
1101 psa_key_type_t key_type;
1102 size_t key_bits;
Nir Sonnenscheineeace0b2018-06-05 11:21:07 +03001103 size_t key_length;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001104 uint8_t* key_ptr = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001105 const mbedtls_cipher_info_t *cipher_info = NULL;
1106
1107 operation->alg = 0;
1108 operation->key_set = 0;
1109 operation->iv_set = 0;
1110 operation->iv_required = 1;
1111 operation->has_input = 0;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001112 operation->key_usage_sign = 0;
1113 operation->key_usage_verify = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001114
1115 status = psa_get_key_information( key, &key_type, &key_bits );
1116 if( status != PSA_SUCCESS )
1117 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001118
Gilles Peskine8c9def32018-02-08 10:02:12 +01001119 slot = &global_data.key_slots[key];
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001120 if (slot->type == PSA_KEY_TYPE_NONE)
1121 return(PSA_ERROR_EMPTY_SLOT);
1122
1123 key_ptr = slot->data.raw.data;
Nir Sonnenscheineeace0b2018-06-05 11:21:07 +03001124 key_length = slot->data.raw.bytes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001125
Moran Pekerd7326592018-05-29 16:56:39 +03001126 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001127 operation->key_usage_sign = 1;
1128
Moran Pekerd7326592018-05-29 16:56:39 +03001129 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001130 operation->key_usage_verify = 1;
1131
Gilles Peskine8c9def32018-02-08 10:02:12 +01001132 if( ! PSA_ALG_IS_HMAC( alg ) )
1133 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001134 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001135 if( cipher_info == NULL )
1136 return( PSA_ERROR_NOT_SUPPORTED );
1137 operation->mac_size = cipher_info->block_size;
1138 }
1139 switch( alg )
1140 {
1141#if defined(MBEDTLS_CMAC_C)
1142 case PSA_ALG_CMAC:
1143 operation->iv_required = 0;
1144 mbedtls_cipher_init( &operation->ctx.cmac );
1145 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1146 if( ret != 0 )
1147 break;
1148 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1149 slot->data.raw.data,
1150 key_bits );
1151 break;
1152#endif /* MBEDTLS_CMAC_C */
1153 default:
1154#if defined(MBEDTLS_MD_C)
1155 if( PSA_ALG_IS_HMAC( alg ) )
1156 {
Nir Sonnenschein35dfbf42018-06-07 16:20:17 +03001157 unsigned char sum[PSA_CRYPTO_MD_MAX_SIZE];
1158 unsigned char ipad[PSA_CRYPTO_MD_BLOCK_SIZE];
1159 unsigned char *opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001160 size_t i;
1161 size_t sum_size = MBEDTLS_MD_MAX_SIZE;
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001162 unsigned int block_size = 0;
1163 unsigned int digest_size = 0;
1164 status = get_block_size_from_hash_algorithm( alg,
1165 &block_size, &digest_size);
1166 if( status != PSA_SUCCESS )
1167 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001168
Gilles Peskine8c9def32018-02-08 10:02:12 +01001169 if( key_type != PSA_KEY_TYPE_HMAC )
1170 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001171
Gilles Peskine8c9def32018-02-08 10:02:12 +01001172 operation->iv_required = 0;
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001173 operation->mac_size = digest_size;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001174
1175 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1176 PSA_ALG_HMAC_HASH( alg ) );
1177 if( status != PSA_SUCCESS )
1178 goto cleanup;
1179
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001180 if( key_bits / 8 > (size_t) block_size )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001181 {
1182 status = psa_hash_update(&operation->ctx.hmac.hash_ctx,
1183 key_ptr, slot->data.raw.bytes);
1184 if( status != PSA_SUCCESS )
1185 goto cleanup;
1186 status = psa_hash_finish(&operation->ctx.hmac.hash_ctx, sum,
1187 sum_size, &sum_size);
1188 if ( status != PSA_SUCCESS )
1189 goto cleanup;
1190
Nir Sonnenscheineeace0b2018-06-05 11:21:07 +03001191 key_length = sum_size;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001192 key_ptr = sum;
1193 }
1194
Nir Sonnenschein35dfbf42018-06-07 16:20:17 +03001195 opad = ( unsigned char * ) operation->ctx.hmac.hmac_ctx;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001196
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001197 memset( ipad, 0x36, block_size );
1198 memset( opad, 0x5C, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001199
Nir Sonnenscheineeace0b2018-06-05 11:21:07 +03001200 for( i = 0; i < key_length; i++ )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001201 {
1202 ipad[i] = ( unsigned char )( ipad[i] ^ key_ptr[i] );
1203 opad[i] = ( unsigned char )( opad[i] ^ key_ptr[i] );
1204 }
1205
1206 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1207 PSA_ALG_HMAC_HASH( alg ) );
1208 if( status != PSA_SUCCESS )
1209 goto cleanup;
1210
1211 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001212 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001213 if( status != PSA_SUCCESS )
1214 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001215 break;
1216 }
1217 else
1218#endif /* MBEDTLS_MD_C */
1219 return( PSA_ERROR_NOT_SUPPORTED );
1220 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001221cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001222 /* If we reach this point, then the algorithm-specific part of the
1223 * context has at least been initialized, and may contain data that
1224 * needs to be wiped on error. */
1225 operation->alg = alg;
1226 if( ret != 0 )
1227 {
1228 psa_mac_abort( operation );
1229 return( mbedtls_to_psa_error( ret ) );
1230 }
1231 operation->key_set = 1;
Gilles Peskine47c1bc02018-03-20 17:55:04 +01001232 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001233}
1234
1235psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1236 const uint8_t *input,
1237 size_t input_length )
1238{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001239 int ret = 0 ;
1240 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001241 if( ! operation->key_set )
1242 return( PSA_ERROR_BAD_STATE );
1243 if( operation->iv_required && ! operation->iv_set )
1244 return( PSA_ERROR_BAD_STATE );
1245 operation->has_input = 1;
1246
1247 switch( operation->alg )
1248 {
1249#if defined(MBEDTLS_CMAC_C)
1250 case PSA_ALG_CMAC:
1251 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1252 input, input_length );
1253 break;
1254#endif /* MBEDTLS_CMAC_C */
1255 default:
1256#if defined(MBEDTLS_MD_C)
1257 if( PSA_ALG_IS_HMAC( operation->alg ) )
1258 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001259 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1260 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001261 }
1262 else
1263#endif /* MBEDTLS_MD_C */
1264 {
1265 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1266 }
1267 break;
1268 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001269 if ( ( ret != 0 ) || ( status != PSA_SUCCESS ) )
1270 {
1271 psa_mac_abort(operation);
1272 if (ret != 0)
1273 status = mbedtls_to_psa_error(ret);
1274 }
1275
1276 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001277}
1278
mohammad16036df908f2018-04-02 08:34:15 -07001279static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01001280 uint8_t *mac,
1281 size_t mac_size,
1282 size_t *mac_length )
1283{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001284 int ret = 0;
1285 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001286 if( ! operation->key_set )
1287 return( PSA_ERROR_BAD_STATE );
1288 if( operation->iv_required && ! operation->iv_set )
1289 return( PSA_ERROR_BAD_STATE );
1290
1291 /* Fill the output buffer with something that isn't a valid mac
1292 * (barring an attack on the mac and deliberately-crafted input),
1293 * in case the caller doesn't check the return status properly. */
1294 *mac_length = operation->mac_size;
1295 memset( mac, '!', mac_size );
1296
1297 if( mac_size < operation->mac_size )
1298 return( PSA_ERROR_BUFFER_TOO_SMALL );
1299
1300 switch( operation->alg )
1301 {
1302#if defined(MBEDTLS_CMAC_C)
1303 case PSA_ALG_CMAC:
1304 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1305 break;
1306#endif /* MBEDTLS_CMAC_C */
1307 default:
1308#if defined(MBEDTLS_MD_C)
1309 if( PSA_ALG_IS_HMAC( operation->alg ) )
1310 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001311 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1312 unsigned char *opad;
1313 size_t hash_size = 0;
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001314 unsigned int block_size = 0;
1315 unsigned int digest_size = 0;
1316 status = get_block_size_from_hash_algorithm( operation->alg,
1317 &block_size, &digest_size);
1318 if( status != PSA_SUCCESS )
1319 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001320
Nir Sonnenschein35dfbf42018-06-07 16:20:17 +03001321 opad = (unsigned char *) operation->ctx.hmac.hmac_ctx;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001322
1323 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
1324 sizeof ( tmp ), &hash_size );
1325 if( status != PSA_SUCCESS )
1326 goto cleanup;
1327
1328 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1329 PSA_ALG_HMAC_HASH( operation->alg ) );
1330 if( status != PSA_SUCCESS )
1331 goto cleanup;
1332
1333 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001334 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001335 if( status != PSA_SUCCESS )
1336 goto cleanup;
1337
1338 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
1339 hash_size);
1340 if( status != PSA_SUCCESS )
1341 goto cleanup;
1342
1343 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1344 mac_size, mac_length );
1345 if( status != PSA_SUCCESS )
1346 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001347 }
1348 else
1349#endif /* MBEDTLS_MD_C */
1350 {
1351 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1352 }
1353 break;
1354 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001355cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001356
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001357 if( ( ret == 0 ) && (status == PSA_SUCCESS) )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001358 {
1359 return( psa_mac_abort( operation ) );
1360 }
1361 else
1362 {
1363 psa_mac_abort( operation );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001364 if (ret != 0)
1365 status = mbedtls_to_psa_error(ret);
1366
1367 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001368 }
1369}
1370
mohammad16036df908f2018-04-02 08:34:15 -07001371psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1372 uint8_t *mac,
1373 size_t mac_size,
1374 size_t *mac_length )
1375{
1376 if( !( operation->key_usage_sign ) )
1377 return( PSA_ERROR_NOT_PERMITTED );
1378
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001379 return( psa_mac_finish_internal(operation, mac,
1380 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001381}
1382
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383#define MBEDTLS_PSA_MAC_MAX_SIZE \
1384 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1385 MBEDTLS_MD_MAX_SIZE : \
1386 MBEDTLS_MAX_BLOCK_LENGTH )
1387psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1388 const uint8_t *mac,
1389 size_t mac_length )
1390{
1391 uint8_t actual_mac[MBEDTLS_PSA_MAC_MAX_SIZE];
1392 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001393 psa_status_t status;
1394
1395 if( !( operation->key_usage_verify ) )
1396 return( PSA_ERROR_NOT_PERMITTED );
1397
1398 status = psa_mac_finish_internal( operation,
1399 actual_mac, sizeof( actual_mac ),
1400 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001401 if( status != PSA_SUCCESS )
1402 return( status );
1403 if( actual_mac_length != mac_length )
1404 return( PSA_ERROR_INVALID_SIGNATURE );
1405 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1406 return( PSA_ERROR_INVALID_SIGNATURE );
1407 return( PSA_SUCCESS );
1408}
1409
1410
Gilles Peskine20035e32018-02-03 22:44:14 +01001411
1412
1413/****************************************************************/
1414/* Asymmetric cryptography */
1415/****************************************************************/
1416
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001417/* Decode the hash algorithm from alg and store the mbedtls encoding in
1418 * md_alg. Verify that the hash length is consistent. */
1419static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1420 size_t hash_length,
1421 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001422{
Gilles Peskine61b91d42018-06-08 16:09:36 +02001423 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
1424 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1425 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1426 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001427 {
1428#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001429 if( hash_length > UINT_MAX )
1430 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001431#endif
1432 }
1433 else
1434 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001435 if( mbedtls_md_get_size( md_info ) != hash_length )
1436 return( PSA_ERROR_INVALID_ARGUMENT );
1437 if( md_info == NULL )
1438 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001439 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001440 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001441}
1442
Gilles Peskine61b91d42018-06-08 16:09:36 +02001443psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1444 psa_algorithm_t alg,
1445 const uint8_t *hash,
1446 size_t hash_length,
1447 const uint8_t *salt,
1448 size_t salt_length,
1449 uint8_t *signature,
1450 size_t signature_size,
1451 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001452{
1453 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001454 psa_status_t status;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001455 *signature_length = 0;
1456 (void) salt;
1457 (void) salt_length;
1458
Gilles Peskine20035e32018-02-03 22:44:14 +01001459 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1460 return( PSA_ERROR_EMPTY_SLOT );
1461 slot = &global_data.key_slots[key];
1462 if( slot->type == PSA_KEY_TYPE_NONE )
1463 return( PSA_ERROR_EMPTY_SLOT );
1464 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1465 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001466 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
mohammad160306e79202018-03-28 13:17:44 +03001467 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001468
Gilles Peskine20035e32018-02-03 22:44:14 +01001469#if defined(MBEDTLS_RSA_C)
1470 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1471 {
1472 mbedtls_rsa_context *rsa = slot->data.rsa;
1473 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001474 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001475 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001476 if( status != PSA_SUCCESS )
1477 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001478
Gilles Peskine20035e32018-02-03 22:44:14 +01001479 if( signature_size < rsa->len )
1480 return( PSA_ERROR_BUFFER_TOO_SMALL );
1481#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001482 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001483 {
1484 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1485 MBEDTLS_MD_NONE );
1486 ret = mbedtls_rsa_pkcs1_sign( rsa,
1487 mbedtls_ctr_drbg_random,
1488 &global_data.ctr_drbg,
1489 MBEDTLS_RSA_PRIVATE,
1490 md_alg, hash_length, hash,
1491 signature );
1492 }
1493 else
1494#endif /* MBEDTLS_PKCS1_V15 */
1495#if defined(MBEDTLS_PKCS1_V21)
1496 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1497 {
1498 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1499 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1500 mbedtls_ctr_drbg_random,
1501 &global_data.ctr_drbg,
1502 MBEDTLS_RSA_PRIVATE,
1503 md_alg, hash_length, hash,
1504 signature );
1505 }
1506 else
1507#endif /* MBEDTLS_PKCS1_V21 */
1508 {
1509 return( PSA_ERROR_INVALID_ARGUMENT );
1510 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001511 if( ret == 0 )
1512 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001513 return( mbedtls_to_psa_error( ret ) );
1514 }
1515 else
1516#endif /* defined(MBEDTLS_RSA_C) */
1517#if defined(MBEDTLS_ECP_C)
1518 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1519 {
itayzafrir5c753392018-05-08 11:18:38 +03001520 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1521 int ret;
1522 const mbedtls_md_info_t *md_info;
1523 mbedtls_md_type_t md_alg;
1524 if( signature_size < PSA_ECDSA_SIGNATURE_SIZE( ecdsa->grp.pbits ) )
1525 return( PSA_ERROR_BUFFER_TOO_SMALL );
1526 md_info = mbedtls_md_info_from_psa( alg );
1527 md_alg = mbedtls_md_get_type( md_info );
1528 ret = mbedtls_ecdsa_write_signature( ecdsa, md_alg, hash, hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001529 signature, signature_length,
1530 mbedtls_ctr_drbg_random,
1531 &global_data.ctr_drbg );
itayzafrir5c753392018-05-08 11:18:38 +03001532 return( mbedtls_to_psa_error( ret ) );
1533 }
1534 else
1535#endif /* defined(MBEDTLS_ECP_C) */
1536 {
Gilles Peskine20035e32018-02-03 22:44:14 +01001537 return( PSA_ERROR_NOT_SUPPORTED );
1538 }
itayzafrir5c753392018-05-08 11:18:38 +03001539}
1540
1541psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1542 psa_algorithm_t alg,
1543 const uint8_t *hash,
1544 size_t hash_length,
1545 const uint8_t *salt,
1546 size_t salt_length,
1547 uint8_t *signature,
1548 size_t signature_size )
1549{
1550 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001551 psa_status_t status;
itayzafrir5c753392018-05-08 11:18:38 +03001552 (void) salt;
1553 (void) salt_length;
1554
1555 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1556 return( PSA_ERROR_INVALID_ARGUMENT );
1557 slot = &global_data.key_slots[key];
1558 if( slot->type == PSA_KEY_TYPE_NONE )
1559 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001560 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001561 return( PSA_ERROR_NOT_PERMITTED );
1562
Gilles Peskine61b91d42018-06-08 16:09:36 +02001563#if defined(MBEDTLS_RSA_C)
Nir Sonnenschein1c2a7ea2018-06-05 15:01:42 +03001564 if( ( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) ||
1565 ( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001566 {
1567 mbedtls_rsa_context *rsa = slot->data.rsa;
1568 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001569 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001570 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001571 if( status != PSA_SUCCESS )
1572 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001573
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001574 if( signature_size < rsa->len )
1575 return( PSA_ERROR_BUFFER_TOO_SMALL );
1576#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001577 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001578 {
1579 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1580 MBEDTLS_MD_NONE );
1581
1582 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001583 mbedtls_ctr_drbg_random,
1584 &global_data.ctr_drbg,
1585 MBEDTLS_RSA_PUBLIC,
1586 md_alg,
1587 hash_length,
1588 hash,
1589 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001590
1591 }
1592 else
1593#endif /* MBEDTLS_PKCS1_V15 */
1594#if defined(MBEDTLS_PKCS1_V21)
1595 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1596 {
Gilles Peskinebeb49482018-06-08 17:44:35 +02001597 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1598 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1599 mbedtls_ctr_drbg_random,
1600 &global_data.ctr_drbg,
1601 MBEDTLS_RSA_PUBLIC,
1602 md_alg, hash_length, hash,
1603 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001604 }
1605 else
1606#endif /* MBEDTLS_PKCS1_V21 */
1607 {
1608 return( PSA_ERROR_INVALID_ARGUMENT );
1609 }
1610 return( mbedtls_to_psa_error( ret ) );
1611 }
1612 else
1613#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001614#if defined(MBEDTLS_ECP_C)
1615 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1616 {
1617 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1618 int ret;
Nir Sonnenscheinc4602912018-06-04 16:42:18 +03001619 (void)alg;
Gilles Peskine61b91d42018-06-08 16:09:36 +02001620 ret = mbedtls_ecdsa_read_signature( ecdsa, hash, hash_length,
1621 signature, signature_size );
itayzafrir5c753392018-05-08 11:18:38 +03001622 return( mbedtls_to_psa_error( ret ) );
1623 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001624 else
1625#endif /* defined(MBEDTLS_ECP_C) */
1626 {
1627 return( PSA_ERROR_NOT_SUPPORTED );
1628 }
1629}
1630
Gilles Peskine61b91d42018-06-08 16:09:36 +02001631psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
1632 psa_algorithm_t alg,
1633 const uint8_t *input,
1634 size_t input_length,
1635 const uint8_t *salt,
1636 size_t salt_length,
1637 uint8_t *output,
1638 size_t output_size,
1639 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001640{
1641 key_slot_t *slot;
1642 (void) salt;
1643 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001644 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001645
1646 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03001647 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001648 slot = &global_data.key_slots[key];
1649 if( slot->type == PSA_KEY_TYPE_NONE )
1650 return( PSA_ERROR_EMPTY_SLOT );
1651 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1652 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001653 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
1654 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001655
1656#if defined(MBEDTLS_RSA_C)
Nir Sonnenschein1c2a7ea2018-06-05 15:01:42 +03001657 if( ( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) ||
Gilles Peskine61b91d42018-06-08 16:09:36 +02001658 ( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001659 {
1660 mbedtls_rsa_context *rsa = slot->data.rsa;
1661 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001662 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02001663 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001664#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001665 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001666 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001667 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
1668 mbedtls_ctr_drbg_random,
1669 &global_data.ctr_drbg,
1670 MBEDTLS_RSA_PUBLIC,
1671 input_length,
1672 input,
1673 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001674 }
1675 else
1676#endif /* MBEDTLS_PKCS1_V15 */
1677#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001678 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001679 {
1680 return( PSA_ERROR_NOT_SUPPORTED );
1681 }
1682 else
1683#endif /* MBEDTLS_PKCS1_V21 */
1684 {
1685 return( PSA_ERROR_INVALID_ARGUMENT );
1686 }
1687 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001688 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001689 return( mbedtls_to_psa_error( ret ) );
1690 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001691 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001692#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001693 {
1694 return( PSA_ERROR_NOT_SUPPORTED );
1695 }
1696
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001697}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001698
Gilles Peskine61b91d42018-06-08 16:09:36 +02001699psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
1700 psa_algorithm_t alg,
1701 const uint8_t *input,
1702 size_t input_length,
1703 const uint8_t *salt,
1704 size_t salt_length,
1705 uint8_t *output,
1706 size_t output_size,
1707 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001708{
1709 key_slot_t *slot;
1710 (void) salt;
1711 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001712 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001713
1714 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
1715 return( PSA_ERROR_EMPTY_SLOT );
1716 slot = &global_data.key_slots[key];
1717 if( slot->type == PSA_KEY_TYPE_NONE )
1718 return( PSA_ERROR_EMPTY_SLOT );
1719 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1720 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001721 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
1722 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001723
1724#if defined(MBEDTLS_RSA_C)
1725 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1726 {
1727 mbedtls_rsa_context *rsa = slot->data.rsa;
1728 int ret;
1729
Gilles Peskinec4def2f2018-06-08 17:53:48 +02001730 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001731 return( PSA_ERROR_INVALID_ARGUMENT );
1732
1733#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001734 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001735 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001736 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
1737 mbedtls_ctr_drbg_random,
1738 &global_data.ctr_drbg,
1739 MBEDTLS_RSA_PRIVATE,
1740 output_length,
1741 input,
1742 output,
1743 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001744 }
1745 else
1746#endif /* MBEDTLS_PKCS1_V15 */
1747#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001748 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001749 {
1750 return( PSA_ERROR_NOT_SUPPORTED );
1751 }
1752 else
1753#endif /* MBEDTLS_PKCS1_V21 */
1754 {
1755 return( PSA_ERROR_INVALID_ARGUMENT );
1756 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001757
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001758 return( mbedtls_to_psa_error( ret ) );
1759 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001760 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001761#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001762 {
1763 return( PSA_ERROR_NOT_SUPPORTED );
1764 }
1765
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001766}
Gilles Peskine20035e32018-02-03 22:44:14 +01001767
mohammad1603503973b2018-03-12 15:59:30 +02001768/****************************************************************/
1769/* Symmetric cryptography */
1770/****************************************************************/
1771
Gilles Peskinee553c652018-06-04 16:22:46 +02001772static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
1773 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02001774 psa_algorithm_t alg,
1775 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02001776{
1777 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1778 psa_status_t status;
1779 key_slot_t *slot;
1780 psa_key_type_t key_type;
1781 size_t key_bits;
1782 const mbedtls_cipher_info_t *cipher_info = NULL;
1783
Moran Peker41deec42018-04-04 15:43:05 +03001784 operation->alg = alg;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001785 operation->key_set = 0;
1786 operation->iv_set = 0;
1787 operation->iv_required = 1;
1788 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001789 operation->block_size = 0;
mohammad1603503973b2018-03-12 15:59:30 +02001790
1791 status = psa_get_key_information( key, &key_type, &key_bits );
1792 if( status != PSA_SUCCESS )
1793 return( status );
1794 slot = &global_data.key_slots[key];
1795
Gilles Peskinebb1072f2018-06-08 18:46:05 +02001796 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02001797 if( cipher_info == NULL )
1798 return( PSA_ERROR_NOT_SUPPORTED );
1799
mohammad1603503973b2018-03-12 15:59:30 +02001800 mbedtls_cipher_init( &operation->ctx.cipher );
1801 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03001802 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001803 {
1804 psa_cipher_abort( operation );
1805 return( mbedtls_to_psa_error( ret ) );
1806 }
1807
1808 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data,
Gilles Peskinee553c652018-06-04 16:22:46 +02001809 key_bits, cipher_operation );
Moran Peker41deec42018-04-04 15:43:05 +03001810 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001811 {
1812 psa_cipher_abort( operation );
1813 return( mbedtls_to_psa_error( ret ) );
1814 }
1815
mohammad16038481e742018-03-18 13:57:31 +02001816#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03001817 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02001818 {
Gilles Peskine53514202018-06-06 15:11:46 +02001819 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1820 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02001821
Moran Pekera28258c2018-05-29 16:25:04 +03001822 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02001823 {
1824 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
1825 mode = MBEDTLS_PADDING_PKCS7;
1826 break;
1827 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
1828 mode = MBEDTLS_PADDING_NONE;
1829 break;
1830 default:
Moran Pekerae382792018-05-31 14:06:17 +03001831 psa_cipher_abort( operation );
1832 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02001833 }
1834 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03001835 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03001836 {
1837 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02001838 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03001839 }
mohammad16038481e742018-03-18 13:57:31 +02001840 }
1841#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
1842
mohammad1603503973b2018-03-12 15:59:30 +02001843 operation->key_set = 1;
1844 operation->alg = alg;
Gilles Peskine7e928852018-06-04 16:23:10 +02001845 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
1846 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
1847 1 );
Moran Peker41deec42018-04-04 15:43:05 +03001848 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || ( alg == PSA_ALG_CTR ) )
mohammad16038481e742018-03-18 13:57:31 +02001849 {
mohammad160389e0f462018-04-12 08:48:45 +03001850 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02001851 }
mohammad1603503973b2018-03-12 15:59:30 +02001852
Moran Peker395db872018-05-31 14:07:14 +03001853 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001854}
1855
Gilles Peskinee553c652018-06-04 16:22:46 +02001856psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
1857 psa_key_slot_t key,
1858 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02001859{
Moran Pekera28258c2018-05-29 16:25:04 +03001860 return psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT );
mohammad16038481e742018-03-18 13:57:31 +02001861}
1862
Gilles Peskinee553c652018-06-04 16:22:46 +02001863psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
1864 psa_key_slot_t key,
1865 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02001866{
Moran Pekera28258c2018-05-29 16:25:04 +03001867 return psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT );
mohammad16038481e742018-03-18 13:57:31 +02001868}
1869
Gilles Peskinee553c652018-06-04 16:22:46 +02001870psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
1871 unsigned char *iv,
1872 size_t iv_size,
1873 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02001874{
Moran Peker41deec42018-04-04 15:43:05 +03001875 int ret = PSA_SUCCESS;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001876 if( operation->iv_set || !( operation->iv_required ) )
Moran Peker41deec42018-04-04 15:43:05 +03001877 return( PSA_ERROR_BAD_STATE );
1878 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02001879 {
Moran Peker41deec42018-04-04 15:43:05 +03001880 ret = PSA_ERROR_BUFFER_TOO_SMALL;
1881 goto exit;
1882 }
Gilles Peskine7e928852018-06-04 16:23:10 +02001883 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
1884 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03001885 if( ret != 0 )
1886 {
1887 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02001888 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02001889 }
Gilles Peskinee553c652018-06-04 16:22:46 +02001890
mohammad16038481e742018-03-18 13:57:31 +02001891 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03001892 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03001893
Moran Peker395db872018-05-31 14:07:14 +03001894exit:
1895 if( ret != PSA_SUCCESS )
1896 psa_cipher_abort( operation );
1897 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02001898}
1899
Gilles Peskinee553c652018-06-04 16:22:46 +02001900psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
1901 const unsigned char *iv,
1902 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02001903{
Moran Peker41deec42018-04-04 15:43:05 +03001904 int ret = PSA_SUCCESS;
Moran Pekerad9d82c2018-04-30 12:31:04 +03001905 if( operation->iv_set || !( operation->iv_required ) )
Moran Peker41deec42018-04-04 15:43:05 +03001906 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03001907 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03001908 {
Moran Pekerae382792018-05-31 14:06:17 +03001909 psa_cipher_abort( operation );
1910 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03001911 }
mohammad1603503973b2018-03-12 15:59:30 +02001912 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03001913 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001914 {
1915 psa_cipher_abort( operation );
1916 return( mbedtls_to_psa_error( ret ) );
1917 }
1918
1919 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02001920
Moran Peker395db872018-05-31 14:07:14 +03001921 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001922}
1923
Gilles Peskinee553c652018-06-04 16:22:46 +02001924psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
1925 const uint8_t *input,
1926 size_t input_length,
1927 unsigned char *output,
1928 size_t output_size,
1929 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02001930{
1931 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02001932 size_t expected_output_size;
1933 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
1934 {
1935 /* Take the unprocessed partial block left over from previous
1936 * update calls, if any, plus the input to this call. Remove
1937 * the last partial block, if any. You get the data that will be
1938 * output in this call. */
1939 expected_output_size =
1940 ( operation->ctx.cipher.unprocessed_len + input_length )
1941 / operation->block_size * operation->block_size;
1942 }
1943 else
1944 {
1945 expected_output_size = input_length;
1946 }
1947 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03001948 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02001949
mohammad1603503973b2018-03-12 15:59:30 +02001950 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02001951 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03001952 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001953 {
1954 psa_cipher_abort( operation );
1955 return( mbedtls_to_psa_error( ret ) );
1956 }
1957
Moran Peker395db872018-05-31 14:07:14 +03001958 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02001959}
1960
Gilles Peskinee553c652018-06-04 16:22:46 +02001961psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
1962 uint8_t *output,
1963 size_t output_size,
1964 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02001965{
1966 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02001967 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03001968
mohammad1603503973b2018-03-12 15:59:30 +02001969 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03001970 {
Moran Peker7cb22b82018-06-05 11:40:02 +03001971 psa_cipher_abort( operation );
1972 return( PSA_ERROR_BAD_STATE );
1973 }
1974 if( operation->iv_required && ! operation->iv_set )
1975 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001976 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03001977 return( PSA_ERROR_BAD_STATE );
1978 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001979 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
1980 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03001981 {
Gilles Peskine53514202018-06-06 15:11:46 +02001982 psa_algorithm_t padding_mode =
1983 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03001984 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02001985 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001986 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03001987 return( PSA_ERROR_TAMPERING_DETECTED );
1988 }
Gilles Peskine53514202018-06-06 15:11:46 +02001989 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03001990 {
1991 if( operation->ctx.cipher.unprocessed_len != 0 )
1992 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02001993 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03001994 return( PSA_ERROR_INVALID_ARGUMENT );
1995 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02001996 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03001997 }
1998
1999 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002000 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002001 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002002 {
2003 psa_cipher_abort( operation );
2004 return( mbedtls_to_psa_error( ret ) );
2005 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002006 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002007 memcpy( output, temp_output_buffer, *output_length );
2008 else
2009 {
2010 psa_cipher_abort( operation );
2011 return( PSA_ERROR_BUFFER_TOO_SMALL );
2012 }
mohammad1603503973b2018-03-12 15:59:30 +02002013
Moran Peker4c80d832018-04-22 20:15:31 +03002014 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002015}
2016
Gilles Peskinee553c652018-06-04 16:22:46 +02002017psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2018{
mohammad1603503973b2018-03-12 15:59:30 +02002019 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002020
Moran Peker41deec42018-04-04 15:43:05 +03002021 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002022 operation->key_set = 0;
2023 operation->iv_set = 0;
2024 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002025 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002026 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002027
Moran Peker395db872018-05-31 14:07:14 +03002028 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002029}
2030
Gilles Peskinea0655c32018-04-30 17:06:50 +02002031
mohammad16038cc1cee2018-03-28 01:21:33 +03002032/****************************************************************/
2033/* Key Policy */
2034/****************************************************************/
2035
2036void psa_key_policy_init(psa_key_policy_t *policy)
2037{
mohammad16036df908f2018-04-02 08:34:15 -07002038 memset( policy, 0, sizeof( psa_key_policy_t ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002039}
2040
2041void psa_key_policy_set_usage(psa_key_policy_t *policy,
2042 psa_key_usage_t usage,
2043 psa_algorithm_t alg)
2044{
mohammad16034eed7572018-03-28 05:14:59 -07002045 policy->usage = usage;
2046 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002047}
2048
2049psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy)
2050{
mohammad16036df908f2018-04-02 08:34:15 -07002051 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002052}
2053
2054psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy)
2055{
mohammad16036df908f2018-04-02 08:34:15 -07002056 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002057}
2058
2059psa_status_t psa_set_key_policy(psa_key_slot_t key,
2060 const psa_key_policy_t *policy)
2061{
2062 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002063
2064 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
2065 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002066
mohammad16038cc1cee2018-03-28 01:21:33 +03002067 slot = &global_data.key_slots[key];
2068 if( slot->type != PSA_KEY_TYPE_NONE )
2069 return( PSA_ERROR_OCCUPIED_SLOT );
2070
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002071 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_ENCRYPT
2072 | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN
mohammad16036df908f2018-04-02 08:34:15 -07002073 | PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002074 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002075
mohammad16036df908f2018-04-02 08:34:15 -07002076 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002077
2078 return( PSA_SUCCESS );
2079}
2080
2081psa_status_t psa_get_key_policy(psa_key_slot_t key,
2082 psa_key_policy_t *policy)
2083{
2084 key_slot_t *slot;
2085
2086 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
2087 return( PSA_ERROR_INVALID_ARGUMENT );
2088
2089 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002090
mohammad16036df908f2018-04-02 08:34:15 -07002091 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002092
2093 return( PSA_SUCCESS );
2094}
Gilles Peskine20035e32018-02-03 22:44:14 +01002095
Gilles Peskinea0655c32018-04-30 17:06:50 +02002096
2097
mohammad1603804cd712018-03-20 22:44:08 +02002098/****************************************************************/
2099/* Key Lifetime */
2100/****************************************************************/
2101
2102psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
2103 psa_key_lifetime_t *lifetime)
2104{
2105 key_slot_t *slot;
2106
2107 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
2108 return( PSA_ERROR_INVALID_ARGUMENT );
2109
2110 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002111
mohammad1603804cd712018-03-20 22:44:08 +02002112 *lifetime = slot->lifetime;
2113
2114 return( PSA_SUCCESS );
2115}
2116
2117psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
2118 const psa_key_lifetime_t lifetime)
2119{
2120 key_slot_t *slot;
2121
2122 if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT )
2123 return( PSA_ERROR_INVALID_ARGUMENT );
2124
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002125 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2126 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002127 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2128 return( PSA_ERROR_INVALID_ARGUMENT );
2129
mohammad1603804cd712018-03-20 22:44:08 +02002130 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002131 if( slot->type != PSA_KEY_TYPE_NONE )
2132 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002133
Moran Pekerd7326592018-05-29 16:56:39 +03002134 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002135 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002136
mohammad1603060ad8a2018-03-20 14:28:38 -07002137 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002138
2139 return( PSA_SUCCESS );
2140}
2141
Gilles Peskine20035e32018-02-03 22:44:14 +01002142
mohammad16035955c982018-04-26 00:53:03 +03002143/****************************************************************/
2144/* AEAD */
2145/****************************************************************/
2146psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2147 psa_algorithm_t alg,
2148 const uint8_t *nonce,
2149 size_t nonce_length,
2150 const uint8_t *additional_data,
2151 size_t additional_data_length,
2152 const uint8_t *plaintext,
2153 size_t plaintext_length,
2154 uint8_t *ciphertext,
2155 size_t ciphertext_size,
2156 size_t *ciphertext_length )
2157{
2158 int ret;
2159 psa_status_t status;
2160 key_slot_t *slot;
2161 psa_key_type_t key_type;
2162 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002163 uint8_t *tag;
2164 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002165 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002166 const mbedtls_cipher_info_t *cipher_info = NULL;
2167
mohammad1603f08a5502018-06-03 15:05:47 +03002168 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002169
mohammad16035955c982018-04-26 00:53:03 +03002170 status = psa_get_key_information( key, &key_type, &key_bits );
2171 if( status != PSA_SUCCESS )
2172 return( status );
2173 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002174 if( slot->type == PSA_KEY_TYPE_NONE )
2175 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002176
mohammad16035ed06212018-06-06 13:09:34 +03002177 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2178 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002179 if( cipher_info == NULL )
2180 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002181
mohammad1603f14394b2018-06-04 14:33:19 +03002182 if( !( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
2183 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002184
mohammad16036b4d98c2018-06-06 13:19:51 +03002185 if ( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
mohammad16035ed06212018-06-06 13:09:34 +03002186 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002187 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002188
mohammad16035955c982018-04-26 00:53:03 +03002189 if( alg == PSA_ALG_GCM )
2190 {
2191 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002192 tag_length = 16;
2193
mohammad160396910d82018-06-04 14:33:00 +03002194 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2195 return( PSA_ERROR_INVALID_ARGUMENT );
2196
mohammad160315223a82018-06-03 17:19:55 +03002197 //make sure we have place to hold the tag in the ciphertext buffer
2198 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002199 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002200
2201 //update the tag pointer to point to the end of the ciphertext_length
2202 tag = ciphertext + plaintext_length;
2203
mohammad16035955c982018-04-26 00:53:03 +03002204 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002205 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002206 slot->data.raw.data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002207 key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002208 if( ret != 0 )
2209 {
2210 mbedtls_gcm_free( &gcm );
2211 return( mbedtls_to_psa_error( ret ) );
2212 }
2213 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002214 plaintext_length, nonce,
2215 nonce_length, additional_data,
2216 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002217 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002218 mbedtls_gcm_free( &gcm );
2219 }
2220 else if( alg == PSA_ALG_CCM )
2221 {
2222 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002223 tag_length = 16;
2224
mohammad160396910d82018-06-04 14:33:00 +03002225 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2226 return( PSA_ERROR_INVALID_ARGUMENT );
2227
mohammad160347ddf3d2018-04-26 01:11:21 +03002228 if( nonce_length < 7 || nonce_length > 13 )
2229 return( PSA_ERROR_INVALID_ARGUMENT );
2230
mohammad160315223a82018-06-03 17:19:55 +03002231 //make sure we have place to hold the tag in the ciphertext buffer
2232 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002233 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002234
2235 //update the tag pointer to point to the end of the ciphertext_length
2236 tag = ciphertext + plaintext_length;
2237
2238
2239
mohammad16035955c982018-04-26 00:53:03 +03002240 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002241 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002242 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002243 if( ret != 0 )
2244 {
2245 mbedtls_ccm_free( &ccm );
2246 return( mbedtls_to_psa_error( ret ) );
2247 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002248 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
2249 nonce, nonce_length, additional_data,
2250 additional_data_length,
2251 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002252 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002253 mbedtls_ccm_free( &ccm );
2254 }
mohammad16035c8845f2018-05-09 05:40:09 -07002255 else
2256 {
mohammad1603554faad2018-06-03 15:07:38 +03002257 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002258 }
mohammad160315223a82018-06-03 17:19:55 +03002259
2260 if( ret != 0 )
2261 {
2262 memset( ciphertext, 0, ciphertext_size );
2263 return( mbedtls_to_psa_error( ret ) );
2264 }
2265
2266 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002267 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002268}
2269
Gilles Peskineee652a32018-06-01 19:23:52 +02002270/* Locate the tag in a ciphertext buffer containing the encrypted data
2271 * followed by the tag. Return the length of the part preceding the tag in
2272 * *plaintext_length. This is the size of the plaintext in modes where
2273 * the encrypted data has the same size as the plaintext, such as
2274 * CCM and GCM. */
2275static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2276 const uint8_t *ciphertext,
2277 size_t ciphertext_length,
2278 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002279 const uint8_t **p_tag )
2280{
2281 size_t payload_length;
2282 if( tag_length > ciphertext_length )
2283 return( PSA_ERROR_INVALID_ARGUMENT );
2284 payload_length = ciphertext_length - tag_length;
2285 if( payload_length > plaintext_size )
2286 return( PSA_ERROR_BUFFER_TOO_SMALL );
2287 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002288 return( PSA_SUCCESS );
2289}
2290
mohammad16035955c982018-04-26 00:53:03 +03002291psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2292 psa_algorithm_t alg,
2293 const uint8_t *nonce,
2294 size_t nonce_length,
2295 const uint8_t *additional_data,
2296 size_t additional_data_length,
2297 const uint8_t *ciphertext,
2298 size_t ciphertext_length,
2299 uint8_t *plaintext,
2300 size_t plaintext_size,
2301 size_t *plaintext_length )
2302{
2303 int ret;
2304 psa_status_t status;
2305 key_slot_t *slot;
2306 psa_key_type_t key_type;
2307 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002308 const uint8_t *tag;
2309 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002310 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002311 const mbedtls_cipher_info_t *cipher_info = NULL;
2312
Gilles Peskineee652a32018-06-01 19:23:52 +02002313 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002314
mohammad16035955c982018-04-26 00:53:03 +03002315 status = psa_get_key_information( key, &key_type, &key_bits );
2316 if( status != PSA_SUCCESS )
2317 return( status );
2318 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002319 if( slot->type == PSA_KEY_TYPE_NONE )
2320 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002321
mohammad16035ed06212018-06-06 13:09:34 +03002322 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2323 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002324 if( cipher_info == NULL )
2325 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603f14394b2018-06-04 14:33:19 +03002326
2327 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2328 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002329
mohammad1603fc614b12018-06-07 01:43:52 +03002330 if ( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2331 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002332 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002333
mohammad16035955c982018-04-26 00:53:03 +03002334 if( alg == PSA_ALG_GCM )
2335 {
2336 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002337
Gilles Peskineee652a32018-06-01 19:23:52 +02002338 tag_length = 16;
2339 status = psa_aead_unpadded_locate_tag( tag_length,
2340 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002341 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002342 if( status != PSA_SUCCESS )
2343 return( status );
2344
mohammad16035955c982018-04-26 00:53:03 +03002345 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002346 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002347 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002348 if( ret != 0 )
2349 {
2350 mbedtls_gcm_free( &gcm );
2351 return( mbedtls_to_psa_error( ret ) );
2352 }
mohammad16035955c982018-04-26 00:53:03 +03002353
Gilles Peskineee652a32018-06-01 19:23:52 +02002354 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002355 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002356 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002357 additional_data,
2358 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002359 tag, tag_length,
2360 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002361 mbedtls_gcm_free( &gcm );
2362 }
2363 else if( alg == PSA_ALG_CCM )
2364 {
2365 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002366
mohammad160347ddf3d2018-04-26 01:11:21 +03002367 if( nonce_length < 7 || nonce_length > 13 )
2368 return( PSA_ERROR_INVALID_ARGUMENT );
2369
mohammad16039375f842018-06-03 14:28:24 +03002370 tag_length = 16;
2371 status = psa_aead_unpadded_locate_tag( tag_length,
2372 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002373 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002374 if( status != PSA_SUCCESS )
2375 return( status );
2376
mohammad16035955c982018-04-26 00:53:03 +03002377 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002378 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002379 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002380 if( ret != 0 )
2381 {
2382 mbedtls_ccm_free( &ccm );
2383 return( mbedtls_to_psa_error( ret ) );
2384 }
mohammad160360a64d02018-06-03 17:20:42 +03002385 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002386 nonce, nonce_length,
2387 additional_data, additional_data_length,
2388 ciphertext, plaintext,
2389 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002390 mbedtls_ccm_free( &ccm );
2391 }
mohammad160339574652018-06-01 04:39:53 -07002392 else
2393 {
mohammad1603554faad2018-06-03 15:07:38 +03002394 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002395 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002396
Gilles Peskineee652a32018-06-01 19:23:52 +02002397 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002398 memset( plaintext, 0, plaintext_size );
2399 else
2400 *plaintext_length = ciphertext_length - tag_length;
2401
Gilles Peskineee652a32018-06-01 19:23:52 +02002402 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002403}
2404
Gilles Peskinea0655c32018-04-30 17:06:50 +02002405
Gilles Peskine20035e32018-02-03 22:44:14 +01002406/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002407/* Module setup */
2408/****************************************************************/
2409
Gilles Peskinee59236f2018-01-27 23:32:46 +01002410void mbedtls_psa_crypto_free( void )
2411{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002412 size_t key;
2413 for( key = 1; key < MBEDTLS_PSA_KEY_SLOT_COUNT; key++ )
2414 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002415 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2416 mbedtls_entropy_free( &global_data.entropy );
2417 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2418}
2419
2420psa_status_t psa_crypto_init( void )
2421{
2422 int ret;
2423 const unsigned char drbg_seed[] = "PSA";
2424
2425 if( global_data.initialized != 0 )
2426 return( PSA_SUCCESS );
2427
2428 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2429 mbedtls_entropy_init( &global_data.entropy );
2430 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2431
2432 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2433 mbedtls_entropy_func,
2434 &global_data.entropy,
2435 drbg_seed, sizeof( drbg_seed ) - 1 );
2436 if( ret != 0 )
2437 goto exit;
2438
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002439 global_data.initialized = 1;
2440
Gilles Peskinee59236f2018-01-27 23:32:46 +01002441exit:
2442 if( ret != 0 )
2443 mbedtls_psa_crypto_free( );
2444 return( mbedtls_to_psa_error( ret ) );
2445}
2446
2447#endif /* MBEDTLS_PSA_CRYPTO_C */