blob: dc6f2da4973ba5f8fc4da5fdc36dc2b1a9848dbc [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)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030029/*
30 * In case MBEDTLS_PSA_CRYPTO_SPM is defined the code is built for SPM (Secure
31 * Partition Manager) integration which separate the code into two parts
32 * NSPE (Non-Secure Process Environment) and SPE (Secure Process Environment).
33 * In this mode an additional header file should be included.
34 */
mohammad160327010052018-07-03 13:16:15 +030035#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030036/*
37 * PSA_CRYPTO_SECURE means that this file is compiled to the SPE side.
38 * some headers will be affected by this flag.
39 */
mohammad160327010052018-07-03 13:16:15 +030040#define PSA_CRYPTO_SECURE 1
41#include "crypto_spe.h"
42#endif
43
Gilles Peskinee59236f2018-01-27 23:32:46 +010044#include "psa/crypto.h"
45
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
48#if defined(MBEDTLS_PLATFORM_C)
49#include "mbedtls/platform.h"
50#else
51#define mbedtls_calloc calloc
52#define mbedtls_free free
53#endif
54
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020057#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/blowfish.h"
59#include "mbedtls/camellia.h"
60#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010077#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/sha1.h"
79#include "mbedtls/sha256.h"
80#include "mbedtls/sha512.h"
81#include "mbedtls/xtea.h"
82
Gilles Peskinee59236f2018-01-27 23:32:46 +010083
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskinee59236f2018-01-27 23:32:46 +010087/* Implementation that should never be optimized out by the compiler */
88static void mbedtls_zeroize( void *v, size_t n )
89{
90 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
91}
92
Gilles Peskine9ef733f2018-02-07 21:05:37 +010093/* constant-time buffer comparison */
94static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
95{
96 size_t i;
97 unsigned char diff = 0;
98
99 for( i = 0; i < n; i++ )
100 diff |= a[i] ^ b[i];
101
102 return( diff );
103}
104
105
106
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100107/****************************************************************/
108/* Global data, support functions and library management */
109/****************************************************************/
110
111/* Number of key slots (plus one because 0 is not used).
112 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +0200113#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100117 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300118 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200119 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200120 union
121 {
122 struct raw_data
123 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100124 uint8_t *data;
125 size_t bytes;
126 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100127#if defined(MBEDTLS_RSA_C)
128 mbedtls_rsa_context *rsa;
129#endif /* MBEDTLS_RSA_C */
130#if defined(MBEDTLS_ECP_C)
131 mbedtls_ecp_keypair *ecp;
132#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100133 } data;
134} key_slot_t;
135
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200136static int key_type_is_raw_bytes( psa_key_type_t type )
137{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200138 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200139}
140
Gilles Peskine2d277862018-06-18 15:41:12 +0200141typedef struct
142{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 int initialized;
144 mbedtls_entropy_context entropy;
145 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200146 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147} psa_global_data_t;
148
149static psa_global_data_t global_data;
150
itayzafrir0adf0fc2018-09-06 16:24:41 +0300151#define GUARD_MODULE_INITIALIZED \
152 if( global_data.initialized == 0 ) \
153 return( PSA_ERROR_BAD_STATE );
154
Gilles Peskinee59236f2018-01-27 23:32:46 +0100155static psa_status_t mbedtls_to_psa_error( int ret )
156{
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 /* If there's both a high-level code and low-level code, dispatch on
158 * the high-level code. */
159 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100160 {
161 case 0:
162 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100163
164 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
165 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
166 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
171 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
172 return( PSA_ERROR_HARDWARE_FAILURE );
173
Gilles Peskine9a944802018-06-21 09:35:35 +0200174 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
175 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
176 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
177 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
178 case MBEDTLS_ERR_ASN1_INVALID_DATA:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
181 return( PSA_ERROR_INSUFFICIENT_MEMORY );
182 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
183 return( PSA_ERROR_BUFFER_TOO_SMALL );
184
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
186 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
191 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
192 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
193 return( PSA_ERROR_NOT_SUPPORTED );
194 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
195 return( PSA_ERROR_HARDWARE_FAILURE );
196
197 case MBEDTLS_ERR_CCM_BAD_INPUT:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CCM_AUTH_FAILED:
200 return( PSA_ERROR_INVALID_SIGNATURE );
201 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
202 return( PSA_ERROR_HARDWARE_FAILURE );
203
204 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
205 return( PSA_ERROR_NOT_SUPPORTED );
206 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
207 return( PSA_ERROR_INVALID_ARGUMENT );
208 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
209 return( PSA_ERROR_INSUFFICIENT_MEMORY );
210 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
211 return( PSA_ERROR_INVALID_PADDING );
212 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
213 return( PSA_ERROR_BAD_STATE );
214 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
215 return( PSA_ERROR_INVALID_SIGNATURE );
216 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
217 return( PSA_ERROR_TAMPERING_DETECTED );
218 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
221 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
222 return( PSA_ERROR_HARDWARE_FAILURE );
223
224 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
225 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
226 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
227 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
228 return( PSA_ERROR_NOT_SUPPORTED );
229 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
231
232 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
233 return( PSA_ERROR_NOT_SUPPORTED );
234 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
235 return( PSA_ERROR_HARDWARE_FAILURE );
236
Gilles Peskinee59236f2018-01-27 23:32:46 +0100237 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
238 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
239 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
240 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100241
242 case MBEDTLS_ERR_GCM_AUTH_FAILED:
243 return( PSA_ERROR_INVALID_SIGNATURE );
244 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200245 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100246 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
247 return( PSA_ERROR_HARDWARE_FAILURE );
248
249 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
250 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
251 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
254 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
255 return( PSA_ERROR_NOT_SUPPORTED );
256 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MD_ALLOC_FAILED:
259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
260 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
261 return( PSA_ERROR_STORAGE_FAILURE );
262 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
263 return( PSA_ERROR_HARDWARE_FAILURE );
264
Gilles Peskinef76aa772018-10-29 19:24:33 +0100265 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
266 return( PSA_ERROR_STORAGE_FAILURE );
267 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
272 return( PSA_ERROR_BUFFER_TOO_SMALL );
273 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
280 return( PSA_ERROR_INSUFFICIENT_MEMORY );
281
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_ALLOC_FAILED:
283 return( PSA_ERROR_INSUFFICIENT_MEMORY );
284 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
285 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100289 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
290 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
293 return( PSA_ERROR_NOT_SUPPORTED );
294 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
295 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
296 return( PSA_ERROR_NOT_PERMITTED );
297 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
298 return( PSA_ERROR_INVALID_ARGUMENT );
299 case MBEDTLS_ERR_PK_INVALID_ALG:
300 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
301 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
302 return( PSA_ERROR_NOT_SUPPORTED );
303 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
304 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
309 return( PSA_ERROR_HARDWARE_FAILURE );
310
311 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_RSA_INVALID_PADDING:
314 return( PSA_ERROR_INVALID_PADDING );
315 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
316 return( PSA_ERROR_HARDWARE_FAILURE );
317 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
320 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
321 return( PSA_ERROR_TAMPERING_DETECTED );
322 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
323 return( PSA_ERROR_INVALID_SIGNATURE );
324 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
325 return( PSA_ERROR_BUFFER_TOO_SMALL );
326 case MBEDTLS_ERR_RSA_RNG_FAILED:
327 return( PSA_ERROR_INSUFFICIENT_MEMORY );
328 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
329 return( PSA_ERROR_NOT_SUPPORTED );
330 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
333 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
334 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
335 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
337
338 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
339 return( PSA_ERROR_INVALID_ARGUMENT );
340 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
341 return( PSA_ERROR_HARDWARE_FAILURE );
342
itayzafrir5c753392018-05-08 11:18:38 +0300343 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300345 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300346 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
347 return( PSA_ERROR_BUFFER_TOO_SMALL );
348 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
349 return( PSA_ERROR_NOT_SUPPORTED );
350 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
351 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
352 return( PSA_ERROR_INVALID_SIGNATURE );
353 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
354 return( PSA_ERROR_INSUFFICIENT_MEMORY );
355 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
356 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300357
Gilles Peskinee59236f2018-01-27 23:32:46 +0100358 default:
359 return( PSA_ERROR_UNKNOWN_ERROR );
360 }
361}
362
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200363/* Retrieve a key slot, occupied or not. */
364static psa_status_t psa_get_key_slot( psa_key_slot_t key,
365 key_slot_t **p_slot )
366{
itayzafrir90d8c7a2018-09-12 11:44:52 +0300367 GUARD_MODULE_INITIALIZED;
368
Gilles Peskine996deb12018-08-01 15:45:45 +0200369 /* 0 is not a valid slot number under any circumstance. This
370 * implementation provides slots number 1 to N where N is the
371 * number of available slots. */
372 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200373 return( PSA_ERROR_INVALID_ARGUMENT );
374
Gilles Peskine996deb12018-08-01 15:45:45 +0200375 *p_slot = &global_data.key_slots[key - 1];
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200376 return( PSA_SUCCESS );
377}
378
379/* Retrieve an empty key slot (slot with no key data, but possibly
380 * with some metadata such as a policy). */
381static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
382 key_slot_t **p_slot )
383{
384 psa_status_t status;
385 key_slot_t *slot = NULL;
386
387 *p_slot = NULL;
388
389 status = psa_get_key_slot( key, &slot );
390 if( status != PSA_SUCCESS )
391 return( status );
392
393 if( slot->type != PSA_KEY_TYPE_NONE )
394 return( PSA_ERROR_OCCUPIED_SLOT );
395
396 *p_slot = slot;
397 return( status );
398}
399
Jaeden Amerob4fa8c92018-07-11 15:57:44 +0100400/** Retrieve a slot which must contain a key. The key must have allow all the
401 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
402 * operations with this algorithm. */
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200403static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
404 key_slot_t **p_slot,
405 psa_key_usage_t usage,
406 psa_algorithm_t alg )
407{
408 psa_status_t status;
409 key_slot_t *slot = NULL;
410
411 *p_slot = NULL;
412
413 status = psa_get_key_slot( key, &slot );
414 if( status != PSA_SUCCESS )
415 return( status );
416 if( slot->type == PSA_KEY_TYPE_NONE )
417 return( PSA_ERROR_EMPTY_SLOT );
418
419 /* Enforce that usage policy for the key slot contains all the flags
420 * required by the usage parameter. There is one exception: public
421 * keys can always be exported, so we treat public key objects as
422 * if they had the export flag. */
423 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
424 usage &= ~PSA_KEY_USAGE_EXPORT;
425 if( ( slot->policy.usage & usage ) != usage )
426 return( PSA_ERROR_NOT_PERMITTED );
427 if( alg != 0 && ( alg != slot->policy.alg ) )
428 return( PSA_ERROR_NOT_PERMITTED );
429
430 *p_slot = slot;
431 return( PSA_SUCCESS );
432}
433
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200434
435
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100436/****************************************************************/
437/* Key management */
438/****************************************************************/
439
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100440#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200441static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
442{
443 switch( grpid )
444 {
445 case MBEDTLS_ECP_DP_SECP192R1:
446 return( PSA_ECC_CURVE_SECP192R1 );
447 case MBEDTLS_ECP_DP_SECP224R1:
448 return( PSA_ECC_CURVE_SECP224R1 );
449 case MBEDTLS_ECP_DP_SECP256R1:
450 return( PSA_ECC_CURVE_SECP256R1 );
451 case MBEDTLS_ECP_DP_SECP384R1:
452 return( PSA_ECC_CURVE_SECP384R1 );
453 case MBEDTLS_ECP_DP_SECP521R1:
454 return( PSA_ECC_CURVE_SECP521R1 );
455 case MBEDTLS_ECP_DP_BP256R1:
456 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
457 case MBEDTLS_ECP_DP_BP384R1:
458 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
459 case MBEDTLS_ECP_DP_BP512R1:
460 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
461 case MBEDTLS_ECP_DP_CURVE25519:
462 return( PSA_ECC_CURVE_CURVE25519 );
463 case MBEDTLS_ECP_DP_SECP192K1:
464 return( PSA_ECC_CURVE_SECP192K1 );
465 case MBEDTLS_ECP_DP_SECP224K1:
466 return( PSA_ECC_CURVE_SECP224K1 );
467 case MBEDTLS_ECP_DP_SECP256K1:
468 return( PSA_ECC_CURVE_SECP256K1 );
469 case MBEDTLS_ECP_DP_CURVE448:
470 return( PSA_ECC_CURVE_CURVE448 );
471 default:
472 return( 0 );
473 }
474}
475
Gilles Peskine12313cd2018-06-20 00:20:32 +0200476static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
477{
478 switch( curve )
479 {
480 case PSA_ECC_CURVE_SECP192R1:
481 return( MBEDTLS_ECP_DP_SECP192R1 );
482 case PSA_ECC_CURVE_SECP224R1:
483 return( MBEDTLS_ECP_DP_SECP224R1 );
484 case PSA_ECC_CURVE_SECP256R1:
485 return( MBEDTLS_ECP_DP_SECP256R1 );
486 case PSA_ECC_CURVE_SECP384R1:
487 return( MBEDTLS_ECP_DP_SECP384R1 );
488 case PSA_ECC_CURVE_SECP521R1:
489 return( MBEDTLS_ECP_DP_SECP521R1 );
490 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
491 return( MBEDTLS_ECP_DP_BP256R1 );
492 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
493 return( MBEDTLS_ECP_DP_BP384R1 );
494 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
495 return( MBEDTLS_ECP_DP_BP512R1 );
496 case PSA_ECC_CURVE_CURVE25519:
497 return( MBEDTLS_ECP_DP_CURVE25519 );
498 case PSA_ECC_CURVE_SECP192K1:
499 return( MBEDTLS_ECP_DP_SECP192K1 );
500 case PSA_ECC_CURVE_SECP224K1:
501 return( MBEDTLS_ECP_DP_SECP224K1 );
502 case PSA_ECC_CURVE_SECP256K1:
503 return( MBEDTLS_ECP_DP_SECP256K1 );
504 case PSA_ECC_CURVE_CURVE448:
505 return( MBEDTLS_ECP_DP_CURVE448 );
506 default:
507 return( MBEDTLS_ECP_DP_NONE );
508 }
509}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100510#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200511
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200512static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
513 size_t bits,
514 struct raw_data *raw )
515{
516 /* Check that the bit size is acceptable for the key type */
517 switch( type )
518 {
519 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200520 if( bits == 0 )
521 {
522 raw->bytes = 0;
523 raw->data = NULL;
524 return( PSA_SUCCESS );
525 }
526 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200527#if defined(MBEDTLS_MD_C)
528 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200529#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200530 case PSA_KEY_TYPE_DERIVE:
531 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200532#if defined(MBEDTLS_AES_C)
533 case PSA_KEY_TYPE_AES:
534 if( bits != 128 && bits != 192 && bits != 256 )
535 return( PSA_ERROR_INVALID_ARGUMENT );
536 break;
537#endif
538#if defined(MBEDTLS_CAMELLIA_C)
539 case PSA_KEY_TYPE_CAMELLIA:
540 if( bits != 128 && bits != 192 && bits != 256 )
541 return( PSA_ERROR_INVALID_ARGUMENT );
542 break;
543#endif
544#if defined(MBEDTLS_DES_C)
545 case PSA_KEY_TYPE_DES:
546 if( bits != 64 && bits != 128 && bits != 192 )
547 return( PSA_ERROR_INVALID_ARGUMENT );
548 break;
549#endif
550#if defined(MBEDTLS_ARC4_C)
551 case PSA_KEY_TYPE_ARC4:
552 if( bits < 8 || bits > 2048 )
553 return( PSA_ERROR_INVALID_ARGUMENT );
554 break;
555#endif
556 default:
557 return( PSA_ERROR_NOT_SUPPORTED );
558 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200559 if( bits % 8 != 0 )
560 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200561
562 /* Allocate memory for the key */
563 raw->bytes = PSA_BITS_TO_BYTES( bits );
564 raw->data = mbedtls_calloc( 1, raw->bytes );
565 if( raw->data == NULL )
566 {
567 raw->bytes = 0;
568 return( PSA_ERROR_INSUFFICIENT_MEMORY );
569 }
570 return( PSA_SUCCESS );
571}
572
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200573#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
574static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
575 mbedtls_rsa_context **p_rsa )
576{
577 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
578 return( PSA_ERROR_INVALID_ARGUMENT );
579 else
580 {
581 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100582 /* The size of an RSA key doesn't have to be a multiple of 8.
583 * Mbed TLS supports non-byte-aligned key sizes, but not well.
584 * For example, mbedtls_rsa_get_len() returns the key size in
585 * bytes, not in bits. */
586 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200587 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
588 return( PSA_ERROR_NOT_SUPPORTED );
589 *p_rsa = rsa;
590 return( PSA_SUCCESS );
591 }
592}
593#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
594
595#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100596/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200597static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
598 mbedtls_pk_context *pk,
599 mbedtls_ecp_keypair **p_ecp )
600{
601 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
602 return( PSA_ERROR_INVALID_ARGUMENT );
603 else
604 {
605 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
606 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
607 if( actual_curve != expected_curve )
608 return( PSA_ERROR_INVALID_ARGUMENT );
609 *p_ecp = ecp;
610 return( PSA_SUCCESS );
611 }
612}
613#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
614
Gilles Peskinef76aa772018-10-29 19:24:33 +0100615#if defined(MBEDTLS_ECP_C)
616/* Import a private key given as a byte string which is the private value
617 * in big-endian order. */
618static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
619 const uint8_t *data,
620 size_t data_length,
621 mbedtls_ecp_keypair **p_ecp )
622{
623 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
624 mbedtls_ecp_keypair *ecp = NULL;
625 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
626
627 *p_ecp = NULL;
628 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
629 if( ecp == NULL )
630 return( PSA_ERROR_INSUFFICIENT_MEMORY );
631
632 /* Load the group. */
633 status = mbedtls_to_psa_error(
634 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
635 if( status != PSA_SUCCESS )
636 goto exit;
637 /* Load the secret value. */
638 status = mbedtls_to_psa_error(
639 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
640 if( status != PSA_SUCCESS )
641 goto exit;
642 /* Validate the private key. */
643 status = mbedtls_to_psa_error(
644 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
645 if( status != PSA_SUCCESS )
646 goto exit;
647 /* Calculate the public key from the private key. */
648 status = mbedtls_to_psa_error(
649 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
650 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
651 if( status != PSA_SUCCESS )
652 goto exit;
653
654 *p_ecp = ecp;
655 return( PSA_SUCCESS );
656
657exit:
658 if( ecp != NULL )
659 {
660 mbedtls_ecp_keypair_free( ecp );
661 mbedtls_free( ecp );
662 }
663 return( status );
664}
665#endif /* defined(MBEDTLS_ECP_C) */
666
Gilles Peskine2d277862018-06-18 15:41:12 +0200667psa_status_t psa_import_key( psa_key_slot_t key,
668 psa_key_type_t type,
669 const uint8_t *data,
670 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100671{
672 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200673 psa_status_t status = PSA_SUCCESS;
674 status = psa_get_empty_key_slot( key, &slot );
675 if( status != PSA_SUCCESS )
676 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200678 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100680 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100681 if( data_length > SIZE_MAX / 8 )
682 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200683 status = prepare_raw_data_slot( type,
684 PSA_BYTES_TO_BITS( data_length ),
685 &slot->data.raw );
686 if( status != PSA_SUCCESS )
687 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200688 if( data_length != 0 )
689 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690 }
691 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100692#if defined(MBEDTLS_ECP_C)
693 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
694 {
695 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( type ),
696 data, data_length,
697 &slot->data.ecp );
698 if( status != PSA_SUCCESS )
699 return( status );
700 }
701 else
702#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100703#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200704 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100705 {
706 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100707 mbedtls_pk_context pk;
708 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200709
710 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100711 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
712 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
713 else
714 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 if( ret != 0 )
716 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200717
718 /* We have something that the pkparse module recognizes.
719 * If it has the expected type and passes any type-specific
720 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100721#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200722 if( PSA_KEY_TYPE_IS_RSA( type ) )
723 status = psa_import_rsa_key( &pk, &slot->data.rsa );
724 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100725#endif /* MBEDTLS_RSA_C */
726#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200727 if( PSA_KEY_TYPE_IS_ECC( type ) )
728 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
729 &pk, &slot->data.ecp );
730 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100731#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200732 {
733 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200734 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200735
Gilles Peskinec648d692018-06-28 08:46:13 +0200736 /* Free the content of the pk object only on error. On success,
737 * the content of the object has been stored in the slot. */
738 if( status != PSA_SUCCESS )
739 {
740 mbedtls_pk_free( &pk );
741 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100742 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 }
744 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100745#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 {
747 return( PSA_ERROR_NOT_SUPPORTED );
748 }
749
750 slot->type = type;
751 return( PSA_SUCCESS );
752}
753
Gilles Peskine2d277862018-06-18 15:41:12 +0200754psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100755{
756 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200757 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100758
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200759 status = psa_get_key_slot( key, &slot );
760 if( status != PSA_SUCCESS )
761 return( status );
762
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100763 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200764 {
765 /* No key material to clean, but do zeroize the slot below to wipe
766 * metadata such as policies. */
767 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200768 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100769 {
770 mbedtls_free( slot->data.raw.data );
771 }
772 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100773#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200774 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100775 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100776 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100777 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100778 }
779 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100780#endif /* defined(MBEDTLS_RSA_C) */
781#if defined(MBEDTLS_ECP_C)
782 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
783 {
784 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100785 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100786 }
787 else
788#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 {
790 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100791 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100792 return( PSA_ERROR_TAMPERING_DETECTED );
793 }
794
795 mbedtls_zeroize( slot, sizeof( *slot ) );
796 return( PSA_SUCCESS );
797}
798
Gilles Peskineb870b182018-07-06 16:02:09 +0200799/* Return the size of the key in the given slot, in bits. */
800static size_t psa_get_key_bits( const key_slot_t *slot )
801{
802 if( key_type_is_raw_bytes( slot->type ) )
803 return( slot->data.raw.bytes * 8 );
804#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200805 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100806 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200807#endif /* defined(MBEDTLS_RSA_C) */
808#if defined(MBEDTLS_ECP_C)
809 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
810 return( slot->data.ecp->grp.pbits );
811#endif /* defined(MBEDTLS_ECP_C) */
812 /* Shouldn't happen except on an empty slot. */
813 return( 0 );
814}
815
Gilles Peskine2d277862018-06-18 15:41:12 +0200816psa_status_t psa_get_key_information( psa_key_slot_t key,
817 psa_key_type_t *type,
818 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819{
820 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200821 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100822
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200824 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100825 if( bits != NULL )
826 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200827 status = psa_get_key_slot( key, &slot );
828 if( status != PSA_SUCCESS )
829 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200830
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100831 if( slot->type == PSA_KEY_TYPE_NONE )
832 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200833 if( type != NULL )
834 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200835 if( bits != NULL )
836 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837 return( PSA_SUCCESS );
838}
839
Gilles Peskine2d277862018-06-18 15:41:12 +0200840static psa_status_t psa_internal_export_key( psa_key_slot_t key,
841 uint8_t *data,
842 size_t data_size,
843 size_t *data_length,
844 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845{
846 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200847 psa_status_t status;
848 /* Exporting a public key doesn't require a usage flag. If we're
849 * called by psa_export_public_key(), don't require the EXPORT flag.
850 * If we're called by psa_export_key(), do require the EXPORT flag;
851 * if the key turns out to be public key object, psa_get_key_from_slot()
852 * will ignore this flag. */
853 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100854
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100855 /* Set the key to empty now, so that even when there are errors, we always
856 * set data_length to a value between 0 and data_size. On error, setting
857 * the key to empty is a good choice because an empty key representation is
858 * unlikely to be accepted anywhere. */
859 *data_length = 0;
860
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200861 status = psa_get_key_from_slot( key, &slot, usage, 0 );
862 if( status != PSA_SUCCESS )
863 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200864 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300865 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300866
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200867 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100868 {
869 if( slot->data.raw.bytes > data_size )
870 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100871 if( data_size != 0 )
872 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200873 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100874 memset( data + slot->data.raw.bytes, 0,
875 data_size - slot->data.raw.bytes );
876 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877 *data_length = slot->data.raw.bytes;
878 return( PSA_SUCCESS );
879 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100880#if defined(MBEDTLS_ECP_C)
881 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
882 {
883 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
884 if( bytes > data_size )
885 return( PSA_ERROR_BUFFER_TOO_SMALL );
886 status = mbedtls_to_psa_error(
887 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
888 if( status != PSA_SUCCESS )
889 return( status );
890 memset( data + bytes, 0, data_size - bytes );
891 *data_length = bytes;
892 return( PSA_SUCCESS );
893 }
894#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100895 else
Moran Peker17e36e12018-05-02 12:55:20 +0300896 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100897#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200898 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300899 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100900 {
Moran Pekera998bc62018-04-16 18:16:20 +0300901 mbedtls_pk_context pk;
902 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200903 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300904 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100905#if defined(MBEDTLS_RSA_C)
906 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300907 pk.pk_info = &mbedtls_rsa_info;
908 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100909#else
910 return( PSA_ERROR_NOT_SUPPORTED );
911#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300912 }
913 else
914 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100915#if defined(MBEDTLS_ECP_C)
916 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300917 pk.pk_info = &mbedtls_eckey_info;
918 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100919#else
920 return( PSA_ERROR_NOT_SUPPORTED );
921#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300922 }
Moran Pekerd7326592018-05-29 16:56:39 +0300923 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300924 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300925 else
926 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300927 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200928 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200929 /* If data_size is 0 then data may be NULL and then the
930 * call to memset would have undefined behavior. */
931 if( data_size != 0 )
932 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300933 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200934 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200935 /* The mbedtls_pk_xxx functions write to the end of the buffer.
936 * Move the data to the beginning and erase remaining data
937 * at the original location. */
938 if( 2 * (size_t) ret <= data_size )
939 {
940 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200941 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200942 }
943 else if( (size_t) ret < data_size )
944 {
945 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200946 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200947 }
Moran Pekera998bc62018-04-16 18:16:20 +0300948 *data_length = ret;
949 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100950 }
951 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100952#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300953 {
954 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200955 it is valid for a special-purpose implementation to omit
956 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300957 return( PSA_ERROR_NOT_SUPPORTED );
958 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100959 }
960}
961
Gilles Peskine2d277862018-06-18 15:41:12 +0200962psa_status_t psa_export_key( psa_key_slot_t key,
963 uint8_t *data,
964 size_t data_size,
965 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300966{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200967 return( psa_internal_export_key( key, data, data_size,
968 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100970
Gilles Peskine2d277862018-06-18 15:41:12 +0200971psa_status_t psa_export_public_key( psa_key_slot_t key,
972 uint8_t *data,
973 size_t data_size,
974 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300975{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200976 return( psa_internal_export_key( key, data, data_size,
977 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300978}
979
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200980
981
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100982/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100983/* Message digests */
984/****************************************************************/
985
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100986static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100987{
988 switch( alg )
989 {
990#if defined(MBEDTLS_MD2_C)
991 case PSA_ALG_MD2:
992 return( &mbedtls_md2_info );
993#endif
994#if defined(MBEDTLS_MD4_C)
995 case PSA_ALG_MD4:
996 return( &mbedtls_md4_info );
997#endif
998#if defined(MBEDTLS_MD5_C)
999 case PSA_ALG_MD5:
1000 return( &mbedtls_md5_info );
1001#endif
1002#if defined(MBEDTLS_RIPEMD160_C)
1003 case PSA_ALG_RIPEMD160:
1004 return( &mbedtls_ripemd160_info );
1005#endif
1006#if defined(MBEDTLS_SHA1_C)
1007 case PSA_ALG_SHA_1:
1008 return( &mbedtls_sha1_info );
1009#endif
1010#if defined(MBEDTLS_SHA256_C)
1011 case PSA_ALG_SHA_224:
1012 return( &mbedtls_sha224_info );
1013 case PSA_ALG_SHA_256:
1014 return( &mbedtls_sha256_info );
1015#endif
1016#if defined(MBEDTLS_SHA512_C)
1017 case PSA_ALG_SHA_384:
1018 return( &mbedtls_sha384_info );
1019 case PSA_ALG_SHA_512:
1020 return( &mbedtls_sha512_info );
1021#endif
1022 default:
1023 return( NULL );
1024 }
1025}
1026
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001027psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1028{
1029 switch( operation->alg )
1030 {
Gilles Peskine81736312018-06-26 15:04:31 +02001031 case 0:
1032 /* The object has (apparently) been initialized but it is not
1033 * in use. It's ok to call abort on such an object, and there's
1034 * nothing to do. */
1035 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001036#if defined(MBEDTLS_MD2_C)
1037 case PSA_ALG_MD2:
1038 mbedtls_md2_free( &operation->ctx.md2 );
1039 break;
1040#endif
1041#if defined(MBEDTLS_MD4_C)
1042 case PSA_ALG_MD4:
1043 mbedtls_md4_free( &operation->ctx.md4 );
1044 break;
1045#endif
1046#if defined(MBEDTLS_MD5_C)
1047 case PSA_ALG_MD5:
1048 mbedtls_md5_free( &operation->ctx.md5 );
1049 break;
1050#endif
1051#if defined(MBEDTLS_RIPEMD160_C)
1052 case PSA_ALG_RIPEMD160:
1053 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1054 break;
1055#endif
1056#if defined(MBEDTLS_SHA1_C)
1057 case PSA_ALG_SHA_1:
1058 mbedtls_sha1_free( &operation->ctx.sha1 );
1059 break;
1060#endif
1061#if defined(MBEDTLS_SHA256_C)
1062 case PSA_ALG_SHA_224:
1063 case PSA_ALG_SHA_256:
1064 mbedtls_sha256_free( &operation->ctx.sha256 );
1065 break;
1066#endif
1067#if defined(MBEDTLS_SHA512_C)
1068 case PSA_ALG_SHA_384:
1069 case PSA_ALG_SHA_512:
1070 mbedtls_sha512_free( &operation->ctx.sha512 );
1071 break;
1072#endif
1073 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001074 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001075 }
1076 operation->alg = 0;
1077 return( PSA_SUCCESS );
1078}
1079
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001080psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001081 psa_algorithm_t alg )
1082{
1083 int ret;
1084 operation->alg = 0;
1085 switch( alg )
1086 {
1087#if defined(MBEDTLS_MD2_C)
1088 case PSA_ALG_MD2:
1089 mbedtls_md2_init( &operation->ctx.md2 );
1090 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1091 break;
1092#endif
1093#if defined(MBEDTLS_MD4_C)
1094 case PSA_ALG_MD4:
1095 mbedtls_md4_init( &operation->ctx.md4 );
1096 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1097 break;
1098#endif
1099#if defined(MBEDTLS_MD5_C)
1100 case PSA_ALG_MD5:
1101 mbedtls_md5_init( &operation->ctx.md5 );
1102 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1103 break;
1104#endif
1105#if defined(MBEDTLS_RIPEMD160_C)
1106 case PSA_ALG_RIPEMD160:
1107 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1108 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1109 break;
1110#endif
1111#if defined(MBEDTLS_SHA1_C)
1112 case PSA_ALG_SHA_1:
1113 mbedtls_sha1_init( &operation->ctx.sha1 );
1114 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1115 break;
1116#endif
1117#if defined(MBEDTLS_SHA256_C)
1118 case PSA_ALG_SHA_224:
1119 mbedtls_sha256_init( &operation->ctx.sha256 );
1120 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1121 break;
1122 case PSA_ALG_SHA_256:
1123 mbedtls_sha256_init( &operation->ctx.sha256 );
1124 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1125 break;
1126#endif
1127#if defined(MBEDTLS_SHA512_C)
1128 case PSA_ALG_SHA_384:
1129 mbedtls_sha512_init( &operation->ctx.sha512 );
1130 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1131 break;
1132 case PSA_ALG_SHA_512:
1133 mbedtls_sha512_init( &operation->ctx.sha512 );
1134 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1135 break;
1136#endif
1137 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001138 return( PSA_ALG_IS_HASH( alg ) ?
1139 PSA_ERROR_NOT_SUPPORTED :
1140 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001141 }
1142 if( ret == 0 )
1143 operation->alg = alg;
1144 else
1145 psa_hash_abort( operation );
1146 return( mbedtls_to_psa_error( ret ) );
1147}
1148
1149psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1150 const uint8_t *input,
1151 size_t input_length )
1152{
1153 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001154
1155 /* Don't require hash implementations to behave correctly on a
1156 * zero-length input, which may have an invalid pointer. */
1157 if( input_length == 0 )
1158 return( PSA_SUCCESS );
1159
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001160 switch( operation->alg )
1161 {
1162#if defined(MBEDTLS_MD2_C)
1163 case PSA_ALG_MD2:
1164 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1165 input, input_length );
1166 break;
1167#endif
1168#if defined(MBEDTLS_MD4_C)
1169 case PSA_ALG_MD4:
1170 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1171 input, input_length );
1172 break;
1173#endif
1174#if defined(MBEDTLS_MD5_C)
1175 case PSA_ALG_MD5:
1176 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1177 input, input_length );
1178 break;
1179#endif
1180#if defined(MBEDTLS_RIPEMD160_C)
1181 case PSA_ALG_RIPEMD160:
1182 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1183 input, input_length );
1184 break;
1185#endif
1186#if defined(MBEDTLS_SHA1_C)
1187 case PSA_ALG_SHA_1:
1188 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1189 input, input_length );
1190 break;
1191#endif
1192#if defined(MBEDTLS_SHA256_C)
1193 case PSA_ALG_SHA_224:
1194 case PSA_ALG_SHA_256:
1195 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1196 input, input_length );
1197 break;
1198#endif
1199#if defined(MBEDTLS_SHA512_C)
1200 case PSA_ALG_SHA_384:
1201 case PSA_ALG_SHA_512:
1202 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1203 input, input_length );
1204 break;
1205#endif
1206 default:
1207 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1208 break;
1209 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001210
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001211 if( ret != 0 )
1212 psa_hash_abort( operation );
1213 return( mbedtls_to_psa_error( ret ) );
1214}
1215
1216psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1217 uint8_t *hash,
1218 size_t hash_size,
1219 size_t *hash_length )
1220{
itayzafrir40835d42018-08-02 13:14:17 +03001221 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001222 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001223 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001224
1225 /* Fill the output buffer with something that isn't a valid hash
1226 * (barring an attack on the hash and deliberately-crafted input),
1227 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001228 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001229 /* If hash_size is 0 then hash may be NULL and then the
1230 * call to memset would have undefined behavior. */
1231 if( hash_size != 0 )
1232 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001233
1234 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001235 {
1236 status = PSA_ERROR_BUFFER_TOO_SMALL;
1237 goto exit;
1238 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001239
1240 switch( operation->alg )
1241 {
1242#if defined(MBEDTLS_MD2_C)
1243 case PSA_ALG_MD2:
1244 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1245 break;
1246#endif
1247#if defined(MBEDTLS_MD4_C)
1248 case PSA_ALG_MD4:
1249 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1250 break;
1251#endif
1252#if defined(MBEDTLS_MD5_C)
1253 case PSA_ALG_MD5:
1254 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1255 break;
1256#endif
1257#if defined(MBEDTLS_RIPEMD160_C)
1258 case PSA_ALG_RIPEMD160:
1259 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1260 break;
1261#endif
1262#if defined(MBEDTLS_SHA1_C)
1263 case PSA_ALG_SHA_1:
1264 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1265 break;
1266#endif
1267#if defined(MBEDTLS_SHA256_C)
1268 case PSA_ALG_SHA_224:
1269 case PSA_ALG_SHA_256:
1270 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1271 break;
1272#endif
1273#if defined(MBEDTLS_SHA512_C)
1274 case PSA_ALG_SHA_384:
1275 case PSA_ALG_SHA_512:
1276 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1277 break;
1278#endif
1279 default:
1280 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1281 break;
1282 }
itayzafrir40835d42018-08-02 13:14:17 +03001283 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001284
itayzafrir40835d42018-08-02 13:14:17 +03001285exit:
1286 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001287 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001288 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001289 return( psa_hash_abort( operation ) );
1290 }
1291 else
1292 {
1293 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001294 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001295 }
1296}
1297
Gilles Peskine2d277862018-06-18 15:41:12 +02001298psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1299 const uint8_t *hash,
1300 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001301{
1302 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1303 size_t actual_hash_length;
1304 psa_status_t status = psa_hash_finish( operation,
1305 actual_hash, sizeof( actual_hash ),
1306 &actual_hash_length );
1307 if( status != PSA_SUCCESS )
1308 return( status );
1309 if( actual_hash_length != hash_length )
1310 return( PSA_ERROR_INVALID_SIGNATURE );
1311 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1312 return( PSA_ERROR_INVALID_SIGNATURE );
1313 return( PSA_SUCCESS );
1314}
1315
1316
1317
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001318/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001319/* MAC */
1320/****************************************************************/
1321
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001322static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001323 psa_algorithm_t alg,
1324 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001325 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001326 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001327{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001328 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001329 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001330
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001331 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001332 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001333
Gilles Peskine8c9def32018-02-08 10:02:12 +01001334 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1335 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001336 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001337 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001338 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001339 mode = MBEDTLS_MODE_STREAM;
1340 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001341 case PSA_ALG_CTR:
1342 mode = MBEDTLS_MODE_CTR;
1343 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001344 case PSA_ALG_CFB:
1345 mode = MBEDTLS_MODE_CFB;
1346 break;
1347 case PSA_ALG_OFB:
1348 mode = MBEDTLS_MODE_OFB;
1349 break;
1350 case PSA_ALG_CBC_NO_PADDING:
1351 mode = MBEDTLS_MODE_CBC;
1352 break;
1353 case PSA_ALG_CBC_PKCS7:
1354 mode = MBEDTLS_MODE_CBC;
1355 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001356 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001357 mode = MBEDTLS_MODE_CCM;
1358 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001359 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001360 mode = MBEDTLS_MODE_GCM;
1361 break;
1362 default:
1363 return( NULL );
1364 }
1365 }
1366 else if( alg == PSA_ALG_CMAC )
1367 mode = MBEDTLS_MODE_ECB;
1368 else if( alg == PSA_ALG_GMAC )
1369 mode = MBEDTLS_MODE_GCM;
1370 else
1371 return( NULL );
1372
1373 switch( key_type )
1374 {
1375 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001376 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001377 break;
1378 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001379 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1380 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001381 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001382 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001384 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001385 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1386 * but two-key Triple-DES is functionally three-key Triple-DES
1387 * with K1=K3, so that's how we present it to mbedtls. */
1388 if( key_bits == 128 )
1389 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001390 break;
1391 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001392 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001393 break;
1394 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001395 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001396 break;
1397 default:
1398 return( NULL );
1399 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001400 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001401 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001402
Jaeden Amero23bbb752018-06-26 14:16:54 +01001403 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1404 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001405}
1406
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001407static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001408{
Gilles Peskine2d277862018-06-18 15:41:12 +02001409 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001410 {
1411 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001412 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001413 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001414 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001415 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001416 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001417 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001418 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001419 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001420 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001421 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001422 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001423 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001424 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001425 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001426 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001427 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001428 return( 128 );
1429 default:
1430 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001431 }
1432}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001433
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001434/* Initialize the MAC operation structure. Once this function has been
1435 * called, psa_mac_abort can run and will do the right thing. */
1436static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1437 psa_algorithm_t alg )
1438{
1439 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1440
1441 operation->alg = alg;
1442 operation->key_set = 0;
1443 operation->iv_set = 0;
1444 operation->iv_required = 0;
1445 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001446 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001447
1448#if defined(MBEDTLS_CMAC_C)
1449 if( alg == PSA_ALG_CMAC )
1450 {
1451 operation->iv_required = 0;
1452 mbedtls_cipher_init( &operation->ctx.cmac );
1453 status = PSA_SUCCESS;
1454 }
1455 else
1456#endif /* MBEDTLS_CMAC_C */
1457#if defined(MBEDTLS_MD_C)
1458 if( PSA_ALG_IS_HMAC( operation->alg ) )
1459 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001460 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1461 operation->ctx.hmac.hash_ctx.alg = 0;
1462 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001463 }
1464 else
1465#endif /* MBEDTLS_MD_C */
1466 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001467 if( ! PSA_ALG_IS_MAC( alg ) )
1468 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001469 }
1470
1471 if( status != PSA_SUCCESS )
1472 memset( operation, 0, sizeof( *operation ) );
1473 return( status );
1474}
1475
Gilles Peskine01126fa2018-07-12 17:04:55 +02001476#if defined(MBEDTLS_MD_C)
1477static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1478{
1479 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1480 return( psa_hash_abort( &hmac->hash_ctx ) );
1481}
1482#endif /* MBEDTLS_MD_C */
1483
Gilles Peskine8c9def32018-02-08 10:02:12 +01001484psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1485{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001486 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001487 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001488 /* The object has (apparently) been initialized but it is not
1489 * in use. It's ok to call abort on such an object, and there's
1490 * nothing to do. */
1491 return( PSA_SUCCESS );
1492 }
1493 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001494#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001495 if( operation->alg == PSA_ALG_CMAC )
1496 {
1497 mbedtls_cipher_free( &operation->ctx.cmac );
1498 }
1499 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001500#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001501#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001502 if( PSA_ALG_IS_HMAC( operation->alg ) )
1503 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001504 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001505 }
1506 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001507#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001508 {
1509 /* Sanity check (shouldn't happen: operation->alg should
1510 * always have been initialized to a valid value). */
1511 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001512 }
Moran Peker41deec42018-04-04 15:43:05 +03001513
Gilles Peskine8c9def32018-02-08 10:02:12 +01001514 operation->alg = 0;
1515 operation->key_set = 0;
1516 operation->iv_set = 0;
1517 operation->iv_required = 0;
1518 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001519 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001520
Gilles Peskine8c9def32018-02-08 10:02:12 +01001521 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001522
1523bad_state:
1524 /* If abort is called on an uninitialized object, we can't trust
1525 * anything. Wipe the object in case it contains confidential data.
1526 * This may result in a memory leak if a pointer gets overwritten,
1527 * but it's too late to do anything about this. */
1528 memset( operation, 0, sizeof( *operation ) );
1529 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001530}
1531
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001532#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001533static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001534 size_t key_bits,
1535 key_slot_t *slot,
1536 const mbedtls_cipher_info_t *cipher_info )
1537{
1538 int ret;
1539
1540 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001541
1542 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1543 if( ret != 0 )
1544 return( ret );
1545
1546 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1547 slot->data.raw.data,
1548 key_bits );
1549 return( ret );
1550}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001551#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001552
Gilles Peskine248051a2018-06-20 16:09:38 +02001553#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001554static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1555 const uint8_t *key,
1556 size_t key_length,
1557 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001558{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001559 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001560 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001561 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001562 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001563 psa_status_t status;
1564
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001565 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1566 * overflow below. This should never trigger if the hash algorithm
1567 * is implemented correctly. */
1568 /* The size checks against the ipad and opad buffers cannot be written
1569 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1570 * because that triggers -Wlogical-op on GCC 7.3. */
1571 if( block_size > sizeof( ipad ) )
1572 return( PSA_ERROR_NOT_SUPPORTED );
1573 if( block_size > sizeof( hmac->opad ) )
1574 return( PSA_ERROR_NOT_SUPPORTED );
1575 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001576 return( PSA_ERROR_NOT_SUPPORTED );
1577
Gilles Peskined223b522018-06-11 18:12:58 +02001578 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001579 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001580 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001581 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001582 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001583 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001584 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001585 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001586 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001587 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001588 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001589 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001590 }
Gilles Peskine96889972018-07-12 17:07:03 +02001591 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1592 * but it is permitted. It is common when HMAC is used in HKDF, for
1593 * example. Don't call `memcpy` in the 0-length because `key` could be
1594 * an invalid pointer which would make the behavior undefined. */
1595 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001596 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001597
Gilles Peskined223b522018-06-11 18:12:58 +02001598 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1599 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001600 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001601 ipad[i] ^= 0x36;
1602 memset( ipad + key_length, 0x36, block_size - key_length );
1603
1604 /* Copy the key material from ipad to opad, flipping the requisite bits,
1605 * and filling the rest of opad with the requisite constant. */
1606 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001607 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1608 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001609
Gilles Peskine01126fa2018-07-12 17:04:55 +02001610 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001611 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001612 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001613
Gilles Peskine01126fa2018-07-12 17:04:55 +02001614 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001615
1616cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001617 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001618
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001619 return( status );
1620}
Gilles Peskine248051a2018-06-20 16:09:38 +02001621#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001622
Gilles Peskine89167cb2018-07-08 20:12:23 +02001623static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1624 psa_key_slot_t key,
1625 psa_algorithm_t alg,
1626 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001627{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001628 psa_status_t status;
1629 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001630 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001631 psa_key_usage_t usage =
1632 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001633 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001634 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001635
Gilles Peskined911eb72018-08-14 15:18:45 +02001636 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001637 if( status != PSA_SUCCESS )
1638 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001639 if( is_sign )
1640 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001641
Gilles Peskine89167cb2018-07-08 20:12:23 +02001642 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001643 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001644 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001645 key_bits = psa_get_key_bits( slot );
1646
Gilles Peskine8c9def32018-02-08 10:02:12 +01001647#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001648 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001649 {
1650 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001651 mbedtls_cipher_info_from_psa( full_length_alg,
1652 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001653 int ret;
1654 if( cipher_info == NULL )
1655 {
1656 status = PSA_ERROR_NOT_SUPPORTED;
1657 goto exit;
1658 }
1659 operation->mac_size = cipher_info->block_size;
1660 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1661 status = mbedtls_to_psa_error( ret );
1662 }
1663 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001664#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001665#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001666 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001667 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001668 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001669 if( hash_alg == 0 )
1670 {
1671 status = PSA_ERROR_NOT_SUPPORTED;
1672 goto exit;
1673 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001674
1675 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1676 /* Sanity check. This shouldn't fail on a valid configuration. */
1677 if( operation->mac_size == 0 ||
1678 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1679 {
1680 status = PSA_ERROR_NOT_SUPPORTED;
1681 goto exit;
1682 }
1683
Gilles Peskine01126fa2018-07-12 17:04:55 +02001684 if( slot->type != PSA_KEY_TYPE_HMAC )
1685 {
1686 status = PSA_ERROR_INVALID_ARGUMENT;
1687 goto exit;
1688 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001689
Gilles Peskine01126fa2018-07-12 17:04:55 +02001690 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1691 slot->data.raw.data,
1692 slot->data.raw.bytes,
1693 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001694 }
1695 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001696#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001697 {
1698 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001699 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001700
Gilles Peskined911eb72018-08-14 15:18:45 +02001701 if( truncated == 0 )
1702 {
1703 /* The "normal" case: untruncated algorithm. Nothing to do. */
1704 }
1705 else if( truncated < 4 )
1706 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001707 /* A very short MAC is too short for security since it can be
1708 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1709 * so we make this our minimum, even though 32 bits is still
1710 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001711 status = PSA_ERROR_NOT_SUPPORTED;
1712 }
1713 else if( truncated > operation->mac_size )
1714 {
1715 /* It's impossible to "truncate" to a larger length. */
1716 status = PSA_ERROR_INVALID_ARGUMENT;
1717 }
1718 else
1719 operation->mac_size = truncated;
1720
Gilles Peskinefbfac682018-07-08 20:51:54 +02001721exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001722 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001723 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001724 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001725 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001726 else
1727 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001728 operation->key_set = 1;
1729 }
1730 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001731}
1732
Gilles Peskine89167cb2018-07-08 20:12:23 +02001733psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1734 psa_key_slot_t key,
1735 psa_algorithm_t alg )
1736{
1737 return( psa_mac_setup( operation, key, alg, 1 ) );
1738}
1739
1740psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1741 psa_key_slot_t key,
1742 psa_algorithm_t alg )
1743{
1744 return( psa_mac_setup( operation, key, alg, 0 ) );
1745}
1746
Gilles Peskine8c9def32018-02-08 10:02:12 +01001747psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1748 const uint8_t *input,
1749 size_t input_length )
1750{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001751 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001752 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001753 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001754 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001755 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001756 operation->has_input = 1;
1757
Gilles Peskine8c9def32018-02-08 10:02:12 +01001758#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001759 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001760 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001761 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1762 input, input_length );
1763 status = mbedtls_to_psa_error( ret );
1764 }
1765 else
1766#endif /* MBEDTLS_CMAC_C */
1767#if defined(MBEDTLS_MD_C)
1768 if( PSA_ALG_IS_HMAC( operation->alg ) )
1769 {
1770 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1771 input_length );
1772 }
1773 else
1774#endif /* MBEDTLS_MD_C */
1775 {
1776 /* This shouldn't happen if `operation` was initialized by
1777 * a setup function. */
1778 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001779 }
1780
Gilles Peskinefbfac682018-07-08 20:51:54 +02001781cleanup:
1782 if( status != PSA_SUCCESS )
1783 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001784 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001785}
1786
Gilles Peskine01126fa2018-07-12 17:04:55 +02001787#if defined(MBEDTLS_MD_C)
1788static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1789 uint8_t *mac,
1790 size_t mac_size )
1791{
1792 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1793 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1794 size_t hash_size = 0;
1795 size_t block_size = psa_get_hash_block_size( hash_alg );
1796 psa_status_t status;
1797
Gilles Peskine01126fa2018-07-12 17:04:55 +02001798 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1799 if( status != PSA_SUCCESS )
1800 return( status );
1801 /* From here on, tmp needs to be wiped. */
1802
1803 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1804 if( status != PSA_SUCCESS )
1805 goto exit;
1806
1807 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1808 if( status != PSA_SUCCESS )
1809 goto exit;
1810
1811 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1812 if( status != PSA_SUCCESS )
1813 goto exit;
1814
Gilles Peskined911eb72018-08-14 15:18:45 +02001815 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1816 if( status != PSA_SUCCESS )
1817 goto exit;
1818
1819 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001820
1821exit:
1822 mbedtls_zeroize( tmp, hash_size );
1823 return( status );
1824}
1825#endif /* MBEDTLS_MD_C */
1826
mohammad16036df908f2018-04-02 08:34:15 -07001827static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001828 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001829 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001830{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001831 if( ! operation->key_set )
1832 return( PSA_ERROR_BAD_STATE );
1833 if( operation->iv_required && ! operation->iv_set )
1834 return( PSA_ERROR_BAD_STATE );
1835
Gilles Peskine8c9def32018-02-08 10:02:12 +01001836 if( mac_size < operation->mac_size )
1837 return( PSA_ERROR_BUFFER_TOO_SMALL );
1838
Gilles Peskine8c9def32018-02-08 10:02:12 +01001839#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001840 if( operation->alg == PSA_ALG_CMAC )
1841 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001842 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1843 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1844 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02001845 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02001846 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001847 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001848 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001849 else
1850#endif /* MBEDTLS_CMAC_C */
1851#if defined(MBEDTLS_MD_C)
1852 if( PSA_ALG_IS_HMAC( operation->alg ) )
1853 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001854 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001855 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001856 }
1857 else
1858#endif /* MBEDTLS_MD_C */
1859 {
1860 /* This shouldn't happen if `operation` was initialized by
1861 * a setup function. */
1862 return( PSA_ERROR_BAD_STATE );
1863 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001864}
1865
Gilles Peskineacd4be32018-07-08 19:56:25 +02001866psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1867 uint8_t *mac,
1868 size_t mac_size,
1869 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001870{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001871 psa_status_t status;
1872
1873 /* Fill the output buffer with something that isn't a valid mac
1874 * (barring an attack on the mac and deliberately-crafted input),
1875 * in case the caller doesn't check the return status properly. */
1876 *mac_length = mac_size;
1877 /* If mac_size is 0 then mac may be NULL and then the
1878 * call to memset would have undefined behavior. */
1879 if( mac_size != 0 )
1880 memset( mac, '!', mac_size );
1881
Gilles Peskine89167cb2018-07-08 20:12:23 +02001882 if( ! operation->is_sign )
1883 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001884 status = PSA_ERROR_BAD_STATE;
1885 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001886 }
mohammad16036df908f2018-04-02 08:34:15 -07001887
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001888 status = psa_mac_finish_internal( operation, mac, mac_size );
1889
1890cleanup:
1891 if( status == PSA_SUCCESS )
1892 {
1893 status = psa_mac_abort( operation );
1894 if( status == PSA_SUCCESS )
1895 *mac_length = operation->mac_size;
1896 else
1897 memset( mac, '!', mac_size );
1898 }
1899 else
1900 psa_mac_abort( operation );
1901 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001902}
1903
Gilles Peskineacd4be32018-07-08 19:56:25 +02001904psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1905 const uint8_t *mac,
1906 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001907{
Gilles Peskine828ed142018-06-18 23:25:51 +02001908 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001909 psa_status_t status;
1910
Gilles Peskine89167cb2018-07-08 20:12:23 +02001911 if( operation->is_sign )
1912 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001913 status = PSA_ERROR_BAD_STATE;
1914 goto cleanup;
1915 }
1916 if( operation->mac_size != mac_length )
1917 {
1918 status = PSA_ERROR_INVALID_SIGNATURE;
1919 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001920 }
mohammad16036df908f2018-04-02 08:34:15 -07001921
1922 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001923 actual_mac, sizeof( actual_mac ) );
1924
1925 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1926 status = PSA_ERROR_INVALID_SIGNATURE;
1927
1928cleanup:
1929 if( status == PSA_SUCCESS )
1930 status = psa_mac_abort( operation );
1931 else
1932 psa_mac_abort( operation );
1933
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02001934 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02001935
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001936 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001937}
1938
1939
Gilles Peskine20035e32018-02-03 22:44:14 +01001940
Gilles Peskine20035e32018-02-03 22:44:14 +01001941/****************************************************************/
1942/* Asymmetric cryptography */
1943/****************************************************************/
1944
Gilles Peskine2b450e32018-06-27 15:42:46 +02001945#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001946/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001947 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001948static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1949 size_t hash_length,
1950 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001951{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001952 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001953 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001954 *md_alg = mbedtls_md_get_type( md_info );
1955
1956 /* The Mbed TLS RSA module uses an unsigned int for hash length
1957 * parameters. Validate that it fits so that we don't risk an
1958 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001959#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001960 if( hash_length > UINT_MAX )
1961 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001962#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001963
1964#if defined(MBEDTLS_PKCS1_V15)
1965 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1966 * must be correct. */
1967 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1968 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001969 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001970 if( md_info == NULL )
1971 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001972 if( mbedtls_md_get_size( md_info ) != hash_length )
1973 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001974 }
1975#endif /* MBEDTLS_PKCS1_V15 */
1976
1977#if defined(MBEDTLS_PKCS1_V21)
1978 /* PSS requires a hash internally. */
1979 if( PSA_ALG_IS_RSA_PSS( alg ) )
1980 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001981 if( md_info == NULL )
1982 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001983 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001984#endif /* MBEDTLS_PKCS1_V21 */
1985
Gilles Peskine61b91d42018-06-08 16:09:36 +02001986 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001987}
1988
Gilles Peskine2b450e32018-06-27 15:42:46 +02001989static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1990 psa_algorithm_t alg,
1991 const uint8_t *hash,
1992 size_t hash_length,
1993 uint8_t *signature,
1994 size_t signature_size,
1995 size_t *signature_length )
1996{
1997 psa_status_t status;
1998 int ret;
1999 mbedtls_md_type_t md_alg;
2000
2001 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2002 if( status != PSA_SUCCESS )
2003 return( status );
2004
Gilles Peskine630a18a2018-06-29 17:49:35 +02002005 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002006 return( PSA_ERROR_BUFFER_TOO_SMALL );
2007
2008#if defined(MBEDTLS_PKCS1_V15)
2009 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2010 {
2011 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2012 MBEDTLS_MD_NONE );
2013 ret = mbedtls_rsa_pkcs1_sign( rsa,
2014 mbedtls_ctr_drbg_random,
2015 &global_data.ctr_drbg,
2016 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002017 md_alg,
2018 (unsigned int) hash_length,
2019 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002020 signature );
2021 }
2022 else
2023#endif /* MBEDTLS_PKCS1_V15 */
2024#if defined(MBEDTLS_PKCS1_V21)
2025 if( PSA_ALG_IS_RSA_PSS( alg ) )
2026 {
2027 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2028 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2029 mbedtls_ctr_drbg_random,
2030 &global_data.ctr_drbg,
2031 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002032 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002033 (unsigned int) hash_length,
2034 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002035 signature );
2036 }
2037 else
2038#endif /* MBEDTLS_PKCS1_V21 */
2039 {
2040 return( PSA_ERROR_INVALID_ARGUMENT );
2041 }
2042
2043 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002044 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002045 return( mbedtls_to_psa_error( ret ) );
2046}
2047
2048static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2049 psa_algorithm_t alg,
2050 const uint8_t *hash,
2051 size_t hash_length,
2052 const uint8_t *signature,
2053 size_t signature_length )
2054{
2055 psa_status_t status;
2056 int ret;
2057 mbedtls_md_type_t md_alg;
2058
2059 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2060 if( status != PSA_SUCCESS )
2061 return( status );
2062
Gilles Peskine630a18a2018-06-29 17:49:35 +02002063 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002064 return( PSA_ERROR_BUFFER_TOO_SMALL );
2065
2066#if defined(MBEDTLS_PKCS1_V15)
2067 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2068 {
2069 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2070 MBEDTLS_MD_NONE );
2071 ret = mbedtls_rsa_pkcs1_verify( rsa,
2072 mbedtls_ctr_drbg_random,
2073 &global_data.ctr_drbg,
2074 MBEDTLS_RSA_PUBLIC,
2075 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002076 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002077 hash,
2078 signature );
2079 }
2080 else
2081#endif /* MBEDTLS_PKCS1_V15 */
2082#if defined(MBEDTLS_PKCS1_V21)
2083 if( PSA_ALG_IS_RSA_PSS( alg ) )
2084 {
2085 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2086 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2087 mbedtls_ctr_drbg_random,
2088 &global_data.ctr_drbg,
2089 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002090 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002091 (unsigned int) hash_length,
2092 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002093 signature );
2094 }
2095 else
2096#endif /* MBEDTLS_PKCS1_V21 */
2097 {
2098 return( PSA_ERROR_INVALID_ARGUMENT );
2099 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002100
2101 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2102 * the rest of the signature is invalid". This has little use in
2103 * practice and PSA doesn't report this distinction. */
2104 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2105 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002106 return( mbedtls_to_psa_error( ret ) );
2107}
2108#endif /* MBEDTLS_RSA_C */
2109
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002110#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002111/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2112 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2113 * (even though these functions don't modify it). */
2114static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2115 psa_algorithm_t alg,
2116 const uint8_t *hash,
2117 size_t hash_length,
2118 uint8_t *signature,
2119 size_t signature_size,
2120 size_t *signature_length )
2121{
2122 int ret;
2123 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002124 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002125 mbedtls_mpi_init( &r );
2126 mbedtls_mpi_init( &s );
2127
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002128 if( signature_size < 2 * curve_bytes )
2129 {
2130 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2131 goto cleanup;
2132 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002133
2134 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2135 {
2136 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2137 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2138 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2139 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2140 hash, hash_length,
2141 md_alg ) );
2142 }
2143 else
2144 {
2145 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2146 hash, hash_length,
2147 mbedtls_ctr_drbg_random,
2148 &global_data.ctr_drbg ) );
2149 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002150
2151 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2152 signature,
2153 curve_bytes ) );
2154 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2155 signature + curve_bytes,
2156 curve_bytes ) );
2157
2158cleanup:
2159 mbedtls_mpi_free( &r );
2160 mbedtls_mpi_free( &s );
2161 if( ret == 0 )
2162 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002163 return( mbedtls_to_psa_error( ret ) );
2164}
2165
2166static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2167 const uint8_t *hash,
2168 size_t hash_length,
2169 const uint8_t *signature,
2170 size_t signature_length )
2171{
2172 int ret;
2173 mbedtls_mpi r, s;
2174 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2175 mbedtls_mpi_init( &r );
2176 mbedtls_mpi_init( &s );
2177
2178 if( signature_length != 2 * curve_bytes )
2179 return( PSA_ERROR_INVALID_SIGNATURE );
2180
2181 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2182 signature,
2183 curve_bytes ) );
2184 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2185 signature + curve_bytes,
2186 curve_bytes ) );
2187
2188 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2189 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002190
2191cleanup:
2192 mbedtls_mpi_free( &r );
2193 mbedtls_mpi_free( &s );
2194 return( mbedtls_to_psa_error( ret ) );
2195}
2196#endif /* MBEDTLS_ECDSA_C */
2197
Gilles Peskine61b91d42018-06-08 16:09:36 +02002198psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2199 psa_algorithm_t alg,
2200 const uint8_t *hash,
2201 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002202 uint8_t *signature,
2203 size_t signature_size,
2204 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002205{
2206 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002207 psa_status_t status;
2208
2209 *signature_length = signature_size;
2210
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002211 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2212 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002213 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002214 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002215 {
2216 status = PSA_ERROR_INVALID_ARGUMENT;
2217 goto exit;
2218 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002219
Gilles Peskine20035e32018-02-03 22:44:14 +01002220#if defined(MBEDTLS_RSA_C)
2221 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2222 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002223 status = psa_rsa_sign( slot->data.rsa,
2224 alg,
2225 hash, hash_length,
2226 signature, signature_size,
2227 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002228 }
2229 else
2230#endif /* defined(MBEDTLS_RSA_C) */
2231#if defined(MBEDTLS_ECP_C)
2232 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2233 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002234#if defined(MBEDTLS_ECDSA_C)
2235 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002236 status = psa_ecdsa_sign( slot->data.ecp,
2237 alg,
2238 hash, hash_length,
2239 signature, signature_size,
2240 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002241 else
2242#endif /* defined(MBEDTLS_ECDSA_C) */
2243 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002244 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002245 }
itayzafrir5c753392018-05-08 11:18:38 +03002246 }
2247 else
2248#endif /* defined(MBEDTLS_ECP_C) */
2249 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002250 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002251 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002252
2253exit:
2254 /* Fill the unused part of the output buffer (the whole buffer on error,
2255 * the trailing part on success) with something that isn't a valid mac
2256 * (barring an attack on the mac and deliberately-crafted input),
2257 * in case the caller doesn't check the return status properly. */
2258 if( status == PSA_SUCCESS )
2259 memset( signature + *signature_length, '!',
2260 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002261 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002262 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002263 /* If signature_size is 0 then we have nothing to do. We must not call
2264 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002265 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002266}
2267
2268psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2269 psa_algorithm_t alg,
2270 const uint8_t *hash,
2271 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002272 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002273 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002274{
2275 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002276 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002277
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002278 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2279 if( status != PSA_SUCCESS )
2280 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002281
Gilles Peskine61b91d42018-06-08 16:09:36 +02002282#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002283 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002284 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002285 return( psa_rsa_verify( slot->data.rsa,
2286 alg,
2287 hash, hash_length,
2288 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002289 }
2290 else
2291#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002292#if defined(MBEDTLS_ECP_C)
2293 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2294 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002295#if defined(MBEDTLS_ECDSA_C)
2296 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002297 return( psa_ecdsa_verify( slot->data.ecp,
2298 hash, hash_length,
2299 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002300 else
2301#endif /* defined(MBEDTLS_ECDSA_C) */
2302 {
2303 return( PSA_ERROR_INVALID_ARGUMENT );
2304 }
itayzafrir5c753392018-05-08 11:18:38 +03002305 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002306 else
2307#endif /* defined(MBEDTLS_ECP_C) */
2308 {
2309 return( PSA_ERROR_NOT_SUPPORTED );
2310 }
2311}
2312
Gilles Peskine072ac562018-06-30 00:21:29 +02002313#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2314static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2315 mbedtls_rsa_context *rsa )
2316{
2317 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2318 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2319 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2320 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2321}
2322#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2323
Gilles Peskine61b91d42018-06-08 16:09:36 +02002324psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2325 psa_algorithm_t alg,
2326 const uint8_t *input,
2327 size_t input_length,
2328 const uint8_t *salt,
2329 size_t salt_length,
2330 uint8_t *output,
2331 size_t output_size,
2332 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002333{
2334 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002335 psa_status_t status;
2336
Darryl Green5cc689a2018-07-24 15:34:10 +01002337 (void) input;
2338 (void) input_length;
2339 (void) salt;
2340 (void) output;
2341 (void) output_size;
2342
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002343 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002344
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002345 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2346 return( PSA_ERROR_INVALID_ARGUMENT );
2347
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002348 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2349 if( status != PSA_SUCCESS )
2350 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002351 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2352 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002353 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002354
2355#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002356 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002357 {
2358 mbedtls_rsa_context *rsa = slot->data.rsa;
2359 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002360 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002361 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002362#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002363 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002364 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002365 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2366 mbedtls_ctr_drbg_random,
2367 &global_data.ctr_drbg,
2368 MBEDTLS_RSA_PUBLIC,
2369 input_length,
2370 input,
2371 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002372 }
2373 else
2374#endif /* MBEDTLS_PKCS1_V15 */
2375#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002376 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002377 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002378 psa_rsa_oaep_set_padding_mode( alg, rsa );
2379 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2380 mbedtls_ctr_drbg_random,
2381 &global_data.ctr_drbg,
2382 MBEDTLS_RSA_PUBLIC,
2383 salt, salt_length,
2384 input_length,
2385 input,
2386 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002387 }
2388 else
2389#endif /* MBEDTLS_PKCS1_V21 */
2390 {
2391 return( PSA_ERROR_INVALID_ARGUMENT );
2392 }
2393 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002394 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002395 return( mbedtls_to_psa_error( ret ) );
2396 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002397 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002398#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002399 {
2400 return( PSA_ERROR_NOT_SUPPORTED );
2401 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002402}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002403
Gilles Peskine61b91d42018-06-08 16:09:36 +02002404psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2405 psa_algorithm_t alg,
2406 const uint8_t *input,
2407 size_t input_length,
2408 const uint8_t *salt,
2409 size_t salt_length,
2410 uint8_t *output,
2411 size_t output_size,
2412 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002413{
2414 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002415 psa_status_t status;
2416
Darryl Green5cc689a2018-07-24 15:34:10 +01002417 (void) input;
2418 (void) input_length;
2419 (void) salt;
2420 (void) output;
2421 (void) output_size;
2422
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002423 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002424
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002425 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2426 return( PSA_ERROR_INVALID_ARGUMENT );
2427
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002428 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2429 if( status != PSA_SUCCESS )
2430 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002431 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2432 return( PSA_ERROR_INVALID_ARGUMENT );
2433
2434#if defined(MBEDTLS_RSA_C)
2435 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2436 {
2437 mbedtls_rsa_context *rsa = slot->data.rsa;
2438 int ret;
2439
Gilles Peskine630a18a2018-06-29 17:49:35 +02002440 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002441 return( PSA_ERROR_INVALID_ARGUMENT );
2442
2443#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002444 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002445 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002446 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2447 mbedtls_ctr_drbg_random,
2448 &global_data.ctr_drbg,
2449 MBEDTLS_RSA_PRIVATE,
2450 output_length,
2451 input,
2452 output,
2453 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002454 }
2455 else
2456#endif /* MBEDTLS_PKCS1_V15 */
2457#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002458 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002459 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002460 psa_rsa_oaep_set_padding_mode( alg, rsa );
2461 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2462 mbedtls_ctr_drbg_random,
2463 &global_data.ctr_drbg,
2464 MBEDTLS_RSA_PRIVATE,
2465 salt, salt_length,
2466 output_length,
2467 input,
2468 output,
2469 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002470 }
2471 else
2472#endif /* MBEDTLS_PKCS1_V21 */
2473 {
2474 return( PSA_ERROR_INVALID_ARGUMENT );
2475 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002476
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002477 return( mbedtls_to_psa_error( ret ) );
2478 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002479 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002480#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002481 {
2482 return( PSA_ERROR_NOT_SUPPORTED );
2483 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002484}
Gilles Peskine20035e32018-02-03 22:44:14 +01002485
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002486
2487
mohammad1603503973b2018-03-12 15:59:30 +02002488/****************************************************************/
2489/* Symmetric cryptography */
2490/****************************************************************/
2491
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002492/* Initialize the cipher operation structure. Once this function has been
2493 * called, psa_cipher_abort can run and will do the right thing. */
2494static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2495 psa_algorithm_t alg )
2496{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002497 if( ! PSA_ALG_IS_CIPHER( alg ) )
2498 {
2499 memset( operation, 0, sizeof( *operation ) );
2500 return( PSA_ERROR_INVALID_ARGUMENT );
2501 }
2502
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002503 operation->alg = alg;
2504 operation->key_set = 0;
2505 operation->iv_set = 0;
2506 operation->iv_required = 1;
2507 operation->iv_size = 0;
2508 operation->block_size = 0;
2509 mbedtls_cipher_init( &operation->ctx.cipher );
2510 return( PSA_SUCCESS );
2511}
2512
Gilles Peskinee553c652018-06-04 16:22:46 +02002513static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2514 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002515 psa_algorithm_t alg,
2516 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002517{
2518 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2519 psa_status_t status;
2520 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002521 size_t key_bits;
2522 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002523 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2524 PSA_KEY_USAGE_ENCRYPT :
2525 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002526
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002527 status = psa_cipher_init( operation, alg );
2528 if( status != PSA_SUCCESS )
2529 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002530
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002531 status = psa_get_key_from_slot( key, &slot, usage, alg);
2532 if( status != PSA_SUCCESS )
2533 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002534 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002535
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002536 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002537 if( cipher_info == NULL )
2538 return( PSA_ERROR_NOT_SUPPORTED );
2539
mohammad1603503973b2018-03-12 15:59:30 +02002540 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002541 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002542 {
2543 psa_cipher_abort( operation );
2544 return( mbedtls_to_psa_error( ret ) );
2545 }
2546
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002547#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002548 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002549 {
2550 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2551 unsigned char keys[24];
2552 memcpy( keys, slot->data.raw.data, 16 );
2553 memcpy( keys + 16, slot->data.raw.data, 8 );
2554 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2555 keys,
2556 192, cipher_operation );
2557 }
2558 else
2559#endif
2560 {
2561 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2562 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002563 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002564 }
Moran Peker41deec42018-04-04 15:43:05 +03002565 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002566 {
2567 psa_cipher_abort( operation );
2568 return( mbedtls_to_psa_error( ret ) );
2569 }
2570
mohammad16038481e742018-03-18 13:57:31 +02002571#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002572 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002573 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002574 case PSA_ALG_CBC_NO_PADDING:
2575 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2576 MBEDTLS_PADDING_NONE );
2577 break;
2578 case PSA_ALG_CBC_PKCS7:
2579 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2580 MBEDTLS_PADDING_PKCS7 );
2581 break;
2582 default:
2583 /* The algorithm doesn't involve padding. */
2584 ret = 0;
2585 break;
2586 }
2587 if( ret != 0 )
2588 {
2589 psa_cipher_abort( operation );
2590 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002591 }
2592#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2593
mohammad1603503973b2018-03-12 15:59:30 +02002594 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002595 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2596 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2597 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002598 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002599 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002600 }
mohammad1603503973b2018-03-12 15:59:30 +02002601
Moran Peker395db872018-05-31 14:07:14 +03002602 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002603}
2604
Gilles Peskinefe119512018-07-08 21:39:34 +02002605psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2606 psa_key_slot_t key,
2607 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002608{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002609 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002610}
2611
Gilles Peskinefe119512018-07-08 21:39:34 +02002612psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2613 psa_key_slot_t key,
2614 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002615{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002616 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002617}
2618
Gilles Peskinefe119512018-07-08 21:39:34 +02002619psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2620 unsigned char *iv,
2621 size_t iv_size,
2622 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002623{
itayzafrir534bd7c2018-08-02 13:56:32 +03002624 psa_status_t status;
2625 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002626 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002627 {
2628 status = PSA_ERROR_BAD_STATE;
2629 goto exit;
2630 }
Moran Peker41deec42018-04-04 15:43:05 +03002631 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002632 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002633 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002634 goto exit;
2635 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002636 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2637 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002638 if( ret != 0 )
2639 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002640 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002641 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002642 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002643
mohammad16038481e742018-03-18 13:57:31 +02002644 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002645 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002646
Moran Peker395db872018-05-31 14:07:14 +03002647exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002648 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002649 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002650 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002651}
2652
Gilles Peskinefe119512018-07-08 21:39:34 +02002653psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2654 const unsigned char *iv,
2655 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002656{
itayzafrir534bd7c2018-08-02 13:56:32 +03002657 psa_status_t status;
2658 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002659 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002660 {
2661 status = PSA_ERROR_BAD_STATE;
2662 goto exit;
2663 }
Moran Pekera28258c2018-05-29 16:25:04 +03002664 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002665 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002666 status = PSA_ERROR_INVALID_ARGUMENT;
2667 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002668 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002669 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2670 status = mbedtls_to_psa_error( ret );
2671exit:
2672 if( status == PSA_SUCCESS )
2673 operation->iv_set = 1;
2674 else
mohammad1603503973b2018-03-12 15:59:30 +02002675 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002676 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002677}
2678
Gilles Peskinee553c652018-06-04 16:22:46 +02002679psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2680 const uint8_t *input,
2681 size_t input_length,
2682 unsigned char *output,
2683 size_t output_size,
2684 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002685{
itayzafrir534bd7c2018-08-02 13:56:32 +03002686 psa_status_t status;
2687 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002688 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002689 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002690 {
2691 /* Take the unprocessed partial block left over from previous
2692 * update calls, if any, plus the input to this call. Remove
2693 * the last partial block, if any. You get the data that will be
2694 * output in this call. */
2695 expected_output_size =
2696 ( operation->ctx.cipher.unprocessed_len + input_length )
2697 / operation->block_size * operation->block_size;
2698 }
2699 else
2700 {
2701 expected_output_size = input_length;
2702 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002703
Gilles Peskine89d789c2018-06-04 17:17:16 +02002704 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002705 {
2706 status = PSA_ERROR_BUFFER_TOO_SMALL;
2707 goto exit;
2708 }
mohammad160382759612018-03-12 18:16:40 +02002709
mohammad1603503973b2018-03-12 15:59:30 +02002710 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002711 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002712 status = mbedtls_to_psa_error( ret );
2713exit:
2714 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002715 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002716 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002717}
2718
Gilles Peskinee553c652018-06-04 16:22:46 +02002719psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2720 uint8_t *output,
2721 size_t output_size,
2722 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002723{
Janos Follath315b51c2018-07-09 16:04:51 +01002724 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2725 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002726 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002727
mohammad1603503973b2018-03-12 15:59:30 +02002728 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002729 {
Janos Follath315b51c2018-07-09 16:04:51 +01002730 status = PSA_ERROR_BAD_STATE;
2731 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002732 }
2733 if( operation->iv_required && ! operation->iv_set )
2734 {
Janos Follath315b51c2018-07-09 16:04:51 +01002735 status = PSA_ERROR_BAD_STATE;
2736 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002737 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002738
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002739 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002740 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2741 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002742 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002743 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002744 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002745 }
2746
Janos Follath315b51c2018-07-09 16:04:51 +01002747 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2748 temp_output_buffer,
2749 output_length );
2750 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002751 {
Janos Follath315b51c2018-07-09 16:04:51 +01002752 status = mbedtls_to_psa_error( cipher_ret );
2753 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002754 }
Janos Follath315b51c2018-07-09 16:04:51 +01002755
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002756 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002757 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002758 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002759 memcpy( output, temp_output_buffer, *output_length );
2760 else
2761 {
Janos Follath315b51c2018-07-09 16:04:51 +01002762 status = PSA_ERROR_BUFFER_TOO_SMALL;
2763 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002764 }
mohammad1603503973b2018-03-12 15:59:30 +02002765
Janos Follath279ab8e2018-07-09 16:13:21 +01002766 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002767 status = psa_cipher_abort( operation );
2768
2769 return( status );
2770
2771error:
2772
2773 *output_length = 0;
2774
Janos Follath279ab8e2018-07-09 16:13:21 +01002775 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002776 (void) psa_cipher_abort( operation );
2777
2778 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002779}
2780
Gilles Peskinee553c652018-06-04 16:22:46 +02002781psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2782{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002783 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002784 {
2785 /* The object has (apparently) been initialized but it is not
2786 * in use. It's ok to call abort on such an object, and there's
2787 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002788 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002789 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002790
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002791 /* Sanity check (shouldn't happen: operation->alg should
2792 * always have been initialized to a valid value). */
2793 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2794 return( PSA_ERROR_BAD_STATE );
2795
mohammad1603503973b2018-03-12 15:59:30 +02002796 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002797
Moran Peker41deec42018-04-04 15:43:05 +03002798 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002799 operation->key_set = 0;
2800 operation->iv_set = 0;
2801 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002802 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002803 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002804
Moran Peker395db872018-05-31 14:07:14 +03002805 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002806}
2807
Gilles Peskinea0655c32018-04-30 17:06:50 +02002808
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002809
mohammad16038cc1cee2018-03-28 01:21:33 +03002810/****************************************************************/
2811/* Key Policy */
2812/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002813
mohammad160327010052018-07-03 13:16:15 +03002814#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002815void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002816{
Gilles Peskine803ce742018-06-18 16:07:14 +02002817 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002818}
2819
Gilles Peskine2d277862018-06-18 15:41:12 +02002820void psa_key_policy_set_usage( psa_key_policy_t *policy,
2821 psa_key_usage_t usage,
2822 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002823{
mohammad16034eed7572018-03-28 05:14:59 -07002824 policy->usage = usage;
2825 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002826}
2827
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002828psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002829{
mohammad16036df908f2018-04-02 08:34:15 -07002830 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002831}
2832
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002833psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002834{
mohammad16036df908f2018-04-02 08:34:15 -07002835 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002836}
mohammad160327010052018-07-03 13:16:15 +03002837#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002838
Gilles Peskine2d277862018-06-18 15:41:12 +02002839psa_status_t psa_set_key_policy( psa_key_slot_t key,
2840 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002841{
2842 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002843 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002844
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002845 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002846 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002847
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002848 status = psa_get_empty_key_slot( key, &slot );
2849 if( status != PSA_SUCCESS )
2850 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002851
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002852 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2853 PSA_KEY_USAGE_ENCRYPT |
2854 PSA_KEY_USAGE_DECRYPT |
2855 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002856 PSA_KEY_USAGE_VERIFY |
2857 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002858 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002859
mohammad16036df908f2018-04-02 08:34:15 -07002860 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002861
2862 return( PSA_SUCCESS );
2863}
2864
Gilles Peskine2d277862018-06-18 15:41:12 +02002865psa_status_t psa_get_key_policy( psa_key_slot_t key,
2866 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002867{
2868 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002869 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002870
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002871 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002872 return( PSA_ERROR_INVALID_ARGUMENT );
2873
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002874 status = psa_get_key_slot( key, &slot );
2875 if( status != PSA_SUCCESS )
2876 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002877
mohammad16036df908f2018-04-02 08:34:15 -07002878 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002879
2880 return( PSA_SUCCESS );
2881}
Gilles Peskine20035e32018-02-03 22:44:14 +01002882
Gilles Peskinea0655c32018-04-30 17:06:50 +02002883
2884
mohammad1603804cd712018-03-20 22:44:08 +02002885/****************************************************************/
2886/* Key Lifetime */
2887/****************************************************************/
2888
Gilles Peskine2d277862018-06-18 15:41:12 +02002889psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2890 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002891{
2892 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002893 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002894
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002895 status = psa_get_key_slot( key, &slot );
2896 if( status != PSA_SUCCESS )
2897 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002898
mohammad1603804cd712018-03-20 22:44:08 +02002899 *lifetime = slot->lifetime;
2900
2901 return( PSA_SUCCESS );
2902}
2903
Gilles Peskine2d277862018-06-18 15:41:12 +02002904psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002905 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002906{
2907 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002908 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002909
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002910 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2911 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002912 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2913 return( PSA_ERROR_INVALID_ARGUMENT );
2914
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002915 status = psa_get_empty_key_slot( key, &slot );
2916 if( status != PSA_SUCCESS )
2917 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002918
Moran Pekerd7326592018-05-29 16:56:39 +03002919 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002920 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002921
mohammad1603060ad8a2018-03-20 14:28:38 -07002922 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002923
2924 return( PSA_SUCCESS );
2925}
2926
Gilles Peskine20035e32018-02-03 22:44:14 +01002927
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002928
mohammad16035955c982018-04-26 00:53:03 +03002929/****************************************************************/
2930/* AEAD */
2931/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002932
Gilles Peskineedf9a652018-08-17 18:11:56 +02002933typedef struct
2934{
2935 key_slot_t *slot;
2936 const mbedtls_cipher_info_t *cipher_info;
2937 union
2938 {
2939#if defined(MBEDTLS_CCM_C)
2940 mbedtls_ccm_context ccm;
2941#endif /* MBEDTLS_CCM_C */
2942#if defined(MBEDTLS_GCM_C)
2943 mbedtls_gcm_context gcm;
2944#endif /* MBEDTLS_GCM_C */
2945 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002946 psa_algorithm_t core_alg;
2947 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002948 uint8_t tag_length;
2949} aead_operation_t;
2950
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002951static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002952{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002953 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002954 {
2955#if defined(MBEDTLS_CCM_C)
2956 case PSA_ALG_CCM:
2957 mbedtls_ccm_free( &operation->ctx.ccm );
2958 break;
2959#endif /* MBEDTLS_CCM_C */
2960#if defined(MBEDTLS_CCM_C)
2961 case PSA_ALG_GCM:
2962 mbedtls_gcm_free( &operation->ctx.gcm );
2963 break;
2964#endif /* MBEDTLS_GCM_C */
2965 }
2966}
2967
2968static psa_status_t psa_aead_setup( aead_operation_t *operation,
2969 psa_key_slot_t key,
2970 psa_key_usage_t usage,
2971 psa_algorithm_t alg )
2972{
2973 psa_status_t status;
2974 size_t key_bits;
2975 mbedtls_cipher_id_t cipher_id;
2976
2977 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
2978 if( status != PSA_SUCCESS )
2979 return( status );
2980
2981 key_bits = psa_get_key_bits( operation->slot );
2982
2983 operation->cipher_info =
2984 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
2985 &cipher_id );
2986 if( operation->cipher_info == NULL )
2987 return( PSA_ERROR_NOT_SUPPORTED );
2988
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002989 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002990 {
2991#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002992 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
2993 operation->core_alg = PSA_ALG_CCM;
2994 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002995 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
2996 return( PSA_ERROR_INVALID_ARGUMENT );
2997 mbedtls_ccm_init( &operation->ctx.ccm );
2998 status = mbedtls_to_psa_error(
2999 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3000 operation->slot->data.raw.data,
3001 (unsigned int) key_bits ) );
3002 if( status != 0 )
3003 goto cleanup;
3004 break;
3005#endif /* MBEDTLS_CCM_C */
3006
3007#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003008 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3009 operation->core_alg = PSA_ALG_GCM;
3010 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003011 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3012 return( PSA_ERROR_INVALID_ARGUMENT );
3013 mbedtls_gcm_init( &operation->ctx.gcm );
3014 status = mbedtls_to_psa_error(
3015 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3016 operation->slot->data.raw.data,
3017 (unsigned int) key_bits ) );
3018 break;
3019#endif /* MBEDTLS_GCM_C */
3020
3021 default:
3022 return( PSA_ERROR_NOT_SUPPORTED );
3023 }
3024
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003025 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3026 {
3027 status = PSA_ERROR_INVALID_ARGUMENT;
3028 goto cleanup;
3029 }
3030 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3031 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3032 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3033 * In both cases, mbedtls_xxx will validate the tag length below. */
3034
Gilles Peskineedf9a652018-08-17 18:11:56 +02003035 return( PSA_SUCCESS );
3036
3037cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003038 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003039 return( status );
3040}
3041
mohammad16035955c982018-04-26 00:53:03 +03003042psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3043 psa_algorithm_t alg,
3044 const uint8_t *nonce,
3045 size_t nonce_length,
3046 const uint8_t *additional_data,
3047 size_t additional_data_length,
3048 const uint8_t *plaintext,
3049 size_t plaintext_length,
3050 uint8_t *ciphertext,
3051 size_t ciphertext_size,
3052 size_t *ciphertext_length )
3053{
mohammad16035955c982018-04-26 00:53:03 +03003054 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003055 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003056 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003057
mohammad1603f08a5502018-06-03 15:05:47 +03003058 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003059
Gilles Peskineedf9a652018-08-17 18:11:56 +02003060 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003061 if( status != PSA_SUCCESS )
3062 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003063
Gilles Peskineedf9a652018-08-17 18:11:56 +02003064 /* For all currently supported modes, the tag is at the end of the
3065 * ciphertext. */
3066 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3067 {
3068 status = PSA_ERROR_BUFFER_TOO_SMALL;
3069 goto exit;
3070 }
3071 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003072
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003073 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003074 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003075 status = mbedtls_to_psa_error(
3076 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3077 MBEDTLS_GCM_ENCRYPT,
3078 plaintext_length,
3079 nonce, nonce_length,
3080 additional_data, additional_data_length,
3081 plaintext, ciphertext,
3082 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003083 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003084 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003085 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003086 status = mbedtls_to_psa_error(
3087 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3088 plaintext_length,
3089 nonce, nonce_length,
3090 additional_data,
3091 additional_data_length,
3092 plaintext, ciphertext,
3093 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003094 }
mohammad16035c8845f2018-05-09 05:40:09 -07003095 else
3096 {
mohammad1603554faad2018-06-03 15:07:38 +03003097 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003098 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003099
Gilles Peskineedf9a652018-08-17 18:11:56 +02003100 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3101 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003102
Gilles Peskineedf9a652018-08-17 18:11:56 +02003103exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003104 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003105 if( status == PSA_SUCCESS )
3106 *ciphertext_length = plaintext_length + operation.tag_length;
3107 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003108}
3109
Gilles Peskineee652a32018-06-01 19:23:52 +02003110/* Locate the tag in a ciphertext buffer containing the encrypted data
3111 * followed by the tag. Return the length of the part preceding the tag in
3112 * *plaintext_length. This is the size of the plaintext in modes where
3113 * the encrypted data has the same size as the plaintext, such as
3114 * CCM and GCM. */
3115static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3116 const uint8_t *ciphertext,
3117 size_t ciphertext_length,
3118 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003119 const uint8_t **p_tag )
3120{
3121 size_t payload_length;
3122 if( tag_length > ciphertext_length )
3123 return( PSA_ERROR_INVALID_ARGUMENT );
3124 payload_length = ciphertext_length - tag_length;
3125 if( payload_length > plaintext_size )
3126 return( PSA_ERROR_BUFFER_TOO_SMALL );
3127 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003128 return( PSA_SUCCESS );
3129}
3130
mohammad16035955c982018-04-26 00:53:03 +03003131psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3132 psa_algorithm_t alg,
3133 const uint8_t *nonce,
3134 size_t nonce_length,
3135 const uint8_t *additional_data,
3136 size_t additional_data_length,
3137 const uint8_t *ciphertext,
3138 size_t ciphertext_length,
3139 uint8_t *plaintext,
3140 size_t plaintext_size,
3141 size_t *plaintext_length )
3142{
mohammad16035955c982018-04-26 00:53:03 +03003143 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003144 aead_operation_t operation;
3145 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003146
Gilles Peskineee652a32018-06-01 19:23:52 +02003147 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003148
Gilles Peskineedf9a652018-08-17 18:11:56 +02003149 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003150 if( status != PSA_SUCCESS )
3151 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003152
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003153 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003154 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003155 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003156 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003157 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003158 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003159 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003160
Gilles Peskineedf9a652018-08-17 18:11:56 +02003161 status = mbedtls_to_psa_error(
3162 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3163 ciphertext_length - operation.tag_length,
3164 nonce, nonce_length,
3165 additional_data,
3166 additional_data_length,
3167 tag, operation.tag_length,
3168 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003169 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003170 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003171 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003172 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003173 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003174 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003175 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003176 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003177
Gilles Peskineedf9a652018-08-17 18:11:56 +02003178 status = mbedtls_to_psa_error(
3179 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3180 ciphertext_length - operation.tag_length,
3181 nonce, nonce_length,
3182 additional_data,
3183 additional_data_length,
3184 ciphertext, plaintext,
3185 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003186 }
mohammad160339574652018-06-01 04:39:53 -07003187 else
3188 {
mohammad1603554faad2018-06-03 15:07:38 +03003189 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003190 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003191
Gilles Peskineedf9a652018-08-17 18:11:56 +02003192 if( status != PSA_SUCCESS && plaintext_size != 0 )
3193 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003194
Gilles Peskineedf9a652018-08-17 18:11:56 +02003195exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003196 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003197 if( status == PSA_SUCCESS )
3198 *plaintext_length = ciphertext_length - operation.tag_length;
3199 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003200}
3201
Gilles Peskinea0655c32018-04-30 17:06:50 +02003202
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003203
Gilles Peskine20035e32018-02-03 22:44:14 +01003204/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003205/* Generators */
3206/****************************************************************/
3207
3208psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3209{
3210 psa_status_t status = PSA_SUCCESS;
3211 if( generator->alg == 0 )
3212 {
3213 /* The object has (apparently) been initialized but it is not
3214 * in use. It's ok to call abort on such an object, and there's
3215 * nothing to do. */
3216 }
3217 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003218#if defined(MBEDTLS_MD_C)
3219 if( PSA_ALG_IS_HKDF( generator->alg ) )
3220 {
3221 mbedtls_free( generator->ctx.hkdf.info );
3222 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3223 }
3224 else
3225#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003226 {
3227 status = PSA_ERROR_BAD_STATE;
3228 }
3229 memset( generator, 0, sizeof( *generator ) );
3230 return( status );
3231}
3232
3233
3234psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3235 size_t *capacity)
3236{
3237 *capacity = generator->capacity;
3238 return( PSA_SUCCESS );
3239}
3240
Gilles Peskinebef7f142018-07-12 17:22:21 +02003241#if defined(MBEDTLS_MD_C)
3242/* Read some bytes from an HKDF-based generator. This performs a chunk
3243 * of the expand phase of the HKDF algorithm. */
3244static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3245 psa_algorithm_t hash_alg,
3246 uint8_t *output,
3247 size_t output_length )
3248{
3249 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3250 psa_status_t status;
3251
3252 while( output_length != 0 )
3253 {
3254 /* Copy what remains of the current block */
3255 uint8_t n = hash_length - hkdf->offset_in_block;
3256 if( n > output_length )
3257 n = (uint8_t) output_length;
3258 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3259 output += n;
3260 output_length -= n;
3261 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003262 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003263 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003264 /* We can't be wanting more output after block 0xff, otherwise
3265 * the capacity check in psa_generator_read() would have
3266 * prevented this call. It could happen only if the generator
3267 * object was corrupted or if this function is called directly
3268 * inside the library. */
3269 if( hkdf->block_number == 0xff )
3270 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003271
3272 /* We need a new block */
3273 ++hkdf->block_number;
3274 hkdf->offset_in_block = 0;
3275 status = psa_hmac_setup_internal( &hkdf->hmac,
3276 hkdf->prk, hash_length,
3277 hash_alg );
3278 if( status != PSA_SUCCESS )
3279 return( status );
3280 if( hkdf->block_number != 1 )
3281 {
3282 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3283 hkdf->output_block,
3284 hash_length );
3285 if( status != PSA_SUCCESS )
3286 return( status );
3287 }
3288 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3289 hkdf->info,
3290 hkdf->info_length );
3291 if( status != PSA_SUCCESS )
3292 return( status );
3293 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3294 &hkdf->block_number, 1 );
3295 if( status != PSA_SUCCESS )
3296 return( status );
3297 status = psa_hmac_finish_internal( &hkdf->hmac,
3298 hkdf->output_block,
3299 sizeof( hkdf->output_block ) );
3300 if( status != PSA_SUCCESS )
3301 return( status );
3302 }
3303
3304 return( PSA_SUCCESS );
3305}
3306#endif /* MBEDTLS_MD_C */
3307
Gilles Peskineeab56e42018-07-12 17:12:33 +02003308psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3309 uint8_t *output,
3310 size_t output_length )
3311{
3312 psa_status_t status;
3313
3314 if( output_length > generator->capacity )
3315 {
3316 generator->capacity = 0;
3317 /* Go through the error path to wipe all confidential data now
3318 * that the generator object is useless. */
3319 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3320 goto exit;
3321 }
3322 if( output_length == 0 &&
3323 generator->capacity == 0 && generator->alg == 0 )
3324 {
3325 /* Edge case: this is a blank or finished generator, and 0
3326 * bytes were requested. The right error in this case could
3327 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3328 * INSUFFICIENT_CAPACITY, which is right for a finished
3329 * generator, for consistency with the case when
3330 * output_length > 0. */
3331 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3332 }
3333 generator->capacity -= output_length;
3334
Gilles Peskinebef7f142018-07-12 17:22:21 +02003335#if defined(MBEDTLS_MD_C)
3336 if( PSA_ALG_IS_HKDF( generator->alg ) )
3337 {
3338 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3339 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3340 output, output_length );
3341 }
3342 else
3343#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003344 {
3345 return( PSA_ERROR_BAD_STATE );
3346 }
3347
3348exit:
3349 if( status != PSA_SUCCESS )
3350 {
3351 psa_generator_abort( generator );
3352 memset( output, '!', output_length );
3353 }
3354 return( status );
3355}
3356
Gilles Peskine08542d82018-07-19 17:05:42 +02003357#if defined(MBEDTLS_DES_C)
3358static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3359{
3360 if( data_size >= 8 )
3361 mbedtls_des_key_set_parity( data );
3362 if( data_size >= 16 )
3363 mbedtls_des_key_set_parity( data + 8 );
3364 if( data_size >= 24 )
3365 mbedtls_des_key_set_parity( data + 16 );
3366}
3367#endif /* MBEDTLS_DES_C */
3368
Gilles Peskineeab56e42018-07-12 17:12:33 +02003369psa_status_t psa_generator_import_key( psa_key_slot_t key,
3370 psa_key_type_t type,
3371 size_t bits,
3372 psa_crypto_generator_t *generator )
3373{
3374 uint8_t *data = NULL;
3375 size_t bytes = PSA_BITS_TO_BYTES( bits );
3376 psa_status_t status;
3377
3378 if( ! key_type_is_raw_bytes( type ) )
3379 return( PSA_ERROR_INVALID_ARGUMENT );
3380 if( bits % 8 != 0 )
3381 return( PSA_ERROR_INVALID_ARGUMENT );
3382 data = mbedtls_calloc( 1, bytes );
3383 if( data == NULL )
3384 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3385
3386 status = psa_generator_read( generator, data, bytes );
3387 if( status != PSA_SUCCESS )
3388 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003389#if defined(MBEDTLS_DES_C)
3390 if( type == PSA_KEY_TYPE_DES )
3391 psa_des_set_key_parity( data, bytes );
3392#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003393 status = psa_import_key( key, type, data, bytes );
3394
3395exit:
3396 mbedtls_free( data );
3397 return( status );
3398}
3399
3400
3401
3402/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003403/* Key derivation */
3404/****************************************************************/
3405
Gilles Peskinebef7f142018-07-12 17:22:21 +02003406/* Set up an HKDF-based generator. This is exactly the extract phase
3407 * of the HKDF algorithm. */
3408static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3409 key_slot_t *slot,
3410 psa_algorithm_t hash_alg,
3411 const uint8_t *salt,
3412 size_t salt_length,
3413 const uint8_t *label,
3414 size_t label_length )
3415{
3416 psa_status_t status;
3417 status = psa_hmac_setup_internal( &hkdf->hmac,
3418 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003419 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003420 if( status != PSA_SUCCESS )
3421 return( status );
3422 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3423 slot->data.raw.data,
3424 slot->data.raw.bytes );
3425 if( status != PSA_SUCCESS )
3426 return( status );
3427 status = psa_hmac_finish_internal( &hkdf->hmac,
3428 hkdf->prk,
3429 sizeof( hkdf->prk ) );
3430 if( status != PSA_SUCCESS )
3431 return( status );
3432 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3433 hkdf->block_number = 0;
3434 hkdf->info_length = label_length;
3435 if( label_length != 0 )
3436 {
3437 hkdf->info = mbedtls_calloc( 1, label_length );
3438 if( hkdf->info == NULL )
3439 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3440 memcpy( hkdf->info, label, label_length );
3441 }
3442 return( PSA_SUCCESS );
3443}
3444
Gilles Peskineea0fb492018-07-12 17:17:20 +02003445psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003446 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003447 psa_algorithm_t alg,
3448 const uint8_t *salt,
3449 size_t salt_length,
3450 const uint8_t *label,
3451 size_t label_length,
3452 size_t capacity )
3453{
3454 key_slot_t *slot;
3455 psa_status_t status;
3456
3457 if( generator->alg != 0 )
3458 return( PSA_ERROR_BAD_STATE );
3459
3460 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3461 if( status != PSA_SUCCESS )
3462 return( status );
3463 if( slot->type != PSA_KEY_TYPE_DERIVE )
3464 return( PSA_ERROR_INVALID_ARGUMENT );
3465
3466 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3467 return( PSA_ERROR_INVALID_ARGUMENT );
3468
Gilles Peskinebef7f142018-07-12 17:22:21 +02003469#if defined(MBEDTLS_MD_C)
3470 if( PSA_ALG_IS_HKDF( alg ) )
3471 {
3472 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3473 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3474 if( hash_size == 0 )
3475 return( PSA_ERROR_NOT_SUPPORTED );
3476 if( capacity > 255 * hash_size )
3477 return( PSA_ERROR_INVALID_ARGUMENT );
3478 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3479 slot,
3480 hash_alg,
3481 salt, salt_length,
3482 label, label_length );
3483 }
3484 else
3485#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003486 {
3487 return( PSA_ERROR_NOT_SUPPORTED );
3488 }
3489
3490 /* Set generator->alg even on failure so that abort knows what to do. */
3491 generator->alg = alg;
3492 if( status == PSA_SUCCESS )
3493 generator->capacity = capacity;
3494 else
3495 psa_generator_abort( generator );
3496 return( status );
3497}
3498
3499
3500
3501/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003502/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003503/****************************************************************/
3504
3505psa_status_t psa_generate_random( uint8_t *output,
3506 size_t output_size )
3507{
itayzafrir0adf0fc2018-09-06 16:24:41 +03003508 int ret;
3509 GUARD_MODULE_INITIALIZED;
3510
3511 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02003512 return( mbedtls_to_psa_error( ret ) );
3513}
3514
3515psa_status_t psa_generate_key( psa_key_slot_t key,
3516 psa_key_type_t type,
3517 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003518 const void *extra,
3519 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003520{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003521 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003522 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003523
Gilles Peskine53d991e2018-07-12 01:14:59 +02003524 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003525 return( PSA_ERROR_INVALID_ARGUMENT );
3526
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003527 status = psa_get_empty_key_slot( key, &slot );
3528 if( status != PSA_SUCCESS )
3529 return( status );
3530
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003531 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003532 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003533 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003534 if( status != PSA_SUCCESS )
3535 return( status );
3536 status = psa_generate_random( slot->data.raw.data,
3537 slot->data.raw.bytes );
3538 if( status != PSA_SUCCESS )
3539 {
3540 mbedtls_free( slot->data.raw.data );
3541 return( status );
3542 }
3543#if defined(MBEDTLS_DES_C)
3544 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003545 psa_des_set_key_parity( slot->data.raw.data,
3546 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003547#endif /* MBEDTLS_DES_C */
3548 }
3549 else
3550
3551#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3552 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3553 {
3554 mbedtls_rsa_context *rsa;
3555 int ret;
3556 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003557 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3558 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003559 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003560 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003561 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003562 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003563 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003564#if INT_MAX < 0xffffffff
3565 /* Check that the uint32_t value passed by the caller fits
3566 * in the range supported by this implementation. */
3567 if( p->e > INT_MAX )
3568 return( PSA_ERROR_NOT_SUPPORTED );
3569#endif
3570 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003571 }
3572 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3573 if( rsa == NULL )
3574 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3575 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3576 ret = mbedtls_rsa_gen_key( rsa,
3577 mbedtls_ctr_drbg_random,
3578 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003579 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003580 exponent );
3581 if( ret != 0 )
3582 {
3583 mbedtls_rsa_free( rsa );
3584 mbedtls_free( rsa );
3585 return( mbedtls_to_psa_error( ret ) );
3586 }
3587 slot->data.rsa = rsa;
3588 }
3589 else
3590#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3591
3592#if defined(MBEDTLS_ECP_C)
3593 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3594 {
3595 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3596 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3597 const mbedtls_ecp_curve_info *curve_info =
3598 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3599 mbedtls_ecp_keypair *ecp;
3600 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003601 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003602 return( PSA_ERROR_NOT_SUPPORTED );
3603 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3604 return( PSA_ERROR_NOT_SUPPORTED );
3605 if( curve_info->bit_size != bits )
3606 return( PSA_ERROR_INVALID_ARGUMENT );
3607 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3608 if( ecp == NULL )
3609 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3610 mbedtls_ecp_keypair_init( ecp );
3611 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3612 mbedtls_ctr_drbg_random,
3613 &global_data.ctr_drbg );
3614 if( ret != 0 )
3615 {
3616 mbedtls_ecp_keypair_free( ecp );
3617 mbedtls_free( ecp );
3618 return( mbedtls_to_psa_error( ret ) );
3619 }
3620 slot->data.ecp = ecp;
3621 }
3622 else
3623#endif /* MBEDTLS_ECP_C */
3624
3625 return( PSA_ERROR_NOT_SUPPORTED );
3626
3627 slot->type = type;
3628 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003629}
3630
3631
3632/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003633/* Module setup */
3634/****************************************************************/
3635
Gilles Peskinee59236f2018-01-27 23:32:46 +01003636void mbedtls_psa_crypto_free( void )
3637{
Jaeden Amero045bd502018-06-26 14:00:08 +01003638 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003639 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003640 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003641 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3642 mbedtls_entropy_free( &global_data.entropy );
3643 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3644}
3645
3646psa_status_t psa_crypto_init( void )
3647{
3648 int ret;
3649 const unsigned char drbg_seed[] = "PSA";
3650
3651 if( global_data.initialized != 0 )
3652 return( PSA_SUCCESS );
3653
3654 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3655 mbedtls_entropy_init( &global_data.entropy );
3656 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3657
3658 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3659 mbedtls_entropy_func,
3660 &global_data.entropy,
3661 drbg_seed, sizeof( drbg_seed ) - 1 );
3662 if( ret != 0 )
3663 goto exit;
3664
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003665 global_data.initialized = 1;
3666
Gilles Peskinee59236f2018-01-27 23:32:46 +01003667exit:
3668 if( ret != 0 )
3669 mbedtls_psa_crypto_free( );
3670 return( mbedtls_to_psa_error( ret ) );
3671}
3672
3673#endif /* MBEDTLS_PSA_CRYPTO_C */