blob: bff0ce70978100ab2e3757e1f964574ae074bf4a [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 Peskine2d277862018-06-18 15:41:12 +02003209psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01003210 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003211{
3212 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003213 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003214
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003215 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
3216 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
Darryl Greend49a4992018-06-18 17:27:26 +01003217 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003218 return( PSA_ERROR_INVALID_ARGUMENT );
3219
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003220 status = psa_get_empty_key_slot( key, &slot );
3221 if( status != PSA_SUCCESS )
3222 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02003223
Darryl Greend49a4992018-06-18 17:27:26 +01003224 if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003225 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003226
Darryl Greend49a4992018-06-18 17:27:26 +01003227#if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
3228 if( lifetime == PSA_KEY_LIFETIME_PERSISTENT )
3229 return( PSA_ERROR_NOT_SUPPORTED );
3230#endif
3231
mohammad1603060ad8a2018-03-20 14:28:38 -07003232 slot->lifetime = lifetime;
Gilles Peskine69f976b2018-11-30 18:46:56 +01003233 slot->persistent_storage_id = key;
mohammad1603804cd712018-03-20 22:44:08 +02003234
3235 return( PSA_SUCCESS );
3236}
3237
Gilles Peskine20035e32018-02-03 22:44:14 +01003238
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003239
mohammad16035955c982018-04-26 00:53:03 +03003240/****************************************************************/
3241/* AEAD */
3242/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003243
Gilles Peskineedf9a652018-08-17 18:11:56 +02003244typedef struct
3245{
3246 key_slot_t *slot;
3247 const mbedtls_cipher_info_t *cipher_info;
3248 union
3249 {
3250#if defined(MBEDTLS_CCM_C)
3251 mbedtls_ccm_context ccm;
3252#endif /* MBEDTLS_CCM_C */
3253#if defined(MBEDTLS_GCM_C)
3254 mbedtls_gcm_context gcm;
3255#endif /* MBEDTLS_GCM_C */
3256 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003257 psa_algorithm_t core_alg;
3258 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003259 uint8_t tag_length;
3260} aead_operation_t;
3261
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003262static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003263{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003264 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003265 {
3266#if defined(MBEDTLS_CCM_C)
3267 case PSA_ALG_CCM:
3268 mbedtls_ccm_free( &operation->ctx.ccm );
3269 break;
3270#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003271#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003272 case PSA_ALG_GCM:
3273 mbedtls_gcm_free( &operation->ctx.gcm );
3274 break;
3275#endif /* MBEDTLS_GCM_C */
3276 }
3277}
3278
3279static psa_status_t psa_aead_setup( aead_operation_t *operation,
3280 psa_key_slot_t key,
3281 psa_key_usage_t usage,
3282 psa_algorithm_t alg )
3283{
3284 psa_status_t status;
3285 size_t key_bits;
3286 mbedtls_cipher_id_t cipher_id;
3287
3288 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3289 if( status != PSA_SUCCESS )
3290 return( status );
3291
3292 key_bits = psa_get_key_bits( operation->slot );
3293
3294 operation->cipher_info =
3295 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3296 &cipher_id );
3297 if( operation->cipher_info == NULL )
3298 return( PSA_ERROR_NOT_SUPPORTED );
3299
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003300 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003301 {
3302#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003303 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3304 operation->core_alg = PSA_ALG_CCM;
3305 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003306 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3307 return( PSA_ERROR_INVALID_ARGUMENT );
3308 mbedtls_ccm_init( &operation->ctx.ccm );
3309 status = mbedtls_to_psa_error(
3310 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3311 operation->slot->data.raw.data,
3312 (unsigned int) key_bits ) );
3313 if( status != 0 )
3314 goto cleanup;
3315 break;
3316#endif /* MBEDTLS_CCM_C */
3317
3318#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003319 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3320 operation->core_alg = PSA_ALG_GCM;
3321 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003322 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3323 return( PSA_ERROR_INVALID_ARGUMENT );
3324 mbedtls_gcm_init( &operation->ctx.gcm );
3325 status = mbedtls_to_psa_error(
3326 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3327 operation->slot->data.raw.data,
3328 (unsigned int) key_bits ) );
3329 break;
3330#endif /* MBEDTLS_GCM_C */
3331
3332 default:
3333 return( PSA_ERROR_NOT_SUPPORTED );
3334 }
3335
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003336 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3337 {
3338 status = PSA_ERROR_INVALID_ARGUMENT;
3339 goto cleanup;
3340 }
3341 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3342 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3343 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3344 * In both cases, mbedtls_xxx will validate the tag length below. */
3345
Gilles Peskineedf9a652018-08-17 18:11:56 +02003346 return( PSA_SUCCESS );
3347
3348cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003349 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003350 return( status );
3351}
3352
mohammad16035955c982018-04-26 00:53:03 +03003353psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3354 psa_algorithm_t alg,
3355 const uint8_t *nonce,
3356 size_t nonce_length,
3357 const uint8_t *additional_data,
3358 size_t additional_data_length,
3359 const uint8_t *plaintext,
3360 size_t plaintext_length,
3361 uint8_t *ciphertext,
3362 size_t ciphertext_size,
3363 size_t *ciphertext_length )
3364{
mohammad16035955c982018-04-26 00:53:03 +03003365 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003366 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003367 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003368
mohammad1603f08a5502018-06-03 15:05:47 +03003369 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003370
Gilles Peskineedf9a652018-08-17 18:11:56 +02003371 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003372 if( status != PSA_SUCCESS )
3373 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003374
Gilles Peskineedf9a652018-08-17 18:11:56 +02003375 /* For all currently supported modes, the tag is at the end of the
3376 * ciphertext. */
3377 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3378 {
3379 status = PSA_ERROR_BUFFER_TOO_SMALL;
3380 goto exit;
3381 }
3382 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003383
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003384#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003385 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003386 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003387 status = mbedtls_to_psa_error(
3388 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3389 MBEDTLS_GCM_ENCRYPT,
3390 plaintext_length,
3391 nonce, nonce_length,
3392 additional_data, additional_data_length,
3393 plaintext, ciphertext,
3394 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003395 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003396 else
3397#endif /* MBEDTLS_GCM_C */
3398#if defined(MBEDTLS_CCM_C)
3399 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003400 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003401 status = mbedtls_to_psa_error(
3402 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3403 plaintext_length,
3404 nonce, nonce_length,
3405 additional_data,
3406 additional_data_length,
3407 plaintext, ciphertext,
3408 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003409 }
mohammad16035c8845f2018-05-09 05:40:09 -07003410 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003411#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003412 {
mohammad1603554faad2018-06-03 15:07:38 +03003413 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003414 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003415
Gilles Peskineedf9a652018-08-17 18:11:56 +02003416 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3417 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003418
Gilles Peskineedf9a652018-08-17 18:11:56 +02003419exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003420 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003421 if( status == PSA_SUCCESS )
3422 *ciphertext_length = plaintext_length + operation.tag_length;
3423 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003424}
3425
Gilles Peskineee652a32018-06-01 19:23:52 +02003426/* Locate the tag in a ciphertext buffer containing the encrypted data
3427 * followed by the tag. Return the length of the part preceding the tag in
3428 * *plaintext_length. This is the size of the plaintext in modes where
3429 * the encrypted data has the same size as the plaintext, such as
3430 * CCM and GCM. */
3431static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3432 const uint8_t *ciphertext,
3433 size_t ciphertext_length,
3434 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003435 const uint8_t **p_tag )
3436{
3437 size_t payload_length;
3438 if( tag_length > ciphertext_length )
3439 return( PSA_ERROR_INVALID_ARGUMENT );
3440 payload_length = ciphertext_length - tag_length;
3441 if( payload_length > plaintext_size )
3442 return( PSA_ERROR_BUFFER_TOO_SMALL );
3443 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003444 return( PSA_SUCCESS );
3445}
3446
mohammad16035955c982018-04-26 00:53:03 +03003447psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3448 psa_algorithm_t alg,
3449 const uint8_t *nonce,
3450 size_t nonce_length,
3451 const uint8_t *additional_data,
3452 size_t additional_data_length,
3453 const uint8_t *ciphertext,
3454 size_t ciphertext_length,
3455 uint8_t *plaintext,
3456 size_t plaintext_size,
3457 size_t *plaintext_length )
3458{
mohammad16035955c982018-04-26 00:53:03 +03003459 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003460 aead_operation_t operation;
3461 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003462
Gilles Peskineee652a32018-06-01 19:23:52 +02003463 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003464
Gilles Peskineedf9a652018-08-17 18:11:56 +02003465 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003466 if( status != PSA_SUCCESS )
3467 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003468
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003469#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003470 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003471 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003472 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003473 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003474 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003475 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003476 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003477
Gilles Peskineedf9a652018-08-17 18:11:56 +02003478 status = mbedtls_to_psa_error(
3479 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3480 ciphertext_length - operation.tag_length,
3481 nonce, nonce_length,
3482 additional_data,
3483 additional_data_length,
3484 tag, operation.tag_length,
3485 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003486 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003487 else
3488#endif /* MBEDTLS_GCM_C */
3489#if defined(MBEDTLS_CCM_C)
3490 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003491 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003492 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003493 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003494 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003495 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003496 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003497
Gilles Peskineedf9a652018-08-17 18:11:56 +02003498 status = mbedtls_to_psa_error(
3499 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3500 ciphertext_length - operation.tag_length,
3501 nonce, nonce_length,
3502 additional_data,
3503 additional_data_length,
3504 ciphertext, plaintext,
3505 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003506 }
mohammad160339574652018-06-01 04:39:53 -07003507 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003508#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003509 {
mohammad1603554faad2018-06-03 15:07:38 +03003510 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003511 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003512
Gilles Peskineedf9a652018-08-17 18:11:56 +02003513 if( status != PSA_SUCCESS && plaintext_size != 0 )
3514 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003515
Gilles Peskineedf9a652018-08-17 18:11:56 +02003516exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003517 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003518 if( status == PSA_SUCCESS )
3519 *plaintext_length = ciphertext_length - operation.tag_length;
3520 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003521}
3522
Gilles Peskinea0655c32018-04-30 17:06:50 +02003523
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003524
Gilles Peskine20035e32018-02-03 22:44:14 +01003525/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003526/* Generators */
3527/****************************************************************/
3528
3529psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3530{
3531 psa_status_t status = PSA_SUCCESS;
3532 if( generator->alg == 0 )
3533 {
3534 /* The object has (apparently) been initialized but it is not
3535 * in use. It's ok to call abort on such an object, and there's
3536 * nothing to do. */
3537 }
3538 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003539 if( generator->alg == PSA_ALG_SELECT_RAW )
3540 {
3541 if( generator->ctx.buffer.data != NULL )
3542 {
3543 mbedtls_zeroize( generator->ctx.buffer.data,
3544 generator->ctx.buffer.size );
3545 mbedtls_free( generator->ctx.buffer.data );
3546 }
3547 }
3548 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003549#if defined(MBEDTLS_MD_C)
3550 if( PSA_ALG_IS_HKDF( generator->alg ) )
3551 {
3552 mbedtls_free( generator->ctx.hkdf.info );
3553 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3554 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003555 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3556 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3557 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003558 {
3559 if( generator->ctx.tls12_prf.key != NULL )
3560 {
3561 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3562 generator->ctx.tls12_prf.key_len );
3563 mbedtls_free( generator->ctx.tls12_prf.key );
3564 }
Hanno Becker580fba12018-11-13 20:50:45 +00003565
3566 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3567 {
3568 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3569 generator->ctx.tls12_prf.Ai_with_seed_len );
3570 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3571 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003572 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003573 else
3574#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003575 {
3576 status = PSA_ERROR_BAD_STATE;
3577 }
3578 memset( generator, 0, sizeof( *generator ) );
3579 return( status );
3580}
3581
3582
3583psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3584 size_t *capacity)
3585{
3586 *capacity = generator->capacity;
3587 return( PSA_SUCCESS );
3588}
3589
Gilles Peskinebef7f142018-07-12 17:22:21 +02003590#if defined(MBEDTLS_MD_C)
3591/* Read some bytes from an HKDF-based generator. This performs a chunk
3592 * of the expand phase of the HKDF algorithm. */
3593static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3594 psa_algorithm_t hash_alg,
3595 uint8_t *output,
3596 size_t output_length )
3597{
3598 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3599 psa_status_t status;
3600
3601 while( output_length != 0 )
3602 {
3603 /* Copy what remains of the current block */
3604 uint8_t n = hash_length - hkdf->offset_in_block;
3605 if( n > output_length )
3606 n = (uint8_t) output_length;
3607 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3608 output += n;
3609 output_length -= n;
3610 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003611 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003612 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003613 /* We can't be wanting more output after block 0xff, otherwise
3614 * the capacity check in psa_generator_read() would have
3615 * prevented this call. It could happen only if the generator
3616 * object was corrupted or if this function is called directly
3617 * inside the library. */
3618 if( hkdf->block_number == 0xff )
3619 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003620
3621 /* We need a new block */
3622 ++hkdf->block_number;
3623 hkdf->offset_in_block = 0;
3624 status = psa_hmac_setup_internal( &hkdf->hmac,
3625 hkdf->prk, hash_length,
3626 hash_alg );
3627 if( status != PSA_SUCCESS )
3628 return( status );
3629 if( hkdf->block_number != 1 )
3630 {
3631 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3632 hkdf->output_block,
3633 hash_length );
3634 if( status != PSA_SUCCESS )
3635 return( status );
3636 }
3637 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3638 hkdf->info,
3639 hkdf->info_length );
3640 if( status != PSA_SUCCESS )
3641 return( status );
3642 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3643 &hkdf->block_number, 1 );
3644 if( status != PSA_SUCCESS )
3645 return( status );
3646 status = psa_hmac_finish_internal( &hkdf->hmac,
3647 hkdf->output_block,
3648 sizeof( hkdf->output_block ) );
3649 if( status != PSA_SUCCESS )
3650 return( status );
3651 }
3652
3653 return( PSA_SUCCESS );
3654}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003655
3656static psa_status_t psa_generator_tls12_prf_generate_next_block(
3657 psa_tls12_prf_generator_t *tls12_prf,
3658 psa_algorithm_t alg )
3659{
3660 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3661 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3662 psa_hmac_internal_data hmac;
3663 psa_status_t status, cleanup_status;
3664
Hanno Becker3b339e22018-11-13 20:56:14 +00003665 unsigned char *Ai;
3666 size_t Ai_len;
3667
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003668 /* We can't be wanting more output after block 0xff, otherwise
3669 * the capacity check in psa_generator_read() would have
3670 * prevented this call. It could happen only if the generator
3671 * object was corrupted or if this function is called directly
3672 * inside the library. */
3673 if( tls12_prf->block_number == 0xff )
3674 return( PSA_ERROR_BAD_STATE );
3675
3676 /* We need a new block */
3677 ++tls12_prf->block_number;
3678 tls12_prf->offset_in_block = 0;
3679
3680 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3681 *
3682 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3683 *
3684 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3685 * HMAC_hash(secret, A(2) + seed) +
3686 * HMAC_hash(secret, A(3) + seed) + ...
3687 *
3688 * A(0) = seed
3689 * A(i) = HMAC_hash( secret, A(i-1) )
3690 *
3691 * The `psa_tls12_prf_generator` structures saves the block
3692 * `HMAC_hash(secret, A(i) + seed)` from which the output
3693 * is currently extracted as `output_block`, while
3694 * `A(i) + seed` is stored in `Ai_with_seed`.
3695 *
3696 * Generating a new block means recalculating `Ai_with_seed`
3697 * from the A(i)-part of it, and afterwards recalculating
3698 * `output_block`.
3699 *
3700 * A(0) is computed at setup time.
3701 *
3702 */
3703
3704 psa_hmac_init_internal( &hmac );
3705
3706 /* We must distinguish the calculation of A(1) from those
3707 * of A(2) and higher, because A(0)=seed has a different
3708 * length than the other A(i). */
3709 if( tls12_prf->block_number == 1 )
3710 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003711 Ai = tls12_prf->Ai_with_seed + hash_length;
3712 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003713 }
3714 else
3715 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003716 Ai = tls12_prf->Ai_with_seed;
3717 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003718 }
3719
Hanno Becker3b339e22018-11-13 20:56:14 +00003720 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3721 status = psa_hmac_setup_internal( &hmac,
3722 tls12_prf->key,
3723 tls12_prf->key_len,
3724 hash_alg );
3725 if( status != PSA_SUCCESS )
3726 goto cleanup;
3727
3728 status = psa_hash_update( &hmac.hash_ctx,
3729 Ai, Ai_len );
3730 if( status != PSA_SUCCESS )
3731 goto cleanup;
3732
3733 status = psa_hmac_finish_internal( &hmac,
3734 tls12_prf->Ai_with_seed,
3735 hash_length );
3736 if( status != PSA_SUCCESS )
3737 goto cleanup;
3738
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003739 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3740 status = psa_hmac_setup_internal( &hmac,
3741 tls12_prf->key,
3742 tls12_prf->key_len,
3743 hash_alg );
3744 if( status != PSA_SUCCESS )
3745 goto cleanup;
3746
3747 status = psa_hash_update( &hmac.hash_ctx,
3748 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003749 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003750 if( status != PSA_SUCCESS )
3751 goto cleanup;
3752
3753 status = psa_hmac_finish_internal( &hmac,
3754 tls12_prf->output_block,
3755 hash_length );
3756 if( status != PSA_SUCCESS )
3757 goto cleanup;
3758
3759cleanup:
3760
3761 cleanup_status = psa_hmac_abort_internal( &hmac );
3762 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3763 status = cleanup_status;
3764
3765 return( status );
3766}
3767
3768/* Read some bytes from an TLS-1.2-PRF-based generator.
3769 * See Section 5 of RFC 5246. */
3770static psa_status_t psa_generator_tls12_prf_read(
3771 psa_tls12_prf_generator_t *tls12_prf,
3772 psa_algorithm_t alg,
3773 uint8_t *output,
3774 size_t output_length )
3775{
3776 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3777 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3778 psa_status_t status;
3779
3780 while( output_length != 0 )
3781 {
3782 /* Copy what remains of the current block */
3783 uint8_t n = hash_length - tls12_prf->offset_in_block;
3784
3785 /* Check if we have fully processed the current block. */
3786 if( n == 0 )
3787 {
3788 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3789 alg );
3790 if( status != PSA_SUCCESS )
3791 return( status );
3792
3793 continue;
3794 }
3795
3796 if( n > output_length )
3797 n = (uint8_t) output_length;
3798 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3799 n );
3800 output += n;
3801 output_length -= n;
3802 tls12_prf->offset_in_block += n;
3803 }
3804
3805 return( PSA_SUCCESS );
3806}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003807#endif /* MBEDTLS_MD_C */
3808
Gilles Peskineeab56e42018-07-12 17:12:33 +02003809psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3810 uint8_t *output,
3811 size_t output_length )
3812{
3813 psa_status_t status;
3814
3815 if( output_length > generator->capacity )
3816 {
3817 generator->capacity = 0;
3818 /* Go through the error path to wipe all confidential data now
3819 * that the generator object is useless. */
3820 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3821 goto exit;
3822 }
3823 if( output_length == 0 &&
3824 generator->capacity == 0 && generator->alg == 0 )
3825 {
3826 /* Edge case: this is a blank or finished generator, and 0
3827 * bytes were requested. The right error in this case could
3828 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3829 * INSUFFICIENT_CAPACITY, which is right for a finished
3830 * generator, for consistency with the case when
3831 * output_length > 0. */
3832 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3833 }
3834 generator->capacity -= output_length;
3835
Gilles Peskine751d9652018-09-18 12:05:44 +02003836 if( generator->alg == PSA_ALG_SELECT_RAW )
3837 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003838 /* Initially, the capacity of a selection generator is always
3839 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3840 * abbreviated in this comment as `size`. When the remaining
3841 * capacity is `c`, the next bytes to serve start `c` bytes
3842 * from the end of the buffer, i.e. `size - c` from the
3843 * beginning of the buffer. Since `generator->capacity` was just
3844 * decremented above, we need to serve the bytes from
3845 * `size - generator->capacity - output_length` to
3846 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003847 size_t offset =
3848 generator->ctx.buffer.size - generator->capacity - output_length;
3849 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3850 status = PSA_SUCCESS;
3851 }
3852 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003853#if defined(MBEDTLS_MD_C)
3854 if( PSA_ALG_IS_HKDF( generator->alg ) )
3855 {
3856 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3857 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3858 output, output_length );
3859 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003860 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3861 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003862 {
3863 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3864 generator->alg, output,
3865 output_length );
3866 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003867 else
3868#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003869 {
3870 return( PSA_ERROR_BAD_STATE );
3871 }
3872
3873exit:
3874 if( status != PSA_SUCCESS )
3875 {
3876 psa_generator_abort( generator );
3877 memset( output, '!', output_length );
3878 }
3879 return( status );
3880}
3881
Gilles Peskine08542d82018-07-19 17:05:42 +02003882#if defined(MBEDTLS_DES_C)
3883static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3884{
3885 if( data_size >= 8 )
3886 mbedtls_des_key_set_parity( data );
3887 if( data_size >= 16 )
3888 mbedtls_des_key_set_parity( data + 8 );
3889 if( data_size >= 24 )
3890 mbedtls_des_key_set_parity( data + 16 );
3891}
3892#endif /* MBEDTLS_DES_C */
3893
Gilles Peskineeab56e42018-07-12 17:12:33 +02003894psa_status_t psa_generator_import_key( psa_key_slot_t key,
3895 psa_key_type_t type,
3896 size_t bits,
3897 psa_crypto_generator_t *generator )
3898{
3899 uint8_t *data = NULL;
3900 size_t bytes = PSA_BITS_TO_BYTES( bits );
3901 psa_status_t status;
3902
3903 if( ! key_type_is_raw_bytes( type ) )
3904 return( PSA_ERROR_INVALID_ARGUMENT );
3905 if( bits % 8 != 0 )
3906 return( PSA_ERROR_INVALID_ARGUMENT );
3907 data = mbedtls_calloc( 1, bytes );
3908 if( data == NULL )
3909 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3910
3911 status = psa_generator_read( generator, data, bytes );
3912 if( status != PSA_SUCCESS )
3913 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003914#if defined(MBEDTLS_DES_C)
3915 if( type == PSA_KEY_TYPE_DES )
3916 psa_des_set_key_parity( data, bytes );
3917#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003918 status = psa_import_key( key, type, data, bytes );
3919
3920exit:
3921 mbedtls_free( data );
3922 return( status );
3923}
3924
3925
3926
3927/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003928/* Key derivation */
3929/****************************************************************/
3930
Gilles Peskinea05219c2018-11-16 16:02:56 +01003931#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003932/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003933 * of the HKDF algorithm.
3934 *
3935 * Note that if this function fails, you must call psa_generator_abort()
3936 * to potentially free embedded data structures and wipe confidential data.
3937 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003938static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003939 const uint8_t *secret,
3940 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003941 psa_algorithm_t hash_alg,
3942 const uint8_t *salt,
3943 size_t salt_length,
3944 const uint8_t *label,
3945 size_t label_length )
3946{
3947 psa_status_t status;
3948 status = psa_hmac_setup_internal( &hkdf->hmac,
3949 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003950 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003951 if( status != PSA_SUCCESS )
3952 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003953 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003954 if( status != PSA_SUCCESS )
3955 return( status );
3956 status = psa_hmac_finish_internal( &hkdf->hmac,
3957 hkdf->prk,
3958 sizeof( hkdf->prk ) );
3959 if( status != PSA_SUCCESS )
3960 return( status );
3961 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3962 hkdf->block_number = 0;
3963 hkdf->info_length = label_length;
3964 if( label_length != 0 )
3965 {
3966 hkdf->info = mbedtls_calloc( 1, label_length );
3967 if( hkdf->info == NULL )
3968 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3969 memcpy( hkdf->info, label, label_length );
3970 }
3971 return( PSA_SUCCESS );
3972}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003973#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003974
Gilles Peskinea05219c2018-11-16 16:02:56 +01003975#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003976/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3977 *
3978 * Note that if this function fails, you must call psa_generator_abort()
3979 * to potentially free embedded data structures and wipe confidential data.
3980 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003981static psa_status_t psa_generator_tls12_prf_setup(
3982 psa_tls12_prf_generator_t *tls12_prf,
3983 const unsigned char *key,
3984 size_t key_len,
3985 psa_algorithm_t hash_alg,
3986 const uint8_t *salt,
3987 size_t salt_length,
3988 const uint8_t *label,
3989 size_t label_length )
3990{
3991 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003992 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3993 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003994
3995 tls12_prf->key = mbedtls_calloc( 1, key_len );
3996 if( tls12_prf->key == NULL )
3997 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3998 tls12_prf->key_len = key_len;
3999 memcpy( tls12_prf->key, key, key_len );
4000
Hanno Becker580fba12018-11-13 20:50:45 +00004001 overflow = ( salt_length + label_length < salt_length ) ||
4002 ( salt_length + label_length + hash_length < hash_length );
4003 if( overflow )
4004 return( PSA_ERROR_INVALID_ARGUMENT );
4005
4006 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4007 if( tls12_prf->Ai_with_seed == NULL )
4008 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4009 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4010
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004011 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4012 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004013 if( label_length != 0 )
4014 {
4015 memcpy( tls12_prf->Ai_with_seed + hash_length,
4016 label, label_length );
4017 }
4018
4019 if( salt_length != 0 )
4020 {
4021 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4022 salt, salt_length );
4023 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004024
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004025 /* The first block gets generated when
4026 * psa_generator_read() is called. */
4027 tls12_prf->block_number = 0;
4028 tls12_prf->offset_in_block = hash_length;
4029
4030 return( PSA_SUCCESS );
4031}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004032
4033/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4034static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4035 psa_tls12_prf_generator_t *tls12_prf,
4036 const unsigned char *psk,
4037 size_t psk_len,
4038 psa_algorithm_t hash_alg,
4039 const uint8_t *salt,
4040 size_t salt_length,
4041 const uint8_t *label,
4042 size_t label_length )
4043{
4044 psa_status_t status;
4045 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4046
4047 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4048 return( PSA_ERROR_INVALID_ARGUMENT );
4049
4050 /* Quoting RFC 4279, Section 2:
4051 *
4052 * The premaster secret is formed as follows: if the PSK is N octets
4053 * long, concatenate a uint16 with the value N, N zero octets, a second
4054 * uint16 with the value N, and the PSK itself.
4055 */
4056
4057 pms[0] = ( psk_len >> 8 ) & 0xff;
4058 pms[1] = ( psk_len >> 0 ) & 0xff;
4059 memset( pms + 2, 0, psk_len );
4060 pms[2 + psk_len + 0] = pms[0];
4061 pms[2 + psk_len + 1] = pms[1];
4062 memcpy( pms + 4 + psk_len, psk, psk_len );
4063
4064 status = psa_generator_tls12_prf_setup( tls12_prf,
4065 pms, 4 + 2 * psk_len,
4066 hash_alg,
4067 salt, salt_length,
4068 label, label_length );
4069
4070 mbedtls_zeroize( pms, sizeof( pms ) );
4071 return( status );
4072}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004073#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004074
Gilles Peskine346797d2018-11-16 16:05:06 +01004075/* Note that if this function fails, you must call psa_generator_abort()
4076 * to potentially free embedded data structures and wipe confidential data.
4077 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004078static psa_status_t psa_key_derivation_internal(
4079 psa_crypto_generator_t *generator,
4080 const uint8_t *secret, size_t secret_length,
4081 psa_algorithm_t alg,
4082 const uint8_t *salt, size_t salt_length,
4083 const uint8_t *label, size_t label_length,
4084 size_t capacity )
4085{
4086 psa_status_t status;
4087 size_t max_capacity;
4088
4089 /* Set generator->alg even on failure so that abort knows what to do. */
4090 generator->alg = alg;
4091
Gilles Peskine751d9652018-09-18 12:05:44 +02004092 if( alg == PSA_ALG_SELECT_RAW )
4093 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004094 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004095 if( salt_length != 0 )
4096 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004097 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004098 if( label_length != 0 )
4099 return( PSA_ERROR_INVALID_ARGUMENT );
4100 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4101 if( generator->ctx.buffer.data == NULL )
4102 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4103 memcpy( generator->ctx.buffer.data, secret, secret_length );
4104 generator->ctx.buffer.size = secret_length;
4105 max_capacity = secret_length;
4106 status = PSA_SUCCESS;
4107 }
4108 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004109#if defined(MBEDTLS_MD_C)
4110 if( PSA_ALG_IS_HKDF( alg ) )
4111 {
4112 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4113 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4114 if( hash_size == 0 )
4115 return( PSA_ERROR_NOT_SUPPORTED );
4116 max_capacity = 255 * hash_size;
4117 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4118 secret, secret_length,
4119 hash_alg,
4120 salt, salt_length,
4121 label, label_length );
4122 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004123 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4124 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4125 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004126 {
4127 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4128 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4129
4130 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4131 if( hash_alg != PSA_ALG_SHA_256 &&
4132 hash_alg != PSA_ALG_SHA_384 )
4133 {
4134 return( PSA_ERROR_NOT_SUPPORTED );
4135 }
4136
4137 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004138
4139 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4140 {
4141 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4142 secret, secret_length,
4143 hash_alg, salt, salt_length,
4144 label, label_length );
4145 }
4146 else
4147 {
4148 status = psa_generator_tls12_psk_to_ms_setup(
4149 &generator->ctx.tls12_prf,
4150 secret, secret_length,
4151 hash_alg, salt, salt_length,
4152 label, label_length );
4153 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004154 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004155 else
4156#endif
4157 {
4158 return( PSA_ERROR_NOT_SUPPORTED );
4159 }
4160
4161 if( status != PSA_SUCCESS )
4162 return( status );
4163
4164 if( capacity <= max_capacity )
4165 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004166 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4167 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004168 else
4169 return( PSA_ERROR_INVALID_ARGUMENT );
4170
4171 return( PSA_SUCCESS );
4172}
4173
Gilles Peskineea0fb492018-07-12 17:17:20 +02004174psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01004175 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004176 psa_algorithm_t alg,
4177 const uint8_t *salt,
4178 size_t salt_length,
4179 const uint8_t *label,
4180 size_t label_length,
4181 size_t capacity )
4182{
4183 key_slot_t *slot;
4184 psa_status_t status;
4185
4186 if( generator->alg != 0 )
4187 return( PSA_ERROR_BAD_STATE );
4188
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004189 /* Make sure that alg is a key derivation algorithm. This prevents
4190 * key selection algorithms, which psa_key_derivation_internal
4191 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004192 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4193 return( PSA_ERROR_INVALID_ARGUMENT );
4194
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004195 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4196 if( status != PSA_SUCCESS )
4197 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004198
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004199 if( slot->type != PSA_KEY_TYPE_DERIVE )
4200 return( PSA_ERROR_INVALID_ARGUMENT );
4201
4202 status = psa_key_derivation_internal( generator,
4203 slot->data.raw.data,
4204 slot->data.raw.bytes,
4205 alg,
4206 salt, salt_length,
4207 label, label_length,
4208 capacity );
4209 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004210 psa_generator_abort( generator );
4211 return( status );
4212}
4213
4214
4215
4216/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004217/* Key agreement */
4218/****************************************************************/
4219
Gilles Peskinea05219c2018-11-16 16:02:56 +01004220#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004221static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4222 size_t peer_key_length,
4223 const mbedtls_ecp_keypair *our_key,
4224 uint8_t *shared_secret,
4225 size_t shared_secret_size,
4226 size_t *shared_secret_length )
4227{
4228 mbedtls_pk_context pk;
4229 mbedtls_ecp_keypair *their_key = NULL;
4230 mbedtls_ecdh_context ecdh;
4231 int ret;
4232 mbedtls_ecdh_init( &ecdh );
4233 mbedtls_pk_init( &pk );
4234
4235 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4236 if( ret != 0 )
4237 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004238 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004239 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004240 case MBEDTLS_PK_ECKEY:
4241 case MBEDTLS_PK_ECKEY_DH:
4242 break;
4243 default:
4244 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4245 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004246 }
4247 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004248 if( their_key->grp.id != our_key->grp.id )
4249 {
4250 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4251 goto exit;
4252 }
4253
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004254 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4255 if( ret != 0 )
4256 goto exit;
4257 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4258 if( ret != 0 )
4259 goto exit;
4260
4261 ret = mbedtls_ecdh_calc_secret( &ecdh,
4262 shared_secret_length,
4263 shared_secret, shared_secret_size,
4264 mbedtls_ctr_drbg_random,
4265 &global_data.ctr_drbg );
4266
4267exit:
4268 mbedtls_pk_free( &pk );
4269 mbedtls_ecdh_free( &ecdh );
4270 return( mbedtls_to_psa_error( ret ) );
4271}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004272#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004273
Gilles Peskine01d718c2018-09-18 12:01:02 +02004274#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4275
Gilles Peskine346797d2018-11-16 16:05:06 +01004276/* Note that if this function fails, you must call psa_generator_abort()
4277 * to potentially free embedded data structures and wipe confidential data.
4278 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004279static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4280 key_slot_t *private_key,
4281 const uint8_t *peer_key,
4282 size_t peer_key_length,
4283 psa_algorithm_t alg )
4284{
4285 psa_status_t status;
4286 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4287 size_t shared_secret_length = 0;
4288
4289 /* Step 1: run the secret agreement algorithm to generate the shared
4290 * secret. */
4291 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4292 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004293#if defined(MBEDTLS_ECDH_C)
4294 case PSA_ALG_ECDH_BASE:
4295 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4296 return( PSA_ERROR_INVALID_ARGUMENT );
4297 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4298 private_key->data.ecp,
4299 shared_secret,
4300 sizeof( shared_secret ),
4301 &shared_secret_length );
4302 break;
4303#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004304 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004305 (void) private_key;
4306 (void) peer_key;
4307 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004308 return( PSA_ERROR_NOT_SUPPORTED );
4309 }
4310 if( status != PSA_SUCCESS )
4311 goto exit;
4312
4313 /* Step 2: set up the key derivation to generate key material from
4314 * the shared secret. */
4315 status = psa_key_derivation_internal( generator,
4316 shared_secret, shared_secret_length,
4317 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4318 NULL, 0, NULL, 0,
4319 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4320exit:
4321 mbedtls_zeroize( shared_secret, shared_secret_length );
4322 return( status );
4323}
4324
4325psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4326 psa_key_slot_t private_key,
4327 const uint8_t *peer_key,
4328 size_t peer_key_length,
4329 psa_algorithm_t alg )
4330{
4331 key_slot_t *slot;
4332 psa_status_t status;
4333 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4334 return( PSA_ERROR_INVALID_ARGUMENT );
4335 status = psa_get_key_from_slot( private_key, &slot,
4336 PSA_KEY_USAGE_DERIVE, alg );
4337 if( status != PSA_SUCCESS )
4338 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004339 status = psa_key_agreement_internal( generator,
4340 slot,
4341 peer_key, peer_key_length,
4342 alg );
4343 if( status != PSA_SUCCESS )
4344 psa_generator_abort( generator );
4345 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004346}
4347
4348
4349
4350/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004351/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004352/****************************************************************/
4353
4354psa_status_t psa_generate_random( uint8_t *output,
4355 size_t output_size )
4356{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004357 int ret;
4358 GUARD_MODULE_INITIALIZED;
4359
4360 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004361 return( mbedtls_to_psa_error( ret ) );
4362}
4363
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004364#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004365
4366/* Support function for error conversion between psa_its error codes to psa crypto */
4367static psa_status_t its_to_psa_error( psa_its_status_t ret )
4368{
4369 switch( ret )
4370 {
4371 case PSA_ITS_SUCCESS:
4372 return( PSA_SUCCESS );
4373
4374 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4375 return( PSA_ERROR_EMPTY_SLOT );
4376
4377 case PSA_ITS_ERROR_STORAGE_FAILURE:
4378 return( PSA_ERROR_STORAGE_FAILURE );
4379
4380 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4381 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4382
4383 case PSA_ITS_ERROR_INVALID_KEY:
4384 case PSA_PS_ERROR_OFFSET_INVALID:
4385 case PSA_ITS_ERROR_INCORRECT_SIZE:
4386 case PSA_ITS_ERROR_BAD_POINTER:
4387 return( PSA_ERROR_INVALID_ARGUMENT );
4388
4389 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4390 return( PSA_ERROR_NOT_SUPPORTED );
4391
4392 case PSA_ITS_ERROR_WRITE_ONCE:
4393 return( PSA_ERROR_OCCUPIED_SLOT );
4394
4395 default:
4396 return( PSA_ERROR_UNKNOWN_ERROR );
4397 }
4398}
4399
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004400psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4401 size_t seed_size )
4402{
4403 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004404 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004405 struct psa_its_info_t p_info;
4406 if( global_data.initialized )
4407 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004408
4409 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4410 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4411 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4412 return( PSA_ERROR_INVALID_ARGUMENT );
4413
avolinski0d2c2662018-11-21 17:31:07 +02004414 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004415 status = its_to_psa_error( its_status );
4416
4417 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004418 {
avolinski0d2c2662018-11-21 17:31:07 +02004419 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004420 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004421 }
avolinski13beb102018-11-20 16:51:49 +02004422 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004423 {
4424 /* You should not be here. Seed needs to be injected only once */
4425 status = PSA_ERROR_NOT_PERMITTED;
4426 }
4427 return( status );
4428}
4429#endif
4430
Gilles Peskine05d69892018-06-19 22:00:52 +02004431psa_status_t psa_generate_key( psa_key_slot_t key,
4432 psa_key_type_t type,
4433 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004434 const void *extra,
4435 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004436{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004437 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004438 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004439
Gilles Peskine53d991e2018-07-12 01:14:59 +02004440 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004441 return( PSA_ERROR_INVALID_ARGUMENT );
4442
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004443 status = psa_get_empty_key_slot( key, &slot );
4444 if( status != PSA_SUCCESS )
4445 return( status );
4446
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004447 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004448 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004449 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004450 if( status != PSA_SUCCESS )
4451 return( status );
4452 status = psa_generate_random( slot->data.raw.data,
4453 slot->data.raw.bytes );
4454 if( status != PSA_SUCCESS )
4455 {
4456 mbedtls_free( slot->data.raw.data );
4457 return( status );
4458 }
4459#if defined(MBEDTLS_DES_C)
4460 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004461 psa_des_set_key_parity( slot->data.raw.data,
4462 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004463#endif /* MBEDTLS_DES_C */
4464 }
4465 else
4466
4467#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4468 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4469 {
4470 mbedtls_rsa_context *rsa;
4471 int ret;
4472 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004473 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4474 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004475 /* Accept only byte-aligned keys, for the same reasons as
4476 * in psa_import_rsa_key(). */
4477 if( bits % 8 != 0 )
4478 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004479 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004480 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004481 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004482 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004483 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004484#if INT_MAX < 0xffffffff
4485 /* Check that the uint32_t value passed by the caller fits
4486 * in the range supported by this implementation. */
4487 if( p->e > INT_MAX )
4488 return( PSA_ERROR_NOT_SUPPORTED );
4489#endif
4490 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004491 }
4492 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4493 if( rsa == NULL )
4494 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4495 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4496 ret = mbedtls_rsa_gen_key( rsa,
4497 mbedtls_ctr_drbg_random,
4498 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004499 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004500 exponent );
4501 if( ret != 0 )
4502 {
4503 mbedtls_rsa_free( rsa );
4504 mbedtls_free( rsa );
4505 return( mbedtls_to_psa_error( ret ) );
4506 }
4507 slot->data.rsa = rsa;
4508 }
4509 else
4510#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4511
4512#if defined(MBEDTLS_ECP_C)
4513 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4514 {
4515 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4516 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4517 const mbedtls_ecp_curve_info *curve_info =
4518 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4519 mbedtls_ecp_keypair *ecp;
4520 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004521 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004522 return( PSA_ERROR_NOT_SUPPORTED );
4523 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4524 return( PSA_ERROR_NOT_SUPPORTED );
4525 if( curve_info->bit_size != bits )
4526 return( PSA_ERROR_INVALID_ARGUMENT );
4527 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4528 if( ecp == NULL )
4529 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4530 mbedtls_ecp_keypair_init( ecp );
4531 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4532 mbedtls_ctr_drbg_random,
4533 &global_data.ctr_drbg );
4534 if( ret != 0 )
4535 {
4536 mbedtls_ecp_keypair_free( ecp );
4537 mbedtls_free( ecp );
4538 return( mbedtls_to_psa_error( ret ) );
4539 }
4540 slot->data.ecp = ecp;
4541 }
4542 else
4543#endif /* MBEDTLS_ECP_C */
4544
4545 return( PSA_ERROR_NOT_SUPPORTED );
4546
4547 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004548
4549#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4550 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4551 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004552 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004553 }
4554#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4555
4556 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004557}
4558
4559
4560/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004561/* Module setup */
4562/****************************************************************/
4563
Gilles Peskine5e769522018-11-20 21:59:56 +01004564psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4565 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4566 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4567{
4568 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4569 return( PSA_ERROR_BAD_STATE );
4570 global_data.entropy_init = entropy_init;
4571 global_data.entropy_free = entropy_free;
4572 return( PSA_SUCCESS );
4573}
4574
Gilles Peskinee59236f2018-01-27 23:32:46 +01004575void mbedtls_psa_crypto_free( void )
4576{
Gilles Peskinec6b69072018-11-20 21:42:52 +01004577 if( global_data.key_slots_initialized )
Darryl Green40225ba2018-11-15 14:48:15 +00004578 {
Gilles Peskined7c75702018-12-03 10:36:46 +01004579 psa_key_slot_t key;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004580 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
4581 {
Gilles Peskined7c75702018-12-03 10:36:46 +01004582 key_slot_t *slot = &global_data.key_slots[key - 1];
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01004583 (void) psa_wipe_key_slot( slot );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004584 }
Darryl Green40225ba2018-11-15 14:48:15 +00004585 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01004586 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4587 {
4588 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004589 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004590 }
4591 /* Wipe all remaining data, including configuration.
4592 * In particular, this sets all state indicator to the value
4593 * indicating "uninitialized". */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004594 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4595}
4596
4597psa_status_t psa_crypto_init( void )
4598{
4599 int ret;
4600 const unsigned char drbg_seed[] = "PSA";
4601
Gilles Peskinec6b69072018-11-20 21:42:52 +01004602 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004603 if( global_data.initialized != 0 )
4604 return( PSA_SUCCESS );
4605
Gilles Peskine5e769522018-11-20 21:59:56 +01004606 /* Set default configuration if
4607 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4608 if( global_data.entropy_init == NULL )
4609 global_data.entropy_init = mbedtls_entropy_init;
4610 if( global_data.entropy_free == NULL )
4611 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004612
Gilles Peskinec6b69072018-11-20 21:42:52 +01004613 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004614 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004615 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004616 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004617 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4618 mbedtls_entropy_func,
4619 &global_data.entropy,
4620 drbg_seed, sizeof( drbg_seed ) - 1 );
4621 if( ret != 0 )
4622 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004623 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004624
Gilles Peskinec6b69072018-11-20 21:42:52 +01004625 /* Initialize the key slots. Zero-initialization has made all key
4626 * slots empty, so there is nothing to do. In a future version we will
4627 * load data from storage. */
4628 global_data.key_slots_initialized = 1;
4629
4630 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004631 global_data.initialized = 1;
4632
Gilles Peskinee59236f2018-01-27 23:32:46 +01004633exit:
4634 if( ret != 0 )
4635 mbedtls_psa_crypto_free( );
4636 return( mbedtls_to_psa_error( ret ) );
4637}
4638
4639#endif /* MBEDTLS_PSA_CRYPTO_C */