blob: f77df305160363f6ddbc68326f5e68700ff6b778 [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 Peskine46f1fd72018-06-28 19:31:31 +0200867 if( slot->data.raw.bytes != 0 )
868 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100869 *data_length = slot->data.raw.bytes;
870 return( PSA_SUCCESS );
871 }
872 else
Moran Peker17e36e12018-05-02 12:55:20 +0300873 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100874#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200875 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300876 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100877 {
Moran Pekera998bc62018-04-16 18:16:20 +0300878 mbedtls_pk_context pk;
879 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200880 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300881 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100882#if defined(MBEDTLS_RSA_C)
883 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300884 pk.pk_info = &mbedtls_rsa_info;
885 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100886#else
887 return( PSA_ERROR_NOT_SUPPORTED );
888#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300889 }
890 else
891 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100892#if defined(MBEDTLS_ECP_C)
893 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300894 pk.pk_info = &mbedtls_eckey_info;
895 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100896#else
897 return( PSA_ERROR_NOT_SUPPORTED );
898#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300899 }
Moran Pekerd7326592018-05-29 16:56:39 +0300900 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300901 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300902 else
903 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300904 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200905 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200906 /* If data_size is 0 then data may be NULL and then the
907 * call to memset would have undefined behavior. */
908 if( data_size != 0 )
909 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300910 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200911 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200912 /* The mbedtls_pk_xxx functions write to the end of the buffer.
913 * Move the data to the beginning and erase remaining data
914 * at the original location. */
915 if( 2 * (size_t) ret <= data_size )
916 {
917 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200918 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200919 }
920 else if( (size_t) ret < data_size )
921 {
922 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200923 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200924 }
Moran Pekera998bc62018-04-16 18:16:20 +0300925 *data_length = ret;
926 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100927 }
928 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100929#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300930 {
931 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200932 it is valid for a special-purpose implementation to omit
933 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300934 return( PSA_ERROR_NOT_SUPPORTED );
935 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100936 }
937}
938
Gilles Peskine2d277862018-06-18 15:41:12 +0200939psa_status_t psa_export_key( psa_key_slot_t key,
940 uint8_t *data,
941 size_t data_size,
942 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300943{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200944 return( psa_internal_export_key( key, data, data_size,
945 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947
Gilles Peskine2d277862018-06-18 15:41:12 +0200948psa_status_t psa_export_public_key( psa_key_slot_t key,
949 uint8_t *data,
950 size_t data_size,
951 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300952{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200953 return( psa_internal_export_key( key, data, data_size,
954 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300955}
956
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200957
958
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100959/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100960/* Message digests */
961/****************************************************************/
962
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100963static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100964{
965 switch( alg )
966 {
967#if defined(MBEDTLS_MD2_C)
968 case PSA_ALG_MD2:
969 return( &mbedtls_md2_info );
970#endif
971#if defined(MBEDTLS_MD4_C)
972 case PSA_ALG_MD4:
973 return( &mbedtls_md4_info );
974#endif
975#if defined(MBEDTLS_MD5_C)
976 case PSA_ALG_MD5:
977 return( &mbedtls_md5_info );
978#endif
979#if defined(MBEDTLS_RIPEMD160_C)
980 case PSA_ALG_RIPEMD160:
981 return( &mbedtls_ripemd160_info );
982#endif
983#if defined(MBEDTLS_SHA1_C)
984 case PSA_ALG_SHA_1:
985 return( &mbedtls_sha1_info );
986#endif
987#if defined(MBEDTLS_SHA256_C)
988 case PSA_ALG_SHA_224:
989 return( &mbedtls_sha224_info );
990 case PSA_ALG_SHA_256:
991 return( &mbedtls_sha256_info );
992#endif
993#if defined(MBEDTLS_SHA512_C)
994 case PSA_ALG_SHA_384:
995 return( &mbedtls_sha384_info );
996 case PSA_ALG_SHA_512:
997 return( &mbedtls_sha512_info );
998#endif
999 default:
1000 return( NULL );
1001 }
1002}
1003
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001004psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1005{
1006 switch( operation->alg )
1007 {
Gilles Peskine81736312018-06-26 15:04:31 +02001008 case 0:
1009 /* The object has (apparently) been initialized but it is not
1010 * in use. It's ok to call abort on such an object, and there's
1011 * nothing to do. */
1012 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001013#if defined(MBEDTLS_MD2_C)
1014 case PSA_ALG_MD2:
1015 mbedtls_md2_free( &operation->ctx.md2 );
1016 break;
1017#endif
1018#if defined(MBEDTLS_MD4_C)
1019 case PSA_ALG_MD4:
1020 mbedtls_md4_free( &operation->ctx.md4 );
1021 break;
1022#endif
1023#if defined(MBEDTLS_MD5_C)
1024 case PSA_ALG_MD5:
1025 mbedtls_md5_free( &operation->ctx.md5 );
1026 break;
1027#endif
1028#if defined(MBEDTLS_RIPEMD160_C)
1029 case PSA_ALG_RIPEMD160:
1030 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1031 break;
1032#endif
1033#if defined(MBEDTLS_SHA1_C)
1034 case PSA_ALG_SHA_1:
1035 mbedtls_sha1_free( &operation->ctx.sha1 );
1036 break;
1037#endif
1038#if defined(MBEDTLS_SHA256_C)
1039 case PSA_ALG_SHA_224:
1040 case PSA_ALG_SHA_256:
1041 mbedtls_sha256_free( &operation->ctx.sha256 );
1042 break;
1043#endif
1044#if defined(MBEDTLS_SHA512_C)
1045 case PSA_ALG_SHA_384:
1046 case PSA_ALG_SHA_512:
1047 mbedtls_sha512_free( &operation->ctx.sha512 );
1048 break;
1049#endif
1050 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001051 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001052 }
1053 operation->alg = 0;
1054 return( PSA_SUCCESS );
1055}
1056
Gilles Peskineda8191d2018-07-08 19:46:38 +02001057psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001058 psa_algorithm_t alg )
1059{
1060 int ret;
1061 operation->alg = 0;
1062 switch( alg )
1063 {
1064#if defined(MBEDTLS_MD2_C)
1065 case PSA_ALG_MD2:
1066 mbedtls_md2_init( &operation->ctx.md2 );
1067 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1068 break;
1069#endif
1070#if defined(MBEDTLS_MD4_C)
1071 case PSA_ALG_MD4:
1072 mbedtls_md4_init( &operation->ctx.md4 );
1073 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1074 break;
1075#endif
1076#if defined(MBEDTLS_MD5_C)
1077 case PSA_ALG_MD5:
1078 mbedtls_md5_init( &operation->ctx.md5 );
1079 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1080 break;
1081#endif
1082#if defined(MBEDTLS_RIPEMD160_C)
1083 case PSA_ALG_RIPEMD160:
1084 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1085 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1086 break;
1087#endif
1088#if defined(MBEDTLS_SHA1_C)
1089 case PSA_ALG_SHA_1:
1090 mbedtls_sha1_init( &operation->ctx.sha1 );
1091 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1092 break;
1093#endif
1094#if defined(MBEDTLS_SHA256_C)
1095 case PSA_ALG_SHA_224:
1096 mbedtls_sha256_init( &operation->ctx.sha256 );
1097 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1098 break;
1099 case PSA_ALG_SHA_256:
1100 mbedtls_sha256_init( &operation->ctx.sha256 );
1101 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1102 break;
1103#endif
1104#if defined(MBEDTLS_SHA512_C)
1105 case PSA_ALG_SHA_384:
1106 mbedtls_sha512_init( &operation->ctx.sha512 );
1107 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1108 break;
1109 case PSA_ALG_SHA_512:
1110 mbedtls_sha512_init( &operation->ctx.sha512 );
1111 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1112 break;
1113#endif
1114 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001115 return( PSA_ALG_IS_HASH( alg ) ?
1116 PSA_ERROR_NOT_SUPPORTED :
1117 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001118 }
1119 if( ret == 0 )
1120 operation->alg = alg;
1121 else
1122 psa_hash_abort( operation );
1123 return( mbedtls_to_psa_error( ret ) );
1124}
1125
1126psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1127 const uint8_t *input,
1128 size_t input_length )
1129{
1130 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001131
1132 /* Don't require hash implementations to behave correctly on a
1133 * zero-length input, which may have an invalid pointer. */
1134 if( input_length == 0 )
1135 return( PSA_SUCCESS );
1136
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001137 switch( operation->alg )
1138 {
1139#if defined(MBEDTLS_MD2_C)
1140 case PSA_ALG_MD2:
1141 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1142 input, input_length );
1143 break;
1144#endif
1145#if defined(MBEDTLS_MD4_C)
1146 case PSA_ALG_MD4:
1147 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1148 input, input_length );
1149 break;
1150#endif
1151#if defined(MBEDTLS_MD5_C)
1152 case PSA_ALG_MD5:
1153 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1154 input, input_length );
1155 break;
1156#endif
1157#if defined(MBEDTLS_RIPEMD160_C)
1158 case PSA_ALG_RIPEMD160:
1159 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1160 input, input_length );
1161 break;
1162#endif
1163#if defined(MBEDTLS_SHA1_C)
1164 case PSA_ALG_SHA_1:
1165 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1166 input, input_length );
1167 break;
1168#endif
1169#if defined(MBEDTLS_SHA256_C)
1170 case PSA_ALG_SHA_224:
1171 case PSA_ALG_SHA_256:
1172 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1173 input, input_length );
1174 break;
1175#endif
1176#if defined(MBEDTLS_SHA512_C)
1177 case PSA_ALG_SHA_384:
1178 case PSA_ALG_SHA_512:
1179 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1180 input, input_length );
1181 break;
1182#endif
1183 default:
1184 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1185 break;
1186 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001187
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001188 if( ret != 0 )
1189 psa_hash_abort( operation );
1190 return( mbedtls_to_psa_error( ret ) );
1191}
1192
1193psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1194 uint8_t *hash,
1195 size_t hash_size,
1196 size_t *hash_length )
1197{
itayzafrir40835d42018-08-02 13:14:17 +03001198 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001199 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001200 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001201
1202 /* Fill the output buffer with something that isn't a valid hash
1203 * (barring an attack on the hash and deliberately-crafted input),
1204 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001205 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001206 /* If hash_size is 0 then hash may be NULL and then the
1207 * call to memset would have undefined behavior. */
1208 if( hash_size != 0 )
1209 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001210
1211 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001212 {
1213 status = PSA_ERROR_BUFFER_TOO_SMALL;
1214 goto exit;
1215 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001216
1217 switch( operation->alg )
1218 {
1219#if defined(MBEDTLS_MD2_C)
1220 case PSA_ALG_MD2:
1221 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1222 break;
1223#endif
1224#if defined(MBEDTLS_MD4_C)
1225 case PSA_ALG_MD4:
1226 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1227 break;
1228#endif
1229#if defined(MBEDTLS_MD5_C)
1230 case PSA_ALG_MD5:
1231 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1232 break;
1233#endif
1234#if defined(MBEDTLS_RIPEMD160_C)
1235 case PSA_ALG_RIPEMD160:
1236 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1237 break;
1238#endif
1239#if defined(MBEDTLS_SHA1_C)
1240 case PSA_ALG_SHA_1:
1241 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1242 break;
1243#endif
1244#if defined(MBEDTLS_SHA256_C)
1245 case PSA_ALG_SHA_224:
1246 case PSA_ALG_SHA_256:
1247 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1248 break;
1249#endif
1250#if defined(MBEDTLS_SHA512_C)
1251 case PSA_ALG_SHA_384:
1252 case PSA_ALG_SHA_512:
1253 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1254 break;
1255#endif
1256 default:
1257 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1258 break;
1259 }
itayzafrir40835d42018-08-02 13:14:17 +03001260 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001261
itayzafrir40835d42018-08-02 13:14:17 +03001262exit:
1263 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001264 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001265 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001266 return( psa_hash_abort( operation ) );
1267 }
1268 else
1269 {
1270 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001271 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001272 }
1273}
1274
Gilles Peskine2d277862018-06-18 15:41:12 +02001275psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1276 const uint8_t *hash,
1277 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001278{
1279 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1280 size_t actual_hash_length;
1281 psa_status_t status = psa_hash_finish( operation,
1282 actual_hash, sizeof( actual_hash ),
1283 &actual_hash_length );
1284 if( status != PSA_SUCCESS )
1285 return( status );
1286 if( actual_hash_length != hash_length )
1287 return( PSA_ERROR_INVALID_SIGNATURE );
1288 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1289 return( PSA_ERROR_INVALID_SIGNATURE );
1290 return( PSA_SUCCESS );
1291}
1292
1293
1294
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001295/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001296/* MAC */
1297/****************************************************************/
1298
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001299static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001300 psa_algorithm_t alg,
1301 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001302 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001303 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001304{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001305 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001306 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001307
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001308 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001309 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001310
Gilles Peskine8c9def32018-02-08 10:02:12 +01001311 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1312 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001313 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001314 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001315 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001316 mode = MBEDTLS_MODE_STREAM;
1317 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001318 case PSA_ALG_CTR:
1319 mode = MBEDTLS_MODE_CTR;
1320 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001321 case PSA_ALG_CFB:
1322 mode = MBEDTLS_MODE_CFB;
1323 break;
1324 case PSA_ALG_OFB:
1325 mode = MBEDTLS_MODE_OFB;
1326 break;
1327 case PSA_ALG_CBC_NO_PADDING:
1328 mode = MBEDTLS_MODE_CBC;
1329 break;
1330 case PSA_ALG_CBC_PKCS7:
1331 mode = MBEDTLS_MODE_CBC;
1332 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001333 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001334 mode = MBEDTLS_MODE_CCM;
1335 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001336 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001337 mode = MBEDTLS_MODE_GCM;
1338 break;
1339 default:
1340 return( NULL );
1341 }
1342 }
1343 else if( alg == PSA_ALG_CMAC )
1344 mode = MBEDTLS_MODE_ECB;
1345 else if( alg == PSA_ALG_GMAC )
1346 mode = MBEDTLS_MODE_GCM;
1347 else
1348 return( NULL );
1349
1350 switch( key_type )
1351 {
1352 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001353 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001354 break;
1355 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001356 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1357 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001358 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001359 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001360 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001361 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001362 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1363 * but two-key Triple-DES is functionally three-key Triple-DES
1364 * with K1=K3, so that's how we present it to mbedtls. */
1365 if( key_bits == 128 )
1366 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001367 break;
1368 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001369 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001370 break;
1371 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001372 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001373 break;
1374 default:
1375 return( NULL );
1376 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001377 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001378 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001379
Jaeden Amero23bbb752018-06-26 14:16:54 +01001380 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1381 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001382}
1383
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001384static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001385{
Gilles Peskine2d277862018-06-18 15:41:12 +02001386 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001387 {
1388 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001389 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001390 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001391 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001392 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001393 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001394 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001395 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001396 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001397 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001398 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001399 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001400 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001401 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001402 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001403 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001404 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001405 return( 128 );
1406 default:
1407 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001408 }
1409}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001410
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001411/* Initialize the MAC operation structure. Once this function has been
1412 * called, psa_mac_abort can run and will do the right thing. */
1413static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1414 psa_algorithm_t alg )
1415{
1416 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1417
1418 operation->alg = alg;
1419 operation->key_set = 0;
1420 operation->iv_set = 0;
1421 operation->iv_required = 0;
1422 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001423 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001424
1425#if defined(MBEDTLS_CMAC_C)
1426 if( alg == PSA_ALG_CMAC )
1427 {
1428 operation->iv_required = 0;
1429 mbedtls_cipher_init( &operation->ctx.cmac );
1430 status = PSA_SUCCESS;
1431 }
1432 else
1433#endif /* MBEDTLS_CMAC_C */
1434#if defined(MBEDTLS_MD_C)
1435 if( PSA_ALG_IS_HMAC( operation->alg ) )
1436 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001437 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1438 operation->ctx.hmac.hash_ctx.alg = 0;
1439 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001440 }
1441 else
1442#endif /* MBEDTLS_MD_C */
1443 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001444 if( ! PSA_ALG_IS_MAC( alg ) )
1445 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001446 }
1447
1448 if( status != PSA_SUCCESS )
1449 memset( operation, 0, sizeof( *operation ) );
1450 return( status );
1451}
1452
Gilles Peskine01126fa2018-07-12 17:04:55 +02001453#if defined(MBEDTLS_MD_C)
1454static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1455{
1456 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1457 return( psa_hash_abort( &hmac->hash_ctx ) );
1458}
1459#endif /* MBEDTLS_MD_C */
1460
Gilles Peskine8c9def32018-02-08 10:02:12 +01001461psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1462{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001463 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001464 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001465 /* The object has (apparently) been initialized but it is not
1466 * in use. It's ok to call abort on such an object, and there's
1467 * nothing to do. */
1468 return( PSA_SUCCESS );
1469 }
1470 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001471#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001472 if( operation->alg == PSA_ALG_CMAC )
1473 {
1474 mbedtls_cipher_free( &operation->ctx.cmac );
1475 }
1476 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001477#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001478#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001479 if( PSA_ALG_IS_HMAC( operation->alg ) )
1480 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001481 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001482 }
1483 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001484#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001485 {
1486 /* Sanity check (shouldn't happen: operation->alg should
1487 * always have been initialized to a valid value). */
1488 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001489 }
Moran Peker41deec42018-04-04 15:43:05 +03001490
Gilles Peskine8c9def32018-02-08 10:02:12 +01001491 operation->alg = 0;
1492 operation->key_set = 0;
1493 operation->iv_set = 0;
1494 operation->iv_required = 0;
1495 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001496 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001497
Gilles Peskine8c9def32018-02-08 10:02:12 +01001498 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001499
1500bad_state:
1501 /* If abort is called on an uninitialized object, we can't trust
1502 * anything. Wipe the object in case it contains confidential data.
1503 * This may result in a memory leak if a pointer gets overwritten,
1504 * but it's too late to do anything about this. */
1505 memset( operation, 0, sizeof( *operation ) );
1506 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001507}
1508
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001509#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001510static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001511 size_t key_bits,
1512 key_slot_t *slot,
1513 const mbedtls_cipher_info_t *cipher_info )
1514{
1515 int ret;
1516
1517 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001518
1519 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1520 if( ret != 0 )
1521 return( ret );
1522
1523 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1524 slot->data.raw.data,
1525 key_bits );
1526 return( ret );
1527}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001528#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001529
Gilles Peskine248051a2018-06-20 16:09:38 +02001530#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001531static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1532 const uint8_t *key,
1533 size_t key_length,
1534 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001535{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001536 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001537 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001538 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001539 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001540 psa_status_t status;
1541
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001542 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1543 * overflow below. This should never trigger if the hash algorithm
1544 * is implemented correctly. */
1545 /* The size checks against the ipad and opad buffers cannot be written
1546 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1547 * because that triggers -Wlogical-op on GCC 7.3. */
1548 if( block_size > sizeof( ipad ) )
1549 return( PSA_ERROR_NOT_SUPPORTED );
1550 if( block_size > sizeof( hmac->opad ) )
1551 return( PSA_ERROR_NOT_SUPPORTED );
1552 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001553 return( PSA_ERROR_NOT_SUPPORTED );
1554
Gilles Peskined223b522018-06-11 18:12:58 +02001555 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001556 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001557 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001558 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001559 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001560 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001561 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001562 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001563 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001564 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001565 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001566 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001567 }
Gilles Peskine96889972018-07-12 17:07:03 +02001568 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1569 * but it is permitted. It is common when HMAC is used in HKDF, for
1570 * example. Don't call `memcpy` in the 0-length because `key` could be
1571 * an invalid pointer which would make the behavior undefined. */
1572 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001573 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001574
Gilles Peskined223b522018-06-11 18:12:58 +02001575 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1576 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001577 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001578 ipad[i] ^= 0x36;
1579 memset( ipad + key_length, 0x36, block_size - key_length );
1580
1581 /* Copy the key material from ipad to opad, flipping the requisite bits,
1582 * and filling the rest of opad with the requisite constant. */
1583 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001584 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1585 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001586
Gilles Peskine01126fa2018-07-12 17:04:55 +02001587 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001588 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001589 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001590
Gilles Peskine01126fa2018-07-12 17:04:55 +02001591 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001592
1593cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001594 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001595
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001596 return( status );
1597}
Gilles Peskine248051a2018-06-20 16:09:38 +02001598#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001599
Gilles Peskine89167cb2018-07-08 20:12:23 +02001600static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1601 psa_key_slot_t key,
1602 psa_algorithm_t alg,
1603 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001604{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001605 psa_status_t status;
1606 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001607 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001608 psa_key_usage_t usage =
1609 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001610 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001611 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001612
Gilles Peskined911eb72018-08-14 15:18:45 +02001613 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001614 if( status != PSA_SUCCESS )
1615 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001616 if( is_sign )
1617 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001618
Gilles Peskine89167cb2018-07-08 20:12:23 +02001619 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001620 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001621 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001622 key_bits = psa_get_key_bits( slot );
1623
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001625 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001626 {
1627 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001628 mbedtls_cipher_info_from_psa( full_length_alg,
1629 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001630 int ret;
1631 if( cipher_info == NULL )
1632 {
1633 status = PSA_ERROR_NOT_SUPPORTED;
1634 goto exit;
1635 }
1636 operation->mac_size = cipher_info->block_size;
1637 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1638 status = mbedtls_to_psa_error( ret );
1639 }
1640 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001641#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001642#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001643 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001644 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001645 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001646 if( hash_alg == 0 )
1647 {
1648 status = PSA_ERROR_NOT_SUPPORTED;
1649 goto exit;
1650 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001651
1652 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1653 /* Sanity check. This shouldn't fail on a valid configuration. */
1654 if( operation->mac_size == 0 ||
1655 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1656 {
1657 status = PSA_ERROR_NOT_SUPPORTED;
1658 goto exit;
1659 }
1660
Gilles Peskine01126fa2018-07-12 17:04:55 +02001661 if( slot->type != PSA_KEY_TYPE_HMAC )
1662 {
1663 status = PSA_ERROR_INVALID_ARGUMENT;
1664 goto exit;
1665 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001666
Gilles Peskine01126fa2018-07-12 17:04:55 +02001667 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1668 slot->data.raw.data,
1669 slot->data.raw.bytes,
1670 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001671 }
1672 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001673#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001674 {
1675 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001676 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001677
Gilles Peskined911eb72018-08-14 15:18:45 +02001678 if( truncated == 0 )
1679 {
1680 /* The "normal" case: untruncated algorithm. Nothing to do. */
1681 }
1682 else if( truncated < 4 )
1683 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001684 /* A very short MAC is too short for security since it can be
1685 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1686 * so we make this our minimum, even though 32 bits is still
1687 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001688 status = PSA_ERROR_NOT_SUPPORTED;
1689 }
1690 else if( truncated > operation->mac_size )
1691 {
1692 /* It's impossible to "truncate" to a larger length. */
1693 status = PSA_ERROR_INVALID_ARGUMENT;
1694 }
1695 else
1696 operation->mac_size = truncated;
1697
Gilles Peskinefbfac682018-07-08 20:51:54 +02001698exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001699 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001700 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001701 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001702 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001703 else
1704 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001705 operation->key_set = 1;
1706 }
1707 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001708}
1709
Gilles Peskine89167cb2018-07-08 20:12:23 +02001710psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1711 psa_key_slot_t key,
1712 psa_algorithm_t alg )
1713{
1714 return( psa_mac_setup( operation, key, alg, 1 ) );
1715}
1716
1717psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1718 psa_key_slot_t key,
1719 psa_algorithm_t alg )
1720{
1721 return( psa_mac_setup( operation, key, alg, 0 ) );
1722}
1723
Gilles Peskine8c9def32018-02-08 10:02:12 +01001724psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1725 const uint8_t *input,
1726 size_t input_length )
1727{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001728 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001729 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001730 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001731 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001732 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001733 operation->has_input = 1;
1734
Gilles Peskine8c9def32018-02-08 10:02:12 +01001735#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001736 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001737 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001738 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1739 input, input_length );
1740 status = mbedtls_to_psa_error( ret );
1741 }
1742 else
1743#endif /* MBEDTLS_CMAC_C */
1744#if defined(MBEDTLS_MD_C)
1745 if( PSA_ALG_IS_HMAC( operation->alg ) )
1746 {
1747 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1748 input_length );
1749 }
1750 else
1751#endif /* MBEDTLS_MD_C */
1752 {
1753 /* This shouldn't happen if `operation` was initialized by
1754 * a setup function. */
1755 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001756 }
1757
Gilles Peskinefbfac682018-07-08 20:51:54 +02001758cleanup:
1759 if( status != PSA_SUCCESS )
1760 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001761 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001762}
1763
Gilles Peskine01126fa2018-07-12 17:04:55 +02001764#if defined(MBEDTLS_MD_C)
1765static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1766 uint8_t *mac,
1767 size_t mac_size )
1768{
1769 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1770 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1771 size_t hash_size = 0;
1772 size_t block_size = psa_get_hash_block_size( hash_alg );
1773 psa_status_t status;
1774
Gilles Peskine01126fa2018-07-12 17:04:55 +02001775 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1776 if( status != PSA_SUCCESS )
1777 return( status );
1778 /* From here on, tmp needs to be wiped. */
1779
1780 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1781 if( status != PSA_SUCCESS )
1782 goto exit;
1783
1784 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1785 if( status != PSA_SUCCESS )
1786 goto exit;
1787
1788 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1789 if( status != PSA_SUCCESS )
1790 goto exit;
1791
Gilles Peskined911eb72018-08-14 15:18:45 +02001792 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1793 if( status != PSA_SUCCESS )
1794 goto exit;
1795
1796 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001797
1798exit:
1799 mbedtls_zeroize( tmp, hash_size );
1800 return( status );
1801}
1802#endif /* MBEDTLS_MD_C */
1803
mohammad16036df908f2018-04-02 08:34:15 -07001804static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001805 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001806 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001807{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001808 if( ! operation->key_set )
1809 return( PSA_ERROR_BAD_STATE );
1810 if( operation->iv_required && ! operation->iv_set )
1811 return( PSA_ERROR_BAD_STATE );
1812
Gilles Peskine8c9def32018-02-08 10:02:12 +01001813 if( mac_size < operation->mac_size )
1814 return( PSA_ERROR_BUFFER_TOO_SMALL );
1815
Gilles Peskine8c9def32018-02-08 10:02:12 +01001816#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001817 if( operation->alg == PSA_ALG_CMAC )
1818 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001819 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1820 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1821 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02001822 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02001823 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001824 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001825 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001826 else
1827#endif /* MBEDTLS_CMAC_C */
1828#if defined(MBEDTLS_MD_C)
1829 if( PSA_ALG_IS_HMAC( operation->alg ) )
1830 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001831 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001832 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001833 }
1834 else
1835#endif /* MBEDTLS_MD_C */
1836 {
1837 /* This shouldn't happen if `operation` was initialized by
1838 * a setup function. */
1839 return( PSA_ERROR_BAD_STATE );
1840 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001841}
1842
Gilles Peskineacd4be32018-07-08 19:56:25 +02001843psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1844 uint8_t *mac,
1845 size_t mac_size,
1846 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001847{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001848 psa_status_t status;
1849
1850 /* Fill the output buffer with something that isn't a valid mac
1851 * (barring an attack on the mac and deliberately-crafted input),
1852 * in case the caller doesn't check the return status properly. */
1853 *mac_length = mac_size;
1854 /* If mac_size is 0 then mac may be NULL and then the
1855 * call to memset would have undefined behavior. */
1856 if( mac_size != 0 )
1857 memset( mac, '!', mac_size );
1858
Gilles Peskine89167cb2018-07-08 20:12:23 +02001859 if( ! operation->is_sign )
1860 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001861 status = PSA_ERROR_BAD_STATE;
1862 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001863 }
mohammad16036df908f2018-04-02 08:34:15 -07001864
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001865 status = psa_mac_finish_internal( operation, mac, mac_size );
1866
1867cleanup:
1868 if( status == PSA_SUCCESS )
1869 {
1870 status = psa_mac_abort( operation );
1871 if( status == PSA_SUCCESS )
1872 *mac_length = operation->mac_size;
1873 else
1874 memset( mac, '!', mac_size );
1875 }
1876 else
1877 psa_mac_abort( operation );
1878 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001879}
1880
Gilles Peskineacd4be32018-07-08 19:56:25 +02001881psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1882 const uint8_t *mac,
1883 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001884{
Gilles Peskine828ed142018-06-18 23:25:51 +02001885 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001886 psa_status_t status;
1887
Gilles Peskine89167cb2018-07-08 20:12:23 +02001888 if( operation->is_sign )
1889 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001890 status = PSA_ERROR_BAD_STATE;
1891 goto cleanup;
1892 }
1893 if( operation->mac_size != mac_length )
1894 {
1895 status = PSA_ERROR_INVALID_SIGNATURE;
1896 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001897 }
mohammad16036df908f2018-04-02 08:34:15 -07001898
1899 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001900 actual_mac, sizeof( actual_mac ) );
1901
1902 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1903 status = PSA_ERROR_INVALID_SIGNATURE;
1904
1905cleanup:
1906 if( status == PSA_SUCCESS )
1907 status = psa_mac_abort( operation );
1908 else
1909 psa_mac_abort( operation );
1910
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02001911 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02001912
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001913 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001914}
1915
1916
Gilles Peskine20035e32018-02-03 22:44:14 +01001917
Gilles Peskine20035e32018-02-03 22:44:14 +01001918/****************************************************************/
1919/* Asymmetric cryptography */
1920/****************************************************************/
1921
Gilles Peskine2b450e32018-06-27 15:42:46 +02001922#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001923/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001924 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001925static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1926 size_t hash_length,
1927 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001928{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001929 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001930 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001931 *md_alg = mbedtls_md_get_type( md_info );
1932
1933 /* The Mbed TLS RSA module uses an unsigned int for hash length
1934 * parameters. Validate that it fits so that we don't risk an
1935 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001936#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001937 if( hash_length > UINT_MAX )
1938 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001939#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001940
1941#if defined(MBEDTLS_PKCS1_V15)
1942 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1943 * must be correct. */
1944 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1945 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001946 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001947 if( md_info == NULL )
1948 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001949 if( mbedtls_md_get_size( md_info ) != hash_length )
1950 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001951 }
1952#endif /* MBEDTLS_PKCS1_V15 */
1953
1954#if defined(MBEDTLS_PKCS1_V21)
1955 /* PSS requires a hash internally. */
1956 if( PSA_ALG_IS_RSA_PSS( alg ) )
1957 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001958 if( md_info == NULL )
1959 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001960 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001961#endif /* MBEDTLS_PKCS1_V21 */
1962
Gilles Peskine61b91d42018-06-08 16:09:36 +02001963 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001964}
1965
Gilles Peskine2b450e32018-06-27 15:42:46 +02001966static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1967 psa_algorithm_t alg,
1968 const uint8_t *hash,
1969 size_t hash_length,
1970 uint8_t *signature,
1971 size_t signature_size,
1972 size_t *signature_length )
1973{
1974 psa_status_t status;
1975 int ret;
1976 mbedtls_md_type_t md_alg;
1977
1978 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1979 if( status != PSA_SUCCESS )
1980 return( status );
1981
Gilles Peskine630a18a2018-06-29 17:49:35 +02001982 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001983 return( PSA_ERROR_BUFFER_TOO_SMALL );
1984
1985#if defined(MBEDTLS_PKCS1_V15)
1986 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1987 {
1988 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1989 MBEDTLS_MD_NONE );
1990 ret = mbedtls_rsa_pkcs1_sign( rsa,
1991 mbedtls_ctr_drbg_random,
1992 &global_data.ctr_drbg,
1993 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001994 md_alg,
1995 (unsigned int) hash_length,
1996 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001997 signature );
1998 }
1999 else
2000#endif /* MBEDTLS_PKCS1_V15 */
2001#if defined(MBEDTLS_PKCS1_V21)
2002 if( PSA_ALG_IS_RSA_PSS( alg ) )
2003 {
2004 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2005 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2006 mbedtls_ctr_drbg_random,
2007 &global_data.ctr_drbg,
2008 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002009 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002010 (unsigned int) hash_length,
2011 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002012 signature );
2013 }
2014 else
2015#endif /* MBEDTLS_PKCS1_V21 */
2016 {
2017 return( PSA_ERROR_INVALID_ARGUMENT );
2018 }
2019
2020 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002021 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002022 return( mbedtls_to_psa_error( ret ) );
2023}
2024
2025static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2026 psa_algorithm_t alg,
2027 const uint8_t *hash,
2028 size_t hash_length,
2029 const uint8_t *signature,
2030 size_t signature_length )
2031{
2032 psa_status_t status;
2033 int ret;
2034 mbedtls_md_type_t md_alg;
2035
2036 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2037 if( status != PSA_SUCCESS )
2038 return( status );
2039
Gilles Peskine630a18a2018-06-29 17:49:35 +02002040 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002041 return( PSA_ERROR_BUFFER_TOO_SMALL );
2042
2043#if defined(MBEDTLS_PKCS1_V15)
2044 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2045 {
2046 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2047 MBEDTLS_MD_NONE );
2048 ret = mbedtls_rsa_pkcs1_verify( rsa,
2049 mbedtls_ctr_drbg_random,
2050 &global_data.ctr_drbg,
2051 MBEDTLS_RSA_PUBLIC,
2052 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002053 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002054 hash,
2055 signature );
2056 }
2057 else
2058#endif /* MBEDTLS_PKCS1_V15 */
2059#if defined(MBEDTLS_PKCS1_V21)
2060 if( PSA_ALG_IS_RSA_PSS( alg ) )
2061 {
2062 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2063 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2064 mbedtls_ctr_drbg_random,
2065 &global_data.ctr_drbg,
2066 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002067 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002068 (unsigned int) hash_length,
2069 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002070 signature );
2071 }
2072 else
2073#endif /* MBEDTLS_PKCS1_V21 */
2074 {
2075 return( PSA_ERROR_INVALID_ARGUMENT );
2076 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002077
2078 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2079 * the rest of the signature is invalid". This has little use in
2080 * practice and PSA doesn't report this distinction. */
2081 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2082 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002083 return( mbedtls_to_psa_error( ret ) );
2084}
2085#endif /* MBEDTLS_RSA_C */
2086
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002087#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002088/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2089 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2090 * (even though these functions don't modify it). */
2091static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2092 psa_algorithm_t alg,
2093 const uint8_t *hash,
2094 size_t hash_length,
2095 uint8_t *signature,
2096 size_t signature_size,
2097 size_t *signature_length )
2098{
2099 int ret;
2100 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002101 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002102 mbedtls_mpi_init( &r );
2103 mbedtls_mpi_init( &s );
2104
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002105 if( signature_size < 2 * curve_bytes )
2106 {
2107 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2108 goto cleanup;
2109 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002110
2111 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2112 {
2113 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2114 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2115 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2116 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2117 hash, hash_length,
2118 md_alg ) );
2119 }
2120 else
2121 {
2122 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2123 hash, hash_length,
2124 mbedtls_ctr_drbg_random,
2125 &global_data.ctr_drbg ) );
2126 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002127
2128 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2129 signature,
2130 curve_bytes ) );
2131 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2132 signature + curve_bytes,
2133 curve_bytes ) );
2134
2135cleanup:
2136 mbedtls_mpi_free( &r );
2137 mbedtls_mpi_free( &s );
2138 if( ret == 0 )
2139 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002140 return( mbedtls_to_psa_error( ret ) );
2141}
2142
2143static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2144 const uint8_t *hash,
2145 size_t hash_length,
2146 const uint8_t *signature,
2147 size_t signature_length )
2148{
2149 int ret;
2150 mbedtls_mpi r, s;
2151 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2152 mbedtls_mpi_init( &r );
2153 mbedtls_mpi_init( &s );
2154
2155 if( signature_length != 2 * curve_bytes )
2156 return( PSA_ERROR_INVALID_SIGNATURE );
2157
2158 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2159 signature,
2160 curve_bytes ) );
2161 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2162 signature + curve_bytes,
2163 curve_bytes ) );
2164
2165 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2166 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002167
2168cleanup:
2169 mbedtls_mpi_free( &r );
2170 mbedtls_mpi_free( &s );
2171 return( mbedtls_to_psa_error( ret ) );
2172}
2173#endif /* MBEDTLS_ECDSA_C */
2174
Gilles Peskine61b91d42018-06-08 16:09:36 +02002175psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2176 psa_algorithm_t alg,
2177 const uint8_t *hash,
2178 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002179 uint8_t *signature,
2180 size_t signature_size,
2181 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002182{
2183 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002184 psa_status_t status;
2185
2186 *signature_length = signature_size;
2187
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002188 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2189 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002190 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002191 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002192 {
2193 status = PSA_ERROR_INVALID_ARGUMENT;
2194 goto exit;
2195 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002196
Gilles Peskine20035e32018-02-03 22:44:14 +01002197#if defined(MBEDTLS_RSA_C)
2198 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2199 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002200 status = psa_rsa_sign( slot->data.rsa,
2201 alg,
2202 hash, hash_length,
2203 signature, signature_size,
2204 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002205 }
2206 else
2207#endif /* defined(MBEDTLS_RSA_C) */
2208#if defined(MBEDTLS_ECP_C)
2209 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2210 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002211#if defined(MBEDTLS_ECDSA_C)
2212 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002213 status = psa_ecdsa_sign( slot->data.ecp,
2214 alg,
2215 hash, hash_length,
2216 signature, signature_size,
2217 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002218 else
2219#endif /* defined(MBEDTLS_ECDSA_C) */
2220 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002221 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002222 }
itayzafrir5c753392018-05-08 11:18:38 +03002223 }
2224 else
2225#endif /* defined(MBEDTLS_ECP_C) */
2226 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002227 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002228 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002229
2230exit:
2231 /* Fill the unused part of the output buffer (the whole buffer on error,
2232 * the trailing part on success) with something that isn't a valid mac
2233 * (barring an attack on the mac and deliberately-crafted input),
2234 * in case the caller doesn't check the return status properly. */
2235 if( status == PSA_SUCCESS )
2236 memset( signature + *signature_length, '!',
2237 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002238 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002239 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002240 /* If signature_size is 0 then we have nothing to do. We must not call
2241 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002242 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002243}
2244
2245psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2246 psa_algorithm_t alg,
2247 const uint8_t *hash,
2248 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002249 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002250 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002251{
2252 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002253 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002254
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002255 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2256 if( status != PSA_SUCCESS )
2257 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002258
Gilles Peskine61b91d42018-06-08 16:09:36 +02002259#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002260 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002261 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002262 return( psa_rsa_verify( slot->data.rsa,
2263 alg,
2264 hash, hash_length,
2265 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002266 }
2267 else
2268#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002269#if defined(MBEDTLS_ECP_C)
2270 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2271 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002272#if defined(MBEDTLS_ECDSA_C)
2273 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002274 return( psa_ecdsa_verify( slot->data.ecp,
2275 hash, hash_length,
2276 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002277 else
2278#endif /* defined(MBEDTLS_ECDSA_C) */
2279 {
2280 return( PSA_ERROR_INVALID_ARGUMENT );
2281 }
itayzafrir5c753392018-05-08 11:18:38 +03002282 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002283 else
2284#endif /* defined(MBEDTLS_ECP_C) */
2285 {
2286 return( PSA_ERROR_NOT_SUPPORTED );
2287 }
2288}
2289
Gilles Peskine072ac562018-06-30 00:21:29 +02002290#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2291static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2292 mbedtls_rsa_context *rsa )
2293{
2294 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2295 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2296 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2297 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2298}
2299#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2300
Gilles Peskine61b91d42018-06-08 16:09:36 +02002301psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2302 psa_algorithm_t alg,
2303 const uint8_t *input,
2304 size_t input_length,
2305 const uint8_t *salt,
2306 size_t salt_length,
2307 uint8_t *output,
2308 size_t output_size,
2309 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002310{
2311 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002312 psa_status_t status;
2313
Darryl Green5cc689a2018-07-24 15:34:10 +01002314 (void) input;
2315 (void) input_length;
2316 (void) salt;
2317 (void) output;
2318 (void) output_size;
2319
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002320 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002321
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002322 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2323 return( PSA_ERROR_INVALID_ARGUMENT );
2324
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002325 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2326 if( status != PSA_SUCCESS )
2327 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002328 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2329 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002330 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002331
2332#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002333 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002334 {
2335 mbedtls_rsa_context *rsa = slot->data.rsa;
2336 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002337 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002338 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002339#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002340 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002341 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002342 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2343 mbedtls_ctr_drbg_random,
2344 &global_data.ctr_drbg,
2345 MBEDTLS_RSA_PUBLIC,
2346 input_length,
2347 input,
2348 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002349 }
2350 else
2351#endif /* MBEDTLS_PKCS1_V15 */
2352#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002353 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002354 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002355 psa_rsa_oaep_set_padding_mode( alg, rsa );
2356 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2357 mbedtls_ctr_drbg_random,
2358 &global_data.ctr_drbg,
2359 MBEDTLS_RSA_PUBLIC,
2360 salt, salt_length,
2361 input_length,
2362 input,
2363 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002364 }
2365 else
2366#endif /* MBEDTLS_PKCS1_V21 */
2367 {
2368 return( PSA_ERROR_INVALID_ARGUMENT );
2369 }
2370 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002371 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002372 return( mbedtls_to_psa_error( ret ) );
2373 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002374 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002375#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002376 {
2377 return( PSA_ERROR_NOT_SUPPORTED );
2378 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002379}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002380
Gilles Peskine61b91d42018-06-08 16:09:36 +02002381psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2382 psa_algorithm_t alg,
2383 const uint8_t *input,
2384 size_t input_length,
2385 const uint8_t *salt,
2386 size_t salt_length,
2387 uint8_t *output,
2388 size_t output_size,
2389 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002390{
2391 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002392 psa_status_t status;
2393
Darryl Green5cc689a2018-07-24 15:34:10 +01002394 (void) input;
2395 (void) input_length;
2396 (void) salt;
2397 (void) output;
2398 (void) output_size;
2399
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002400 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002401
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002402 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2403 return( PSA_ERROR_INVALID_ARGUMENT );
2404
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002405 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2406 if( status != PSA_SUCCESS )
2407 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002408 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2409 return( PSA_ERROR_INVALID_ARGUMENT );
2410
2411#if defined(MBEDTLS_RSA_C)
2412 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2413 {
2414 mbedtls_rsa_context *rsa = slot->data.rsa;
2415 int ret;
2416
Gilles Peskine630a18a2018-06-29 17:49:35 +02002417 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002418 return( PSA_ERROR_INVALID_ARGUMENT );
2419
2420#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002421 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002422 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002423 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2424 mbedtls_ctr_drbg_random,
2425 &global_data.ctr_drbg,
2426 MBEDTLS_RSA_PRIVATE,
2427 output_length,
2428 input,
2429 output,
2430 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002431 }
2432 else
2433#endif /* MBEDTLS_PKCS1_V15 */
2434#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002435 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002436 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002437 psa_rsa_oaep_set_padding_mode( alg, rsa );
2438 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2439 mbedtls_ctr_drbg_random,
2440 &global_data.ctr_drbg,
2441 MBEDTLS_RSA_PRIVATE,
2442 salt, salt_length,
2443 output_length,
2444 input,
2445 output,
2446 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002447 }
2448 else
2449#endif /* MBEDTLS_PKCS1_V21 */
2450 {
2451 return( PSA_ERROR_INVALID_ARGUMENT );
2452 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002453
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002454 return( mbedtls_to_psa_error( ret ) );
2455 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002456 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002457#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002458 {
2459 return( PSA_ERROR_NOT_SUPPORTED );
2460 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002461}
Gilles Peskine20035e32018-02-03 22:44:14 +01002462
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002463
2464
mohammad1603503973b2018-03-12 15:59:30 +02002465/****************************************************************/
2466/* Symmetric cryptography */
2467/****************************************************************/
2468
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002469/* Initialize the cipher operation structure. Once this function has been
2470 * called, psa_cipher_abort can run and will do the right thing. */
2471static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2472 psa_algorithm_t alg )
2473{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002474 if( ! PSA_ALG_IS_CIPHER( alg ) )
2475 {
2476 memset( operation, 0, sizeof( *operation ) );
2477 return( PSA_ERROR_INVALID_ARGUMENT );
2478 }
2479
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002480 operation->alg = alg;
2481 operation->key_set = 0;
2482 operation->iv_set = 0;
2483 operation->iv_required = 1;
2484 operation->iv_size = 0;
2485 operation->block_size = 0;
2486 mbedtls_cipher_init( &operation->ctx.cipher );
2487 return( PSA_SUCCESS );
2488}
2489
Gilles Peskinee553c652018-06-04 16:22:46 +02002490static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2491 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002492 psa_algorithm_t alg,
2493 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002494{
2495 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2496 psa_status_t status;
2497 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002498 size_t key_bits;
2499 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002500 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2501 PSA_KEY_USAGE_ENCRYPT :
2502 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002503
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002504 status = psa_cipher_init( operation, alg );
2505 if( status != PSA_SUCCESS )
2506 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002507
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002508 status = psa_get_key_from_slot( key, &slot, usage, alg);
2509 if( status != PSA_SUCCESS )
2510 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002511 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002512
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002513 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002514 if( cipher_info == NULL )
2515 return( PSA_ERROR_NOT_SUPPORTED );
2516
mohammad1603503973b2018-03-12 15:59:30 +02002517 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002518 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002519 {
2520 psa_cipher_abort( operation );
2521 return( mbedtls_to_psa_error( ret ) );
2522 }
2523
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002524#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002525 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002526 {
2527 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2528 unsigned char keys[24];
2529 memcpy( keys, slot->data.raw.data, 16 );
2530 memcpy( keys + 16, slot->data.raw.data, 8 );
2531 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2532 keys,
2533 192, cipher_operation );
2534 }
2535 else
2536#endif
2537 {
2538 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2539 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002540 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002541 }
Moran Peker41deec42018-04-04 15:43:05 +03002542 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002543 {
2544 psa_cipher_abort( operation );
2545 return( mbedtls_to_psa_error( ret ) );
2546 }
2547
mohammad16038481e742018-03-18 13:57:31 +02002548#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002549 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002550 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002551 case PSA_ALG_CBC_NO_PADDING:
2552 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2553 MBEDTLS_PADDING_NONE );
2554 break;
2555 case PSA_ALG_CBC_PKCS7:
2556 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2557 MBEDTLS_PADDING_PKCS7 );
2558 break;
2559 default:
2560 /* The algorithm doesn't involve padding. */
2561 ret = 0;
2562 break;
2563 }
2564 if( ret != 0 )
2565 {
2566 psa_cipher_abort( operation );
2567 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002568 }
2569#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2570
mohammad1603503973b2018-03-12 15:59:30 +02002571 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002572 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2573 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2574 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002575 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002576 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002577 }
mohammad1603503973b2018-03-12 15:59:30 +02002578
Moran Peker395db872018-05-31 14:07:14 +03002579 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002580}
2581
Gilles Peskinefe119512018-07-08 21:39:34 +02002582psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2583 psa_key_slot_t key,
2584 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002585{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002586 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002587}
2588
Gilles Peskinefe119512018-07-08 21:39:34 +02002589psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2590 psa_key_slot_t key,
2591 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002592{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002593 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002594}
2595
Gilles Peskinefe119512018-07-08 21:39:34 +02002596psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2597 unsigned char *iv,
2598 size_t iv_size,
2599 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002600{
itayzafrir534bd7c2018-08-02 13:56:32 +03002601 psa_status_t status;
2602 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002603 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002604 {
2605 status = PSA_ERROR_BAD_STATE;
2606 goto exit;
2607 }
Moran Peker41deec42018-04-04 15:43:05 +03002608 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002609 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002610 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002611 goto exit;
2612 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002613 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2614 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002615 if( ret != 0 )
2616 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002617 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002618 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002619 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002620
mohammad16038481e742018-03-18 13:57:31 +02002621 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002622 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002623
Moran Peker395db872018-05-31 14:07:14 +03002624exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002625 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002626 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002627 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002628}
2629
Gilles Peskinefe119512018-07-08 21:39:34 +02002630psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2631 const unsigned char *iv,
2632 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002633{
itayzafrir534bd7c2018-08-02 13:56:32 +03002634 psa_status_t status;
2635 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002636 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002637 {
2638 status = PSA_ERROR_BAD_STATE;
2639 goto exit;
2640 }
Moran Pekera28258c2018-05-29 16:25:04 +03002641 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002642 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002643 status = PSA_ERROR_INVALID_ARGUMENT;
2644 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002645 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002646 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2647 status = mbedtls_to_psa_error( ret );
2648exit:
2649 if( status == PSA_SUCCESS )
2650 operation->iv_set = 1;
2651 else
mohammad1603503973b2018-03-12 15:59:30 +02002652 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002653 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002654}
2655
Gilles Peskinee553c652018-06-04 16:22:46 +02002656psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2657 const uint8_t *input,
2658 size_t input_length,
2659 unsigned char *output,
2660 size_t output_size,
2661 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002662{
itayzafrir534bd7c2018-08-02 13:56:32 +03002663 psa_status_t status;
2664 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002665 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002666 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002667 {
2668 /* Take the unprocessed partial block left over from previous
2669 * update calls, if any, plus the input to this call. Remove
2670 * the last partial block, if any. You get the data that will be
2671 * output in this call. */
2672 expected_output_size =
2673 ( operation->ctx.cipher.unprocessed_len + input_length )
2674 / operation->block_size * operation->block_size;
2675 }
2676 else
2677 {
2678 expected_output_size = input_length;
2679 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002680
Gilles Peskine89d789c2018-06-04 17:17:16 +02002681 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002682 {
2683 status = PSA_ERROR_BUFFER_TOO_SMALL;
2684 goto exit;
2685 }
mohammad160382759612018-03-12 18:16:40 +02002686
mohammad1603503973b2018-03-12 15:59:30 +02002687 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002688 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002689 status = mbedtls_to_psa_error( ret );
2690exit:
2691 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002692 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002693 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002694}
2695
Gilles Peskinee553c652018-06-04 16:22:46 +02002696psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2697 uint8_t *output,
2698 size_t output_size,
2699 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002700{
Janos Follath315b51c2018-07-09 16:04:51 +01002701 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2702 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002703 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002704
mohammad1603503973b2018-03-12 15:59:30 +02002705 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002706 {
Janos Follath315b51c2018-07-09 16:04:51 +01002707 status = PSA_ERROR_BAD_STATE;
2708 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002709 }
2710 if( operation->iv_required && ! operation->iv_set )
2711 {
Janos Follath315b51c2018-07-09 16:04:51 +01002712 status = PSA_ERROR_BAD_STATE;
2713 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002714 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002715
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002716 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002717 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2718 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002719 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002720 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002721 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002722 }
2723
Janos Follath315b51c2018-07-09 16:04:51 +01002724 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2725 temp_output_buffer,
2726 output_length );
2727 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002728 {
Janos Follath315b51c2018-07-09 16:04:51 +01002729 status = mbedtls_to_psa_error( cipher_ret );
2730 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002731 }
Janos Follath315b51c2018-07-09 16:04:51 +01002732
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002733 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002734 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002735 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002736 memcpy( output, temp_output_buffer, *output_length );
2737 else
2738 {
Janos Follath315b51c2018-07-09 16:04:51 +01002739 status = PSA_ERROR_BUFFER_TOO_SMALL;
2740 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002741 }
mohammad1603503973b2018-03-12 15:59:30 +02002742
Janos Follath279ab8e2018-07-09 16:13:21 +01002743 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002744 status = psa_cipher_abort( operation );
2745
2746 return( status );
2747
2748error:
2749
2750 *output_length = 0;
2751
Janos Follath279ab8e2018-07-09 16:13:21 +01002752 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002753 (void) psa_cipher_abort( operation );
2754
2755 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002756}
2757
Gilles Peskinee553c652018-06-04 16:22:46 +02002758psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2759{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002760 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002761 {
2762 /* The object has (apparently) been initialized but it is not
2763 * in use. It's ok to call abort on such an object, and there's
2764 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002765 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002766 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002767
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002768 /* Sanity check (shouldn't happen: operation->alg should
2769 * always have been initialized to a valid value). */
2770 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2771 return( PSA_ERROR_BAD_STATE );
2772
mohammad1603503973b2018-03-12 15:59:30 +02002773 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002774
Moran Peker41deec42018-04-04 15:43:05 +03002775 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002776 operation->key_set = 0;
2777 operation->iv_set = 0;
2778 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002779 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002780 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002781
Moran Peker395db872018-05-31 14:07:14 +03002782 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002783}
2784
Gilles Peskinea0655c32018-04-30 17:06:50 +02002785
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002786
mohammad16038cc1cee2018-03-28 01:21:33 +03002787/****************************************************************/
2788/* Key Policy */
2789/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002790
mohammad160327010052018-07-03 13:16:15 +03002791#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002792void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002793{
Gilles Peskine803ce742018-06-18 16:07:14 +02002794 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002795}
2796
Gilles Peskine2d277862018-06-18 15:41:12 +02002797void psa_key_policy_set_usage( psa_key_policy_t *policy,
2798 psa_key_usage_t usage,
2799 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002800{
mohammad16034eed7572018-03-28 05:14:59 -07002801 policy->usage = usage;
2802 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002803}
2804
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002805psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002806{
mohammad16036df908f2018-04-02 08:34:15 -07002807 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002808}
2809
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002810psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002811{
mohammad16036df908f2018-04-02 08:34:15 -07002812 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002813}
mohammad160327010052018-07-03 13:16:15 +03002814#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002815
Gilles Peskine2d277862018-06-18 15:41:12 +02002816psa_status_t psa_set_key_policy( psa_key_slot_t key,
2817 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002818{
2819 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002820 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002821
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002822 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002823 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002824
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002825 status = psa_get_empty_key_slot( key, &slot );
2826 if( status != PSA_SUCCESS )
2827 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002828
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002829 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2830 PSA_KEY_USAGE_ENCRYPT |
2831 PSA_KEY_USAGE_DECRYPT |
2832 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002833 PSA_KEY_USAGE_VERIFY |
2834 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002835 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002836
mohammad16036df908f2018-04-02 08:34:15 -07002837 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002838
2839 return( PSA_SUCCESS );
2840}
2841
Gilles Peskine2d277862018-06-18 15:41:12 +02002842psa_status_t psa_get_key_policy( psa_key_slot_t key,
2843 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002844{
2845 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002846 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002847
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002848 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002849 return( PSA_ERROR_INVALID_ARGUMENT );
2850
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002851 status = psa_get_key_slot( key, &slot );
2852 if( status != PSA_SUCCESS )
2853 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002854
mohammad16036df908f2018-04-02 08:34:15 -07002855 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002856
2857 return( PSA_SUCCESS );
2858}
Gilles Peskine20035e32018-02-03 22:44:14 +01002859
Gilles Peskinea0655c32018-04-30 17:06:50 +02002860
2861
mohammad1603804cd712018-03-20 22:44:08 +02002862/****************************************************************/
2863/* Key Lifetime */
2864/****************************************************************/
2865
Gilles Peskine2d277862018-06-18 15:41:12 +02002866psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2867 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002868{
2869 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002870 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002871
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002872 status = psa_get_key_slot( key, &slot );
2873 if( status != PSA_SUCCESS )
2874 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002875
mohammad1603804cd712018-03-20 22:44:08 +02002876 *lifetime = slot->lifetime;
2877
2878 return( PSA_SUCCESS );
2879}
2880
Gilles Peskine2d277862018-06-18 15:41:12 +02002881psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002882 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002883{
2884 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002885 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002886
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002887 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2888 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002889 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2890 return( PSA_ERROR_INVALID_ARGUMENT );
2891
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002892 status = psa_get_empty_key_slot( key, &slot );
2893 if( status != PSA_SUCCESS )
2894 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002895
Moran Pekerd7326592018-05-29 16:56:39 +03002896 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002897 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002898
mohammad1603060ad8a2018-03-20 14:28:38 -07002899 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002900
2901 return( PSA_SUCCESS );
2902}
2903
Gilles Peskine20035e32018-02-03 22:44:14 +01002904
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002905
mohammad16035955c982018-04-26 00:53:03 +03002906/****************************************************************/
2907/* AEAD */
2908/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002909
Gilles Peskineedf9a652018-08-17 18:11:56 +02002910typedef struct
2911{
2912 key_slot_t *slot;
2913 const mbedtls_cipher_info_t *cipher_info;
2914 union
2915 {
2916#if defined(MBEDTLS_CCM_C)
2917 mbedtls_ccm_context ccm;
2918#endif /* MBEDTLS_CCM_C */
2919#if defined(MBEDTLS_GCM_C)
2920 mbedtls_gcm_context gcm;
2921#endif /* MBEDTLS_GCM_C */
2922 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002923 psa_algorithm_t core_alg;
2924 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002925 uint8_t tag_length;
2926} aead_operation_t;
2927
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002928static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002929{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002930 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002931 {
2932#if defined(MBEDTLS_CCM_C)
2933 case PSA_ALG_CCM:
2934 mbedtls_ccm_free( &operation->ctx.ccm );
2935 break;
2936#endif /* MBEDTLS_CCM_C */
2937#if defined(MBEDTLS_CCM_C)
2938 case PSA_ALG_GCM:
2939 mbedtls_gcm_free( &operation->ctx.gcm );
2940 break;
2941#endif /* MBEDTLS_GCM_C */
2942 }
2943}
2944
2945static psa_status_t psa_aead_setup( aead_operation_t *operation,
2946 psa_key_slot_t key,
2947 psa_key_usage_t usage,
2948 psa_algorithm_t alg )
2949{
2950 psa_status_t status;
2951 size_t key_bits;
2952 mbedtls_cipher_id_t cipher_id;
2953
2954 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
2955 if( status != PSA_SUCCESS )
2956 return( status );
2957
2958 key_bits = psa_get_key_bits( operation->slot );
2959
2960 operation->cipher_info =
2961 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
2962 &cipher_id );
2963 if( operation->cipher_info == NULL )
2964 return( PSA_ERROR_NOT_SUPPORTED );
2965
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002966 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002967 {
2968#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002969 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
2970 operation->core_alg = PSA_ALG_CCM;
2971 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002972 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
2973 return( PSA_ERROR_INVALID_ARGUMENT );
2974 mbedtls_ccm_init( &operation->ctx.ccm );
2975 status = mbedtls_to_psa_error(
2976 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
2977 operation->slot->data.raw.data,
2978 (unsigned int) key_bits ) );
2979 if( status != 0 )
2980 goto cleanup;
2981 break;
2982#endif /* MBEDTLS_CCM_C */
2983
2984#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002985 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
2986 operation->core_alg = PSA_ALG_GCM;
2987 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002988 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
2989 return( PSA_ERROR_INVALID_ARGUMENT );
2990 mbedtls_gcm_init( &operation->ctx.gcm );
2991 status = mbedtls_to_psa_error(
2992 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
2993 operation->slot->data.raw.data,
2994 (unsigned int) key_bits ) );
2995 break;
2996#endif /* MBEDTLS_GCM_C */
2997
2998 default:
2999 return( PSA_ERROR_NOT_SUPPORTED );
3000 }
3001
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003002 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3003 {
3004 status = PSA_ERROR_INVALID_ARGUMENT;
3005 goto cleanup;
3006 }
3007 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3008 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3009 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3010 * In both cases, mbedtls_xxx will validate the tag length below. */
3011
Gilles Peskineedf9a652018-08-17 18:11:56 +02003012 return( PSA_SUCCESS );
3013
3014cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003015 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003016 return( status );
3017}
3018
mohammad16035955c982018-04-26 00:53:03 +03003019psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3020 psa_algorithm_t alg,
3021 const uint8_t *nonce,
3022 size_t nonce_length,
3023 const uint8_t *additional_data,
3024 size_t additional_data_length,
3025 const uint8_t *plaintext,
3026 size_t plaintext_length,
3027 uint8_t *ciphertext,
3028 size_t ciphertext_size,
3029 size_t *ciphertext_length )
3030{
mohammad16035955c982018-04-26 00:53:03 +03003031 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003032 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003033 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003034
mohammad1603f08a5502018-06-03 15:05:47 +03003035 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003036
Gilles Peskineedf9a652018-08-17 18:11:56 +02003037 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003038 if( status != PSA_SUCCESS )
3039 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003040
Gilles Peskineedf9a652018-08-17 18:11:56 +02003041 /* For all currently supported modes, the tag is at the end of the
3042 * ciphertext. */
3043 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3044 {
3045 status = PSA_ERROR_BUFFER_TOO_SMALL;
3046 goto exit;
3047 }
3048 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003049
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003050 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003051 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003052 status = mbedtls_to_psa_error(
3053 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3054 MBEDTLS_GCM_ENCRYPT,
3055 plaintext_length,
3056 nonce, nonce_length,
3057 additional_data, additional_data_length,
3058 plaintext, ciphertext,
3059 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003060 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003061 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003062 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003063 status = mbedtls_to_psa_error(
3064 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3065 plaintext_length,
3066 nonce, nonce_length,
3067 additional_data,
3068 additional_data_length,
3069 plaintext, ciphertext,
3070 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003071 }
mohammad16035c8845f2018-05-09 05:40:09 -07003072 else
3073 {
mohammad1603554faad2018-06-03 15:07:38 +03003074 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003075 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003076
Gilles Peskineedf9a652018-08-17 18:11:56 +02003077 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3078 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003079
Gilles Peskineedf9a652018-08-17 18:11:56 +02003080exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003081 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003082 if( status == PSA_SUCCESS )
3083 *ciphertext_length = plaintext_length + operation.tag_length;
3084 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003085}
3086
Gilles Peskineee652a32018-06-01 19:23:52 +02003087/* Locate the tag in a ciphertext buffer containing the encrypted data
3088 * followed by the tag. Return the length of the part preceding the tag in
3089 * *plaintext_length. This is the size of the plaintext in modes where
3090 * the encrypted data has the same size as the plaintext, such as
3091 * CCM and GCM. */
3092static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3093 const uint8_t *ciphertext,
3094 size_t ciphertext_length,
3095 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003096 const uint8_t **p_tag )
3097{
3098 size_t payload_length;
3099 if( tag_length > ciphertext_length )
3100 return( PSA_ERROR_INVALID_ARGUMENT );
3101 payload_length = ciphertext_length - tag_length;
3102 if( payload_length > plaintext_size )
3103 return( PSA_ERROR_BUFFER_TOO_SMALL );
3104 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003105 return( PSA_SUCCESS );
3106}
3107
mohammad16035955c982018-04-26 00:53:03 +03003108psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3109 psa_algorithm_t alg,
3110 const uint8_t *nonce,
3111 size_t nonce_length,
3112 const uint8_t *additional_data,
3113 size_t additional_data_length,
3114 const uint8_t *ciphertext,
3115 size_t ciphertext_length,
3116 uint8_t *plaintext,
3117 size_t plaintext_size,
3118 size_t *plaintext_length )
3119{
mohammad16035955c982018-04-26 00:53:03 +03003120 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003121 aead_operation_t operation;
3122 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003123
Gilles Peskineee652a32018-06-01 19:23:52 +02003124 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003125
Gilles Peskineedf9a652018-08-17 18:11:56 +02003126 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003127 if( status != PSA_SUCCESS )
3128 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003129
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003130 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003131 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003132 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003133 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003134 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003135 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003136 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003137
Gilles Peskineedf9a652018-08-17 18:11:56 +02003138 status = mbedtls_to_psa_error(
3139 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3140 ciphertext_length - operation.tag_length,
3141 nonce, nonce_length,
3142 additional_data,
3143 additional_data_length,
3144 tag, operation.tag_length,
3145 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003146 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003147 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003148 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003149 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003150 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003151 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003152 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003153 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003154
Gilles Peskineedf9a652018-08-17 18:11:56 +02003155 status = mbedtls_to_psa_error(
3156 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3157 ciphertext_length - operation.tag_length,
3158 nonce, nonce_length,
3159 additional_data,
3160 additional_data_length,
3161 ciphertext, plaintext,
3162 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003163 }
mohammad160339574652018-06-01 04:39:53 -07003164 else
3165 {
mohammad1603554faad2018-06-03 15:07:38 +03003166 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003167 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003168
Gilles Peskineedf9a652018-08-17 18:11:56 +02003169 if( status != PSA_SUCCESS && plaintext_size != 0 )
3170 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003171
Gilles Peskineedf9a652018-08-17 18:11:56 +02003172exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003173 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003174 if( status == PSA_SUCCESS )
3175 *plaintext_length = ciphertext_length - operation.tag_length;
3176 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003177}
3178
Gilles Peskinea0655c32018-04-30 17:06:50 +02003179
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003180
Gilles Peskine20035e32018-02-03 22:44:14 +01003181/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003182/* Generators */
3183/****************************************************************/
3184
3185psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3186{
3187 psa_status_t status = PSA_SUCCESS;
3188 if( generator->alg == 0 )
3189 {
3190 /* The object has (apparently) been initialized but it is not
3191 * in use. It's ok to call abort on such an object, and there's
3192 * nothing to do. */
3193 }
3194 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003195#if defined(MBEDTLS_MD_C)
3196 if( PSA_ALG_IS_HKDF( generator->alg ) )
3197 {
3198 mbedtls_free( generator->ctx.hkdf.info );
3199 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3200 }
3201 else
3202#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003203 {
3204 status = PSA_ERROR_BAD_STATE;
3205 }
3206 memset( generator, 0, sizeof( *generator ) );
3207 return( status );
3208}
3209
3210
3211psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3212 size_t *capacity)
3213{
3214 *capacity = generator->capacity;
3215 return( PSA_SUCCESS );
3216}
3217
Gilles Peskinebef7f142018-07-12 17:22:21 +02003218#if defined(MBEDTLS_MD_C)
3219/* Read some bytes from an HKDF-based generator. This performs a chunk
3220 * of the expand phase of the HKDF algorithm. */
3221static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3222 psa_algorithm_t hash_alg,
3223 uint8_t *output,
3224 size_t output_length )
3225{
3226 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3227 psa_status_t status;
3228
3229 while( output_length != 0 )
3230 {
3231 /* Copy what remains of the current block */
3232 uint8_t n = hash_length - hkdf->offset_in_block;
3233 if( n > output_length )
3234 n = (uint8_t) output_length;
3235 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3236 output += n;
3237 output_length -= n;
3238 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003239 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003240 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003241 /* We can't be wanting more output after block 0xff, otherwise
3242 * the capacity check in psa_generator_read() would have
3243 * prevented this call. It could happen only if the generator
3244 * object was corrupted or if this function is called directly
3245 * inside the library. */
3246 if( hkdf->block_number == 0xff )
3247 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003248
3249 /* We need a new block */
3250 ++hkdf->block_number;
3251 hkdf->offset_in_block = 0;
3252 status = psa_hmac_setup_internal( &hkdf->hmac,
3253 hkdf->prk, hash_length,
3254 hash_alg );
3255 if( status != PSA_SUCCESS )
3256 return( status );
3257 if( hkdf->block_number != 1 )
3258 {
3259 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3260 hkdf->output_block,
3261 hash_length );
3262 if( status != PSA_SUCCESS )
3263 return( status );
3264 }
3265 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3266 hkdf->info,
3267 hkdf->info_length );
3268 if( status != PSA_SUCCESS )
3269 return( status );
3270 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3271 &hkdf->block_number, 1 );
3272 if( status != PSA_SUCCESS )
3273 return( status );
3274 status = psa_hmac_finish_internal( &hkdf->hmac,
3275 hkdf->output_block,
3276 sizeof( hkdf->output_block ) );
3277 if( status != PSA_SUCCESS )
3278 return( status );
3279 }
3280
3281 return( PSA_SUCCESS );
3282}
3283#endif /* MBEDTLS_MD_C */
3284
Gilles Peskineeab56e42018-07-12 17:12:33 +02003285psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3286 uint8_t *output,
3287 size_t output_length )
3288{
3289 psa_status_t status;
3290
3291 if( output_length > generator->capacity )
3292 {
3293 generator->capacity = 0;
3294 /* Go through the error path to wipe all confidential data now
3295 * that the generator object is useless. */
3296 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3297 goto exit;
3298 }
3299 if( output_length == 0 &&
3300 generator->capacity == 0 && generator->alg == 0 )
3301 {
3302 /* Edge case: this is a blank or finished generator, and 0
3303 * bytes were requested. The right error in this case could
3304 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3305 * INSUFFICIENT_CAPACITY, which is right for a finished
3306 * generator, for consistency with the case when
3307 * output_length > 0. */
3308 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3309 }
3310 generator->capacity -= output_length;
3311
Gilles Peskinebef7f142018-07-12 17:22:21 +02003312#if defined(MBEDTLS_MD_C)
3313 if( PSA_ALG_IS_HKDF( generator->alg ) )
3314 {
3315 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3316 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3317 output, output_length );
3318 }
3319 else
3320#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003321 {
3322 return( PSA_ERROR_BAD_STATE );
3323 }
3324
3325exit:
3326 if( status != PSA_SUCCESS )
3327 {
3328 psa_generator_abort( generator );
3329 memset( output, '!', output_length );
3330 }
3331 return( status );
3332}
3333
Gilles Peskine08542d82018-07-19 17:05:42 +02003334#if defined(MBEDTLS_DES_C)
3335static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3336{
3337 if( data_size >= 8 )
3338 mbedtls_des_key_set_parity( data );
3339 if( data_size >= 16 )
3340 mbedtls_des_key_set_parity( data + 8 );
3341 if( data_size >= 24 )
3342 mbedtls_des_key_set_parity( data + 16 );
3343}
3344#endif /* MBEDTLS_DES_C */
3345
Gilles Peskineeab56e42018-07-12 17:12:33 +02003346psa_status_t psa_generator_import_key( psa_key_slot_t key,
3347 psa_key_type_t type,
3348 size_t bits,
3349 psa_crypto_generator_t *generator )
3350{
3351 uint8_t *data = NULL;
3352 size_t bytes = PSA_BITS_TO_BYTES( bits );
3353 psa_status_t status;
3354
3355 if( ! key_type_is_raw_bytes( type ) )
3356 return( PSA_ERROR_INVALID_ARGUMENT );
3357 if( bits % 8 != 0 )
3358 return( PSA_ERROR_INVALID_ARGUMENT );
3359 data = mbedtls_calloc( 1, bytes );
3360 if( data == NULL )
3361 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3362
3363 status = psa_generator_read( generator, data, bytes );
3364 if( status != PSA_SUCCESS )
3365 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003366#if defined(MBEDTLS_DES_C)
3367 if( type == PSA_KEY_TYPE_DES )
3368 psa_des_set_key_parity( data, bytes );
3369#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003370 status = psa_import_key( key, type, data, bytes );
3371
3372exit:
3373 mbedtls_free( data );
3374 return( status );
3375}
3376
3377
3378
3379/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003380/* Key derivation */
3381/****************************************************************/
3382
Gilles Peskinebef7f142018-07-12 17:22:21 +02003383/* Set up an HKDF-based generator. This is exactly the extract phase
3384 * of the HKDF algorithm. */
3385static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3386 key_slot_t *slot,
3387 psa_algorithm_t hash_alg,
3388 const uint8_t *salt,
3389 size_t salt_length,
3390 const uint8_t *label,
3391 size_t label_length )
3392{
3393 psa_status_t status;
3394 status = psa_hmac_setup_internal( &hkdf->hmac,
3395 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003396 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003397 if( status != PSA_SUCCESS )
3398 return( status );
3399 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3400 slot->data.raw.data,
3401 slot->data.raw.bytes );
3402 if( status != PSA_SUCCESS )
3403 return( status );
3404 status = psa_hmac_finish_internal( &hkdf->hmac,
3405 hkdf->prk,
3406 sizeof( hkdf->prk ) );
3407 if( status != PSA_SUCCESS )
3408 return( status );
3409 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3410 hkdf->block_number = 0;
3411 hkdf->info_length = label_length;
3412 if( label_length != 0 )
3413 {
3414 hkdf->info = mbedtls_calloc( 1, label_length );
3415 if( hkdf->info == NULL )
3416 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3417 memcpy( hkdf->info, label, label_length );
3418 }
3419 return( PSA_SUCCESS );
3420}
3421
Gilles Peskineea0fb492018-07-12 17:17:20 +02003422psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003423 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003424 psa_algorithm_t alg,
3425 const uint8_t *salt,
3426 size_t salt_length,
3427 const uint8_t *label,
3428 size_t label_length,
3429 size_t capacity )
3430{
3431 key_slot_t *slot;
3432 psa_status_t status;
3433
3434 if( generator->alg != 0 )
3435 return( PSA_ERROR_BAD_STATE );
3436
3437 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3438 if( status != PSA_SUCCESS )
3439 return( status );
3440 if( slot->type != PSA_KEY_TYPE_DERIVE )
3441 return( PSA_ERROR_INVALID_ARGUMENT );
3442
3443 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3444 return( PSA_ERROR_INVALID_ARGUMENT );
3445
Gilles Peskinebef7f142018-07-12 17:22:21 +02003446#if defined(MBEDTLS_MD_C)
3447 if( PSA_ALG_IS_HKDF( alg ) )
3448 {
3449 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3450 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3451 if( hash_size == 0 )
3452 return( PSA_ERROR_NOT_SUPPORTED );
3453 if( capacity > 255 * hash_size )
3454 return( PSA_ERROR_INVALID_ARGUMENT );
3455 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3456 slot,
3457 hash_alg,
3458 salt, salt_length,
3459 label, label_length );
3460 }
3461 else
3462#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003463 {
3464 return( PSA_ERROR_NOT_SUPPORTED );
3465 }
3466
3467 /* Set generator->alg even on failure so that abort knows what to do. */
3468 generator->alg = alg;
3469 if( status == PSA_SUCCESS )
3470 generator->capacity = capacity;
3471 else
3472 psa_generator_abort( generator );
3473 return( status );
3474}
3475
3476
3477
3478/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003479/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003480/****************************************************************/
3481
3482psa_status_t psa_generate_random( uint8_t *output,
3483 size_t output_size )
3484{
itayzafrir0adf0fc2018-09-06 16:24:41 +03003485 int ret;
3486 GUARD_MODULE_INITIALIZED;
3487
3488 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02003489 return( mbedtls_to_psa_error( ret ) );
3490}
3491
3492psa_status_t psa_generate_key( psa_key_slot_t key,
3493 psa_key_type_t type,
3494 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003495 const void *extra,
3496 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003497{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003498 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003499 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003500
Gilles Peskine53d991e2018-07-12 01:14:59 +02003501 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003502 return( PSA_ERROR_INVALID_ARGUMENT );
3503
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003504 status = psa_get_empty_key_slot( key, &slot );
3505 if( status != PSA_SUCCESS )
3506 return( status );
3507
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003508 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003509 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003510 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003511 if( status != PSA_SUCCESS )
3512 return( status );
3513 status = psa_generate_random( slot->data.raw.data,
3514 slot->data.raw.bytes );
3515 if( status != PSA_SUCCESS )
3516 {
3517 mbedtls_free( slot->data.raw.data );
3518 return( status );
3519 }
3520#if defined(MBEDTLS_DES_C)
3521 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003522 psa_des_set_key_parity( slot->data.raw.data,
3523 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003524#endif /* MBEDTLS_DES_C */
3525 }
3526 else
3527
3528#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3529 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3530 {
3531 mbedtls_rsa_context *rsa;
3532 int ret;
3533 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003534 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3535 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003536 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003537 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003538 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003539 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003540 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003541#if INT_MAX < 0xffffffff
3542 /* Check that the uint32_t value passed by the caller fits
3543 * in the range supported by this implementation. */
3544 if( p->e > INT_MAX )
3545 return( PSA_ERROR_NOT_SUPPORTED );
3546#endif
3547 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003548 }
3549 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3550 if( rsa == NULL )
3551 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3552 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3553 ret = mbedtls_rsa_gen_key( rsa,
3554 mbedtls_ctr_drbg_random,
3555 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003556 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003557 exponent );
3558 if( ret != 0 )
3559 {
3560 mbedtls_rsa_free( rsa );
3561 mbedtls_free( rsa );
3562 return( mbedtls_to_psa_error( ret ) );
3563 }
3564 slot->data.rsa = rsa;
3565 }
3566 else
3567#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3568
3569#if defined(MBEDTLS_ECP_C)
3570 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3571 {
3572 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3573 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3574 const mbedtls_ecp_curve_info *curve_info =
3575 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3576 mbedtls_ecp_keypair *ecp;
3577 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003578 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003579 return( PSA_ERROR_NOT_SUPPORTED );
3580 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3581 return( PSA_ERROR_NOT_SUPPORTED );
3582 if( curve_info->bit_size != bits )
3583 return( PSA_ERROR_INVALID_ARGUMENT );
3584 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3585 if( ecp == NULL )
3586 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3587 mbedtls_ecp_keypair_init( ecp );
3588 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3589 mbedtls_ctr_drbg_random,
3590 &global_data.ctr_drbg );
3591 if( ret != 0 )
3592 {
3593 mbedtls_ecp_keypair_free( ecp );
3594 mbedtls_free( ecp );
3595 return( mbedtls_to_psa_error( ret ) );
3596 }
3597 slot->data.ecp = ecp;
3598 }
3599 else
3600#endif /* MBEDTLS_ECP_C */
3601
3602 return( PSA_ERROR_NOT_SUPPORTED );
3603
3604 slot->type = type;
3605 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003606}
3607
3608
3609/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003610/* Module setup */
3611/****************************************************************/
3612
Gilles Peskinee59236f2018-01-27 23:32:46 +01003613void mbedtls_psa_crypto_free( void )
3614{
Jaeden Amero045bd502018-06-26 14:00:08 +01003615 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003616 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003617 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003618 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3619 mbedtls_entropy_free( &global_data.entropy );
3620 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3621}
3622
3623psa_status_t psa_crypto_init( void )
3624{
3625 int ret;
3626 const unsigned char drbg_seed[] = "PSA";
3627
3628 if( global_data.initialized != 0 )
3629 return( PSA_SUCCESS );
3630
3631 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3632 mbedtls_entropy_init( &global_data.entropy );
3633 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3634
3635 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3636 mbedtls_entropy_func,
3637 &global_data.entropy,
3638 drbg_seed, sizeof( drbg_seed ) - 1 );
3639 if( ret != 0 )
3640 goto exit;
3641
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003642 global_data.initialized = 1;
3643
Gilles Peskinee59236f2018-01-27 23:32:46 +01003644exit:
3645 if( ret != 0 )
3646 mbedtls_psa_crypto_free( );
3647 return( mbedtls_to_psa_error( ret ) );
3648}
3649
3650#endif /* MBEDTLS_PSA_CRYPTO_C */