blob: eac1eb4d53ec1ed4417506e93a68f106f7157c35 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030029/*
30 * In case MBEDTLS_PSA_CRYPTO_SPM is defined the code is built for SPM (Secure
31 * Partition Manager) integration which separate the code into two parts
32 * NSPE (Non-Secure Process Environment) and SPE (Secure Process Environment).
33 * In this mode an additional header file should be included.
34 */
mohammad160327010052018-07-03 13:16:15 +030035#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030036/*
37 * PSA_CRYPTO_SECURE means that this file is compiled to the SPE side.
38 * some headers will be affected by this flag.
39 */
mohammad160327010052018-07-03 13:16:15 +030040#define PSA_CRYPTO_SECURE 1
41#include "crypto_spe.h"
42#endif
43
Gilles Peskinee59236f2018-01-27 23:32:46 +010044#include "psa/crypto.h"
45
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
48#if defined(MBEDTLS_PLATFORM_C)
49#include "mbedtls/platform.h"
50#else
51#define mbedtls_calloc calloc
52#define mbedtls_free free
53#endif
54
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020057#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/blowfish.h"
59#include "mbedtls/camellia.h"
60#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010077#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/sha1.h"
79#include "mbedtls/sha256.h"
80#include "mbedtls/sha512.h"
81#include "mbedtls/xtea.h"
82
Gilles Peskinee59236f2018-01-27 23:32:46 +010083
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskinee59236f2018-01-27 23:32:46 +010087/* Implementation that should never be optimized out by the compiler */
88static void mbedtls_zeroize( void *v, size_t n )
89{
90 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
91}
92
Gilles Peskine9ef733f2018-02-07 21:05:37 +010093/* constant-time buffer comparison */
94static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
95{
96 size_t i;
97 unsigned char diff = 0;
98
99 for( i = 0; i < n; i++ )
100 diff |= a[i] ^ b[i];
101
102 return( diff );
103}
104
105
106
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100107/****************************************************************/
108/* Global data, support functions and library management */
109/****************************************************************/
110
111/* Number of key slots (plus one because 0 is not used).
112 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +0200113#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100117 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300118 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200119 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200120 union
121 {
122 struct raw_data
123 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100124 uint8_t *data;
125 size_t bytes;
126 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100127#if defined(MBEDTLS_RSA_C)
128 mbedtls_rsa_context *rsa;
129#endif /* MBEDTLS_RSA_C */
130#if defined(MBEDTLS_ECP_C)
131 mbedtls_ecp_keypair *ecp;
132#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100133 } data;
134} key_slot_t;
135
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200136static int key_type_is_raw_bytes( psa_key_type_t type )
137{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200138 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200139}
140
Gilles Peskine2d277862018-06-18 15:41:12 +0200141typedef struct
142{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 int initialized;
144 mbedtls_entropy_context entropy;
145 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200146 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147} psa_global_data_t;
148
149static psa_global_data_t global_data;
150
itayzafrir0adf0fc2018-09-06 16:24:41 +0300151#define GUARD_MODULE_INITIALIZED \
152 if( global_data.initialized == 0 ) \
153 return( PSA_ERROR_BAD_STATE );
154
Gilles Peskinee59236f2018-01-27 23:32:46 +0100155static psa_status_t mbedtls_to_psa_error( int ret )
156{
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 /* If there's both a high-level code and low-level code, dispatch on
158 * the high-level code. */
159 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100160 {
161 case 0:
162 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100163
164 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
165 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
166 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
171 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
172 return( PSA_ERROR_HARDWARE_FAILURE );
173
Gilles Peskine9a944802018-06-21 09:35:35 +0200174 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
175 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
176 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
177 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
178 case MBEDTLS_ERR_ASN1_INVALID_DATA:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
181 return( PSA_ERROR_INSUFFICIENT_MEMORY );
182 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
183 return( PSA_ERROR_BUFFER_TOO_SMALL );
184
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
186 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
191 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
192 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
193 return( PSA_ERROR_NOT_SUPPORTED );
194 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
195 return( PSA_ERROR_HARDWARE_FAILURE );
196
197 case MBEDTLS_ERR_CCM_BAD_INPUT:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CCM_AUTH_FAILED:
200 return( PSA_ERROR_INVALID_SIGNATURE );
201 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
202 return( PSA_ERROR_HARDWARE_FAILURE );
203
204 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
205 return( PSA_ERROR_NOT_SUPPORTED );
206 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
207 return( PSA_ERROR_INVALID_ARGUMENT );
208 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
209 return( PSA_ERROR_INSUFFICIENT_MEMORY );
210 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
211 return( PSA_ERROR_INVALID_PADDING );
212 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
213 return( PSA_ERROR_BAD_STATE );
214 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
215 return( PSA_ERROR_INVALID_SIGNATURE );
216 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
217 return( PSA_ERROR_TAMPERING_DETECTED );
218 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
221 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
222 return( PSA_ERROR_HARDWARE_FAILURE );
223
224 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
225 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
226 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
227 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
228 return( PSA_ERROR_NOT_SUPPORTED );
229 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
231
232 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
233 return( PSA_ERROR_NOT_SUPPORTED );
234 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
235 return( PSA_ERROR_HARDWARE_FAILURE );
236
Gilles Peskinee59236f2018-01-27 23:32:46 +0100237 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
238 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
239 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
240 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100241
242 case MBEDTLS_ERR_GCM_AUTH_FAILED:
243 return( PSA_ERROR_INVALID_SIGNATURE );
244 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200245 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100246 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
247 return( PSA_ERROR_HARDWARE_FAILURE );
248
249 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
250 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
251 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
254 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
255 return( PSA_ERROR_NOT_SUPPORTED );
256 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MD_ALLOC_FAILED:
259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
260 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
261 return( PSA_ERROR_STORAGE_FAILURE );
262 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
263 return( PSA_ERROR_HARDWARE_FAILURE );
264
Gilles Peskinef76aa772018-10-29 19:24:33 +0100265 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
266 return( PSA_ERROR_STORAGE_FAILURE );
267 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
272 return( PSA_ERROR_BUFFER_TOO_SMALL );
273 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
280 return( PSA_ERROR_INSUFFICIENT_MEMORY );
281
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_ALLOC_FAILED:
283 return( PSA_ERROR_INSUFFICIENT_MEMORY );
284 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
285 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100289 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
290 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
293 return( PSA_ERROR_NOT_SUPPORTED );
294 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
295 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
296 return( PSA_ERROR_NOT_PERMITTED );
297 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
298 return( PSA_ERROR_INVALID_ARGUMENT );
299 case MBEDTLS_ERR_PK_INVALID_ALG:
300 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
301 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
302 return( PSA_ERROR_NOT_SUPPORTED );
303 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
304 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
309 return( PSA_ERROR_HARDWARE_FAILURE );
310
311 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_RSA_INVALID_PADDING:
314 return( PSA_ERROR_INVALID_PADDING );
315 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
316 return( PSA_ERROR_HARDWARE_FAILURE );
317 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
320 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
321 return( PSA_ERROR_TAMPERING_DETECTED );
322 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
323 return( PSA_ERROR_INVALID_SIGNATURE );
324 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
325 return( PSA_ERROR_BUFFER_TOO_SMALL );
326 case MBEDTLS_ERR_RSA_RNG_FAILED:
327 return( PSA_ERROR_INSUFFICIENT_MEMORY );
328 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
329 return( PSA_ERROR_NOT_SUPPORTED );
330 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
333 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
334 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
335 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
337
338 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
339 return( PSA_ERROR_INVALID_ARGUMENT );
340 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
341 return( PSA_ERROR_HARDWARE_FAILURE );
342
itayzafrir5c753392018-05-08 11:18:38 +0300343 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300345 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300346 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
347 return( PSA_ERROR_BUFFER_TOO_SMALL );
348 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
349 return( PSA_ERROR_NOT_SUPPORTED );
350 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
351 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
352 return( PSA_ERROR_INVALID_SIGNATURE );
353 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
354 return( PSA_ERROR_INSUFFICIENT_MEMORY );
355 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
356 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300357
Gilles Peskinee59236f2018-01-27 23:32:46 +0100358 default:
359 return( PSA_ERROR_UNKNOWN_ERROR );
360 }
361}
362
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200363/* Retrieve a key slot, occupied or not. */
364static psa_status_t psa_get_key_slot( psa_key_slot_t key,
365 key_slot_t **p_slot )
366{
itayzafrir90d8c7a2018-09-12 11:44:52 +0300367 GUARD_MODULE_INITIALIZED;
368
Gilles Peskine996deb12018-08-01 15:45:45 +0200369 /* 0 is not a valid slot number under any circumstance. This
370 * implementation provides slots number 1 to N where N is the
371 * number of available slots. */
372 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200373 return( PSA_ERROR_INVALID_ARGUMENT );
374
Gilles Peskine996deb12018-08-01 15:45:45 +0200375 *p_slot = &global_data.key_slots[key - 1];
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200376 return( PSA_SUCCESS );
377}
378
379/* Retrieve an empty key slot (slot with no key data, but possibly
380 * with some metadata such as a policy). */
381static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
382 key_slot_t **p_slot )
383{
384 psa_status_t status;
385 key_slot_t *slot = NULL;
386
387 *p_slot = NULL;
388
389 status = psa_get_key_slot( key, &slot );
390 if( status != PSA_SUCCESS )
391 return( status );
392
393 if( slot->type != PSA_KEY_TYPE_NONE )
394 return( PSA_ERROR_OCCUPIED_SLOT );
395
396 *p_slot = slot;
397 return( status );
398}
399
Jaeden Amerob4fa8c92018-07-11 15:57:44 +0100400/** Retrieve a slot which must contain a key. The key must have allow all the
401 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
402 * operations with this algorithm. */
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200403static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
404 key_slot_t **p_slot,
405 psa_key_usage_t usage,
406 psa_algorithm_t alg )
407{
408 psa_status_t status;
409 key_slot_t *slot = NULL;
410
411 *p_slot = NULL;
412
413 status = psa_get_key_slot( key, &slot );
414 if( status != PSA_SUCCESS )
415 return( status );
416 if( slot->type == PSA_KEY_TYPE_NONE )
417 return( PSA_ERROR_EMPTY_SLOT );
418
419 /* Enforce that usage policy for the key slot contains all the flags
420 * required by the usage parameter. There is one exception: public
421 * keys can always be exported, so we treat public key objects as
422 * if they had the export flag. */
423 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
424 usage &= ~PSA_KEY_USAGE_EXPORT;
425 if( ( slot->policy.usage & usage ) != usage )
426 return( PSA_ERROR_NOT_PERMITTED );
427 if( alg != 0 && ( alg != slot->policy.alg ) )
428 return( PSA_ERROR_NOT_PERMITTED );
429
430 *p_slot = slot;
431 return( PSA_SUCCESS );
432}
433
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200434
435
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100436/****************************************************************/
437/* Key management */
438/****************************************************************/
439
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100440#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200441static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
442{
443 switch( grpid )
444 {
445 case MBEDTLS_ECP_DP_SECP192R1:
446 return( PSA_ECC_CURVE_SECP192R1 );
447 case MBEDTLS_ECP_DP_SECP224R1:
448 return( PSA_ECC_CURVE_SECP224R1 );
449 case MBEDTLS_ECP_DP_SECP256R1:
450 return( PSA_ECC_CURVE_SECP256R1 );
451 case MBEDTLS_ECP_DP_SECP384R1:
452 return( PSA_ECC_CURVE_SECP384R1 );
453 case MBEDTLS_ECP_DP_SECP521R1:
454 return( PSA_ECC_CURVE_SECP521R1 );
455 case MBEDTLS_ECP_DP_BP256R1:
456 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
457 case MBEDTLS_ECP_DP_BP384R1:
458 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
459 case MBEDTLS_ECP_DP_BP512R1:
460 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
461 case MBEDTLS_ECP_DP_CURVE25519:
462 return( PSA_ECC_CURVE_CURVE25519 );
463 case MBEDTLS_ECP_DP_SECP192K1:
464 return( PSA_ECC_CURVE_SECP192K1 );
465 case MBEDTLS_ECP_DP_SECP224K1:
466 return( PSA_ECC_CURVE_SECP224K1 );
467 case MBEDTLS_ECP_DP_SECP256K1:
468 return( PSA_ECC_CURVE_SECP256K1 );
469 case MBEDTLS_ECP_DP_CURVE448:
470 return( PSA_ECC_CURVE_CURVE448 );
471 default:
472 return( 0 );
473 }
474}
475
Gilles Peskine12313cd2018-06-20 00:20:32 +0200476static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
477{
478 switch( curve )
479 {
480 case PSA_ECC_CURVE_SECP192R1:
481 return( MBEDTLS_ECP_DP_SECP192R1 );
482 case PSA_ECC_CURVE_SECP224R1:
483 return( MBEDTLS_ECP_DP_SECP224R1 );
484 case PSA_ECC_CURVE_SECP256R1:
485 return( MBEDTLS_ECP_DP_SECP256R1 );
486 case PSA_ECC_CURVE_SECP384R1:
487 return( MBEDTLS_ECP_DP_SECP384R1 );
488 case PSA_ECC_CURVE_SECP521R1:
489 return( MBEDTLS_ECP_DP_SECP521R1 );
490 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
491 return( MBEDTLS_ECP_DP_BP256R1 );
492 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
493 return( MBEDTLS_ECP_DP_BP384R1 );
494 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
495 return( MBEDTLS_ECP_DP_BP512R1 );
496 case PSA_ECC_CURVE_CURVE25519:
497 return( MBEDTLS_ECP_DP_CURVE25519 );
498 case PSA_ECC_CURVE_SECP192K1:
499 return( MBEDTLS_ECP_DP_SECP192K1 );
500 case PSA_ECC_CURVE_SECP224K1:
501 return( MBEDTLS_ECP_DP_SECP224K1 );
502 case PSA_ECC_CURVE_SECP256K1:
503 return( MBEDTLS_ECP_DP_SECP256K1 );
504 case PSA_ECC_CURVE_CURVE448:
505 return( MBEDTLS_ECP_DP_CURVE448 );
506 default:
507 return( MBEDTLS_ECP_DP_NONE );
508 }
509}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100510#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200511
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200512static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
513 size_t bits,
514 struct raw_data *raw )
515{
516 /* Check that the bit size is acceptable for the key type */
517 switch( type )
518 {
519 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200520 if( bits == 0 )
521 {
522 raw->bytes = 0;
523 raw->data = NULL;
524 return( PSA_SUCCESS );
525 }
526 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200527#if defined(MBEDTLS_MD_C)
528 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200529#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200530 case PSA_KEY_TYPE_DERIVE:
531 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200532#if defined(MBEDTLS_AES_C)
533 case PSA_KEY_TYPE_AES:
534 if( bits != 128 && bits != 192 && bits != 256 )
535 return( PSA_ERROR_INVALID_ARGUMENT );
536 break;
537#endif
538#if defined(MBEDTLS_CAMELLIA_C)
539 case PSA_KEY_TYPE_CAMELLIA:
540 if( bits != 128 && bits != 192 && bits != 256 )
541 return( PSA_ERROR_INVALID_ARGUMENT );
542 break;
543#endif
544#if defined(MBEDTLS_DES_C)
545 case PSA_KEY_TYPE_DES:
546 if( bits != 64 && bits != 128 && bits != 192 )
547 return( PSA_ERROR_INVALID_ARGUMENT );
548 break;
549#endif
550#if defined(MBEDTLS_ARC4_C)
551 case PSA_KEY_TYPE_ARC4:
552 if( bits < 8 || bits > 2048 )
553 return( PSA_ERROR_INVALID_ARGUMENT );
554 break;
555#endif
556 default:
557 return( PSA_ERROR_NOT_SUPPORTED );
558 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200559 if( bits % 8 != 0 )
560 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200561
562 /* Allocate memory for the key */
563 raw->bytes = PSA_BITS_TO_BYTES( bits );
564 raw->data = mbedtls_calloc( 1, raw->bytes );
565 if( raw->data == NULL )
566 {
567 raw->bytes = 0;
568 return( PSA_ERROR_INSUFFICIENT_MEMORY );
569 }
570 return( PSA_SUCCESS );
571}
572
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200573#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
574static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
575 mbedtls_rsa_context **p_rsa )
576{
577 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
578 return( PSA_ERROR_INVALID_ARGUMENT );
579 else
580 {
581 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
582 size_t bits = mbedtls_rsa_get_bitlen( rsa );
583 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
584 return( PSA_ERROR_NOT_SUPPORTED );
585 *p_rsa = rsa;
586 return( PSA_SUCCESS );
587 }
588}
589#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
590
591#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100592/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200593static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
594 mbedtls_pk_context *pk,
595 mbedtls_ecp_keypair **p_ecp )
596{
597 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
598 return( PSA_ERROR_INVALID_ARGUMENT );
599 else
600 {
601 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
602 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
603 if( actual_curve != expected_curve )
604 return( PSA_ERROR_INVALID_ARGUMENT );
605 *p_ecp = ecp;
606 return( PSA_SUCCESS );
607 }
608}
609#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
610
Gilles Peskinef76aa772018-10-29 19:24:33 +0100611#if defined(MBEDTLS_ECP_C)
612/* Import a private key given as a byte string which is the private value
613 * in big-endian order. */
614static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
615 const uint8_t *data,
616 size_t data_length,
617 mbedtls_ecp_keypair **p_ecp )
618{
619 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
620 mbedtls_ecp_keypair *ecp = NULL;
621 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
622
623 *p_ecp = NULL;
624 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
625 if( ecp == NULL )
626 return( PSA_ERROR_INSUFFICIENT_MEMORY );
627
628 /* Load the group. */
629 status = mbedtls_to_psa_error(
630 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
631 if( status != PSA_SUCCESS )
632 goto exit;
633 /* Load the secret value. */
634 status = mbedtls_to_psa_error(
635 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
636 if( status != PSA_SUCCESS )
637 goto exit;
638 /* Validate the private key. */
639 status = mbedtls_to_psa_error(
640 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
641 if( status != PSA_SUCCESS )
642 goto exit;
643 /* Calculate the public key from the private key. */
644 status = mbedtls_to_psa_error(
645 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
646 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
647 if( status != PSA_SUCCESS )
648 goto exit;
649
650 *p_ecp = ecp;
651 return( PSA_SUCCESS );
652
653exit:
654 if( ecp != NULL )
655 {
656 mbedtls_ecp_keypair_free( ecp );
657 mbedtls_free( ecp );
658 }
659 return( status );
660}
661#endif /* defined(MBEDTLS_ECP_C) */
662
Gilles Peskine2d277862018-06-18 15:41:12 +0200663psa_status_t psa_import_key( psa_key_slot_t key,
664 psa_key_type_t type,
665 const uint8_t *data,
666 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667{
668 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200669 psa_status_t status = PSA_SUCCESS;
670 status = psa_get_empty_key_slot( key, &slot );
671 if( status != PSA_SUCCESS )
672 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200674 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100676 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 if( data_length > SIZE_MAX / 8 )
678 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200679 status = prepare_raw_data_slot( type,
680 PSA_BYTES_TO_BITS( data_length ),
681 &slot->data.raw );
682 if( status != PSA_SUCCESS )
683 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200684 if( data_length != 0 )
685 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 }
687 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100688#if defined(MBEDTLS_ECP_C)
689 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
690 {
691 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( type ),
692 data, data_length,
693 &slot->data.ecp );
694 if( status != PSA_SUCCESS )
695 return( status );
696 }
697 else
698#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100699#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200700 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100701 {
702 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100703 mbedtls_pk_context pk;
704 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200705
706 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100707 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
708 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
709 else
710 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100711 if( ret != 0 )
712 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200713
714 /* We have something that the pkparse module recognizes.
715 * If it has the expected type and passes any type-specific
716 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100717#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200718 if( PSA_KEY_TYPE_IS_RSA( type ) )
719 status = psa_import_rsa_key( &pk, &slot->data.rsa );
720 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100721#endif /* MBEDTLS_RSA_C */
722#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200723 if( PSA_KEY_TYPE_IS_ECC( type ) )
724 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
725 &pk, &slot->data.ecp );
726 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100727#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200728 {
729 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200730 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200731
Gilles Peskinec648d692018-06-28 08:46:13 +0200732 /* Free the content of the pk object only on error. On success,
733 * the content of the object has been stored in the slot. */
734 if( status != PSA_SUCCESS )
735 {
736 mbedtls_pk_free( &pk );
737 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100738 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 }
740 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100741#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742 {
743 return( PSA_ERROR_NOT_SUPPORTED );
744 }
745
746 slot->type = type;
747 return( PSA_SUCCESS );
748}
749
Gilles Peskine2d277862018-06-18 15:41:12 +0200750psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751{
752 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200753 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100754
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200755 status = psa_get_key_slot( key, &slot );
756 if( status != PSA_SUCCESS )
757 return( status );
758
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100759 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200760 {
761 /* No key material to clean, but do zeroize the slot below to wipe
762 * metadata such as policies. */
763 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200764 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100765 {
766 mbedtls_free( slot->data.raw.data );
767 }
768 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100769#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200770 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100771 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100772 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100773 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100774 }
775 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100776#endif /* defined(MBEDTLS_RSA_C) */
777#if defined(MBEDTLS_ECP_C)
778 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
779 {
780 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100781 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100782 }
783 else
784#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100785 {
786 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100787 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100788 return( PSA_ERROR_TAMPERING_DETECTED );
789 }
790
791 mbedtls_zeroize( slot, sizeof( *slot ) );
792 return( PSA_SUCCESS );
793}
794
Gilles Peskineb870b182018-07-06 16:02:09 +0200795/* Return the size of the key in the given slot, in bits. */
796static size_t psa_get_key_bits( const key_slot_t *slot )
797{
798 if( key_type_is_raw_bytes( slot->type ) )
799 return( slot->data.raw.bytes * 8 );
800#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200801 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200802 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
803#endif /* defined(MBEDTLS_RSA_C) */
804#if defined(MBEDTLS_ECP_C)
805 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
806 return( slot->data.ecp->grp.pbits );
807#endif /* defined(MBEDTLS_ECP_C) */
808 /* Shouldn't happen except on an empty slot. */
809 return( 0 );
810}
811
Gilles Peskine2d277862018-06-18 15:41:12 +0200812psa_status_t psa_get_key_information( psa_key_slot_t key,
813 psa_key_type_t *type,
814 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815{
816 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200817 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200820 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100821 if( bits != NULL )
822 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200823 status = psa_get_key_slot( key, &slot );
824 if( status != PSA_SUCCESS )
825 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200826
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100827 if( slot->type == PSA_KEY_TYPE_NONE )
828 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200829 if( type != NULL )
830 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200831 if( bits != NULL )
832 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100833 return( PSA_SUCCESS );
834}
835
Gilles Peskine2d277862018-06-18 15:41:12 +0200836static psa_status_t psa_internal_export_key( psa_key_slot_t key,
837 uint8_t *data,
838 size_t data_size,
839 size_t *data_length,
840 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100841{
842 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200843 psa_status_t status;
844 /* Exporting a public key doesn't require a usage flag. If we're
845 * called by psa_export_public_key(), don't require the EXPORT flag.
846 * If we're called by psa_export_key(), do require the EXPORT flag;
847 * if the key turns out to be public key object, psa_get_key_from_slot()
848 * will ignore this flag. */
849 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100850
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100851 /* Set the key to empty now, so that even when there are errors, we always
852 * set data_length to a value between 0 and data_size. On error, setting
853 * the key to empty is a good choice because an empty key representation is
854 * unlikely to be accepted anywhere. */
855 *data_length = 0;
856
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200857 status = psa_get_key_from_slot( key, &slot, usage, 0 );
858 if( status != PSA_SUCCESS )
859 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200860 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300861 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300862
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200863 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100864 {
865 if( slot->data.raw.bytes > data_size )
866 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200867 if( slot->data.raw.bytes != 0 )
868 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100869 *data_length = slot->data.raw.bytes;
870 return( PSA_SUCCESS );
871 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100872#if defined(MBEDTLS_ECP_C)
873 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
874 {
875 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
876 if( bytes > data_size )
877 return( PSA_ERROR_BUFFER_TOO_SMALL );
878 status = mbedtls_to_psa_error(
879 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
880 if( status != PSA_SUCCESS )
881 return( status );
882 memset( data + bytes, 0, data_size - bytes );
883 *data_length = bytes;
884 return( PSA_SUCCESS );
885 }
886#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887 else
Moran Peker17e36e12018-05-02 12:55:20 +0300888 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100889#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200890 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300891 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100892 {
Moran Pekera998bc62018-04-16 18:16:20 +0300893 mbedtls_pk_context pk;
894 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200895 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300896 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100897#if defined(MBEDTLS_RSA_C)
898 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300899 pk.pk_info = &mbedtls_rsa_info;
900 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100901#else
902 return( PSA_ERROR_NOT_SUPPORTED );
903#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300904 }
905 else
906 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100907#if defined(MBEDTLS_ECP_C)
908 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300909 pk.pk_info = &mbedtls_eckey_info;
910 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100911#else
912 return( PSA_ERROR_NOT_SUPPORTED );
913#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300914 }
Moran Pekerd7326592018-05-29 16:56:39 +0300915 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300916 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300917 else
918 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300919 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200920 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200921 /* If data_size is 0 then data may be NULL and then the
922 * call to memset would have undefined behavior. */
923 if( data_size != 0 )
924 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300925 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200926 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200927 /* The mbedtls_pk_xxx functions write to the end of the buffer.
928 * Move the data to the beginning and erase remaining data
929 * at the original location. */
930 if( 2 * (size_t) ret <= data_size )
931 {
932 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200933 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200934 }
935 else if( (size_t) ret < data_size )
936 {
937 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200938 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200939 }
Moran Pekera998bc62018-04-16 18:16:20 +0300940 *data_length = ret;
941 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100942 }
943 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100944#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300945 {
946 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200947 it is valid for a special-purpose implementation to omit
948 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300949 return( PSA_ERROR_NOT_SUPPORTED );
950 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100951 }
952}
953
Gilles Peskine2d277862018-06-18 15:41:12 +0200954psa_status_t psa_export_key( psa_key_slot_t key,
955 uint8_t *data,
956 size_t data_size,
957 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300958{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200959 return( psa_internal_export_key( key, data, data_size,
960 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100962
Gilles Peskine2d277862018-06-18 15:41:12 +0200963psa_status_t psa_export_public_key( psa_key_slot_t key,
964 uint8_t *data,
965 size_t data_size,
966 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300967{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200968 return( psa_internal_export_key( key, data, data_size,
969 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300970}
971
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200972
973
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100975/* Message digests */
976/****************************************************************/
977
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100978static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100979{
980 switch( alg )
981 {
982#if defined(MBEDTLS_MD2_C)
983 case PSA_ALG_MD2:
984 return( &mbedtls_md2_info );
985#endif
986#if defined(MBEDTLS_MD4_C)
987 case PSA_ALG_MD4:
988 return( &mbedtls_md4_info );
989#endif
990#if defined(MBEDTLS_MD5_C)
991 case PSA_ALG_MD5:
992 return( &mbedtls_md5_info );
993#endif
994#if defined(MBEDTLS_RIPEMD160_C)
995 case PSA_ALG_RIPEMD160:
996 return( &mbedtls_ripemd160_info );
997#endif
998#if defined(MBEDTLS_SHA1_C)
999 case PSA_ALG_SHA_1:
1000 return( &mbedtls_sha1_info );
1001#endif
1002#if defined(MBEDTLS_SHA256_C)
1003 case PSA_ALG_SHA_224:
1004 return( &mbedtls_sha224_info );
1005 case PSA_ALG_SHA_256:
1006 return( &mbedtls_sha256_info );
1007#endif
1008#if defined(MBEDTLS_SHA512_C)
1009 case PSA_ALG_SHA_384:
1010 return( &mbedtls_sha384_info );
1011 case PSA_ALG_SHA_512:
1012 return( &mbedtls_sha512_info );
1013#endif
1014 default:
1015 return( NULL );
1016 }
1017}
1018
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001019psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1020{
1021 switch( operation->alg )
1022 {
Gilles Peskine81736312018-06-26 15:04:31 +02001023 case 0:
1024 /* The object has (apparently) been initialized but it is not
1025 * in use. It's ok to call abort on such an object, and there's
1026 * nothing to do. */
1027 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001028#if defined(MBEDTLS_MD2_C)
1029 case PSA_ALG_MD2:
1030 mbedtls_md2_free( &operation->ctx.md2 );
1031 break;
1032#endif
1033#if defined(MBEDTLS_MD4_C)
1034 case PSA_ALG_MD4:
1035 mbedtls_md4_free( &operation->ctx.md4 );
1036 break;
1037#endif
1038#if defined(MBEDTLS_MD5_C)
1039 case PSA_ALG_MD5:
1040 mbedtls_md5_free( &operation->ctx.md5 );
1041 break;
1042#endif
1043#if defined(MBEDTLS_RIPEMD160_C)
1044 case PSA_ALG_RIPEMD160:
1045 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1046 break;
1047#endif
1048#if defined(MBEDTLS_SHA1_C)
1049 case PSA_ALG_SHA_1:
1050 mbedtls_sha1_free( &operation->ctx.sha1 );
1051 break;
1052#endif
1053#if defined(MBEDTLS_SHA256_C)
1054 case PSA_ALG_SHA_224:
1055 case PSA_ALG_SHA_256:
1056 mbedtls_sha256_free( &operation->ctx.sha256 );
1057 break;
1058#endif
1059#if defined(MBEDTLS_SHA512_C)
1060 case PSA_ALG_SHA_384:
1061 case PSA_ALG_SHA_512:
1062 mbedtls_sha512_free( &operation->ctx.sha512 );
1063 break;
1064#endif
1065 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001066 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001067 }
1068 operation->alg = 0;
1069 return( PSA_SUCCESS );
1070}
1071
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001072psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001073 psa_algorithm_t alg )
1074{
1075 int ret;
1076 operation->alg = 0;
1077 switch( alg )
1078 {
1079#if defined(MBEDTLS_MD2_C)
1080 case PSA_ALG_MD2:
1081 mbedtls_md2_init( &operation->ctx.md2 );
1082 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1083 break;
1084#endif
1085#if defined(MBEDTLS_MD4_C)
1086 case PSA_ALG_MD4:
1087 mbedtls_md4_init( &operation->ctx.md4 );
1088 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1089 break;
1090#endif
1091#if defined(MBEDTLS_MD5_C)
1092 case PSA_ALG_MD5:
1093 mbedtls_md5_init( &operation->ctx.md5 );
1094 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1095 break;
1096#endif
1097#if defined(MBEDTLS_RIPEMD160_C)
1098 case PSA_ALG_RIPEMD160:
1099 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1100 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1101 break;
1102#endif
1103#if defined(MBEDTLS_SHA1_C)
1104 case PSA_ALG_SHA_1:
1105 mbedtls_sha1_init( &operation->ctx.sha1 );
1106 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1107 break;
1108#endif
1109#if defined(MBEDTLS_SHA256_C)
1110 case PSA_ALG_SHA_224:
1111 mbedtls_sha256_init( &operation->ctx.sha256 );
1112 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1113 break;
1114 case PSA_ALG_SHA_256:
1115 mbedtls_sha256_init( &operation->ctx.sha256 );
1116 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1117 break;
1118#endif
1119#if defined(MBEDTLS_SHA512_C)
1120 case PSA_ALG_SHA_384:
1121 mbedtls_sha512_init( &operation->ctx.sha512 );
1122 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1123 break;
1124 case PSA_ALG_SHA_512:
1125 mbedtls_sha512_init( &operation->ctx.sha512 );
1126 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1127 break;
1128#endif
1129 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001130 return( PSA_ALG_IS_HASH( alg ) ?
1131 PSA_ERROR_NOT_SUPPORTED :
1132 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001133 }
1134 if( ret == 0 )
1135 operation->alg = alg;
1136 else
1137 psa_hash_abort( operation );
1138 return( mbedtls_to_psa_error( ret ) );
1139}
1140
1141psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1142 const uint8_t *input,
1143 size_t input_length )
1144{
1145 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001146
1147 /* Don't require hash implementations to behave correctly on a
1148 * zero-length input, which may have an invalid pointer. */
1149 if( input_length == 0 )
1150 return( PSA_SUCCESS );
1151
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001152 switch( operation->alg )
1153 {
1154#if defined(MBEDTLS_MD2_C)
1155 case PSA_ALG_MD2:
1156 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1157 input, input_length );
1158 break;
1159#endif
1160#if defined(MBEDTLS_MD4_C)
1161 case PSA_ALG_MD4:
1162 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1163 input, input_length );
1164 break;
1165#endif
1166#if defined(MBEDTLS_MD5_C)
1167 case PSA_ALG_MD5:
1168 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1169 input, input_length );
1170 break;
1171#endif
1172#if defined(MBEDTLS_RIPEMD160_C)
1173 case PSA_ALG_RIPEMD160:
1174 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1175 input, input_length );
1176 break;
1177#endif
1178#if defined(MBEDTLS_SHA1_C)
1179 case PSA_ALG_SHA_1:
1180 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1181 input, input_length );
1182 break;
1183#endif
1184#if defined(MBEDTLS_SHA256_C)
1185 case PSA_ALG_SHA_224:
1186 case PSA_ALG_SHA_256:
1187 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1188 input, input_length );
1189 break;
1190#endif
1191#if defined(MBEDTLS_SHA512_C)
1192 case PSA_ALG_SHA_384:
1193 case PSA_ALG_SHA_512:
1194 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1195 input, input_length );
1196 break;
1197#endif
1198 default:
1199 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1200 break;
1201 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001202
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001203 if( ret != 0 )
1204 psa_hash_abort( operation );
1205 return( mbedtls_to_psa_error( ret ) );
1206}
1207
1208psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1209 uint8_t *hash,
1210 size_t hash_size,
1211 size_t *hash_length )
1212{
itayzafrir40835d42018-08-02 13:14:17 +03001213 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001214 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001215 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001216
1217 /* Fill the output buffer with something that isn't a valid hash
1218 * (barring an attack on the hash and deliberately-crafted input),
1219 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001220 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001221 /* If hash_size is 0 then hash may be NULL and then the
1222 * call to memset would have undefined behavior. */
1223 if( hash_size != 0 )
1224 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001225
1226 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001227 {
1228 status = PSA_ERROR_BUFFER_TOO_SMALL;
1229 goto exit;
1230 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001231
1232 switch( operation->alg )
1233 {
1234#if defined(MBEDTLS_MD2_C)
1235 case PSA_ALG_MD2:
1236 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1237 break;
1238#endif
1239#if defined(MBEDTLS_MD4_C)
1240 case PSA_ALG_MD4:
1241 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1242 break;
1243#endif
1244#if defined(MBEDTLS_MD5_C)
1245 case PSA_ALG_MD5:
1246 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1247 break;
1248#endif
1249#if defined(MBEDTLS_RIPEMD160_C)
1250 case PSA_ALG_RIPEMD160:
1251 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1252 break;
1253#endif
1254#if defined(MBEDTLS_SHA1_C)
1255 case PSA_ALG_SHA_1:
1256 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1257 break;
1258#endif
1259#if defined(MBEDTLS_SHA256_C)
1260 case PSA_ALG_SHA_224:
1261 case PSA_ALG_SHA_256:
1262 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1263 break;
1264#endif
1265#if defined(MBEDTLS_SHA512_C)
1266 case PSA_ALG_SHA_384:
1267 case PSA_ALG_SHA_512:
1268 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1269 break;
1270#endif
1271 default:
1272 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1273 break;
1274 }
itayzafrir40835d42018-08-02 13:14:17 +03001275 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001276
itayzafrir40835d42018-08-02 13:14:17 +03001277exit:
1278 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001279 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001280 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001281 return( psa_hash_abort( operation ) );
1282 }
1283 else
1284 {
1285 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001286 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001287 }
1288}
1289
Gilles Peskine2d277862018-06-18 15:41:12 +02001290psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1291 const uint8_t *hash,
1292 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001293{
1294 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1295 size_t actual_hash_length;
1296 psa_status_t status = psa_hash_finish( operation,
1297 actual_hash, sizeof( actual_hash ),
1298 &actual_hash_length );
1299 if( status != PSA_SUCCESS )
1300 return( status );
1301 if( actual_hash_length != hash_length )
1302 return( PSA_ERROR_INVALID_SIGNATURE );
1303 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1304 return( PSA_ERROR_INVALID_SIGNATURE );
1305 return( PSA_SUCCESS );
1306}
1307
1308
1309
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001310/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001311/* MAC */
1312/****************************************************************/
1313
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001314static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001315 psa_algorithm_t alg,
1316 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001317 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001318 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001319{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001320 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001321 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001322
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001323 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001324 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001325
Gilles Peskine8c9def32018-02-08 10:02:12 +01001326 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1327 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001328 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001329 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001330 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001331 mode = MBEDTLS_MODE_STREAM;
1332 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001333 case PSA_ALG_CTR:
1334 mode = MBEDTLS_MODE_CTR;
1335 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001336 case PSA_ALG_CFB:
1337 mode = MBEDTLS_MODE_CFB;
1338 break;
1339 case PSA_ALG_OFB:
1340 mode = MBEDTLS_MODE_OFB;
1341 break;
1342 case PSA_ALG_CBC_NO_PADDING:
1343 mode = MBEDTLS_MODE_CBC;
1344 break;
1345 case PSA_ALG_CBC_PKCS7:
1346 mode = MBEDTLS_MODE_CBC;
1347 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001348 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001349 mode = MBEDTLS_MODE_CCM;
1350 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001351 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001352 mode = MBEDTLS_MODE_GCM;
1353 break;
1354 default:
1355 return( NULL );
1356 }
1357 }
1358 else if( alg == PSA_ALG_CMAC )
1359 mode = MBEDTLS_MODE_ECB;
1360 else if( alg == PSA_ALG_GMAC )
1361 mode = MBEDTLS_MODE_GCM;
1362 else
1363 return( NULL );
1364
1365 switch( key_type )
1366 {
1367 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001368 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001369 break;
1370 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001371 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1372 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001373 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001374 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001375 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001376 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001377 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1378 * but two-key Triple-DES is functionally three-key Triple-DES
1379 * with K1=K3, so that's how we present it to mbedtls. */
1380 if( key_bits == 128 )
1381 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001382 break;
1383 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001384 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001385 break;
1386 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001387 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001388 break;
1389 default:
1390 return( NULL );
1391 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001392 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001393 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001394
Jaeden Amero23bbb752018-06-26 14:16:54 +01001395 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1396 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001397}
1398
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001399static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001400{
Gilles Peskine2d277862018-06-18 15:41:12 +02001401 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001402 {
1403 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001404 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001405 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001406 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001407 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001408 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001409 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001410 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001411 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001412 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001413 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001414 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001415 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001416 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001417 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001418 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001419 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001420 return( 128 );
1421 default:
1422 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001423 }
1424}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001425
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001426/* Initialize the MAC operation structure. Once this function has been
1427 * called, psa_mac_abort can run and will do the right thing. */
1428static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1429 psa_algorithm_t alg )
1430{
1431 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1432
1433 operation->alg = alg;
1434 operation->key_set = 0;
1435 operation->iv_set = 0;
1436 operation->iv_required = 0;
1437 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001438 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001439
1440#if defined(MBEDTLS_CMAC_C)
1441 if( alg == PSA_ALG_CMAC )
1442 {
1443 operation->iv_required = 0;
1444 mbedtls_cipher_init( &operation->ctx.cmac );
1445 status = PSA_SUCCESS;
1446 }
1447 else
1448#endif /* MBEDTLS_CMAC_C */
1449#if defined(MBEDTLS_MD_C)
1450 if( PSA_ALG_IS_HMAC( operation->alg ) )
1451 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001452 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1453 operation->ctx.hmac.hash_ctx.alg = 0;
1454 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001455 }
1456 else
1457#endif /* MBEDTLS_MD_C */
1458 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001459 if( ! PSA_ALG_IS_MAC( alg ) )
1460 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001461 }
1462
1463 if( status != PSA_SUCCESS )
1464 memset( operation, 0, sizeof( *operation ) );
1465 return( status );
1466}
1467
Gilles Peskine01126fa2018-07-12 17:04:55 +02001468#if defined(MBEDTLS_MD_C)
1469static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1470{
1471 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1472 return( psa_hash_abort( &hmac->hash_ctx ) );
1473}
1474#endif /* MBEDTLS_MD_C */
1475
Gilles Peskine8c9def32018-02-08 10:02:12 +01001476psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1477{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001478 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001479 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001480 /* The object has (apparently) been initialized but it is not
1481 * in use. It's ok to call abort on such an object, and there's
1482 * nothing to do. */
1483 return( PSA_SUCCESS );
1484 }
1485 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001486#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001487 if( operation->alg == PSA_ALG_CMAC )
1488 {
1489 mbedtls_cipher_free( &operation->ctx.cmac );
1490 }
1491 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001492#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001493#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001494 if( PSA_ALG_IS_HMAC( operation->alg ) )
1495 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001496 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001497 }
1498 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001499#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001500 {
1501 /* Sanity check (shouldn't happen: operation->alg should
1502 * always have been initialized to a valid value). */
1503 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504 }
Moran Peker41deec42018-04-04 15:43:05 +03001505
Gilles Peskine8c9def32018-02-08 10:02:12 +01001506 operation->alg = 0;
1507 operation->key_set = 0;
1508 operation->iv_set = 0;
1509 operation->iv_required = 0;
1510 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001511 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001512
Gilles Peskine8c9def32018-02-08 10:02:12 +01001513 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001514
1515bad_state:
1516 /* If abort is called on an uninitialized object, we can't trust
1517 * anything. Wipe the object in case it contains confidential data.
1518 * This may result in a memory leak if a pointer gets overwritten,
1519 * but it's too late to do anything about this. */
1520 memset( operation, 0, sizeof( *operation ) );
1521 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001522}
1523
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001524#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001525static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001526 size_t key_bits,
1527 key_slot_t *slot,
1528 const mbedtls_cipher_info_t *cipher_info )
1529{
1530 int ret;
1531
1532 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001533
1534 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1535 if( ret != 0 )
1536 return( ret );
1537
1538 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1539 slot->data.raw.data,
1540 key_bits );
1541 return( ret );
1542}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001543#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001544
Gilles Peskine248051a2018-06-20 16:09:38 +02001545#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001546static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1547 const uint8_t *key,
1548 size_t key_length,
1549 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001550{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001551 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001552 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001553 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001554 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001555 psa_status_t status;
1556
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001557 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1558 * overflow below. This should never trigger if the hash algorithm
1559 * is implemented correctly. */
1560 /* The size checks against the ipad and opad buffers cannot be written
1561 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1562 * because that triggers -Wlogical-op on GCC 7.3. */
1563 if( block_size > sizeof( ipad ) )
1564 return( PSA_ERROR_NOT_SUPPORTED );
1565 if( block_size > sizeof( hmac->opad ) )
1566 return( PSA_ERROR_NOT_SUPPORTED );
1567 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001568 return( PSA_ERROR_NOT_SUPPORTED );
1569
Gilles Peskined223b522018-06-11 18:12:58 +02001570 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001571 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001572 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001573 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001574 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001575 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001576 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001577 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001578 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001579 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001580 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001581 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001582 }
Gilles Peskine96889972018-07-12 17:07:03 +02001583 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1584 * but it is permitted. It is common when HMAC is used in HKDF, for
1585 * example. Don't call `memcpy` in the 0-length because `key` could be
1586 * an invalid pointer which would make the behavior undefined. */
1587 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001588 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001589
Gilles Peskined223b522018-06-11 18:12:58 +02001590 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1591 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001592 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001593 ipad[i] ^= 0x36;
1594 memset( ipad + key_length, 0x36, block_size - key_length );
1595
1596 /* Copy the key material from ipad to opad, flipping the requisite bits,
1597 * and filling the rest of opad with the requisite constant. */
1598 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001599 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1600 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001601
Gilles Peskine01126fa2018-07-12 17:04:55 +02001602 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001603 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001604 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001605
Gilles Peskine01126fa2018-07-12 17:04:55 +02001606 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001607
1608cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001609 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001610
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001611 return( status );
1612}
Gilles Peskine248051a2018-06-20 16:09:38 +02001613#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001614
Gilles Peskine89167cb2018-07-08 20:12:23 +02001615static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1616 psa_key_slot_t key,
1617 psa_algorithm_t alg,
1618 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001619{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001620 psa_status_t status;
1621 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001622 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001623 psa_key_usage_t usage =
1624 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001625 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001626 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001627
Gilles Peskined911eb72018-08-14 15:18:45 +02001628 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001629 if( status != PSA_SUCCESS )
1630 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001631 if( is_sign )
1632 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001633
Gilles Peskine89167cb2018-07-08 20:12:23 +02001634 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001635 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001636 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001637 key_bits = psa_get_key_bits( slot );
1638
Gilles Peskine8c9def32018-02-08 10:02:12 +01001639#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001640 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001641 {
1642 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001643 mbedtls_cipher_info_from_psa( full_length_alg,
1644 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001645 int ret;
1646 if( cipher_info == NULL )
1647 {
1648 status = PSA_ERROR_NOT_SUPPORTED;
1649 goto exit;
1650 }
1651 operation->mac_size = cipher_info->block_size;
1652 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1653 status = mbedtls_to_psa_error( ret );
1654 }
1655 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001656#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001657#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001658 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001659 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001660 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001661 if( hash_alg == 0 )
1662 {
1663 status = PSA_ERROR_NOT_SUPPORTED;
1664 goto exit;
1665 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001666
1667 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1668 /* Sanity check. This shouldn't fail on a valid configuration. */
1669 if( operation->mac_size == 0 ||
1670 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1671 {
1672 status = PSA_ERROR_NOT_SUPPORTED;
1673 goto exit;
1674 }
1675
Gilles Peskine01126fa2018-07-12 17:04:55 +02001676 if( slot->type != PSA_KEY_TYPE_HMAC )
1677 {
1678 status = PSA_ERROR_INVALID_ARGUMENT;
1679 goto exit;
1680 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001681
Gilles Peskine01126fa2018-07-12 17:04:55 +02001682 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1683 slot->data.raw.data,
1684 slot->data.raw.bytes,
1685 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001686 }
1687 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001688#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001689 {
1690 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001691 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001692
Gilles Peskined911eb72018-08-14 15:18:45 +02001693 if( truncated == 0 )
1694 {
1695 /* The "normal" case: untruncated algorithm. Nothing to do. */
1696 }
1697 else if( truncated < 4 )
1698 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001699 /* A very short MAC is too short for security since it can be
1700 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1701 * so we make this our minimum, even though 32 bits is still
1702 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001703 status = PSA_ERROR_NOT_SUPPORTED;
1704 }
1705 else if( truncated > operation->mac_size )
1706 {
1707 /* It's impossible to "truncate" to a larger length. */
1708 status = PSA_ERROR_INVALID_ARGUMENT;
1709 }
1710 else
1711 operation->mac_size = truncated;
1712
Gilles Peskinefbfac682018-07-08 20:51:54 +02001713exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001714 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001715 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001716 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001717 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001718 else
1719 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001720 operation->key_set = 1;
1721 }
1722 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001723}
1724
Gilles Peskine89167cb2018-07-08 20:12:23 +02001725psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1726 psa_key_slot_t key,
1727 psa_algorithm_t alg )
1728{
1729 return( psa_mac_setup( operation, key, alg, 1 ) );
1730}
1731
1732psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1733 psa_key_slot_t key,
1734 psa_algorithm_t alg )
1735{
1736 return( psa_mac_setup( operation, key, alg, 0 ) );
1737}
1738
Gilles Peskine8c9def32018-02-08 10:02:12 +01001739psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1740 const uint8_t *input,
1741 size_t input_length )
1742{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001743 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001744 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001745 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001746 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001747 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001748 operation->has_input = 1;
1749
Gilles Peskine8c9def32018-02-08 10:02:12 +01001750#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001751 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001752 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001753 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1754 input, input_length );
1755 status = mbedtls_to_psa_error( ret );
1756 }
1757 else
1758#endif /* MBEDTLS_CMAC_C */
1759#if defined(MBEDTLS_MD_C)
1760 if( PSA_ALG_IS_HMAC( operation->alg ) )
1761 {
1762 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1763 input_length );
1764 }
1765 else
1766#endif /* MBEDTLS_MD_C */
1767 {
1768 /* This shouldn't happen if `operation` was initialized by
1769 * a setup function. */
1770 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001771 }
1772
Gilles Peskinefbfac682018-07-08 20:51:54 +02001773cleanup:
1774 if( status != PSA_SUCCESS )
1775 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001776 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001777}
1778
Gilles Peskine01126fa2018-07-12 17:04:55 +02001779#if defined(MBEDTLS_MD_C)
1780static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1781 uint8_t *mac,
1782 size_t mac_size )
1783{
1784 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1785 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1786 size_t hash_size = 0;
1787 size_t block_size = psa_get_hash_block_size( hash_alg );
1788 psa_status_t status;
1789
Gilles Peskine01126fa2018-07-12 17:04:55 +02001790 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1791 if( status != PSA_SUCCESS )
1792 return( status );
1793 /* From here on, tmp needs to be wiped. */
1794
1795 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1796 if( status != PSA_SUCCESS )
1797 goto exit;
1798
1799 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1800 if( status != PSA_SUCCESS )
1801 goto exit;
1802
1803 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1804 if( status != PSA_SUCCESS )
1805 goto exit;
1806
Gilles Peskined911eb72018-08-14 15:18:45 +02001807 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1808 if( status != PSA_SUCCESS )
1809 goto exit;
1810
1811 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001812
1813exit:
1814 mbedtls_zeroize( tmp, hash_size );
1815 return( status );
1816}
1817#endif /* MBEDTLS_MD_C */
1818
mohammad16036df908f2018-04-02 08:34:15 -07001819static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001820 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001821 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001822{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001823 if( ! operation->key_set )
1824 return( PSA_ERROR_BAD_STATE );
1825 if( operation->iv_required && ! operation->iv_set )
1826 return( PSA_ERROR_BAD_STATE );
1827
Gilles Peskine8c9def32018-02-08 10:02:12 +01001828 if( mac_size < operation->mac_size )
1829 return( PSA_ERROR_BUFFER_TOO_SMALL );
1830
Gilles Peskine8c9def32018-02-08 10:02:12 +01001831#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001832 if( operation->alg == PSA_ALG_CMAC )
1833 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001834 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1835 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1836 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02001837 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02001838 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001839 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001840 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001841 else
1842#endif /* MBEDTLS_CMAC_C */
1843#if defined(MBEDTLS_MD_C)
1844 if( PSA_ALG_IS_HMAC( operation->alg ) )
1845 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001846 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001847 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001848 }
1849 else
1850#endif /* MBEDTLS_MD_C */
1851 {
1852 /* This shouldn't happen if `operation` was initialized by
1853 * a setup function. */
1854 return( PSA_ERROR_BAD_STATE );
1855 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001856}
1857
Gilles Peskineacd4be32018-07-08 19:56:25 +02001858psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1859 uint8_t *mac,
1860 size_t mac_size,
1861 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001862{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001863 psa_status_t status;
1864
1865 /* Fill the output buffer with something that isn't a valid mac
1866 * (barring an attack on the mac and deliberately-crafted input),
1867 * in case the caller doesn't check the return status properly. */
1868 *mac_length = mac_size;
1869 /* If mac_size is 0 then mac may be NULL and then the
1870 * call to memset would have undefined behavior. */
1871 if( mac_size != 0 )
1872 memset( mac, '!', mac_size );
1873
Gilles Peskine89167cb2018-07-08 20:12:23 +02001874 if( ! operation->is_sign )
1875 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001876 status = PSA_ERROR_BAD_STATE;
1877 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001878 }
mohammad16036df908f2018-04-02 08:34:15 -07001879
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001880 status = psa_mac_finish_internal( operation, mac, mac_size );
1881
1882cleanup:
1883 if( status == PSA_SUCCESS )
1884 {
1885 status = psa_mac_abort( operation );
1886 if( status == PSA_SUCCESS )
1887 *mac_length = operation->mac_size;
1888 else
1889 memset( mac, '!', mac_size );
1890 }
1891 else
1892 psa_mac_abort( operation );
1893 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001894}
1895
Gilles Peskineacd4be32018-07-08 19:56:25 +02001896psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1897 const uint8_t *mac,
1898 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001899{
Gilles Peskine828ed142018-06-18 23:25:51 +02001900 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001901 psa_status_t status;
1902
Gilles Peskine89167cb2018-07-08 20:12:23 +02001903 if( operation->is_sign )
1904 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001905 status = PSA_ERROR_BAD_STATE;
1906 goto cleanup;
1907 }
1908 if( operation->mac_size != mac_length )
1909 {
1910 status = PSA_ERROR_INVALID_SIGNATURE;
1911 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001912 }
mohammad16036df908f2018-04-02 08:34:15 -07001913
1914 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001915 actual_mac, sizeof( actual_mac ) );
1916
1917 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1918 status = PSA_ERROR_INVALID_SIGNATURE;
1919
1920cleanup:
1921 if( status == PSA_SUCCESS )
1922 status = psa_mac_abort( operation );
1923 else
1924 psa_mac_abort( operation );
1925
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02001926 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02001927
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001928 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001929}
1930
1931
Gilles Peskine20035e32018-02-03 22:44:14 +01001932
Gilles Peskine20035e32018-02-03 22:44:14 +01001933/****************************************************************/
1934/* Asymmetric cryptography */
1935/****************************************************************/
1936
Gilles Peskine2b450e32018-06-27 15:42:46 +02001937#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001938/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001939 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001940static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1941 size_t hash_length,
1942 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001943{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001944 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001945 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001946 *md_alg = mbedtls_md_get_type( md_info );
1947
1948 /* The Mbed TLS RSA module uses an unsigned int for hash length
1949 * parameters. Validate that it fits so that we don't risk an
1950 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001951#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001952 if( hash_length > UINT_MAX )
1953 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001954#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001955
1956#if defined(MBEDTLS_PKCS1_V15)
1957 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1958 * must be correct. */
1959 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1960 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001961 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001962 if( md_info == NULL )
1963 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001964 if( mbedtls_md_get_size( md_info ) != hash_length )
1965 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001966 }
1967#endif /* MBEDTLS_PKCS1_V15 */
1968
1969#if defined(MBEDTLS_PKCS1_V21)
1970 /* PSS requires a hash internally. */
1971 if( PSA_ALG_IS_RSA_PSS( alg ) )
1972 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001973 if( md_info == NULL )
1974 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001975 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001976#endif /* MBEDTLS_PKCS1_V21 */
1977
Gilles Peskine61b91d42018-06-08 16:09:36 +02001978 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001979}
1980
Gilles Peskine2b450e32018-06-27 15:42:46 +02001981static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1982 psa_algorithm_t alg,
1983 const uint8_t *hash,
1984 size_t hash_length,
1985 uint8_t *signature,
1986 size_t signature_size,
1987 size_t *signature_length )
1988{
1989 psa_status_t status;
1990 int ret;
1991 mbedtls_md_type_t md_alg;
1992
1993 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1994 if( status != PSA_SUCCESS )
1995 return( status );
1996
Gilles Peskine630a18a2018-06-29 17:49:35 +02001997 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001998 return( PSA_ERROR_BUFFER_TOO_SMALL );
1999
2000#if defined(MBEDTLS_PKCS1_V15)
2001 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2002 {
2003 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2004 MBEDTLS_MD_NONE );
2005 ret = mbedtls_rsa_pkcs1_sign( rsa,
2006 mbedtls_ctr_drbg_random,
2007 &global_data.ctr_drbg,
2008 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002009 md_alg,
2010 (unsigned int) hash_length,
2011 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002012 signature );
2013 }
2014 else
2015#endif /* MBEDTLS_PKCS1_V15 */
2016#if defined(MBEDTLS_PKCS1_V21)
2017 if( PSA_ALG_IS_RSA_PSS( alg ) )
2018 {
2019 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2020 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2021 mbedtls_ctr_drbg_random,
2022 &global_data.ctr_drbg,
2023 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002024 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002025 (unsigned int) hash_length,
2026 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002027 signature );
2028 }
2029 else
2030#endif /* MBEDTLS_PKCS1_V21 */
2031 {
2032 return( PSA_ERROR_INVALID_ARGUMENT );
2033 }
2034
2035 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002036 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002037 return( mbedtls_to_psa_error( ret ) );
2038}
2039
2040static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2041 psa_algorithm_t alg,
2042 const uint8_t *hash,
2043 size_t hash_length,
2044 const uint8_t *signature,
2045 size_t signature_length )
2046{
2047 psa_status_t status;
2048 int ret;
2049 mbedtls_md_type_t md_alg;
2050
2051 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2052 if( status != PSA_SUCCESS )
2053 return( status );
2054
Gilles Peskine630a18a2018-06-29 17:49:35 +02002055 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002056 return( PSA_ERROR_BUFFER_TOO_SMALL );
2057
2058#if defined(MBEDTLS_PKCS1_V15)
2059 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2060 {
2061 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2062 MBEDTLS_MD_NONE );
2063 ret = mbedtls_rsa_pkcs1_verify( rsa,
2064 mbedtls_ctr_drbg_random,
2065 &global_data.ctr_drbg,
2066 MBEDTLS_RSA_PUBLIC,
2067 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002068 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002069 hash,
2070 signature );
2071 }
2072 else
2073#endif /* MBEDTLS_PKCS1_V15 */
2074#if defined(MBEDTLS_PKCS1_V21)
2075 if( PSA_ALG_IS_RSA_PSS( alg ) )
2076 {
2077 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2078 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2079 mbedtls_ctr_drbg_random,
2080 &global_data.ctr_drbg,
2081 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002082 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002083 (unsigned int) hash_length,
2084 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002085 signature );
2086 }
2087 else
2088#endif /* MBEDTLS_PKCS1_V21 */
2089 {
2090 return( PSA_ERROR_INVALID_ARGUMENT );
2091 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002092
2093 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2094 * the rest of the signature is invalid". This has little use in
2095 * practice and PSA doesn't report this distinction. */
2096 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2097 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002098 return( mbedtls_to_psa_error( ret ) );
2099}
2100#endif /* MBEDTLS_RSA_C */
2101
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002102#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002103/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2104 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2105 * (even though these functions don't modify it). */
2106static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2107 psa_algorithm_t alg,
2108 const uint8_t *hash,
2109 size_t hash_length,
2110 uint8_t *signature,
2111 size_t signature_size,
2112 size_t *signature_length )
2113{
2114 int ret;
2115 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002116 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002117 mbedtls_mpi_init( &r );
2118 mbedtls_mpi_init( &s );
2119
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002120 if( signature_size < 2 * curve_bytes )
2121 {
2122 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2123 goto cleanup;
2124 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002125
2126 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2127 {
2128 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2129 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2130 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2131 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2132 hash, hash_length,
2133 md_alg ) );
2134 }
2135 else
2136 {
2137 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2138 hash, hash_length,
2139 mbedtls_ctr_drbg_random,
2140 &global_data.ctr_drbg ) );
2141 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002142
2143 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2144 signature,
2145 curve_bytes ) );
2146 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2147 signature + curve_bytes,
2148 curve_bytes ) );
2149
2150cleanup:
2151 mbedtls_mpi_free( &r );
2152 mbedtls_mpi_free( &s );
2153 if( ret == 0 )
2154 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002155 return( mbedtls_to_psa_error( ret ) );
2156}
2157
2158static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2159 const uint8_t *hash,
2160 size_t hash_length,
2161 const uint8_t *signature,
2162 size_t signature_length )
2163{
2164 int ret;
2165 mbedtls_mpi r, s;
2166 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2167 mbedtls_mpi_init( &r );
2168 mbedtls_mpi_init( &s );
2169
2170 if( signature_length != 2 * curve_bytes )
2171 return( PSA_ERROR_INVALID_SIGNATURE );
2172
2173 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2174 signature,
2175 curve_bytes ) );
2176 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2177 signature + curve_bytes,
2178 curve_bytes ) );
2179
2180 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2181 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002182
2183cleanup:
2184 mbedtls_mpi_free( &r );
2185 mbedtls_mpi_free( &s );
2186 return( mbedtls_to_psa_error( ret ) );
2187}
2188#endif /* MBEDTLS_ECDSA_C */
2189
Gilles Peskine61b91d42018-06-08 16:09:36 +02002190psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2191 psa_algorithm_t alg,
2192 const uint8_t *hash,
2193 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002194 uint8_t *signature,
2195 size_t signature_size,
2196 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002197{
2198 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002199 psa_status_t status;
2200
2201 *signature_length = signature_size;
2202
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002203 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2204 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002205 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002206 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002207 {
2208 status = PSA_ERROR_INVALID_ARGUMENT;
2209 goto exit;
2210 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002211
Gilles Peskine20035e32018-02-03 22:44:14 +01002212#if defined(MBEDTLS_RSA_C)
2213 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2214 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002215 status = psa_rsa_sign( slot->data.rsa,
2216 alg,
2217 hash, hash_length,
2218 signature, signature_size,
2219 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002220 }
2221 else
2222#endif /* defined(MBEDTLS_RSA_C) */
2223#if defined(MBEDTLS_ECP_C)
2224 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2225 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002226#if defined(MBEDTLS_ECDSA_C)
2227 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002228 status = psa_ecdsa_sign( slot->data.ecp,
2229 alg,
2230 hash, hash_length,
2231 signature, signature_size,
2232 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002233 else
2234#endif /* defined(MBEDTLS_ECDSA_C) */
2235 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002236 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002237 }
itayzafrir5c753392018-05-08 11:18:38 +03002238 }
2239 else
2240#endif /* defined(MBEDTLS_ECP_C) */
2241 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002242 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002243 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002244
2245exit:
2246 /* Fill the unused part of the output buffer (the whole buffer on error,
2247 * the trailing part on success) with something that isn't a valid mac
2248 * (barring an attack on the mac and deliberately-crafted input),
2249 * in case the caller doesn't check the return status properly. */
2250 if( status == PSA_SUCCESS )
2251 memset( signature + *signature_length, '!',
2252 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002253 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002254 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002255 /* If signature_size is 0 then we have nothing to do. We must not call
2256 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002257 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002258}
2259
2260psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2261 psa_algorithm_t alg,
2262 const uint8_t *hash,
2263 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002264 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002265 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002266{
2267 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002268 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002269
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002270 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2271 if( status != PSA_SUCCESS )
2272 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002273
Gilles Peskine61b91d42018-06-08 16:09:36 +02002274#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002275 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002276 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002277 return( psa_rsa_verify( slot->data.rsa,
2278 alg,
2279 hash, hash_length,
2280 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002281 }
2282 else
2283#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002284#if defined(MBEDTLS_ECP_C)
2285 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2286 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002287#if defined(MBEDTLS_ECDSA_C)
2288 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002289 return( psa_ecdsa_verify( slot->data.ecp,
2290 hash, hash_length,
2291 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002292 else
2293#endif /* defined(MBEDTLS_ECDSA_C) */
2294 {
2295 return( PSA_ERROR_INVALID_ARGUMENT );
2296 }
itayzafrir5c753392018-05-08 11:18:38 +03002297 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002298 else
2299#endif /* defined(MBEDTLS_ECP_C) */
2300 {
2301 return( PSA_ERROR_NOT_SUPPORTED );
2302 }
2303}
2304
Gilles Peskine072ac562018-06-30 00:21:29 +02002305#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2306static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2307 mbedtls_rsa_context *rsa )
2308{
2309 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2310 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2311 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2312 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2313}
2314#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2315
Gilles Peskine61b91d42018-06-08 16:09:36 +02002316psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2317 psa_algorithm_t alg,
2318 const uint8_t *input,
2319 size_t input_length,
2320 const uint8_t *salt,
2321 size_t salt_length,
2322 uint8_t *output,
2323 size_t output_size,
2324 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002325{
2326 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002327 psa_status_t status;
2328
Darryl Green5cc689a2018-07-24 15:34:10 +01002329 (void) input;
2330 (void) input_length;
2331 (void) salt;
2332 (void) output;
2333 (void) output_size;
2334
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002335 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002336
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002337 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2338 return( PSA_ERROR_INVALID_ARGUMENT );
2339
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002340 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2341 if( status != PSA_SUCCESS )
2342 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002343 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2344 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002345 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002346
2347#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002348 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002349 {
2350 mbedtls_rsa_context *rsa = slot->data.rsa;
2351 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002352 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002353 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002354#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002355 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002356 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002357 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2358 mbedtls_ctr_drbg_random,
2359 &global_data.ctr_drbg,
2360 MBEDTLS_RSA_PUBLIC,
2361 input_length,
2362 input,
2363 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002364 }
2365 else
2366#endif /* MBEDTLS_PKCS1_V15 */
2367#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002368 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002369 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002370 psa_rsa_oaep_set_padding_mode( alg, rsa );
2371 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2372 mbedtls_ctr_drbg_random,
2373 &global_data.ctr_drbg,
2374 MBEDTLS_RSA_PUBLIC,
2375 salt, salt_length,
2376 input_length,
2377 input,
2378 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002379 }
2380 else
2381#endif /* MBEDTLS_PKCS1_V21 */
2382 {
2383 return( PSA_ERROR_INVALID_ARGUMENT );
2384 }
2385 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002386 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002387 return( mbedtls_to_psa_error( ret ) );
2388 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002389 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002390#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002391 {
2392 return( PSA_ERROR_NOT_SUPPORTED );
2393 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002394}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002395
Gilles Peskine61b91d42018-06-08 16:09:36 +02002396psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2397 psa_algorithm_t alg,
2398 const uint8_t *input,
2399 size_t input_length,
2400 const uint8_t *salt,
2401 size_t salt_length,
2402 uint8_t *output,
2403 size_t output_size,
2404 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002405{
2406 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002407 psa_status_t status;
2408
Darryl Green5cc689a2018-07-24 15:34:10 +01002409 (void) input;
2410 (void) input_length;
2411 (void) salt;
2412 (void) output;
2413 (void) output_size;
2414
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002415 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002416
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002417 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2418 return( PSA_ERROR_INVALID_ARGUMENT );
2419
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002420 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2421 if( status != PSA_SUCCESS )
2422 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002423 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2424 return( PSA_ERROR_INVALID_ARGUMENT );
2425
2426#if defined(MBEDTLS_RSA_C)
2427 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2428 {
2429 mbedtls_rsa_context *rsa = slot->data.rsa;
2430 int ret;
2431
Gilles Peskine630a18a2018-06-29 17:49:35 +02002432 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002433 return( PSA_ERROR_INVALID_ARGUMENT );
2434
2435#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002436 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002437 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002438 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2439 mbedtls_ctr_drbg_random,
2440 &global_data.ctr_drbg,
2441 MBEDTLS_RSA_PRIVATE,
2442 output_length,
2443 input,
2444 output,
2445 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002446 }
2447 else
2448#endif /* MBEDTLS_PKCS1_V15 */
2449#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002450 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002451 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002452 psa_rsa_oaep_set_padding_mode( alg, rsa );
2453 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2454 mbedtls_ctr_drbg_random,
2455 &global_data.ctr_drbg,
2456 MBEDTLS_RSA_PRIVATE,
2457 salt, salt_length,
2458 output_length,
2459 input,
2460 output,
2461 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002462 }
2463 else
2464#endif /* MBEDTLS_PKCS1_V21 */
2465 {
2466 return( PSA_ERROR_INVALID_ARGUMENT );
2467 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002468
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002469 return( mbedtls_to_psa_error( ret ) );
2470 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002471 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002472#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002473 {
2474 return( PSA_ERROR_NOT_SUPPORTED );
2475 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002476}
Gilles Peskine20035e32018-02-03 22:44:14 +01002477
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002478
2479
mohammad1603503973b2018-03-12 15:59:30 +02002480/****************************************************************/
2481/* Symmetric cryptography */
2482/****************************************************************/
2483
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002484/* Initialize the cipher operation structure. Once this function has been
2485 * called, psa_cipher_abort can run and will do the right thing. */
2486static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2487 psa_algorithm_t alg )
2488{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002489 if( ! PSA_ALG_IS_CIPHER( alg ) )
2490 {
2491 memset( operation, 0, sizeof( *operation ) );
2492 return( PSA_ERROR_INVALID_ARGUMENT );
2493 }
2494
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002495 operation->alg = alg;
2496 operation->key_set = 0;
2497 operation->iv_set = 0;
2498 operation->iv_required = 1;
2499 operation->iv_size = 0;
2500 operation->block_size = 0;
2501 mbedtls_cipher_init( &operation->ctx.cipher );
2502 return( PSA_SUCCESS );
2503}
2504
Gilles Peskinee553c652018-06-04 16:22:46 +02002505static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2506 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002507 psa_algorithm_t alg,
2508 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002509{
2510 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2511 psa_status_t status;
2512 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002513 size_t key_bits;
2514 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002515 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2516 PSA_KEY_USAGE_ENCRYPT :
2517 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002518
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002519 status = psa_cipher_init( operation, alg );
2520 if( status != PSA_SUCCESS )
2521 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002522
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002523 status = psa_get_key_from_slot( key, &slot, usage, alg);
2524 if( status != PSA_SUCCESS )
2525 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002526 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002527
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002528 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002529 if( cipher_info == NULL )
2530 return( PSA_ERROR_NOT_SUPPORTED );
2531
mohammad1603503973b2018-03-12 15:59:30 +02002532 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002533 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002534 {
2535 psa_cipher_abort( operation );
2536 return( mbedtls_to_psa_error( ret ) );
2537 }
2538
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002539#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002540 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002541 {
2542 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2543 unsigned char keys[24];
2544 memcpy( keys, slot->data.raw.data, 16 );
2545 memcpy( keys + 16, slot->data.raw.data, 8 );
2546 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2547 keys,
2548 192, cipher_operation );
2549 }
2550 else
2551#endif
2552 {
2553 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2554 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002555 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002556 }
Moran Peker41deec42018-04-04 15:43:05 +03002557 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002558 {
2559 psa_cipher_abort( operation );
2560 return( mbedtls_to_psa_error( ret ) );
2561 }
2562
mohammad16038481e742018-03-18 13:57:31 +02002563#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002564 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002565 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002566 case PSA_ALG_CBC_NO_PADDING:
2567 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2568 MBEDTLS_PADDING_NONE );
2569 break;
2570 case PSA_ALG_CBC_PKCS7:
2571 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2572 MBEDTLS_PADDING_PKCS7 );
2573 break;
2574 default:
2575 /* The algorithm doesn't involve padding. */
2576 ret = 0;
2577 break;
2578 }
2579 if( ret != 0 )
2580 {
2581 psa_cipher_abort( operation );
2582 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002583 }
2584#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2585
mohammad1603503973b2018-03-12 15:59:30 +02002586 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002587 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2588 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2589 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002590 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002591 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002592 }
mohammad1603503973b2018-03-12 15:59:30 +02002593
Moran Peker395db872018-05-31 14:07:14 +03002594 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002595}
2596
Gilles Peskinefe119512018-07-08 21:39:34 +02002597psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2598 psa_key_slot_t key,
2599 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002600{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002601 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002602}
2603
Gilles Peskinefe119512018-07-08 21:39:34 +02002604psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2605 psa_key_slot_t key,
2606 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002607{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002608 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002609}
2610
Gilles Peskinefe119512018-07-08 21:39:34 +02002611psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2612 unsigned char *iv,
2613 size_t iv_size,
2614 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002615{
itayzafrir534bd7c2018-08-02 13:56:32 +03002616 psa_status_t status;
2617 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002618 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002619 {
2620 status = PSA_ERROR_BAD_STATE;
2621 goto exit;
2622 }
Moran Peker41deec42018-04-04 15:43:05 +03002623 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002624 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002625 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002626 goto exit;
2627 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002628 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2629 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002630 if( ret != 0 )
2631 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002632 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002633 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002634 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002635
mohammad16038481e742018-03-18 13:57:31 +02002636 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002637 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002638
Moran Peker395db872018-05-31 14:07:14 +03002639exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002640 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002641 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002642 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002643}
2644
Gilles Peskinefe119512018-07-08 21:39:34 +02002645psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2646 const unsigned char *iv,
2647 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002648{
itayzafrir534bd7c2018-08-02 13:56:32 +03002649 psa_status_t status;
2650 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002651 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002652 {
2653 status = PSA_ERROR_BAD_STATE;
2654 goto exit;
2655 }
Moran Pekera28258c2018-05-29 16:25:04 +03002656 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002657 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002658 status = PSA_ERROR_INVALID_ARGUMENT;
2659 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002660 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002661 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2662 status = mbedtls_to_psa_error( ret );
2663exit:
2664 if( status == PSA_SUCCESS )
2665 operation->iv_set = 1;
2666 else
mohammad1603503973b2018-03-12 15:59:30 +02002667 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002668 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002669}
2670
Gilles Peskinee553c652018-06-04 16:22:46 +02002671psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2672 const uint8_t *input,
2673 size_t input_length,
2674 unsigned char *output,
2675 size_t output_size,
2676 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002677{
itayzafrir534bd7c2018-08-02 13:56:32 +03002678 psa_status_t status;
2679 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002680 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002681 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002682 {
2683 /* Take the unprocessed partial block left over from previous
2684 * update calls, if any, plus the input to this call. Remove
2685 * the last partial block, if any. You get the data that will be
2686 * output in this call. */
2687 expected_output_size =
2688 ( operation->ctx.cipher.unprocessed_len + input_length )
2689 / operation->block_size * operation->block_size;
2690 }
2691 else
2692 {
2693 expected_output_size = input_length;
2694 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002695
Gilles Peskine89d789c2018-06-04 17:17:16 +02002696 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002697 {
2698 status = PSA_ERROR_BUFFER_TOO_SMALL;
2699 goto exit;
2700 }
mohammad160382759612018-03-12 18:16:40 +02002701
mohammad1603503973b2018-03-12 15:59:30 +02002702 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002703 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002704 status = mbedtls_to_psa_error( ret );
2705exit:
2706 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002707 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002708 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002709}
2710
Gilles Peskinee553c652018-06-04 16:22:46 +02002711psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2712 uint8_t *output,
2713 size_t output_size,
2714 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002715{
Janos Follath315b51c2018-07-09 16:04:51 +01002716 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2717 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002718 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002719
mohammad1603503973b2018-03-12 15:59:30 +02002720 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002721 {
Janos Follath315b51c2018-07-09 16:04:51 +01002722 status = PSA_ERROR_BAD_STATE;
2723 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002724 }
2725 if( operation->iv_required && ! operation->iv_set )
2726 {
Janos Follath315b51c2018-07-09 16:04:51 +01002727 status = PSA_ERROR_BAD_STATE;
2728 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002729 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002730
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002731 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002732 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2733 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002734 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002735 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002736 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002737 }
2738
Janos Follath315b51c2018-07-09 16:04:51 +01002739 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2740 temp_output_buffer,
2741 output_length );
2742 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002743 {
Janos Follath315b51c2018-07-09 16:04:51 +01002744 status = mbedtls_to_psa_error( cipher_ret );
2745 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002746 }
Janos Follath315b51c2018-07-09 16:04:51 +01002747
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002748 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002749 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002750 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002751 memcpy( output, temp_output_buffer, *output_length );
2752 else
2753 {
Janos Follath315b51c2018-07-09 16:04:51 +01002754 status = PSA_ERROR_BUFFER_TOO_SMALL;
2755 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002756 }
mohammad1603503973b2018-03-12 15:59:30 +02002757
Janos Follath279ab8e2018-07-09 16:13:21 +01002758 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002759 status = psa_cipher_abort( operation );
2760
2761 return( status );
2762
2763error:
2764
2765 *output_length = 0;
2766
Janos Follath279ab8e2018-07-09 16:13:21 +01002767 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002768 (void) psa_cipher_abort( operation );
2769
2770 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002771}
2772
Gilles Peskinee553c652018-06-04 16:22:46 +02002773psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2774{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002775 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002776 {
2777 /* The object has (apparently) been initialized but it is not
2778 * in use. It's ok to call abort on such an object, and there's
2779 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002780 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002781 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002782
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002783 /* Sanity check (shouldn't happen: operation->alg should
2784 * always have been initialized to a valid value). */
2785 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2786 return( PSA_ERROR_BAD_STATE );
2787
mohammad1603503973b2018-03-12 15:59:30 +02002788 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002789
Moran Peker41deec42018-04-04 15:43:05 +03002790 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002791 operation->key_set = 0;
2792 operation->iv_set = 0;
2793 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002794 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002795 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002796
Moran Peker395db872018-05-31 14:07:14 +03002797 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002798}
2799
Gilles Peskinea0655c32018-04-30 17:06:50 +02002800
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002801
mohammad16038cc1cee2018-03-28 01:21:33 +03002802/****************************************************************/
2803/* Key Policy */
2804/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002805
mohammad160327010052018-07-03 13:16:15 +03002806#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002807void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002808{
Gilles Peskine803ce742018-06-18 16:07:14 +02002809 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002810}
2811
Gilles Peskine2d277862018-06-18 15:41:12 +02002812void psa_key_policy_set_usage( psa_key_policy_t *policy,
2813 psa_key_usage_t usage,
2814 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002815{
mohammad16034eed7572018-03-28 05:14:59 -07002816 policy->usage = usage;
2817 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002818}
2819
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002820psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002821{
mohammad16036df908f2018-04-02 08:34:15 -07002822 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002823}
2824
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002825psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002826{
mohammad16036df908f2018-04-02 08:34:15 -07002827 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002828}
mohammad160327010052018-07-03 13:16:15 +03002829#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002830
Gilles Peskine2d277862018-06-18 15:41:12 +02002831psa_status_t psa_set_key_policy( psa_key_slot_t key,
2832 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002833{
2834 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002835 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002836
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002837 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002838 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002839
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002840 status = psa_get_empty_key_slot( key, &slot );
2841 if( status != PSA_SUCCESS )
2842 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002843
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002844 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2845 PSA_KEY_USAGE_ENCRYPT |
2846 PSA_KEY_USAGE_DECRYPT |
2847 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002848 PSA_KEY_USAGE_VERIFY |
2849 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002850 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002851
mohammad16036df908f2018-04-02 08:34:15 -07002852 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002853
2854 return( PSA_SUCCESS );
2855}
2856
Gilles Peskine2d277862018-06-18 15:41:12 +02002857psa_status_t psa_get_key_policy( psa_key_slot_t key,
2858 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002859{
2860 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002861 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002862
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002863 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002864 return( PSA_ERROR_INVALID_ARGUMENT );
2865
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002866 status = psa_get_key_slot( key, &slot );
2867 if( status != PSA_SUCCESS )
2868 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002869
mohammad16036df908f2018-04-02 08:34:15 -07002870 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002871
2872 return( PSA_SUCCESS );
2873}
Gilles Peskine20035e32018-02-03 22:44:14 +01002874
Gilles Peskinea0655c32018-04-30 17:06:50 +02002875
2876
mohammad1603804cd712018-03-20 22:44:08 +02002877/****************************************************************/
2878/* Key Lifetime */
2879/****************************************************************/
2880
Gilles Peskine2d277862018-06-18 15:41:12 +02002881psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2882 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002883{
2884 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002885 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002886
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002887 status = psa_get_key_slot( key, &slot );
2888 if( status != PSA_SUCCESS )
2889 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002890
mohammad1603804cd712018-03-20 22:44:08 +02002891 *lifetime = slot->lifetime;
2892
2893 return( PSA_SUCCESS );
2894}
2895
Gilles Peskine2d277862018-06-18 15:41:12 +02002896psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002897 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002898{
2899 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002900 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002901
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002902 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2903 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002904 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2905 return( PSA_ERROR_INVALID_ARGUMENT );
2906
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002907 status = psa_get_empty_key_slot( key, &slot );
2908 if( status != PSA_SUCCESS )
2909 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002910
Moran Pekerd7326592018-05-29 16:56:39 +03002911 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002912 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002913
mohammad1603060ad8a2018-03-20 14:28:38 -07002914 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002915
2916 return( PSA_SUCCESS );
2917}
2918
Gilles Peskine20035e32018-02-03 22:44:14 +01002919
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002920
mohammad16035955c982018-04-26 00:53:03 +03002921/****************************************************************/
2922/* AEAD */
2923/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002924
Gilles Peskineedf9a652018-08-17 18:11:56 +02002925typedef struct
2926{
2927 key_slot_t *slot;
2928 const mbedtls_cipher_info_t *cipher_info;
2929 union
2930 {
2931#if defined(MBEDTLS_CCM_C)
2932 mbedtls_ccm_context ccm;
2933#endif /* MBEDTLS_CCM_C */
2934#if defined(MBEDTLS_GCM_C)
2935 mbedtls_gcm_context gcm;
2936#endif /* MBEDTLS_GCM_C */
2937 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002938 psa_algorithm_t core_alg;
2939 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002940 uint8_t tag_length;
2941} aead_operation_t;
2942
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002943static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002944{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002945 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002946 {
2947#if defined(MBEDTLS_CCM_C)
2948 case PSA_ALG_CCM:
2949 mbedtls_ccm_free( &operation->ctx.ccm );
2950 break;
2951#endif /* MBEDTLS_CCM_C */
2952#if defined(MBEDTLS_CCM_C)
2953 case PSA_ALG_GCM:
2954 mbedtls_gcm_free( &operation->ctx.gcm );
2955 break;
2956#endif /* MBEDTLS_GCM_C */
2957 }
2958}
2959
2960static psa_status_t psa_aead_setup( aead_operation_t *operation,
2961 psa_key_slot_t key,
2962 psa_key_usage_t usage,
2963 psa_algorithm_t alg )
2964{
2965 psa_status_t status;
2966 size_t key_bits;
2967 mbedtls_cipher_id_t cipher_id;
2968
2969 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
2970 if( status != PSA_SUCCESS )
2971 return( status );
2972
2973 key_bits = psa_get_key_bits( operation->slot );
2974
2975 operation->cipher_info =
2976 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
2977 &cipher_id );
2978 if( operation->cipher_info == NULL )
2979 return( PSA_ERROR_NOT_SUPPORTED );
2980
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002981 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002982 {
2983#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002984 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
2985 operation->core_alg = PSA_ALG_CCM;
2986 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002987 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
2988 return( PSA_ERROR_INVALID_ARGUMENT );
2989 mbedtls_ccm_init( &operation->ctx.ccm );
2990 status = mbedtls_to_psa_error(
2991 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
2992 operation->slot->data.raw.data,
2993 (unsigned int) key_bits ) );
2994 if( status != 0 )
2995 goto cleanup;
2996 break;
2997#endif /* MBEDTLS_CCM_C */
2998
2999#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003000 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3001 operation->core_alg = PSA_ALG_GCM;
3002 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003003 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3004 return( PSA_ERROR_INVALID_ARGUMENT );
3005 mbedtls_gcm_init( &operation->ctx.gcm );
3006 status = mbedtls_to_psa_error(
3007 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3008 operation->slot->data.raw.data,
3009 (unsigned int) key_bits ) );
3010 break;
3011#endif /* MBEDTLS_GCM_C */
3012
3013 default:
3014 return( PSA_ERROR_NOT_SUPPORTED );
3015 }
3016
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003017 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3018 {
3019 status = PSA_ERROR_INVALID_ARGUMENT;
3020 goto cleanup;
3021 }
3022 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3023 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3024 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3025 * In both cases, mbedtls_xxx will validate the tag length below. */
3026
Gilles Peskineedf9a652018-08-17 18:11:56 +02003027 return( PSA_SUCCESS );
3028
3029cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003030 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003031 return( status );
3032}
3033
mohammad16035955c982018-04-26 00:53:03 +03003034psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3035 psa_algorithm_t alg,
3036 const uint8_t *nonce,
3037 size_t nonce_length,
3038 const uint8_t *additional_data,
3039 size_t additional_data_length,
3040 const uint8_t *plaintext,
3041 size_t plaintext_length,
3042 uint8_t *ciphertext,
3043 size_t ciphertext_size,
3044 size_t *ciphertext_length )
3045{
mohammad16035955c982018-04-26 00:53:03 +03003046 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003047 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003048 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003049
mohammad1603f08a5502018-06-03 15:05:47 +03003050 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003051
Gilles Peskineedf9a652018-08-17 18:11:56 +02003052 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003053 if( status != PSA_SUCCESS )
3054 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003055
Gilles Peskineedf9a652018-08-17 18:11:56 +02003056 /* For all currently supported modes, the tag is at the end of the
3057 * ciphertext. */
3058 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3059 {
3060 status = PSA_ERROR_BUFFER_TOO_SMALL;
3061 goto exit;
3062 }
3063 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003064
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003065 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003066 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003067 status = mbedtls_to_psa_error(
3068 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3069 MBEDTLS_GCM_ENCRYPT,
3070 plaintext_length,
3071 nonce, nonce_length,
3072 additional_data, additional_data_length,
3073 plaintext, ciphertext,
3074 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003075 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003076 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003077 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003078 status = mbedtls_to_psa_error(
3079 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3080 plaintext_length,
3081 nonce, nonce_length,
3082 additional_data,
3083 additional_data_length,
3084 plaintext, ciphertext,
3085 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003086 }
mohammad16035c8845f2018-05-09 05:40:09 -07003087 else
3088 {
mohammad1603554faad2018-06-03 15:07:38 +03003089 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003090 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003091
Gilles Peskineedf9a652018-08-17 18:11:56 +02003092 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3093 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003094
Gilles Peskineedf9a652018-08-17 18:11:56 +02003095exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003096 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003097 if( status == PSA_SUCCESS )
3098 *ciphertext_length = plaintext_length + operation.tag_length;
3099 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003100}
3101
Gilles Peskineee652a32018-06-01 19:23:52 +02003102/* Locate the tag in a ciphertext buffer containing the encrypted data
3103 * followed by the tag. Return the length of the part preceding the tag in
3104 * *plaintext_length. This is the size of the plaintext in modes where
3105 * the encrypted data has the same size as the plaintext, such as
3106 * CCM and GCM. */
3107static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3108 const uint8_t *ciphertext,
3109 size_t ciphertext_length,
3110 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003111 const uint8_t **p_tag )
3112{
3113 size_t payload_length;
3114 if( tag_length > ciphertext_length )
3115 return( PSA_ERROR_INVALID_ARGUMENT );
3116 payload_length = ciphertext_length - tag_length;
3117 if( payload_length > plaintext_size )
3118 return( PSA_ERROR_BUFFER_TOO_SMALL );
3119 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003120 return( PSA_SUCCESS );
3121}
3122
mohammad16035955c982018-04-26 00:53:03 +03003123psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3124 psa_algorithm_t alg,
3125 const uint8_t *nonce,
3126 size_t nonce_length,
3127 const uint8_t *additional_data,
3128 size_t additional_data_length,
3129 const uint8_t *ciphertext,
3130 size_t ciphertext_length,
3131 uint8_t *plaintext,
3132 size_t plaintext_size,
3133 size_t *plaintext_length )
3134{
mohammad16035955c982018-04-26 00:53:03 +03003135 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003136 aead_operation_t operation;
3137 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003138
Gilles Peskineee652a32018-06-01 19:23:52 +02003139 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003140
Gilles Peskineedf9a652018-08-17 18:11:56 +02003141 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003142 if( status != PSA_SUCCESS )
3143 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003144
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003145 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003146 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003147 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003148 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003149 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003150 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003151 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003152
Gilles Peskineedf9a652018-08-17 18:11:56 +02003153 status = mbedtls_to_psa_error(
3154 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3155 ciphertext_length - operation.tag_length,
3156 nonce, nonce_length,
3157 additional_data,
3158 additional_data_length,
3159 tag, operation.tag_length,
3160 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003161 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003162 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003163 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003164 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003165 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003166 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003167 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003168 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003169
Gilles Peskineedf9a652018-08-17 18:11:56 +02003170 status = mbedtls_to_psa_error(
3171 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3172 ciphertext_length - operation.tag_length,
3173 nonce, nonce_length,
3174 additional_data,
3175 additional_data_length,
3176 ciphertext, plaintext,
3177 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003178 }
mohammad160339574652018-06-01 04:39:53 -07003179 else
3180 {
mohammad1603554faad2018-06-03 15:07:38 +03003181 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003182 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003183
Gilles Peskineedf9a652018-08-17 18:11:56 +02003184 if( status != PSA_SUCCESS && plaintext_size != 0 )
3185 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003186
Gilles Peskineedf9a652018-08-17 18:11:56 +02003187exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003188 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003189 if( status == PSA_SUCCESS )
3190 *plaintext_length = ciphertext_length - operation.tag_length;
3191 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003192}
3193
Gilles Peskinea0655c32018-04-30 17:06:50 +02003194
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003195
Gilles Peskine20035e32018-02-03 22:44:14 +01003196/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003197/* Generators */
3198/****************************************************************/
3199
3200psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3201{
3202 psa_status_t status = PSA_SUCCESS;
3203 if( generator->alg == 0 )
3204 {
3205 /* The object has (apparently) been initialized but it is not
3206 * in use. It's ok to call abort on such an object, and there's
3207 * nothing to do. */
3208 }
3209 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003210#if defined(MBEDTLS_MD_C)
3211 if( PSA_ALG_IS_HKDF( generator->alg ) )
3212 {
3213 mbedtls_free( generator->ctx.hkdf.info );
3214 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3215 }
3216 else
3217#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003218 {
3219 status = PSA_ERROR_BAD_STATE;
3220 }
3221 memset( generator, 0, sizeof( *generator ) );
3222 return( status );
3223}
3224
3225
3226psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3227 size_t *capacity)
3228{
3229 *capacity = generator->capacity;
3230 return( PSA_SUCCESS );
3231}
3232
Gilles Peskinebef7f142018-07-12 17:22:21 +02003233#if defined(MBEDTLS_MD_C)
3234/* Read some bytes from an HKDF-based generator. This performs a chunk
3235 * of the expand phase of the HKDF algorithm. */
3236static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3237 psa_algorithm_t hash_alg,
3238 uint8_t *output,
3239 size_t output_length )
3240{
3241 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3242 psa_status_t status;
3243
3244 while( output_length != 0 )
3245 {
3246 /* Copy what remains of the current block */
3247 uint8_t n = hash_length - hkdf->offset_in_block;
3248 if( n > output_length )
3249 n = (uint8_t) output_length;
3250 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3251 output += n;
3252 output_length -= n;
3253 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003254 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003255 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003256 /* We can't be wanting more output after block 0xff, otherwise
3257 * the capacity check in psa_generator_read() would have
3258 * prevented this call. It could happen only if the generator
3259 * object was corrupted or if this function is called directly
3260 * inside the library. */
3261 if( hkdf->block_number == 0xff )
3262 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003263
3264 /* We need a new block */
3265 ++hkdf->block_number;
3266 hkdf->offset_in_block = 0;
3267 status = psa_hmac_setup_internal( &hkdf->hmac,
3268 hkdf->prk, hash_length,
3269 hash_alg );
3270 if( status != PSA_SUCCESS )
3271 return( status );
3272 if( hkdf->block_number != 1 )
3273 {
3274 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3275 hkdf->output_block,
3276 hash_length );
3277 if( status != PSA_SUCCESS )
3278 return( status );
3279 }
3280 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3281 hkdf->info,
3282 hkdf->info_length );
3283 if( status != PSA_SUCCESS )
3284 return( status );
3285 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3286 &hkdf->block_number, 1 );
3287 if( status != PSA_SUCCESS )
3288 return( status );
3289 status = psa_hmac_finish_internal( &hkdf->hmac,
3290 hkdf->output_block,
3291 sizeof( hkdf->output_block ) );
3292 if( status != PSA_SUCCESS )
3293 return( status );
3294 }
3295
3296 return( PSA_SUCCESS );
3297}
3298#endif /* MBEDTLS_MD_C */
3299
Gilles Peskineeab56e42018-07-12 17:12:33 +02003300psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3301 uint8_t *output,
3302 size_t output_length )
3303{
3304 psa_status_t status;
3305
3306 if( output_length > generator->capacity )
3307 {
3308 generator->capacity = 0;
3309 /* Go through the error path to wipe all confidential data now
3310 * that the generator object is useless. */
3311 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3312 goto exit;
3313 }
3314 if( output_length == 0 &&
3315 generator->capacity == 0 && generator->alg == 0 )
3316 {
3317 /* Edge case: this is a blank or finished generator, and 0
3318 * bytes were requested. The right error in this case could
3319 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3320 * INSUFFICIENT_CAPACITY, which is right for a finished
3321 * generator, for consistency with the case when
3322 * output_length > 0. */
3323 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3324 }
3325 generator->capacity -= output_length;
3326
Gilles Peskinebef7f142018-07-12 17:22:21 +02003327#if defined(MBEDTLS_MD_C)
3328 if( PSA_ALG_IS_HKDF( generator->alg ) )
3329 {
3330 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3331 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3332 output, output_length );
3333 }
3334 else
3335#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003336 {
3337 return( PSA_ERROR_BAD_STATE );
3338 }
3339
3340exit:
3341 if( status != PSA_SUCCESS )
3342 {
3343 psa_generator_abort( generator );
3344 memset( output, '!', output_length );
3345 }
3346 return( status );
3347}
3348
Gilles Peskine08542d82018-07-19 17:05:42 +02003349#if defined(MBEDTLS_DES_C)
3350static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3351{
3352 if( data_size >= 8 )
3353 mbedtls_des_key_set_parity( data );
3354 if( data_size >= 16 )
3355 mbedtls_des_key_set_parity( data + 8 );
3356 if( data_size >= 24 )
3357 mbedtls_des_key_set_parity( data + 16 );
3358}
3359#endif /* MBEDTLS_DES_C */
3360
Gilles Peskineeab56e42018-07-12 17:12:33 +02003361psa_status_t psa_generator_import_key( psa_key_slot_t key,
3362 psa_key_type_t type,
3363 size_t bits,
3364 psa_crypto_generator_t *generator )
3365{
3366 uint8_t *data = NULL;
3367 size_t bytes = PSA_BITS_TO_BYTES( bits );
3368 psa_status_t status;
3369
3370 if( ! key_type_is_raw_bytes( type ) )
3371 return( PSA_ERROR_INVALID_ARGUMENT );
3372 if( bits % 8 != 0 )
3373 return( PSA_ERROR_INVALID_ARGUMENT );
3374 data = mbedtls_calloc( 1, bytes );
3375 if( data == NULL )
3376 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3377
3378 status = psa_generator_read( generator, data, bytes );
3379 if( status != PSA_SUCCESS )
3380 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003381#if defined(MBEDTLS_DES_C)
3382 if( type == PSA_KEY_TYPE_DES )
3383 psa_des_set_key_parity( data, bytes );
3384#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003385 status = psa_import_key( key, type, data, bytes );
3386
3387exit:
3388 mbedtls_free( data );
3389 return( status );
3390}
3391
3392
3393
3394/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003395/* Key derivation */
3396/****************************************************************/
3397
Gilles Peskinebef7f142018-07-12 17:22:21 +02003398/* Set up an HKDF-based generator. This is exactly the extract phase
3399 * of the HKDF algorithm. */
3400static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3401 key_slot_t *slot,
3402 psa_algorithm_t hash_alg,
3403 const uint8_t *salt,
3404 size_t salt_length,
3405 const uint8_t *label,
3406 size_t label_length )
3407{
3408 psa_status_t status;
3409 status = psa_hmac_setup_internal( &hkdf->hmac,
3410 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003411 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003412 if( status != PSA_SUCCESS )
3413 return( status );
3414 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3415 slot->data.raw.data,
3416 slot->data.raw.bytes );
3417 if( status != PSA_SUCCESS )
3418 return( status );
3419 status = psa_hmac_finish_internal( &hkdf->hmac,
3420 hkdf->prk,
3421 sizeof( hkdf->prk ) );
3422 if( status != PSA_SUCCESS )
3423 return( status );
3424 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3425 hkdf->block_number = 0;
3426 hkdf->info_length = label_length;
3427 if( label_length != 0 )
3428 {
3429 hkdf->info = mbedtls_calloc( 1, label_length );
3430 if( hkdf->info == NULL )
3431 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3432 memcpy( hkdf->info, label, label_length );
3433 }
3434 return( PSA_SUCCESS );
3435}
3436
Gilles Peskineea0fb492018-07-12 17:17:20 +02003437psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003438 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003439 psa_algorithm_t alg,
3440 const uint8_t *salt,
3441 size_t salt_length,
3442 const uint8_t *label,
3443 size_t label_length,
3444 size_t capacity )
3445{
3446 key_slot_t *slot;
3447 psa_status_t status;
3448
3449 if( generator->alg != 0 )
3450 return( PSA_ERROR_BAD_STATE );
3451
3452 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3453 if( status != PSA_SUCCESS )
3454 return( status );
3455 if( slot->type != PSA_KEY_TYPE_DERIVE )
3456 return( PSA_ERROR_INVALID_ARGUMENT );
3457
3458 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3459 return( PSA_ERROR_INVALID_ARGUMENT );
3460
Gilles Peskinebef7f142018-07-12 17:22:21 +02003461#if defined(MBEDTLS_MD_C)
3462 if( PSA_ALG_IS_HKDF( alg ) )
3463 {
3464 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3465 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3466 if( hash_size == 0 )
3467 return( PSA_ERROR_NOT_SUPPORTED );
3468 if( capacity > 255 * hash_size )
3469 return( PSA_ERROR_INVALID_ARGUMENT );
3470 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3471 slot,
3472 hash_alg,
3473 salt, salt_length,
3474 label, label_length );
3475 }
3476 else
3477#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003478 {
3479 return( PSA_ERROR_NOT_SUPPORTED );
3480 }
3481
3482 /* Set generator->alg even on failure so that abort knows what to do. */
3483 generator->alg = alg;
3484 if( status == PSA_SUCCESS )
3485 generator->capacity = capacity;
3486 else
3487 psa_generator_abort( generator );
3488 return( status );
3489}
3490
3491
3492
3493/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003494/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003495/****************************************************************/
3496
3497psa_status_t psa_generate_random( uint8_t *output,
3498 size_t output_size )
3499{
itayzafrir0adf0fc2018-09-06 16:24:41 +03003500 int ret;
3501 GUARD_MODULE_INITIALIZED;
3502
3503 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02003504 return( mbedtls_to_psa_error( ret ) );
3505}
3506
3507psa_status_t psa_generate_key( psa_key_slot_t key,
3508 psa_key_type_t type,
3509 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003510 const void *extra,
3511 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003512{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003513 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003514 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003515
Gilles Peskine53d991e2018-07-12 01:14:59 +02003516 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003517 return( PSA_ERROR_INVALID_ARGUMENT );
3518
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003519 status = psa_get_empty_key_slot( key, &slot );
3520 if( status != PSA_SUCCESS )
3521 return( status );
3522
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003523 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003524 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003525 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003526 if( status != PSA_SUCCESS )
3527 return( status );
3528 status = psa_generate_random( slot->data.raw.data,
3529 slot->data.raw.bytes );
3530 if( status != PSA_SUCCESS )
3531 {
3532 mbedtls_free( slot->data.raw.data );
3533 return( status );
3534 }
3535#if defined(MBEDTLS_DES_C)
3536 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003537 psa_des_set_key_parity( slot->data.raw.data,
3538 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003539#endif /* MBEDTLS_DES_C */
3540 }
3541 else
3542
3543#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3544 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3545 {
3546 mbedtls_rsa_context *rsa;
3547 int ret;
3548 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003549 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3550 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003551 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003552 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003553 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003554 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003555 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003556#if INT_MAX < 0xffffffff
3557 /* Check that the uint32_t value passed by the caller fits
3558 * in the range supported by this implementation. */
3559 if( p->e > INT_MAX )
3560 return( PSA_ERROR_NOT_SUPPORTED );
3561#endif
3562 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003563 }
3564 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3565 if( rsa == NULL )
3566 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3567 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3568 ret = mbedtls_rsa_gen_key( rsa,
3569 mbedtls_ctr_drbg_random,
3570 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003571 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003572 exponent );
3573 if( ret != 0 )
3574 {
3575 mbedtls_rsa_free( rsa );
3576 mbedtls_free( rsa );
3577 return( mbedtls_to_psa_error( ret ) );
3578 }
3579 slot->data.rsa = rsa;
3580 }
3581 else
3582#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3583
3584#if defined(MBEDTLS_ECP_C)
3585 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3586 {
3587 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3588 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3589 const mbedtls_ecp_curve_info *curve_info =
3590 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3591 mbedtls_ecp_keypair *ecp;
3592 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003593 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003594 return( PSA_ERROR_NOT_SUPPORTED );
3595 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3596 return( PSA_ERROR_NOT_SUPPORTED );
3597 if( curve_info->bit_size != bits )
3598 return( PSA_ERROR_INVALID_ARGUMENT );
3599 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3600 if( ecp == NULL )
3601 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3602 mbedtls_ecp_keypair_init( ecp );
3603 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3604 mbedtls_ctr_drbg_random,
3605 &global_data.ctr_drbg );
3606 if( ret != 0 )
3607 {
3608 mbedtls_ecp_keypair_free( ecp );
3609 mbedtls_free( ecp );
3610 return( mbedtls_to_psa_error( ret ) );
3611 }
3612 slot->data.ecp = ecp;
3613 }
3614 else
3615#endif /* MBEDTLS_ECP_C */
3616
3617 return( PSA_ERROR_NOT_SUPPORTED );
3618
3619 slot->type = type;
3620 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003621}
3622
3623
3624/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003625/* Module setup */
3626/****************************************************************/
3627
Gilles Peskinee59236f2018-01-27 23:32:46 +01003628void mbedtls_psa_crypto_free( void )
3629{
Jaeden Amero045bd502018-06-26 14:00:08 +01003630 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003631 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003632 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003633 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3634 mbedtls_entropy_free( &global_data.entropy );
3635 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3636}
3637
3638psa_status_t psa_crypto_init( void )
3639{
3640 int ret;
3641 const unsigned char drbg_seed[] = "PSA";
3642
3643 if( global_data.initialized != 0 )
3644 return( PSA_SUCCESS );
3645
3646 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3647 mbedtls_entropy_init( &global_data.entropy );
3648 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3649
3650 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3651 mbedtls_entropy_func,
3652 &global_data.entropy,
3653 drbg_seed, sizeof( drbg_seed ) - 1 );
3654 if( ret != 0 )
3655 goto exit;
3656
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003657 global_data.initialized = 1;
3658
Gilles Peskinee59236f2018-01-27 23:32:46 +01003659exit:
3660 if( ret != 0 )
3661 mbedtls_psa_crypto_free( );
3662 return( mbedtls_to_psa_error( ret ) );
3663}
3664
3665#endif /* MBEDTLS_PSA_CRYPTO_C */