blob: 87f9147a65b6ed86e860e19125092f7bf366c48d [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 );
582 size_t bits = mbedtls_rsa_get_bitlen( rsa );
583 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
584 return( PSA_ERROR_NOT_SUPPORTED );
585 *p_rsa = rsa;
586 return( PSA_SUCCESS );
587 }
588}
589#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
590
591#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100592/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200593static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
594 mbedtls_pk_context *pk,
595 mbedtls_ecp_keypair **p_ecp )
596{
597 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
598 return( PSA_ERROR_INVALID_ARGUMENT );
599 else
600 {
601 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
602 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
603 if( actual_curve != expected_curve )
604 return( PSA_ERROR_INVALID_ARGUMENT );
605 *p_ecp = ecp;
606 return( PSA_SUCCESS );
607 }
608}
609#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
610
Gilles Peskinef76aa772018-10-29 19:24:33 +0100611#if defined(MBEDTLS_ECP_C)
612/* Import a private key given as a byte string which is the private value
613 * in big-endian order. */
614static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
615 const uint8_t *data,
616 size_t data_length,
617 mbedtls_ecp_keypair **p_ecp )
618{
619 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
620 mbedtls_ecp_keypair *ecp = NULL;
621 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
622
623 *p_ecp = NULL;
624 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
625 if( ecp == NULL )
626 return( PSA_ERROR_INSUFFICIENT_MEMORY );
627
628 /* Load the group. */
629 status = mbedtls_to_psa_error(
630 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
631 if( status != PSA_SUCCESS )
632 goto exit;
633 /* Load the secret value. */
634 status = mbedtls_to_psa_error(
635 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
636 if( status != PSA_SUCCESS )
637 goto exit;
638 /* Validate the private key. */
639 status = mbedtls_to_psa_error(
640 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
641 if( status != PSA_SUCCESS )
642 goto exit;
643 /* Calculate the public key from the private key. */
644 status = mbedtls_to_psa_error(
645 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
646 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
647 if( status != PSA_SUCCESS )
648 goto exit;
649
650 *p_ecp = ecp;
651 return( PSA_SUCCESS );
652
653exit:
654 if( ecp != NULL )
655 {
656 mbedtls_ecp_keypair_free( ecp );
657 mbedtls_free( ecp );
658 }
659 return( status );
660}
661#endif /* defined(MBEDTLS_ECP_C) */
662
Gilles Peskine2d277862018-06-18 15:41:12 +0200663psa_status_t psa_import_key( psa_key_slot_t key,
664 psa_key_type_t type,
665 const uint8_t *data,
666 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667{
668 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200669 psa_status_t status = PSA_SUCCESS;
670 status = psa_get_empty_key_slot( key, &slot );
671 if( status != PSA_SUCCESS )
672 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200674 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100676 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 if( data_length > SIZE_MAX / 8 )
678 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200679 status = prepare_raw_data_slot( type,
680 PSA_BYTES_TO_BITS( data_length ),
681 &slot->data.raw );
682 if( status != PSA_SUCCESS )
683 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200684 if( data_length != 0 )
685 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 }
687 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100688#if defined(MBEDTLS_ECP_C)
689 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
690 {
691 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( type ),
692 data, data_length,
693 &slot->data.ecp );
694 if( status != PSA_SUCCESS )
695 return( status );
696 }
697 else
698#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100699#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200700 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100701 {
702 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100703 mbedtls_pk_context pk;
704 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200705
706 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100707 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
708 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
709 else
710 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100711 if( ret != 0 )
712 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200713
714 /* We have something that the pkparse module recognizes.
715 * If it has the expected type and passes any type-specific
716 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100717#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200718 if( PSA_KEY_TYPE_IS_RSA( type ) )
719 status = psa_import_rsa_key( &pk, &slot->data.rsa );
720 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100721#endif /* MBEDTLS_RSA_C */
722#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200723 if( PSA_KEY_TYPE_IS_ECC( type ) )
724 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
725 &pk, &slot->data.ecp );
726 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100727#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200728 {
729 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200730 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200731
Gilles Peskinec648d692018-06-28 08:46:13 +0200732 /* Free the content of the pk object only on error. On success,
733 * the content of the object has been stored in the slot. */
734 if( status != PSA_SUCCESS )
735 {
736 mbedtls_pk_free( &pk );
737 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100738 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 }
740 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100741#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742 {
743 return( PSA_ERROR_NOT_SUPPORTED );
744 }
745
746 slot->type = type;
747 return( PSA_SUCCESS );
748}
749
Gilles Peskine2d277862018-06-18 15:41:12 +0200750psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751{
752 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200753 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100754
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200755 status = psa_get_key_slot( key, &slot );
756 if( status != PSA_SUCCESS )
757 return( status );
758
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100759 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200760 {
761 /* No key material to clean, but do zeroize the slot below to wipe
762 * metadata such as policies. */
763 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200764 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100765 {
766 mbedtls_free( slot->data.raw.data );
767 }
768 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100769#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200770 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100771 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100772 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100773 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100774 }
775 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100776#endif /* defined(MBEDTLS_RSA_C) */
777#if defined(MBEDTLS_ECP_C)
778 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
779 {
780 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100781 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100782 }
783 else
784#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100785 {
786 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100787 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100788 return( PSA_ERROR_TAMPERING_DETECTED );
789 }
790
791 mbedtls_zeroize( slot, sizeof( *slot ) );
792 return( PSA_SUCCESS );
793}
794
Gilles Peskineb870b182018-07-06 16:02:09 +0200795/* Return the size of the key in the given slot, in bits. */
796static size_t psa_get_key_bits( const key_slot_t *slot )
797{
798 if( key_type_is_raw_bytes( slot->type ) )
799 return( slot->data.raw.bytes * 8 );
800#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200801 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200802 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
803#endif /* defined(MBEDTLS_RSA_C) */
804#if defined(MBEDTLS_ECP_C)
805 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
806 return( slot->data.ecp->grp.pbits );
807#endif /* defined(MBEDTLS_ECP_C) */
808 /* Shouldn't happen except on an empty slot. */
809 return( 0 );
810}
811
Gilles Peskine2d277862018-06-18 15:41:12 +0200812psa_status_t psa_get_key_information( psa_key_slot_t key,
813 psa_key_type_t *type,
814 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815{
816 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200817 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200820 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100821 if( bits != NULL )
822 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200823 status = psa_get_key_slot( key, &slot );
824 if( status != PSA_SUCCESS )
825 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200826
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100827 if( slot->type == PSA_KEY_TYPE_NONE )
828 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200829 if( type != NULL )
830 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200831 if( bits != NULL )
832 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100833 return( PSA_SUCCESS );
834}
835
Gilles Peskine2d277862018-06-18 15:41:12 +0200836static psa_status_t psa_internal_export_key( psa_key_slot_t key,
837 uint8_t *data,
838 size_t data_size,
839 size_t *data_length,
840 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100841{
842 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200843 psa_status_t status;
844 /* Exporting a public key doesn't require a usage flag. If we're
845 * called by psa_export_public_key(), don't require the EXPORT flag.
846 * If we're called by psa_export_key(), do require the EXPORT flag;
847 * if the key turns out to be public key object, psa_get_key_from_slot()
848 * will ignore this flag. */
849 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100850
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100851 /* Set the key to empty now, so that even when there are errors, we always
852 * set data_length to a value between 0 and data_size. On error, setting
853 * the key to empty is a good choice because an empty key representation is
854 * unlikely to be accepted anywhere. */
855 *data_length = 0;
856
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200857 status = psa_get_key_from_slot( key, &slot, usage, 0 );
858 if( status != PSA_SUCCESS )
859 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200860 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300861 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300862
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200863 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100864 {
865 if( slot->data.raw.bytes > data_size )
866 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100867 if( data_size != 0 )
868 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200869 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100870 memset( data + slot->data.raw.bytes, 0,
871 data_size - slot->data.raw.bytes );
872 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873 *data_length = slot->data.raw.bytes;
874 return( PSA_SUCCESS );
875 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100876#if defined(MBEDTLS_ECP_C)
877 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
878 {
879 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
880 if( bytes > data_size )
881 return( PSA_ERROR_BUFFER_TOO_SMALL );
882 status = mbedtls_to_psa_error(
883 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
884 if( status != PSA_SUCCESS )
885 return( status );
886 memset( data + bytes, 0, data_size - bytes );
887 *data_length = bytes;
888 return( PSA_SUCCESS );
889 }
890#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891 else
Moran Peker17e36e12018-05-02 12:55:20 +0300892 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100893#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200894 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300895 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100896 {
Moran Pekera998bc62018-04-16 18:16:20 +0300897 mbedtls_pk_context pk;
898 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200899 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300900 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100901#if defined(MBEDTLS_RSA_C)
902 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300903 pk.pk_info = &mbedtls_rsa_info;
904 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100905#else
906 return( PSA_ERROR_NOT_SUPPORTED );
907#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300908 }
909 else
910 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100911#if defined(MBEDTLS_ECP_C)
912 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300913 pk.pk_info = &mbedtls_eckey_info;
914 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100915#else
916 return( PSA_ERROR_NOT_SUPPORTED );
917#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300918 }
Moran Pekerd7326592018-05-29 16:56:39 +0300919 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300920 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300921 else
922 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300923 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200924 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200925 /* If data_size is 0 then data may be NULL and then the
926 * call to memset would have undefined behavior. */
927 if( data_size != 0 )
928 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300929 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200930 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200931 /* The mbedtls_pk_xxx functions write to the end of the buffer.
932 * Move the data to the beginning and erase remaining data
933 * at the original location. */
934 if( 2 * (size_t) ret <= data_size )
935 {
936 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200937 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200938 }
939 else if( (size_t) ret < data_size )
940 {
941 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200942 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200943 }
Moran Pekera998bc62018-04-16 18:16:20 +0300944 *data_length = ret;
945 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100946 }
947 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100948#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300949 {
950 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200951 it is valid for a special-purpose implementation to omit
952 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300953 return( PSA_ERROR_NOT_SUPPORTED );
954 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100955 }
956}
957
Gilles Peskine2d277862018-06-18 15:41:12 +0200958psa_status_t psa_export_key( psa_key_slot_t key,
959 uint8_t *data,
960 size_t data_size,
961 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300962{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200963 return( psa_internal_export_key( key, data, data_size,
964 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100965}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966
Gilles Peskine2d277862018-06-18 15:41:12 +0200967psa_status_t psa_export_public_key( psa_key_slot_t key,
968 uint8_t *data,
969 size_t data_size,
970 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300971{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200972 return( psa_internal_export_key( key, data, data_size,
973 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300974}
975
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200976
977
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100979/* Message digests */
980/****************************************************************/
981
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100982static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100983{
984 switch( alg )
985 {
986#if defined(MBEDTLS_MD2_C)
987 case PSA_ALG_MD2:
988 return( &mbedtls_md2_info );
989#endif
990#if defined(MBEDTLS_MD4_C)
991 case PSA_ALG_MD4:
992 return( &mbedtls_md4_info );
993#endif
994#if defined(MBEDTLS_MD5_C)
995 case PSA_ALG_MD5:
996 return( &mbedtls_md5_info );
997#endif
998#if defined(MBEDTLS_RIPEMD160_C)
999 case PSA_ALG_RIPEMD160:
1000 return( &mbedtls_ripemd160_info );
1001#endif
1002#if defined(MBEDTLS_SHA1_C)
1003 case PSA_ALG_SHA_1:
1004 return( &mbedtls_sha1_info );
1005#endif
1006#if defined(MBEDTLS_SHA256_C)
1007 case PSA_ALG_SHA_224:
1008 return( &mbedtls_sha224_info );
1009 case PSA_ALG_SHA_256:
1010 return( &mbedtls_sha256_info );
1011#endif
1012#if defined(MBEDTLS_SHA512_C)
1013 case PSA_ALG_SHA_384:
1014 return( &mbedtls_sha384_info );
1015 case PSA_ALG_SHA_512:
1016 return( &mbedtls_sha512_info );
1017#endif
1018 default:
1019 return( NULL );
1020 }
1021}
1022
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001023psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1024{
1025 switch( operation->alg )
1026 {
Gilles Peskine81736312018-06-26 15:04:31 +02001027 case 0:
1028 /* The object has (apparently) been initialized but it is not
1029 * in use. It's ok to call abort on such an object, and there's
1030 * nothing to do. */
1031 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001032#if defined(MBEDTLS_MD2_C)
1033 case PSA_ALG_MD2:
1034 mbedtls_md2_free( &operation->ctx.md2 );
1035 break;
1036#endif
1037#if defined(MBEDTLS_MD4_C)
1038 case PSA_ALG_MD4:
1039 mbedtls_md4_free( &operation->ctx.md4 );
1040 break;
1041#endif
1042#if defined(MBEDTLS_MD5_C)
1043 case PSA_ALG_MD5:
1044 mbedtls_md5_free( &operation->ctx.md5 );
1045 break;
1046#endif
1047#if defined(MBEDTLS_RIPEMD160_C)
1048 case PSA_ALG_RIPEMD160:
1049 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1050 break;
1051#endif
1052#if defined(MBEDTLS_SHA1_C)
1053 case PSA_ALG_SHA_1:
1054 mbedtls_sha1_free( &operation->ctx.sha1 );
1055 break;
1056#endif
1057#if defined(MBEDTLS_SHA256_C)
1058 case PSA_ALG_SHA_224:
1059 case PSA_ALG_SHA_256:
1060 mbedtls_sha256_free( &operation->ctx.sha256 );
1061 break;
1062#endif
1063#if defined(MBEDTLS_SHA512_C)
1064 case PSA_ALG_SHA_384:
1065 case PSA_ALG_SHA_512:
1066 mbedtls_sha512_free( &operation->ctx.sha512 );
1067 break;
1068#endif
1069 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001070 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001071 }
1072 operation->alg = 0;
1073 return( PSA_SUCCESS );
1074}
1075
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001076psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001077 psa_algorithm_t alg )
1078{
1079 int ret;
1080 operation->alg = 0;
1081 switch( alg )
1082 {
1083#if defined(MBEDTLS_MD2_C)
1084 case PSA_ALG_MD2:
1085 mbedtls_md2_init( &operation->ctx.md2 );
1086 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1087 break;
1088#endif
1089#if defined(MBEDTLS_MD4_C)
1090 case PSA_ALG_MD4:
1091 mbedtls_md4_init( &operation->ctx.md4 );
1092 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1093 break;
1094#endif
1095#if defined(MBEDTLS_MD5_C)
1096 case PSA_ALG_MD5:
1097 mbedtls_md5_init( &operation->ctx.md5 );
1098 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1099 break;
1100#endif
1101#if defined(MBEDTLS_RIPEMD160_C)
1102 case PSA_ALG_RIPEMD160:
1103 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1104 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1105 break;
1106#endif
1107#if defined(MBEDTLS_SHA1_C)
1108 case PSA_ALG_SHA_1:
1109 mbedtls_sha1_init( &operation->ctx.sha1 );
1110 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1111 break;
1112#endif
1113#if defined(MBEDTLS_SHA256_C)
1114 case PSA_ALG_SHA_224:
1115 mbedtls_sha256_init( &operation->ctx.sha256 );
1116 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1117 break;
1118 case PSA_ALG_SHA_256:
1119 mbedtls_sha256_init( &operation->ctx.sha256 );
1120 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1121 break;
1122#endif
1123#if defined(MBEDTLS_SHA512_C)
1124 case PSA_ALG_SHA_384:
1125 mbedtls_sha512_init( &operation->ctx.sha512 );
1126 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1127 break;
1128 case PSA_ALG_SHA_512:
1129 mbedtls_sha512_init( &operation->ctx.sha512 );
1130 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1131 break;
1132#endif
1133 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001134 return( PSA_ALG_IS_HASH( alg ) ?
1135 PSA_ERROR_NOT_SUPPORTED :
1136 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001137 }
1138 if( ret == 0 )
1139 operation->alg = alg;
1140 else
1141 psa_hash_abort( operation );
1142 return( mbedtls_to_psa_error( ret ) );
1143}
1144
1145psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1146 const uint8_t *input,
1147 size_t input_length )
1148{
1149 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001150
1151 /* Don't require hash implementations to behave correctly on a
1152 * zero-length input, which may have an invalid pointer. */
1153 if( input_length == 0 )
1154 return( PSA_SUCCESS );
1155
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001156 switch( operation->alg )
1157 {
1158#if defined(MBEDTLS_MD2_C)
1159 case PSA_ALG_MD2:
1160 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1161 input, input_length );
1162 break;
1163#endif
1164#if defined(MBEDTLS_MD4_C)
1165 case PSA_ALG_MD4:
1166 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1167 input, input_length );
1168 break;
1169#endif
1170#if defined(MBEDTLS_MD5_C)
1171 case PSA_ALG_MD5:
1172 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1173 input, input_length );
1174 break;
1175#endif
1176#if defined(MBEDTLS_RIPEMD160_C)
1177 case PSA_ALG_RIPEMD160:
1178 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1179 input, input_length );
1180 break;
1181#endif
1182#if defined(MBEDTLS_SHA1_C)
1183 case PSA_ALG_SHA_1:
1184 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1185 input, input_length );
1186 break;
1187#endif
1188#if defined(MBEDTLS_SHA256_C)
1189 case PSA_ALG_SHA_224:
1190 case PSA_ALG_SHA_256:
1191 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1192 input, input_length );
1193 break;
1194#endif
1195#if defined(MBEDTLS_SHA512_C)
1196 case PSA_ALG_SHA_384:
1197 case PSA_ALG_SHA_512:
1198 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1199 input, input_length );
1200 break;
1201#endif
1202 default:
1203 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1204 break;
1205 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001206
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001207 if( ret != 0 )
1208 psa_hash_abort( operation );
1209 return( mbedtls_to_psa_error( ret ) );
1210}
1211
1212psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1213 uint8_t *hash,
1214 size_t hash_size,
1215 size_t *hash_length )
1216{
itayzafrir40835d42018-08-02 13:14:17 +03001217 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001218 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001219 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001220
1221 /* Fill the output buffer with something that isn't a valid hash
1222 * (barring an attack on the hash and deliberately-crafted input),
1223 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001224 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001225 /* If hash_size is 0 then hash may be NULL and then the
1226 * call to memset would have undefined behavior. */
1227 if( hash_size != 0 )
1228 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001229
1230 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001231 {
1232 status = PSA_ERROR_BUFFER_TOO_SMALL;
1233 goto exit;
1234 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001235
1236 switch( operation->alg )
1237 {
1238#if defined(MBEDTLS_MD2_C)
1239 case PSA_ALG_MD2:
1240 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1241 break;
1242#endif
1243#if defined(MBEDTLS_MD4_C)
1244 case PSA_ALG_MD4:
1245 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1246 break;
1247#endif
1248#if defined(MBEDTLS_MD5_C)
1249 case PSA_ALG_MD5:
1250 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1251 break;
1252#endif
1253#if defined(MBEDTLS_RIPEMD160_C)
1254 case PSA_ALG_RIPEMD160:
1255 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1256 break;
1257#endif
1258#if defined(MBEDTLS_SHA1_C)
1259 case PSA_ALG_SHA_1:
1260 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1261 break;
1262#endif
1263#if defined(MBEDTLS_SHA256_C)
1264 case PSA_ALG_SHA_224:
1265 case PSA_ALG_SHA_256:
1266 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1267 break;
1268#endif
1269#if defined(MBEDTLS_SHA512_C)
1270 case PSA_ALG_SHA_384:
1271 case PSA_ALG_SHA_512:
1272 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1273 break;
1274#endif
1275 default:
1276 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1277 break;
1278 }
itayzafrir40835d42018-08-02 13:14:17 +03001279 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001280
itayzafrir40835d42018-08-02 13:14:17 +03001281exit:
1282 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001283 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001284 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001285 return( psa_hash_abort( operation ) );
1286 }
1287 else
1288 {
1289 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001290 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001291 }
1292}
1293
Gilles Peskine2d277862018-06-18 15:41:12 +02001294psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1295 const uint8_t *hash,
1296 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001297{
1298 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1299 size_t actual_hash_length;
1300 psa_status_t status = psa_hash_finish( operation,
1301 actual_hash, sizeof( actual_hash ),
1302 &actual_hash_length );
1303 if( status != PSA_SUCCESS )
1304 return( status );
1305 if( actual_hash_length != hash_length )
1306 return( PSA_ERROR_INVALID_SIGNATURE );
1307 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1308 return( PSA_ERROR_INVALID_SIGNATURE );
1309 return( PSA_SUCCESS );
1310}
1311
1312
1313
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001314/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001315/* MAC */
1316/****************************************************************/
1317
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001318static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001319 psa_algorithm_t alg,
1320 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001321 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001322 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001323{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001324 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001325 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001326
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001327 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001328 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001329
Gilles Peskine8c9def32018-02-08 10:02:12 +01001330 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1331 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001332 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001333 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001334 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001335 mode = MBEDTLS_MODE_STREAM;
1336 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001337 case PSA_ALG_CTR:
1338 mode = MBEDTLS_MODE_CTR;
1339 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001340 case PSA_ALG_CFB:
1341 mode = MBEDTLS_MODE_CFB;
1342 break;
1343 case PSA_ALG_OFB:
1344 mode = MBEDTLS_MODE_OFB;
1345 break;
1346 case PSA_ALG_CBC_NO_PADDING:
1347 mode = MBEDTLS_MODE_CBC;
1348 break;
1349 case PSA_ALG_CBC_PKCS7:
1350 mode = MBEDTLS_MODE_CBC;
1351 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001352 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001353 mode = MBEDTLS_MODE_CCM;
1354 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001355 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001356 mode = MBEDTLS_MODE_GCM;
1357 break;
1358 default:
1359 return( NULL );
1360 }
1361 }
1362 else if( alg == PSA_ALG_CMAC )
1363 mode = MBEDTLS_MODE_ECB;
1364 else if( alg == PSA_ALG_GMAC )
1365 mode = MBEDTLS_MODE_GCM;
1366 else
1367 return( NULL );
1368
1369 switch( key_type )
1370 {
1371 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001372 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001373 break;
1374 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001375 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1376 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001377 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001378 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001379 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001380 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001381 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1382 * but two-key Triple-DES is functionally three-key Triple-DES
1383 * with K1=K3, so that's how we present it to mbedtls. */
1384 if( key_bits == 128 )
1385 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001386 break;
1387 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001388 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001389 break;
1390 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001391 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001392 break;
1393 default:
1394 return( NULL );
1395 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001396 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001397 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001398
Jaeden Amero23bbb752018-06-26 14:16:54 +01001399 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1400 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001401}
1402
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001403static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001404{
Gilles Peskine2d277862018-06-18 15:41:12 +02001405 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001406 {
1407 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001408 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001409 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001410 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001411 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001412 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001413 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001414 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001415 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001416 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001417 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001418 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001419 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001420 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001421 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001422 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001423 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001424 return( 128 );
1425 default:
1426 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001427 }
1428}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001429
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001430/* Initialize the MAC operation structure. Once this function has been
1431 * called, psa_mac_abort can run and will do the right thing. */
1432static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1433 psa_algorithm_t alg )
1434{
1435 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1436
1437 operation->alg = alg;
1438 operation->key_set = 0;
1439 operation->iv_set = 0;
1440 operation->iv_required = 0;
1441 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001442 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001443
1444#if defined(MBEDTLS_CMAC_C)
1445 if( alg == PSA_ALG_CMAC )
1446 {
1447 operation->iv_required = 0;
1448 mbedtls_cipher_init( &operation->ctx.cmac );
1449 status = PSA_SUCCESS;
1450 }
1451 else
1452#endif /* MBEDTLS_CMAC_C */
1453#if defined(MBEDTLS_MD_C)
1454 if( PSA_ALG_IS_HMAC( operation->alg ) )
1455 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001456 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1457 operation->ctx.hmac.hash_ctx.alg = 0;
1458 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001459 }
1460 else
1461#endif /* MBEDTLS_MD_C */
1462 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001463 if( ! PSA_ALG_IS_MAC( alg ) )
1464 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001465 }
1466
1467 if( status != PSA_SUCCESS )
1468 memset( operation, 0, sizeof( *operation ) );
1469 return( status );
1470}
1471
Gilles Peskine01126fa2018-07-12 17:04:55 +02001472#if defined(MBEDTLS_MD_C)
1473static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1474{
1475 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1476 return( psa_hash_abort( &hmac->hash_ctx ) );
1477}
1478#endif /* MBEDTLS_MD_C */
1479
Gilles Peskine8c9def32018-02-08 10:02:12 +01001480psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1481{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001482 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001483 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001484 /* The object has (apparently) been initialized but it is not
1485 * in use. It's ok to call abort on such an object, and there's
1486 * nothing to do. */
1487 return( PSA_SUCCESS );
1488 }
1489 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001490#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001491 if( operation->alg == PSA_ALG_CMAC )
1492 {
1493 mbedtls_cipher_free( &operation->ctx.cmac );
1494 }
1495 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001496#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001497#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001498 if( PSA_ALG_IS_HMAC( operation->alg ) )
1499 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001500 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001501 }
1502 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001503#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001504 {
1505 /* Sanity check (shouldn't happen: operation->alg should
1506 * always have been initialized to a valid value). */
1507 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001508 }
Moran Peker41deec42018-04-04 15:43:05 +03001509
Gilles Peskine8c9def32018-02-08 10:02:12 +01001510 operation->alg = 0;
1511 operation->key_set = 0;
1512 operation->iv_set = 0;
1513 operation->iv_required = 0;
1514 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001515 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001516
Gilles Peskine8c9def32018-02-08 10:02:12 +01001517 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001518
1519bad_state:
1520 /* If abort is called on an uninitialized object, we can't trust
1521 * anything. Wipe the object in case it contains confidential data.
1522 * This may result in a memory leak if a pointer gets overwritten,
1523 * but it's too late to do anything about this. */
1524 memset( operation, 0, sizeof( *operation ) );
1525 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526}
1527
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001528#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001529static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001530 size_t key_bits,
1531 key_slot_t *slot,
1532 const mbedtls_cipher_info_t *cipher_info )
1533{
1534 int ret;
1535
1536 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001537
1538 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1539 if( ret != 0 )
1540 return( ret );
1541
1542 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1543 slot->data.raw.data,
1544 key_bits );
1545 return( ret );
1546}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001547#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001548
Gilles Peskine248051a2018-06-20 16:09:38 +02001549#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001550static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1551 const uint8_t *key,
1552 size_t key_length,
1553 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001554{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001555 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001556 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001557 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001558 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001559 psa_status_t status;
1560
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001561 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1562 * overflow below. This should never trigger if the hash algorithm
1563 * is implemented correctly. */
1564 /* The size checks against the ipad and opad buffers cannot be written
1565 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1566 * because that triggers -Wlogical-op on GCC 7.3. */
1567 if( block_size > sizeof( ipad ) )
1568 return( PSA_ERROR_NOT_SUPPORTED );
1569 if( block_size > sizeof( hmac->opad ) )
1570 return( PSA_ERROR_NOT_SUPPORTED );
1571 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001572 return( PSA_ERROR_NOT_SUPPORTED );
1573
Gilles Peskined223b522018-06-11 18:12:58 +02001574 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001575 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001576 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001577 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001578 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001579 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001580 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001581 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001582 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001583 ipad, sizeof( ipad ), &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 Peskine7e454bc2018-06-11 17:26:17 +02001586 }
Gilles Peskine96889972018-07-12 17:07:03 +02001587 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1588 * but it is permitted. It is common when HMAC is used in HKDF, for
1589 * example. Don't call `memcpy` in the 0-length because `key` could be
1590 * an invalid pointer which would make the behavior undefined. */
1591 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001592 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001593
Gilles Peskined223b522018-06-11 18:12:58 +02001594 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1595 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001596 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001597 ipad[i] ^= 0x36;
1598 memset( ipad + key_length, 0x36, block_size - key_length );
1599
1600 /* Copy the key material from ipad to opad, flipping the requisite bits,
1601 * and filling the rest of opad with the requisite constant. */
1602 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001603 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1604 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001605
Gilles Peskine01126fa2018-07-12 17:04:55 +02001606 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001607 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001608 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001609
Gilles Peskine01126fa2018-07-12 17:04:55 +02001610 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001611
1612cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001613 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001614
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001615 return( status );
1616}
Gilles Peskine248051a2018-06-20 16:09:38 +02001617#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001618
Gilles Peskine89167cb2018-07-08 20:12:23 +02001619static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1620 psa_key_slot_t key,
1621 psa_algorithm_t alg,
1622 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001623{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624 psa_status_t status;
1625 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001626 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001627 psa_key_usage_t usage =
1628 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001629 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001630 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001631
Gilles Peskined911eb72018-08-14 15:18:45 +02001632 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001633 if( status != PSA_SUCCESS )
1634 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001635 if( is_sign )
1636 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001637
Gilles Peskine89167cb2018-07-08 20:12:23 +02001638 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001639 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001640 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001641 key_bits = psa_get_key_bits( slot );
1642
Gilles Peskine8c9def32018-02-08 10:02:12 +01001643#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001644 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001645 {
1646 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001647 mbedtls_cipher_info_from_psa( full_length_alg,
1648 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001649 int ret;
1650 if( cipher_info == NULL )
1651 {
1652 status = PSA_ERROR_NOT_SUPPORTED;
1653 goto exit;
1654 }
1655 operation->mac_size = cipher_info->block_size;
1656 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1657 status = mbedtls_to_psa_error( ret );
1658 }
1659 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001660#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001661#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001662 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001663 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001664 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001665 if( hash_alg == 0 )
1666 {
1667 status = PSA_ERROR_NOT_SUPPORTED;
1668 goto exit;
1669 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001670
1671 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1672 /* Sanity check. This shouldn't fail on a valid configuration. */
1673 if( operation->mac_size == 0 ||
1674 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1675 {
1676 status = PSA_ERROR_NOT_SUPPORTED;
1677 goto exit;
1678 }
1679
Gilles Peskine01126fa2018-07-12 17:04:55 +02001680 if( slot->type != PSA_KEY_TYPE_HMAC )
1681 {
1682 status = PSA_ERROR_INVALID_ARGUMENT;
1683 goto exit;
1684 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001685
Gilles Peskine01126fa2018-07-12 17:04:55 +02001686 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1687 slot->data.raw.data,
1688 slot->data.raw.bytes,
1689 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001690 }
1691 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001693 {
1694 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001695 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001696
Gilles Peskined911eb72018-08-14 15:18:45 +02001697 if( truncated == 0 )
1698 {
1699 /* The "normal" case: untruncated algorithm. Nothing to do. */
1700 }
1701 else if( truncated < 4 )
1702 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001703 /* A very short MAC is too short for security since it can be
1704 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1705 * so we make this our minimum, even though 32 bits is still
1706 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001707 status = PSA_ERROR_NOT_SUPPORTED;
1708 }
1709 else if( truncated > operation->mac_size )
1710 {
1711 /* It's impossible to "truncate" to a larger length. */
1712 status = PSA_ERROR_INVALID_ARGUMENT;
1713 }
1714 else
1715 operation->mac_size = truncated;
1716
Gilles Peskinefbfac682018-07-08 20:51:54 +02001717exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001718 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001719 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001720 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001721 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001722 else
1723 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001724 operation->key_set = 1;
1725 }
1726 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001727}
1728
Gilles Peskine89167cb2018-07-08 20:12:23 +02001729psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1730 psa_key_slot_t key,
1731 psa_algorithm_t alg )
1732{
1733 return( psa_mac_setup( operation, key, alg, 1 ) );
1734}
1735
1736psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1737 psa_key_slot_t key,
1738 psa_algorithm_t alg )
1739{
1740 return( psa_mac_setup( operation, key, alg, 0 ) );
1741}
1742
Gilles Peskine8c9def32018-02-08 10:02:12 +01001743psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1744 const uint8_t *input,
1745 size_t input_length )
1746{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001747 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001748 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001749 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001750 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001751 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001752 operation->has_input = 1;
1753
Gilles Peskine8c9def32018-02-08 10:02:12 +01001754#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001755 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001756 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001757 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1758 input, input_length );
1759 status = mbedtls_to_psa_error( ret );
1760 }
1761 else
1762#endif /* MBEDTLS_CMAC_C */
1763#if defined(MBEDTLS_MD_C)
1764 if( PSA_ALG_IS_HMAC( operation->alg ) )
1765 {
1766 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1767 input_length );
1768 }
1769 else
1770#endif /* MBEDTLS_MD_C */
1771 {
1772 /* This shouldn't happen if `operation` was initialized by
1773 * a setup function. */
1774 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001775 }
1776
Gilles Peskinefbfac682018-07-08 20:51:54 +02001777cleanup:
1778 if( status != PSA_SUCCESS )
1779 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001780 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001781}
1782
Gilles Peskine01126fa2018-07-12 17:04:55 +02001783#if defined(MBEDTLS_MD_C)
1784static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1785 uint8_t *mac,
1786 size_t mac_size )
1787{
1788 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1789 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1790 size_t hash_size = 0;
1791 size_t block_size = psa_get_hash_block_size( hash_alg );
1792 psa_status_t status;
1793
Gilles Peskine01126fa2018-07-12 17:04:55 +02001794 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1795 if( status != PSA_SUCCESS )
1796 return( status );
1797 /* From here on, tmp needs to be wiped. */
1798
1799 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1800 if( status != PSA_SUCCESS )
1801 goto exit;
1802
1803 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1804 if( status != PSA_SUCCESS )
1805 goto exit;
1806
1807 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1808 if( status != PSA_SUCCESS )
1809 goto exit;
1810
Gilles Peskined911eb72018-08-14 15:18:45 +02001811 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1812 if( status != PSA_SUCCESS )
1813 goto exit;
1814
1815 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001816
1817exit:
1818 mbedtls_zeroize( tmp, hash_size );
1819 return( status );
1820}
1821#endif /* MBEDTLS_MD_C */
1822
mohammad16036df908f2018-04-02 08:34:15 -07001823static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001824 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001825 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001826{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001827 if( ! operation->key_set )
1828 return( PSA_ERROR_BAD_STATE );
1829 if( operation->iv_required && ! operation->iv_set )
1830 return( PSA_ERROR_BAD_STATE );
1831
Gilles Peskine8c9def32018-02-08 10:02:12 +01001832 if( mac_size < operation->mac_size )
1833 return( PSA_ERROR_BUFFER_TOO_SMALL );
1834
Gilles Peskine8c9def32018-02-08 10:02:12 +01001835#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001836 if( operation->alg == PSA_ALG_CMAC )
1837 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001838 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1839 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1840 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02001841 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02001842 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001843 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001844 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001845 else
1846#endif /* MBEDTLS_CMAC_C */
1847#if defined(MBEDTLS_MD_C)
1848 if( PSA_ALG_IS_HMAC( operation->alg ) )
1849 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001850 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001851 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001852 }
1853 else
1854#endif /* MBEDTLS_MD_C */
1855 {
1856 /* This shouldn't happen if `operation` was initialized by
1857 * a setup function. */
1858 return( PSA_ERROR_BAD_STATE );
1859 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001860}
1861
Gilles Peskineacd4be32018-07-08 19:56:25 +02001862psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1863 uint8_t *mac,
1864 size_t mac_size,
1865 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001866{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001867 psa_status_t status;
1868
1869 /* Fill the output buffer with something that isn't a valid mac
1870 * (barring an attack on the mac and deliberately-crafted input),
1871 * in case the caller doesn't check the return status properly. */
1872 *mac_length = mac_size;
1873 /* If mac_size is 0 then mac may be NULL and then the
1874 * call to memset would have undefined behavior. */
1875 if( mac_size != 0 )
1876 memset( mac, '!', mac_size );
1877
Gilles Peskine89167cb2018-07-08 20:12:23 +02001878 if( ! operation->is_sign )
1879 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001880 status = PSA_ERROR_BAD_STATE;
1881 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001882 }
mohammad16036df908f2018-04-02 08:34:15 -07001883
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001884 status = psa_mac_finish_internal( operation, mac, mac_size );
1885
1886cleanup:
1887 if( status == PSA_SUCCESS )
1888 {
1889 status = psa_mac_abort( operation );
1890 if( status == PSA_SUCCESS )
1891 *mac_length = operation->mac_size;
1892 else
1893 memset( mac, '!', mac_size );
1894 }
1895 else
1896 psa_mac_abort( operation );
1897 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001898}
1899
Gilles Peskineacd4be32018-07-08 19:56:25 +02001900psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1901 const uint8_t *mac,
1902 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001903{
Gilles Peskine828ed142018-06-18 23:25:51 +02001904 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001905 psa_status_t status;
1906
Gilles Peskine89167cb2018-07-08 20:12:23 +02001907 if( operation->is_sign )
1908 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001909 status = PSA_ERROR_BAD_STATE;
1910 goto cleanup;
1911 }
1912 if( operation->mac_size != mac_length )
1913 {
1914 status = PSA_ERROR_INVALID_SIGNATURE;
1915 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001916 }
mohammad16036df908f2018-04-02 08:34:15 -07001917
1918 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001919 actual_mac, sizeof( actual_mac ) );
1920
1921 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1922 status = PSA_ERROR_INVALID_SIGNATURE;
1923
1924cleanup:
1925 if( status == PSA_SUCCESS )
1926 status = psa_mac_abort( operation );
1927 else
1928 psa_mac_abort( operation );
1929
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02001930 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02001931
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001932 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001933}
1934
1935
Gilles Peskine20035e32018-02-03 22:44:14 +01001936
Gilles Peskine20035e32018-02-03 22:44:14 +01001937/****************************************************************/
1938/* Asymmetric cryptography */
1939/****************************************************************/
1940
Gilles Peskine2b450e32018-06-27 15:42:46 +02001941#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001942/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001943 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001944static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1945 size_t hash_length,
1946 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001947{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001948 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001949 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001950 *md_alg = mbedtls_md_get_type( md_info );
1951
1952 /* The Mbed TLS RSA module uses an unsigned int for hash length
1953 * parameters. Validate that it fits so that we don't risk an
1954 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001955#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001956 if( hash_length > UINT_MAX )
1957 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001958#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001959
1960#if defined(MBEDTLS_PKCS1_V15)
1961 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1962 * must be correct. */
1963 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1964 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001965 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001966 if( md_info == NULL )
1967 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001968 if( mbedtls_md_get_size( md_info ) != hash_length )
1969 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001970 }
1971#endif /* MBEDTLS_PKCS1_V15 */
1972
1973#if defined(MBEDTLS_PKCS1_V21)
1974 /* PSS requires a hash internally. */
1975 if( PSA_ALG_IS_RSA_PSS( alg ) )
1976 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001977 if( md_info == NULL )
1978 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001979 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001980#endif /* MBEDTLS_PKCS1_V21 */
1981
Gilles Peskine61b91d42018-06-08 16:09:36 +02001982 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001983}
1984
Gilles Peskine2b450e32018-06-27 15:42:46 +02001985static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1986 psa_algorithm_t alg,
1987 const uint8_t *hash,
1988 size_t hash_length,
1989 uint8_t *signature,
1990 size_t signature_size,
1991 size_t *signature_length )
1992{
1993 psa_status_t status;
1994 int ret;
1995 mbedtls_md_type_t md_alg;
1996
1997 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1998 if( status != PSA_SUCCESS )
1999 return( status );
2000
Gilles Peskine630a18a2018-06-29 17:49:35 +02002001 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002002 return( PSA_ERROR_BUFFER_TOO_SMALL );
2003
2004#if defined(MBEDTLS_PKCS1_V15)
2005 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2006 {
2007 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2008 MBEDTLS_MD_NONE );
2009 ret = mbedtls_rsa_pkcs1_sign( rsa,
2010 mbedtls_ctr_drbg_random,
2011 &global_data.ctr_drbg,
2012 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002013 md_alg,
2014 (unsigned int) hash_length,
2015 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002016 signature );
2017 }
2018 else
2019#endif /* MBEDTLS_PKCS1_V15 */
2020#if defined(MBEDTLS_PKCS1_V21)
2021 if( PSA_ALG_IS_RSA_PSS( alg ) )
2022 {
2023 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2024 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2025 mbedtls_ctr_drbg_random,
2026 &global_data.ctr_drbg,
2027 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002028 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002029 (unsigned int) hash_length,
2030 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002031 signature );
2032 }
2033 else
2034#endif /* MBEDTLS_PKCS1_V21 */
2035 {
2036 return( PSA_ERROR_INVALID_ARGUMENT );
2037 }
2038
2039 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002040 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002041 return( mbedtls_to_psa_error( ret ) );
2042}
2043
2044static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2045 psa_algorithm_t alg,
2046 const uint8_t *hash,
2047 size_t hash_length,
2048 const uint8_t *signature,
2049 size_t signature_length )
2050{
2051 psa_status_t status;
2052 int ret;
2053 mbedtls_md_type_t md_alg;
2054
2055 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2056 if( status != PSA_SUCCESS )
2057 return( status );
2058
Gilles Peskine630a18a2018-06-29 17:49:35 +02002059 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002060 return( PSA_ERROR_BUFFER_TOO_SMALL );
2061
2062#if defined(MBEDTLS_PKCS1_V15)
2063 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2064 {
2065 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2066 MBEDTLS_MD_NONE );
2067 ret = mbedtls_rsa_pkcs1_verify( rsa,
2068 mbedtls_ctr_drbg_random,
2069 &global_data.ctr_drbg,
2070 MBEDTLS_RSA_PUBLIC,
2071 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002072 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002073 hash,
2074 signature );
2075 }
2076 else
2077#endif /* MBEDTLS_PKCS1_V15 */
2078#if defined(MBEDTLS_PKCS1_V21)
2079 if( PSA_ALG_IS_RSA_PSS( alg ) )
2080 {
2081 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2082 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2083 mbedtls_ctr_drbg_random,
2084 &global_data.ctr_drbg,
2085 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002086 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002087 (unsigned int) hash_length,
2088 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002089 signature );
2090 }
2091 else
2092#endif /* MBEDTLS_PKCS1_V21 */
2093 {
2094 return( PSA_ERROR_INVALID_ARGUMENT );
2095 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002096
2097 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2098 * the rest of the signature is invalid". This has little use in
2099 * practice and PSA doesn't report this distinction. */
2100 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2101 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002102 return( mbedtls_to_psa_error( ret ) );
2103}
2104#endif /* MBEDTLS_RSA_C */
2105
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002106#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002107/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2108 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2109 * (even though these functions don't modify it). */
2110static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2111 psa_algorithm_t alg,
2112 const uint8_t *hash,
2113 size_t hash_length,
2114 uint8_t *signature,
2115 size_t signature_size,
2116 size_t *signature_length )
2117{
2118 int ret;
2119 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002120 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002121 mbedtls_mpi_init( &r );
2122 mbedtls_mpi_init( &s );
2123
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002124 if( signature_size < 2 * curve_bytes )
2125 {
2126 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2127 goto cleanup;
2128 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002129
2130 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2131 {
2132 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2133 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2134 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2135 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2136 hash, hash_length,
2137 md_alg ) );
2138 }
2139 else
2140 {
2141 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2142 hash, hash_length,
2143 mbedtls_ctr_drbg_random,
2144 &global_data.ctr_drbg ) );
2145 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002146
2147 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2148 signature,
2149 curve_bytes ) );
2150 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2151 signature + curve_bytes,
2152 curve_bytes ) );
2153
2154cleanup:
2155 mbedtls_mpi_free( &r );
2156 mbedtls_mpi_free( &s );
2157 if( ret == 0 )
2158 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002159 return( mbedtls_to_psa_error( ret ) );
2160}
2161
2162static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2163 const uint8_t *hash,
2164 size_t hash_length,
2165 const uint8_t *signature,
2166 size_t signature_length )
2167{
2168 int ret;
2169 mbedtls_mpi r, s;
2170 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2171 mbedtls_mpi_init( &r );
2172 mbedtls_mpi_init( &s );
2173
2174 if( signature_length != 2 * curve_bytes )
2175 return( PSA_ERROR_INVALID_SIGNATURE );
2176
2177 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2178 signature,
2179 curve_bytes ) );
2180 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2181 signature + curve_bytes,
2182 curve_bytes ) );
2183
2184 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2185 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002186
2187cleanup:
2188 mbedtls_mpi_free( &r );
2189 mbedtls_mpi_free( &s );
2190 return( mbedtls_to_psa_error( ret ) );
2191}
2192#endif /* MBEDTLS_ECDSA_C */
2193
Gilles Peskine61b91d42018-06-08 16:09:36 +02002194psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2195 psa_algorithm_t alg,
2196 const uint8_t *hash,
2197 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002198 uint8_t *signature,
2199 size_t signature_size,
2200 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002201{
2202 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002203 psa_status_t status;
2204
2205 *signature_length = signature_size;
2206
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002207 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2208 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002209 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002210 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002211 {
2212 status = PSA_ERROR_INVALID_ARGUMENT;
2213 goto exit;
2214 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002215
Gilles Peskine20035e32018-02-03 22:44:14 +01002216#if defined(MBEDTLS_RSA_C)
2217 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2218 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002219 status = psa_rsa_sign( slot->data.rsa,
2220 alg,
2221 hash, hash_length,
2222 signature, signature_size,
2223 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002224 }
2225 else
2226#endif /* defined(MBEDTLS_RSA_C) */
2227#if defined(MBEDTLS_ECP_C)
2228 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2229 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002230#if defined(MBEDTLS_ECDSA_C)
2231 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002232 status = psa_ecdsa_sign( slot->data.ecp,
2233 alg,
2234 hash, hash_length,
2235 signature, signature_size,
2236 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002237 else
2238#endif /* defined(MBEDTLS_ECDSA_C) */
2239 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002240 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002241 }
itayzafrir5c753392018-05-08 11:18:38 +03002242 }
2243 else
2244#endif /* defined(MBEDTLS_ECP_C) */
2245 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002246 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002247 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002248
2249exit:
2250 /* Fill the unused part of the output buffer (the whole buffer on error,
2251 * the trailing part on success) with something that isn't a valid mac
2252 * (barring an attack on the mac and deliberately-crafted input),
2253 * in case the caller doesn't check the return status properly. */
2254 if( status == PSA_SUCCESS )
2255 memset( signature + *signature_length, '!',
2256 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002257 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002258 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002259 /* If signature_size is 0 then we have nothing to do. We must not call
2260 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002261 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002262}
2263
2264psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2265 psa_algorithm_t alg,
2266 const uint8_t *hash,
2267 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002268 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002269 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002270{
2271 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002272 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002273
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002274 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2275 if( status != PSA_SUCCESS )
2276 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002277
Gilles Peskine61b91d42018-06-08 16:09:36 +02002278#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002279 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002280 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002281 return( psa_rsa_verify( slot->data.rsa,
2282 alg,
2283 hash, hash_length,
2284 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002285 }
2286 else
2287#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002288#if defined(MBEDTLS_ECP_C)
2289 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2290 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002291#if defined(MBEDTLS_ECDSA_C)
2292 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002293 return( psa_ecdsa_verify( slot->data.ecp,
2294 hash, hash_length,
2295 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002296 else
2297#endif /* defined(MBEDTLS_ECDSA_C) */
2298 {
2299 return( PSA_ERROR_INVALID_ARGUMENT );
2300 }
itayzafrir5c753392018-05-08 11:18:38 +03002301 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002302 else
2303#endif /* defined(MBEDTLS_ECP_C) */
2304 {
2305 return( PSA_ERROR_NOT_SUPPORTED );
2306 }
2307}
2308
Gilles Peskine072ac562018-06-30 00:21:29 +02002309#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2310static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2311 mbedtls_rsa_context *rsa )
2312{
2313 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2314 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2315 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2316 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2317}
2318#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2319
Gilles Peskine61b91d42018-06-08 16:09:36 +02002320psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2321 psa_algorithm_t alg,
2322 const uint8_t *input,
2323 size_t input_length,
2324 const uint8_t *salt,
2325 size_t salt_length,
2326 uint8_t *output,
2327 size_t output_size,
2328 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002329{
2330 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002331 psa_status_t status;
2332
Darryl Green5cc689a2018-07-24 15:34:10 +01002333 (void) input;
2334 (void) input_length;
2335 (void) salt;
2336 (void) output;
2337 (void) output_size;
2338
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002339 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002340
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002341 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2342 return( PSA_ERROR_INVALID_ARGUMENT );
2343
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002344 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2345 if( status != PSA_SUCCESS )
2346 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002347 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2348 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002349 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002350
2351#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002352 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002353 {
2354 mbedtls_rsa_context *rsa = slot->data.rsa;
2355 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002356 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002357 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002358#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002359 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002360 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002361 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2362 mbedtls_ctr_drbg_random,
2363 &global_data.ctr_drbg,
2364 MBEDTLS_RSA_PUBLIC,
2365 input_length,
2366 input,
2367 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002368 }
2369 else
2370#endif /* MBEDTLS_PKCS1_V15 */
2371#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002372 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002373 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002374 psa_rsa_oaep_set_padding_mode( alg, rsa );
2375 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2376 mbedtls_ctr_drbg_random,
2377 &global_data.ctr_drbg,
2378 MBEDTLS_RSA_PUBLIC,
2379 salt, salt_length,
2380 input_length,
2381 input,
2382 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002383 }
2384 else
2385#endif /* MBEDTLS_PKCS1_V21 */
2386 {
2387 return( PSA_ERROR_INVALID_ARGUMENT );
2388 }
2389 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002390 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002391 return( mbedtls_to_psa_error( ret ) );
2392 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002393 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002394#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002395 {
2396 return( PSA_ERROR_NOT_SUPPORTED );
2397 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002398}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002399
Gilles Peskine61b91d42018-06-08 16:09:36 +02002400psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2401 psa_algorithm_t alg,
2402 const uint8_t *input,
2403 size_t input_length,
2404 const uint8_t *salt,
2405 size_t salt_length,
2406 uint8_t *output,
2407 size_t output_size,
2408 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002409{
2410 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002411 psa_status_t status;
2412
Darryl Green5cc689a2018-07-24 15:34:10 +01002413 (void) input;
2414 (void) input_length;
2415 (void) salt;
2416 (void) output;
2417 (void) output_size;
2418
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002419 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002420
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002421 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2422 return( PSA_ERROR_INVALID_ARGUMENT );
2423
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002424 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2425 if( status != PSA_SUCCESS )
2426 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002427 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2428 return( PSA_ERROR_INVALID_ARGUMENT );
2429
2430#if defined(MBEDTLS_RSA_C)
2431 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2432 {
2433 mbedtls_rsa_context *rsa = slot->data.rsa;
2434 int ret;
2435
Gilles Peskine630a18a2018-06-29 17:49:35 +02002436 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002437 return( PSA_ERROR_INVALID_ARGUMENT );
2438
2439#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002440 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002441 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002442 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2443 mbedtls_ctr_drbg_random,
2444 &global_data.ctr_drbg,
2445 MBEDTLS_RSA_PRIVATE,
2446 output_length,
2447 input,
2448 output,
2449 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002450 }
2451 else
2452#endif /* MBEDTLS_PKCS1_V15 */
2453#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002454 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002455 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002456 psa_rsa_oaep_set_padding_mode( alg, rsa );
2457 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2458 mbedtls_ctr_drbg_random,
2459 &global_data.ctr_drbg,
2460 MBEDTLS_RSA_PRIVATE,
2461 salt, salt_length,
2462 output_length,
2463 input,
2464 output,
2465 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002466 }
2467 else
2468#endif /* MBEDTLS_PKCS1_V21 */
2469 {
2470 return( PSA_ERROR_INVALID_ARGUMENT );
2471 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002472
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002473 return( mbedtls_to_psa_error( ret ) );
2474 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002475 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002476#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002477 {
2478 return( PSA_ERROR_NOT_SUPPORTED );
2479 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002480}
Gilles Peskine20035e32018-02-03 22:44:14 +01002481
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002482
2483
mohammad1603503973b2018-03-12 15:59:30 +02002484/****************************************************************/
2485/* Symmetric cryptography */
2486/****************************************************************/
2487
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002488/* Initialize the cipher operation structure. Once this function has been
2489 * called, psa_cipher_abort can run and will do the right thing. */
2490static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2491 psa_algorithm_t alg )
2492{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002493 if( ! PSA_ALG_IS_CIPHER( alg ) )
2494 {
2495 memset( operation, 0, sizeof( *operation ) );
2496 return( PSA_ERROR_INVALID_ARGUMENT );
2497 }
2498
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002499 operation->alg = alg;
2500 operation->key_set = 0;
2501 operation->iv_set = 0;
2502 operation->iv_required = 1;
2503 operation->iv_size = 0;
2504 operation->block_size = 0;
2505 mbedtls_cipher_init( &operation->ctx.cipher );
2506 return( PSA_SUCCESS );
2507}
2508
Gilles Peskinee553c652018-06-04 16:22:46 +02002509static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2510 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002511 psa_algorithm_t alg,
2512 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002513{
2514 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2515 psa_status_t status;
2516 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002517 size_t key_bits;
2518 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002519 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2520 PSA_KEY_USAGE_ENCRYPT :
2521 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002522
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002523 status = psa_cipher_init( operation, alg );
2524 if( status != PSA_SUCCESS )
2525 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002526
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002527 status = psa_get_key_from_slot( key, &slot, usage, alg);
2528 if( status != PSA_SUCCESS )
2529 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002530 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002531
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002532 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002533 if( cipher_info == NULL )
2534 return( PSA_ERROR_NOT_SUPPORTED );
2535
mohammad1603503973b2018-03-12 15:59:30 +02002536 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002537 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002538 {
2539 psa_cipher_abort( operation );
2540 return( mbedtls_to_psa_error( ret ) );
2541 }
2542
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002543#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002544 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002545 {
2546 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2547 unsigned char keys[24];
2548 memcpy( keys, slot->data.raw.data, 16 );
2549 memcpy( keys + 16, slot->data.raw.data, 8 );
2550 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2551 keys,
2552 192, cipher_operation );
2553 }
2554 else
2555#endif
2556 {
2557 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2558 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002559 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002560 }
Moran Peker41deec42018-04-04 15:43:05 +03002561 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002562 {
2563 psa_cipher_abort( operation );
2564 return( mbedtls_to_psa_error( ret ) );
2565 }
2566
mohammad16038481e742018-03-18 13:57:31 +02002567#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002568 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002569 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002570 case PSA_ALG_CBC_NO_PADDING:
2571 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2572 MBEDTLS_PADDING_NONE );
2573 break;
2574 case PSA_ALG_CBC_PKCS7:
2575 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2576 MBEDTLS_PADDING_PKCS7 );
2577 break;
2578 default:
2579 /* The algorithm doesn't involve padding. */
2580 ret = 0;
2581 break;
2582 }
2583 if( ret != 0 )
2584 {
2585 psa_cipher_abort( operation );
2586 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002587 }
2588#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2589
mohammad1603503973b2018-03-12 15:59:30 +02002590 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002591 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2592 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2593 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002594 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002595 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002596 }
mohammad1603503973b2018-03-12 15:59:30 +02002597
Moran Peker395db872018-05-31 14:07:14 +03002598 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002599}
2600
Gilles Peskinefe119512018-07-08 21:39:34 +02002601psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2602 psa_key_slot_t key,
2603 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002604{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002605 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002606}
2607
Gilles Peskinefe119512018-07-08 21:39:34 +02002608psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2609 psa_key_slot_t key,
2610 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002611{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002612 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002613}
2614
Gilles Peskinefe119512018-07-08 21:39:34 +02002615psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2616 unsigned char *iv,
2617 size_t iv_size,
2618 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002619{
itayzafrir534bd7c2018-08-02 13:56:32 +03002620 psa_status_t status;
2621 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002622 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002623 {
2624 status = PSA_ERROR_BAD_STATE;
2625 goto exit;
2626 }
Moran Peker41deec42018-04-04 15:43:05 +03002627 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002628 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002629 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002630 goto exit;
2631 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002632 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2633 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002634 if( ret != 0 )
2635 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002636 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002637 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002638 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002639
mohammad16038481e742018-03-18 13:57:31 +02002640 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002641 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002642
Moran Peker395db872018-05-31 14:07:14 +03002643exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002644 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002645 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002646 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002647}
2648
Gilles Peskinefe119512018-07-08 21:39:34 +02002649psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2650 const unsigned char *iv,
2651 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002652{
itayzafrir534bd7c2018-08-02 13:56:32 +03002653 psa_status_t status;
2654 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002655 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002656 {
2657 status = PSA_ERROR_BAD_STATE;
2658 goto exit;
2659 }
Moran Pekera28258c2018-05-29 16:25:04 +03002660 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002661 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002662 status = PSA_ERROR_INVALID_ARGUMENT;
2663 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002664 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002665 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2666 status = mbedtls_to_psa_error( ret );
2667exit:
2668 if( status == PSA_SUCCESS )
2669 operation->iv_set = 1;
2670 else
mohammad1603503973b2018-03-12 15:59:30 +02002671 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002672 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002673}
2674
Gilles Peskinee553c652018-06-04 16:22:46 +02002675psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2676 const uint8_t *input,
2677 size_t input_length,
2678 unsigned char *output,
2679 size_t output_size,
2680 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002681{
itayzafrir534bd7c2018-08-02 13:56:32 +03002682 psa_status_t status;
2683 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002684 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002685 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002686 {
2687 /* Take the unprocessed partial block left over from previous
2688 * update calls, if any, plus the input to this call. Remove
2689 * the last partial block, if any. You get the data that will be
2690 * output in this call. */
2691 expected_output_size =
2692 ( operation->ctx.cipher.unprocessed_len + input_length )
2693 / operation->block_size * operation->block_size;
2694 }
2695 else
2696 {
2697 expected_output_size = input_length;
2698 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002699
Gilles Peskine89d789c2018-06-04 17:17:16 +02002700 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002701 {
2702 status = PSA_ERROR_BUFFER_TOO_SMALL;
2703 goto exit;
2704 }
mohammad160382759612018-03-12 18:16:40 +02002705
mohammad1603503973b2018-03-12 15:59:30 +02002706 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002707 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002708 status = mbedtls_to_psa_error( ret );
2709exit:
2710 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002711 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002712 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002713}
2714
Gilles Peskinee553c652018-06-04 16:22:46 +02002715psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2716 uint8_t *output,
2717 size_t output_size,
2718 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002719{
Janos Follath315b51c2018-07-09 16:04:51 +01002720 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2721 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002722 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002723
mohammad1603503973b2018-03-12 15:59:30 +02002724 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002725 {
Janos Follath315b51c2018-07-09 16:04:51 +01002726 status = PSA_ERROR_BAD_STATE;
2727 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002728 }
2729 if( operation->iv_required && ! operation->iv_set )
2730 {
Janos Follath315b51c2018-07-09 16:04:51 +01002731 status = PSA_ERROR_BAD_STATE;
2732 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002733 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002734
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002735 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002736 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2737 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002738 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002739 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002740 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002741 }
2742
Janos Follath315b51c2018-07-09 16:04:51 +01002743 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2744 temp_output_buffer,
2745 output_length );
2746 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002747 {
Janos Follath315b51c2018-07-09 16:04:51 +01002748 status = mbedtls_to_psa_error( cipher_ret );
2749 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002750 }
Janos Follath315b51c2018-07-09 16:04:51 +01002751
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002752 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002753 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002754 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002755 memcpy( output, temp_output_buffer, *output_length );
2756 else
2757 {
Janos Follath315b51c2018-07-09 16:04:51 +01002758 status = PSA_ERROR_BUFFER_TOO_SMALL;
2759 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002760 }
mohammad1603503973b2018-03-12 15:59:30 +02002761
Janos Follath279ab8e2018-07-09 16:13:21 +01002762 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002763 status = psa_cipher_abort( operation );
2764
2765 return( status );
2766
2767error:
2768
2769 *output_length = 0;
2770
Janos Follath279ab8e2018-07-09 16:13:21 +01002771 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002772 (void) psa_cipher_abort( operation );
2773
2774 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002775}
2776
Gilles Peskinee553c652018-06-04 16:22:46 +02002777psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2778{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002779 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002780 {
2781 /* The object has (apparently) been initialized but it is not
2782 * in use. It's ok to call abort on such an object, and there's
2783 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002784 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002785 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002786
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002787 /* Sanity check (shouldn't happen: operation->alg should
2788 * always have been initialized to a valid value). */
2789 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2790 return( PSA_ERROR_BAD_STATE );
2791
mohammad1603503973b2018-03-12 15:59:30 +02002792 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002793
Moran Peker41deec42018-04-04 15:43:05 +03002794 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002795 operation->key_set = 0;
2796 operation->iv_set = 0;
2797 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002798 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002799 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002800
Moran Peker395db872018-05-31 14:07:14 +03002801 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002802}
2803
Gilles Peskinea0655c32018-04-30 17:06:50 +02002804
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002805
mohammad16038cc1cee2018-03-28 01:21:33 +03002806/****************************************************************/
2807/* Key Policy */
2808/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002809
mohammad160327010052018-07-03 13:16:15 +03002810#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002811void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002812{
Gilles Peskine803ce742018-06-18 16:07:14 +02002813 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002814}
2815
Gilles Peskine2d277862018-06-18 15:41:12 +02002816void psa_key_policy_set_usage( psa_key_policy_t *policy,
2817 psa_key_usage_t usage,
2818 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002819{
mohammad16034eed7572018-03-28 05:14:59 -07002820 policy->usage = usage;
2821 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002822}
2823
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002824psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002825{
mohammad16036df908f2018-04-02 08:34:15 -07002826 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002827}
2828
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002829psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002830{
mohammad16036df908f2018-04-02 08:34:15 -07002831 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002832}
mohammad160327010052018-07-03 13:16:15 +03002833#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002834
Gilles Peskine2d277862018-06-18 15:41:12 +02002835psa_status_t psa_set_key_policy( psa_key_slot_t key,
2836 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002837{
2838 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002839 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002840
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002841 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002842 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002843
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002844 status = psa_get_empty_key_slot( key, &slot );
2845 if( status != PSA_SUCCESS )
2846 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002847
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002848 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2849 PSA_KEY_USAGE_ENCRYPT |
2850 PSA_KEY_USAGE_DECRYPT |
2851 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002852 PSA_KEY_USAGE_VERIFY |
2853 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002854 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002855
mohammad16036df908f2018-04-02 08:34:15 -07002856 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002857
2858 return( PSA_SUCCESS );
2859}
2860
Gilles Peskine2d277862018-06-18 15:41:12 +02002861psa_status_t psa_get_key_policy( psa_key_slot_t key,
2862 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002863{
2864 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002865 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002866
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002867 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002868 return( PSA_ERROR_INVALID_ARGUMENT );
2869
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002870 status = psa_get_key_slot( key, &slot );
2871 if( status != PSA_SUCCESS )
2872 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002873
mohammad16036df908f2018-04-02 08:34:15 -07002874 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002875
2876 return( PSA_SUCCESS );
2877}
Gilles Peskine20035e32018-02-03 22:44:14 +01002878
Gilles Peskinea0655c32018-04-30 17:06:50 +02002879
2880
mohammad1603804cd712018-03-20 22:44:08 +02002881/****************************************************************/
2882/* Key Lifetime */
2883/****************************************************************/
2884
Gilles Peskine2d277862018-06-18 15:41:12 +02002885psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2886 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002887{
2888 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002889 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002890
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002891 status = psa_get_key_slot( key, &slot );
2892 if( status != PSA_SUCCESS )
2893 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002894
mohammad1603804cd712018-03-20 22:44:08 +02002895 *lifetime = slot->lifetime;
2896
2897 return( PSA_SUCCESS );
2898}
2899
Gilles Peskine2d277862018-06-18 15:41:12 +02002900psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002901 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002902{
2903 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002904 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002905
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002906 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2907 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002908 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2909 return( PSA_ERROR_INVALID_ARGUMENT );
2910
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002911 status = psa_get_empty_key_slot( key, &slot );
2912 if( status != PSA_SUCCESS )
2913 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002914
Moran Pekerd7326592018-05-29 16:56:39 +03002915 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002916 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002917
mohammad1603060ad8a2018-03-20 14:28:38 -07002918 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002919
2920 return( PSA_SUCCESS );
2921}
2922
Gilles Peskine20035e32018-02-03 22:44:14 +01002923
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002924
mohammad16035955c982018-04-26 00:53:03 +03002925/****************************************************************/
2926/* AEAD */
2927/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002928
Gilles Peskineedf9a652018-08-17 18:11:56 +02002929typedef struct
2930{
2931 key_slot_t *slot;
2932 const mbedtls_cipher_info_t *cipher_info;
2933 union
2934 {
2935#if defined(MBEDTLS_CCM_C)
2936 mbedtls_ccm_context ccm;
2937#endif /* MBEDTLS_CCM_C */
2938#if defined(MBEDTLS_GCM_C)
2939 mbedtls_gcm_context gcm;
2940#endif /* MBEDTLS_GCM_C */
2941 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002942 psa_algorithm_t core_alg;
2943 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002944 uint8_t tag_length;
2945} aead_operation_t;
2946
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002947static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002948{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002949 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002950 {
2951#if defined(MBEDTLS_CCM_C)
2952 case PSA_ALG_CCM:
2953 mbedtls_ccm_free( &operation->ctx.ccm );
2954 break;
2955#endif /* MBEDTLS_CCM_C */
2956#if defined(MBEDTLS_CCM_C)
2957 case PSA_ALG_GCM:
2958 mbedtls_gcm_free( &operation->ctx.gcm );
2959 break;
2960#endif /* MBEDTLS_GCM_C */
2961 }
2962}
2963
2964static psa_status_t psa_aead_setup( aead_operation_t *operation,
2965 psa_key_slot_t key,
2966 psa_key_usage_t usage,
2967 psa_algorithm_t alg )
2968{
2969 psa_status_t status;
2970 size_t key_bits;
2971 mbedtls_cipher_id_t cipher_id;
2972
2973 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
2974 if( status != PSA_SUCCESS )
2975 return( status );
2976
2977 key_bits = psa_get_key_bits( operation->slot );
2978
2979 operation->cipher_info =
2980 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
2981 &cipher_id );
2982 if( operation->cipher_info == NULL )
2983 return( PSA_ERROR_NOT_SUPPORTED );
2984
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002985 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002986 {
2987#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002988 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
2989 operation->core_alg = PSA_ALG_CCM;
2990 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002991 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
2992 return( PSA_ERROR_INVALID_ARGUMENT );
2993 mbedtls_ccm_init( &operation->ctx.ccm );
2994 status = mbedtls_to_psa_error(
2995 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
2996 operation->slot->data.raw.data,
2997 (unsigned int) key_bits ) );
2998 if( status != 0 )
2999 goto cleanup;
3000 break;
3001#endif /* MBEDTLS_CCM_C */
3002
3003#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003004 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3005 operation->core_alg = PSA_ALG_GCM;
3006 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003007 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3008 return( PSA_ERROR_INVALID_ARGUMENT );
3009 mbedtls_gcm_init( &operation->ctx.gcm );
3010 status = mbedtls_to_psa_error(
3011 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3012 operation->slot->data.raw.data,
3013 (unsigned int) key_bits ) );
3014 break;
3015#endif /* MBEDTLS_GCM_C */
3016
3017 default:
3018 return( PSA_ERROR_NOT_SUPPORTED );
3019 }
3020
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003021 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3022 {
3023 status = PSA_ERROR_INVALID_ARGUMENT;
3024 goto cleanup;
3025 }
3026 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3027 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3028 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3029 * In both cases, mbedtls_xxx will validate the tag length below. */
3030
Gilles Peskineedf9a652018-08-17 18:11:56 +02003031 return( PSA_SUCCESS );
3032
3033cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003034 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003035 return( status );
3036}
3037
mohammad16035955c982018-04-26 00:53:03 +03003038psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3039 psa_algorithm_t alg,
3040 const uint8_t *nonce,
3041 size_t nonce_length,
3042 const uint8_t *additional_data,
3043 size_t additional_data_length,
3044 const uint8_t *plaintext,
3045 size_t plaintext_length,
3046 uint8_t *ciphertext,
3047 size_t ciphertext_size,
3048 size_t *ciphertext_length )
3049{
mohammad16035955c982018-04-26 00:53:03 +03003050 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003051 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003052 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003053
mohammad1603f08a5502018-06-03 15:05:47 +03003054 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003055
Gilles Peskineedf9a652018-08-17 18:11:56 +02003056 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003057 if( status != PSA_SUCCESS )
3058 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003059
Gilles Peskineedf9a652018-08-17 18:11:56 +02003060 /* For all currently supported modes, the tag is at the end of the
3061 * ciphertext. */
3062 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3063 {
3064 status = PSA_ERROR_BUFFER_TOO_SMALL;
3065 goto exit;
3066 }
3067 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003068
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003069 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003070 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003071 status = mbedtls_to_psa_error(
3072 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3073 MBEDTLS_GCM_ENCRYPT,
3074 plaintext_length,
3075 nonce, nonce_length,
3076 additional_data, additional_data_length,
3077 plaintext, ciphertext,
3078 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003079 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003080 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003081 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003082 status = mbedtls_to_psa_error(
3083 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3084 plaintext_length,
3085 nonce, nonce_length,
3086 additional_data,
3087 additional_data_length,
3088 plaintext, ciphertext,
3089 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003090 }
mohammad16035c8845f2018-05-09 05:40:09 -07003091 else
3092 {
mohammad1603554faad2018-06-03 15:07:38 +03003093 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003094 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003095
Gilles Peskineedf9a652018-08-17 18:11:56 +02003096 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3097 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003098
Gilles Peskineedf9a652018-08-17 18:11:56 +02003099exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003100 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003101 if( status == PSA_SUCCESS )
3102 *ciphertext_length = plaintext_length + operation.tag_length;
3103 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003104}
3105
Gilles Peskineee652a32018-06-01 19:23:52 +02003106/* Locate the tag in a ciphertext buffer containing the encrypted data
3107 * followed by the tag. Return the length of the part preceding the tag in
3108 * *plaintext_length. This is the size of the plaintext in modes where
3109 * the encrypted data has the same size as the plaintext, such as
3110 * CCM and GCM. */
3111static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3112 const uint8_t *ciphertext,
3113 size_t ciphertext_length,
3114 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003115 const uint8_t **p_tag )
3116{
3117 size_t payload_length;
3118 if( tag_length > ciphertext_length )
3119 return( PSA_ERROR_INVALID_ARGUMENT );
3120 payload_length = ciphertext_length - tag_length;
3121 if( payload_length > plaintext_size )
3122 return( PSA_ERROR_BUFFER_TOO_SMALL );
3123 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003124 return( PSA_SUCCESS );
3125}
3126
mohammad16035955c982018-04-26 00:53:03 +03003127psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3128 psa_algorithm_t alg,
3129 const uint8_t *nonce,
3130 size_t nonce_length,
3131 const uint8_t *additional_data,
3132 size_t additional_data_length,
3133 const uint8_t *ciphertext,
3134 size_t ciphertext_length,
3135 uint8_t *plaintext,
3136 size_t plaintext_size,
3137 size_t *plaintext_length )
3138{
mohammad16035955c982018-04-26 00:53:03 +03003139 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003140 aead_operation_t operation;
3141 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003142
Gilles Peskineee652a32018-06-01 19:23:52 +02003143 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003144
Gilles Peskineedf9a652018-08-17 18:11:56 +02003145 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003146 if( status != PSA_SUCCESS )
3147 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003148
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003149 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003150 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003151 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003152 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003153 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003154 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003155 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003156
Gilles Peskineedf9a652018-08-17 18:11:56 +02003157 status = mbedtls_to_psa_error(
3158 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3159 ciphertext_length - operation.tag_length,
3160 nonce, nonce_length,
3161 additional_data,
3162 additional_data_length,
3163 tag, operation.tag_length,
3164 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003165 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003166 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003167 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003168 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003169 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003170 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003171 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003172 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003173
Gilles Peskineedf9a652018-08-17 18:11:56 +02003174 status = mbedtls_to_psa_error(
3175 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3176 ciphertext_length - operation.tag_length,
3177 nonce, nonce_length,
3178 additional_data,
3179 additional_data_length,
3180 ciphertext, plaintext,
3181 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003182 }
mohammad160339574652018-06-01 04:39:53 -07003183 else
3184 {
mohammad1603554faad2018-06-03 15:07:38 +03003185 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003186 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003187
Gilles Peskineedf9a652018-08-17 18:11:56 +02003188 if( status != PSA_SUCCESS && plaintext_size != 0 )
3189 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003190
Gilles Peskineedf9a652018-08-17 18:11:56 +02003191exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003192 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003193 if( status == PSA_SUCCESS )
3194 *plaintext_length = ciphertext_length - operation.tag_length;
3195 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003196}
3197
Gilles Peskinea0655c32018-04-30 17:06:50 +02003198
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003199
Gilles Peskine20035e32018-02-03 22:44:14 +01003200/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003201/* Generators */
3202/****************************************************************/
3203
3204psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3205{
3206 psa_status_t status = PSA_SUCCESS;
3207 if( generator->alg == 0 )
3208 {
3209 /* The object has (apparently) been initialized but it is not
3210 * in use. It's ok to call abort on such an object, and there's
3211 * nothing to do. */
3212 }
3213 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003214#if defined(MBEDTLS_MD_C)
3215 if( PSA_ALG_IS_HKDF( generator->alg ) )
3216 {
3217 mbedtls_free( generator->ctx.hkdf.info );
3218 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3219 }
3220 else
3221#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003222 {
3223 status = PSA_ERROR_BAD_STATE;
3224 }
3225 memset( generator, 0, sizeof( *generator ) );
3226 return( status );
3227}
3228
3229
3230psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3231 size_t *capacity)
3232{
3233 *capacity = generator->capacity;
3234 return( PSA_SUCCESS );
3235}
3236
Gilles Peskinebef7f142018-07-12 17:22:21 +02003237#if defined(MBEDTLS_MD_C)
3238/* Read some bytes from an HKDF-based generator. This performs a chunk
3239 * of the expand phase of the HKDF algorithm. */
3240static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3241 psa_algorithm_t hash_alg,
3242 uint8_t *output,
3243 size_t output_length )
3244{
3245 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3246 psa_status_t status;
3247
3248 while( output_length != 0 )
3249 {
3250 /* Copy what remains of the current block */
3251 uint8_t n = hash_length - hkdf->offset_in_block;
3252 if( n > output_length )
3253 n = (uint8_t) output_length;
3254 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3255 output += n;
3256 output_length -= n;
3257 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003258 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003259 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003260 /* We can't be wanting more output after block 0xff, otherwise
3261 * the capacity check in psa_generator_read() would have
3262 * prevented this call. It could happen only if the generator
3263 * object was corrupted or if this function is called directly
3264 * inside the library. */
3265 if( hkdf->block_number == 0xff )
3266 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003267
3268 /* We need a new block */
3269 ++hkdf->block_number;
3270 hkdf->offset_in_block = 0;
3271 status = psa_hmac_setup_internal( &hkdf->hmac,
3272 hkdf->prk, hash_length,
3273 hash_alg );
3274 if( status != PSA_SUCCESS )
3275 return( status );
3276 if( hkdf->block_number != 1 )
3277 {
3278 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3279 hkdf->output_block,
3280 hash_length );
3281 if( status != PSA_SUCCESS )
3282 return( status );
3283 }
3284 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3285 hkdf->info,
3286 hkdf->info_length );
3287 if( status != PSA_SUCCESS )
3288 return( status );
3289 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3290 &hkdf->block_number, 1 );
3291 if( status != PSA_SUCCESS )
3292 return( status );
3293 status = psa_hmac_finish_internal( &hkdf->hmac,
3294 hkdf->output_block,
3295 sizeof( hkdf->output_block ) );
3296 if( status != PSA_SUCCESS )
3297 return( status );
3298 }
3299
3300 return( PSA_SUCCESS );
3301}
3302#endif /* MBEDTLS_MD_C */
3303
Gilles Peskineeab56e42018-07-12 17:12:33 +02003304psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3305 uint8_t *output,
3306 size_t output_length )
3307{
3308 psa_status_t status;
3309
3310 if( output_length > generator->capacity )
3311 {
3312 generator->capacity = 0;
3313 /* Go through the error path to wipe all confidential data now
3314 * that the generator object is useless. */
3315 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3316 goto exit;
3317 }
3318 if( output_length == 0 &&
3319 generator->capacity == 0 && generator->alg == 0 )
3320 {
3321 /* Edge case: this is a blank or finished generator, and 0
3322 * bytes were requested. The right error in this case could
3323 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3324 * INSUFFICIENT_CAPACITY, which is right for a finished
3325 * generator, for consistency with the case when
3326 * output_length > 0. */
3327 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3328 }
3329 generator->capacity -= output_length;
3330
Gilles Peskinebef7f142018-07-12 17:22:21 +02003331#if defined(MBEDTLS_MD_C)
3332 if( PSA_ALG_IS_HKDF( generator->alg ) )
3333 {
3334 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3335 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3336 output, output_length );
3337 }
3338 else
3339#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003340 {
3341 return( PSA_ERROR_BAD_STATE );
3342 }
3343
3344exit:
3345 if( status != PSA_SUCCESS )
3346 {
3347 psa_generator_abort( generator );
3348 memset( output, '!', output_length );
3349 }
3350 return( status );
3351}
3352
Gilles Peskine08542d82018-07-19 17:05:42 +02003353#if defined(MBEDTLS_DES_C)
3354static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3355{
3356 if( data_size >= 8 )
3357 mbedtls_des_key_set_parity( data );
3358 if( data_size >= 16 )
3359 mbedtls_des_key_set_parity( data + 8 );
3360 if( data_size >= 24 )
3361 mbedtls_des_key_set_parity( data + 16 );
3362}
3363#endif /* MBEDTLS_DES_C */
3364
Gilles Peskineeab56e42018-07-12 17:12:33 +02003365psa_status_t psa_generator_import_key( psa_key_slot_t key,
3366 psa_key_type_t type,
3367 size_t bits,
3368 psa_crypto_generator_t *generator )
3369{
3370 uint8_t *data = NULL;
3371 size_t bytes = PSA_BITS_TO_BYTES( bits );
3372 psa_status_t status;
3373
3374 if( ! key_type_is_raw_bytes( type ) )
3375 return( PSA_ERROR_INVALID_ARGUMENT );
3376 if( bits % 8 != 0 )
3377 return( PSA_ERROR_INVALID_ARGUMENT );
3378 data = mbedtls_calloc( 1, bytes );
3379 if( data == NULL )
3380 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3381
3382 status = psa_generator_read( generator, data, bytes );
3383 if( status != PSA_SUCCESS )
3384 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003385#if defined(MBEDTLS_DES_C)
3386 if( type == PSA_KEY_TYPE_DES )
3387 psa_des_set_key_parity( data, bytes );
3388#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003389 status = psa_import_key( key, type, data, bytes );
3390
3391exit:
3392 mbedtls_free( data );
3393 return( status );
3394}
3395
3396
3397
3398/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003399/* Key derivation */
3400/****************************************************************/
3401
Gilles Peskinebef7f142018-07-12 17:22:21 +02003402/* Set up an HKDF-based generator. This is exactly the extract phase
3403 * of the HKDF algorithm. */
3404static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3405 key_slot_t *slot,
3406 psa_algorithm_t hash_alg,
3407 const uint8_t *salt,
3408 size_t salt_length,
3409 const uint8_t *label,
3410 size_t label_length )
3411{
3412 psa_status_t status;
3413 status = psa_hmac_setup_internal( &hkdf->hmac,
3414 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003415 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003416 if( status != PSA_SUCCESS )
3417 return( status );
3418 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3419 slot->data.raw.data,
3420 slot->data.raw.bytes );
3421 if( status != PSA_SUCCESS )
3422 return( status );
3423 status = psa_hmac_finish_internal( &hkdf->hmac,
3424 hkdf->prk,
3425 sizeof( hkdf->prk ) );
3426 if( status != PSA_SUCCESS )
3427 return( status );
3428 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3429 hkdf->block_number = 0;
3430 hkdf->info_length = label_length;
3431 if( label_length != 0 )
3432 {
3433 hkdf->info = mbedtls_calloc( 1, label_length );
3434 if( hkdf->info == NULL )
3435 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3436 memcpy( hkdf->info, label, label_length );
3437 }
3438 return( PSA_SUCCESS );
3439}
3440
Gilles Peskineea0fb492018-07-12 17:17:20 +02003441psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003442 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003443 psa_algorithm_t alg,
3444 const uint8_t *salt,
3445 size_t salt_length,
3446 const uint8_t *label,
3447 size_t label_length,
3448 size_t capacity )
3449{
3450 key_slot_t *slot;
3451 psa_status_t status;
3452
3453 if( generator->alg != 0 )
3454 return( PSA_ERROR_BAD_STATE );
3455
3456 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3457 if( status != PSA_SUCCESS )
3458 return( status );
3459 if( slot->type != PSA_KEY_TYPE_DERIVE )
3460 return( PSA_ERROR_INVALID_ARGUMENT );
3461
3462 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3463 return( PSA_ERROR_INVALID_ARGUMENT );
3464
Gilles Peskinebef7f142018-07-12 17:22:21 +02003465#if defined(MBEDTLS_MD_C)
3466 if( PSA_ALG_IS_HKDF( alg ) )
3467 {
3468 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3469 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3470 if( hash_size == 0 )
3471 return( PSA_ERROR_NOT_SUPPORTED );
3472 if( capacity > 255 * hash_size )
3473 return( PSA_ERROR_INVALID_ARGUMENT );
3474 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3475 slot,
3476 hash_alg,
3477 salt, salt_length,
3478 label, label_length );
3479 }
3480 else
3481#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003482 {
3483 return( PSA_ERROR_NOT_SUPPORTED );
3484 }
3485
3486 /* Set generator->alg even on failure so that abort knows what to do. */
3487 generator->alg = alg;
3488 if( status == PSA_SUCCESS )
3489 generator->capacity = capacity;
3490 else
3491 psa_generator_abort( generator );
3492 return( status );
3493}
3494
3495
3496
3497/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003498/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003499/****************************************************************/
3500
3501psa_status_t psa_generate_random( uint8_t *output,
3502 size_t output_size )
3503{
itayzafrir0adf0fc2018-09-06 16:24:41 +03003504 int ret;
3505 GUARD_MODULE_INITIALIZED;
3506
3507 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02003508 return( mbedtls_to_psa_error( ret ) );
3509}
3510
3511psa_status_t psa_generate_key( psa_key_slot_t key,
3512 psa_key_type_t type,
3513 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003514 const void *extra,
3515 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003516{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003517 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003518 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003519
Gilles Peskine53d991e2018-07-12 01:14:59 +02003520 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003521 return( PSA_ERROR_INVALID_ARGUMENT );
3522
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003523 status = psa_get_empty_key_slot( key, &slot );
3524 if( status != PSA_SUCCESS )
3525 return( status );
3526
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003527 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003528 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003529 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003530 if( status != PSA_SUCCESS )
3531 return( status );
3532 status = psa_generate_random( slot->data.raw.data,
3533 slot->data.raw.bytes );
3534 if( status != PSA_SUCCESS )
3535 {
3536 mbedtls_free( slot->data.raw.data );
3537 return( status );
3538 }
3539#if defined(MBEDTLS_DES_C)
3540 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003541 psa_des_set_key_parity( slot->data.raw.data,
3542 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003543#endif /* MBEDTLS_DES_C */
3544 }
3545 else
3546
3547#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3548 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3549 {
3550 mbedtls_rsa_context *rsa;
3551 int ret;
3552 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003553 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3554 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003555 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003556 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003557 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003558 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003559 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003560#if INT_MAX < 0xffffffff
3561 /* Check that the uint32_t value passed by the caller fits
3562 * in the range supported by this implementation. */
3563 if( p->e > INT_MAX )
3564 return( PSA_ERROR_NOT_SUPPORTED );
3565#endif
3566 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003567 }
3568 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3569 if( rsa == NULL )
3570 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3571 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3572 ret = mbedtls_rsa_gen_key( rsa,
3573 mbedtls_ctr_drbg_random,
3574 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003575 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003576 exponent );
3577 if( ret != 0 )
3578 {
3579 mbedtls_rsa_free( rsa );
3580 mbedtls_free( rsa );
3581 return( mbedtls_to_psa_error( ret ) );
3582 }
3583 slot->data.rsa = rsa;
3584 }
3585 else
3586#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3587
3588#if defined(MBEDTLS_ECP_C)
3589 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3590 {
3591 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3592 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3593 const mbedtls_ecp_curve_info *curve_info =
3594 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3595 mbedtls_ecp_keypair *ecp;
3596 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003597 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003598 return( PSA_ERROR_NOT_SUPPORTED );
3599 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3600 return( PSA_ERROR_NOT_SUPPORTED );
3601 if( curve_info->bit_size != bits )
3602 return( PSA_ERROR_INVALID_ARGUMENT );
3603 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3604 if( ecp == NULL )
3605 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3606 mbedtls_ecp_keypair_init( ecp );
3607 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3608 mbedtls_ctr_drbg_random,
3609 &global_data.ctr_drbg );
3610 if( ret != 0 )
3611 {
3612 mbedtls_ecp_keypair_free( ecp );
3613 mbedtls_free( ecp );
3614 return( mbedtls_to_psa_error( ret ) );
3615 }
3616 slot->data.ecp = ecp;
3617 }
3618 else
3619#endif /* MBEDTLS_ECP_C */
3620
3621 return( PSA_ERROR_NOT_SUPPORTED );
3622
3623 slot->type = type;
3624 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003625}
3626
3627
3628/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003629/* Module setup */
3630/****************************************************************/
3631
Gilles Peskinee59236f2018-01-27 23:32:46 +01003632void mbedtls_psa_crypto_free( void )
3633{
Jaeden Amero045bd502018-06-26 14:00:08 +01003634 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003635 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003636 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003637 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3638 mbedtls_entropy_free( &global_data.entropy );
3639 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3640}
3641
3642psa_status_t psa_crypto_init( void )
3643{
3644 int ret;
3645 const unsigned char drbg_seed[] = "PSA";
3646
3647 if( global_data.initialized != 0 )
3648 return( PSA_SUCCESS );
3649
3650 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3651 mbedtls_entropy_init( &global_data.entropy );
3652 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3653
3654 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3655 mbedtls_entropy_func,
3656 &global_data.entropy,
3657 drbg_seed, sizeof( drbg_seed ) - 1 );
3658 if( ret != 0 )
3659 goto exit;
3660
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003661 global_data.initialized = 1;
3662
Gilles Peskinee59236f2018-01-27 23:32:46 +01003663exit:
3664 if( ret != 0 )
3665 mbedtls_psa_crypto_free( );
3666 return( mbedtls_to_psa_error( ret ) );
3667}
3668
3669#endif /* MBEDTLS_PSA_CRYPTO_C */