blob: 15bab676f998d68d8d2fbe4aa80496c922998f7e [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 Peskinef6cc4352018-12-03 17:44:43 +010046/* Transitional definition while moving away from directly-accessible key
47 * slots and to a handle-only interface. */
48typedef psa_key_handle_t psa_key_slot_t;
49
Gilles Peskine5e769522018-11-20 21:59:56 +010050#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010051#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010052/* Include internal declarations that are useful for implementing persistently
53 * stored keys. */
54#include "psa_crypto_storage.h"
55
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056#include <stdlib.h>
57#include <string.h>
58#if defined(MBEDTLS_PLATFORM_C)
59#include "mbedtls/platform.h"
60#else
61#define mbedtls_calloc calloc
62#define mbedtls_free free
63#endif
64
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020066#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020067#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/blowfish.h"
69#include "mbedtls/camellia.h"
70#include "mbedtls/cipher.h"
71#include "mbedtls/ccm.h"
72#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010073#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020075#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010077#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020078#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/error.h"
80#include "mbedtls/gcm.h"
81#include "mbedtls/md2.h"
82#include "mbedtls/md4.h"
83#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010084#include "mbedtls/md.h"
85#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010086#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010087#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010089#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010090#include "mbedtls/sha1.h"
91#include "mbedtls/sha256.h"
92#include "mbedtls/sha512.h"
93#include "mbedtls/xtea.h"
94
Netanel Gonen2bcd3122018-11-19 11:53:02 +020095#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
96#include "psa_prot_internal_storage.h"
97#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010098
Gilles Peskine996deb12018-08-01 15:45:45 +020099#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
100
Gilles Peskinee59236f2018-01-27 23:32:46 +0100101/* Implementation that should never be optimized out by the compiler */
102static void mbedtls_zeroize( void *v, size_t n )
103{
104 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
105}
106
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100107/* constant-time buffer comparison */
108static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
109{
110 size_t i;
111 unsigned char diff = 0;
112
113 for( i = 0; i < n; i++ )
114 diff |= a[i] ^ b[i];
115
116 return( diff );
117}
118
119
120
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100121/****************************************************************/
122/* Global data, support functions and library management */
123/****************************************************************/
124
Gilles Peskine2d277862018-06-18 15:41:12 +0200125typedef struct
126{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100127 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300128 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200129 psa_key_lifetime_t lifetime;
Gilles Peskine69f976b2018-11-30 18:46:56 +0100130 psa_key_id_t persistent_storage_id;
Gilles Peskine961849f2018-11-30 18:54:54 +0100131 unsigned allocated : 1;
Gilles Peskine2d277862018-06-18 15:41:12 +0200132 union
133 {
134 struct raw_data
135 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100136 uint8_t *data;
137 size_t bytes;
138 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100139#if defined(MBEDTLS_RSA_C)
140 mbedtls_rsa_context *rsa;
141#endif /* MBEDTLS_RSA_C */
142#if defined(MBEDTLS_ECP_C)
143 mbedtls_ecp_keypair *ecp;
144#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100145 } data;
146} key_slot_t;
147
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200148static int key_type_is_raw_bytes( psa_key_type_t type )
149{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200150 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200151}
152
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100153/* Values for psa_global_data_t::rng_state */
154#define RNG_NOT_INITIALIZED 0
155#define RNG_INITIALIZED 1
156#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100157
Gilles Peskine2d277862018-06-18 15:41:12 +0200158typedef struct
159{
Gilles Peskine5e769522018-11-20 21:59:56 +0100160 void (* entropy_init )( mbedtls_entropy_context *ctx );
161 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100162 mbedtls_entropy_context entropy;
163 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200164 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinec6b69072018-11-20 21:42:52 +0100165 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100166 unsigned rng_state : 2;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100167 unsigned key_slots_initialized : 1;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100168} psa_global_data_t;
169
170static psa_global_data_t global_data;
171
itayzafrir0adf0fc2018-09-06 16:24:41 +0300172#define GUARD_MODULE_INITIALIZED \
173 if( global_data.initialized == 0 ) \
174 return( PSA_ERROR_BAD_STATE );
175
Gilles Peskinee59236f2018-01-27 23:32:46 +0100176static psa_status_t mbedtls_to_psa_error( int ret )
177{
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 /* If there's both a high-level code and low-level code, dispatch on
179 * the high-level code. */
180 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100181 {
182 case 0:
183 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100184
185 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
186 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
187 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
188 return( PSA_ERROR_NOT_SUPPORTED );
189 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
190 return( PSA_ERROR_HARDWARE_FAILURE );
191
192 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
193 return( PSA_ERROR_HARDWARE_FAILURE );
194
Gilles Peskine9a944802018-06-21 09:35:35 +0200195 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
196 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
197 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
198 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
199 case MBEDTLS_ERR_ASN1_INVALID_DATA:
200 return( PSA_ERROR_INVALID_ARGUMENT );
201 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
202 return( PSA_ERROR_INSUFFICIENT_MEMORY );
203 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
204 return( PSA_ERROR_BUFFER_TOO_SMALL );
205
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
207 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
208 return( PSA_ERROR_NOT_SUPPORTED );
209 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
213 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
214 return( PSA_ERROR_NOT_SUPPORTED );
215 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
216 return( PSA_ERROR_HARDWARE_FAILURE );
217
218 case MBEDTLS_ERR_CCM_BAD_INPUT:
219 return( PSA_ERROR_INVALID_ARGUMENT );
220 case MBEDTLS_ERR_CCM_AUTH_FAILED:
221 return( PSA_ERROR_INVALID_SIGNATURE );
222 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
223 return( PSA_ERROR_HARDWARE_FAILURE );
224
225 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
228 return( PSA_ERROR_INVALID_ARGUMENT );
229 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_MEMORY );
231 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
232 return( PSA_ERROR_INVALID_PADDING );
233 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
234 return( PSA_ERROR_BAD_STATE );
235 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
236 return( PSA_ERROR_INVALID_SIGNATURE );
237 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
238 return( PSA_ERROR_TAMPERING_DETECTED );
239 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
246 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
247 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
248 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
249 return( PSA_ERROR_NOT_SUPPORTED );
250 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
251 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
252
253 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
254 return( PSA_ERROR_NOT_SUPPORTED );
255 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
256 return( PSA_ERROR_HARDWARE_FAILURE );
257
Gilles Peskinee59236f2018-01-27 23:32:46 +0100258 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
259 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
260 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
261 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100262
263 case MBEDTLS_ERR_GCM_AUTH_FAILED:
264 return( PSA_ERROR_INVALID_SIGNATURE );
265 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200266 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
268 return( PSA_ERROR_HARDWARE_FAILURE );
269
270 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
271 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
272 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
273 return( PSA_ERROR_HARDWARE_FAILURE );
274
275 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
276 return( PSA_ERROR_NOT_SUPPORTED );
277 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_MD_ALLOC_FAILED:
280 return( PSA_ERROR_INSUFFICIENT_MEMORY );
281 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
282 return( PSA_ERROR_STORAGE_FAILURE );
283 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
284 return( PSA_ERROR_HARDWARE_FAILURE );
285
Gilles Peskinef76aa772018-10-29 19:24:33 +0100286 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
287 return( PSA_ERROR_STORAGE_FAILURE );
288 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
293 return( PSA_ERROR_BUFFER_TOO_SMALL );
294 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
299 return( PSA_ERROR_INVALID_ARGUMENT );
300 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
301 return( PSA_ERROR_INSUFFICIENT_MEMORY );
302
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 case MBEDTLS_ERR_PK_ALLOC_FAILED:
304 return( PSA_ERROR_INSUFFICIENT_MEMORY );
305 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
306 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
307 return( PSA_ERROR_INVALID_ARGUMENT );
308 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100309 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100310 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
311 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
314 return( PSA_ERROR_NOT_SUPPORTED );
315 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
316 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
317 return( PSA_ERROR_NOT_PERMITTED );
318 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
319 return( PSA_ERROR_INVALID_ARGUMENT );
320 case MBEDTLS_ERR_PK_INVALID_ALG:
321 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
322 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
323 return( PSA_ERROR_NOT_SUPPORTED );
324 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
325 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100326 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328
329 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
332 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
333 return( PSA_ERROR_INVALID_ARGUMENT );
334 case MBEDTLS_ERR_RSA_INVALID_PADDING:
335 return( PSA_ERROR_INVALID_PADDING );
336 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
338 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
339 return( PSA_ERROR_INVALID_ARGUMENT );
340 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
341 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
342 return( PSA_ERROR_TAMPERING_DETECTED );
343 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
344 return( PSA_ERROR_INVALID_SIGNATURE );
345 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
346 return( PSA_ERROR_BUFFER_TOO_SMALL );
347 case MBEDTLS_ERR_RSA_RNG_FAILED:
348 return( PSA_ERROR_INSUFFICIENT_MEMORY );
349 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
350 return( PSA_ERROR_NOT_SUPPORTED );
351 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
352 return( PSA_ERROR_HARDWARE_FAILURE );
353
354 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
355 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
356 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
357 return( PSA_ERROR_HARDWARE_FAILURE );
358
359 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
360 return( PSA_ERROR_INVALID_ARGUMENT );
361 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
362 return( PSA_ERROR_HARDWARE_FAILURE );
363
itayzafrir5c753392018-05-08 11:18:38 +0300364 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300365 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300366 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300367 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
368 return( PSA_ERROR_BUFFER_TOO_SMALL );
369 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
370 return( PSA_ERROR_NOT_SUPPORTED );
371 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
372 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
373 return( PSA_ERROR_INVALID_SIGNATURE );
374 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
375 return( PSA_ERROR_INSUFFICIENT_MEMORY );
376 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
377 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300378
Gilles Peskinee59236f2018-01-27 23:32:46 +0100379 default:
380 return( PSA_ERROR_UNKNOWN_ERROR );
381 }
382}
383
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200384
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200385
386
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100387/****************************************************************/
388/* Key management */
389/****************************************************************/
390
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100391#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200392static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
393{
394 switch( grpid )
395 {
396 case MBEDTLS_ECP_DP_SECP192R1:
397 return( PSA_ECC_CURVE_SECP192R1 );
398 case MBEDTLS_ECP_DP_SECP224R1:
399 return( PSA_ECC_CURVE_SECP224R1 );
400 case MBEDTLS_ECP_DP_SECP256R1:
401 return( PSA_ECC_CURVE_SECP256R1 );
402 case MBEDTLS_ECP_DP_SECP384R1:
403 return( PSA_ECC_CURVE_SECP384R1 );
404 case MBEDTLS_ECP_DP_SECP521R1:
405 return( PSA_ECC_CURVE_SECP521R1 );
406 case MBEDTLS_ECP_DP_BP256R1:
407 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
408 case MBEDTLS_ECP_DP_BP384R1:
409 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
410 case MBEDTLS_ECP_DP_BP512R1:
411 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
412 case MBEDTLS_ECP_DP_CURVE25519:
413 return( PSA_ECC_CURVE_CURVE25519 );
414 case MBEDTLS_ECP_DP_SECP192K1:
415 return( PSA_ECC_CURVE_SECP192K1 );
416 case MBEDTLS_ECP_DP_SECP224K1:
417 return( PSA_ECC_CURVE_SECP224K1 );
418 case MBEDTLS_ECP_DP_SECP256K1:
419 return( PSA_ECC_CURVE_SECP256K1 );
420 case MBEDTLS_ECP_DP_CURVE448:
421 return( PSA_ECC_CURVE_CURVE448 );
422 default:
423 return( 0 );
424 }
425}
426
Gilles Peskine12313cd2018-06-20 00:20:32 +0200427static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
428{
429 switch( curve )
430 {
431 case PSA_ECC_CURVE_SECP192R1:
432 return( MBEDTLS_ECP_DP_SECP192R1 );
433 case PSA_ECC_CURVE_SECP224R1:
434 return( MBEDTLS_ECP_DP_SECP224R1 );
435 case PSA_ECC_CURVE_SECP256R1:
436 return( MBEDTLS_ECP_DP_SECP256R1 );
437 case PSA_ECC_CURVE_SECP384R1:
438 return( MBEDTLS_ECP_DP_SECP384R1 );
439 case PSA_ECC_CURVE_SECP521R1:
440 return( MBEDTLS_ECP_DP_SECP521R1 );
441 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
442 return( MBEDTLS_ECP_DP_BP256R1 );
443 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
444 return( MBEDTLS_ECP_DP_BP384R1 );
445 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
446 return( MBEDTLS_ECP_DP_BP512R1 );
447 case PSA_ECC_CURVE_CURVE25519:
448 return( MBEDTLS_ECP_DP_CURVE25519 );
449 case PSA_ECC_CURVE_SECP192K1:
450 return( MBEDTLS_ECP_DP_SECP192K1 );
451 case PSA_ECC_CURVE_SECP224K1:
452 return( MBEDTLS_ECP_DP_SECP224K1 );
453 case PSA_ECC_CURVE_SECP256K1:
454 return( MBEDTLS_ECP_DP_SECP256K1 );
455 case PSA_ECC_CURVE_CURVE448:
456 return( MBEDTLS_ECP_DP_CURVE448 );
457 default:
458 return( MBEDTLS_ECP_DP_NONE );
459 }
460}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100461#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200462
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200463static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
464 size_t bits,
465 struct raw_data *raw )
466{
467 /* Check that the bit size is acceptable for the key type */
468 switch( type )
469 {
470 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200471 if( bits == 0 )
472 {
473 raw->bytes = 0;
474 raw->data = NULL;
475 return( PSA_SUCCESS );
476 }
477 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200478#if defined(MBEDTLS_MD_C)
479 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200480#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200481 case PSA_KEY_TYPE_DERIVE:
482 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200483#if defined(MBEDTLS_AES_C)
484 case PSA_KEY_TYPE_AES:
485 if( bits != 128 && bits != 192 && bits != 256 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
489#if defined(MBEDTLS_CAMELLIA_C)
490 case PSA_KEY_TYPE_CAMELLIA:
491 if( bits != 128 && bits != 192 && bits != 256 )
492 return( PSA_ERROR_INVALID_ARGUMENT );
493 break;
494#endif
495#if defined(MBEDTLS_DES_C)
496 case PSA_KEY_TYPE_DES:
497 if( bits != 64 && bits != 128 && bits != 192 )
498 return( PSA_ERROR_INVALID_ARGUMENT );
499 break;
500#endif
501#if defined(MBEDTLS_ARC4_C)
502 case PSA_KEY_TYPE_ARC4:
503 if( bits < 8 || bits > 2048 )
504 return( PSA_ERROR_INVALID_ARGUMENT );
505 break;
506#endif
507 default:
508 return( PSA_ERROR_NOT_SUPPORTED );
509 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200510 if( bits % 8 != 0 )
511 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200512
513 /* Allocate memory for the key */
514 raw->bytes = PSA_BITS_TO_BYTES( bits );
515 raw->data = mbedtls_calloc( 1, raw->bytes );
516 if( raw->data == NULL )
517 {
518 raw->bytes = 0;
519 return( PSA_ERROR_INSUFFICIENT_MEMORY );
520 }
521 return( PSA_SUCCESS );
522}
523
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200524#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100525/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
526 * that are not a multiple of 8) well. For example, there is only
527 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
528 * way to return the exact bit size of a key.
529 * To keep things simple, reject non-byte-aligned key sizes. */
530static psa_status_t psa_check_rsa_key_byte_aligned(
531 const mbedtls_rsa_context *rsa )
532{
533 mbedtls_mpi n;
534 psa_status_t status;
535 mbedtls_mpi_init( &n );
536 status = mbedtls_to_psa_error(
537 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
538 if( status == PSA_SUCCESS )
539 {
540 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
541 status = PSA_ERROR_NOT_SUPPORTED;
542 }
543 mbedtls_mpi_free( &n );
544 return( status );
545}
546
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200547static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
548 mbedtls_rsa_context **p_rsa )
549{
550 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
551 return( PSA_ERROR_INVALID_ARGUMENT );
552 else
553 {
554 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100555 /* The size of an RSA key doesn't have to be a multiple of 8.
556 * Mbed TLS supports non-byte-aligned key sizes, but not well.
557 * For example, mbedtls_rsa_get_len() returns the key size in
558 * bytes, not in bits. */
559 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100560 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200561 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
562 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100563 status = psa_check_rsa_key_byte_aligned( rsa );
564 if( status != PSA_SUCCESS )
565 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200566 *p_rsa = rsa;
567 return( PSA_SUCCESS );
568 }
569}
570#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
571
572#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100573/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200574static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
575 mbedtls_pk_context *pk,
576 mbedtls_ecp_keypair **p_ecp )
577{
578 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
579 return( PSA_ERROR_INVALID_ARGUMENT );
580 else
581 {
582 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
583 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
584 if( actual_curve != expected_curve )
585 return( PSA_ERROR_INVALID_ARGUMENT );
586 *p_ecp = ecp;
587 return( PSA_SUCCESS );
588 }
589}
590#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
591
Gilles Peskinef76aa772018-10-29 19:24:33 +0100592#if defined(MBEDTLS_ECP_C)
593/* Import a private key given as a byte string which is the private value
594 * in big-endian order. */
595static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
596 const uint8_t *data,
597 size_t data_length,
598 mbedtls_ecp_keypair **p_ecp )
599{
600 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
601 mbedtls_ecp_keypair *ecp = NULL;
602 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
603
604 *p_ecp = NULL;
605 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
606 if( ecp == NULL )
607 return( PSA_ERROR_INSUFFICIENT_MEMORY );
608
609 /* Load the group. */
610 status = mbedtls_to_psa_error(
611 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
612 if( status != PSA_SUCCESS )
613 goto exit;
614 /* Load the secret value. */
615 status = mbedtls_to_psa_error(
616 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
617 if( status != PSA_SUCCESS )
618 goto exit;
619 /* Validate the private key. */
620 status = mbedtls_to_psa_error(
621 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
622 if( status != PSA_SUCCESS )
623 goto exit;
624 /* Calculate the public key from the private key. */
625 status = mbedtls_to_psa_error(
626 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
627 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
628 if( status != PSA_SUCCESS )
629 goto exit;
630
631 *p_ecp = ecp;
632 return( PSA_SUCCESS );
633
634exit:
635 if( ecp != NULL )
636 {
637 mbedtls_ecp_keypair_free( ecp );
638 mbedtls_free( ecp );
639 }
640 return( status );
641}
642#endif /* defined(MBEDTLS_ECP_C) */
643
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100644/** Import key data into a slot. `slot->type` must have been set
645 * previously. This function assumes that the slot does not contain
646 * any key material yet. On failure, the slot content is unchanged. */
Darryl Green940d72c2018-07-13 13:18:51 +0100647static psa_status_t psa_import_key_into_slot( key_slot_t *slot,
648 const uint8_t *data,
649 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200651 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652
Darryl Green940d72c2018-07-13 13:18:51 +0100653 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100654 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100655 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100656 if( data_length > SIZE_MAX / 8 )
657 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100658 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200659 PSA_BYTES_TO_BITS( data_length ),
660 &slot->data.raw );
661 if( status != PSA_SUCCESS )
662 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200663 if( data_length != 0 )
664 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100665 }
666 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100668 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100669 {
Darryl Green940d72c2018-07-13 13:18:51 +0100670 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100671 data, data_length,
672 &slot->data.ecp );
673 if( status != PSA_SUCCESS )
674 return( status );
675 }
676 else
677#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100678#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100679 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
680 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100681 {
682 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100683 mbedtls_pk_context pk;
684 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200685
686 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100687 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100688 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
689 else
690 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100691 if( ret != 0 )
692 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200693
694 /* We have something that the pkparse module recognizes.
695 * If it has the expected type and passes any type-specific
696 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100697#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100698 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200699 status = psa_import_rsa_key( &pk, &slot->data.rsa );
700 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100701#endif /* MBEDTLS_RSA_C */
702#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100703 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
704 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200705 &pk, &slot->data.ecp );
706 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100707#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200708 {
709 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200710 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200711
Gilles Peskinec648d692018-06-28 08:46:13 +0200712 /* Free the content of the pk object only on error. On success,
713 * the content of the object has been stored in the slot. */
714 if( status != PSA_SUCCESS )
715 {
716 mbedtls_pk_free( &pk );
717 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100718 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100719 }
720 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100721#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100722 {
723 return( PSA_ERROR_NOT_SUPPORTED );
724 }
Darryl Green940d72c2018-07-13 13:18:51 +0100725 return( PSA_SUCCESS );
726}
727
Darryl Greend49a4992018-06-18 17:27:26 +0100728#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine69f976b2018-11-30 18:46:56 +0100729static psa_status_t psa_load_persistent_key_into_slot( key_slot_t *p_slot )
Darryl Greend49a4992018-06-18 17:27:26 +0100730{
731 psa_status_t status = PSA_SUCCESS;
732 uint8_t *key_data = NULL;
733 size_t key_data_length = 0;
734
Gilles Peskine69f976b2018-11-30 18:46:56 +0100735 status = psa_load_persistent_key( p_slot->persistent_storage_id,
736 &( p_slot )->type,
Darryl Greend49a4992018-06-18 17:27:26 +0100737 &( p_slot )->policy, &key_data,
738 &key_data_length );
739 if( status != PSA_SUCCESS )
740 goto exit;
741 status = psa_import_key_into_slot( p_slot,
742 key_data, key_data_length );
743exit:
744 psa_free_persistent_key_data( key_data, key_data_length );
745 return( status );
746}
747#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
748
Darryl Green06fd18d2018-07-16 11:21:11 +0100749/* Retrieve a key slot, occupied or not. */
Gilles Peskine961849f2018-11-30 18:54:54 +0100750static psa_status_t psa_get_key_slot( psa_key_slot_t key_or_handle,
Darryl Green06fd18d2018-07-16 11:21:11 +0100751 key_slot_t **p_slot )
752{
Gilles Peskine961849f2018-11-30 18:54:54 +0100753 psa_key_slot_t key = key_or_handle & ~PSA_KEY_HANDLE_ALLOCATED_FLAG;
754 int is_handle = ( key_or_handle & PSA_KEY_HANDLE_ALLOCATED_FLAG ) != 0;
755 psa_status_t error_if_invalid =
756 ( is_handle ?
757 PSA_ERROR_INVALID_HANDLE :
758 PSA_ERROR_INVALID_ARGUMENT );
759
Darryl Green06fd18d2018-07-16 11:21:11 +0100760 GUARD_MODULE_INITIALIZED;
761
762 /* 0 is not a valid slot number under any circumstance. This
763 * implementation provides slots number 1 to N where N is the
764 * number of available slots. */
765 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskine961849f2018-11-30 18:54:54 +0100766 return( error_if_invalid );
Darryl Green06fd18d2018-07-16 11:21:11 +0100767
768 *p_slot = &global_data.key_slots[key - 1];
Darryl Greend49a4992018-06-18 17:27:26 +0100769
Gilles Peskine961849f2018-11-30 18:54:54 +0100770 /* Allocated slots must only be accessed via a handle.
771 * Unallocated slots must only be accessed directly. */
772 if( ( *p_slot )->allocated != is_handle )
773 return( error_if_invalid );
774
Darryl Greend49a4992018-06-18 17:27:26 +0100775#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100776 if( ! ( *p_slot )->allocated &&
777 ( *p_slot )->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Darryl Greend49a4992018-06-18 17:27:26 +0100778 {
779 /* There are two circumstances this can occur: the key material has
780 * not yet been created, or the key exists in storage but has not yet
781 * been loaded into memory. */
782 if( ( *p_slot )->type == PSA_KEY_TYPE_NONE )
783 {
784 psa_status_t status = PSA_SUCCESS;
Gilles Peskine69f976b2018-11-30 18:46:56 +0100785 status = psa_load_persistent_key_into_slot( *p_slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100786 if( status != PSA_ERROR_EMPTY_SLOT )
787 return( status );
788 }
789 }
790#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
791
Darryl Green06fd18d2018-07-16 11:21:11 +0100792 return( PSA_SUCCESS );
793}
794
795/* Retrieve an empty key slot (slot with no key data, but possibly
796 * with some metadata such as a policy). */
797static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
798 key_slot_t **p_slot )
799{
800 psa_status_t status;
801 key_slot_t *slot = NULL;
802
803 *p_slot = NULL;
804
805 status = psa_get_key_slot( key, &slot );
806 if( status != PSA_SUCCESS )
807 return( status );
808
809 if( slot->type != PSA_KEY_TYPE_NONE )
810 return( PSA_ERROR_OCCUPIED_SLOT );
811
812 *p_slot = slot;
813 return( status );
814}
815
816/** Retrieve a slot which must contain a key. The key must have allow all the
817 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
818 * operations with this algorithm. */
819static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
820 key_slot_t **p_slot,
821 psa_key_usage_t usage,
822 psa_algorithm_t alg )
823{
824 psa_status_t status;
825 key_slot_t *slot = NULL;
826
827 *p_slot = NULL;
828
829 status = psa_get_key_slot( key, &slot );
830 if( status != PSA_SUCCESS )
831 return( status );
832 if( slot->type == PSA_KEY_TYPE_NONE )
833 return( PSA_ERROR_EMPTY_SLOT );
834
835 /* Enforce that usage policy for the key slot contains all the flags
836 * required by the usage parameter. There is one exception: public
837 * keys can always be exported, so we treat public key objects as
838 * if they had the export flag. */
839 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
840 usage &= ~PSA_KEY_USAGE_EXPORT;
841 if( ( slot->policy.usage & usage ) != usage )
842 return( PSA_ERROR_NOT_PERMITTED );
843 if( alg != 0 && ( alg != slot->policy.alg ) )
844 return( PSA_ERROR_NOT_PERMITTED );
845
846 *p_slot = slot;
847 return( PSA_SUCCESS );
848}
Darryl Green940d72c2018-07-13 13:18:51 +0100849
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100850/** Wipe key data from a slot. Preserve metadata such as the policy. */
Darryl Green40225ba2018-11-15 14:48:15 +0000851static psa_status_t psa_remove_key_data_from_memory( key_slot_t *slot )
852{
853 if( slot->type == PSA_KEY_TYPE_NONE )
854 {
855 /* No key material to clean. */
856 }
857 else if( key_type_is_raw_bytes( slot->type ) )
858 {
859 mbedtls_free( slot->data.raw.data );
860 }
861 else
862#if defined(MBEDTLS_RSA_C)
863 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
864 {
865 mbedtls_rsa_free( slot->data.rsa );
866 mbedtls_free( slot->data.rsa );
867 }
868 else
869#endif /* defined(MBEDTLS_RSA_C) */
870#if defined(MBEDTLS_ECP_C)
871 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
872 {
873 mbedtls_ecp_keypair_free( slot->data.ecp );
874 mbedtls_free( slot->data.ecp );
875 }
876 else
877#endif /* defined(MBEDTLS_ECP_C) */
878 {
879 /* Shouldn't happen: the key type is not any type that we
880 * put in. */
881 return( PSA_ERROR_TAMPERING_DETECTED );
882 }
883
884 return( PSA_SUCCESS );
885}
886
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100887/** Completely wipe a slot in memory, including its policy.
888 * Persistent storage is not affected. */
889static psa_status_t psa_wipe_key_slot( key_slot_t *slot )
890{
891 psa_status_t status = psa_remove_key_data_from_memory( slot );
892 /* At this point, key material and other type-specific content has
893 * been wiped. Clear remaining metadata. We can call memset and not
894 * zeroize because the metadata is not particularly sensitive. */
895 memset( slot, 0, sizeof( *slot ) );
896 return( status );
897}
898
Gilles Peskine961849f2018-11-30 18:54:54 +0100899/* A slot is available if nothing has been set in it: default lifetime
900 * and policy, no key type. */
901static int psa_internal_is_slot_available( key_slot_t *slot )
902{
903 if( slot->allocated )
904 return( 0 );
905 if( slot->type != PSA_KEY_TYPE_NONE )
906 return( 0 );
907 if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE )
908 return( 0 );
909 if( slot->policy.usage != 0 || slot->policy.alg != 0 )
910 return( 0 );
911 return( 1 );
912}
913
914psa_status_t psa_internal_allocate_key_slot( psa_key_handle_t *handle )
915{
916 psa_key_slot_t key;
917 for( key = PSA_KEY_SLOT_COUNT; key != 0; --( key ) )
918 {
919 key_slot_t *slot = &global_data.key_slots[key - 1];
920 if( psa_internal_is_slot_available( slot ) )
921 {
922 slot->allocated = 1;
923 *handle = key | PSA_KEY_HANDLE_ALLOCATED_FLAG;
924 return( PSA_SUCCESS );
925 }
926 }
927 return( PSA_ERROR_INSUFFICIENT_MEMORY );
928}
929
930psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
931 psa_key_id_t id )
932{
933 key_slot_t *slot;
934 psa_status_t status;
935
936 /* Reject id=0 because by general library conventions, 0 is an invalid
937 * value wherever possible. */
938 if( id == 0 )
939 return( PSA_ERROR_INVALID_ARGUMENT );
940 /* Reject high values because the file names are reserved for the
941 * library's internal use. */
942 if( id >= 0xffff0000 )
943 return( PSA_ERROR_INVALID_ARGUMENT );
944 /* Reject values that don't fit in the key slot number type.
945 * This is a temporary limitation due to the library's internal
946 * plumbing. */
947 if( id > (psa_key_slot_t)( -1 ) )
948 return( PSA_ERROR_INVALID_ARGUMENT );
949
950 status = psa_get_key_slot( handle, &slot );
951 if( status != PSA_SUCCESS )
952 return( status );
953
954 slot->lifetime = PSA_KEY_LIFETIME_PERSISTENT;
955 slot->persistent_storage_id = id;
956 status = psa_load_persistent_key_into_slot( slot );
957
958 return( status );
959}
960
961psa_status_t psa_internal_release_key_slot( psa_key_handle_t handle )
962{
963 psa_key_slot_t key;
964 key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100965 /* Don't call psa_get_key_slot() so as not to trigger its automatic
966 * loading of persistent key data. */
967 if( ( handle & PSA_KEY_HANDLE_ALLOCATED_FLAG ) == 0 )
968 return( PSA_ERROR_INVALID_HANDLE );
969 key = handle & ~PSA_KEY_HANDLE_ALLOCATED_FLAG;
970 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
971 return( PSA_ERROR_INVALID_HANDLE );
972 slot = &global_data.key_slots[key - 1];
973 if( ! slot->allocated )
974 return( PSA_ERROR_INVALID_HANDLE );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100975 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100976}
977
Darryl Green940d72c2018-07-13 13:18:51 +0100978psa_status_t psa_import_key( psa_key_slot_t key,
979 psa_key_type_t type,
980 const uint8_t *data,
981 size_t data_length )
982{
983 key_slot_t *slot;
984 psa_status_t status;
985
986 status = psa_get_empty_key_slot( key, &slot );
987 if( status != PSA_SUCCESS )
988 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989
990 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100991
992 status = psa_import_key_into_slot( slot, data, data_length );
993 if( status != PSA_SUCCESS )
994 {
995 slot->type = PSA_KEY_TYPE_NONE;
996 return( status );
997 }
998
Darryl Greend49a4992018-06-18 17:27:26 +0100999#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1000 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1001 {
1002 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001003 status = psa_save_persistent_key( slot->persistent_storage_id,
1004 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +01001005 data_length );
1006 if( status != PSA_SUCCESS )
1007 {
1008 (void) psa_remove_key_data_from_memory( slot );
1009 slot->type = PSA_KEY_TYPE_NONE;
1010 }
1011 }
1012#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1013
1014 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015}
1016
Gilles Peskine2d277862018-06-18 15:41:12 +02001017psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018{
1019 key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +01001020 psa_status_t status = PSA_SUCCESS;
1021 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001023 status = psa_get_key_slot( key, &slot );
1024 if( status != PSA_SUCCESS )
1025 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +01001026#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1027 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1028 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001029 storage_status =
1030 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +01001031 }
1032#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001033 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001034 if( status != PSA_SUCCESS )
1035 return( status );
1036 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001037}
1038
Gilles Peskineb870b182018-07-06 16:02:09 +02001039/* Return the size of the key in the given slot, in bits. */
1040static size_t psa_get_key_bits( const key_slot_t *slot )
1041{
1042 if( key_type_is_raw_bytes( slot->type ) )
1043 return( slot->data.raw.bytes * 8 );
1044#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001045 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001046 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001047#endif /* defined(MBEDTLS_RSA_C) */
1048#if defined(MBEDTLS_ECP_C)
1049 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1050 return( slot->data.ecp->grp.pbits );
1051#endif /* defined(MBEDTLS_ECP_C) */
1052 /* Shouldn't happen except on an empty slot. */
1053 return( 0 );
1054}
1055
Gilles Peskine2d277862018-06-18 15:41:12 +02001056psa_status_t psa_get_key_information( psa_key_slot_t key,
1057 psa_key_type_t *type,
1058 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001059{
1060 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001061 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001062
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001063 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001064 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001065 if( bits != NULL )
1066 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001067 status = psa_get_key_slot( key, &slot );
1068 if( status != PSA_SUCCESS )
1069 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001070
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001071 if( slot->type == PSA_KEY_TYPE_NONE )
1072 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001073 if( type != NULL )
1074 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001075 if( bits != NULL )
1076 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001077 return( PSA_SUCCESS );
1078}
1079
Darryl Greendd8fb772018-11-07 16:00:44 +00001080static psa_status_t psa_internal_export_key( key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001081 uint8_t *data,
1082 size_t data_size,
1083 size_t *data_length,
1084 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001085{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001086 *data_length = 0;
1087
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001088 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001089 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001090
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001091 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001092 {
1093 if( slot->data.raw.bytes > data_size )
1094 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001095 if( data_size != 0 )
1096 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001097 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001098 memset( data + slot->data.raw.bytes, 0,
1099 data_size - slot->data.raw.bytes );
1100 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001101 *data_length = slot->data.raw.bytes;
1102 return( PSA_SUCCESS );
1103 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001104#if defined(MBEDTLS_ECP_C)
1105 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1106 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001107 psa_status_t status;
1108
Gilles Peskine188c71e2018-10-29 19:26:02 +01001109 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1110 if( bytes > data_size )
1111 return( PSA_ERROR_BUFFER_TOO_SMALL );
1112 status = mbedtls_to_psa_error(
1113 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1114 if( status != PSA_SUCCESS )
1115 return( status );
1116 memset( data + bytes, 0, data_size - bytes );
1117 *data_length = bytes;
1118 return( PSA_SUCCESS );
1119 }
1120#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001121 else
Moran Peker17e36e12018-05-02 12:55:20 +03001122 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001123#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001124 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001125 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001126 {
Moran Pekera998bc62018-04-16 18:16:20 +03001127 mbedtls_pk_context pk;
1128 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001129 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001130 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001131#if defined(MBEDTLS_RSA_C)
1132 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001133 pk.pk_info = &mbedtls_rsa_info;
1134 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001135#else
1136 return( PSA_ERROR_NOT_SUPPORTED );
1137#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001138 }
1139 else
1140 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001141#if defined(MBEDTLS_ECP_C)
1142 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001143 pk.pk_info = &mbedtls_eckey_info;
1144 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001145#else
1146 return( PSA_ERROR_NOT_SUPPORTED );
1147#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001148 }
Moran Pekerd7326592018-05-29 16:56:39 +03001149 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001150 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001151 else
1152 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001153 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001154 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001155 /* If data_size is 0 then data may be NULL and then the
1156 * call to memset would have undefined behavior. */
1157 if( data_size != 0 )
1158 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001159 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001160 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001161 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1162 * Move the data to the beginning and erase remaining data
1163 * at the original location. */
1164 if( 2 * (size_t) ret <= data_size )
1165 {
1166 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001167 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001168 }
1169 else if( (size_t) ret < data_size )
1170 {
1171 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001172 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001173 }
Moran Pekera998bc62018-04-16 18:16:20 +03001174 *data_length = ret;
1175 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001176 }
1177 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001178#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001179 {
1180 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001181 it is valid for a special-purpose implementation to omit
1182 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001183 return( PSA_ERROR_NOT_SUPPORTED );
1184 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001185 }
1186}
1187
Gilles Peskine2d277862018-06-18 15:41:12 +02001188psa_status_t psa_export_key( psa_key_slot_t key,
1189 uint8_t *data,
1190 size_t data_size,
1191 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001192{
Darryl Greendd8fb772018-11-07 16:00:44 +00001193 key_slot_t *slot;
1194 psa_status_t status;
1195
1196 /* Set the key to empty now, so that even when there are errors, we always
1197 * set data_length to a value between 0 and data_size. On error, setting
1198 * the key to empty is a good choice because an empty key representation is
1199 * unlikely to be accepted anywhere. */
1200 *data_length = 0;
1201
1202 /* Export requires the EXPORT flag. There is an exception for public keys,
1203 * which don't require any flag, but psa_get_key_from_slot takes
1204 * care of this. */
1205 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
1206 if( status != PSA_SUCCESS )
1207 return( status );
1208 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001209 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001210}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001211
Gilles Peskine2d277862018-06-18 15:41:12 +02001212psa_status_t psa_export_public_key( psa_key_slot_t key,
1213 uint8_t *data,
1214 size_t data_size,
1215 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001216{
Darryl Greendd8fb772018-11-07 16:00:44 +00001217 key_slot_t *slot;
1218 psa_status_t status;
1219
1220 /* Set the key to empty now, so that even when there are errors, we always
1221 * set data_length to a value between 0 and data_size. On error, setting
1222 * the key to empty is a good choice because an empty key representation is
1223 * unlikely to be accepted anywhere. */
1224 *data_length = 0;
1225
1226 /* Exporting a public key doesn't require a usage flag. */
1227 status = psa_get_key_from_slot( key, &slot, 0, 0 );
1228 if( status != PSA_SUCCESS )
1229 return( status );
1230 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001231 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001232}
1233
Darryl Green0c6575a2018-11-07 16:05:30 +00001234#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine69f976b2018-11-30 18:46:56 +01001235static psa_status_t psa_save_generated_persistent_key( key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001236 size_t bits )
1237{
1238 psa_status_t status;
1239 uint8_t *data;
1240 size_t key_length;
1241 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1242 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001243 if( data == NULL )
1244 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001245 /* Get key data in export format */
1246 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1247 if( status != PSA_SUCCESS )
1248 {
1249 slot->type = PSA_KEY_TYPE_NONE;
1250 goto exit;
1251 }
1252 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001253 status = psa_save_persistent_key( slot->persistent_storage_id,
1254 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001255 data, key_length );
1256 if( status != PSA_SUCCESS )
1257 {
1258 slot->type = PSA_KEY_TYPE_NONE;
1259 }
1260exit:
1261 mbedtls_zeroize( data, key_length );
1262 mbedtls_free( data );
1263 return( status );
1264}
1265#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1266
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001267
1268
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001270/* Message digests */
1271/****************************************************************/
1272
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001273static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001274{
1275 switch( alg )
1276 {
1277#if defined(MBEDTLS_MD2_C)
1278 case PSA_ALG_MD2:
1279 return( &mbedtls_md2_info );
1280#endif
1281#if defined(MBEDTLS_MD4_C)
1282 case PSA_ALG_MD4:
1283 return( &mbedtls_md4_info );
1284#endif
1285#if defined(MBEDTLS_MD5_C)
1286 case PSA_ALG_MD5:
1287 return( &mbedtls_md5_info );
1288#endif
1289#if defined(MBEDTLS_RIPEMD160_C)
1290 case PSA_ALG_RIPEMD160:
1291 return( &mbedtls_ripemd160_info );
1292#endif
1293#if defined(MBEDTLS_SHA1_C)
1294 case PSA_ALG_SHA_1:
1295 return( &mbedtls_sha1_info );
1296#endif
1297#if defined(MBEDTLS_SHA256_C)
1298 case PSA_ALG_SHA_224:
1299 return( &mbedtls_sha224_info );
1300 case PSA_ALG_SHA_256:
1301 return( &mbedtls_sha256_info );
1302#endif
1303#if defined(MBEDTLS_SHA512_C)
1304 case PSA_ALG_SHA_384:
1305 return( &mbedtls_sha384_info );
1306 case PSA_ALG_SHA_512:
1307 return( &mbedtls_sha512_info );
1308#endif
1309 default:
1310 return( NULL );
1311 }
1312}
1313
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001314psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1315{
1316 switch( operation->alg )
1317 {
Gilles Peskine81736312018-06-26 15:04:31 +02001318 case 0:
1319 /* The object has (apparently) been initialized but it is not
1320 * in use. It's ok to call abort on such an object, and there's
1321 * nothing to do. */
1322 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001323#if defined(MBEDTLS_MD2_C)
1324 case PSA_ALG_MD2:
1325 mbedtls_md2_free( &operation->ctx.md2 );
1326 break;
1327#endif
1328#if defined(MBEDTLS_MD4_C)
1329 case PSA_ALG_MD4:
1330 mbedtls_md4_free( &operation->ctx.md4 );
1331 break;
1332#endif
1333#if defined(MBEDTLS_MD5_C)
1334 case PSA_ALG_MD5:
1335 mbedtls_md5_free( &operation->ctx.md5 );
1336 break;
1337#endif
1338#if defined(MBEDTLS_RIPEMD160_C)
1339 case PSA_ALG_RIPEMD160:
1340 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1341 break;
1342#endif
1343#if defined(MBEDTLS_SHA1_C)
1344 case PSA_ALG_SHA_1:
1345 mbedtls_sha1_free( &operation->ctx.sha1 );
1346 break;
1347#endif
1348#if defined(MBEDTLS_SHA256_C)
1349 case PSA_ALG_SHA_224:
1350 case PSA_ALG_SHA_256:
1351 mbedtls_sha256_free( &operation->ctx.sha256 );
1352 break;
1353#endif
1354#if defined(MBEDTLS_SHA512_C)
1355 case PSA_ALG_SHA_384:
1356 case PSA_ALG_SHA_512:
1357 mbedtls_sha512_free( &operation->ctx.sha512 );
1358 break;
1359#endif
1360 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001361 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001362 }
1363 operation->alg = 0;
1364 return( PSA_SUCCESS );
1365}
1366
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001367psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001368 psa_algorithm_t alg )
1369{
1370 int ret;
1371 operation->alg = 0;
1372 switch( alg )
1373 {
1374#if defined(MBEDTLS_MD2_C)
1375 case PSA_ALG_MD2:
1376 mbedtls_md2_init( &operation->ctx.md2 );
1377 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1378 break;
1379#endif
1380#if defined(MBEDTLS_MD4_C)
1381 case PSA_ALG_MD4:
1382 mbedtls_md4_init( &operation->ctx.md4 );
1383 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1384 break;
1385#endif
1386#if defined(MBEDTLS_MD5_C)
1387 case PSA_ALG_MD5:
1388 mbedtls_md5_init( &operation->ctx.md5 );
1389 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1390 break;
1391#endif
1392#if defined(MBEDTLS_RIPEMD160_C)
1393 case PSA_ALG_RIPEMD160:
1394 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1395 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1396 break;
1397#endif
1398#if defined(MBEDTLS_SHA1_C)
1399 case PSA_ALG_SHA_1:
1400 mbedtls_sha1_init( &operation->ctx.sha1 );
1401 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1402 break;
1403#endif
1404#if defined(MBEDTLS_SHA256_C)
1405 case PSA_ALG_SHA_224:
1406 mbedtls_sha256_init( &operation->ctx.sha256 );
1407 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1408 break;
1409 case PSA_ALG_SHA_256:
1410 mbedtls_sha256_init( &operation->ctx.sha256 );
1411 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1412 break;
1413#endif
1414#if defined(MBEDTLS_SHA512_C)
1415 case PSA_ALG_SHA_384:
1416 mbedtls_sha512_init( &operation->ctx.sha512 );
1417 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1418 break;
1419 case PSA_ALG_SHA_512:
1420 mbedtls_sha512_init( &operation->ctx.sha512 );
1421 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1422 break;
1423#endif
1424 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001425 return( PSA_ALG_IS_HASH( alg ) ?
1426 PSA_ERROR_NOT_SUPPORTED :
1427 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001428 }
1429 if( ret == 0 )
1430 operation->alg = alg;
1431 else
1432 psa_hash_abort( operation );
1433 return( mbedtls_to_psa_error( ret ) );
1434}
1435
1436psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1437 const uint8_t *input,
1438 size_t input_length )
1439{
1440 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001441
1442 /* Don't require hash implementations to behave correctly on a
1443 * zero-length input, which may have an invalid pointer. */
1444 if( input_length == 0 )
1445 return( PSA_SUCCESS );
1446
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001447 switch( operation->alg )
1448 {
1449#if defined(MBEDTLS_MD2_C)
1450 case PSA_ALG_MD2:
1451 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1452 input, input_length );
1453 break;
1454#endif
1455#if defined(MBEDTLS_MD4_C)
1456 case PSA_ALG_MD4:
1457 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1458 input, input_length );
1459 break;
1460#endif
1461#if defined(MBEDTLS_MD5_C)
1462 case PSA_ALG_MD5:
1463 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1464 input, input_length );
1465 break;
1466#endif
1467#if defined(MBEDTLS_RIPEMD160_C)
1468 case PSA_ALG_RIPEMD160:
1469 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1470 input, input_length );
1471 break;
1472#endif
1473#if defined(MBEDTLS_SHA1_C)
1474 case PSA_ALG_SHA_1:
1475 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1476 input, input_length );
1477 break;
1478#endif
1479#if defined(MBEDTLS_SHA256_C)
1480 case PSA_ALG_SHA_224:
1481 case PSA_ALG_SHA_256:
1482 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1483 input, input_length );
1484 break;
1485#endif
1486#if defined(MBEDTLS_SHA512_C)
1487 case PSA_ALG_SHA_384:
1488 case PSA_ALG_SHA_512:
1489 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1490 input, input_length );
1491 break;
1492#endif
1493 default:
1494 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1495 break;
1496 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001497
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001498 if( ret != 0 )
1499 psa_hash_abort( operation );
1500 return( mbedtls_to_psa_error( ret ) );
1501}
1502
1503psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1504 uint8_t *hash,
1505 size_t hash_size,
1506 size_t *hash_length )
1507{
itayzafrir40835d42018-08-02 13:14:17 +03001508 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001509 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001510 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001511
1512 /* Fill the output buffer with something that isn't a valid hash
1513 * (barring an attack on the hash and deliberately-crafted input),
1514 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001515 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001516 /* If hash_size is 0 then hash may be NULL and then the
1517 * call to memset would have undefined behavior. */
1518 if( hash_size != 0 )
1519 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001520
1521 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001522 {
1523 status = PSA_ERROR_BUFFER_TOO_SMALL;
1524 goto exit;
1525 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001526
1527 switch( operation->alg )
1528 {
1529#if defined(MBEDTLS_MD2_C)
1530 case PSA_ALG_MD2:
1531 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1532 break;
1533#endif
1534#if defined(MBEDTLS_MD4_C)
1535 case PSA_ALG_MD4:
1536 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1537 break;
1538#endif
1539#if defined(MBEDTLS_MD5_C)
1540 case PSA_ALG_MD5:
1541 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1542 break;
1543#endif
1544#if defined(MBEDTLS_RIPEMD160_C)
1545 case PSA_ALG_RIPEMD160:
1546 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1547 break;
1548#endif
1549#if defined(MBEDTLS_SHA1_C)
1550 case PSA_ALG_SHA_1:
1551 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1552 break;
1553#endif
1554#if defined(MBEDTLS_SHA256_C)
1555 case PSA_ALG_SHA_224:
1556 case PSA_ALG_SHA_256:
1557 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1558 break;
1559#endif
1560#if defined(MBEDTLS_SHA512_C)
1561 case PSA_ALG_SHA_384:
1562 case PSA_ALG_SHA_512:
1563 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1564 break;
1565#endif
1566 default:
1567 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1568 break;
1569 }
itayzafrir40835d42018-08-02 13:14:17 +03001570 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001571
itayzafrir40835d42018-08-02 13:14:17 +03001572exit:
1573 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001574 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001575 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001576 return( psa_hash_abort( operation ) );
1577 }
1578 else
1579 {
1580 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001581 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001582 }
1583}
1584
Gilles Peskine2d277862018-06-18 15:41:12 +02001585psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1586 const uint8_t *hash,
1587 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001588{
1589 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1590 size_t actual_hash_length;
1591 psa_status_t status = psa_hash_finish( operation,
1592 actual_hash, sizeof( actual_hash ),
1593 &actual_hash_length );
1594 if( status != PSA_SUCCESS )
1595 return( status );
1596 if( actual_hash_length != hash_length )
1597 return( PSA_ERROR_INVALID_SIGNATURE );
1598 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1599 return( PSA_ERROR_INVALID_SIGNATURE );
1600 return( PSA_SUCCESS );
1601}
1602
1603
1604
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001605/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001606/* MAC */
1607/****************************************************************/
1608
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001609static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001610 psa_algorithm_t alg,
1611 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001612 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001613 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001614{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001615 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001616 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001617
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001618 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001619 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001620
Gilles Peskine8c9def32018-02-08 10:02:12 +01001621 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1622 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001623 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001625 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001626 mode = MBEDTLS_MODE_STREAM;
1627 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001628 case PSA_ALG_CTR:
1629 mode = MBEDTLS_MODE_CTR;
1630 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001631 case PSA_ALG_CFB:
1632 mode = MBEDTLS_MODE_CFB;
1633 break;
1634 case PSA_ALG_OFB:
1635 mode = MBEDTLS_MODE_OFB;
1636 break;
1637 case PSA_ALG_CBC_NO_PADDING:
1638 mode = MBEDTLS_MODE_CBC;
1639 break;
1640 case PSA_ALG_CBC_PKCS7:
1641 mode = MBEDTLS_MODE_CBC;
1642 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001643 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001644 mode = MBEDTLS_MODE_CCM;
1645 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001646 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001647 mode = MBEDTLS_MODE_GCM;
1648 break;
1649 default:
1650 return( NULL );
1651 }
1652 }
1653 else if( alg == PSA_ALG_CMAC )
1654 mode = MBEDTLS_MODE_ECB;
1655 else if( alg == PSA_ALG_GMAC )
1656 mode = MBEDTLS_MODE_GCM;
1657 else
1658 return( NULL );
1659
1660 switch( key_type )
1661 {
1662 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001663 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001664 break;
1665 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001666 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1667 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001668 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001669 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001670 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001671 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001672 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1673 * but two-key Triple-DES is functionally three-key Triple-DES
1674 * with K1=K3, so that's how we present it to mbedtls. */
1675 if( key_bits == 128 )
1676 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001677 break;
1678 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001679 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001680 break;
1681 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001682 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683 break;
1684 default:
1685 return( NULL );
1686 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001687 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001688 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001689
Jaeden Amero23bbb752018-06-26 14:16:54 +01001690 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1691 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692}
1693
Gilles Peskinea05219c2018-11-16 16:02:56 +01001694#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001695static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001696{
Gilles Peskine2d277862018-06-18 15:41:12 +02001697 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001698 {
1699 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001700 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001701 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001702 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001703 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001704 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001705 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001706 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001707 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001708 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001709 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001710 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001711 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001712 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001713 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001714 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001715 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001716 return( 128 );
1717 default:
1718 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001719 }
1720}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001721#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001722
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001723/* Initialize the MAC operation structure. Once this function has been
1724 * called, psa_mac_abort can run and will do the right thing. */
1725static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1726 psa_algorithm_t alg )
1727{
1728 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1729
1730 operation->alg = alg;
1731 operation->key_set = 0;
1732 operation->iv_set = 0;
1733 operation->iv_required = 0;
1734 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001735 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001736
1737#if defined(MBEDTLS_CMAC_C)
1738 if( alg == PSA_ALG_CMAC )
1739 {
1740 operation->iv_required = 0;
1741 mbedtls_cipher_init( &operation->ctx.cmac );
1742 status = PSA_SUCCESS;
1743 }
1744 else
1745#endif /* MBEDTLS_CMAC_C */
1746#if defined(MBEDTLS_MD_C)
1747 if( PSA_ALG_IS_HMAC( operation->alg ) )
1748 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001749 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1750 operation->ctx.hmac.hash_ctx.alg = 0;
1751 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001752 }
1753 else
1754#endif /* MBEDTLS_MD_C */
1755 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001756 if( ! PSA_ALG_IS_MAC( alg ) )
1757 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001758 }
1759
1760 if( status != PSA_SUCCESS )
1761 memset( operation, 0, sizeof( *operation ) );
1762 return( status );
1763}
1764
Gilles Peskine01126fa2018-07-12 17:04:55 +02001765#if defined(MBEDTLS_MD_C)
1766static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1767{
1768 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1769 return( psa_hash_abort( &hmac->hash_ctx ) );
1770}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001771
1772static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1773{
1774 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1775 memset( hmac, 0, sizeof( *hmac ) );
1776}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001777#endif /* MBEDTLS_MD_C */
1778
Gilles Peskine8c9def32018-02-08 10:02:12 +01001779psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1780{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001781 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001782 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001783 /* The object has (apparently) been initialized but it is not
1784 * in use. It's ok to call abort on such an object, and there's
1785 * nothing to do. */
1786 return( PSA_SUCCESS );
1787 }
1788 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001789#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001790 if( operation->alg == PSA_ALG_CMAC )
1791 {
1792 mbedtls_cipher_free( &operation->ctx.cmac );
1793 }
1794 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001795#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001796#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001797 if( PSA_ALG_IS_HMAC( operation->alg ) )
1798 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001799 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001800 }
1801 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001802#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001803 {
1804 /* Sanity check (shouldn't happen: operation->alg should
1805 * always have been initialized to a valid value). */
1806 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001807 }
Moran Peker41deec42018-04-04 15:43:05 +03001808
Gilles Peskine8c9def32018-02-08 10:02:12 +01001809 operation->alg = 0;
1810 operation->key_set = 0;
1811 operation->iv_set = 0;
1812 operation->iv_required = 0;
1813 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001814 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001815
Gilles Peskine8c9def32018-02-08 10:02:12 +01001816 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001817
1818bad_state:
1819 /* If abort is called on an uninitialized object, we can't trust
1820 * anything. Wipe the object in case it contains confidential data.
1821 * This may result in a memory leak if a pointer gets overwritten,
1822 * but it's too late to do anything about this. */
1823 memset( operation, 0, sizeof( *operation ) );
1824 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001825}
1826
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001827#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001828static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001829 size_t key_bits,
1830 key_slot_t *slot,
1831 const mbedtls_cipher_info_t *cipher_info )
1832{
1833 int ret;
1834
1835 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001836
1837 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1838 if( ret != 0 )
1839 return( ret );
1840
1841 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1842 slot->data.raw.data,
1843 key_bits );
1844 return( ret );
1845}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001846#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001847
Gilles Peskine248051a2018-06-20 16:09:38 +02001848#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001849static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1850 const uint8_t *key,
1851 size_t key_length,
1852 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001853{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001854 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001855 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001856 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001857 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001858 psa_status_t status;
1859
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001860 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1861 * overflow below. This should never trigger if the hash algorithm
1862 * is implemented correctly. */
1863 /* The size checks against the ipad and opad buffers cannot be written
1864 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1865 * because that triggers -Wlogical-op on GCC 7.3. */
1866 if( block_size > sizeof( ipad ) )
1867 return( PSA_ERROR_NOT_SUPPORTED );
1868 if( block_size > sizeof( hmac->opad ) )
1869 return( PSA_ERROR_NOT_SUPPORTED );
1870 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001871 return( PSA_ERROR_NOT_SUPPORTED );
1872
Gilles Peskined223b522018-06-11 18:12:58 +02001873 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001874 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001875 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001876 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001877 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001878 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001879 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001880 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001881 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001882 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001883 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001884 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001885 }
Gilles Peskine96889972018-07-12 17:07:03 +02001886 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1887 * but it is permitted. It is common when HMAC is used in HKDF, for
1888 * example. Don't call `memcpy` in the 0-length because `key` could be
1889 * an invalid pointer which would make the behavior undefined. */
1890 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001891 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001892
Gilles Peskined223b522018-06-11 18:12:58 +02001893 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1894 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001895 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001896 ipad[i] ^= 0x36;
1897 memset( ipad + key_length, 0x36, block_size - key_length );
1898
1899 /* Copy the key material from ipad to opad, flipping the requisite bits,
1900 * and filling the rest of opad with the requisite constant. */
1901 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001902 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1903 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001904
Gilles Peskine01126fa2018-07-12 17:04:55 +02001905 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001906 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001907 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001908
Gilles Peskine01126fa2018-07-12 17:04:55 +02001909 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001910
1911cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001912 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001913
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001914 return( status );
1915}
Gilles Peskine248051a2018-06-20 16:09:38 +02001916#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001917
Gilles Peskine89167cb2018-07-08 20:12:23 +02001918static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1919 psa_key_slot_t key,
1920 psa_algorithm_t alg,
1921 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001922{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001923 psa_status_t status;
1924 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001925 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001926 psa_key_usage_t usage =
1927 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001928 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001929 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001930
Gilles Peskined911eb72018-08-14 15:18:45 +02001931 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001932 if( status != PSA_SUCCESS )
1933 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001934 if( is_sign )
1935 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001936
Gilles Peskine89167cb2018-07-08 20:12:23 +02001937 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001938 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001939 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001940 key_bits = psa_get_key_bits( slot );
1941
Gilles Peskine8c9def32018-02-08 10:02:12 +01001942#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001943 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001944 {
1945 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001946 mbedtls_cipher_info_from_psa( full_length_alg,
1947 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001948 int ret;
1949 if( cipher_info == NULL )
1950 {
1951 status = PSA_ERROR_NOT_SUPPORTED;
1952 goto exit;
1953 }
1954 operation->mac_size = cipher_info->block_size;
1955 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1956 status = mbedtls_to_psa_error( ret );
1957 }
1958 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001959#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001960#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001961 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001962 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001963 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001964 if( hash_alg == 0 )
1965 {
1966 status = PSA_ERROR_NOT_SUPPORTED;
1967 goto exit;
1968 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001969
1970 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1971 /* Sanity check. This shouldn't fail on a valid configuration. */
1972 if( operation->mac_size == 0 ||
1973 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1974 {
1975 status = PSA_ERROR_NOT_SUPPORTED;
1976 goto exit;
1977 }
1978
Gilles Peskine01126fa2018-07-12 17:04:55 +02001979 if( slot->type != PSA_KEY_TYPE_HMAC )
1980 {
1981 status = PSA_ERROR_INVALID_ARGUMENT;
1982 goto exit;
1983 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001984
Gilles Peskine01126fa2018-07-12 17:04:55 +02001985 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1986 slot->data.raw.data,
1987 slot->data.raw.bytes,
1988 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001989 }
1990 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001991#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001992 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001993 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001994 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001995 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001996
Gilles Peskined911eb72018-08-14 15:18:45 +02001997 if( truncated == 0 )
1998 {
1999 /* The "normal" case: untruncated algorithm. Nothing to do. */
2000 }
2001 else if( truncated < 4 )
2002 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002003 /* A very short MAC is too short for security since it can be
2004 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2005 * so we make this our minimum, even though 32 bits is still
2006 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002007 status = PSA_ERROR_NOT_SUPPORTED;
2008 }
2009 else if( truncated > operation->mac_size )
2010 {
2011 /* It's impossible to "truncate" to a larger length. */
2012 status = PSA_ERROR_INVALID_ARGUMENT;
2013 }
2014 else
2015 operation->mac_size = truncated;
2016
Gilles Peskinefbfac682018-07-08 20:51:54 +02002017exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002018 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002019 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002020 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002021 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002022 else
2023 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002024 operation->key_set = 1;
2025 }
2026 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002027}
2028
Gilles Peskine89167cb2018-07-08 20:12:23 +02002029psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
2030 psa_key_slot_t key,
2031 psa_algorithm_t alg )
2032{
2033 return( psa_mac_setup( operation, key, alg, 1 ) );
2034}
2035
2036psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
2037 psa_key_slot_t key,
2038 psa_algorithm_t alg )
2039{
2040 return( psa_mac_setup( operation, key, alg, 0 ) );
2041}
2042
Gilles Peskine8c9def32018-02-08 10:02:12 +01002043psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2044 const uint8_t *input,
2045 size_t input_length )
2046{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002047 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002048 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002049 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002050 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002051 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002052 operation->has_input = 1;
2053
Gilles Peskine8c9def32018-02-08 10:02:12 +01002054#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002055 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002056 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002057 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2058 input, input_length );
2059 status = mbedtls_to_psa_error( ret );
2060 }
2061 else
2062#endif /* MBEDTLS_CMAC_C */
2063#if defined(MBEDTLS_MD_C)
2064 if( PSA_ALG_IS_HMAC( operation->alg ) )
2065 {
2066 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2067 input_length );
2068 }
2069 else
2070#endif /* MBEDTLS_MD_C */
2071 {
2072 /* This shouldn't happen if `operation` was initialized by
2073 * a setup function. */
2074 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002075 }
2076
Gilles Peskinefbfac682018-07-08 20:51:54 +02002077cleanup:
2078 if( status != PSA_SUCCESS )
2079 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002080 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002081}
2082
Gilles Peskine01126fa2018-07-12 17:04:55 +02002083#if defined(MBEDTLS_MD_C)
2084static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2085 uint8_t *mac,
2086 size_t mac_size )
2087{
2088 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2089 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2090 size_t hash_size = 0;
2091 size_t block_size = psa_get_hash_block_size( hash_alg );
2092 psa_status_t status;
2093
Gilles Peskine01126fa2018-07-12 17:04:55 +02002094 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2095 if( status != PSA_SUCCESS )
2096 return( status );
2097 /* From here on, tmp needs to be wiped. */
2098
2099 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2100 if( status != PSA_SUCCESS )
2101 goto exit;
2102
2103 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2104 if( status != PSA_SUCCESS )
2105 goto exit;
2106
2107 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2108 if( status != PSA_SUCCESS )
2109 goto exit;
2110
Gilles Peskined911eb72018-08-14 15:18:45 +02002111 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2112 if( status != PSA_SUCCESS )
2113 goto exit;
2114
2115 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002116
2117exit:
2118 mbedtls_zeroize( tmp, hash_size );
2119 return( status );
2120}
2121#endif /* MBEDTLS_MD_C */
2122
mohammad16036df908f2018-04-02 08:34:15 -07002123static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002124 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002125 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002126{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002127 if( ! operation->key_set )
2128 return( PSA_ERROR_BAD_STATE );
2129 if( operation->iv_required && ! operation->iv_set )
2130 return( PSA_ERROR_BAD_STATE );
2131
Gilles Peskine8c9def32018-02-08 10:02:12 +01002132 if( mac_size < operation->mac_size )
2133 return( PSA_ERROR_BUFFER_TOO_SMALL );
2134
Gilles Peskine8c9def32018-02-08 10:02:12 +01002135#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002136 if( operation->alg == PSA_ALG_CMAC )
2137 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002138 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2139 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2140 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002141 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02002142 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002143 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002144 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002145 else
2146#endif /* MBEDTLS_CMAC_C */
2147#if defined(MBEDTLS_MD_C)
2148 if( PSA_ALG_IS_HMAC( operation->alg ) )
2149 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002150 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002151 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002152 }
2153 else
2154#endif /* MBEDTLS_MD_C */
2155 {
2156 /* This shouldn't happen if `operation` was initialized by
2157 * a setup function. */
2158 return( PSA_ERROR_BAD_STATE );
2159 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002160}
2161
Gilles Peskineacd4be32018-07-08 19:56:25 +02002162psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2163 uint8_t *mac,
2164 size_t mac_size,
2165 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002166{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002167 psa_status_t status;
2168
2169 /* Fill the output buffer with something that isn't a valid mac
2170 * (barring an attack on the mac and deliberately-crafted input),
2171 * in case the caller doesn't check the return status properly. */
2172 *mac_length = mac_size;
2173 /* If mac_size is 0 then mac may be NULL and then the
2174 * call to memset would have undefined behavior. */
2175 if( mac_size != 0 )
2176 memset( mac, '!', mac_size );
2177
Gilles Peskine89167cb2018-07-08 20:12:23 +02002178 if( ! operation->is_sign )
2179 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002180 status = PSA_ERROR_BAD_STATE;
2181 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002182 }
mohammad16036df908f2018-04-02 08:34:15 -07002183
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002184 status = psa_mac_finish_internal( operation, mac, mac_size );
2185
2186cleanup:
2187 if( status == PSA_SUCCESS )
2188 {
2189 status = psa_mac_abort( operation );
2190 if( status == PSA_SUCCESS )
2191 *mac_length = operation->mac_size;
2192 else
2193 memset( mac, '!', mac_size );
2194 }
2195 else
2196 psa_mac_abort( operation );
2197 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002198}
2199
Gilles Peskineacd4be32018-07-08 19:56:25 +02002200psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2201 const uint8_t *mac,
2202 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002203{
Gilles Peskine828ed142018-06-18 23:25:51 +02002204 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002205 psa_status_t status;
2206
Gilles Peskine89167cb2018-07-08 20:12:23 +02002207 if( operation->is_sign )
2208 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002209 status = PSA_ERROR_BAD_STATE;
2210 goto cleanup;
2211 }
2212 if( operation->mac_size != mac_length )
2213 {
2214 status = PSA_ERROR_INVALID_SIGNATURE;
2215 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002216 }
mohammad16036df908f2018-04-02 08:34:15 -07002217
2218 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002219 actual_mac, sizeof( actual_mac ) );
2220
2221 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2222 status = PSA_ERROR_INVALID_SIGNATURE;
2223
2224cleanup:
2225 if( status == PSA_SUCCESS )
2226 status = psa_mac_abort( operation );
2227 else
2228 psa_mac_abort( operation );
2229
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002230 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002231
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002232 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002233}
2234
2235
Gilles Peskine20035e32018-02-03 22:44:14 +01002236
Gilles Peskine20035e32018-02-03 22:44:14 +01002237/****************************************************************/
2238/* Asymmetric cryptography */
2239/****************************************************************/
2240
Gilles Peskine2b450e32018-06-27 15:42:46 +02002241#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002242/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002243 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002244static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2245 size_t hash_length,
2246 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002247{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002248 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002249 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002250 *md_alg = mbedtls_md_get_type( md_info );
2251
2252 /* The Mbed TLS RSA module uses an unsigned int for hash length
2253 * parameters. Validate that it fits so that we don't risk an
2254 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002255#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002256 if( hash_length > UINT_MAX )
2257 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002258#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002259
2260#if defined(MBEDTLS_PKCS1_V15)
2261 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2262 * must be correct. */
2263 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2264 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002265 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002266 if( md_info == NULL )
2267 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002268 if( mbedtls_md_get_size( md_info ) != hash_length )
2269 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002270 }
2271#endif /* MBEDTLS_PKCS1_V15 */
2272
2273#if defined(MBEDTLS_PKCS1_V21)
2274 /* PSS requires a hash internally. */
2275 if( PSA_ALG_IS_RSA_PSS( alg ) )
2276 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002277 if( md_info == NULL )
2278 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002279 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002280#endif /* MBEDTLS_PKCS1_V21 */
2281
Gilles Peskine61b91d42018-06-08 16:09:36 +02002282 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002283}
2284
Gilles Peskine2b450e32018-06-27 15:42:46 +02002285static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2286 psa_algorithm_t alg,
2287 const uint8_t *hash,
2288 size_t hash_length,
2289 uint8_t *signature,
2290 size_t signature_size,
2291 size_t *signature_length )
2292{
2293 psa_status_t status;
2294 int ret;
2295 mbedtls_md_type_t md_alg;
2296
2297 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2298 if( status != PSA_SUCCESS )
2299 return( status );
2300
Gilles Peskine630a18a2018-06-29 17:49:35 +02002301 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002302 return( PSA_ERROR_BUFFER_TOO_SMALL );
2303
2304#if defined(MBEDTLS_PKCS1_V15)
2305 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2306 {
2307 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2308 MBEDTLS_MD_NONE );
2309 ret = mbedtls_rsa_pkcs1_sign( rsa,
2310 mbedtls_ctr_drbg_random,
2311 &global_data.ctr_drbg,
2312 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002313 md_alg,
2314 (unsigned int) hash_length,
2315 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002316 signature );
2317 }
2318 else
2319#endif /* MBEDTLS_PKCS1_V15 */
2320#if defined(MBEDTLS_PKCS1_V21)
2321 if( PSA_ALG_IS_RSA_PSS( alg ) )
2322 {
2323 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2324 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2325 mbedtls_ctr_drbg_random,
2326 &global_data.ctr_drbg,
2327 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002328 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002329 (unsigned int) hash_length,
2330 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002331 signature );
2332 }
2333 else
2334#endif /* MBEDTLS_PKCS1_V21 */
2335 {
2336 return( PSA_ERROR_INVALID_ARGUMENT );
2337 }
2338
2339 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002340 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002341 return( mbedtls_to_psa_error( ret ) );
2342}
2343
2344static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2345 psa_algorithm_t alg,
2346 const uint8_t *hash,
2347 size_t hash_length,
2348 const uint8_t *signature,
2349 size_t signature_length )
2350{
2351 psa_status_t status;
2352 int ret;
2353 mbedtls_md_type_t md_alg;
2354
2355 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2356 if( status != PSA_SUCCESS )
2357 return( status );
2358
Gilles Peskine630a18a2018-06-29 17:49:35 +02002359 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002360 return( PSA_ERROR_BUFFER_TOO_SMALL );
2361
2362#if defined(MBEDTLS_PKCS1_V15)
2363 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2364 {
2365 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2366 MBEDTLS_MD_NONE );
2367 ret = mbedtls_rsa_pkcs1_verify( rsa,
2368 mbedtls_ctr_drbg_random,
2369 &global_data.ctr_drbg,
2370 MBEDTLS_RSA_PUBLIC,
2371 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002372 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002373 hash,
2374 signature );
2375 }
2376 else
2377#endif /* MBEDTLS_PKCS1_V15 */
2378#if defined(MBEDTLS_PKCS1_V21)
2379 if( PSA_ALG_IS_RSA_PSS( alg ) )
2380 {
2381 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2382 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2383 mbedtls_ctr_drbg_random,
2384 &global_data.ctr_drbg,
2385 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002386 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002387 (unsigned int) hash_length,
2388 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002389 signature );
2390 }
2391 else
2392#endif /* MBEDTLS_PKCS1_V21 */
2393 {
2394 return( PSA_ERROR_INVALID_ARGUMENT );
2395 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002396
2397 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2398 * the rest of the signature is invalid". This has little use in
2399 * practice and PSA doesn't report this distinction. */
2400 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2401 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002402 return( mbedtls_to_psa_error( ret ) );
2403}
2404#endif /* MBEDTLS_RSA_C */
2405
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002406#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002407/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2408 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2409 * (even though these functions don't modify it). */
2410static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2411 psa_algorithm_t alg,
2412 const uint8_t *hash,
2413 size_t hash_length,
2414 uint8_t *signature,
2415 size_t signature_size,
2416 size_t *signature_length )
2417{
2418 int ret;
2419 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002420 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002421 mbedtls_mpi_init( &r );
2422 mbedtls_mpi_init( &s );
2423
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002424 if( signature_size < 2 * curve_bytes )
2425 {
2426 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2427 goto cleanup;
2428 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002429
Gilles Peskinea05219c2018-11-16 16:02:56 +01002430#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002431 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2432 {
2433 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2434 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2435 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2436 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2437 hash, hash_length,
2438 md_alg ) );
2439 }
2440 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002441#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002442 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002443 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002444 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2445 hash, hash_length,
2446 mbedtls_ctr_drbg_random,
2447 &global_data.ctr_drbg ) );
2448 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002449
2450 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2451 signature,
2452 curve_bytes ) );
2453 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2454 signature + curve_bytes,
2455 curve_bytes ) );
2456
2457cleanup:
2458 mbedtls_mpi_free( &r );
2459 mbedtls_mpi_free( &s );
2460 if( ret == 0 )
2461 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002462 return( mbedtls_to_psa_error( ret ) );
2463}
2464
2465static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2466 const uint8_t *hash,
2467 size_t hash_length,
2468 const uint8_t *signature,
2469 size_t signature_length )
2470{
2471 int ret;
2472 mbedtls_mpi r, s;
2473 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2474 mbedtls_mpi_init( &r );
2475 mbedtls_mpi_init( &s );
2476
2477 if( signature_length != 2 * curve_bytes )
2478 return( PSA_ERROR_INVALID_SIGNATURE );
2479
2480 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2481 signature,
2482 curve_bytes ) );
2483 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2484 signature + curve_bytes,
2485 curve_bytes ) );
2486
2487 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2488 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002489
2490cleanup:
2491 mbedtls_mpi_free( &r );
2492 mbedtls_mpi_free( &s );
2493 return( mbedtls_to_psa_error( ret ) );
2494}
2495#endif /* MBEDTLS_ECDSA_C */
2496
Gilles Peskine61b91d42018-06-08 16:09:36 +02002497psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2498 psa_algorithm_t alg,
2499 const uint8_t *hash,
2500 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002501 uint8_t *signature,
2502 size_t signature_size,
2503 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002504{
2505 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002506 psa_status_t status;
2507
2508 *signature_length = signature_size;
2509
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002510 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2511 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002512 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002513 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002514 {
2515 status = PSA_ERROR_INVALID_ARGUMENT;
2516 goto exit;
2517 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002518
Gilles Peskine20035e32018-02-03 22:44:14 +01002519#if defined(MBEDTLS_RSA_C)
2520 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2521 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002522 status = psa_rsa_sign( slot->data.rsa,
2523 alg,
2524 hash, hash_length,
2525 signature, signature_size,
2526 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002527 }
2528 else
2529#endif /* defined(MBEDTLS_RSA_C) */
2530#if defined(MBEDTLS_ECP_C)
2531 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2532 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002533#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002534 if(
2535#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2536 PSA_ALG_IS_ECDSA( alg )
2537#else
2538 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2539#endif
2540 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002541 status = psa_ecdsa_sign( slot->data.ecp,
2542 alg,
2543 hash, hash_length,
2544 signature, signature_size,
2545 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002546 else
2547#endif /* defined(MBEDTLS_ECDSA_C) */
2548 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002549 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002550 }
itayzafrir5c753392018-05-08 11:18:38 +03002551 }
2552 else
2553#endif /* defined(MBEDTLS_ECP_C) */
2554 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002555 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002556 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002557
2558exit:
2559 /* Fill the unused part of the output buffer (the whole buffer on error,
2560 * the trailing part on success) with something that isn't a valid mac
2561 * (barring an attack on the mac and deliberately-crafted input),
2562 * in case the caller doesn't check the return status properly. */
2563 if( status == PSA_SUCCESS )
2564 memset( signature + *signature_length, '!',
2565 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002566 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002567 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002568 /* If signature_size is 0 then we have nothing to do. We must not call
2569 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002570 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002571}
2572
2573psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2574 psa_algorithm_t alg,
2575 const uint8_t *hash,
2576 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002577 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002578 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002579{
2580 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002581 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002582
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002583 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2584 if( status != PSA_SUCCESS )
2585 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002586
Gilles Peskine61b91d42018-06-08 16:09:36 +02002587#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002588 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002589 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002590 return( psa_rsa_verify( slot->data.rsa,
2591 alg,
2592 hash, hash_length,
2593 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002594 }
2595 else
2596#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002597#if defined(MBEDTLS_ECP_C)
2598 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2599 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002600#if defined(MBEDTLS_ECDSA_C)
2601 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002602 return( psa_ecdsa_verify( slot->data.ecp,
2603 hash, hash_length,
2604 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002605 else
2606#endif /* defined(MBEDTLS_ECDSA_C) */
2607 {
2608 return( PSA_ERROR_INVALID_ARGUMENT );
2609 }
itayzafrir5c753392018-05-08 11:18:38 +03002610 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002611 else
2612#endif /* defined(MBEDTLS_ECP_C) */
2613 {
2614 return( PSA_ERROR_NOT_SUPPORTED );
2615 }
2616}
2617
Gilles Peskine072ac562018-06-30 00:21:29 +02002618#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2619static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2620 mbedtls_rsa_context *rsa )
2621{
2622 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2623 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2624 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2625 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2626}
2627#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2628
Gilles Peskine61b91d42018-06-08 16:09:36 +02002629psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2630 psa_algorithm_t alg,
2631 const uint8_t *input,
2632 size_t input_length,
2633 const uint8_t *salt,
2634 size_t salt_length,
2635 uint8_t *output,
2636 size_t output_size,
2637 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002638{
2639 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002640 psa_status_t status;
2641
Darryl Green5cc689a2018-07-24 15:34:10 +01002642 (void) input;
2643 (void) input_length;
2644 (void) salt;
2645 (void) output;
2646 (void) output_size;
2647
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002648 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002649
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002650 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2651 return( PSA_ERROR_INVALID_ARGUMENT );
2652
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002653 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2654 if( status != PSA_SUCCESS )
2655 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002656 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2657 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002658 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002659
2660#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002661 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002662 {
2663 mbedtls_rsa_context *rsa = slot->data.rsa;
2664 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002665 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002666 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002667#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002668 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002669 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002670 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2671 mbedtls_ctr_drbg_random,
2672 &global_data.ctr_drbg,
2673 MBEDTLS_RSA_PUBLIC,
2674 input_length,
2675 input,
2676 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002677 }
2678 else
2679#endif /* MBEDTLS_PKCS1_V15 */
2680#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002681 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002682 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002683 psa_rsa_oaep_set_padding_mode( alg, rsa );
2684 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2685 mbedtls_ctr_drbg_random,
2686 &global_data.ctr_drbg,
2687 MBEDTLS_RSA_PUBLIC,
2688 salt, salt_length,
2689 input_length,
2690 input,
2691 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002692 }
2693 else
2694#endif /* MBEDTLS_PKCS1_V21 */
2695 {
2696 return( PSA_ERROR_INVALID_ARGUMENT );
2697 }
2698 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002699 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002700 return( mbedtls_to_psa_error( ret ) );
2701 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002702 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002703#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002704 {
2705 return( PSA_ERROR_NOT_SUPPORTED );
2706 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002707}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002708
Gilles Peskine61b91d42018-06-08 16:09:36 +02002709psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2710 psa_algorithm_t alg,
2711 const uint8_t *input,
2712 size_t input_length,
2713 const uint8_t *salt,
2714 size_t salt_length,
2715 uint8_t *output,
2716 size_t output_size,
2717 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002718{
2719 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002720 psa_status_t status;
2721
Darryl Green5cc689a2018-07-24 15:34:10 +01002722 (void) input;
2723 (void) input_length;
2724 (void) salt;
2725 (void) output;
2726 (void) output_size;
2727
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002728 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002729
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002730 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2731 return( PSA_ERROR_INVALID_ARGUMENT );
2732
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002733 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2734 if( status != PSA_SUCCESS )
2735 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002736 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2737 return( PSA_ERROR_INVALID_ARGUMENT );
2738
2739#if defined(MBEDTLS_RSA_C)
2740 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2741 {
2742 mbedtls_rsa_context *rsa = slot->data.rsa;
2743 int ret;
2744
Gilles Peskine630a18a2018-06-29 17:49:35 +02002745 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002746 return( PSA_ERROR_INVALID_ARGUMENT );
2747
2748#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002749 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002750 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002751 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2752 mbedtls_ctr_drbg_random,
2753 &global_data.ctr_drbg,
2754 MBEDTLS_RSA_PRIVATE,
2755 output_length,
2756 input,
2757 output,
2758 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002759 }
2760 else
2761#endif /* MBEDTLS_PKCS1_V15 */
2762#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002763 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002764 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002765 psa_rsa_oaep_set_padding_mode( alg, rsa );
2766 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2767 mbedtls_ctr_drbg_random,
2768 &global_data.ctr_drbg,
2769 MBEDTLS_RSA_PRIVATE,
2770 salt, salt_length,
2771 output_length,
2772 input,
2773 output,
2774 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002775 }
2776 else
2777#endif /* MBEDTLS_PKCS1_V21 */
2778 {
2779 return( PSA_ERROR_INVALID_ARGUMENT );
2780 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002781
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002782 return( mbedtls_to_psa_error( ret ) );
2783 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002784 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002785#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002786 {
2787 return( PSA_ERROR_NOT_SUPPORTED );
2788 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002789}
Gilles Peskine20035e32018-02-03 22:44:14 +01002790
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002791
2792
mohammad1603503973b2018-03-12 15:59:30 +02002793/****************************************************************/
2794/* Symmetric cryptography */
2795/****************************************************************/
2796
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002797/* Initialize the cipher operation structure. Once this function has been
2798 * called, psa_cipher_abort can run and will do the right thing. */
2799static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2800 psa_algorithm_t alg )
2801{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002802 if( ! PSA_ALG_IS_CIPHER( alg ) )
2803 {
2804 memset( operation, 0, sizeof( *operation ) );
2805 return( PSA_ERROR_INVALID_ARGUMENT );
2806 }
2807
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002808 operation->alg = alg;
2809 operation->key_set = 0;
2810 operation->iv_set = 0;
2811 operation->iv_required = 1;
2812 operation->iv_size = 0;
2813 operation->block_size = 0;
2814 mbedtls_cipher_init( &operation->ctx.cipher );
2815 return( PSA_SUCCESS );
2816}
2817
Gilles Peskinee553c652018-06-04 16:22:46 +02002818static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2819 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002820 psa_algorithm_t alg,
2821 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002822{
2823 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2824 psa_status_t status;
2825 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002826 size_t key_bits;
2827 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002828 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2829 PSA_KEY_USAGE_ENCRYPT :
2830 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002831
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002832 status = psa_cipher_init( operation, alg );
2833 if( status != PSA_SUCCESS )
2834 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002835
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002836 status = psa_get_key_from_slot( key, &slot, usage, alg);
2837 if( status != PSA_SUCCESS )
2838 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002839 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002840
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002841 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002842 if( cipher_info == NULL )
2843 return( PSA_ERROR_NOT_SUPPORTED );
2844
mohammad1603503973b2018-03-12 15:59:30 +02002845 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002846 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002847 {
2848 psa_cipher_abort( operation );
2849 return( mbedtls_to_psa_error( ret ) );
2850 }
2851
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002852#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002853 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002854 {
2855 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2856 unsigned char keys[24];
2857 memcpy( keys, slot->data.raw.data, 16 );
2858 memcpy( keys + 16, slot->data.raw.data, 8 );
2859 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2860 keys,
2861 192, cipher_operation );
2862 }
2863 else
2864#endif
2865 {
2866 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2867 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002868 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002869 }
Moran Peker41deec42018-04-04 15:43:05 +03002870 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002871 {
2872 psa_cipher_abort( operation );
2873 return( mbedtls_to_psa_error( ret ) );
2874 }
2875
mohammad16038481e742018-03-18 13:57:31 +02002876#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002877 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002878 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002879 case PSA_ALG_CBC_NO_PADDING:
2880 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2881 MBEDTLS_PADDING_NONE );
2882 break;
2883 case PSA_ALG_CBC_PKCS7:
2884 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2885 MBEDTLS_PADDING_PKCS7 );
2886 break;
2887 default:
2888 /* The algorithm doesn't involve padding. */
2889 ret = 0;
2890 break;
2891 }
2892 if( ret != 0 )
2893 {
2894 psa_cipher_abort( operation );
2895 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002896 }
2897#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2898
mohammad1603503973b2018-03-12 15:59:30 +02002899 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002900 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2901 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2902 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002903 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002904 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002905 }
mohammad1603503973b2018-03-12 15:59:30 +02002906
Moran Peker395db872018-05-31 14:07:14 +03002907 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002908}
2909
Gilles Peskinefe119512018-07-08 21:39:34 +02002910psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2911 psa_key_slot_t key,
2912 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002913{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002914 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002915}
2916
Gilles Peskinefe119512018-07-08 21:39:34 +02002917psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2918 psa_key_slot_t key,
2919 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002920{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002921 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002922}
2923
Gilles Peskinefe119512018-07-08 21:39:34 +02002924psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2925 unsigned char *iv,
2926 size_t iv_size,
2927 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002928{
itayzafrir534bd7c2018-08-02 13:56:32 +03002929 psa_status_t status;
2930 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002931 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002932 {
2933 status = PSA_ERROR_BAD_STATE;
2934 goto exit;
2935 }
Moran Peker41deec42018-04-04 15:43:05 +03002936 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002937 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002938 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002939 goto exit;
2940 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002941 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2942 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002943 if( ret != 0 )
2944 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002945 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002946 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002947 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002948
mohammad16038481e742018-03-18 13:57:31 +02002949 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002950 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002951
Moran Peker395db872018-05-31 14:07:14 +03002952exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002953 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002954 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002955 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002956}
2957
Gilles Peskinefe119512018-07-08 21:39:34 +02002958psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2959 const unsigned char *iv,
2960 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002961{
itayzafrir534bd7c2018-08-02 13:56:32 +03002962 psa_status_t status;
2963 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002964 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002965 {
2966 status = PSA_ERROR_BAD_STATE;
2967 goto exit;
2968 }
Moran Pekera28258c2018-05-29 16:25:04 +03002969 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002970 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002971 status = PSA_ERROR_INVALID_ARGUMENT;
2972 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002973 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002974 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2975 status = mbedtls_to_psa_error( ret );
2976exit:
2977 if( status == PSA_SUCCESS )
2978 operation->iv_set = 1;
2979 else
mohammad1603503973b2018-03-12 15:59:30 +02002980 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002981 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002982}
2983
Gilles Peskinee553c652018-06-04 16:22:46 +02002984psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2985 const uint8_t *input,
2986 size_t input_length,
2987 unsigned char *output,
2988 size_t output_size,
2989 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002990{
itayzafrir534bd7c2018-08-02 13:56:32 +03002991 psa_status_t status;
2992 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002993 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002994 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002995 {
2996 /* Take the unprocessed partial block left over from previous
2997 * update calls, if any, plus the input to this call. Remove
2998 * the last partial block, if any. You get the data that will be
2999 * output in this call. */
3000 expected_output_size =
3001 ( operation->ctx.cipher.unprocessed_len + input_length )
3002 / operation->block_size * operation->block_size;
3003 }
3004 else
3005 {
3006 expected_output_size = input_length;
3007 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003008
Gilles Peskine89d789c2018-06-04 17:17:16 +02003009 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003010 {
3011 status = PSA_ERROR_BUFFER_TOO_SMALL;
3012 goto exit;
3013 }
mohammad160382759612018-03-12 18:16:40 +02003014
mohammad1603503973b2018-03-12 15:59:30 +02003015 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003016 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003017 status = mbedtls_to_psa_error( ret );
3018exit:
3019 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003020 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003021 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003022}
3023
Gilles Peskinee553c652018-06-04 16:22:46 +02003024psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3025 uint8_t *output,
3026 size_t output_size,
3027 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003028{
Janos Follath315b51c2018-07-09 16:04:51 +01003029 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
3030 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003031 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003032
mohammad1603503973b2018-03-12 15:59:30 +02003033 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003034 {
Janos Follath315b51c2018-07-09 16:04:51 +01003035 status = PSA_ERROR_BAD_STATE;
3036 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003037 }
3038 if( operation->iv_required && ! operation->iv_set )
3039 {
Janos Follath315b51c2018-07-09 16:04:51 +01003040 status = PSA_ERROR_BAD_STATE;
3041 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003042 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003043
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003044 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003045 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3046 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003047 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003048 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003049 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003050 }
3051
Janos Follath315b51c2018-07-09 16:04:51 +01003052 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3053 temp_output_buffer,
3054 output_length );
3055 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003056 {
Janos Follath315b51c2018-07-09 16:04:51 +01003057 status = mbedtls_to_psa_error( cipher_ret );
3058 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003059 }
Janos Follath315b51c2018-07-09 16:04:51 +01003060
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003061 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003062 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003063 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003064 memcpy( output, temp_output_buffer, *output_length );
3065 else
3066 {
Janos Follath315b51c2018-07-09 16:04:51 +01003067 status = PSA_ERROR_BUFFER_TOO_SMALL;
3068 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003069 }
mohammad1603503973b2018-03-12 15:59:30 +02003070
Janos Follath279ab8e2018-07-09 16:13:21 +01003071 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003072 status = psa_cipher_abort( operation );
3073
3074 return( status );
3075
3076error:
3077
3078 *output_length = 0;
3079
Janos Follath279ab8e2018-07-09 16:13:21 +01003080 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003081 (void) psa_cipher_abort( operation );
3082
3083 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003084}
3085
Gilles Peskinee553c652018-06-04 16:22:46 +02003086psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3087{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003088 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003089 {
3090 /* The object has (apparently) been initialized but it is not
3091 * in use. It's ok to call abort on such an object, and there's
3092 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003093 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003094 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003095
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003096 /* Sanity check (shouldn't happen: operation->alg should
3097 * always have been initialized to a valid value). */
3098 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3099 return( PSA_ERROR_BAD_STATE );
3100
mohammad1603503973b2018-03-12 15:59:30 +02003101 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003102
Moran Peker41deec42018-04-04 15:43:05 +03003103 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003104 operation->key_set = 0;
3105 operation->iv_set = 0;
3106 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003107 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003108 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003109
Moran Peker395db872018-05-31 14:07:14 +03003110 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003111}
3112
Gilles Peskinea0655c32018-04-30 17:06:50 +02003113
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003114
mohammad16038cc1cee2018-03-28 01:21:33 +03003115/****************************************************************/
3116/* Key Policy */
3117/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003118
mohammad160327010052018-07-03 13:16:15 +03003119#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003120void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003121{
Gilles Peskine803ce742018-06-18 16:07:14 +02003122 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003123}
3124
Gilles Peskine2d277862018-06-18 15:41:12 +02003125void psa_key_policy_set_usage( psa_key_policy_t *policy,
3126 psa_key_usage_t usage,
3127 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003128{
mohammad16034eed7572018-03-28 05:14:59 -07003129 policy->usage = usage;
3130 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003131}
3132
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003133psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003134{
mohammad16036df908f2018-04-02 08:34:15 -07003135 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003136}
3137
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003138psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003139{
mohammad16036df908f2018-04-02 08:34:15 -07003140 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003141}
mohammad160327010052018-07-03 13:16:15 +03003142#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003143
Gilles Peskine2d277862018-06-18 15:41:12 +02003144psa_status_t psa_set_key_policy( psa_key_slot_t key,
3145 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003146{
3147 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003148 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003149
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003150 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003151 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003152
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003153 status = psa_get_empty_key_slot( key, &slot );
3154 if( status != PSA_SUCCESS )
3155 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003156
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003157 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3158 PSA_KEY_USAGE_ENCRYPT |
3159 PSA_KEY_USAGE_DECRYPT |
3160 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003161 PSA_KEY_USAGE_VERIFY |
3162 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003163 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003164
mohammad16036df908f2018-04-02 08:34:15 -07003165 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003166
3167 return( PSA_SUCCESS );
3168}
3169
Gilles Peskine2d277862018-06-18 15:41:12 +02003170psa_status_t psa_get_key_policy( psa_key_slot_t key,
3171 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003172{
3173 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003174 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003175
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003176 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003177 return( PSA_ERROR_INVALID_ARGUMENT );
3178
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003179 status = psa_get_key_slot( key, &slot );
3180 if( status != PSA_SUCCESS )
3181 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003182
mohammad16036df908f2018-04-02 08:34:15 -07003183 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003184
3185 return( PSA_SUCCESS );
3186}
Gilles Peskine20035e32018-02-03 22:44:14 +01003187
Gilles Peskinea0655c32018-04-30 17:06:50 +02003188
3189
mohammad1603804cd712018-03-20 22:44:08 +02003190/****************************************************************/
3191/* Key Lifetime */
3192/****************************************************************/
3193
Gilles Peskine2d277862018-06-18 15:41:12 +02003194psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
3195 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003196{
3197 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003198 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003199
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003200 status = psa_get_key_slot( key, &slot );
3201 if( status != PSA_SUCCESS )
3202 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003203
mohammad1603804cd712018-03-20 22:44:08 +02003204 *lifetime = slot->lifetime;
3205
3206 return( PSA_SUCCESS );
3207}
3208
Gilles Peskine20035e32018-02-03 22:44:14 +01003209
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003210
mohammad16035955c982018-04-26 00:53:03 +03003211/****************************************************************/
3212/* AEAD */
3213/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003214
Gilles Peskineedf9a652018-08-17 18:11:56 +02003215typedef struct
3216{
3217 key_slot_t *slot;
3218 const mbedtls_cipher_info_t *cipher_info;
3219 union
3220 {
3221#if defined(MBEDTLS_CCM_C)
3222 mbedtls_ccm_context ccm;
3223#endif /* MBEDTLS_CCM_C */
3224#if defined(MBEDTLS_GCM_C)
3225 mbedtls_gcm_context gcm;
3226#endif /* MBEDTLS_GCM_C */
3227 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003228 psa_algorithm_t core_alg;
3229 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003230 uint8_t tag_length;
3231} aead_operation_t;
3232
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003233static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003234{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003235 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003236 {
3237#if defined(MBEDTLS_CCM_C)
3238 case PSA_ALG_CCM:
3239 mbedtls_ccm_free( &operation->ctx.ccm );
3240 break;
3241#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003242#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003243 case PSA_ALG_GCM:
3244 mbedtls_gcm_free( &operation->ctx.gcm );
3245 break;
3246#endif /* MBEDTLS_GCM_C */
3247 }
3248}
3249
3250static psa_status_t psa_aead_setup( aead_operation_t *operation,
3251 psa_key_slot_t key,
3252 psa_key_usage_t usage,
3253 psa_algorithm_t alg )
3254{
3255 psa_status_t status;
3256 size_t key_bits;
3257 mbedtls_cipher_id_t cipher_id;
3258
3259 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3260 if( status != PSA_SUCCESS )
3261 return( status );
3262
3263 key_bits = psa_get_key_bits( operation->slot );
3264
3265 operation->cipher_info =
3266 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3267 &cipher_id );
3268 if( operation->cipher_info == NULL )
3269 return( PSA_ERROR_NOT_SUPPORTED );
3270
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003271 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003272 {
3273#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003274 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3275 operation->core_alg = PSA_ALG_CCM;
3276 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003277 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3278 return( PSA_ERROR_INVALID_ARGUMENT );
3279 mbedtls_ccm_init( &operation->ctx.ccm );
3280 status = mbedtls_to_psa_error(
3281 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3282 operation->slot->data.raw.data,
3283 (unsigned int) key_bits ) );
3284 if( status != 0 )
3285 goto cleanup;
3286 break;
3287#endif /* MBEDTLS_CCM_C */
3288
3289#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003290 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3291 operation->core_alg = PSA_ALG_GCM;
3292 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003293 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3294 return( PSA_ERROR_INVALID_ARGUMENT );
3295 mbedtls_gcm_init( &operation->ctx.gcm );
3296 status = mbedtls_to_psa_error(
3297 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3298 operation->slot->data.raw.data,
3299 (unsigned int) key_bits ) );
3300 break;
3301#endif /* MBEDTLS_GCM_C */
3302
3303 default:
3304 return( PSA_ERROR_NOT_SUPPORTED );
3305 }
3306
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003307 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3308 {
3309 status = PSA_ERROR_INVALID_ARGUMENT;
3310 goto cleanup;
3311 }
3312 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3313 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3314 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3315 * In both cases, mbedtls_xxx will validate the tag length below. */
3316
Gilles Peskineedf9a652018-08-17 18:11:56 +02003317 return( PSA_SUCCESS );
3318
3319cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003320 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003321 return( status );
3322}
3323
mohammad16035955c982018-04-26 00:53:03 +03003324psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3325 psa_algorithm_t alg,
3326 const uint8_t *nonce,
3327 size_t nonce_length,
3328 const uint8_t *additional_data,
3329 size_t additional_data_length,
3330 const uint8_t *plaintext,
3331 size_t plaintext_length,
3332 uint8_t *ciphertext,
3333 size_t ciphertext_size,
3334 size_t *ciphertext_length )
3335{
mohammad16035955c982018-04-26 00:53:03 +03003336 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003337 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003338 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003339
mohammad1603f08a5502018-06-03 15:05:47 +03003340 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003341
Gilles Peskineedf9a652018-08-17 18:11:56 +02003342 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003343 if( status != PSA_SUCCESS )
3344 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003345
Gilles Peskineedf9a652018-08-17 18:11:56 +02003346 /* For all currently supported modes, the tag is at the end of the
3347 * ciphertext. */
3348 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3349 {
3350 status = PSA_ERROR_BUFFER_TOO_SMALL;
3351 goto exit;
3352 }
3353 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003354
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003355#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003356 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003357 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003358 status = mbedtls_to_psa_error(
3359 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3360 MBEDTLS_GCM_ENCRYPT,
3361 plaintext_length,
3362 nonce, nonce_length,
3363 additional_data, additional_data_length,
3364 plaintext, ciphertext,
3365 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003366 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003367 else
3368#endif /* MBEDTLS_GCM_C */
3369#if defined(MBEDTLS_CCM_C)
3370 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003371 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003372 status = mbedtls_to_psa_error(
3373 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3374 plaintext_length,
3375 nonce, nonce_length,
3376 additional_data,
3377 additional_data_length,
3378 plaintext, ciphertext,
3379 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003380 }
mohammad16035c8845f2018-05-09 05:40:09 -07003381 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003382#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003383 {
mohammad1603554faad2018-06-03 15:07:38 +03003384 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003385 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003386
Gilles Peskineedf9a652018-08-17 18:11:56 +02003387 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3388 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003389
Gilles Peskineedf9a652018-08-17 18:11:56 +02003390exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003391 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003392 if( status == PSA_SUCCESS )
3393 *ciphertext_length = plaintext_length + operation.tag_length;
3394 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003395}
3396
Gilles Peskineee652a32018-06-01 19:23:52 +02003397/* Locate the tag in a ciphertext buffer containing the encrypted data
3398 * followed by the tag. Return the length of the part preceding the tag in
3399 * *plaintext_length. This is the size of the plaintext in modes where
3400 * the encrypted data has the same size as the plaintext, such as
3401 * CCM and GCM. */
3402static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3403 const uint8_t *ciphertext,
3404 size_t ciphertext_length,
3405 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003406 const uint8_t **p_tag )
3407{
3408 size_t payload_length;
3409 if( tag_length > ciphertext_length )
3410 return( PSA_ERROR_INVALID_ARGUMENT );
3411 payload_length = ciphertext_length - tag_length;
3412 if( payload_length > plaintext_size )
3413 return( PSA_ERROR_BUFFER_TOO_SMALL );
3414 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003415 return( PSA_SUCCESS );
3416}
3417
mohammad16035955c982018-04-26 00:53:03 +03003418psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3419 psa_algorithm_t alg,
3420 const uint8_t *nonce,
3421 size_t nonce_length,
3422 const uint8_t *additional_data,
3423 size_t additional_data_length,
3424 const uint8_t *ciphertext,
3425 size_t ciphertext_length,
3426 uint8_t *plaintext,
3427 size_t plaintext_size,
3428 size_t *plaintext_length )
3429{
mohammad16035955c982018-04-26 00:53:03 +03003430 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003431 aead_operation_t operation;
3432 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003433
Gilles Peskineee652a32018-06-01 19:23:52 +02003434 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003435
Gilles Peskineedf9a652018-08-17 18:11:56 +02003436 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003437 if( status != PSA_SUCCESS )
3438 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003439
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003440#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003441 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003442 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003443 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003444 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003445 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003446 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003447 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003448
Gilles Peskineedf9a652018-08-17 18:11:56 +02003449 status = mbedtls_to_psa_error(
3450 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3451 ciphertext_length - operation.tag_length,
3452 nonce, nonce_length,
3453 additional_data,
3454 additional_data_length,
3455 tag, operation.tag_length,
3456 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003457 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003458 else
3459#endif /* MBEDTLS_GCM_C */
3460#if defined(MBEDTLS_CCM_C)
3461 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003462 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003463 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003464 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003465 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003466 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003467 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003468
Gilles Peskineedf9a652018-08-17 18:11:56 +02003469 status = mbedtls_to_psa_error(
3470 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3471 ciphertext_length - operation.tag_length,
3472 nonce, nonce_length,
3473 additional_data,
3474 additional_data_length,
3475 ciphertext, plaintext,
3476 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003477 }
mohammad160339574652018-06-01 04:39:53 -07003478 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003479#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003480 {
mohammad1603554faad2018-06-03 15:07:38 +03003481 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003482 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003483
Gilles Peskineedf9a652018-08-17 18:11:56 +02003484 if( status != PSA_SUCCESS && plaintext_size != 0 )
3485 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003486
Gilles Peskineedf9a652018-08-17 18:11:56 +02003487exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003488 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003489 if( status == PSA_SUCCESS )
3490 *plaintext_length = ciphertext_length - operation.tag_length;
3491 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003492}
3493
Gilles Peskinea0655c32018-04-30 17:06:50 +02003494
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003495
Gilles Peskine20035e32018-02-03 22:44:14 +01003496/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003497/* Generators */
3498/****************************************************************/
3499
3500psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3501{
3502 psa_status_t status = PSA_SUCCESS;
3503 if( generator->alg == 0 )
3504 {
3505 /* The object has (apparently) been initialized but it is not
3506 * in use. It's ok to call abort on such an object, and there's
3507 * nothing to do. */
3508 }
3509 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003510 if( generator->alg == PSA_ALG_SELECT_RAW )
3511 {
3512 if( generator->ctx.buffer.data != NULL )
3513 {
3514 mbedtls_zeroize( generator->ctx.buffer.data,
3515 generator->ctx.buffer.size );
3516 mbedtls_free( generator->ctx.buffer.data );
3517 }
3518 }
3519 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003520#if defined(MBEDTLS_MD_C)
3521 if( PSA_ALG_IS_HKDF( generator->alg ) )
3522 {
3523 mbedtls_free( generator->ctx.hkdf.info );
3524 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3525 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003526 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3527 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3528 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003529 {
3530 if( generator->ctx.tls12_prf.key != NULL )
3531 {
3532 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3533 generator->ctx.tls12_prf.key_len );
3534 mbedtls_free( generator->ctx.tls12_prf.key );
3535 }
Hanno Becker580fba12018-11-13 20:50:45 +00003536
3537 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3538 {
3539 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3540 generator->ctx.tls12_prf.Ai_with_seed_len );
3541 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3542 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003543 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003544 else
3545#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003546 {
3547 status = PSA_ERROR_BAD_STATE;
3548 }
3549 memset( generator, 0, sizeof( *generator ) );
3550 return( status );
3551}
3552
3553
3554psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3555 size_t *capacity)
3556{
3557 *capacity = generator->capacity;
3558 return( PSA_SUCCESS );
3559}
3560
Gilles Peskinebef7f142018-07-12 17:22:21 +02003561#if defined(MBEDTLS_MD_C)
3562/* Read some bytes from an HKDF-based generator. This performs a chunk
3563 * of the expand phase of the HKDF algorithm. */
3564static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3565 psa_algorithm_t hash_alg,
3566 uint8_t *output,
3567 size_t output_length )
3568{
3569 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3570 psa_status_t status;
3571
3572 while( output_length != 0 )
3573 {
3574 /* Copy what remains of the current block */
3575 uint8_t n = hash_length - hkdf->offset_in_block;
3576 if( n > output_length )
3577 n = (uint8_t) output_length;
3578 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3579 output += n;
3580 output_length -= n;
3581 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003582 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003583 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003584 /* We can't be wanting more output after block 0xff, otherwise
3585 * the capacity check in psa_generator_read() would have
3586 * prevented this call. It could happen only if the generator
3587 * object was corrupted or if this function is called directly
3588 * inside the library. */
3589 if( hkdf->block_number == 0xff )
3590 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003591
3592 /* We need a new block */
3593 ++hkdf->block_number;
3594 hkdf->offset_in_block = 0;
3595 status = psa_hmac_setup_internal( &hkdf->hmac,
3596 hkdf->prk, hash_length,
3597 hash_alg );
3598 if( status != PSA_SUCCESS )
3599 return( status );
3600 if( hkdf->block_number != 1 )
3601 {
3602 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3603 hkdf->output_block,
3604 hash_length );
3605 if( status != PSA_SUCCESS )
3606 return( status );
3607 }
3608 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3609 hkdf->info,
3610 hkdf->info_length );
3611 if( status != PSA_SUCCESS )
3612 return( status );
3613 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3614 &hkdf->block_number, 1 );
3615 if( status != PSA_SUCCESS )
3616 return( status );
3617 status = psa_hmac_finish_internal( &hkdf->hmac,
3618 hkdf->output_block,
3619 sizeof( hkdf->output_block ) );
3620 if( status != PSA_SUCCESS )
3621 return( status );
3622 }
3623
3624 return( PSA_SUCCESS );
3625}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003626
3627static psa_status_t psa_generator_tls12_prf_generate_next_block(
3628 psa_tls12_prf_generator_t *tls12_prf,
3629 psa_algorithm_t alg )
3630{
3631 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3632 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3633 psa_hmac_internal_data hmac;
3634 psa_status_t status, cleanup_status;
3635
Hanno Becker3b339e22018-11-13 20:56:14 +00003636 unsigned char *Ai;
3637 size_t Ai_len;
3638
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003639 /* We can't be wanting more output after block 0xff, otherwise
3640 * the capacity check in psa_generator_read() would have
3641 * prevented this call. It could happen only if the generator
3642 * object was corrupted or if this function is called directly
3643 * inside the library. */
3644 if( tls12_prf->block_number == 0xff )
3645 return( PSA_ERROR_BAD_STATE );
3646
3647 /* We need a new block */
3648 ++tls12_prf->block_number;
3649 tls12_prf->offset_in_block = 0;
3650
3651 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3652 *
3653 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3654 *
3655 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3656 * HMAC_hash(secret, A(2) + seed) +
3657 * HMAC_hash(secret, A(3) + seed) + ...
3658 *
3659 * A(0) = seed
3660 * A(i) = HMAC_hash( secret, A(i-1) )
3661 *
3662 * The `psa_tls12_prf_generator` structures saves the block
3663 * `HMAC_hash(secret, A(i) + seed)` from which the output
3664 * is currently extracted as `output_block`, while
3665 * `A(i) + seed` is stored in `Ai_with_seed`.
3666 *
3667 * Generating a new block means recalculating `Ai_with_seed`
3668 * from the A(i)-part of it, and afterwards recalculating
3669 * `output_block`.
3670 *
3671 * A(0) is computed at setup time.
3672 *
3673 */
3674
3675 psa_hmac_init_internal( &hmac );
3676
3677 /* We must distinguish the calculation of A(1) from those
3678 * of A(2) and higher, because A(0)=seed has a different
3679 * length than the other A(i). */
3680 if( tls12_prf->block_number == 1 )
3681 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003682 Ai = tls12_prf->Ai_with_seed + hash_length;
3683 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003684 }
3685 else
3686 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003687 Ai = tls12_prf->Ai_with_seed;
3688 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003689 }
3690
Hanno Becker3b339e22018-11-13 20:56:14 +00003691 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3692 status = psa_hmac_setup_internal( &hmac,
3693 tls12_prf->key,
3694 tls12_prf->key_len,
3695 hash_alg );
3696 if( status != PSA_SUCCESS )
3697 goto cleanup;
3698
3699 status = psa_hash_update( &hmac.hash_ctx,
3700 Ai, Ai_len );
3701 if( status != PSA_SUCCESS )
3702 goto cleanup;
3703
3704 status = psa_hmac_finish_internal( &hmac,
3705 tls12_prf->Ai_with_seed,
3706 hash_length );
3707 if( status != PSA_SUCCESS )
3708 goto cleanup;
3709
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003710 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3711 status = psa_hmac_setup_internal( &hmac,
3712 tls12_prf->key,
3713 tls12_prf->key_len,
3714 hash_alg );
3715 if( status != PSA_SUCCESS )
3716 goto cleanup;
3717
3718 status = psa_hash_update( &hmac.hash_ctx,
3719 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003720 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003721 if( status != PSA_SUCCESS )
3722 goto cleanup;
3723
3724 status = psa_hmac_finish_internal( &hmac,
3725 tls12_prf->output_block,
3726 hash_length );
3727 if( status != PSA_SUCCESS )
3728 goto cleanup;
3729
3730cleanup:
3731
3732 cleanup_status = psa_hmac_abort_internal( &hmac );
3733 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3734 status = cleanup_status;
3735
3736 return( status );
3737}
3738
3739/* Read some bytes from an TLS-1.2-PRF-based generator.
3740 * See Section 5 of RFC 5246. */
3741static psa_status_t psa_generator_tls12_prf_read(
3742 psa_tls12_prf_generator_t *tls12_prf,
3743 psa_algorithm_t alg,
3744 uint8_t *output,
3745 size_t output_length )
3746{
3747 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3748 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3749 psa_status_t status;
3750
3751 while( output_length != 0 )
3752 {
3753 /* Copy what remains of the current block */
3754 uint8_t n = hash_length - tls12_prf->offset_in_block;
3755
3756 /* Check if we have fully processed the current block. */
3757 if( n == 0 )
3758 {
3759 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3760 alg );
3761 if( status != PSA_SUCCESS )
3762 return( status );
3763
3764 continue;
3765 }
3766
3767 if( n > output_length )
3768 n = (uint8_t) output_length;
3769 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3770 n );
3771 output += n;
3772 output_length -= n;
3773 tls12_prf->offset_in_block += n;
3774 }
3775
3776 return( PSA_SUCCESS );
3777}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003778#endif /* MBEDTLS_MD_C */
3779
Gilles Peskineeab56e42018-07-12 17:12:33 +02003780psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3781 uint8_t *output,
3782 size_t output_length )
3783{
3784 psa_status_t status;
3785
3786 if( output_length > generator->capacity )
3787 {
3788 generator->capacity = 0;
3789 /* Go through the error path to wipe all confidential data now
3790 * that the generator object is useless. */
3791 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3792 goto exit;
3793 }
3794 if( output_length == 0 &&
3795 generator->capacity == 0 && generator->alg == 0 )
3796 {
3797 /* Edge case: this is a blank or finished generator, and 0
3798 * bytes were requested. The right error in this case could
3799 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3800 * INSUFFICIENT_CAPACITY, which is right for a finished
3801 * generator, for consistency with the case when
3802 * output_length > 0. */
3803 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3804 }
3805 generator->capacity -= output_length;
3806
Gilles Peskine751d9652018-09-18 12:05:44 +02003807 if( generator->alg == PSA_ALG_SELECT_RAW )
3808 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003809 /* Initially, the capacity of a selection generator is always
3810 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3811 * abbreviated in this comment as `size`. When the remaining
3812 * capacity is `c`, the next bytes to serve start `c` bytes
3813 * from the end of the buffer, i.e. `size - c` from the
3814 * beginning of the buffer. Since `generator->capacity` was just
3815 * decremented above, we need to serve the bytes from
3816 * `size - generator->capacity - output_length` to
3817 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003818 size_t offset =
3819 generator->ctx.buffer.size - generator->capacity - output_length;
3820 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3821 status = PSA_SUCCESS;
3822 }
3823 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003824#if defined(MBEDTLS_MD_C)
3825 if( PSA_ALG_IS_HKDF( generator->alg ) )
3826 {
3827 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3828 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3829 output, output_length );
3830 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003831 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3832 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003833 {
3834 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3835 generator->alg, output,
3836 output_length );
3837 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003838 else
3839#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003840 {
3841 return( PSA_ERROR_BAD_STATE );
3842 }
3843
3844exit:
3845 if( status != PSA_SUCCESS )
3846 {
3847 psa_generator_abort( generator );
3848 memset( output, '!', output_length );
3849 }
3850 return( status );
3851}
3852
Gilles Peskine08542d82018-07-19 17:05:42 +02003853#if defined(MBEDTLS_DES_C)
3854static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3855{
3856 if( data_size >= 8 )
3857 mbedtls_des_key_set_parity( data );
3858 if( data_size >= 16 )
3859 mbedtls_des_key_set_parity( data + 8 );
3860 if( data_size >= 24 )
3861 mbedtls_des_key_set_parity( data + 16 );
3862}
3863#endif /* MBEDTLS_DES_C */
3864
Gilles Peskineeab56e42018-07-12 17:12:33 +02003865psa_status_t psa_generator_import_key( psa_key_slot_t key,
3866 psa_key_type_t type,
3867 size_t bits,
3868 psa_crypto_generator_t *generator )
3869{
3870 uint8_t *data = NULL;
3871 size_t bytes = PSA_BITS_TO_BYTES( bits );
3872 psa_status_t status;
3873
3874 if( ! key_type_is_raw_bytes( type ) )
3875 return( PSA_ERROR_INVALID_ARGUMENT );
3876 if( bits % 8 != 0 )
3877 return( PSA_ERROR_INVALID_ARGUMENT );
3878 data = mbedtls_calloc( 1, bytes );
3879 if( data == NULL )
3880 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3881
3882 status = psa_generator_read( generator, data, bytes );
3883 if( status != PSA_SUCCESS )
3884 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003885#if defined(MBEDTLS_DES_C)
3886 if( type == PSA_KEY_TYPE_DES )
3887 psa_des_set_key_parity( data, bytes );
3888#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003889 status = psa_import_key( key, type, data, bytes );
3890
3891exit:
3892 mbedtls_free( data );
3893 return( status );
3894}
3895
3896
3897
3898/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003899/* Key derivation */
3900/****************************************************************/
3901
Gilles Peskinea05219c2018-11-16 16:02:56 +01003902#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003903/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003904 * of the HKDF algorithm.
3905 *
3906 * Note that if this function fails, you must call psa_generator_abort()
3907 * to potentially free embedded data structures and wipe confidential data.
3908 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003909static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003910 const uint8_t *secret,
3911 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003912 psa_algorithm_t hash_alg,
3913 const uint8_t *salt,
3914 size_t salt_length,
3915 const uint8_t *label,
3916 size_t label_length )
3917{
3918 psa_status_t status;
3919 status = psa_hmac_setup_internal( &hkdf->hmac,
3920 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003921 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003922 if( status != PSA_SUCCESS )
3923 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003924 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003925 if( status != PSA_SUCCESS )
3926 return( status );
3927 status = psa_hmac_finish_internal( &hkdf->hmac,
3928 hkdf->prk,
3929 sizeof( hkdf->prk ) );
3930 if( status != PSA_SUCCESS )
3931 return( status );
3932 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3933 hkdf->block_number = 0;
3934 hkdf->info_length = label_length;
3935 if( label_length != 0 )
3936 {
3937 hkdf->info = mbedtls_calloc( 1, label_length );
3938 if( hkdf->info == NULL )
3939 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3940 memcpy( hkdf->info, label, label_length );
3941 }
3942 return( PSA_SUCCESS );
3943}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003944#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003945
Gilles Peskinea05219c2018-11-16 16:02:56 +01003946#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003947/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3948 *
3949 * Note that if this function fails, you must call psa_generator_abort()
3950 * to potentially free embedded data structures and wipe confidential data.
3951 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003952static psa_status_t psa_generator_tls12_prf_setup(
3953 psa_tls12_prf_generator_t *tls12_prf,
3954 const unsigned char *key,
3955 size_t key_len,
3956 psa_algorithm_t hash_alg,
3957 const uint8_t *salt,
3958 size_t salt_length,
3959 const uint8_t *label,
3960 size_t label_length )
3961{
3962 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003963 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3964 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003965
3966 tls12_prf->key = mbedtls_calloc( 1, key_len );
3967 if( tls12_prf->key == NULL )
3968 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3969 tls12_prf->key_len = key_len;
3970 memcpy( tls12_prf->key, key, key_len );
3971
Hanno Becker580fba12018-11-13 20:50:45 +00003972 overflow = ( salt_length + label_length < salt_length ) ||
3973 ( salt_length + label_length + hash_length < hash_length );
3974 if( overflow )
3975 return( PSA_ERROR_INVALID_ARGUMENT );
3976
3977 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3978 if( tls12_prf->Ai_with_seed == NULL )
3979 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3980 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3981
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003982 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3983 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003984 if( label_length != 0 )
3985 {
3986 memcpy( tls12_prf->Ai_with_seed + hash_length,
3987 label, label_length );
3988 }
3989
3990 if( salt_length != 0 )
3991 {
3992 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3993 salt, salt_length );
3994 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003995
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003996 /* The first block gets generated when
3997 * psa_generator_read() is called. */
3998 tls12_prf->block_number = 0;
3999 tls12_prf->offset_in_block = hash_length;
4000
4001 return( PSA_SUCCESS );
4002}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004003
4004/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4005static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4006 psa_tls12_prf_generator_t *tls12_prf,
4007 const unsigned char *psk,
4008 size_t psk_len,
4009 psa_algorithm_t hash_alg,
4010 const uint8_t *salt,
4011 size_t salt_length,
4012 const uint8_t *label,
4013 size_t label_length )
4014{
4015 psa_status_t status;
4016 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4017
4018 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4019 return( PSA_ERROR_INVALID_ARGUMENT );
4020
4021 /* Quoting RFC 4279, Section 2:
4022 *
4023 * The premaster secret is formed as follows: if the PSK is N octets
4024 * long, concatenate a uint16 with the value N, N zero octets, a second
4025 * uint16 with the value N, and the PSK itself.
4026 */
4027
4028 pms[0] = ( psk_len >> 8 ) & 0xff;
4029 pms[1] = ( psk_len >> 0 ) & 0xff;
4030 memset( pms + 2, 0, psk_len );
4031 pms[2 + psk_len + 0] = pms[0];
4032 pms[2 + psk_len + 1] = pms[1];
4033 memcpy( pms + 4 + psk_len, psk, psk_len );
4034
4035 status = psa_generator_tls12_prf_setup( tls12_prf,
4036 pms, 4 + 2 * psk_len,
4037 hash_alg,
4038 salt, salt_length,
4039 label, label_length );
4040
4041 mbedtls_zeroize( pms, sizeof( pms ) );
4042 return( status );
4043}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004044#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004045
Gilles Peskine346797d2018-11-16 16:05:06 +01004046/* Note that if this function fails, you must call psa_generator_abort()
4047 * to potentially free embedded data structures and wipe confidential data.
4048 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004049static psa_status_t psa_key_derivation_internal(
4050 psa_crypto_generator_t *generator,
4051 const uint8_t *secret, size_t secret_length,
4052 psa_algorithm_t alg,
4053 const uint8_t *salt, size_t salt_length,
4054 const uint8_t *label, size_t label_length,
4055 size_t capacity )
4056{
4057 psa_status_t status;
4058 size_t max_capacity;
4059
4060 /* Set generator->alg even on failure so that abort knows what to do. */
4061 generator->alg = alg;
4062
Gilles Peskine751d9652018-09-18 12:05:44 +02004063 if( alg == PSA_ALG_SELECT_RAW )
4064 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004065 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004066 if( salt_length != 0 )
4067 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004068 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004069 if( label_length != 0 )
4070 return( PSA_ERROR_INVALID_ARGUMENT );
4071 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4072 if( generator->ctx.buffer.data == NULL )
4073 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4074 memcpy( generator->ctx.buffer.data, secret, secret_length );
4075 generator->ctx.buffer.size = secret_length;
4076 max_capacity = secret_length;
4077 status = PSA_SUCCESS;
4078 }
4079 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004080#if defined(MBEDTLS_MD_C)
4081 if( PSA_ALG_IS_HKDF( alg ) )
4082 {
4083 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4084 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4085 if( hash_size == 0 )
4086 return( PSA_ERROR_NOT_SUPPORTED );
4087 max_capacity = 255 * hash_size;
4088 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4089 secret, secret_length,
4090 hash_alg,
4091 salt, salt_length,
4092 label, label_length );
4093 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004094 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4095 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4096 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004097 {
4098 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4099 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4100
4101 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4102 if( hash_alg != PSA_ALG_SHA_256 &&
4103 hash_alg != PSA_ALG_SHA_384 )
4104 {
4105 return( PSA_ERROR_NOT_SUPPORTED );
4106 }
4107
4108 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004109
4110 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4111 {
4112 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4113 secret, secret_length,
4114 hash_alg, salt, salt_length,
4115 label, label_length );
4116 }
4117 else
4118 {
4119 status = psa_generator_tls12_psk_to_ms_setup(
4120 &generator->ctx.tls12_prf,
4121 secret, secret_length,
4122 hash_alg, salt, salt_length,
4123 label, label_length );
4124 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004125 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004126 else
4127#endif
4128 {
4129 return( PSA_ERROR_NOT_SUPPORTED );
4130 }
4131
4132 if( status != PSA_SUCCESS )
4133 return( status );
4134
4135 if( capacity <= max_capacity )
4136 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004137 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4138 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004139 else
4140 return( PSA_ERROR_INVALID_ARGUMENT );
4141
4142 return( PSA_SUCCESS );
4143}
4144
Gilles Peskineea0fb492018-07-12 17:17:20 +02004145psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01004146 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004147 psa_algorithm_t alg,
4148 const uint8_t *salt,
4149 size_t salt_length,
4150 const uint8_t *label,
4151 size_t label_length,
4152 size_t capacity )
4153{
4154 key_slot_t *slot;
4155 psa_status_t status;
4156
4157 if( generator->alg != 0 )
4158 return( PSA_ERROR_BAD_STATE );
4159
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004160 /* Make sure that alg is a key derivation algorithm. This prevents
4161 * key selection algorithms, which psa_key_derivation_internal
4162 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004163 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4164 return( PSA_ERROR_INVALID_ARGUMENT );
4165
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004166 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4167 if( status != PSA_SUCCESS )
4168 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004169
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004170 if( slot->type != PSA_KEY_TYPE_DERIVE )
4171 return( PSA_ERROR_INVALID_ARGUMENT );
4172
4173 status = psa_key_derivation_internal( generator,
4174 slot->data.raw.data,
4175 slot->data.raw.bytes,
4176 alg,
4177 salt, salt_length,
4178 label, label_length,
4179 capacity );
4180 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004181 psa_generator_abort( generator );
4182 return( status );
4183}
4184
4185
4186
4187/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004188/* Key agreement */
4189/****************************************************************/
4190
Gilles Peskinea05219c2018-11-16 16:02:56 +01004191#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004192static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4193 size_t peer_key_length,
4194 const mbedtls_ecp_keypair *our_key,
4195 uint8_t *shared_secret,
4196 size_t shared_secret_size,
4197 size_t *shared_secret_length )
4198{
4199 mbedtls_pk_context pk;
4200 mbedtls_ecp_keypair *their_key = NULL;
4201 mbedtls_ecdh_context ecdh;
4202 int ret;
4203 mbedtls_ecdh_init( &ecdh );
4204 mbedtls_pk_init( &pk );
4205
4206 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4207 if( ret != 0 )
4208 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004209 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004210 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004211 case MBEDTLS_PK_ECKEY:
4212 case MBEDTLS_PK_ECKEY_DH:
4213 break;
4214 default:
4215 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4216 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004217 }
4218 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004219 if( their_key->grp.id != our_key->grp.id )
4220 {
4221 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4222 goto exit;
4223 }
4224
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004225 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4226 if( ret != 0 )
4227 goto exit;
4228 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4229 if( ret != 0 )
4230 goto exit;
4231
4232 ret = mbedtls_ecdh_calc_secret( &ecdh,
4233 shared_secret_length,
4234 shared_secret, shared_secret_size,
4235 mbedtls_ctr_drbg_random,
4236 &global_data.ctr_drbg );
4237
4238exit:
4239 mbedtls_pk_free( &pk );
4240 mbedtls_ecdh_free( &ecdh );
4241 return( mbedtls_to_psa_error( ret ) );
4242}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004243#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004244
Gilles Peskine01d718c2018-09-18 12:01:02 +02004245#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4246
Gilles Peskine346797d2018-11-16 16:05:06 +01004247/* Note that if this function fails, you must call psa_generator_abort()
4248 * to potentially free embedded data structures and wipe confidential data.
4249 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004250static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4251 key_slot_t *private_key,
4252 const uint8_t *peer_key,
4253 size_t peer_key_length,
4254 psa_algorithm_t alg )
4255{
4256 psa_status_t status;
4257 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4258 size_t shared_secret_length = 0;
4259
4260 /* Step 1: run the secret agreement algorithm to generate the shared
4261 * secret. */
4262 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4263 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004264#if defined(MBEDTLS_ECDH_C)
4265 case PSA_ALG_ECDH_BASE:
4266 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4267 return( PSA_ERROR_INVALID_ARGUMENT );
4268 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4269 private_key->data.ecp,
4270 shared_secret,
4271 sizeof( shared_secret ),
4272 &shared_secret_length );
4273 break;
4274#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004275 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004276 (void) private_key;
4277 (void) peer_key;
4278 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004279 return( PSA_ERROR_NOT_SUPPORTED );
4280 }
4281 if( status != PSA_SUCCESS )
4282 goto exit;
4283
4284 /* Step 2: set up the key derivation to generate key material from
4285 * the shared secret. */
4286 status = psa_key_derivation_internal( generator,
4287 shared_secret, shared_secret_length,
4288 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4289 NULL, 0, NULL, 0,
4290 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4291exit:
4292 mbedtls_zeroize( shared_secret, shared_secret_length );
4293 return( status );
4294}
4295
4296psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4297 psa_key_slot_t private_key,
4298 const uint8_t *peer_key,
4299 size_t peer_key_length,
4300 psa_algorithm_t alg )
4301{
4302 key_slot_t *slot;
4303 psa_status_t status;
4304 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4305 return( PSA_ERROR_INVALID_ARGUMENT );
4306 status = psa_get_key_from_slot( private_key, &slot,
4307 PSA_KEY_USAGE_DERIVE, alg );
4308 if( status != PSA_SUCCESS )
4309 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004310 status = psa_key_agreement_internal( generator,
4311 slot,
4312 peer_key, peer_key_length,
4313 alg );
4314 if( status != PSA_SUCCESS )
4315 psa_generator_abort( generator );
4316 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004317}
4318
4319
4320
4321/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004322/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004323/****************************************************************/
4324
4325psa_status_t psa_generate_random( uint8_t *output,
4326 size_t output_size )
4327{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004328 int ret;
4329 GUARD_MODULE_INITIALIZED;
4330
4331 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004332 return( mbedtls_to_psa_error( ret ) );
4333}
4334
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004335#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004336
4337/* Support function for error conversion between psa_its error codes to psa crypto */
4338static psa_status_t its_to_psa_error( psa_its_status_t ret )
4339{
4340 switch( ret )
4341 {
4342 case PSA_ITS_SUCCESS:
4343 return( PSA_SUCCESS );
4344
4345 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4346 return( PSA_ERROR_EMPTY_SLOT );
4347
4348 case PSA_ITS_ERROR_STORAGE_FAILURE:
4349 return( PSA_ERROR_STORAGE_FAILURE );
4350
4351 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4352 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4353
4354 case PSA_ITS_ERROR_INVALID_KEY:
4355 case PSA_PS_ERROR_OFFSET_INVALID:
4356 case PSA_ITS_ERROR_INCORRECT_SIZE:
4357 case PSA_ITS_ERROR_BAD_POINTER:
4358 return( PSA_ERROR_INVALID_ARGUMENT );
4359
4360 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4361 return( PSA_ERROR_NOT_SUPPORTED );
4362
4363 case PSA_ITS_ERROR_WRITE_ONCE:
4364 return( PSA_ERROR_OCCUPIED_SLOT );
4365
4366 default:
4367 return( PSA_ERROR_UNKNOWN_ERROR );
4368 }
4369}
4370
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004371psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4372 size_t seed_size )
4373{
4374 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004375 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004376 struct psa_its_info_t p_info;
4377 if( global_data.initialized )
4378 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004379
4380 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4381 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4382 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4383 return( PSA_ERROR_INVALID_ARGUMENT );
4384
avolinski0d2c2662018-11-21 17:31:07 +02004385 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004386 status = its_to_psa_error( its_status );
4387
4388 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004389 {
avolinski0d2c2662018-11-21 17:31:07 +02004390 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004391 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004392 }
avolinski13beb102018-11-20 16:51:49 +02004393 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004394 {
4395 /* You should not be here. Seed needs to be injected only once */
4396 status = PSA_ERROR_NOT_PERMITTED;
4397 }
4398 return( status );
4399}
4400#endif
4401
Gilles Peskine05d69892018-06-19 22:00:52 +02004402psa_status_t psa_generate_key( psa_key_slot_t key,
4403 psa_key_type_t type,
4404 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004405 const void *extra,
4406 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004407{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004408 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004409 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004410
Gilles Peskine53d991e2018-07-12 01:14:59 +02004411 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004412 return( PSA_ERROR_INVALID_ARGUMENT );
4413
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004414 status = psa_get_empty_key_slot( key, &slot );
4415 if( status != PSA_SUCCESS )
4416 return( status );
4417
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004418 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004419 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004420 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004421 if( status != PSA_SUCCESS )
4422 return( status );
4423 status = psa_generate_random( slot->data.raw.data,
4424 slot->data.raw.bytes );
4425 if( status != PSA_SUCCESS )
4426 {
4427 mbedtls_free( slot->data.raw.data );
4428 return( status );
4429 }
4430#if defined(MBEDTLS_DES_C)
4431 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004432 psa_des_set_key_parity( slot->data.raw.data,
4433 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004434#endif /* MBEDTLS_DES_C */
4435 }
4436 else
4437
4438#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4439 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4440 {
4441 mbedtls_rsa_context *rsa;
4442 int ret;
4443 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004444 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4445 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004446 /* Accept only byte-aligned keys, for the same reasons as
4447 * in psa_import_rsa_key(). */
4448 if( bits % 8 != 0 )
4449 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004450 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004451 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004452 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004453 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004454 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004455#if INT_MAX < 0xffffffff
4456 /* Check that the uint32_t value passed by the caller fits
4457 * in the range supported by this implementation. */
4458 if( p->e > INT_MAX )
4459 return( PSA_ERROR_NOT_SUPPORTED );
4460#endif
4461 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004462 }
4463 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4464 if( rsa == NULL )
4465 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4466 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4467 ret = mbedtls_rsa_gen_key( rsa,
4468 mbedtls_ctr_drbg_random,
4469 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004470 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004471 exponent );
4472 if( ret != 0 )
4473 {
4474 mbedtls_rsa_free( rsa );
4475 mbedtls_free( rsa );
4476 return( mbedtls_to_psa_error( ret ) );
4477 }
4478 slot->data.rsa = rsa;
4479 }
4480 else
4481#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4482
4483#if defined(MBEDTLS_ECP_C)
4484 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4485 {
4486 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4487 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4488 const mbedtls_ecp_curve_info *curve_info =
4489 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4490 mbedtls_ecp_keypair *ecp;
4491 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004492 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004493 return( PSA_ERROR_NOT_SUPPORTED );
4494 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4495 return( PSA_ERROR_NOT_SUPPORTED );
4496 if( curve_info->bit_size != bits )
4497 return( PSA_ERROR_INVALID_ARGUMENT );
4498 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4499 if( ecp == NULL )
4500 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4501 mbedtls_ecp_keypair_init( ecp );
4502 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4503 mbedtls_ctr_drbg_random,
4504 &global_data.ctr_drbg );
4505 if( ret != 0 )
4506 {
4507 mbedtls_ecp_keypair_free( ecp );
4508 mbedtls_free( ecp );
4509 return( mbedtls_to_psa_error( ret ) );
4510 }
4511 slot->data.ecp = ecp;
4512 }
4513 else
4514#endif /* MBEDTLS_ECP_C */
4515
4516 return( PSA_ERROR_NOT_SUPPORTED );
4517
4518 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004519
4520#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4521 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4522 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004523 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004524 }
4525#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4526
4527 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004528}
4529
4530
4531/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004532/* Module setup */
4533/****************************************************************/
4534
Gilles Peskine5e769522018-11-20 21:59:56 +01004535psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4536 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4537 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4538{
4539 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4540 return( PSA_ERROR_BAD_STATE );
4541 global_data.entropy_init = entropy_init;
4542 global_data.entropy_free = entropy_free;
4543 return( PSA_SUCCESS );
4544}
4545
Gilles Peskinee59236f2018-01-27 23:32:46 +01004546void mbedtls_psa_crypto_free( void )
4547{
Gilles Peskinec6b69072018-11-20 21:42:52 +01004548 if( global_data.key_slots_initialized )
Darryl Green40225ba2018-11-15 14:48:15 +00004549 {
Gilles Peskined7c75702018-12-03 10:36:46 +01004550 psa_key_slot_t key;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004551 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
4552 {
Gilles Peskined7c75702018-12-03 10:36:46 +01004553 key_slot_t *slot = &global_data.key_slots[key - 1];
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01004554 (void) psa_wipe_key_slot( slot );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004555 }
Darryl Green40225ba2018-11-15 14:48:15 +00004556 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01004557 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4558 {
4559 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004560 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004561 }
4562 /* Wipe all remaining data, including configuration.
4563 * In particular, this sets all state indicator to the value
4564 * indicating "uninitialized". */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004565 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4566}
4567
4568psa_status_t psa_crypto_init( void )
4569{
4570 int ret;
4571 const unsigned char drbg_seed[] = "PSA";
4572
Gilles Peskinec6b69072018-11-20 21:42:52 +01004573 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004574 if( global_data.initialized != 0 )
4575 return( PSA_SUCCESS );
4576
Gilles Peskine5e769522018-11-20 21:59:56 +01004577 /* Set default configuration if
4578 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4579 if( global_data.entropy_init == NULL )
4580 global_data.entropy_init = mbedtls_entropy_init;
4581 if( global_data.entropy_free == NULL )
4582 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004583
Gilles Peskinec6b69072018-11-20 21:42:52 +01004584 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004585 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004586 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004587 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004588 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4589 mbedtls_entropy_func,
4590 &global_data.entropy,
4591 drbg_seed, sizeof( drbg_seed ) - 1 );
4592 if( ret != 0 )
4593 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004594 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004595
Gilles Peskinec6b69072018-11-20 21:42:52 +01004596 /* Initialize the key slots. Zero-initialization has made all key
4597 * slots empty, so there is nothing to do. In a future version we will
4598 * load data from storage. */
4599 global_data.key_slots_initialized = 1;
4600
4601 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004602 global_data.initialized = 1;
4603
Gilles Peskinee59236f2018-01-27 23:32:46 +01004604exit:
4605 if( ret != 0 )
4606 mbedtls_psa_crypto_free( );
4607 return( mbedtls_to_psa_error( ret ) );
4608}
4609
4610#endif /* MBEDTLS_PSA_CRYPTO_C */