blob: b99c808caea5a464895d8cb9df81c2a3f45432f0 [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)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100574/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
575 * that are not a multiple of 8) well. For example, there is only
576 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
577 * way to return the exact bit size of a key.
578 * To keep things simple, reject non-byte-aligned key sizes. */
579static psa_status_t psa_check_rsa_key_byte_aligned(
580 const mbedtls_rsa_context *rsa )
581{
582 mbedtls_mpi n;
583 psa_status_t status;
584 mbedtls_mpi_init( &n );
585 status = mbedtls_to_psa_error(
586 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
587 if( status == PSA_SUCCESS )
588 {
589 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
590 status = PSA_ERROR_NOT_SUPPORTED;
591 }
592 mbedtls_mpi_free( &n );
593 return( status );
594}
595
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200596static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
597 mbedtls_rsa_context **p_rsa )
598{
599 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
600 return( PSA_ERROR_INVALID_ARGUMENT );
601 else
602 {
603 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100604 /* The size of an RSA key doesn't have to be a multiple of 8.
605 * Mbed TLS supports non-byte-aligned key sizes, but not well.
606 * For example, mbedtls_rsa_get_len() returns the key size in
607 * bytes, not in bits. */
608 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100609 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200610 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
611 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100612 status = psa_check_rsa_key_byte_aligned( rsa );
613 if( status != PSA_SUCCESS )
614 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200615 *p_rsa = rsa;
616 return( PSA_SUCCESS );
617 }
618}
619#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
620
621#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100622/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200623static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
624 mbedtls_pk_context *pk,
625 mbedtls_ecp_keypair **p_ecp )
626{
627 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
628 return( PSA_ERROR_INVALID_ARGUMENT );
629 else
630 {
631 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
632 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
633 if( actual_curve != expected_curve )
634 return( PSA_ERROR_INVALID_ARGUMENT );
635 *p_ecp = ecp;
636 return( PSA_SUCCESS );
637 }
638}
639#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
640
Gilles Peskinef76aa772018-10-29 19:24:33 +0100641#if defined(MBEDTLS_ECP_C)
642/* Import a private key given as a byte string which is the private value
643 * in big-endian order. */
644static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
645 const uint8_t *data,
646 size_t data_length,
647 mbedtls_ecp_keypair **p_ecp )
648{
649 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
650 mbedtls_ecp_keypair *ecp = NULL;
651 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
652
653 *p_ecp = NULL;
654 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
655 if( ecp == NULL )
656 return( PSA_ERROR_INSUFFICIENT_MEMORY );
657
658 /* Load the group. */
659 status = mbedtls_to_psa_error(
660 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
661 if( status != PSA_SUCCESS )
662 goto exit;
663 /* Load the secret value. */
664 status = mbedtls_to_psa_error(
665 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
666 if( status != PSA_SUCCESS )
667 goto exit;
668 /* Validate the private key. */
669 status = mbedtls_to_psa_error(
670 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
671 if( status != PSA_SUCCESS )
672 goto exit;
673 /* Calculate the public key from the private key. */
674 status = mbedtls_to_psa_error(
675 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
676 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
677 if( status != PSA_SUCCESS )
678 goto exit;
679
680 *p_ecp = ecp;
681 return( PSA_SUCCESS );
682
683exit:
684 if( ecp != NULL )
685 {
686 mbedtls_ecp_keypair_free( ecp );
687 mbedtls_free( ecp );
688 }
689 return( status );
690}
691#endif /* defined(MBEDTLS_ECP_C) */
692
Gilles Peskine2d277862018-06-18 15:41:12 +0200693psa_status_t psa_import_key( psa_key_slot_t key,
694 psa_key_type_t type,
695 const uint8_t *data,
696 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100697{
698 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200699 psa_status_t status = PSA_SUCCESS;
700 status = psa_get_empty_key_slot( key, &slot );
701 if( status != PSA_SUCCESS )
702 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200704 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100705 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100706 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 if( data_length > SIZE_MAX / 8 )
708 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200709 status = prepare_raw_data_slot( type,
710 PSA_BYTES_TO_BITS( data_length ),
711 &slot->data.raw );
712 if( status != PSA_SUCCESS )
713 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200714 if( data_length != 0 )
715 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100716 }
717 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100718#if defined(MBEDTLS_ECP_C)
719 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
720 {
721 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( type ),
722 data, data_length,
723 &slot->data.ecp );
724 if( status != PSA_SUCCESS )
725 return( status );
726 }
727 else
728#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100729#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200730 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100731 {
732 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100733 mbedtls_pk_context pk;
734 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200735
736 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100737 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
738 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
739 else
740 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100741 if( ret != 0 )
742 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200743
744 /* We have something that the pkparse module recognizes.
745 * If it has the expected type and passes any type-specific
746 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100747#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200748 if( PSA_KEY_TYPE_IS_RSA( type ) )
749 status = psa_import_rsa_key( &pk, &slot->data.rsa );
750 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100751#endif /* MBEDTLS_RSA_C */
752#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200753 if( PSA_KEY_TYPE_IS_ECC( type ) )
754 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
755 &pk, &slot->data.ecp );
756 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100757#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200758 {
759 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200760 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200761
Gilles Peskinec648d692018-06-28 08:46:13 +0200762 /* Free the content of the pk object only on error. On success,
763 * the content of the object has been stored in the slot. */
764 if( status != PSA_SUCCESS )
765 {
766 mbedtls_pk_free( &pk );
767 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100768 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100769 }
770 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100771#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100772 {
773 return( PSA_ERROR_NOT_SUPPORTED );
774 }
775
776 slot->type = type;
777 return( PSA_SUCCESS );
778}
779
Gilles Peskine2d277862018-06-18 15:41:12 +0200780psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100781{
782 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200783 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100784
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200785 status = psa_get_key_slot( key, &slot );
786 if( status != PSA_SUCCESS )
787 return( status );
788
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200790 {
791 /* No key material to clean, but do zeroize the slot below to wipe
792 * metadata such as policies. */
793 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200794 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100795 {
796 mbedtls_free( slot->data.raw.data );
797 }
798 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100799#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200800 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100801 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100802 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100803 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100804 }
805 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100806#endif /* defined(MBEDTLS_RSA_C) */
807#if defined(MBEDTLS_ECP_C)
808 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
809 {
810 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100811 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100812 }
813 else
814#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 {
816 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100817 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 return( PSA_ERROR_TAMPERING_DETECTED );
819 }
820
821 mbedtls_zeroize( slot, sizeof( *slot ) );
822 return( PSA_SUCCESS );
823}
824
Gilles Peskineb870b182018-07-06 16:02:09 +0200825/* Return the size of the key in the given slot, in bits. */
826static size_t psa_get_key_bits( const key_slot_t *slot )
827{
828 if( key_type_is_raw_bytes( slot->type ) )
829 return( slot->data.raw.bytes * 8 );
830#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200831 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100832 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200833#endif /* defined(MBEDTLS_RSA_C) */
834#if defined(MBEDTLS_ECP_C)
835 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
836 return( slot->data.ecp->grp.pbits );
837#endif /* defined(MBEDTLS_ECP_C) */
838 /* Shouldn't happen except on an empty slot. */
839 return( 0 );
840}
841
Gilles Peskine2d277862018-06-18 15:41:12 +0200842psa_status_t psa_get_key_information( psa_key_slot_t key,
843 psa_key_type_t *type,
844 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845{
846 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200847 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100848
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100849 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200850 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100851 if( bits != NULL )
852 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200853 status = psa_get_key_slot( key, &slot );
854 if( status != PSA_SUCCESS )
855 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200856
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100857 if( slot->type == PSA_KEY_TYPE_NONE )
858 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200859 if( type != NULL )
860 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200861 if( bits != NULL )
862 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100863 return( PSA_SUCCESS );
864}
865
Gilles Peskine2d277862018-06-18 15:41:12 +0200866static psa_status_t psa_internal_export_key( psa_key_slot_t key,
867 uint8_t *data,
868 size_t data_size,
869 size_t *data_length,
870 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871{
872 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200873 psa_status_t status;
874 /* Exporting a public key doesn't require a usage flag. If we're
875 * called by psa_export_public_key(), don't require the EXPORT flag.
876 * If we're called by psa_export_key(), do require the EXPORT flag;
877 * if the key turns out to be public key object, psa_get_key_from_slot()
878 * will ignore this flag. */
879 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100880
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100881 /* Set the key to empty now, so that even when there are errors, we always
882 * set data_length to a value between 0 and data_size. On error, setting
883 * the key to empty is a good choice because an empty key representation is
884 * unlikely to be accepted anywhere. */
885 *data_length = 0;
886
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200887 status = psa_get_key_from_slot( key, &slot, usage, 0 );
888 if( status != PSA_SUCCESS )
889 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200890 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300891 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300892
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200893 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100894 {
895 if( slot->data.raw.bytes > data_size )
896 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100897 if( data_size != 0 )
898 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200899 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100900 memset( data + slot->data.raw.bytes, 0,
901 data_size - slot->data.raw.bytes );
902 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100903 *data_length = slot->data.raw.bytes;
904 return( PSA_SUCCESS );
905 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100906#if defined(MBEDTLS_ECP_C)
907 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
908 {
909 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
910 if( bytes > data_size )
911 return( PSA_ERROR_BUFFER_TOO_SMALL );
912 status = mbedtls_to_psa_error(
913 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
914 if( status != PSA_SUCCESS )
915 return( status );
916 memset( data + bytes, 0, data_size - bytes );
917 *data_length = bytes;
918 return( PSA_SUCCESS );
919 }
920#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100921 else
Moran Peker17e36e12018-05-02 12:55:20 +0300922 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100923#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200924 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300925 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100926 {
Moran Pekera998bc62018-04-16 18:16:20 +0300927 mbedtls_pk_context pk;
928 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200929 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300930 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100931#if defined(MBEDTLS_RSA_C)
932 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300933 pk.pk_info = &mbedtls_rsa_info;
934 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100935#else
936 return( PSA_ERROR_NOT_SUPPORTED );
937#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300938 }
939 else
940 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100941#if defined(MBEDTLS_ECP_C)
942 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300943 pk.pk_info = &mbedtls_eckey_info;
944 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100945#else
946 return( PSA_ERROR_NOT_SUPPORTED );
947#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300948 }
Moran Pekerd7326592018-05-29 16:56:39 +0300949 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300950 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300951 else
952 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300953 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200954 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200955 /* If data_size is 0 then data may be NULL and then the
956 * call to memset would have undefined behavior. */
957 if( data_size != 0 )
958 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300959 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200960 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200961 /* The mbedtls_pk_xxx functions write to the end of the buffer.
962 * Move the data to the beginning and erase remaining data
963 * at the original location. */
964 if( 2 * (size_t) ret <= data_size )
965 {
966 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200967 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200968 }
969 else if( (size_t) ret < data_size )
970 {
971 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200972 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200973 }
Moran Pekera998bc62018-04-16 18:16:20 +0300974 *data_length = ret;
975 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100976 }
977 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100978#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300979 {
980 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200981 it is valid for a special-purpose implementation to omit
982 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300983 return( PSA_ERROR_NOT_SUPPORTED );
984 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985 }
986}
987
Gilles Peskine2d277862018-06-18 15:41:12 +0200988psa_status_t psa_export_key( psa_key_slot_t key,
989 uint8_t *data,
990 size_t data_size,
991 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300992{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200993 return( psa_internal_export_key( key, data, data_size,
994 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996
Gilles Peskine2d277862018-06-18 15:41:12 +0200997psa_status_t psa_export_public_key( psa_key_slot_t key,
998 uint8_t *data,
999 size_t data_size,
1000 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001001{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001002 return( psa_internal_export_key( key, data, data_size,
1003 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001004}
1005
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001006
1007
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001009/* Message digests */
1010/****************************************************************/
1011
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001012static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001013{
1014 switch( alg )
1015 {
1016#if defined(MBEDTLS_MD2_C)
1017 case PSA_ALG_MD2:
1018 return( &mbedtls_md2_info );
1019#endif
1020#if defined(MBEDTLS_MD4_C)
1021 case PSA_ALG_MD4:
1022 return( &mbedtls_md4_info );
1023#endif
1024#if defined(MBEDTLS_MD5_C)
1025 case PSA_ALG_MD5:
1026 return( &mbedtls_md5_info );
1027#endif
1028#if defined(MBEDTLS_RIPEMD160_C)
1029 case PSA_ALG_RIPEMD160:
1030 return( &mbedtls_ripemd160_info );
1031#endif
1032#if defined(MBEDTLS_SHA1_C)
1033 case PSA_ALG_SHA_1:
1034 return( &mbedtls_sha1_info );
1035#endif
1036#if defined(MBEDTLS_SHA256_C)
1037 case PSA_ALG_SHA_224:
1038 return( &mbedtls_sha224_info );
1039 case PSA_ALG_SHA_256:
1040 return( &mbedtls_sha256_info );
1041#endif
1042#if defined(MBEDTLS_SHA512_C)
1043 case PSA_ALG_SHA_384:
1044 return( &mbedtls_sha384_info );
1045 case PSA_ALG_SHA_512:
1046 return( &mbedtls_sha512_info );
1047#endif
1048 default:
1049 return( NULL );
1050 }
1051}
1052
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001053psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1054{
1055 switch( operation->alg )
1056 {
Gilles Peskine81736312018-06-26 15:04:31 +02001057 case 0:
1058 /* The object has (apparently) been initialized but it is not
1059 * in use. It's ok to call abort on such an object, and there's
1060 * nothing to do. */
1061 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001062#if defined(MBEDTLS_MD2_C)
1063 case PSA_ALG_MD2:
1064 mbedtls_md2_free( &operation->ctx.md2 );
1065 break;
1066#endif
1067#if defined(MBEDTLS_MD4_C)
1068 case PSA_ALG_MD4:
1069 mbedtls_md4_free( &operation->ctx.md4 );
1070 break;
1071#endif
1072#if defined(MBEDTLS_MD5_C)
1073 case PSA_ALG_MD5:
1074 mbedtls_md5_free( &operation->ctx.md5 );
1075 break;
1076#endif
1077#if defined(MBEDTLS_RIPEMD160_C)
1078 case PSA_ALG_RIPEMD160:
1079 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1080 break;
1081#endif
1082#if defined(MBEDTLS_SHA1_C)
1083 case PSA_ALG_SHA_1:
1084 mbedtls_sha1_free( &operation->ctx.sha1 );
1085 break;
1086#endif
1087#if defined(MBEDTLS_SHA256_C)
1088 case PSA_ALG_SHA_224:
1089 case PSA_ALG_SHA_256:
1090 mbedtls_sha256_free( &operation->ctx.sha256 );
1091 break;
1092#endif
1093#if defined(MBEDTLS_SHA512_C)
1094 case PSA_ALG_SHA_384:
1095 case PSA_ALG_SHA_512:
1096 mbedtls_sha512_free( &operation->ctx.sha512 );
1097 break;
1098#endif
1099 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001100 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001101 }
1102 operation->alg = 0;
1103 return( PSA_SUCCESS );
1104}
1105
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001106psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001107 psa_algorithm_t alg )
1108{
1109 int ret;
1110 operation->alg = 0;
1111 switch( alg )
1112 {
1113#if defined(MBEDTLS_MD2_C)
1114 case PSA_ALG_MD2:
1115 mbedtls_md2_init( &operation->ctx.md2 );
1116 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1117 break;
1118#endif
1119#if defined(MBEDTLS_MD4_C)
1120 case PSA_ALG_MD4:
1121 mbedtls_md4_init( &operation->ctx.md4 );
1122 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1123 break;
1124#endif
1125#if defined(MBEDTLS_MD5_C)
1126 case PSA_ALG_MD5:
1127 mbedtls_md5_init( &operation->ctx.md5 );
1128 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1129 break;
1130#endif
1131#if defined(MBEDTLS_RIPEMD160_C)
1132 case PSA_ALG_RIPEMD160:
1133 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1134 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1135 break;
1136#endif
1137#if defined(MBEDTLS_SHA1_C)
1138 case PSA_ALG_SHA_1:
1139 mbedtls_sha1_init( &operation->ctx.sha1 );
1140 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1141 break;
1142#endif
1143#if defined(MBEDTLS_SHA256_C)
1144 case PSA_ALG_SHA_224:
1145 mbedtls_sha256_init( &operation->ctx.sha256 );
1146 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1147 break;
1148 case PSA_ALG_SHA_256:
1149 mbedtls_sha256_init( &operation->ctx.sha256 );
1150 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1151 break;
1152#endif
1153#if defined(MBEDTLS_SHA512_C)
1154 case PSA_ALG_SHA_384:
1155 mbedtls_sha512_init( &operation->ctx.sha512 );
1156 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1157 break;
1158 case PSA_ALG_SHA_512:
1159 mbedtls_sha512_init( &operation->ctx.sha512 );
1160 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1161 break;
1162#endif
1163 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001164 return( PSA_ALG_IS_HASH( alg ) ?
1165 PSA_ERROR_NOT_SUPPORTED :
1166 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001167 }
1168 if( ret == 0 )
1169 operation->alg = alg;
1170 else
1171 psa_hash_abort( operation );
1172 return( mbedtls_to_psa_error( ret ) );
1173}
1174
1175psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1176 const uint8_t *input,
1177 size_t input_length )
1178{
1179 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001180
1181 /* Don't require hash implementations to behave correctly on a
1182 * zero-length input, which may have an invalid pointer. */
1183 if( input_length == 0 )
1184 return( PSA_SUCCESS );
1185
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001186 switch( operation->alg )
1187 {
1188#if defined(MBEDTLS_MD2_C)
1189 case PSA_ALG_MD2:
1190 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1191 input, input_length );
1192 break;
1193#endif
1194#if defined(MBEDTLS_MD4_C)
1195 case PSA_ALG_MD4:
1196 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1197 input, input_length );
1198 break;
1199#endif
1200#if defined(MBEDTLS_MD5_C)
1201 case PSA_ALG_MD5:
1202 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1203 input, input_length );
1204 break;
1205#endif
1206#if defined(MBEDTLS_RIPEMD160_C)
1207 case PSA_ALG_RIPEMD160:
1208 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1209 input, input_length );
1210 break;
1211#endif
1212#if defined(MBEDTLS_SHA1_C)
1213 case PSA_ALG_SHA_1:
1214 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1215 input, input_length );
1216 break;
1217#endif
1218#if defined(MBEDTLS_SHA256_C)
1219 case PSA_ALG_SHA_224:
1220 case PSA_ALG_SHA_256:
1221 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1222 input, input_length );
1223 break;
1224#endif
1225#if defined(MBEDTLS_SHA512_C)
1226 case PSA_ALG_SHA_384:
1227 case PSA_ALG_SHA_512:
1228 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1229 input, input_length );
1230 break;
1231#endif
1232 default:
1233 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1234 break;
1235 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001236
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001237 if( ret != 0 )
1238 psa_hash_abort( operation );
1239 return( mbedtls_to_psa_error( ret ) );
1240}
1241
1242psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1243 uint8_t *hash,
1244 size_t hash_size,
1245 size_t *hash_length )
1246{
itayzafrir40835d42018-08-02 13:14:17 +03001247 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001248 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001249 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001250
1251 /* Fill the output buffer with something that isn't a valid hash
1252 * (barring an attack on the hash and deliberately-crafted input),
1253 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001254 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001255 /* If hash_size is 0 then hash may be NULL and then the
1256 * call to memset would have undefined behavior. */
1257 if( hash_size != 0 )
1258 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001259
1260 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001261 {
1262 status = PSA_ERROR_BUFFER_TOO_SMALL;
1263 goto exit;
1264 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001265
1266 switch( operation->alg )
1267 {
1268#if defined(MBEDTLS_MD2_C)
1269 case PSA_ALG_MD2:
1270 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1271 break;
1272#endif
1273#if defined(MBEDTLS_MD4_C)
1274 case PSA_ALG_MD4:
1275 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1276 break;
1277#endif
1278#if defined(MBEDTLS_MD5_C)
1279 case PSA_ALG_MD5:
1280 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1281 break;
1282#endif
1283#if defined(MBEDTLS_RIPEMD160_C)
1284 case PSA_ALG_RIPEMD160:
1285 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1286 break;
1287#endif
1288#if defined(MBEDTLS_SHA1_C)
1289 case PSA_ALG_SHA_1:
1290 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1291 break;
1292#endif
1293#if defined(MBEDTLS_SHA256_C)
1294 case PSA_ALG_SHA_224:
1295 case PSA_ALG_SHA_256:
1296 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1297 break;
1298#endif
1299#if defined(MBEDTLS_SHA512_C)
1300 case PSA_ALG_SHA_384:
1301 case PSA_ALG_SHA_512:
1302 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1303 break;
1304#endif
1305 default:
1306 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1307 break;
1308 }
itayzafrir40835d42018-08-02 13:14:17 +03001309 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001310
itayzafrir40835d42018-08-02 13:14:17 +03001311exit:
1312 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001313 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001314 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001315 return( psa_hash_abort( operation ) );
1316 }
1317 else
1318 {
1319 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001320 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001321 }
1322}
1323
Gilles Peskine2d277862018-06-18 15:41:12 +02001324psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1325 const uint8_t *hash,
1326 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001327{
1328 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1329 size_t actual_hash_length;
1330 psa_status_t status = psa_hash_finish( operation,
1331 actual_hash, sizeof( actual_hash ),
1332 &actual_hash_length );
1333 if( status != PSA_SUCCESS )
1334 return( status );
1335 if( actual_hash_length != hash_length )
1336 return( PSA_ERROR_INVALID_SIGNATURE );
1337 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1338 return( PSA_ERROR_INVALID_SIGNATURE );
1339 return( PSA_SUCCESS );
1340}
1341
1342
1343
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001344/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001345/* MAC */
1346/****************************************************************/
1347
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001348static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001349 psa_algorithm_t alg,
1350 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001351 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001352 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001353{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001354 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001355 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001356
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001357 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001358 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001359
Gilles Peskine8c9def32018-02-08 10:02:12 +01001360 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1361 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001362 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001363 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001364 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001365 mode = MBEDTLS_MODE_STREAM;
1366 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001367 case PSA_ALG_CTR:
1368 mode = MBEDTLS_MODE_CTR;
1369 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001370 case PSA_ALG_CFB:
1371 mode = MBEDTLS_MODE_CFB;
1372 break;
1373 case PSA_ALG_OFB:
1374 mode = MBEDTLS_MODE_OFB;
1375 break;
1376 case PSA_ALG_CBC_NO_PADDING:
1377 mode = MBEDTLS_MODE_CBC;
1378 break;
1379 case PSA_ALG_CBC_PKCS7:
1380 mode = MBEDTLS_MODE_CBC;
1381 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001382 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383 mode = MBEDTLS_MODE_CCM;
1384 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001385 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001386 mode = MBEDTLS_MODE_GCM;
1387 break;
1388 default:
1389 return( NULL );
1390 }
1391 }
1392 else if( alg == PSA_ALG_CMAC )
1393 mode = MBEDTLS_MODE_ECB;
1394 else if( alg == PSA_ALG_GMAC )
1395 mode = MBEDTLS_MODE_GCM;
1396 else
1397 return( NULL );
1398
1399 switch( key_type )
1400 {
1401 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001402 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001403 break;
1404 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001405 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1406 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001407 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001408 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001409 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001410 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001411 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1412 * but two-key Triple-DES is functionally three-key Triple-DES
1413 * with K1=K3, so that's how we present it to mbedtls. */
1414 if( key_bits == 128 )
1415 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001416 break;
1417 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001418 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001419 break;
1420 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001421 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001422 break;
1423 default:
1424 return( NULL );
1425 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001426 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001427 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001428
Jaeden Amero23bbb752018-06-26 14:16:54 +01001429 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1430 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001431}
1432
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001433static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001434{
Gilles Peskine2d277862018-06-18 15:41:12 +02001435 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001436 {
1437 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001438 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001439 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001440 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001441 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001442 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001443 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001444 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001445 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001446 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001447 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001448 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001449 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001450 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001451 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001452 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001453 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001454 return( 128 );
1455 default:
1456 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001457 }
1458}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001459
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460/* Initialize the MAC operation structure. Once this function has been
1461 * called, psa_mac_abort can run and will do the right thing. */
1462static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1463 psa_algorithm_t alg )
1464{
1465 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1466
1467 operation->alg = alg;
1468 operation->key_set = 0;
1469 operation->iv_set = 0;
1470 operation->iv_required = 0;
1471 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001472 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001473
1474#if defined(MBEDTLS_CMAC_C)
1475 if( alg == PSA_ALG_CMAC )
1476 {
1477 operation->iv_required = 0;
1478 mbedtls_cipher_init( &operation->ctx.cmac );
1479 status = PSA_SUCCESS;
1480 }
1481 else
1482#endif /* MBEDTLS_CMAC_C */
1483#if defined(MBEDTLS_MD_C)
1484 if( PSA_ALG_IS_HMAC( operation->alg ) )
1485 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001486 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1487 operation->ctx.hmac.hash_ctx.alg = 0;
1488 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001489 }
1490 else
1491#endif /* MBEDTLS_MD_C */
1492 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001493 if( ! PSA_ALG_IS_MAC( alg ) )
1494 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001495 }
1496
1497 if( status != PSA_SUCCESS )
1498 memset( operation, 0, sizeof( *operation ) );
1499 return( status );
1500}
1501
Gilles Peskine01126fa2018-07-12 17:04:55 +02001502#if defined(MBEDTLS_MD_C)
1503static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1504{
1505 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1506 return( psa_hash_abort( &hmac->hash_ctx ) );
1507}
1508#endif /* MBEDTLS_MD_C */
1509
Gilles Peskine8c9def32018-02-08 10:02:12 +01001510psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1511{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001512 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001513 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001514 /* The object has (apparently) been initialized but it is not
1515 * in use. It's ok to call abort on such an object, and there's
1516 * nothing to do. */
1517 return( PSA_SUCCESS );
1518 }
1519 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001520#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001521 if( operation->alg == PSA_ALG_CMAC )
1522 {
1523 mbedtls_cipher_free( &operation->ctx.cmac );
1524 }
1525 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001527#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001528 if( PSA_ALG_IS_HMAC( operation->alg ) )
1529 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001530 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001531 }
1532 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001533#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001534 {
1535 /* Sanity check (shouldn't happen: operation->alg should
1536 * always have been initialized to a valid value). */
1537 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001538 }
Moran Peker41deec42018-04-04 15:43:05 +03001539
Gilles Peskine8c9def32018-02-08 10:02:12 +01001540 operation->alg = 0;
1541 operation->key_set = 0;
1542 operation->iv_set = 0;
1543 operation->iv_required = 0;
1544 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001545 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001546
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001548
1549bad_state:
1550 /* If abort is called on an uninitialized object, we can't trust
1551 * anything. Wipe the object in case it contains confidential data.
1552 * This may result in a memory leak if a pointer gets overwritten,
1553 * but it's too late to do anything about this. */
1554 memset( operation, 0, sizeof( *operation ) );
1555 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001556}
1557
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001558#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001559static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001560 size_t key_bits,
1561 key_slot_t *slot,
1562 const mbedtls_cipher_info_t *cipher_info )
1563{
1564 int ret;
1565
1566 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001567
1568 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1569 if( ret != 0 )
1570 return( ret );
1571
1572 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1573 slot->data.raw.data,
1574 key_bits );
1575 return( ret );
1576}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001577#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001578
Gilles Peskine248051a2018-06-20 16:09:38 +02001579#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001580static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1581 const uint8_t *key,
1582 size_t key_length,
1583 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001584{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001585 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001586 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001587 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001588 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001589 psa_status_t status;
1590
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001591 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1592 * overflow below. This should never trigger if the hash algorithm
1593 * is implemented correctly. */
1594 /* The size checks against the ipad and opad buffers cannot be written
1595 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1596 * because that triggers -Wlogical-op on GCC 7.3. */
1597 if( block_size > sizeof( ipad ) )
1598 return( PSA_ERROR_NOT_SUPPORTED );
1599 if( block_size > sizeof( hmac->opad ) )
1600 return( PSA_ERROR_NOT_SUPPORTED );
1601 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001602 return( PSA_ERROR_NOT_SUPPORTED );
1603
Gilles Peskined223b522018-06-11 18:12:58 +02001604 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001605 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001606 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001607 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001608 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001609 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001610 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001611 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001612 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001613 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001614 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001615 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001616 }
Gilles Peskine96889972018-07-12 17:07:03 +02001617 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1618 * but it is permitted. It is common when HMAC is used in HKDF, for
1619 * example. Don't call `memcpy` in the 0-length because `key` could be
1620 * an invalid pointer which would make the behavior undefined. */
1621 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001622 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001623
Gilles Peskined223b522018-06-11 18:12:58 +02001624 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1625 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001626 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001627 ipad[i] ^= 0x36;
1628 memset( ipad + key_length, 0x36, block_size - key_length );
1629
1630 /* Copy the key material from ipad to opad, flipping the requisite bits,
1631 * and filling the rest of opad with the requisite constant. */
1632 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001633 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1634 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001635
Gilles Peskine01126fa2018-07-12 17:04:55 +02001636 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001637 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001638 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001639
Gilles Peskine01126fa2018-07-12 17:04:55 +02001640 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001641
1642cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001643 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001644
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001645 return( status );
1646}
Gilles Peskine248051a2018-06-20 16:09:38 +02001647#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001648
Gilles Peskine89167cb2018-07-08 20:12:23 +02001649static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1650 psa_key_slot_t key,
1651 psa_algorithm_t alg,
1652 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001653{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001654 psa_status_t status;
1655 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001656 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001657 psa_key_usage_t usage =
1658 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001659 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001660 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001661
Gilles Peskined911eb72018-08-14 15:18:45 +02001662 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001663 if( status != PSA_SUCCESS )
1664 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001665 if( is_sign )
1666 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001667
Gilles Peskine89167cb2018-07-08 20:12:23 +02001668 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001669 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001670 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001671 key_bits = psa_get_key_bits( slot );
1672
Gilles Peskine8c9def32018-02-08 10:02:12 +01001673#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001674 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001675 {
1676 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001677 mbedtls_cipher_info_from_psa( full_length_alg,
1678 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001679 int ret;
1680 if( cipher_info == NULL )
1681 {
1682 status = PSA_ERROR_NOT_SUPPORTED;
1683 goto exit;
1684 }
1685 operation->mac_size = cipher_info->block_size;
1686 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1687 status = mbedtls_to_psa_error( ret );
1688 }
1689 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001690#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001691#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001692 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001693 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001694 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001695 if( hash_alg == 0 )
1696 {
1697 status = PSA_ERROR_NOT_SUPPORTED;
1698 goto exit;
1699 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001700
1701 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1702 /* Sanity check. This shouldn't fail on a valid configuration. */
1703 if( operation->mac_size == 0 ||
1704 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1705 {
1706 status = PSA_ERROR_NOT_SUPPORTED;
1707 goto exit;
1708 }
1709
Gilles Peskine01126fa2018-07-12 17:04:55 +02001710 if( slot->type != PSA_KEY_TYPE_HMAC )
1711 {
1712 status = PSA_ERROR_INVALID_ARGUMENT;
1713 goto exit;
1714 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001715
Gilles Peskine01126fa2018-07-12 17:04:55 +02001716 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1717 slot->data.raw.data,
1718 slot->data.raw.bytes,
1719 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001720 }
1721 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001722#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001723 {
1724 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001725 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001726
Gilles Peskined911eb72018-08-14 15:18:45 +02001727 if( truncated == 0 )
1728 {
1729 /* The "normal" case: untruncated algorithm. Nothing to do. */
1730 }
1731 else if( truncated < 4 )
1732 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001733 /* A very short MAC is too short for security since it can be
1734 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1735 * so we make this our minimum, even though 32 bits is still
1736 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001737 status = PSA_ERROR_NOT_SUPPORTED;
1738 }
1739 else if( truncated > operation->mac_size )
1740 {
1741 /* It's impossible to "truncate" to a larger length. */
1742 status = PSA_ERROR_INVALID_ARGUMENT;
1743 }
1744 else
1745 operation->mac_size = truncated;
1746
Gilles Peskinefbfac682018-07-08 20:51:54 +02001747exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001748 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001749 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001750 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001751 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001752 else
1753 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001754 operation->key_set = 1;
1755 }
1756 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001757}
1758
Gilles Peskine89167cb2018-07-08 20:12:23 +02001759psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1760 psa_key_slot_t key,
1761 psa_algorithm_t alg )
1762{
1763 return( psa_mac_setup( operation, key, alg, 1 ) );
1764}
1765
1766psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1767 psa_key_slot_t key,
1768 psa_algorithm_t alg )
1769{
1770 return( psa_mac_setup( operation, key, alg, 0 ) );
1771}
1772
Gilles Peskine8c9def32018-02-08 10:02:12 +01001773psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1774 const uint8_t *input,
1775 size_t input_length )
1776{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001777 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001778 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001779 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001780 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001781 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001782 operation->has_input = 1;
1783
Gilles Peskine8c9def32018-02-08 10:02:12 +01001784#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001785 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001786 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001787 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1788 input, input_length );
1789 status = mbedtls_to_psa_error( ret );
1790 }
1791 else
1792#endif /* MBEDTLS_CMAC_C */
1793#if defined(MBEDTLS_MD_C)
1794 if( PSA_ALG_IS_HMAC( operation->alg ) )
1795 {
1796 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1797 input_length );
1798 }
1799 else
1800#endif /* MBEDTLS_MD_C */
1801 {
1802 /* This shouldn't happen if `operation` was initialized by
1803 * a setup function. */
1804 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001805 }
1806
Gilles Peskinefbfac682018-07-08 20:51:54 +02001807cleanup:
1808 if( status != PSA_SUCCESS )
1809 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001810 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001811}
1812
Gilles Peskine01126fa2018-07-12 17:04:55 +02001813#if defined(MBEDTLS_MD_C)
1814static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1815 uint8_t *mac,
1816 size_t mac_size )
1817{
1818 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1819 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1820 size_t hash_size = 0;
1821 size_t block_size = psa_get_hash_block_size( hash_alg );
1822 psa_status_t status;
1823
Gilles Peskine01126fa2018-07-12 17:04:55 +02001824 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1825 if( status != PSA_SUCCESS )
1826 return( status );
1827 /* From here on, tmp needs to be wiped. */
1828
1829 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1830 if( status != PSA_SUCCESS )
1831 goto exit;
1832
1833 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1834 if( status != PSA_SUCCESS )
1835 goto exit;
1836
1837 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1838 if( status != PSA_SUCCESS )
1839 goto exit;
1840
Gilles Peskined911eb72018-08-14 15:18:45 +02001841 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1842 if( status != PSA_SUCCESS )
1843 goto exit;
1844
1845 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001846
1847exit:
1848 mbedtls_zeroize( tmp, hash_size );
1849 return( status );
1850}
1851#endif /* MBEDTLS_MD_C */
1852
mohammad16036df908f2018-04-02 08:34:15 -07001853static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001854 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001855 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001856{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001857 if( ! operation->key_set )
1858 return( PSA_ERROR_BAD_STATE );
1859 if( operation->iv_required && ! operation->iv_set )
1860 return( PSA_ERROR_BAD_STATE );
1861
Gilles Peskine8c9def32018-02-08 10:02:12 +01001862 if( mac_size < operation->mac_size )
1863 return( PSA_ERROR_BUFFER_TOO_SMALL );
1864
Gilles Peskine8c9def32018-02-08 10:02:12 +01001865#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001866 if( operation->alg == PSA_ALG_CMAC )
1867 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001868 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1869 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1870 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02001871 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02001872 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001873 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001874 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001875 else
1876#endif /* MBEDTLS_CMAC_C */
1877#if defined(MBEDTLS_MD_C)
1878 if( PSA_ALG_IS_HMAC( operation->alg ) )
1879 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001880 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001881 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001882 }
1883 else
1884#endif /* MBEDTLS_MD_C */
1885 {
1886 /* This shouldn't happen if `operation` was initialized by
1887 * a setup function. */
1888 return( PSA_ERROR_BAD_STATE );
1889 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001890}
1891
Gilles Peskineacd4be32018-07-08 19:56:25 +02001892psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1893 uint8_t *mac,
1894 size_t mac_size,
1895 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001896{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001897 psa_status_t status;
1898
1899 /* Fill the output buffer with something that isn't a valid mac
1900 * (barring an attack on the mac and deliberately-crafted input),
1901 * in case the caller doesn't check the return status properly. */
1902 *mac_length = mac_size;
1903 /* If mac_size is 0 then mac may be NULL and then the
1904 * call to memset would have undefined behavior. */
1905 if( mac_size != 0 )
1906 memset( mac, '!', mac_size );
1907
Gilles Peskine89167cb2018-07-08 20:12:23 +02001908 if( ! operation->is_sign )
1909 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001910 status = PSA_ERROR_BAD_STATE;
1911 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001912 }
mohammad16036df908f2018-04-02 08:34:15 -07001913
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001914 status = psa_mac_finish_internal( operation, mac, mac_size );
1915
1916cleanup:
1917 if( status == PSA_SUCCESS )
1918 {
1919 status = psa_mac_abort( operation );
1920 if( status == PSA_SUCCESS )
1921 *mac_length = operation->mac_size;
1922 else
1923 memset( mac, '!', mac_size );
1924 }
1925 else
1926 psa_mac_abort( operation );
1927 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001928}
1929
Gilles Peskineacd4be32018-07-08 19:56:25 +02001930psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1931 const uint8_t *mac,
1932 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001933{
Gilles Peskine828ed142018-06-18 23:25:51 +02001934 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001935 psa_status_t status;
1936
Gilles Peskine89167cb2018-07-08 20:12:23 +02001937 if( operation->is_sign )
1938 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001939 status = PSA_ERROR_BAD_STATE;
1940 goto cleanup;
1941 }
1942 if( operation->mac_size != mac_length )
1943 {
1944 status = PSA_ERROR_INVALID_SIGNATURE;
1945 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001946 }
mohammad16036df908f2018-04-02 08:34:15 -07001947
1948 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001949 actual_mac, sizeof( actual_mac ) );
1950
1951 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1952 status = PSA_ERROR_INVALID_SIGNATURE;
1953
1954cleanup:
1955 if( status == PSA_SUCCESS )
1956 status = psa_mac_abort( operation );
1957 else
1958 psa_mac_abort( operation );
1959
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02001960 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02001961
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001962 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001963}
1964
1965
Gilles Peskine20035e32018-02-03 22:44:14 +01001966
Gilles Peskine20035e32018-02-03 22:44:14 +01001967/****************************************************************/
1968/* Asymmetric cryptography */
1969/****************************************************************/
1970
Gilles Peskine2b450e32018-06-27 15:42:46 +02001971#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001972/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001973 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001974static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1975 size_t hash_length,
1976 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001977{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001978 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001979 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001980 *md_alg = mbedtls_md_get_type( md_info );
1981
1982 /* The Mbed TLS RSA module uses an unsigned int for hash length
1983 * parameters. Validate that it fits so that we don't risk an
1984 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001985#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001986 if( hash_length > UINT_MAX )
1987 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001988#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001989
1990#if defined(MBEDTLS_PKCS1_V15)
1991 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1992 * must be correct. */
1993 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1994 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001995 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001996 if( md_info == NULL )
1997 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001998 if( mbedtls_md_get_size( md_info ) != hash_length )
1999 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002000 }
2001#endif /* MBEDTLS_PKCS1_V15 */
2002
2003#if defined(MBEDTLS_PKCS1_V21)
2004 /* PSS requires a hash internally. */
2005 if( PSA_ALG_IS_RSA_PSS( alg ) )
2006 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002007 if( md_info == NULL )
2008 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002009 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002010#endif /* MBEDTLS_PKCS1_V21 */
2011
Gilles Peskine61b91d42018-06-08 16:09:36 +02002012 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002013}
2014
Gilles Peskine2b450e32018-06-27 15:42:46 +02002015static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2016 psa_algorithm_t alg,
2017 const uint8_t *hash,
2018 size_t hash_length,
2019 uint8_t *signature,
2020 size_t signature_size,
2021 size_t *signature_length )
2022{
2023 psa_status_t status;
2024 int ret;
2025 mbedtls_md_type_t md_alg;
2026
2027 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2028 if( status != PSA_SUCCESS )
2029 return( status );
2030
Gilles Peskine630a18a2018-06-29 17:49:35 +02002031 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002032 return( PSA_ERROR_BUFFER_TOO_SMALL );
2033
2034#if defined(MBEDTLS_PKCS1_V15)
2035 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2036 {
2037 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2038 MBEDTLS_MD_NONE );
2039 ret = mbedtls_rsa_pkcs1_sign( rsa,
2040 mbedtls_ctr_drbg_random,
2041 &global_data.ctr_drbg,
2042 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002043 md_alg,
2044 (unsigned int) hash_length,
2045 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002046 signature );
2047 }
2048 else
2049#endif /* MBEDTLS_PKCS1_V15 */
2050#if defined(MBEDTLS_PKCS1_V21)
2051 if( PSA_ALG_IS_RSA_PSS( alg ) )
2052 {
2053 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2054 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2055 mbedtls_ctr_drbg_random,
2056 &global_data.ctr_drbg,
2057 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002058 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002059 (unsigned int) hash_length,
2060 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002061 signature );
2062 }
2063 else
2064#endif /* MBEDTLS_PKCS1_V21 */
2065 {
2066 return( PSA_ERROR_INVALID_ARGUMENT );
2067 }
2068
2069 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002070 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002071 return( mbedtls_to_psa_error( ret ) );
2072}
2073
2074static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2075 psa_algorithm_t alg,
2076 const uint8_t *hash,
2077 size_t hash_length,
2078 const uint8_t *signature,
2079 size_t signature_length )
2080{
2081 psa_status_t status;
2082 int ret;
2083 mbedtls_md_type_t md_alg;
2084
2085 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2086 if( status != PSA_SUCCESS )
2087 return( status );
2088
Gilles Peskine630a18a2018-06-29 17:49:35 +02002089 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002090 return( PSA_ERROR_BUFFER_TOO_SMALL );
2091
2092#if defined(MBEDTLS_PKCS1_V15)
2093 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2094 {
2095 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2096 MBEDTLS_MD_NONE );
2097 ret = mbedtls_rsa_pkcs1_verify( rsa,
2098 mbedtls_ctr_drbg_random,
2099 &global_data.ctr_drbg,
2100 MBEDTLS_RSA_PUBLIC,
2101 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002102 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002103 hash,
2104 signature );
2105 }
2106 else
2107#endif /* MBEDTLS_PKCS1_V15 */
2108#if defined(MBEDTLS_PKCS1_V21)
2109 if( PSA_ALG_IS_RSA_PSS( alg ) )
2110 {
2111 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2112 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2113 mbedtls_ctr_drbg_random,
2114 &global_data.ctr_drbg,
2115 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002116 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002117 (unsigned int) hash_length,
2118 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002119 signature );
2120 }
2121 else
2122#endif /* MBEDTLS_PKCS1_V21 */
2123 {
2124 return( PSA_ERROR_INVALID_ARGUMENT );
2125 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002126
2127 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2128 * the rest of the signature is invalid". This has little use in
2129 * practice and PSA doesn't report this distinction. */
2130 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2131 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002132 return( mbedtls_to_psa_error( ret ) );
2133}
2134#endif /* MBEDTLS_RSA_C */
2135
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002136#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002137/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2138 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2139 * (even though these functions don't modify it). */
2140static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2141 psa_algorithm_t alg,
2142 const uint8_t *hash,
2143 size_t hash_length,
2144 uint8_t *signature,
2145 size_t signature_size,
2146 size_t *signature_length )
2147{
2148 int ret;
2149 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002150 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002151 mbedtls_mpi_init( &r );
2152 mbedtls_mpi_init( &s );
2153
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002154 if( signature_size < 2 * curve_bytes )
2155 {
2156 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2157 goto cleanup;
2158 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002159
2160 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2161 {
2162 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2163 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2164 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2165 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2166 hash, hash_length,
2167 md_alg ) );
2168 }
2169 else
2170 {
2171 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2172 hash, hash_length,
2173 mbedtls_ctr_drbg_random,
2174 &global_data.ctr_drbg ) );
2175 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002176
2177 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2178 signature,
2179 curve_bytes ) );
2180 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2181 signature + curve_bytes,
2182 curve_bytes ) );
2183
2184cleanup:
2185 mbedtls_mpi_free( &r );
2186 mbedtls_mpi_free( &s );
2187 if( ret == 0 )
2188 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002189 return( mbedtls_to_psa_error( ret ) );
2190}
2191
2192static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2193 const uint8_t *hash,
2194 size_t hash_length,
2195 const uint8_t *signature,
2196 size_t signature_length )
2197{
2198 int ret;
2199 mbedtls_mpi r, s;
2200 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2201 mbedtls_mpi_init( &r );
2202 mbedtls_mpi_init( &s );
2203
2204 if( signature_length != 2 * curve_bytes )
2205 return( PSA_ERROR_INVALID_SIGNATURE );
2206
2207 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2208 signature,
2209 curve_bytes ) );
2210 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2211 signature + curve_bytes,
2212 curve_bytes ) );
2213
2214 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2215 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002216
2217cleanup:
2218 mbedtls_mpi_free( &r );
2219 mbedtls_mpi_free( &s );
2220 return( mbedtls_to_psa_error( ret ) );
2221}
2222#endif /* MBEDTLS_ECDSA_C */
2223
Gilles Peskine61b91d42018-06-08 16:09:36 +02002224psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2225 psa_algorithm_t alg,
2226 const uint8_t *hash,
2227 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002228 uint8_t *signature,
2229 size_t signature_size,
2230 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002231{
2232 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002233 psa_status_t status;
2234
2235 *signature_length = signature_size;
2236
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002237 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2238 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002239 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002240 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002241 {
2242 status = PSA_ERROR_INVALID_ARGUMENT;
2243 goto exit;
2244 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002245
Gilles Peskine20035e32018-02-03 22:44:14 +01002246#if defined(MBEDTLS_RSA_C)
2247 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2248 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002249 status = psa_rsa_sign( slot->data.rsa,
2250 alg,
2251 hash, hash_length,
2252 signature, signature_size,
2253 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002254 }
2255 else
2256#endif /* defined(MBEDTLS_RSA_C) */
2257#if defined(MBEDTLS_ECP_C)
2258 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2259 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002260#if defined(MBEDTLS_ECDSA_C)
2261 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002262 status = psa_ecdsa_sign( slot->data.ecp,
2263 alg,
2264 hash, hash_length,
2265 signature, signature_size,
2266 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002267 else
2268#endif /* defined(MBEDTLS_ECDSA_C) */
2269 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002270 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002271 }
itayzafrir5c753392018-05-08 11:18:38 +03002272 }
2273 else
2274#endif /* defined(MBEDTLS_ECP_C) */
2275 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002276 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002277 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002278
2279exit:
2280 /* Fill the unused part of the output buffer (the whole buffer on error,
2281 * the trailing part on success) with something that isn't a valid mac
2282 * (barring an attack on the mac and deliberately-crafted input),
2283 * in case the caller doesn't check the return status properly. */
2284 if( status == PSA_SUCCESS )
2285 memset( signature + *signature_length, '!',
2286 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002287 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002288 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002289 /* If signature_size is 0 then we have nothing to do. We must not call
2290 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002291 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002292}
2293
2294psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2295 psa_algorithm_t alg,
2296 const uint8_t *hash,
2297 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002298 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002299 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002300{
2301 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002302 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002303
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002304 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2305 if( status != PSA_SUCCESS )
2306 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002307
Gilles Peskine61b91d42018-06-08 16:09:36 +02002308#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002309 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002310 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002311 return( psa_rsa_verify( slot->data.rsa,
2312 alg,
2313 hash, hash_length,
2314 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002315 }
2316 else
2317#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002318#if defined(MBEDTLS_ECP_C)
2319 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2320 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002321#if defined(MBEDTLS_ECDSA_C)
2322 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002323 return( psa_ecdsa_verify( slot->data.ecp,
2324 hash, hash_length,
2325 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002326 else
2327#endif /* defined(MBEDTLS_ECDSA_C) */
2328 {
2329 return( PSA_ERROR_INVALID_ARGUMENT );
2330 }
itayzafrir5c753392018-05-08 11:18:38 +03002331 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002332 else
2333#endif /* defined(MBEDTLS_ECP_C) */
2334 {
2335 return( PSA_ERROR_NOT_SUPPORTED );
2336 }
2337}
2338
Gilles Peskine072ac562018-06-30 00:21:29 +02002339#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2340static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2341 mbedtls_rsa_context *rsa )
2342{
2343 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2344 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2345 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2346 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2347}
2348#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2349
Gilles Peskine61b91d42018-06-08 16:09:36 +02002350psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2351 psa_algorithm_t alg,
2352 const uint8_t *input,
2353 size_t input_length,
2354 const uint8_t *salt,
2355 size_t salt_length,
2356 uint8_t *output,
2357 size_t output_size,
2358 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002359{
2360 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002361 psa_status_t status;
2362
Darryl Green5cc689a2018-07-24 15:34:10 +01002363 (void) input;
2364 (void) input_length;
2365 (void) salt;
2366 (void) output;
2367 (void) output_size;
2368
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002369 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002370
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002371 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2372 return( PSA_ERROR_INVALID_ARGUMENT );
2373
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002374 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2375 if( status != PSA_SUCCESS )
2376 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002377 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2378 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002379 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002380
2381#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002382 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002383 {
2384 mbedtls_rsa_context *rsa = slot->data.rsa;
2385 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002386 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002387 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002388#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002389 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002390 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002391 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2392 mbedtls_ctr_drbg_random,
2393 &global_data.ctr_drbg,
2394 MBEDTLS_RSA_PUBLIC,
2395 input_length,
2396 input,
2397 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002398 }
2399 else
2400#endif /* MBEDTLS_PKCS1_V15 */
2401#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002402 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002403 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002404 psa_rsa_oaep_set_padding_mode( alg, rsa );
2405 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2406 mbedtls_ctr_drbg_random,
2407 &global_data.ctr_drbg,
2408 MBEDTLS_RSA_PUBLIC,
2409 salt, salt_length,
2410 input_length,
2411 input,
2412 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002413 }
2414 else
2415#endif /* MBEDTLS_PKCS1_V21 */
2416 {
2417 return( PSA_ERROR_INVALID_ARGUMENT );
2418 }
2419 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002420 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002421 return( mbedtls_to_psa_error( ret ) );
2422 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002423 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002424#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002425 {
2426 return( PSA_ERROR_NOT_SUPPORTED );
2427 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002428}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002429
Gilles Peskine61b91d42018-06-08 16:09:36 +02002430psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2431 psa_algorithm_t alg,
2432 const uint8_t *input,
2433 size_t input_length,
2434 const uint8_t *salt,
2435 size_t salt_length,
2436 uint8_t *output,
2437 size_t output_size,
2438 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002439{
2440 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002441 psa_status_t status;
2442
Darryl Green5cc689a2018-07-24 15:34:10 +01002443 (void) input;
2444 (void) input_length;
2445 (void) salt;
2446 (void) output;
2447 (void) output_size;
2448
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002449 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002450
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002451 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2452 return( PSA_ERROR_INVALID_ARGUMENT );
2453
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002454 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2455 if( status != PSA_SUCCESS )
2456 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002457 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2458 return( PSA_ERROR_INVALID_ARGUMENT );
2459
2460#if defined(MBEDTLS_RSA_C)
2461 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2462 {
2463 mbedtls_rsa_context *rsa = slot->data.rsa;
2464 int ret;
2465
Gilles Peskine630a18a2018-06-29 17:49:35 +02002466 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002467 return( PSA_ERROR_INVALID_ARGUMENT );
2468
2469#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002470 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002471 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002472 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2473 mbedtls_ctr_drbg_random,
2474 &global_data.ctr_drbg,
2475 MBEDTLS_RSA_PRIVATE,
2476 output_length,
2477 input,
2478 output,
2479 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002480 }
2481 else
2482#endif /* MBEDTLS_PKCS1_V15 */
2483#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002484 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002485 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002486 psa_rsa_oaep_set_padding_mode( alg, rsa );
2487 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2488 mbedtls_ctr_drbg_random,
2489 &global_data.ctr_drbg,
2490 MBEDTLS_RSA_PRIVATE,
2491 salt, salt_length,
2492 output_length,
2493 input,
2494 output,
2495 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002496 }
2497 else
2498#endif /* MBEDTLS_PKCS1_V21 */
2499 {
2500 return( PSA_ERROR_INVALID_ARGUMENT );
2501 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002502
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002503 return( mbedtls_to_psa_error( ret ) );
2504 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002505 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002506#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002507 {
2508 return( PSA_ERROR_NOT_SUPPORTED );
2509 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002510}
Gilles Peskine20035e32018-02-03 22:44:14 +01002511
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002512
2513
mohammad1603503973b2018-03-12 15:59:30 +02002514/****************************************************************/
2515/* Symmetric cryptography */
2516/****************************************************************/
2517
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002518/* Initialize the cipher operation structure. Once this function has been
2519 * called, psa_cipher_abort can run and will do the right thing. */
2520static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2521 psa_algorithm_t alg )
2522{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002523 if( ! PSA_ALG_IS_CIPHER( alg ) )
2524 {
2525 memset( operation, 0, sizeof( *operation ) );
2526 return( PSA_ERROR_INVALID_ARGUMENT );
2527 }
2528
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002529 operation->alg = alg;
2530 operation->key_set = 0;
2531 operation->iv_set = 0;
2532 operation->iv_required = 1;
2533 operation->iv_size = 0;
2534 operation->block_size = 0;
2535 mbedtls_cipher_init( &operation->ctx.cipher );
2536 return( PSA_SUCCESS );
2537}
2538
Gilles Peskinee553c652018-06-04 16:22:46 +02002539static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2540 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002541 psa_algorithm_t alg,
2542 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002543{
2544 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2545 psa_status_t status;
2546 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002547 size_t key_bits;
2548 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002549 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2550 PSA_KEY_USAGE_ENCRYPT :
2551 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002552
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002553 status = psa_cipher_init( operation, alg );
2554 if( status != PSA_SUCCESS )
2555 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002556
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002557 status = psa_get_key_from_slot( key, &slot, usage, alg);
2558 if( status != PSA_SUCCESS )
2559 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002560 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002561
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002562 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002563 if( cipher_info == NULL )
2564 return( PSA_ERROR_NOT_SUPPORTED );
2565
mohammad1603503973b2018-03-12 15:59:30 +02002566 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002567 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002568 {
2569 psa_cipher_abort( operation );
2570 return( mbedtls_to_psa_error( ret ) );
2571 }
2572
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002573#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002574 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002575 {
2576 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2577 unsigned char keys[24];
2578 memcpy( keys, slot->data.raw.data, 16 );
2579 memcpy( keys + 16, slot->data.raw.data, 8 );
2580 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2581 keys,
2582 192, cipher_operation );
2583 }
2584 else
2585#endif
2586 {
2587 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2588 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002589 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002590 }
Moran Peker41deec42018-04-04 15:43:05 +03002591 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002592 {
2593 psa_cipher_abort( operation );
2594 return( mbedtls_to_psa_error( ret ) );
2595 }
2596
mohammad16038481e742018-03-18 13:57:31 +02002597#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002598 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002599 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002600 case PSA_ALG_CBC_NO_PADDING:
2601 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2602 MBEDTLS_PADDING_NONE );
2603 break;
2604 case PSA_ALG_CBC_PKCS7:
2605 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2606 MBEDTLS_PADDING_PKCS7 );
2607 break;
2608 default:
2609 /* The algorithm doesn't involve padding. */
2610 ret = 0;
2611 break;
2612 }
2613 if( ret != 0 )
2614 {
2615 psa_cipher_abort( operation );
2616 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002617 }
2618#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2619
mohammad1603503973b2018-03-12 15:59:30 +02002620 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002621 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2622 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2623 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002624 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002625 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002626 }
mohammad1603503973b2018-03-12 15:59:30 +02002627
Moran Peker395db872018-05-31 14:07:14 +03002628 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002629}
2630
Gilles Peskinefe119512018-07-08 21:39:34 +02002631psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2632 psa_key_slot_t key,
2633 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002634{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002635 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002636}
2637
Gilles Peskinefe119512018-07-08 21:39:34 +02002638psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2639 psa_key_slot_t key,
2640 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002641{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002642 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002643}
2644
Gilles Peskinefe119512018-07-08 21:39:34 +02002645psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2646 unsigned char *iv,
2647 size_t iv_size,
2648 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002649{
itayzafrir534bd7c2018-08-02 13:56:32 +03002650 psa_status_t status;
2651 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002652 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002653 {
2654 status = PSA_ERROR_BAD_STATE;
2655 goto exit;
2656 }
Moran Peker41deec42018-04-04 15:43:05 +03002657 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002658 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002659 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002660 goto exit;
2661 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002662 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2663 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002664 if( ret != 0 )
2665 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002666 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002667 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002668 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002669
mohammad16038481e742018-03-18 13:57:31 +02002670 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002671 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002672
Moran Peker395db872018-05-31 14:07:14 +03002673exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002674 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002675 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002676 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002677}
2678
Gilles Peskinefe119512018-07-08 21:39:34 +02002679psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2680 const unsigned char *iv,
2681 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002682{
itayzafrir534bd7c2018-08-02 13:56:32 +03002683 psa_status_t status;
2684 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002685 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002686 {
2687 status = PSA_ERROR_BAD_STATE;
2688 goto exit;
2689 }
Moran Pekera28258c2018-05-29 16:25:04 +03002690 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002691 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002692 status = PSA_ERROR_INVALID_ARGUMENT;
2693 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002694 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002695 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2696 status = mbedtls_to_psa_error( ret );
2697exit:
2698 if( status == PSA_SUCCESS )
2699 operation->iv_set = 1;
2700 else
mohammad1603503973b2018-03-12 15:59:30 +02002701 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002702 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002703}
2704
Gilles Peskinee553c652018-06-04 16:22:46 +02002705psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2706 const uint8_t *input,
2707 size_t input_length,
2708 unsigned char *output,
2709 size_t output_size,
2710 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002711{
itayzafrir534bd7c2018-08-02 13:56:32 +03002712 psa_status_t status;
2713 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002714 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002715 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002716 {
2717 /* Take the unprocessed partial block left over from previous
2718 * update calls, if any, plus the input to this call. Remove
2719 * the last partial block, if any. You get the data that will be
2720 * output in this call. */
2721 expected_output_size =
2722 ( operation->ctx.cipher.unprocessed_len + input_length )
2723 / operation->block_size * operation->block_size;
2724 }
2725 else
2726 {
2727 expected_output_size = input_length;
2728 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002729
Gilles Peskine89d789c2018-06-04 17:17:16 +02002730 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002731 {
2732 status = PSA_ERROR_BUFFER_TOO_SMALL;
2733 goto exit;
2734 }
mohammad160382759612018-03-12 18:16:40 +02002735
mohammad1603503973b2018-03-12 15:59:30 +02002736 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002737 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002738 status = mbedtls_to_psa_error( ret );
2739exit:
2740 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002741 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002742 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002743}
2744
Gilles Peskinee553c652018-06-04 16:22:46 +02002745psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2746 uint8_t *output,
2747 size_t output_size,
2748 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002749{
Janos Follath315b51c2018-07-09 16:04:51 +01002750 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2751 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002752 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002753
mohammad1603503973b2018-03-12 15:59:30 +02002754 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002755 {
Janos Follath315b51c2018-07-09 16:04:51 +01002756 status = PSA_ERROR_BAD_STATE;
2757 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002758 }
2759 if( operation->iv_required && ! operation->iv_set )
2760 {
Janos Follath315b51c2018-07-09 16:04:51 +01002761 status = PSA_ERROR_BAD_STATE;
2762 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002763 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002764
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002765 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002766 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2767 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002768 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002769 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002770 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002771 }
2772
Janos Follath315b51c2018-07-09 16:04:51 +01002773 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2774 temp_output_buffer,
2775 output_length );
2776 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002777 {
Janos Follath315b51c2018-07-09 16:04:51 +01002778 status = mbedtls_to_psa_error( cipher_ret );
2779 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002780 }
Janos Follath315b51c2018-07-09 16:04:51 +01002781
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002782 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002783 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002784 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002785 memcpy( output, temp_output_buffer, *output_length );
2786 else
2787 {
Janos Follath315b51c2018-07-09 16:04:51 +01002788 status = PSA_ERROR_BUFFER_TOO_SMALL;
2789 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002790 }
mohammad1603503973b2018-03-12 15:59:30 +02002791
Janos Follath279ab8e2018-07-09 16:13:21 +01002792 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002793 status = psa_cipher_abort( operation );
2794
2795 return( status );
2796
2797error:
2798
2799 *output_length = 0;
2800
Janos Follath279ab8e2018-07-09 16:13:21 +01002801 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002802 (void) psa_cipher_abort( operation );
2803
2804 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002805}
2806
Gilles Peskinee553c652018-06-04 16:22:46 +02002807psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2808{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002809 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002810 {
2811 /* The object has (apparently) been initialized but it is not
2812 * in use. It's ok to call abort on such an object, and there's
2813 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002814 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002815 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002816
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002817 /* Sanity check (shouldn't happen: operation->alg should
2818 * always have been initialized to a valid value). */
2819 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2820 return( PSA_ERROR_BAD_STATE );
2821
mohammad1603503973b2018-03-12 15:59:30 +02002822 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002823
Moran Peker41deec42018-04-04 15:43:05 +03002824 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002825 operation->key_set = 0;
2826 operation->iv_set = 0;
2827 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002828 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002829 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002830
Moran Peker395db872018-05-31 14:07:14 +03002831 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002832}
2833
Gilles Peskinea0655c32018-04-30 17:06:50 +02002834
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002835
mohammad16038cc1cee2018-03-28 01:21:33 +03002836/****************************************************************/
2837/* Key Policy */
2838/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002839
mohammad160327010052018-07-03 13:16:15 +03002840#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002841void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002842{
Gilles Peskine803ce742018-06-18 16:07:14 +02002843 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002844}
2845
Gilles Peskine2d277862018-06-18 15:41:12 +02002846void psa_key_policy_set_usage( psa_key_policy_t *policy,
2847 psa_key_usage_t usage,
2848 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002849{
mohammad16034eed7572018-03-28 05:14:59 -07002850 policy->usage = usage;
2851 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002852}
2853
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002854psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002855{
mohammad16036df908f2018-04-02 08:34:15 -07002856 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002857}
2858
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002859psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002860{
mohammad16036df908f2018-04-02 08:34:15 -07002861 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002862}
mohammad160327010052018-07-03 13:16:15 +03002863#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002864
Gilles Peskine2d277862018-06-18 15:41:12 +02002865psa_status_t psa_set_key_policy( psa_key_slot_t key,
2866 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002867{
2868 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002869 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002870
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002871 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002872 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002873
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002874 status = psa_get_empty_key_slot( key, &slot );
2875 if( status != PSA_SUCCESS )
2876 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002877
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002878 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2879 PSA_KEY_USAGE_ENCRYPT |
2880 PSA_KEY_USAGE_DECRYPT |
2881 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002882 PSA_KEY_USAGE_VERIFY |
2883 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002884 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002885
mohammad16036df908f2018-04-02 08:34:15 -07002886 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002887
2888 return( PSA_SUCCESS );
2889}
2890
Gilles Peskine2d277862018-06-18 15:41:12 +02002891psa_status_t psa_get_key_policy( psa_key_slot_t key,
2892 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002893{
2894 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002895 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002896
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002897 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002898 return( PSA_ERROR_INVALID_ARGUMENT );
2899
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002900 status = psa_get_key_slot( key, &slot );
2901 if( status != PSA_SUCCESS )
2902 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002903
mohammad16036df908f2018-04-02 08:34:15 -07002904 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002905
2906 return( PSA_SUCCESS );
2907}
Gilles Peskine20035e32018-02-03 22:44:14 +01002908
Gilles Peskinea0655c32018-04-30 17:06:50 +02002909
2910
mohammad1603804cd712018-03-20 22:44:08 +02002911/****************************************************************/
2912/* Key Lifetime */
2913/****************************************************************/
2914
Gilles Peskine2d277862018-06-18 15:41:12 +02002915psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2916 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002917{
2918 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002919 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002920
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002921 status = psa_get_key_slot( key, &slot );
2922 if( status != PSA_SUCCESS )
2923 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002924
mohammad1603804cd712018-03-20 22:44:08 +02002925 *lifetime = slot->lifetime;
2926
2927 return( PSA_SUCCESS );
2928}
2929
Gilles Peskine2d277862018-06-18 15:41:12 +02002930psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002931 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002932{
2933 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002934 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002935
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002936 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2937 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002938 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2939 return( PSA_ERROR_INVALID_ARGUMENT );
2940
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002941 status = psa_get_empty_key_slot( key, &slot );
2942 if( status != PSA_SUCCESS )
2943 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002944
Moran Pekerd7326592018-05-29 16:56:39 +03002945 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002946 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002947
mohammad1603060ad8a2018-03-20 14:28:38 -07002948 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002949
2950 return( PSA_SUCCESS );
2951}
2952
Gilles Peskine20035e32018-02-03 22:44:14 +01002953
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002954
mohammad16035955c982018-04-26 00:53:03 +03002955/****************************************************************/
2956/* AEAD */
2957/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002958
Gilles Peskineedf9a652018-08-17 18:11:56 +02002959typedef struct
2960{
2961 key_slot_t *slot;
2962 const mbedtls_cipher_info_t *cipher_info;
2963 union
2964 {
2965#if defined(MBEDTLS_CCM_C)
2966 mbedtls_ccm_context ccm;
2967#endif /* MBEDTLS_CCM_C */
2968#if defined(MBEDTLS_GCM_C)
2969 mbedtls_gcm_context gcm;
2970#endif /* MBEDTLS_GCM_C */
2971 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002972 psa_algorithm_t core_alg;
2973 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002974 uint8_t tag_length;
2975} aead_operation_t;
2976
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002977static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002978{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002979 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002980 {
2981#if defined(MBEDTLS_CCM_C)
2982 case PSA_ALG_CCM:
2983 mbedtls_ccm_free( &operation->ctx.ccm );
2984 break;
2985#endif /* MBEDTLS_CCM_C */
2986#if defined(MBEDTLS_CCM_C)
2987 case PSA_ALG_GCM:
2988 mbedtls_gcm_free( &operation->ctx.gcm );
2989 break;
2990#endif /* MBEDTLS_GCM_C */
2991 }
2992}
2993
2994static psa_status_t psa_aead_setup( aead_operation_t *operation,
2995 psa_key_slot_t key,
2996 psa_key_usage_t usage,
2997 psa_algorithm_t alg )
2998{
2999 psa_status_t status;
3000 size_t key_bits;
3001 mbedtls_cipher_id_t cipher_id;
3002
3003 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3004 if( status != PSA_SUCCESS )
3005 return( status );
3006
3007 key_bits = psa_get_key_bits( operation->slot );
3008
3009 operation->cipher_info =
3010 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3011 &cipher_id );
3012 if( operation->cipher_info == NULL )
3013 return( PSA_ERROR_NOT_SUPPORTED );
3014
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003015 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003016 {
3017#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003018 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3019 operation->core_alg = PSA_ALG_CCM;
3020 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003021 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3022 return( PSA_ERROR_INVALID_ARGUMENT );
3023 mbedtls_ccm_init( &operation->ctx.ccm );
3024 status = mbedtls_to_psa_error(
3025 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3026 operation->slot->data.raw.data,
3027 (unsigned int) key_bits ) );
3028 if( status != 0 )
3029 goto cleanup;
3030 break;
3031#endif /* MBEDTLS_CCM_C */
3032
3033#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003034 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3035 operation->core_alg = PSA_ALG_GCM;
3036 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003037 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3038 return( PSA_ERROR_INVALID_ARGUMENT );
3039 mbedtls_gcm_init( &operation->ctx.gcm );
3040 status = mbedtls_to_psa_error(
3041 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3042 operation->slot->data.raw.data,
3043 (unsigned int) key_bits ) );
3044 break;
3045#endif /* MBEDTLS_GCM_C */
3046
3047 default:
3048 return( PSA_ERROR_NOT_SUPPORTED );
3049 }
3050
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003051 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3052 {
3053 status = PSA_ERROR_INVALID_ARGUMENT;
3054 goto cleanup;
3055 }
3056 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3057 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3058 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3059 * In both cases, mbedtls_xxx will validate the tag length below. */
3060
Gilles Peskineedf9a652018-08-17 18:11:56 +02003061 return( PSA_SUCCESS );
3062
3063cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003064 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003065 return( status );
3066}
3067
mohammad16035955c982018-04-26 00:53:03 +03003068psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3069 psa_algorithm_t alg,
3070 const uint8_t *nonce,
3071 size_t nonce_length,
3072 const uint8_t *additional_data,
3073 size_t additional_data_length,
3074 const uint8_t *plaintext,
3075 size_t plaintext_length,
3076 uint8_t *ciphertext,
3077 size_t ciphertext_size,
3078 size_t *ciphertext_length )
3079{
mohammad16035955c982018-04-26 00:53:03 +03003080 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003081 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003082 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003083
mohammad1603f08a5502018-06-03 15:05:47 +03003084 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003085
Gilles Peskineedf9a652018-08-17 18:11:56 +02003086 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003087 if( status != PSA_SUCCESS )
3088 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003089
Gilles Peskineedf9a652018-08-17 18:11:56 +02003090 /* For all currently supported modes, the tag is at the end of the
3091 * ciphertext. */
3092 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3093 {
3094 status = PSA_ERROR_BUFFER_TOO_SMALL;
3095 goto exit;
3096 }
3097 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003098
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003099 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003100 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003101 status = mbedtls_to_psa_error(
3102 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3103 MBEDTLS_GCM_ENCRYPT,
3104 plaintext_length,
3105 nonce, nonce_length,
3106 additional_data, additional_data_length,
3107 plaintext, ciphertext,
3108 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003109 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003110 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003111 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003112 status = mbedtls_to_psa_error(
3113 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3114 plaintext_length,
3115 nonce, nonce_length,
3116 additional_data,
3117 additional_data_length,
3118 plaintext, ciphertext,
3119 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003120 }
mohammad16035c8845f2018-05-09 05:40:09 -07003121 else
3122 {
mohammad1603554faad2018-06-03 15:07:38 +03003123 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003124 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003125
Gilles Peskineedf9a652018-08-17 18:11:56 +02003126 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3127 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003128
Gilles Peskineedf9a652018-08-17 18:11:56 +02003129exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003130 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003131 if( status == PSA_SUCCESS )
3132 *ciphertext_length = plaintext_length + operation.tag_length;
3133 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003134}
3135
Gilles Peskineee652a32018-06-01 19:23:52 +02003136/* Locate the tag in a ciphertext buffer containing the encrypted data
3137 * followed by the tag. Return the length of the part preceding the tag in
3138 * *plaintext_length. This is the size of the plaintext in modes where
3139 * the encrypted data has the same size as the plaintext, such as
3140 * CCM and GCM. */
3141static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3142 const uint8_t *ciphertext,
3143 size_t ciphertext_length,
3144 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003145 const uint8_t **p_tag )
3146{
3147 size_t payload_length;
3148 if( tag_length > ciphertext_length )
3149 return( PSA_ERROR_INVALID_ARGUMENT );
3150 payload_length = ciphertext_length - tag_length;
3151 if( payload_length > plaintext_size )
3152 return( PSA_ERROR_BUFFER_TOO_SMALL );
3153 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003154 return( PSA_SUCCESS );
3155}
3156
mohammad16035955c982018-04-26 00:53:03 +03003157psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3158 psa_algorithm_t alg,
3159 const uint8_t *nonce,
3160 size_t nonce_length,
3161 const uint8_t *additional_data,
3162 size_t additional_data_length,
3163 const uint8_t *ciphertext,
3164 size_t ciphertext_length,
3165 uint8_t *plaintext,
3166 size_t plaintext_size,
3167 size_t *plaintext_length )
3168{
mohammad16035955c982018-04-26 00:53:03 +03003169 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003170 aead_operation_t operation;
3171 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003172
Gilles Peskineee652a32018-06-01 19:23:52 +02003173 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003174
Gilles Peskineedf9a652018-08-17 18:11:56 +02003175 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003176 if( status != PSA_SUCCESS )
3177 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003178
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003179 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003180 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003181 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003182 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003183 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003184 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003185 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003186
Gilles Peskineedf9a652018-08-17 18:11:56 +02003187 status = mbedtls_to_psa_error(
3188 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3189 ciphertext_length - operation.tag_length,
3190 nonce, nonce_length,
3191 additional_data,
3192 additional_data_length,
3193 tag, operation.tag_length,
3194 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003195 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003196 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003197 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003198 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003199 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003200 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003201 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003202 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003203
Gilles Peskineedf9a652018-08-17 18:11:56 +02003204 status = mbedtls_to_psa_error(
3205 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3206 ciphertext_length - operation.tag_length,
3207 nonce, nonce_length,
3208 additional_data,
3209 additional_data_length,
3210 ciphertext, plaintext,
3211 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003212 }
mohammad160339574652018-06-01 04:39:53 -07003213 else
3214 {
mohammad1603554faad2018-06-03 15:07:38 +03003215 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003216 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003217
Gilles Peskineedf9a652018-08-17 18:11:56 +02003218 if( status != PSA_SUCCESS && plaintext_size != 0 )
3219 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003220
Gilles Peskineedf9a652018-08-17 18:11:56 +02003221exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003222 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003223 if( status == PSA_SUCCESS )
3224 *plaintext_length = ciphertext_length - operation.tag_length;
3225 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003226}
3227
Gilles Peskinea0655c32018-04-30 17:06:50 +02003228
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003229
Gilles Peskine20035e32018-02-03 22:44:14 +01003230/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003231/* Generators */
3232/****************************************************************/
3233
3234psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3235{
3236 psa_status_t status = PSA_SUCCESS;
3237 if( generator->alg == 0 )
3238 {
3239 /* The object has (apparently) been initialized but it is not
3240 * in use. It's ok to call abort on such an object, and there's
3241 * nothing to do. */
3242 }
3243 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003244#if defined(MBEDTLS_MD_C)
3245 if( PSA_ALG_IS_HKDF( generator->alg ) )
3246 {
3247 mbedtls_free( generator->ctx.hkdf.info );
3248 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3249 }
3250 else
3251#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003252 {
3253 status = PSA_ERROR_BAD_STATE;
3254 }
3255 memset( generator, 0, sizeof( *generator ) );
3256 return( status );
3257}
3258
3259
3260psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3261 size_t *capacity)
3262{
3263 *capacity = generator->capacity;
3264 return( PSA_SUCCESS );
3265}
3266
Gilles Peskinebef7f142018-07-12 17:22:21 +02003267#if defined(MBEDTLS_MD_C)
3268/* Read some bytes from an HKDF-based generator. This performs a chunk
3269 * of the expand phase of the HKDF algorithm. */
3270static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3271 psa_algorithm_t hash_alg,
3272 uint8_t *output,
3273 size_t output_length )
3274{
3275 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3276 psa_status_t status;
3277
3278 while( output_length != 0 )
3279 {
3280 /* Copy what remains of the current block */
3281 uint8_t n = hash_length - hkdf->offset_in_block;
3282 if( n > output_length )
3283 n = (uint8_t) output_length;
3284 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3285 output += n;
3286 output_length -= n;
3287 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003288 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003289 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003290 /* We can't be wanting more output after block 0xff, otherwise
3291 * the capacity check in psa_generator_read() would have
3292 * prevented this call. It could happen only if the generator
3293 * object was corrupted or if this function is called directly
3294 * inside the library. */
3295 if( hkdf->block_number == 0xff )
3296 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003297
3298 /* We need a new block */
3299 ++hkdf->block_number;
3300 hkdf->offset_in_block = 0;
3301 status = psa_hmac_setup_internal( &hkdf->hmac,
3302 hkdf->prk, hash_length,
3303 hash_alg );
3304 if( status != PSA_SUCCESS )
3305 return( status );
3306 if( hkdf->block_number != 1 )
3307 {
3308 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3309 hkdf->output_block,
3310 hash_length );
3311 if( status != PSA_SUCCESS )
3312 return( status );
3313 }
3314 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3315 hkdf->info,
3316 hkdf->info_length );
3317 if( status != PSA_SUCCESS )
3318 return( status );
3319 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3320 &hkdf->block_number, 1 );
3321 if( status != PSA_SUCCESS )
3322 return( status );
3323 status = psa_hmac_finish_internal( &hkdf->hmac,
3324 hkdf->output_block,
3325 sizeof( hkdf->output_block ) );
3326 if( status != PSA_SUCCESS )
3327 return( status );
3328 }
3329
3330 return( PSA_SUCCESS );
3331}
3332#endif /* MBEDTLS_MD_C */
3333
Gilles Peskineeab56e42018-07-12 17:12:33 +02003334psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3335 uint8_t *output,
3336 size_t output_length )
3337{
3338 psa_status_t status;
3339
3340 if( output_length > generator->capacity )
3341 {
3342 generator->capacity = 0;
3343 /* Go through the error path to wipe all confidential data now
3344 * that the generator object is useless. */
3345 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3346 goto exit;
3347 }
3348 if( output_length == 0 &&
3349 generator->capacity == 0 && generator->alg == 0 )
3350 {
3351 /* Edge case: this is a blank or finished generator, and 0
3352 * bytes were requested. The right error in this case could
3353 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3354 * INSUFFICIENT_CAPACITY, which is right for a finished
3355 * generator, for consistency with the case when
3356 * output_length > 0. */
3357 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3358 }
3359 generator->capacity -= output_length;
3360
Gilles Peskinebef7f142018-07-12 17:22:21 +02003361#if defined(MBEDTLS_MD_C)
3362 if( PSA_ALG_IS_HKDF( generator->alg ) )
3363 {
3364 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3365 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3366 output, output_length );
3367 }
3368 else
3369#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003370 {
3371 return( PSA_ERROR_BAD_STATE );
3372 }
3373
3374exit:
3375 if( status != PSA_SUCCESS )
3376 {
3377 psa_generator_abort( generator );
3378 memset( output, '!', output_length );
3379 }
3380 return( status );
3381}
3382
Gilles Peskine08542d82018-07-19 17:05:42 +02003383#if defined(MBEDTLS_DES_C)
3384static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3385{
3386 if( data_size >= 8 )
3387 mbedtls_des_key_set_parity( data );
3388 if( data_size >= 16 )
3389 mbedtls_des_key_set_parity( data + 8 );
3390 if( data_size >= 24 )
3391 mbedtls_des_key_set_parity( data + 16 );
3392}
3393#endif /* MBEDTLS_DES_C */
3394
Gilles Peskineeab56e42018-07-12 17:12:33 +02003395psa_status_t psa_generator_import_key( psa_key_slot_t key,
3396 psa_key_type_t type,
3397 size_t bits,
3398 psa_crypto_generator_t *generator )
3399{
3400 uint8_t *data = NULL;
3401 size_t bytes = PSA_BITS_TO_BYTES( bits );
3402 psa_status_t status;
3403
3404 if( ! key_type_is_raw_bytes( type ) )
3405 return( PSA_ERROR_INVALID_ARGUMENT );
3406 if( bits % 8 != 0 )
3407 return( PSA_ERROR_INVALID_ARGUMENT );
3408 data = mbedtls_calloc( 1, bytes );
3409 if( data == NULL )
3410 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3411
3412 status = psa_generator_read( generator, data, bytes );
3413 if( status != PSA_SUCCESS )
3414 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003415#if defined(MBEDTLS_DES_C)
3416 if( type == PSA_KEY_TYPE_DES )
3417 psa_des_set_key_parity( data, bytes );
3418#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003419 status = psa_import_key( key, type, data, bytes );
3420
3421exit:
3422 mbedtls_free( data );
3423 return( status );
3424}
3425
3426
3427
3428/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003429/* Key derivation */
3430/****************************************************************/
3431
Gilles Peskinebef7f142018-07-12 17:22:21 +02003432/* Set up an HKDF-based generator. This is exactly the extract phase
3433 * of the HKDF algorithm. */
3434static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003435 const uint8_t *secret,
3436 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003437 psa_algorithm_t hash_alg,
3438 const uint8_t *salt,
3439 size_t salt_length,
3440 const uint8_t *label,
3441 size_t label_length )
3442{
3443 psa_status_t status;
3444 status = psa_hmac_setup_internal( &hkdf->hmac,
3445 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003446 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003447 if( status != PSA_SUCCESS )
3448 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003449 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003450 if( status != PSA_SUCCESS )
3451 return( status );
3452 status = psa_hmac_finish_internal( &hkdf->hmac,
3453 hkdf->prk,
3454 sizeof( hkdf->prk ) );
3455 if( status != PSA_SUCCESS )
3456 return( status );
3457 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3458 hkdf->block_number = 0;
3459 hkdf->info_length = label_length;
3460 if( label_length != 0 )
3461 {
3462 hkdf->info = mbedtls_calloc( 1, label_length );
3463 if( hkdf->info == NULL )
3464 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3465 memcpy( hkdf->info, label, label_length );
3466 }
3467 return( PSA_SUCCESS );
3468}
3469
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003470static psa_status_t psa_key_derivation_internal(
3471 psa_crypto_generator_t *generator,
3472 const uint8_t *secret, size_t secret_length,
3473 psa_algorithm_t alg,
3474 const uint8_t *salt, size_t salt_length,
3475 const uint8_t *label, size_t label_length,
3476 size_t capacity )
3477{
3478 psa_status_t status;
3479 size_t max_capacity;
3480
3481 /* Set generator->alg even on failure so that abort knows what to do. */
3482 generator->alg = alg;
3483
3484#if defined(MBEDTLS_MD_C)
3485 if( PSA_ALG_IS_HKDF( alg ) )
3486 {
3487 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3488 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3489 if( hash_size == 0 )
3490 return( PSA_ERROR_NOT_SUPPORTED );
3491 max_capacity = 255 * hash_size;
3492 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3493 secret, secret_length,
3494 hash_alg,
3495 salt, salt_length,
3496 label, label_length );
3497 }
3498 else
3499#endif
3500 {
3501 return( PSA_ERROR_NOT_SUPPORTED );
3502 }
3503
3504 if( status != PSA_SUCCESS )
3505 return( status );
3506
3507 if( capacity <= max_capacity )
3508 generator->capacity = capacity;
3509 else
3510 return( PSA_ERROR_INVALID_ARGUMENT );
3511
3512 return( PSA_SUCCESS );
3513}
3514
Gilles Peskineea0fb492018-07-12 17:17:20 +02003515psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003516 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003517 psa_algorithm_t alg,
3518 const uint8_t *salt,
3519 size_t salt_length,
3520 const uint8_t *label,
3521 size_t label_length,
3522 size_t capacity )
3523{
3524 key_slot_t *slot;
3525 psa_status_t status;
3526
3527 if( generator->alg != 0 )
3528 return( PSA_ERROR_BAD_STATE );
3529
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003530 /* Make sure that alg is a key derivation algorithm. This prevents
3531 * key selection algorithms, which psa_key_derivation_internal
3532 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003533 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3534 return( PSA_ERROR_INVALID_ARGUMENT );
3535
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003536 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3537 if( status != PSA_SUCCESS )
3538 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003539
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003540 if( slot->type != PSA_KEY_TYPE_DERIVE )
3541 return( PSA_ERROR_INVALID_ARGUMENT );
3542
3543 status = psa_key_derivation_internal( generator,
3544 slot->data.raw.data,
3545 slot->data.raw.bytes,
3546 alg,
3547 salt, salt_length,
3548 label, label_length,
3549 capacity );
3550 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003551 psa_generator_abort( generator );
3552 return( status );
3553}
3554
3555
3556
3557/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003558/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003559/****************************************************************/
3560
3561psa_status_t psa_generate_random( uint8_t *output,
3562 size_t output_size )
3563{
itayzafrir0adf0fc2018-09-06 16:24:41 +03003564 int ret;
3565 GUARD_MODULE_INITIALIZED;
3566
3567 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02003568 return( mbedtls_to_psa_error( ret ) );
3569}
3570
3571psa_status_t psa_generate_key( psa_key_slot_t key,
3572 psa_key_type_t type,
3573 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003574 const void *extra,
3575 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003576{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003577 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003578 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003579
Gilles Peskine53d991e2018-07-12 01:14:59 +02003580 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003581 return( PSA_ERROR_INVALID_ARGUMENT );
3582
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003583 status = psa_get_empty_key_slot( key, &slot );
3584 if( status != PSA_SUCCESS )
3585 return( status );
3586
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003587 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003588 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003589 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003590 if( status != PSA_SUCCESS )
3591 return( status );
3592 status = psa_generate_random( slot->data.raw.data,
3593 slot->data.raw.bytes );
3594 if( status != PSA_SUCCESS )
3595 {
3596 mbedtls_free( slot->data.raw.data );
3597 return( status );
3598 }
3599#if defined(MBEDTLS_DES_C)
3600 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003601 psa_des_set_key_parity( slot->data.raw.data,
3602 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003603#endif /* MBEDTLS_DES_C */
3604 }
3605 else
3606
3607#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3608 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3609 {
3610 mbedtls_rsa_context *rsa;
3611 int ret;
3612 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003613 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3614 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01003615 /* Accept only byte-aligned keys, for the same reasons as
3616 * in psa_import_rsa_key(). */
3617 if( bits % 8 != 0 )
3618 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003619 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003620 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003621 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003622 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003623 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003624#if INT_MAX < 0xffffffff
3625 /* Check that the uint32_t value passed by the caller fits
3626 * in the range supported by this implementation. */
3627 if( p->e > INT_MAX )
3628 return( PSA_ERROR_NOT_SUPPORTED );
3629#endif
3630 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003631 }
3632 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3633 if( rsa == NULL )
3634 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3635 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3636 ret = mbedtls_rsa_gen_key( rsa,
3637 mbedtls_ctr_drbg_random,
3638 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003639 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003640 exponent );
3641 if( ret != 0 )
3642 {
3643 mbedtls_rsa_free( rsa );
3644 mbedtls_free( rsa );
3645 return( mbedtls_to_psa_error( ret ) );
3646 }
3647 slot->data.rsa = rsa;
3648 }
3649 else
3650#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3651
3652#if defined(MBEDTLS_ECP_C)
3653 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3654 {
3655 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3656 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3657 const mbedtls_ecp_curve_info *curve_info =
3658 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3659 mbedtls_ecp_keypair *ecp;
3660 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003661 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003662 return( PSA_ERROR_NOT_SUPPORTED );
3663 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3664 return( PSA_ERROR_NOT_SUPPORTED );
3665 if( curve_info->bit_size != bits )
3666 return( PSA_ERROR_INVALID_ARGUMENT );
3667 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3668 if( ecp == NULL )
3669 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3670 mbedtls_ecp_keypair_init( ecp );
3671 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3672 mbedtls_ctr_drbg_random,
3673 &global_data.ctr_drbg );
3674 if( ret != 0 )
3675 {
3676 mbedtls_ecp_keypair_free( ecp );
3677 mbedtls_free( ecp );
3678 return( mbedtls_to_psa_error( ret ) );
3679 }
3680 slot->data.ecp = ecp;
3681 }
3682 else
3683#endif /* MBEDTLS_ECP_C */
3684
3685 return( PSA_ERROR_NOT_SUPPORTED );
3686
3687 slot->type = type;
3688 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003689}
3690
3691
3692/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003693/* Module setup */
3694/****************************************************************/
3695
Gilles Peskinee59236f2018-01-27 23:32:46 +01003696void mbedtls_psa_crypto_free( void )
3697{
Jaeden Amero045bd502018-06-26 14:00:08 +01003698 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003699 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003700 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003701 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3702 mbedtls_entropy_free( &global_data.entropy );
3703 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3704}
3705
3706psa_status_t psa_crypto_init( void )
3707{
3708 int ret;
3709 const unsigned char drbg_seed[] = "PSA";
3710
3711 if( global_data.initialized != 0 )
3712 return( PSA_SUCCESS );
3713
3714 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3715 mbedtls_entropy_init( &global_data.entropy );
3716 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3717
3718 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3719 mbedtls_entropy_func,
3720 &global_data.entropy,
3721 drbg_seed, sizeof( drbg_seed ) - 1 );
3722 if( ret != 0 )
3723 goto exit;
3724
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003725 global_data.initialized = 1;
3726
Gilles Peskinee59236f2018-01-27 23:32:46 +01003727exit:
3728 if( ret != 0 )
3729 mbedtls_psa_crypto_free( );
3730 return( mbedtls_to_psa_error( ret ) );
3731}
3732
3733#endif /* MBEDTLS_PSA_CRYPTO_C */