blob: 1a038a12a577764354f53c591ad36018c58f2fcc [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 Peskine5e769522018-11-20 21:59:56 +010046#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010047#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010048/* Include internal declarations that are useful for implementing persistently
49 * stored keys. */
50#include "psa_crypto_storage.h"
51
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include <stdlib.h>
53#include <string.h>
54#if defined(MBEDTLS_PLATFORM_C)
55#include "mbedtls/platform.h"
56#else
57#define mbedtls_calloc calloc
58#define mbedtls_free free
59#endif
60
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020062#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020063#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/blowfish.h"
65#include "mbedtls/camellia.h"
66#include "mbedtls/cipher.h"
67#include "mbedtls/ccm.h"
68#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020071#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010073#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020074#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/error.h"
76#include "mbedtls/gcm.h"
77#include "mbedtls/md2.h"
78#include "mbedtls/md4.h"
79#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010080#include "mbedtls/md.h"
81#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010082#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010083#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010085#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/sha1.h"
87#include "mbedtls/sha256.h"
88#include "mbedtls/sha512.h"
89#include "mbedtls/xtea.h"
90
Netanel Gonen2bcd3122018-11-19 11:53:02 +020091#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
92#include "psa_prot_internal_storage.h"
93#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010094
Gilles Peskine996deb12018-08-01 15:45:45 +020095#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
96
Gilles Peskinee59236f2018-01-27 23:32:46 +010097/* Implementation that should never be optimized out by the compiler */
98static void mbedtls_zeroize( void *v, size_t n )
99{
100 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
101}
102
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100103/* constant-time buffer comparison */
104static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
105{
106 size_t i;
107 unsigned char diff = 0;
108
109 for( i = 0; i < n; i++ )
110 diff |= a[i] ^ b[i];
111
112 return( diff );
113}
114
115
116
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100117/****************************************************************/
118/* Global data, support functions and library management */
119/****************************************************************/
120
Gilles Peskine2d277862018-06-18 15:41:12 +0200121typedef struct
122{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100123 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300124 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200125 psa_key_lifetime_t lifetime;
Gilles Peskine69f976b2018-11-30 18:46:56 +0100126 psa_key_id_t persistent_storage_id;
Gilles Peskine961849f2018-11-30 18:54:54 +0100127 unsigned allocated : 1;
Gilles Peskine2d277862018-06-18 15:41:12 +0200128 union
129 {
130 struct raw_data
131 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100132 uint8_t *data;
133 size_t bytes;
134 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100135#if defined(MBEDTLS_RSA_C)
136 mbedtls_rsa_context *rsa;
137#endif /* MBEDTLS_RSA_C */
138#if defined(MBEDTLS_ECP_C)
139 mbedtls_ecp_keypair *ecp;
140#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100141 } data;
142} key_slot_t;
143
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200144static int key_type_is_raw_bytes( psa_key_type_t type )
145{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200146 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200147}
148
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100149/* Values for psa_global_data_t::rng_state */
150#define RNG_NOT_INITIALIZED 0
151#define RNG_INITIALIZED 1
152#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100153
Gilles Peskine2d277862018-06-18 15:41:12 +0200154typedef struct
155{
Gilles Peskine5e769522018-11-20 21:59:56 +0100156 void (* entropy_init )( mbedtls_entropy_context *ctx );
157 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100158 mbedtls_entropy_context entropy;
159 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200160 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinec6b69072018-11-20 21:42:52 +0100161 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100162 unsigned rng_state : 2;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100163 unsigned key_slots_initialized : 1;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100164} psa_global_data_t;
165
166static psa_global_data_t global_data;
167
itayzafrir0adf0fc2018-09-06 16:24:41 +0300168#define GUARD_MODULE_INITIALIZED \
169 if( global_data.initialized == 0 ) \
170 return( PSA_ERROR_BAD_STATE );
171
Gilles Peskinee59236f2018-01-27 23:32:46 +0100172static psa_status_t mbedtls_to_psa_error( int ret )
173{
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 /* If there's both a high-level code and low-level code, dispatch on
175 * the high-level code. */
176 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100177 {
178 case 0:
179 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100180
181 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
182 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
183 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
188 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
Gilles Peskine9a944802018-06-21 09:35:35 +0200191 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
192 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
193 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
194 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
195 case MBEDTLS_ERR_ASN1_INVALID_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
200 return( PSA_ERROR_BUFFER_TOO_SMALL );
201
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
203 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
204 return( PSA_ERROR_NOT_SUPPORTED );
205 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
206 return( PSA_ERROR_HARDWARE_FAILURE );
207
208 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
209 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
210 return( PSA_ERROR_NOT_SUPPORTED );
211 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CCM_BAD_INPUT:
215 return( PSA_ERROR_INVALID_ARGUMENT );
216 case MBEDTLS_ERR_CCM_AUTH_FAILED:
217 return( PSA_ERROR_INVALID_SIGNATURE );
218 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
221 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
224 return( PSA_ERROR_INVALID_ARGUMENT );
225 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
226 return( PSA_ERROR_INSUFFICIENT_MEMORY );
227 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
228 return( PSA_ERROR_INVALID_PADDING );
229 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
230 return( PSA_ERROR_BAD_STATE );
231 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
234 return( PSA_ERROR_TAMPERING_DETECTED );
235 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
242 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
243 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
244 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
247 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
248
249 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
250 return( PSA_ERROR_NOT_SUPPORTED );
251 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinee59236f2018-01-27 23:32:46 +0100254 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
255 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
256 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
257 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100258
259 case MBEDTLS_ERR_GCM_AUTH_FAILED:
260 return( PSA_ERROR_INVALID_SIGNATURE );
261 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200262 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100263 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
264 return( PSA_ERROR_HARDWARE_FAILURE );
265
266 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
267 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
268 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
269 return( PSA_ERROR_HARDWARE_FAILURE );
270
271 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_MD_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
278 return( PSA_ERROR_STORAGE_FAILURE );
279 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
280 return( PSA_ERROR_HARDWARE_FAILURE );
281
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
283 return( PSA_ERROR_STORAGE_FAILURE );
284 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
285 return( PSA_ERROR_INVALID_ARGUMENT );
286 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
289 return( PSA_ERROR_BUFFER_TOO_SMALL );
290 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
297 return( PSA_ERROR_INSUFFICIENT_MEMORY );
298
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 case MBEDTLS_ERR_PK_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
302 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
307 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
312 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
313 return( PSA_ERROR_NOT_PERMITTED );
314 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_PK_INVALID_ALG:
317 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
318 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
321 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
323 return( PSA_ERROR_HARDWARE_FAILURE );
324
325 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
329 return( PSA_ERROR_INVALID_ARGUMENT );
330 case MBEDTLS_ERR_RSA_INVALID_PADDING:
331 return( PSA_ERROR_INVALID_PADDING );
332 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
337 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
338 return( PSA_ERROR_TAMPERING_DETECTED );
339 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
340 return( PSA_ERROR_INVALID_SIGNATURE );
341 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
342 return( PSA_ERROR_BUFFER_TOO_SMALL );
343 case MBEDTLS_ERR_RSA_RNG_FAILED:
344 return( PSA_ERROR_INSUFFICIENT_MEMORY );
345 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
348 return( PSA_ERROR_HARDWARE_FAILURE );
349
350 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
351 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
352 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
354
355 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
356 return( PSA_ERROR_INVALID_ARGUMENT );
357 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
358 return( PSA_ERROR_HARDWARE_FAILURE );
359
itayzafrir5c753392018-05-08 11:18:38 +0300360 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300361 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300362 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300363 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
364 return( PSA_ERROR_BUFFER_TOO_SMALL );
365 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
366 return( PSA_ERROR_NOT_SUPPORTED );
367 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
368 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
369 return( PSA_ERROR_INVALID_SIGNATURE );
370 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
371 return( PSA_ERROR_INSUFFICIENT_MEMORY );
372 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
373 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300374
Gilles Peskinee59236f2018-01-27 23:32:46 +0100375 default:
376 return( PSA_ERROR_UNKNOWN_ERROR );
377 }
378}
379
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200380
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200381
382
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100383/****************************************************************/
384/* Key management */
385/****************************************************************/
386
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100387#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200388static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
389{
390 switch( grpid )
391 {
392 case MBEDTLS_ECP_DP_SECP192R1:
393 return( PSA_ECC_CURVE_SECP192R1 );
394 case MBEDTLS_ECP_DP_SECP224R1:
395 return( PSA_ECC_CURVE_SECP224R1 );
396 case MBEDTLS_ECP_DP_SECP256R1:
397 return( PSA_ECC_CURVE_SECP256R1 );
398 case MBEDTLS_ECP_DP_SECP384R1:
399 return( PSA_ECC_CURVE_SECP384R1 );
400 case MBEDTLS_ECP_DP_SECP521R1:
401 return( PSA_ECC_CURVE_SECP521R1 );
402 case MBEDTLS_ECP_DP_BP256R1:
403 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
404 case MBEDTLS_ECP_DP_BP384R1:
405 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
406 case MBEDTLS_ECP_DP_BP512R1:
407 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
408 case MBEDTLS_ECP_DP_CURVE25519:
409 return( PSA_ECC_CURVE_CURVE25519 );
410 case MBEDTLS_ECP_DP_SECP192K1:
411 return( PSA_ECC_CURVE_SECP192K1 );
412 case MBEDTLS_ECP_DP_SECP224K1:
413 return( PSA_ECC_CURVE_SECP224K1 );
414 case MBEDTLS_ECP_DP_SECP256K1:
415 return( PSA_ECC_CURVE_SECP256K1 );
416 case MBEDTLS_ECP_DP_CURVE448:
417 return( PSA_ECC_CURVE_CURVE448 );
418 default:
419 return( 0 );
420 }
421}
422
Gilles Peskine12313cd2018-06-20 00:20:32 +0200423static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
424{
425 switch( curve )
426 {
427 case PSA_ECC_CURVE_SECP192R1:
428 return( MBEDTLS_ECP_DP_SECP192R1 );
429 case PSA_ECC_CURVE_SECP224R1:
430 return( MBEDTLS_ECP_DP_SECP224R1 );
431 case PSA_ECC_CURVE_SECP256R1:
432 return( MBEDTLS_ECP_DP_SECP256R1 );
433 case PSA_ECC_CURVE_SECP384R1:
434 return( MBEDTLS_ECP_DP_SECP384R1 );
435 case PSA_ECC_CURVE_SECP521R1:
436 return( MBEDTLS_ECP_DP_SECP521R1 );
437 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
438 return( MBEDTLS_ECP_DP_BP256R1 );
439 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
440 return( MBEDTLS_ECP_DP_BP384R1 );
441 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
442 return( MBEDTLS_ECP_DP_BP512R1 );
443 case PSA_ECC_CURVE_CURVE25519:
444 return( MBEDTLS_ECP_DP_CURVE25519 );
445 case PSA_ECC_CURVE_SECP192K1:
446 return( MBEDTLS_ECP_DP_SECP192K1 );
447 case PSA_ECC_CURVE_SECP224K1:
448 return( MBEDTLS_ECP_DP_SECP224K1 );
449 case PSA_ECC_CURVE_SECP256K1:
450 return( MBEDTLS_ECP_DP_SECP256K1 );
451 case PSA_ECC_CURVE_CURVE448:
452 return( MBEDTLS_ECP_DP_CURVE448 );
453 default:
454 return( MBEDTLS_ECP_DP_NONE );
455 }
456}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100457#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200458
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
460 size_t bits,
461 struct raw_data *raw )
462{
463 /* Check that the bit size is acceptable for the key type */
464 switch( type )
465 {
466 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200467 if( bits == 0 )
468 {
469 raw->bytes = 0;
470 raw->data = NULL;
471 return( PSA_SUCCESS );
472 }
473 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200474#if defined(MBEDTLS_MD_C)
475 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200476#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200477 case PSA_KEY_TYPE_DERIVE:
478 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200479#if defined(MBEDTLS_AES_C)
480 case PSA_KEY_TYPE_AES:
481 if( bits != 128 && bits != 192 && bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
485#if defined(MBEDTLS_CAMELLIA_C)
486 case PSA_KEY_TYPE_CAMELLIA:
487 if( bits != 128 && bits != 192 && bits != 256 )
488 return( PSA_ERROR_INVALID_ARGUMENT );
489 break;
490#endif
491#if defined(MBEDTLS_DES_C)
492 case PSA_KEY_TYPE_DES:
493 if( bits != 64 && bits != 128 && bits != 192 )
494 return( PSA_ERROR_INVALID_ARGUMENT );
495 break;
496#endif
497#if defined(MBEDTLS_ARC4_C)
498 case PSA_KEY_TYPE_ARC4:
499 if( bits < 8 || bits > 2048 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
501 break;
502#endif
503 default:
504 return( PSA_ERROR_NOT_SUPPORTED );
505 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200506 if( bits % 8 != 0 )
507 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508
509 /* Allocate memory for the key */
510 raw->bytes = PSA_BITS_TO_BYTES( bits );
511 raw->data = mbedtls_calloc( 1, raw->bytes );
512 if( raw->data == NULL )
513 {
514 raw->bytes = 0;
515 return( PSA_ERROR_INSUFFICIENT_MEMORY );
516 }
517 return( PSA_SUCCESS );
518}
519
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200520#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100521/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
522 * that are not a multiple of 8) well. For example, there is only
523 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
524 * way to return the exact bit size of a key.
525 * To keep things simple, reject non-byte-aligned key sizes. */
526static psa_status_t psa_check_rsa_key_byte_aligned(
527 const mbedtls_rsa_context *rsa )
528{
529 mbedtls_mpi n;
530 psa_status_t status;
531 mbedtls_mpi_init( &n );
532 status = mbedtls_to_psa_error(
533 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
534 if( status == PSA_SUCCESS )
535 {
536 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
537 status = PSA_ERROR_NOT_SUPPORTED;
538 }
539 mbedtls_mpi_free( &n );
540 return( status );
541}
542
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200543static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
544 mbedtls_rsa_context **p_rsa )
545{
546 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
547 return( PSA_ERROR_INVALID_ARGUMENT );
548 else
549 {
550 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100551 /* The size of an RSA key doesn't have to be a multiple of 8.
552 * Mbed TLS supports non-byte-aligned key sizes, but not well.
553 * For example, mbedtls_rsa_get_len() returns the key size in
554 * bytes, not in bits. */
555 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100556 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
558 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100559 status = psa_check_rsa_key_byte_aligned( rsa );
560 if( status != PSA_SUCCESS )
561 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200562 *p_rsa = rsa;
563 return( PSA_SUCCESS );
564 }
565}
566#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
567
568#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100569/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
571 mbedtls_pk_context *pk,
572 mbedtls_ecp_keypair **p_ecp )
573{
574 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
575 return( PSA_ERROR_INVALID_ARGUMENT );
576 else
577 {
578 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
579 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
580 if( actual_curve != expected_curve )
581 return( PSA_ERROR_INVALID_ARGUMENT );
582 *p_ecp = ecp;
583 return( PSA_SUCCESS );
584 }
585}
586#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
587
Gilles Peskinef76aa772018-10-29 19:24:33 +0100588#if defined(MBEDTLS_ECP_C)
589/* Import a private key given as a byte string which is the private value
590 * in big-endian order. */
591static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
592 const uint8_t *data,
593 size_t data_length,
594 mbedtls_ecp_keypair **p_ecp )
595{
596 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
597 mbedtls_ecp_keypair *ecp = NULL;
598 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
599
600 *p_ecp = NULL;
601 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
602 if( ecp == NULL )
603 return( PSA_ERROR_INSUFFICIENT_MEMORY );
604
605 /* Load the group. */
606 status = mbedtls_to_psa_error(
607 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
608 if( status != PSA_SUCCESS )
609 goto exit;
610 /* Load the secret value. */
611 status = mbedtls_to_psa_error(
612 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
613 if( status != PSA_SUCCESS )
614 goto exit;
615 /* Validate the private key. */
616 status = mbedtls_to_psa_error(
617 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
618 if( status != PSA_SUCCESS )
619 goto exit;
620 /* Calculate the public key from the private key. */
621 status = mbedtls_to_psa_error(
622 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
623 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
624 if( status != PSA_SUCCESS )
625 goto exit;
626
627 *p_ecp = ecp;
628 return( PSA_SUCCESS );
629
630exit:
631 if( ecp != NULL )
632 {
633 mbedtls_ecp_keypair_free( ecp );
634 mbedtls_free( ecp );
635 }
636 return( status );
637}
638#endif /* defined(MBEDTLS_ECP_C) */
639
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100640/** Import key data into a slot. `slot->type` must have been set
641 * previously. This function assumes that the slot does not contain
642 * any key material yet. On failure, the slot content is unchanged. */
Darryl Green940d72c2018-07-13 13:18:51 +0100643static psa_status_t psa_import_key_into_slot( key_slot_t *slot,
644 const uint8_t *data,
645 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200647 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648
Darryl Green940d72c2018-07-13 13:18:51 +0100649 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100651 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 if( data_length > SIZE_MAX / 8 )
653 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100654 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200655 PSA_BYTES_TO_BITS( data_length ),
656 &slot->data.raw );
657 if( status != PSA_SUCCESS )
658 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200659 if( data_length != 0 )
660 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661 }
662 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100663#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100664 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100665 {
Darryl Green940d72c2018-07-13 13:18:51 +0100666 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667 data, data_length,
668 &slot->data.ecp );
669 if( status != PSA_SUCCESS )
670 return( status );
671 }
672 else
673#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100674#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100675 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
676 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 {
678 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100679 mbedtls_pk_context pk;
680 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200681
682 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100683 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100684 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
685 else
686 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687 if( ret != 0 )
688 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200689
690 /* We have something that the pkparse module recognizes.
691 * If it has the expected type and passes any type-specific
692 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100693#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100694 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200695 status = psa_import_rsa_key( &pk, &slot->data.rsa );
696 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100697#endif /* MBEDTLS_RSA_C */
698#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100699 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
700 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200701 &pk, &slot->data.ecp );
702 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100703#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200704 {
705 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200706 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200707
Gilles Peskinec648d692018-06-28 08:46:13 +0200708 /* Free the content of the pk object only on error. On success,
709 * the content of the object has been stored in the slot. */
710 if( status != PSA_SUCCESS )
711 {
712 mbedtls_pk_free( &pk );
713 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100714 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 }
716 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100717#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100718 {
719 return( PSA_ERROR_NOT_SUPPORTED );
720 }
Darryl Green940d72c2018-07-13 13:18:51 +0100721 return( PSA_SUCCESS );
722}
723
Darryl Greend49a4992018-06-18 17:27:26 +0100724#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine69f976b2018-11-30 18:46:56 +0100725static psa_status_t psa_load_persistent_key_into_slot( key_slot_t *p_slot )
Darryl Greend49a4992018-06-18 17:27:26 +0100726{
727 psa_status_t status = PSA_SUCCESS;
728 uint8_t *key_data = NULL;
729 size_t key_data_length = 0;
730
Gilles Peskine69f976b2018-11-30 18:46:56 +0100731 status = psa_load_persistent_key( p_slot->persistent_storage_id,
732 &( p_slot )->type,
Darryl Greend49a4992018-06-18 17:27:26 +0100733 &( p_slot )->policy, &key_data,
734 &key_data_length );
735 if( status != PSA_SUCCESS )
736 goto exit;
737 status = psa_import_key_into_slot( p_slot,
738 key_data, key_data_length );
739exit:
740 psa_free_persistent_key_data( key_data, key_data_length );
741 return( status );
742}
743#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
744
Darryl Green06fd18d2018-07-16 11:21:11 +0100745/* Retrieve a key slot, occupied or not. */
Gilles Peskine961849f2018-11-30 18:54:54 +0100746static psa_status_t psa_get_key_slot( psa_key_slot_t key_or_handle,
Darryl Green06fd18d2018-07-16 11:21:11 +0100747 key_slot_t **p_slot )
748{
Gilles Peskine961849f2018-11-30 18:54:54 +0100749 psa_key_slot_t key = key_or_handle & ~PSA_KEY_HANDLE_ALLOCATED_FLAG;
750 int is_handle = ( key_or_handle & PSA_KEY_HANDLE_ALLOCATED_FLAG ) != 0;
751 psa_status_t error_if_invalid =
752 ( is_handle ?
753 PSA_ERROR_INVALID_HANDLE :
754 PSA_ERROR_INVALID_ARGUMENT );
755
Darryl Green06fd18d2018-07-16 11:21:11 +0100756 GUARD_MODULE_INITIALIZED;
757
758 /* 0 is not a valid slot number under any circumstance. This
759 * implementation provides slots number 1 to N where N is the
760 * number of available slots. */
761 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskine961849f2018-11-30 18:54:54 +0100762 return( error_if_invalid );
Darryl Green06fd18d2018-07-16 11:21:11 +0100763
764 *p_slot = &global_data.key_slots[key - 1];
Darryl Greend49a4992018-06-18 17:27:26 +0100765
Gilles Peskine961849f2018-11-30 18:54:54 +0100766 /* Allocated slots must only be accessed via a handle.
767 * Unallocated slots must only be accessed directly. */
768 if( ( *p_slot )->allocated != is_handle )
769 return( error_if_invalid );
770
Darryl Greend49a4992018-06-18 17:27:26 +0100771#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100772 if( ! ( *p_slot )->allocated &&
773 ( *p_slot )->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Darryl Greend49a4992018-06-18 17:27:26 +0100774 {
775 /* There are two circumstances this can occur: the key material has
776 * not yet been created, or the key exists in storage but has not yet
777 * been loaded into memory. */
778 if( ( *p_slot )->type == PSA_KEY_TYPE_NONE )
779 {
780 psa_status_t status = PSA_SUCCESS;
Gilles Peskine69f976b2018-11-30 18:46:56 +0100781 status = psa_load_persistent_key_into_slot( *p_slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100782 if( status != PSA_ERROR_EMPTY_SLOT )
783 return( status );
784 }
785 }
786#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
787
Darryl Green06fd18d2018-07-16 11:21:11 +0100788 return( PSA_SUCCESS );
789}
790
791/* Retrieve an empty key slot (slot with no key data, but possibly
792 * with some metadata such as a policy). */
793static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
794 key_slot_t **p_slot )
795{
796 psa_status_t status;
797 key_slot_t *slot = NULL;
798
799 *p_slot = NULL;
800
801 status = psa_get_key_slot( key, &slot );
802 if( status != PSA_SUCCESS )
803 return( status );
804
805 if( slot->type != PSA_KEY_TYPE_NONE )
806 return( PSA_ERROR_OCCUPIED_SLOT );
807
808 *p_slot = slot;
809 return( status );
810}
811
812/** Retrieve a slot which must contain a key. The key must have allow all the
813 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
814 * operations with this algorithm. */
815static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
816 key_slot_t **p_slot,
817 psa_key_usage_t usage,
818 psa_algorithm_t alg )
819{
820 psa_status_t status;
821 key_slot_t *slot = NULL;
822
823 *p_slot = NULL;
824
825 status = psa_get_key_slot( key, &slot );
826 if( status != PSA_SUCCESS )
827 return( status );
828 if( slot->type == PSA_KEY_TYPE_NONE )
829 return( PSA_ERROR_EMPTY_SLOT );
830
831 /* Enforce that usage policy for the key slot contains all the flags
832 * required by the usage parameter. There is one exception: public
833 * keys can always be exported, so we treat public key objects as
834 * if they had the export flag. */
835 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
836 usage &= ~PSA_KEY_USAGE_EXPORT;
837 if( ( slot->policy.usage & usage ) != usage )
838 return( PSA_ERROR_NOT_PERMITTED );
839 if( alg != 0 && ( alg != slot->policy.alg ) )
840 return( PSA_ERROR_NOT_PERMITTED );
841
842 *p_slot = slot;
843 return( PSA_SUCCESS );
844}
Darryl Green940d72c2018-07-13 13:18:51 +0100845
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100846/** Wipe key data from a slot. Preserve metadata such as the policy. */
Darryl Green40225ba2018-11-15 14:48:15 +0000847static psa_status_t psa_remove_key_data_from_memory( key_slot_t *slot )
848{
849 if( slot->type == PSA_KEY_TYPE_NONE )
850 {
851 /* No key material to clean. */
852 }
853 else if( key_type_is_raw_bytes( slot->type ) )
854 {
855 mbedtls_free( slot->data.raw.data );
856 }
857 else
858#if defined(MBEDTLS_RSA_C)
859 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
860 {
861 mbedtls_rsa_free( slot->data.rsa );
862 mbedtls_free( slot->data.rsa );
863 }
864 else
865#endif /* defined(MBEDTLS_RSA_C) */
866#if defined(MBEDTLS_ECP_C)
867 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
868 {
869 mbedtls_ecp_keypair_free( slot->data.ecp );
870 mbedtls_free( slot->data.ecp );
871 }
872 else
873#endif /* defined(MBEDTLS_ECP_C) */
874 {
875 /* Shouldn't happen: the key type is not any type that we
876 * put in. */
877 return( PSA_ERROR_TAMPERING_DETECTED );
878 }
879
880 return( PSA_SUCCESS );
881}
882
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100883/** Completely wipe a slot in memory, including its policy.
884 * Persistent storage is not affected. */
885static psa_status_t psa_wipe_key_slot( key_slot_t *slot )
886{
887 psa_status_t status = psa_remove_key_data_from_memory( slot );
888 /* At this point, key material and other type-specific content has
889 * been wiped. Clear remaining metadata. We can call memset and not
890 * zeroize because the metadata is not particularly sensitive. */
891 memset( slot, 0, sizeof( *slot ) );
892 return( status );
893}
894
Gilles Peskine961849f2018-11-30 18:54:54 +0100895/* A slot is available if nothing has been set in it: default lifetime
896 * and policy, no key type. */
897static int psa_internal_is_slot_available( key_slot_t *slot )
898{
899 if( slot->allocated )
900 return( 0 );
901 if( slot->type != PSA_KEY_TYPE_NONE )
902 return( 0 );
903 if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE )
904 return( 0 );
905 if( slot->policy.usage != 0 || slot->policy.alg != 0 )
906 return( 0 );
907 return( 1 );
908}
909
910psa_status_t psa_internal_allocate_key_slot( psa_key_handle_t *handle )
911{
912 psa_key_slot_t key;
913 for( key = PSA_KEY_SLOT_COUNT; key != 0; --( key ) )
914 {
915 key_slot_t *slot = &global_data.key_slots[key - 1];
916 if( psa_internal_is_slot_available( slot ) )
917 {
918 slot->allocated = 1;
919 *handle = key | PSA_KEY_HANDLE_ALLOCATED_FLAG;
920 return( PSA_SUCCESS );
921 }
922 }
923 return( PSA_ERROR_INSUFFICIENT_MEMORY );
924}
925
926psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
927 psa_key_id_t id )
928{
929 key_slot_t *slot;
930 psa_status_t status;
931
932 /* Reject id=0 because by general library conventions, 0 is an invalid
933 * value wherever possible. */
934 if( id == 0 )
935 return( PSA_ERROR_INVALID_ARGUMENT );
936 /* Reject high values because the file names are reserved for the
937 * library's internal use. */
938 if( id >= 0xffff0000 )
939 return( PSA_ERROR_INVALID_ARGUMENT );
940 /* Reject values that don't fit in the key slot number type.
941 * This is a temporary limitation due to the library's internal
942 * plumbing. */
943 if( id > (psa_key_slot_t)( -1 ) )
944 return( PSA_ERROR_INVALID_ARGUMENT );
945
946 status = psa_get_key_slot( handle, &slot );
947 if( status != PSA_SUCCESS )
948 return( status );
949
950 slot->lifetime = PSA_KEY_LIFETIME_PERSISTENT;
951 slot->persistent_storage_id = id;
952 status = psa_load_persistent_key_into_slot( slot );
953
954 return( status );
955}
956
957psa_status_t psa_internal_release_key_slot( psa_key_handle_t handle )
958{
959 psa_key_slot_t key;
960 key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100961 /* Don't call psa_get_key_slot() so as not to trigger its automatic
962 * loading of persistent key data. */
963 if( ( handle & PSA_KEY_HANDLE_ALLOCATED_FLAG ) == 0 )
964 return( PSA_ERROR_INVALID_HANDLE );
965 key = handle & ~PSA_KEY_HANDLE_ALLOCATED_FLAG;
966 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
967 return( PSA_ERROR_INVALID_HANDLE );
968 slot = &global_data.key_slots[key - 1];
969 if( ! slot->allocated )
970 return( PSA_ERROR_INVALID_HANDLE );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100971 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100972}
973
Darryl Green940d72c2018-07-13 13:18:51 +0100974psa_status_t psa_import_key( psa_key_slot_t key,
975 psa_key_type_t type,
976 const uint8_t *data,
977 size_t data_length )
978{
979 key_slot_t *slot;
980 psa_status_t status;
981
982 status = psa_get_empty_key_slot( key, &slot );
983 if( status != PSA_SUCCESS )
984 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985
986 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100987
988 status = psa_import_key_into_slot( slot, data, data_length );
989 if( status != PSA_SUCCESS )
990 {
991 slot->type = PSA_KEY_TYPE_NONE;
992 return( status );
993 }
994
Darryl Greend49a4992018-06-18 17:27:26 +0100995#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
996 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
997 {
998 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100999 status = psa_save_persistent_key( slot->persistent_storage_id,
1000 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +01001001 data_length );
1002 if( status != PSA_SUCCESS )
1003 {
1004 (void) psa_remove_key_data_from_memory( slot );
1005 slot->type = PSA_KEY_TYPE_NONE;
1006 }
1007 }
1008#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1009
1010 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011}
1012
Gilles Peskine2d277862018-06-18 15:41:12 +02001013psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014{
1015 key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +01001016 psa_status_t status = PSA_SUCCESS;
1017 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001019 status = psa_get_key_slot( key, &slot );
1020 if( status != PSA_SUCCESS )
1021 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +01001022#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1023 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1024 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001025 storage_status =
1026 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +01001027 }
1028#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001029 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001030 if( status != PSA_SUCCESS )
1031 return( status );
1032 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033}
1034
Gilles Peskineb870b182018-07-06 16:02:09 +02001035/* Return the size of the key in the given slot, in bits. */
1036static size_t psa_get_key_bits( const key_slot_t *slot )
1037{
1038 if( key_type_is_raw_bytes( slot->type ) )
1039 return( slot->data.raw.bytes * 8 );
1040#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001041 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001042 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001043#endif /* defined(MBEDTLS_RSA_C) */
1044#if defined(MBEDTLS_ECP_C)
1045 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1046 return( slot->data.ecp->grp.pbits );
1047#endif /* defined(MBEDTLS_ECP_C) */
1048 /* Shouldn't happen except on an empty slot. */
1049 return( 0 );
1050}
1051
Gilles Peskine2d277862018-06-18 15:41:12 +02001052psa_status_t psa_get_key_information( psa_key_slot_t key,
1053 psa_key_type_t *type,
1054 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001055{
1056 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001057 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001058
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001059 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001060 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001061 if( bits != NULL )
1062 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001063 status = psa_get_key_slot( key, &slot );
1064 if( status != PSA_SUCCESS )
1065 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001066
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001067 if( slot->type == PSA_KEY_TYPE_NONE )
1068 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001069 if( type != NULL )
1070 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001071 if( bits != NULL )
1072 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001073 return( PSA_SUCCESS );
1074}
1075
Darryl Greendd8fb772018-11-07 16:00:44 +00001076static psa_status_t psa_internal_export_key( key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001077 uint8_t *data,
1078 size_t data_size,
1079 size_t *data_length,
1080 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001081{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001082 *data_length = 0;
1083
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001084 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001085 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001086
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001087 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001088 {
1089 if( slot->data.raw.bytes > data_size )
1090 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001091 if( data_size != 0 )
1092 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001093 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001094 memset( data + slot->data.raw.bytes, 0,
1095 data_size - slot->data.raw.bytes );
1096 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001097 *data_length = slot->data.raw.bytes;
1098 return( PSA_SUCCESS );
1099 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001100#if defined(MBEDTLS_ECP_C)
1101 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1102 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001103 psa_status_t status;
1104
Gilles Peskine188c71e2018-10-29 19:26:02 +01001105 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1106 if( bytes > data_size )
1107 return( PSA_ERROR_BUFFER_TOO_SMALL );
1108 status = mbedtls_to_psa_error(
1109 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1110 if( status != PSA_SUCCESS )
1111 return( status );
1112 memset( data + bytes, 0, data_size - bytes );
1113 *data_length = bytes;
1114 return( PSA_SUCCESS );
1115 }
1116#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001117 else
Moran Peker17e36e12018-05-02 12:55:20 +03001118 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001119#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001120 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001121 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001122 {
Moran Pekera998bc62018-04-16 18:16:20 +03001123 mbedtls_pk_context pk;
1124 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001125 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001126 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001127#if defined(MBEDTLS_RSA_C)
1128 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001129 pk.pk_info = &mbedtls_rsa_info;
1130 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001131#else
1132 return( PSA_ERROR_NOT_SUPPORTED );
1133#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001134 }
1135 else
1136 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001137#if defined(MBEDTLS_ECP_C)
1138 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001139 pk.pk_info = &mbedtls_eckey_info;
1140 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001141#else
1142 return( PSA_ERROR_NOT_SUPPORTED );
1143#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001144 }
Moran Pekerd7326592018-05-29 16:56:39 +03001145 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001146 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001147 else
1148 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001149 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001150 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001151 /* If data_size is 0 then data may be NULL and then the
1152 * call to memset would have undefined behavior. */
1153 if( data_size != 0 )
1154 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001155 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001156 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001157 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1158 * Move the data to the beginning and erase remaining data
1159 * at the original location. */
1160 if( 2 * (size_t) ret <= data_size )
1161 {
1162 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001163 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001164 }
1165 else if( (size_t) ret < data_size )
1166 {
1167 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001168 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001169 }
Moran Pekera998bc62018-04-16 18:16:20 +03001170 *data_length = ret;
1171 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001172 }
1173 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001174#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001175 {
1176 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001177 it is valid for a special-purpose implementation to omit
1178 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001179 return( PSA_ERROR_NOT_SUPPORTED );
1180 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001181 }
1182}
1183
Gilles Peskine2d277862018-06-18 15:41:12 +02001184psa_status_t psa_export_key( psa_key_slot_t key,
1185 uint8_t *data,
1186 size_t data_size,
1187 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001188{
Darryl Greendd8fb772018-11-07 16:00:44 +00001189 key_slot_t *slot;
1190 psa_status_t status;
1191
1192 /* Set the key to empty now, so that even when there are errors, we always
1193 * set data_length to a value between 0 and data_size. On error, setting
1194 * the key to empty is a good choice because an empty key representation is
1195 * unlikely to be accepted anywhere. */
1196 *data_length = 0;
1197
1198 /* Export requires the EXPORT flag. There is an exception for public keys,
1199 * which don't require any flag, but psa_get_key_from_slot takes
1200 * care of this. */
1201 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
1202 if( status != PSA_SUCCESS )
1203 return( status );
1204 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001205 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001206}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001207
Gilles Peskine2d277862018-06-18 15:41:12 +02001208psa_status_t psa_export_public_key( psa_key_slot_t key,
1209 uint8_t *data,
1210 size_t data_size,
1211 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001212{
Darryl Greendd8fb772018-11-07 16:00:44 +00001213 key_slot_t *slot;
1214 psa_status_t status;
1215
1216 /* Set the key to empty now, so that even when there are errors, we always
1217 * set data_length to a value between 0 and data_size. On error, setting
1218 * the key to empty is a good choice because an empty key representation is
1219 * unlikely to be accepted anywhere. */
1220 *data_length = 0;
1221
1222 /* Exporting a public key doesn't require a usage flag. */
1223 status = psa_get_key_from_slot( key, &slot, 0, 0 );
1224 if( status != PSA_SUCCESS )
1225 return( status );
1226 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001227 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001228}
1229
Darryl Green0c6575a2018-11-07 16:05:30 +00001230#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine69f976b2018-11-30 18:46:56 +01001231static psa_status_t psa_save_generated_persistent_key( key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001232 size_t bits )
1233{
1234 psa_status_t status;
1235 uint8_t *data;
1236 size_t key_length;
1237 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1238 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001239 if( data == NULL )
1240 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001241 /* Get key data in export format */
1242 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1243 if( status != PSA_SUCCESS )
1244 {
1245 slot->type = PSA_KEY_TYPE_NONE;
1246 goto exit;
1247 }
1248 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001249 status = psa_save_persistent_key( slot->persistent_storage_id,
1250 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001251 data, key_length );
1252 if( status != PSA_SUCCESS )
1253 {
1254 slot->type = PSA_KEY_TYPE_NONE;
1255 }
1256exit:
1257 mbedtls_zeroize( data, key_length );
1258 mbedtls_free( data );
1259 return( status );
1260}
1261#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1262
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001263
1264
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001265/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001266/* Message digests */
1267/****************************************************************/
1268
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001269static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001270{
1271 switch( alg )
1272 {
1273#if defined(MBEDTLS_MD2_C)
1274 case PSA_ALG_MD2:
1275 return( &mbedtls_md2_info );
1276#endif
1277#if defined(MBEDTLS_MD4_C)
1278 case PSA_ALG_MD4:
1279 return( &mbedtls_md4_info );
1280#endif
1281#if defined(MBEDTLS_MD5_C)
1282 case PSA_ALG_MD5:
1283 return( &mbedtls_md5_info );
1284#endif
1285#if defined(MBEDTLS_RIPEMD160_C)
1286 case PSA_ALG_RIPEMD160:
1287 return( &mbedtls_ripemd160_info );
1288#endif
1289#if defined(MBEDTLS_SHA1_C)
1290 case PSA_ALG_SHA_1:
1291 return( &mbedtls_sha1_info );
1292#endif
1293#if defined(MBEDTLS_SHA256_C)
1294 case PSA_ALG_SHA_224:
1295 return( &mbedtls_sha224_info );
1296 case PSA_ALG_SHA_256:
1297 return( &mbedtls_sha256_info );
1298#endif
1299#if defined(MBEDTLS_SHA512_C)
1300 case PSA_ALG_SHA_384:
1301 return( &mbedtls_sha384_info );
1302 case PSA_ALG_SHA_512:
1303 return( &mbedtls_sha512_info );
1304#endif
1305 default:
1306 return( NULL );
1307 }
1308}
1309
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001310psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1311{
1312 switch( operation->alg )
1313 {
Gilles Peskine81736312018-06-26 15:04:31 +02001314 case 0:
1315 /* The object has (apparently) been initialized but it is not
1316 * in use. It's ok to call abort on such an object, and there's
1317 * nothing to do. */
1318 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001319#if defined(MBEDTLS_MD2_C)
1320 case PSA_ALG_MD2:
1321 mbedtls_md2_free( &operation->ctx.md2 );
1322 break;
1323#endif
1324#if defined(MBEDTLS_MD4_C)
1325 case PSA_ALG_MD4:
1326 mbedtls_md4_free( &operation->ctx.md4 );
1327 break;
1328#endif
1329#if defined(MBEDTLS_MD5_C)
1330 case PSA_ALG_MD5:
1331 mbedtls_md5_free( &operation->ctx.md5 );
1332 break;
1333#endif
1334#if defined(MBEDTLS_RIPEMD160_C)
1335 case PSA_ALG_RIPEMD160:
1336 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1337 break;
1338#endif
1339#if defined(MBEDTLS_SHA1_C)
1340 case PSA_ALG_SHA_1:
1341 mbedtls_sha1_free( &operation->ctx.sha1 );
1342 break;
1343#endif
1344#if defined(MBEDTLS_SHA256_C)
1345 case PSA_ALG_SHA_224:
1346 case PSA_ALG_SHA_256:
1347 mbedtls_sha256_free( &operation->ctx.sha256 );
1348 break;
1349#endif
1350#if defined(MBEDTLS_SHA512_C)
1351 case PSA_ALG_SHA_384:
1352 case PSA_ALG_SHA_512:
1353 mbedtls_sha512_free( &operation->ctx.sha512 );
1354 break;
1355#endif
1356 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001357 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001358 }
1359 operation->alg = 0;
1360 return( PSA_SUCCESS );
1361}
1362
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001363psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001364 psa_algorithm_t alg )
1365{
1366 int ret;
1367 operation->alg = 0;
1368 switch( alg )
1369 {
1370#if defined(MBEDTLS_MD2_C)
1371 case PSA_ALG_MD2:
1372 mbedtls_md2_init( &operation->ctx.md2 );
1373 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1374 break;
1375#endif
1376#if defined(MBEDTLS_MD4_C)
1377 case PSA_ALG_MD4:
1378 mbedtls_md4_init( &operation->ctx.md4 );
1379 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1380 break;
1381#endif
1382#if defined(MBEDTLS_MD5_C)
1383 case PSA_ALG_MD5:
1384 mbedtls_md5_init( &operation->ctx.md5 );
1385 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1386 break;
1387#endif
1388#if defined(MBEDTLS_RIPEMD160_C)
1389 case PSA_ALG_RIPEMD160:
1390 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1391 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1392 break;
1393#endif
1394#if defined(MBEDTLS_SHA1_C)
1395 case PSA_ALG_SHA_1:
1396 mbedtls_sha1_init( &operation->ctx.sha1 );
1397 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1398 break;
1399#endif
1400#if defined(MBEDTLS_SHA256_C)
1401 case PSA_ALG_SHA_224:
1402 mbedtls_sha256_init( &operation->ctx.sha256 );
1403 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1404 break;
1405 case PSA_ALG_SHA_256:
1406 mbedtls_sha256_init( &operation->ctx.sha256 );
1407 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1408 break;
1409#endif
1410#if defined(MBEDTLS_SHA512_C)
1411 case PSA_ALG_SHA_384:
1412 mbedtls_sha512_init( &operation->ctx.sha512 );
1413 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1414 break;
1415 case PSA_ALG_SHA_512:
1416 mbedtls_sha512_init( &operation->ctx.sha512 );
1417 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1418 break;
1419#endif
1420 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001421 return( PSA_ALG_IS_HASH( alg ) ?
1422 PSA_ERROR_NOT_SUPPORTED :
1423 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001424 }
1425 if( ret == 0 )
1426 operation->alg = alg;
1427 else
1428 psa_hash_abort( operation );
1429 return( mbedtls_to_psa_error( ret ) );
1430}
1431
1432psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1433 const uint8_t *input,
1434 size_t input_length )
1435{
1436 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001437
1438 /* Don't require hash implementations to behave correctly on a
1439 * zero-length input, which may have an invalid pointer. */
1440 if( input_length == 0 )
1441 return( PSA_SUCCESS );
1442
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001443 switch( operation->alg )
1444 {
1445#if defined(MBEDTLS_MD2_C)
1446 case PSA_ALG_MD2:
1447 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1448 input, input_length );
1449 break;
1450#endif
1451#if defined(MBEDTLS_MD4_C)
1452 case PSA_ALG_MD4:
1453 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1454 input, input_length );
1455 break;
1456#endif
1457#if defined(MBEDTLS_MD5_C)
1458 case PSA_ALG_MD5:
1459 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1460 input, input_length );
1461 break;
1462#endif
1463#if defined(MBEDTLS_RIPEMD160_C)
1464 case PSA_ALG_RIPEMD160:
1465 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1466 input, input_length );
1467 break;
1468#endif
1469#if defined(MBEDTLS_SHA1_C)
1470 case PSA_ALG_SHA_1:
1471 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1472 input, input_length );
1473 break;
1474#endif
1475#if defined(MBEDTLS_SHA256_C)
1476 case PSA_ALG_SHA_224:
1477 case PSA_ALG_SHA_256:
1478 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1479 input, input_length );
1480 break;
1481#endif
1482#if defined(MBEDTLS_SHA512_C)
1483 case PSA_ALG_SHA_384:
1484 case PSA_ALG_SHA_512:
1485 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1486 input, input_length );
1487 break;
1488#endif
1489 default:
1490 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1491 break;
1492 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001493
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001494 if( ret != 0 )
1495 psa_hash_abort( operation );
1496 return( mbedtls_to_psa_error( ret ) );
1497}
1498
1499psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1500 uint8_t *hash,
1501 size_t hash_size,
1502 size_t *hash_length )
1503{
itayzafrir40835d42018-08-02 13:14:17 +03001504 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001505 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001506 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001507
1508 /* Fill the output buffer with something that isn't a valid hash
1509 * (barring an attack on the hash and deliberately-crafted input),
1510 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001511 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001512 /* If hash_size is 0 then hash may be NULL and then the
1513 * call to memset would have undefined behavior. */
1514 if( hash_size != 0 )
1515 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001516
1517 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001518 {
1519 status = PSA_ERROR_BUFFER_TOO_SMALL;
1520 goto exit;
1521 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001522
1523 switch( operation->alg )
1524 {
1525#if defined(MBEDTLS_MD2_C)
1526 case PSA_ALG_MD2:
1527 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1528 break;
1529#endif
1530#if defined(MBEDTLS_MD4_C)
1531 case PSA_ALG_MD4:
1532 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1533 break;
1534#endif
1535#if defined(MBEDTLS_MD5_C)
1536 case PSA_ALG_MD5:
1537 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1538 break;
1539#endif
1540#if defined(MBEDTLS_RIPEMD160_C)
1541 case PSA_ALG_RIPEMD160:
1542 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1543 break;
1544#endif
1545#if defined(MBEDTLS_SHA1_C)
1546 case PSA_ALG_SHA_1:
1547 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1548 break;
1549#endif
1550#if defined(MBEDTLS_SHA256_C)
1551 case PSA_ALG_SHA_224:
1552 case PSA_ALG_SHA_256:
1553 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1554 break;
1555#endif
1556#if defined(MBEDTLS_SHA512_C)
1557 case PSA_ALG_SHA_384:
1558 case PSA_ALG_SHA_512:
1559 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1560 break;
1561#endif
1562 default:
1563 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1564 break;
1565 }
itayzafrir40835d42018-08-02 13:14:17 +03001566 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001567
itayzafrir40835d42018-08-02 13:14:17 +03001568exit:
1569 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001570 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001571 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001572 return( psa_hash_abort( operation ) );
1573 }
1574 else
1575 {
1576 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001577 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001578 }
1579}
1580
Gilles Peskine2d277862018-06-18 15:41:12 +02001581psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1582 const uint8_t *hash,
1583 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001584{
1585 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1586 size_t actual_hash_length;
1587 psa_status_t status = psa_hash_finish( operation,
1588 actual_hash, sizeof( actual_hash ),
1589 &actual_hash_length );
1590 if( status != PSA_SUCCESS )
1591 return( status );
1592 if( actual_hash_length != hash_length )
1593 return( PSA_ERROR_INVALID_SIGNATURE );
1594 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1595 return( PSA_ERROR_INVALID_SIGNATURE );
1596 return( PSA_SUCCESS );
1597}
1598
1599
1600
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001601/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001602/* MAC */
1603/****************************************************************/
1604
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001605static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001606 psa_algorithm_t alg,
1607 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001608 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001609 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001610{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001611 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001612 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001613
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001614 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001615 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001616
Gilles Peskine8c9def32018-02-08 10:02:12 +01001617 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1618 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001619 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001620 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001621 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001622 mode = MBEDTLS_MODE_STREAM;
1623 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624 case PSA_ALG_CTR:
1625 mode = MBEDTLS_MODE_CTR;
1626 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001627 case PSA_ALG_CFB:
1628 mode = MBEDTLS_MODE_CFB;
1629 break;
1630 case PSA_ALG_OFB:
1631 mode = MBEDTLS_MODE_OFB;
1632 break;
1633 case PSA_ALG_CBC_NO_PADDING:
1634 mode = MBEDTLS_MODE_CBC;
1635 break;
1636 case PSA_ALG_CBC_PKCS7:
1637 mode = MBEDTLS_MODE_CBC;
1638 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001639 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001640 mode = MBEDTLS_MODE_CCM;
1641 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001642 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001643 mode = MBEDTLS_MODE_GCM;
1644 break;
1645 default:
1646 return( NULL );
1647 }
1648 }
1649 else if( alg == PSA_ALG_CMAC )
1650 mode = MBEDTLS_MODE_ECB;
1651 else if( alg == PSA_ALG_GMAC )
1652 mode = MBEDTLS_MODE_GCM;
1653 else
1654 return( NULL );
1655
1656 switch( key_type )
1657 {
1658 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001659 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001660 break;
1661 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001662 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1663 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001664 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001665 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001666 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001667 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001668 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1669 * but two-key Triple-DES is functionally three-key Triple-DES
1670 * with K1=K3, so that's how we present it to mbedtls. */
1671 if( key_bits == 128 )
1672 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001673 break;
1674 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001675 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001676 break;
1677 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001678 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001679 break;
1680 default:
1681 return( NULL );
1682 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001683 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001684 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001685
Jaeden Amero23bbb752018-06-26 14:16:54 +01001686 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1687 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001688}
1689
Gilles Peskinea05219c2018-11-16 16:02:56 +01001690#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001691static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001692{
Gilles Peskine2d277862018-06-18 15:41:12 +02001693 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001694 {
1695 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001696 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001697 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001698 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001699 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001700 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001701 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001702 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001703 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001704 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001705 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001706 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001707 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001708 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001709 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001710 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001711 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001712 return( 128 );
1713 default:
1714 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001715 }
1716}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001717#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001718
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001719/* Initialize the MAC operation structure. Once this function has been
1720 * called, psa_mac_abort can run and will do the right thing. */
1721static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1722 psa_algorithm_t alg )
1723{
1724 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1725
1726 operation->alg = alg;
1727 operation->key_set = 0;
1728 operation->iv_set = 0;
1729 operation->iv_required = 0;
1730 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001731 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001732
1733#if defined(MBEDTLS_CMAC_C)
1734 if( alg == PSA_ALG_CMAC )
1735 {
1736 operation->iv_required = 0;
1737 mbedtls_cipher_init( &operation->ctx.cmac );
1738 status = PSA_SUCCESS;
1739 }
1740 else
1741#endif /* MBEDTLS_CMAC_C */
1742#if defined(MBEDTLS_MD_C)
1743 if( PSA_ALG_IS_HMAC( operation->alg ) )
1744 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001745 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1746 operation->ctx.hmac.hash_ctx.alg = 0;
1747 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001748 }
1749 else
1750#endif /* MBEDTLS_MD_C */
1751 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001752 if( ! PSA_ALG_IS_MAC( alg ) )
1753 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001754 }
1755
1756 if( status != PSA_SUCCESS )
1757 memset( operation, 0, sizeof( *operation ) );
1758 return( status );
1759}
1760
Gilles Peskine01126fa2018-07-12 17:04:55 +02001761#if defined(MBEDTLS_MD_C)
1762static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1763{
1764 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1765 return( psa_hash_abort( &hmac->hash_ctx ) );
1766}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001767
1768static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1769{
1770 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1771 memset( hmac, 0, sizeof( *hmac ) );
1772}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001773#endif /* MBEDTLS_MD_C */
1774
Gilles Peskine8c9def32018-02-08 10:02:12 +01001775psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1776{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001777 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001778 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001779 /* The object has (apparently) been initialized but it is not
1780 * in use. It's ok to call abort on such an object, and there's
1781 * nothing to do. */
1782 return( PSA_SUCCESS );
1783 }
1784 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001785#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001786 if( operation->alg == PSA_ALG_CMAC )
1787 {
1788 mbedtls_cipher_free( &operation->ctx.cmac );
1789 }
1790 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001791#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001792#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001793 if( PSA_ALG_IS_HMAC( operation->alg ) )
1794 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001795 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001796 }
1797 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001798#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001799 {
1800 /* Sanity check (shouldn't happen: operation->alg should
1801 * always have been initialized to a valid value). */
1802 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001803 }
Moran Peker41deec42018-04-04 15:43:05 +03001804
Gilles Peskine8c9def32018-02-08 10:02:12 +01001805 operation->alg = 0;
1806 operation->key_set = 0;
1807 operation->iv_set = 0;
1808 operation->iv_required = 0;
1809 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001810 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001811
Gilles Peskine8c9def32018-02-08 10:02:12 +01001812 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001813
1814bad_state:
1815 /* If abort is called on an uninitialized object, we can't trust
1816 * anything. Wipe the object in case it contains confidential data.
1817 * This may result in a memory leak if a pointer gets overwritten,
1818 * but it's too late to do anything about this. */
1819 memset( operation, 0, sizeof( *operation ) );
1820 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001821}
1822
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001823#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001824static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001825 size_t key_bits,
1826 key_slot_t *slot,
1827 const mbedtls_cipher_info_t *cipher_info )
1828{
1829 int ret;
1830
1831 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001832
1833 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1834 if( ret != 0 )
1835 return( ret );
1836
1837 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1838 slot->data.raw.data,
1839 key_bits );
1840 return( ret );
1841}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001842#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001843
Gilles Peskine248051a2018-06-20 16:09:38 +02001844#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001845static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1846 const uint8_t *key,
1847 size_t key_length,
1848 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001849{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001850 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001851 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001852 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001853 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001854 psa_status_t status;
1855
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001856 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1857 * overflow below. This should never trigger if the hash algorithm
1858 * is implemented correctly. */
1859 /* The size checks against the ipad and opad buffers cannot be written
1860 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1861 * because that triggers -Wlogical-op on GCC 7.3. */
1862 if( block_size > sizeof( ipad ) )
1863 return( PSA_ERROR_NOT_SUPPORTED );
1864 if( block_size > sizeof( hmac->opad ) )
1865 return( PSA_ERROR_NOT_SUPPORTED );
1866 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001867 return( PSA_ERROR_NOT_SUPPORTED );
1868
Gilles Peskined223b522018-06-11 18:12:58 +02001869 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001870 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001871 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001872 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001873 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001874 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001875 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001876 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001877 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001878 ipad, sizeof( ipad ), &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 Peskine7e454bc2018-06-11 17:26:17 +02001881 }
Gilles Peskine96889972018-07-12 17:07:03 +02001882 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1883 * but it is permitted. It is common when HMAC is used in HKDF, for
1884 * example. Don't call `memcpy` in the 0-length because `key` could be
1885 * an invalid pointer which would make the behavior undefined. */
1886 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001887 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001888
Gilles Peskined223b522018-06-11 18:12:58 +02001889 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1890 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001891 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001892 ipad[i] ^= 0x36;
1893 memset( ipad + key_length, 0x36, block_size - key_length );
1894
1895 /* Copy the key material from ipad to opad, flipping the requisite bits,
1896 * and filling the rest of opad with the requisite constant. */
1897 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001898 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1899 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001900
Gilles Peskine01126fa2018-07-12 17:04:55 +02001901 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001902 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001903 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001904
Gilles Peskine01126fa2018-07-12 17:04:55 +02001905 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001906
1907cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001908 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001909
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001910 return( status );
1911}
Gilles Peskine248051a2018-06-20 16:09:38 +02001912#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001913
Gilles Peskine89167cb2018-07-08 20:12:23 +02001914static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1915 psa_key_slot_t key,
1916 psa_algorithm_t alg,
1917 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001918{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001919 psa_status_t status;
1920 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001921 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001922 psa_key_usage_t usage =
1923 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001924 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001925 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001926
Gilles Peskined911eb72018-08-14 15:18:45 +02001927 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001928 if( status != PSA_SUCCESS )
1929 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001930 if( is_sign )
1931 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001932
Gilles Peskine89167cb2018-07-08 20:12:23 +02001933 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001934 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001935 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001936 key_bits = psa_get_key_bits( slot );
1937
Gilles Peskine8c9def32018-02-08 10:02:12 +01001938#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001939 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001940 {
1941 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001942 mbedtls_cipher_info_from_psa( full_length_alg,
1943 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001944 int ret;
1945 if( cipher_info == NULL )
1946 {
1947 status = PSA_ERROR_NOT_SUPPORTED;
1948 goto exit;
1949 }
1950 operation->mac_size = cipher_info->block_size;
1951 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1952 status = mbedtls_to_psa_error( ret );
1953 }
1954 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001955#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001956#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001957 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001958 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001959 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001960 if( hash_alg == 0 )
1961 {
1962 status = PSA_ERROR_NOT_SUPPORTED;
1963 goto exit;
1964 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001965
1966 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1967 /* Sanity check. This shouldn't fail on a valid configuration. */
1968 if( operation->mac_size == 0 ||
1969 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1970 {
1971 status = PSA_ERROR_NOT_SUPPORTED;
1972 goto exit;
1973 }
1974
Gilles Peskine01126fa2018-07-12 17:04:55 +02001975 if( slot->type != PSA_KEY_TYPE_HMAC )
1976 {
1977 status = PSA_ERROR_INVALID_ARGUMENT;
1978 goto exit;
1979 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001980
Gilles Peskine01126fa2018-07-12 17:04:55 +02001981 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1982 slot->data.raw.data,
1983 slot->data.raw.bytes,
1984 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001985 }
1986 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001987#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001988 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001989 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001990 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001991 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001992
Gilles Peskined911eb72018-08-14 15:18:45 +02001993 if( truncated == 0 )
1994 {
1995 /* The "normal" case: untruncated algorithm. Nothing to do. */
1996 }
1997 else if( truncated < 4 )
1998 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001999 /* A very short MAC is too short for security since it can be
2000 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2001 * so we make this our minimum, even though 32 bits is still
2002 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002003 status = PSA_ERROR_NOT_SUPPORTED;
2004 }
2005 else if( truncated > operation->mac_size )
2006 {
2007 /* It's impossible to "truncate" to a larger length. */
2008 status = PSA_ERROR_INVALID_ARGUMENT;
2009 }
2010 else
2011 operation->mac_size = truncated;
2012
Gilles Peskinefbfac682018-07-08 20:51:54 +02002013exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002014 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002015 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002016 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002017 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002018 else
2019 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002020 operation->key_set = 1;
2021 }
2022 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002023}
2024
Gilles Peskine89167cb2018-07-08 20:12:23 +02002025psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
2026 psa_key_slot_t key,
2027 psa_algorithm_t alg )
2028{
2029 return( psa_mac_setup( operation, key, alg, 1 ) );
2030}
2031
2032psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
2033 psa_key_slot_t key,
2034 psa_algorithm_t alg )
2035{
2036 return( psa_mac_setup( operation, key, alg, 0 ) );
2037}
2038
Gilles Peskine8c9def32018-02-08 10:02:12 +01002039psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2040 const uint8_t *input,
2041 size_t input_length )
2042{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002043 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002044 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002045 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002046 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002047 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002048 operation->has_input = 1;
2049
Gilles Peskine8c9def32018-02-08 10:02:12 +01002050#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002051 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002052 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002053 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2054 input, input_length );
2055 status = mbedtls_to_psa_error( ret );
2056 }
2057 else
2058#endif /* MBEDTLS_CMAC_C */
2059#if defined(MBEDTLS_MD_C)
2060 if( PSA_ALG_IS_HMAC( operation->alg ) )
2061 {
2062 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2063 input_length );
2064 }
2065 else
2066#endif /* MBEDTLS_MD_C */
2067 {
2068 /* This shouldn't happen if `operation` was initialized by
2069 * a setup function. */
2070 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002071 }
2072
Gilles Peskinefbfac682018-07-08 20:51:54 +02002073cleanup:
2074 if( status != PSA_SUCCESS )
2075 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002076 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002077}
2078
Gilles Peskine01126fa2018-07-12 17:04:55 +02002079#if defined(MBEDTLS_MD_C)
2080static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2081 uint8_t *mac,
2082 size_t mac_size )
2083{
2084 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2085 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2086 size_t hash_size = 0;
2087 size_t block_size = psa_get_hash_block_size( hash_alg );
2088 psa_status_t status;
2089
Gilles Peskine01126fa2018-07-12 17:04:55 +02002090 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2091 if( status != PSA_SUCCESS )
2092 return( status );
2093 /* From here on, tmp needs to be wiped. */
2094
2095 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2096 if( status != PSA_SUCCESS )
2097 goto exit;
2098
2099 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2100 if( status != PSA_SUCCESS )
2101 goto exit;
2102
2103 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2104 if( status != PSA_SUCCESS )
2105 goto exit;
2106
Gilles Peskined911eb72018-08-14 15:18:45 +02002107 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2108 if( status != PSA_SUCCESS )
2109 goto exit;
2110
2111 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002112
2113exit:
2114 mbedtls_zeroize( tmp, hash_size );
2115 return( status );
2116}
2117#endif /* MBEDTLS_MD_C */
2118
mohammad16036df908f2018-04-02 08:34:15 -07002119static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002120 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002121 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002122{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002123 if( ! operation->key_set )
2124 return( PSA_ERROR_BAD_STATE );
2125 if( operation->iv_required && ! operation->iv_set )
2126 return( PSA_ERROR_BAD_STATE );
2127
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128 if( mac_size < operation->mac_size )
2129 return( PSA_ERROR_BUFFER_TOO_SMALL );
2130
Gilles Peskine8c9def32018-02-08 10:02:12 +01002131#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002132 if( operation->alg == PSA_ALG_CMAC )
2133 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002134 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2135 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2136 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002137 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02002138 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002139 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002140 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002141 else
2142#endif /* MBEDTLS_CMAC_C */
2143#if defined(MBEDTLS_MD_C)
2144 if( PSA_ALG_IS_HMAC( operation->alg ) )
2145 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002146 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002147 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002148 }
2149 else
2150#endif /* MBEDTLS_MD_C */
2151 {
2152 /* This shouldn't happen if `operation` was initialized by
2153 * a setup function. */
2154 return( PSA_ERROR_BAD_STATE );
2155 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002156}
2157
Gilles Peskineacd4be32018-07-08 19:56:25 +02002158psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2159 uint8_t *mac,
2160 size_t mac_size,
2161 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002162{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002163 psa_status_t status;
2164
2165 /* Fill the output buffer with something that isn't a valid mac
2166 * (barring an attack on the mac and deliberately-crafted input),
2167 * in case the caller doesn't check the return status properly. */
2168 *mac_length = mac_size;
2169 /* If mac_size is 0 then mac may be NULL and then the
2170 * call to memset would have undefined behavior. */
2171 if( mac_size != 0 )
2172 memset( mac, '!', mac_size );
2173
Gilles Peskine89167cb2018-07-08 20:12:23 +02002174 if( ! operation->is_sign )
2175 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002176 status = PSA_ERROR_BAD_STATE;
2177 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002178 }
mohammad16036df908f2018-04-02 08:34:15 -07002179
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002180 status = psa_mac_finish_internal( operation, mac, mac_size );
2181
2182cleanup:
2183 if( status == PSA_SUCCESS )
2184 {
2185 status = psa_mac_abort( operation );
2186 if( status == PSA_SUCCESS )
2187 *mac_length = operation->mac_size;
2188 else
2189 memset( mac, '!', mac_size );
2190 }
2191 else
2192 psa_mac_abort( operation );
2193 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002194}
2195
Gilles Peskineacd4be32018-07-08 19:56:25 +02002196psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2197 const uint8_t *mac,
2198 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002199{
Gilles Peskine828ed142018-06-18 23:25:51 +02002200 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002201 psa_status_t status;
2202
Gilles Peskine89167cb2018-07-08 20:12:23 +02002203 if( operation->is_sign )
2204 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002205 status = PSA_ERROR_BAD_STATE;
2206 goto cleanup;
2207 }
2208 if( operation->mac_size != mac_length )
2209 {
2210 status = PSA_ERROR_INVALID_SIGNATURE;
2211 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002212 }
mohammad16036df908f2018-04-02 08:34:15 -07002213
2214 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002215 actual_mac, sizeof( actual_mac ) );
2216
2217 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2218 status = PSA_ERROR_INVALID_SIGNATURE;
2219
2220cleanup:
2221 if( status == PSA_SUCCESS )
2222 status = psa_mac_abort( operation );
2223 else
2224 psa_mac_abort( operation );
2225
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002226 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002227
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002228 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002229}
2230
2231
Gilles Peskine20035e32018-02-03 22:44:14 +01002232
Gilles Peskine20035e32018-02-03 22:44:14 +01002233/****************************************************************/
2234/* Asymmetric cryptography */
2235/****************************************************************/
2236
Gilles Peskine2b450e32018-06-27 15:42:46 +02002237#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002238/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002239 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002240static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2241 size_t hash_length,
2242 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002243{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002244 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002245 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002246 *md_alg = mbedtls_md_get_type( md_info );
2247
2248 /* The Mbed TLS RSA module uses an unsigned int for hash length
2249 * parameters. Validate that it fits so that we don't risk an
2250 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002251#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002252 if( hash_length > UINT_MAX )
2253 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002254#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002255
2256#if defined(MBEDTLS_PKCS1_V15)
2257 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2258 * must be correct. */
2259 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2260 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002261 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002262 if( md_info == NULL )
2263 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002264 if( mbedtls_md_get_size( md_info ) != hash_length )
2265 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002266 }
2267#endif /* MBEDTLS_PKCS1_V15 */
2268
2269#if defined(MBEDTLS_PKCS1_V21)
2270 /* PSS requires a hash internally. */
2271 if( PSA_ALG_IS_RSA_PSS( alg ) )
2272 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002273 if( md_info == NULL )
2274 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002275 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002276#endif /* MBEDTLS_PKCS1_V21 */
2277
Gilles Peskine61b91d42018-06-08 16:09:36 +02002278 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002279}
2280
Gilles Peskine2b450e32018-06-27 15:42:46 +02002281static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2282 psa_algorithm_t alg,
2283 const uint8_t *hash,
2284 size_t hash_length,
2285 uint8_t *signature,
2286 size_t signature_size,
2287 size_t *signature_length )
2288{
2289 psa_status_t status;
2290 int ret;
2291 mbedtls_md_type_t md_alg;
2292
2293 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2294 if( status != PSA_SUCCESS )
2295 return( status );
2296
Gilles Peskine630a18a2018-06-29 17:49:35 +02002297 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002298 return( PSA_ERROR_BUFFER_TOO_SMALL );
2299
2300#if defined(MBEDTLS_PKCS1_V15)
2301 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2302 {
2303 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2304 MBEDTLS_MD_NONE );
2305 ret = mbedtls_rsa_pkcs1_sign( rsa,
2306 mbedtls_ctr_drbg_random,
2307 &global_data.ctr_drbg,
2308 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002309 md_alg,
2310 (unsigned int) hash_length,
2311 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002312 signature );
2313 }
2314 else
2315#endif /* MBEDTLS_PKCS1_V15 */
2316#if defined(MBEDTLS_PKCS1_V21)
2317 if( PSA_ALG_IS_RSA_PSS( alg ) )
2318 {
2319 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2320 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2321 mbedtls_ctr_drbg_random,
2322 &global_data.ctr_drbg,
2323 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002324 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002325 (unsigned int) hash_length,
2326 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002327 signature );
2328 }
2329 else
2330#endif /* MBEDTLS_PKCS1_V21 */
2331 {
2332 return( PSA_ERROR_INVALID_ARGUMENT );
2333 }
2334
2335 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002336 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002337 return( mbedtls_to_psa_error( ret ) );
2338}
2339
2340static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2341 psa_algorithm_t alg,
2342 const uint8_t *hash,
2343 size_t hash_length,
2344 const uint8_t *signature,
2345 size_t signature_length )
2346{
2347 psa_status_t status;
2348 int ret;
2349 mbedtls_md_type_t md_alg;
2350
2351 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2352 if( status != PSA_SUCCESS )
2353 return( status );
2354
Gilles Peskine630a18a2018-06-29 17:49:35 +02002355 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002356 return( PSA_ERROR_BUFFER_TOO_SMALL );
2357
2358#if defined(MBEDTLS_PKCS1_V15)
2359 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2360 {
2361 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2362 MBEDTLS_MD_NONE );
2363 ret = mbedtls_rsa_pkcs1_verify( rsa,
2364 mbedtls_ctr_drbg_random,
2365 &global_data.ctr_drbg,
2366 MBEDTLS_RSA_PUBLIC,
2367 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002368 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002369 hash,
2370 signature );
2371 }
2372 else
2373#endif /* MBEDTLS_PKCS1_V15 */
2374#if defined(MBEDTLS_PKCS1_V21)
2375 if( PSA_ALG_IS_RSA_PSS( alg ) )
2376 {
2377 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2378 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2379 mbedtls_ctr_drbg_random,
2380 &global_data.ctr_drbg,
2381 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002382 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002383 (unsigned int) hash_length,
2384 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002385 signature );
2386 }
2387 else
2388#endif /* MBEDTLS_PKCS1_V21 */
2389 {
2390 return( PSA_ERROR_INVALID_ARGUMENT );
2391 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002392
2393 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2394 * the rest of the signature is invalid". This has little use in
2395 * practice and PSA doesn't report this distinction. */
2396 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2397 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002398 return( mbedtls_to_psa_error( ret ) );
2399}
2400#endif /* MBEDTLS_RSA_C */
2401
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002402#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002403/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2404 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2405 * (even though these functions don't modify it). */
2406static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2407 psa_algorithm_t alg,
2408 const uint8_t *hash,
2409 size_t hash_length,
2410 uint8_t *signature,
2411 size_t signature_size,
2412 size_t *signature_length )
2413{
2414 int ret;
2415 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002416 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002417 mbedtls_mpi_init( &r );
2418 mbedtls_mpi_init( &s );
2419
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002420 if( signature_size < 2 * curve_bytes )
2421 {
2422 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2423 goto cleanup;
2424 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002425
Gilles Peskinea05219c2018-11-16 16:02:56 +01002426#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002427 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2428 {
2429 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2430 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2431 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2432 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2433 hash, hash_length,
2434 md_alg ) );
2435 }
2436 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002437#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002438 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002439 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002440 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2441 hash, hash_length,
2442 mbedtls_ctr_drbg_random,
2443 &global_data.ctr_drbg ) );
2444 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002445
2446 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2447 signature,
2448 curve_bytes ) );
2449 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2450 signature + curve_bytes,
2451 curve_bytes ) );
2452
2453cleanup:
2454 mbedtls_mpi_free( &r );
2455 mbedtls_mpi_free( &s );
2456 if( ret == 0 )
2457 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002458 return( mbedtls_to_psa_error( ret ) );
2459}
2460
2461static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2462 const uint8_t *hash,
2463 size_t hash_length,
2464 const uint8_t *signature,
2465 size_t signature_length )
2466{
2467 int ret;
2468 mbedtls_mpi r, s;
2469 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2470 mbedtls_mpi_init( &r );
2471 mbedtls_mpi_init( &s );
2472
2473 if( signature_length != 2 * curve_bytes )
2474 return( PSA_ERROR_INVALID_SIGNATURE );
2475
2476 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2477 signature,
2478 curve_bytes ) );
2479 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2480 signature + curve_bytes,
2481 curve_bytes ) );
2482
2483 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2484 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002485
2486cleanup:
2487 mbedtls_mpi_free( &r );
2488 mbedtls_mpi_free( &s );
2489 return( mbedtls_to_psa_error( ret ) );
2490}
2491#endif /* MBEDTLS_ECDSA_C */
2492
Gilles Peskine61b91d42018-06-08 16:09:36 +02002493psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2494 psa_algorithm_t alg,
2495 const uint8_t *hash,
2496 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002497 uint8_t *signature,
2498 size_t signature_size,
2499 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002500{
2501 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002502 psa_status_t status;
2503
2504 *signature_length = signature_size;
2505
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002506 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2507 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002508 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002509 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002510 {
2511 status = PSA_ERROR_INVALID_ARGUMENT;
2512 goto exit;
2513 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002514
Gilles Peskine20035e32018-02-03 22:44:14 +01002515#if defined(MBEDTLS_RSA_C)
2516 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2517 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002518 status = psa_rsa_sign( slot->data.rsa,
2519 alg,
2520 hash, hash_length,
2521 signature, signature_size,
2522 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002523 }
2524 else
2525#endif /* defined(MBEDTLS_RSA_C) */
2526#if defined(MBEDTLS_ECP_C)
2527 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2528 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002529#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002530 if(
2531#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2532 PSA_ALG_IS_ECDSA( alg )
2533#else
2534 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2535#endif
2536 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002537 status = psa_ecdsa_sign( slot->data.ecp,
2538 alg,
2539 hash, hash_length,
2540 signature, signature_size,
2541 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002542 else
2543#endif /* defined(MBEDTLS_ECDSA_C) */
2544 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002545 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002546 }
itayzafrir5c753392018-05-08 11:18:38 +03002547 }
2548 else
2549#endif /* defined(MBEDTLS_ECP_C) */
2550 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002551 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002552 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002553
2554exit:
2555 /* Fill the unused part of the output buffer (the whole buffer on error,
2556 * the trailing part on success) with something that isn't a valid mac
2557 * (barring an attack on the mac and deliberately-crafted input),
2558 * in case the caller doesn't check the return status properly. */
2559 if( status == PSA_SUCCESS )
2560 memset( signature + *signature_length, '!',
2561 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002562 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002563 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002564 /* If signature_size is 0 then we have nothing to do. We must not call
2565 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002566 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002567}
2568
2569psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2570 psa_algorithm_t alg,
2571 const uint8_t *hash,
2572 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002573 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002574 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002575{
2576 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002577 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002578
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002579 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2580 if( status != PSA_SUCCESS )
2581 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002582
Gilles Peskine61b91d42018-06-08 16:09:36 +02002583#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002584 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002585 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002586 return( psa_rsa_verify( slot->data.rsa,
2587 alg,
2588 hash, hash_length,
2589 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002590 }
2591 else
2592#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002593#if defined(MBEDTLS_ECP_C)
2594 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2595 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002596#if defined(MBEDTLS_ECDSA_C)
2597 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002598 return( psa_ecdsa_verify( slot->data.ecp,
2599 hash, hash_length,
2600 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002601 else
2602#endif /* defined(MBEDTLS_ECDSA_C) */
2603 {
2604 return( PSA_ERROR_INVALID_ARGUMENT );
2605 }
itayzafrir5c753392018-05-08 11:18:38 +03002606 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002607 else
2608#endif /* defined(MBEDTLS_ECP_C) */
2609 {
2610 return( PSA_ERROR_NOT_SUPPORTED );
2611 }
2612}
2613
Gilles Peskine072ac562018-06-30 00:21:29 +02002614#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2615static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2616 mbedtls_rsa_context *rsa )
2617{
2618 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2619 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2620 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2621 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2622}
2623#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2624
Gilles Peskine61b91d42018-06-08 16:09:36 +02002625psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2626 psa_algorithm_t alg,
2627 const uint8_t *input,
2628 size_t input_length,
2629 const uint8_t *salt,
2630 size_t salt_length,
2631 uint8_t *output,
2632 size_t output_size,
2633 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002634{
2635 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002636 psa_status_t status;
2637
Darryl Green5cc689a2018-07-24 15:34:10 +01002638 (void) input;
2639 (void) input_length;
2640 (void) salt;
2641 (void) output;
2642 (void) output_size;
2643
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002644 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002645
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002646 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2647 return( PSA_ERROR_INVALID_ARGUMENT );
2648
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002649 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2650 if( status != PSA_SUCCESS )
2651 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002652 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2653 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002654 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002655
2656#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002657 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002658 {
2659 mbedtls_rsa_context *rsa = slot->data.rsa;
2660 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002661 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002662 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002663#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002664 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002665 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002666 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2667 mbedtls_ctr_drbg_random,
2668 &global_data.ctr_drbg,
2669 MBEDTLS_RSA_PUBLIC,
2670 input_length,
2671 input,
2672 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002673 }
2674 else
2675#endif /* MBEDTLS_PKCS1_V15 */
2676#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002677 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002678 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002679 psa_rsa_oaep_set_padding_mode( alg, rsa );
2680 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2681 mbedtls_ctr_drbg_random,
2682 &global_data.ctr_drbg,
2683 MBEDTLS_RSA_PUBLIC,
2684 salt, salt_length,
2685 input_length,
2686 input,
2687 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002688 }
2689 else
2690#endif /* MBEDTLS_PKCS1_V21 */
2691 {
2692 return( PSA_ERROR_INVALID_ARGUMENT );
2693 }
2694 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002695 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002696 return( mbedtls_to_psa_error( ret ) );
2697 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002698 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002699#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002700 {
2701 return( PSA_ERROR_NOT_SUPPORTED );
2702 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002703}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002704
Gilles Peskine61b91d42018-06-08 16:09:36 +02002705psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2706 psa_algorithm_t alg,
2707 const uint8_t *input,
2708 size_t input_length,
2709 const uint8_t *salt,
2710 size_t salt_length,
2711 uint8_t *output,
2712 size_t output_size,
2713 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002714{
2715 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002716 psa_status_t status;
2717
Darryl Green5cc689a2018-07-24 15:34:10 +01002718 (void) input;
2719 (void) input_length;
2720 (void) salt;
2721 (void) output;
2722 (void) output_size;
2723
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002724 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002725
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002726 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2727 return( PSA_ERROR_INVALID_ARGUMENT );
2728
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002729 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2730 if( status != PSA_SUCCESS )
2731 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002732 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2733 return( PSA_ERROR_INVALID_ARGUMENT );
2734
2735#if defined(MBEDTLS_RSA_C)
2736 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2737 {
2738 mbedtls_rsa_context *rsa = slot->data.rsa;
2739 int ret;
2740
Gilles Peskine630a18a2018-06-29 17:49:35 +02002741 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002742 return( PSA_ERROR_INVALID_ARGUMENT );
2743
2744#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002745 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002746 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002747 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2748 mbedtls_ctr_drbg_random,
2749 &global_data.ctr_drbg,
2750 MBEDTLS_RSA_PRIVATE,
2751 output_length,
2752 input,
2753 output,
2754 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002755 }
2756 else
2757#endif /* MBEDTLS_PKCS1_V15 */
2758#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002759 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002760 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002761 psa_rsa_oaep_set_padding_mode( alg, rsa );
2762 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2763 mbedtls_ctr_drbg_random,
2764 &global_data.ctr_drbg,
2765 MBEDTLS_RSA_PRIVATE,
2766 salt, salt_length,
2767 output_length,
2768 input,
2769 output,
2770 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002771 }
2772 else
2773#endif /* MBEDTLS_PKCS1_V21 */
2774 {
2775 return( PSA_ERROR_INVALID_ARGUMENT );
2776 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002777
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002778 return( mbedtls_to_psa_error( ret ) );
2779 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002780 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002781#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002782 {
2783 return( PSA_ERROR_NOT_SUPPORTED );
2784 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002785}
Gilles Peskine20035e32018-02-03 22:44:14 +01002786
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002787
2788
mohammad1603503973b2018-03-12 15:59:30 +02002789/****************************************************************/
2790/* Symmetric cryptography */
2791/****************************************************************/
2792
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002793/* Initialize the cipher operation structure. Once this function has been
2794 * called, psa_cipher_abort can run and will do the right thing. */
2795static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2796 psa_algorithm_t alg )
2797{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002798 if( ! PSA_ALG_IS_CIPHER( alg ) )
2799 {
2800 memset( operation, 0, sizeof( *operation ) );
2801 return( PSA_ERROR_INVALID_ARGUMENT );
2802 }
2803
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002804 operation->alg = alg;
2805 operation->key_set = 0;
2806 operation->iv_set = 0;
2807 operation->iv_required = 1;
2808 operation->iv_size = 0;
2809 operation->block_size = 0;
2810 mbedtls_cipher_init( &operation->ctx.cipher );
2811 return( PSA_SUCCESS );
2812}
2813
Gilles Peskinee553c652018-06-04 16:22:46 +02002814static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2815 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002816 psa_algorithm_t alg,
2817 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002818{
2819 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2820 psa_status_t status;
2821 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002822 size_t key_bits;
2823 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002824 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2825 PSA_KEY_USAGE_ENCRYPT :
2826 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002827
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002828 status = psa_cipher_init( operation, alg );
2829 if( status != PSA_SUCCESS )
2830 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002831
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002832 status = psa_get_key_from_slot( key, &slot, usage, alg);
2833 if( status != PSA_SUCCESS )
2834 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002835 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002836
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002837 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002838 if( cipher_info == NULL )
2839 return( PSA_ERROR_NOT_SUPPORTED );
2840
mohammad1603503973b2018-03-12 15:59:30 +02002841 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002842 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002843 {
2844 psa_cipher_abort( operation );
2845 return( mbedtls_to_psa_error( ret ) );
2846 }
2847
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002848#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002849 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002850 {
2851 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2852 unsigned char keys[24];
2853 memcpy( keys, slot->data.raw.data, 16 );
2854 memcpy( keys + 16, slot->data.raw.data, 8 );
2855 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2856 keys,
2857 192, cipher_operation );
2858 }
2859 else
2860#endif
2861 {
2862 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2863 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002864 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002865 }
Moran Peker41deec42018-04-04 15:43:05 +03002866 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002867 {
2868 psa_cipher_abort( operation );
2869 return( mbedtls_to_psa_error( ret ) );
2870 }
2871
mohammad16038481e742018-03-18 13:57:31 +02002872#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002873 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002874 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002875 case PSA_ALG_CBC_NO_PADDING:
2876 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2877 MBEDTLS_PADDING_NONE );
2878 break;
2879 case PSA_ALG_CBC_PKCS7:
2880 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2881 MBEDTLS_PADDING_PKCS7 );
2882 break;
2883 default:
2884 /* The algorithm doesn't involve padding. */
2885 ret = 0;
2886 break;
2887 }
2888 if( ret != 0 )
2889 {
2890 psa_cipher_abort( operation );
2891 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002892 }
2893#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2894
mohammad1603503973b2018-03-12 15:59:30 +02002895 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002896 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2897 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2898 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002899 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002900 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002901 }
mohammad1603503973b2018-03-12 15:59:30 +02002902
Moran Peker395db872018-05-31 14:07:14 +03002903 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002904}
2905
Gilles Peskinefe119512018-07-08 21:39:34 +02002906psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2907 psa_key_slot_t key,
2908 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002909{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002910 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002911}
2912
Gilles Peskinefe119512018-07-08 21:39:34 +02002913psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2914 psa_key_slot_t key,
2915 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002916{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002917 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002918}
2919
Gilles Peskinefe119512018-07-08 21:39:34 +02002920psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2921 unsigned char *iv,
2922 size_t iv_size,
2923 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002924{
itayzafrir534bd7c2018-08-02 13:56:32 +03002925 psa_status_t status;
2926 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002927 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002928 {
2929 status = PSA_ERROR_BAD_STATE;
2930 goto exit;
2931 }
Moran Peker41deec42018-04-04 15:43:05 +03002932 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002933 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002934 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002935 goto exit;
2936 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002937 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2938 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002939 if( ret != 0 )
2940 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002941 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002942 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002943 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002944
mohammad16038481e742018-03-18 13:57:31 +02002945 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002946 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002947
Moran Peker395db872018-05-31 14:07:14 +03002948exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002949 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002950 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002951 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002952}
2953
Gilles Peskinefe119512018-07-08 21:39:34 +02002954psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2955 const unsigned char *iv,
2956 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002957{
itayzafrir534bd7c2018-08-02 13:56:32 +03002958 psa_status_t status;
2959 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002960 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002961 {
2962 status = PSA_ERROR_BAD_STATE;
2963 goto exit;
2964 }
Moran Pekera28258c2018-05-29 16:25:04 +03002965 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002966 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002967 status = PSA_ERROR_INVALID_ARGUMENT;
2968 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002969 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002970 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2971 status = mbedtls_to_psa_error( ret );
2972exit:
2973 if( status == PSA_SUCCESS )
2974 operation->iv_set = 1;
2975 else
mohammad1603503973b2018-03-12 15:59:30 +02002976 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002977 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002978}
2979
Gilles Peskinee553c652018-06-04 16:22:46 +02002980psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2981 const uint8_t *input,
2982 size_t input_length,
2983 unsigned char *output,
2984 size_t output_size,
2985 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002986{
itayzafrir534bd7c2018-08-02 13:56:32 +03002987 psa_status_t status;
2988 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002989 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002990 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002991 {
2992 /* Take the unprocessed partial block left over from previous
2993 * update calls, if any, plus the input to this call. Remove
2994 * the last partial block, if any. You get the data that will be
2995 * output in this call. */
2996 expected_output_size =
2997 ( operation->ctx.cipher.unprocessed_len + input_length )
2998 / operation->block_size * operation->block_size;
2999 }
3000 else
3001 {
3002 expected_output_size = input_length;
3003 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003004
Gilles Peskine89d789c2018-06-04 17:17:16 +02003005 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003006 {
3007 status = PSA_ERROR_BUFFER_TOO_SMALL;
3008 goto exit;
3009 }
mohammad160382759612018-03-12 18:16:40 +02003010
mohammad1603503973b2018-03-12 15:59:30 +02003011 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003012 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003013 status = mbedtls_to_psa_error( ret );
3014exit:
3015 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003016 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003017 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003018}
3019
Gilles Peskinee553c652018-06-04 16:22:46 +02003020psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3021 uint8_t *output,
3022 size_t output_size,
3023 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003024{
Janos Follath315b51c2018-07-09 16:04:51 +01003025 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
3026 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003027 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003028
mohammad1603503973b2018-03-12 15:59:30 +02003029 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003030 {
Janos Follath315b51c2018-07-09 16:04:51 +01003031 status = PSA_ERROR_BAD_STATE;
3032 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003033 }
3034 if( operation->iv_required && ! operation->iv_set )
3035 {
Janos Follath315b51c2018-07-09 16:04:51 +01003036 status = PSA_ERROR_BAD_STATE;
3037 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003038 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003039
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003040 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003041 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3042 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003043 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003044 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003045 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003046 }
3047
Janos Follath315b51c2018-07-09 16:04:51 +01003048 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3049 temp_output_buffer,
3050 output_length );
3051 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003052 {
Janos Follath315b51c2018-07-09 16:04:51 +01003053 status = mbedtls_to_psa_error( cipher_ret );
3054 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003055 }
Janos Follath315b51c2018-07-09 16:04:51 +01003056
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003057 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003058 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003059 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003060 memcpy( output, temp_output_buffer, *output_length );
3061 else
3062 {
Janos Follath315b51c2018-07-09 16:04:51 +01003063 status = PSA_ERROR_BUFFER_TOO_SMALL;
3064 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003065 }
mohammad1603503973b2018-03-12 15:59:30 +02003066
Janos Follath279ab8e2018-07-09 16:13:21 +01003067 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003068 status = psa_cipher_abort( operation );
3069
3070 return( status );
3071
3072error:
3073
3074 *output_length = 0;
3075
Janos Follath279ab8e2018-07-09 16:13:21 +01003076 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003077 (void) psa_cipher_abort( operation );
3078
3079 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003080}
3081
Gilles Peskinee553c652018-06-04 16:22:46 +02003082psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3083{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003084 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003085 {
3086 /* The object has (apparently) been initialized but it is not
3087 * in use. It's ok to call abort on such an object, and there's
3088 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003089 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003090 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003091
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003092 /* Sanity check (shouldn't happen: operation->alg should
3093 * always have been initialized to a valid value). */
3094 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3095 return( PSA_ERROR_BAD_STATE );
3096
mohammad1603503973b2018-03-12 15:59:30 +02003097 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003098
Moran Peker41deec42018-04-04 15:43:05 +03003099 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003100 operation->key_set = 0;
3101 operation->iv_set = 0;
3102 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003103 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003104 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003105
Moran Peker395db872018-05-31 14:07:14 +03003106 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003107}
3108
Gilles Peskinea0655c32018-04-30 17:06:50 +02003109
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003110
mohammad16038cc1cee2018-03-28 01:21:33 +03003111/****************************************************************/
3112/* Key Policy */
3113/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003114
mohammad160327010052018-07-03 13:16:15 +03003115#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003116void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003117{
Gilles Peskine803ce742018-06-18 16:07:14 +02003118 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003119}
3120
Gilles Peskine2d277862018-06-18 15:41:12 +02003121void psa_key_policy_set_usage( psa_key_policy_t *policy,
3122 psa_key_usage_t usage,
3123 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003124{
mohammad16034eed7572018-03-28 05:14:59 -07003125 policy->usage = usage;
3126 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003127}
3128
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003129psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003130{
mohammad16036df908f2018-04-02 08:34:15 -07003131 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003132}
3133
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003134psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003135{
mohammad16036df908f2018-04-02 08:34:15 -07003136 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003137}
mohammad160327010052018-07-03 13:16:15 +03003138#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003139
Gilles Peskine2d277862018-06-18 15:41:12 +02003140psa_status_t psa_set_key_policy( psa_key_slot_t key,
3141 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003142{
3143 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003144 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003145
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003146 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003147 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003148
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003149 status = psa_get_empty_key_slot( key, &slot );
3150 if( status != PSA_SUCCESS )
3151 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003152
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003153 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3154 PSA_KEY_USAGE_ENCRYPT |
3155 PSA_KEY_USAGE_DECRYPT |
3156 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003157 PSA_KEY_USAGE_VERIFY |
3158 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003159 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003160
mohammad16036df908f2018-04-02 08:34:15 -07003161 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003162
3163 return( PSA_SUCCESS );
3164}
3165
Gilles Peskine2d277862018-06-18 15:41:12 +02003166psa_status_t psa_get_key_policy( psa_key_slot_t key,
3167 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003168{
3169 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003170 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003171
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003172 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003173 return( PSA_ERROR_INVALID_ARGUMENT );
3174
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003175 status = psa_get_key_slot( key, &slot );
3176 if( status != PSA_SUCCESS )
3177 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003178
mohammad16036df908f2018-04-02 08:34:15 -07003179 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003180
3181 return( PSA_SUCCESS );
3182}
Gilles Peskine20035e32018-02-03 22:44:14 +01003183
Gilles Peskinea0655c32018-04-30 17:06:50 +02003184
3185
mohammad1603804cd712018-03-20 22:44:08 +02003186/****************************************************************/
3187/* Key Lifetime */
3188/****************************************************************/
3189
Gilles Peskine2d277862018-06-18 15:41:12 +02003190psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
3191 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003192{
3193 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003194 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003195
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003196 status = psa_get_key_slot( key, &slot );
3197 if( status != PSA_SUCCESS )
3198 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003199
mohammad1603804cd712018-03-20 22:44:08 +02003200 *lifetime = slot->lifetime;
3201
3202 return( PSA_SUCCESS );
3203}
3204
Gilles Peskine2d277862018-06-18 15:41:12 +02003205psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01003206 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003207{
3208 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003209 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003210
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003211 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
3212 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
Darryl Greend49a4992018-06-18 17:27:26 +01003213 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003214 return( PSA_ERROR_INVALID_ARGUMENT );
3215
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003216 status = psa_get_empty_key_slot( key, &slot );
3217 if( status != PSA_SUCCESS )
3218 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02003219
Darryl Greend49a4992018-06-18 17:27:26 +01003220 if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003221 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003222
Darryl Greend49a4992018-06-18 17:27:26 +01003223#if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
3224 if( lifetime == PSA_KEY_LIFETIME_PERSISTENT )
3225 return( PSA_ERROR_NOT_SUPPORTED );
3226#endif
3227
mohammad1603060ad8a2018-03-20 14:28:38 -07003228 slot->lifetime = lifetime;
Gilles Peskine69f976b2018-11-30 18:46:56 +01003229 slot->persistent_storage_id = key;
mohammad1603804cd712018-03-20 22:44:08 +02003230
3231 return( PSA_SUCCESS );
3232}
3233
Gilles Peskine20035e32018-02-03 22:44:14 +01003234
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003235
mohammad16035955c982018-04-26 00:53:03 +03003236/****************************************************************/
3237/* AEAD */
3238/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003239
Gilles Peskineedf9a652018-08-17 18:11:56 +02003240typedef struct
3241{
3242 key_slot_t *slot;
3243 const mbedtls_cipher_info_t *cipher_info;
3244 union
3245 {
3246#if defined(MBEDTLS_CCM_C)
3247 mbedtls_ccm_context ccm;
3248#endif /* MBEDTLS_CCM_C */
3249#if defined(MBEDTLS_GCM_C)
3250 mbedtls_gcm_context gcm;
3251#endif /* MBEDTLS_GCM_C */
3252 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003253 psa_algorithm_t core_alg;
3254 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003255 uint8_t tag_length;
3256} aead_operation_t;
3257
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003258static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003259{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003260 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003261 {
3262#if defined(MBEDTLS_CCM_C)
3263 case PSA_ALG_CCM:
3264 mbedtls_ccm_free( &operation->ctx.ccm );
3265 break;
3266#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003267#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003268 case PSA_ALG_GCM:
3269 mbedtls_gcm_free( &operation->ctx.gcm );
3270 break;
3271#endif /* MBEDTLS_GCM_C */
3272 }
3273}
3274
3275static psa_status_t psa_aead_setup( aead_operation_t *operation,
3276 psa_key_slot_t key,
3277 psa_key_usage_t usage,
3278 psa_algorithm_t alg )
3279{
3280 psa_status_t status;
3281 size_t key_bits;
3282 mbedtls_cipher_id_t cipher_id;
3283
3284 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3285 if( status != PSA_SUCCESS )
3286 return( status );
3287
3288 key_bits = psa_get_key_bits( operation->slot );
3289
3290 operation->cipher_info =
3291 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3292 &cipher_id );
3293 if( operation->cipher_info == NULL )
3294 return( PSA_ERROR_NOT_SUPPORTED );
3295
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003296 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003297 {
3298#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003299 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3300 operation->core_alg = PSA_ALG_CCM;
3301 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003302 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3303 return( PSA_ERROR_INVALID_ARGUMENT );
3304 mbedtls_ccm_init( &operation->ctx.ccm );
3305 status = mbedtls_to_psa_error(
3306 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3307 operation->slot->data.raw.data,
3308 (unsigned int) key_bits ) );
3309 if( status != 0 )
3310 goto cleanup;
3311 break;
3312#endif /* MBEDTLS_CCM_C */
3313
3314#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003315 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3316 operation->core_alg = PSA_ALG_GCM;
3317 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003318 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3319 return( PSA_ERROR_INVALID_ARGUMENT );
3320 mbedtls_gcm_init( &operation->ctx.gcm );
3321 status = mbedtls_to_psa_error(
3322 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3323 operation->slot->data.raw.data,
3324 (unsigned int) key_bits ) );
3325 break;
3326#endif /* MBEDTLS_GCM_C */
3327
3328 default:
3329 return( PSA_ERROR_NOT_SUPPORTED );
3330 }
3331
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003332 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3333 {
3334 status = PSA_ERROR_INVALID_ARGUMENT;
3335 goto cleanup;
3336 }
3337 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3338 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3339 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3340 * In both cases, mbedtls_xxx will validate the tag length below. */
3341
Gilles Peskineedf9a652018-08-17 18:11:56 +02003342 return( PSA_SUCCESS );
3343
3344cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003345 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003346 return( status );
3347}
3348
mohammad16035955c982018-04-26 00:53:03 +03003349psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3350 psa_algorithm_t alg,
3351 const uint8_t *nonce,
3352 size_t nonce_length,
3353 const uint8_t *additional_data,
3354 size_t additional_data_length,
3355 const uint8_t *plaintext,
3356 size_t plaintext_length,
3357 uint8_t *ciphertext,
3358 size_t ciphertext_size,
3359 size_t *ciphertext_length )
3360{
mohammad16035955c982018-04-26 00:53:03 +03003361 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003362 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003363 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003364
mohammad1603f08a5502018-06-03 15:05:47 +03003365 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003366
Gilles Peskineedf9a652018-08-17 18:11:56 +02003367 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003368 if( status != PSA_SUCCESS )
3369 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003370
Gilles Peskineedf9a652018-08-17 18:11:56 +02003371 /* For all currently supported modes, the tag is at the end of the
3372 * ciphertext. */
3373 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3374 {
3375 status = PSA_ERROR_BUFFER_TOO_SMALL;
3376 goto exit;
3377 }
3378 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003379
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003380#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003381 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003382 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003383 status = mbedtls_to_psa_error(
3384 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3385 MBEDTLS_GCM_ENCRYPT,
3386 plaintext_length,
3387 nonce, nonce_length,
3388 additional_data, additional_data_length,
3389 plaintext, ciphertext,
3390 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003391 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003392 else
3393#endif /* MBEDTLS_GCM_C */
3394#if defined(MBEDTLS_CCM_C)
3395 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003396 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003397 status = mbedtls_to_psa_error(
3398 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3399 plaintext_length,
3400 nonce, nonce_length,
3401 additional_data,
3402 additional_data_length,
3403 plaintext, ciphertext,
3404 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003405 }
mohammad16035c8845f2018-05-09 05:40:09 -07003406 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003407#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003408 {
mohammad1603554faad2018-06-03 15:07:38 +03003409 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003410 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003411
Gilles Peskineedf9a652018-08-17 18:11:56 +02003412 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3413 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003414
Gilles Peskineedf9a652018-08-17 18:11:56 +02003415exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003416 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003417 if( status == PSA_SUCCESS )
3418 *ciphertext_length = plaintext_length + operation.tag_length;
3419 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003420}
3421
Gilles Peskineee652a32018-06-01 19:23:52 +02003422/* Locate the tag in a ciphertext buffer containing the encrypted data
3423 * followed by the tag. Return the length of the part preceding the tag in
3424 * *plaintext_length. This is the size of the plaintext in modes where
3425 * the encrypted data has the same size as the plaintext, such as
3426 * CCM and GCM. */
3427static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3428 const uint8_t *ciphertext,
3429 size_t ciphertext_length,
3430 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003431 const uint8_t **p_tag )
3432{
3433 size_t payload_length;
3434 if( tag_length > ciphertext_length )
3435 return( PSA_ERROR_INVALID_ARGUMENT );
3436 payload_length = ciphertext_length - tag_length;
3437 if( payload_length > plaintext_size )
3438 return( PSA_ERROR_BUFFER_TOO_SMALL );
3439 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003440 return( PSA_SUCCESS );
3441}
3442
mohammad16035955c982018-04-26 00:53:03 +03003443psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3444 psa_algorithm_t alg,
3445 const uint8_t *nonce,
3446 size_t nonce_length,
3447 const uint8_t *additional_data,
3448 size_t additional_data_length,
3449 const uint8_t *ciphertext,
3450 size_t ciphertext_length,
3451 uint8_t *plaintext,
3452 size_t plaintext_size,
3453 size_t *plaintext_length )
3454{
mohammad16035955c982018-04-26 00:53:03 +03003455 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003456 aead_operation_t operation;
3457 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003458
Gilles Peskineee652a32018-06-01 19:23:52 +02003459 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003460
Gilles Peskineedf9a652018-08-17 18:11:56 +02003461 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003462 if( status != PSA_SUCCESS )
3463 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003464
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003465#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003466 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003467 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003468 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003469 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003470 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003471 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003472 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003473
Gilles Peskineedf9a652018-08-17 18:11:56 +02003474 status = mbedtls_to_psa_error(
3475 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3476 ciphertext_length - operation.tag_length,
3477 nonce, nonce_length,
3478 additional_data,
3479 additional_data_length,
3480 tag, operation.tag_length,
3481 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003482 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003483 else
3484#endif /* MBEDTLS_GCM_C */
3485#if defined(MBEDTLS_CCM_C)
3486 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003487 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003488 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003489 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003490 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003491 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003492 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003493
Gilles Peskineedf9a652018-08-17 18:11:56 +02003494 status = mbedtls_to_psa_error(
3495 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3496 ciphertext_length - operation.tag_length,
3497 nonce, nonce_length,
3498 additional_data,
3499 additional_data_length,
3500 ciphertext, plaintext,
3501 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003502 }
mohammad160339574652018-06-01 04:39:53 -07003503 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003504#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003505 {
mohammad1603554faad2018-06-03 15:07:38 +03003506 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003507 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003508
Gilles Peskineedf9a652018-08-17 18:11:56 +02003509 if( status != PSA_SUCCESS && plaintext_size != 0 )
3510 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003511
Gilles Peskineedf9a652018-08-17 18:11:56 +02003512exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003513 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003514 if( status == PSA_SUCCESS )
3515 *plaintext_length = ciphertext_length - operation.tag_length;
3516 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003517}
3518
Gilles Peskinea0655c32018-04-30 17:06:50 +02003519
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003520
Gilles Peskine20035e32018-02-03 22:44:14 +01003521/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003522/* Generators */
3523/****************************************************************/
3524
3525psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3526{
3527 psa_status_t status = PSA_SUCCESS;
3528 if( generator->alg == 0 )
3529 {
3530 /* The object has (apparently) been initialized but it is not
3531 * in use. It's ok to call abort on such an object, and there's
3532 * nothing to do. */
3533 }
3534 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003535 if( generator->alg == PSA_ALG_SELECT_RAW )
3536 {
3537 if( generator->ctx.buffer.data != NULL )
3538 {
3539 mbedtls_zeroize( generator->ctx.buffer.data,
3540 generator->ctx.buffer.size );
3541 mbedtls_free( generator->ctx.buffer.data );
3542 }
3543 }
3544 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003545#if defined(MBEDTLS_MD_C)
3546 if( PSA_ALG_IS_HKDF( generator->alg ) )
3547 {
3548 mbedtls_free( generator->ctx.hkdf.info );
3549 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3550 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003551 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3552 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3553 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003554 {
3555 if( generator->ctx.tls12_prf.key != NULL )
3556 {
3557 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3558 generator->ctx.tls12_prf.key_len );
3559 mbedtls_free( generator->ctx.tls12_prf.key );
3560 }
Hanno Becker580fba12018-11-13 20:50:45 +00003561
3562 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3563 {
3564 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3565 generator->ctx.tls12_prf.Ai_with_seed_len );
3566 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3567 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003568 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003569 else
3570#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003571 {
3572 status = PSA_ERROR_BAD_STATE;
3573 }
3574 memset( generator, 0, sizeof( *generator ) );
3575 return( status );
3576}
3577
3578
3579psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3580 size_t *capacity)
3581{
3582 *capacity = generator->capacity;
3583 return( PSA_SUCCESS );
3584}
3585
Gilles Peskinebef7f142018-07-12 17:22:21 +02003586#if defined(MBEDTLS_MD_C)
3587/* Read some bytes from an HKDF-based generator. This performs a chunk
3588 * of the expand phase of the HKDF algorithm. */
3589static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3590 psa_algorithm_t hash_alg,
3591 uint8_t *output,
3592 size_t output_length )
3593{
3594 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3595 psa_status_t status;
3596
3597 while( output_length != 0 )
3598 {
3599 /* Copy what remains of the current block */
3600 uint8_t n = hash_length - hkdf->offset_in_block;
3601 if( n > output_length )
3602 n = (uint8_t) output_length;
3603 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3604 output += n;
3605 output_length -= n;
3606 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003607 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003608 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003609 /* We can't be wanting more output after block 0xff, otherwise
3610 * the capacity check in psa_generator_read() would have
3611 * prevented this call. It could happen only if the generator
3612 * object was corrupted or if this function is called directly
3613 * inside the library. */
3614 if( hkdf->block_number == 0xff )
3615 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003616
3617 /* We need a new block */
3618 ++hkdf->block_number;
3619 hkdf->offset_in_block = 0;
3620 status = psa_hmac_setup_internal( &hkdf->hmac,
3621 hkdf->prk, hash_length,
3622 hash_alg );
3623 if( status != PSA_SUCCESS )
3624 return( status );
3625 if( hkdf->block_number != 1 )
3626 {
3627 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3628 hkdf->output_block,
3629 hash_length );
3630 if( status != PSA_SUCCESS )
3631 return( status );
3632 }
3633 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3634 hkdf->info,
3635 hkdf->info_length );
3636 if( status != PSA_SUCCESS )
3637 return( status );
3638 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3639 &hkdf->block_number, 1 );
3640 if( status != PSA_SUCCESS )
3641 return( status );
3642 status = psa_hmac_finish_internal( &hkdf->hmac,
3643 hkdf->output_block,
3644 sizeof( hkdf->output_block ) );
3645 if( status != PSA_SUCCESS )
3646 return( status );
3647 }
3648
3649 return( PSA_SUCCESS );
3650}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003651
3652static psa_status_t psa_generator_tls12_prf_generate_next_block(
3653 psa_tls12_prf_generator_t *tls12_prf,
3654 psa_algorithm_t alg )
3655{
3656 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3657 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3658 psa_hmac_internal_data hmac;
3659 psa_status_t status, cleanup_status;
3660
Hanno Becker3b339e22018-11-13 20:56:14 +00003661 unsigned char *Ai;
3662 size_t Ai_len;
3663
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003664 /* We can't be wanting more output after block 0xff, otherwise
3665 * the capacity check in psa_generator_read() would have
3666 * prevented this call. It could happen only if the generator
3667 * object was corrupted or if this function is called directly
3668 * inside the library. */
3669 if( tls12_prf->block_number == 0xff )
3670 return( PSA_ERROR_BAD_STATE );
3671
3672 /* We need a new block */
3673 ++tls12_prf->block_number;
3674 tls12_prf->offset_in_block = 0;
3675
3676 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3677 *
3678 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3679 *
3680 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3681 * HMAC_hash(secret, A(2) + seed) +
3682 * HMAC_hash(secret, A(3) + seed) + ...
3683 *
3684 * A(0) = seed
3685 * A(i) = HMAC_hash( secret, A(i-1) )
3686 *
3687 * The `psa_tls12_prf_generator` structures saves the block
3688 * `HMAC_hash(secret, A(i) + seed)` from which the output
3689 * is currently extracted as `output_block`, while
3690 * `A(i) + seed` is stored in `Ai_with_seed`.
3691 *
3692 * Generating a new block means recalculating `Ai_with_seed`
3693 * from the A(i)-part of it, and afterwards recalculating
3694 * `output_block`.
3695 *
3696 * A(0) is computed at setup time.
3697 *
3698 */
3699
3700 psa_hmac_init_internal( &hmac );
3701
3702 /* We must distinguish the calculation of A(1) from those
3703 * of A(2) and higher, because A(0)=seed has a different
3704 * length than the other A(i). */
3705 if( tls12_prf->block_number == 1 )
3706 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003707 Ai = tls12_prf->Ai_with_seed + hash_length;
3708 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003709 }
3710 else
3711 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003712 Ai = tls12_prf->Ai_with_seed;
3713 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003714 }
3715
Hanno Becker3b339e22018-11-13 20:56:14 +00003716 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3717 status = psa_hmac_setup_internal( &hmac,
3718 tls12_prf->key,
3719 tls12_prf->key_len,
3720 hash_alg );
3721 if( status != PSA_SUCCESS )
3722 goto cleanup;
3723
3724 status = psa_hash_update( &hmac.hash_ctx,
3725 Ai, Ai_len );
3726 if( status != PSA_SUCCESS )
3727 goto cleanup;
3728
3729 status = psa_hmac_finish_internal( &hmac,
3730 tls12_prf->Ai_with_seed,
3731 hash_length );
3732 if( status != PSA_SUCCESS )
3733 goto cleanup;
3734
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003735 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3736 status = psa_hmac_setup_internal( &hmac,
3737 tls12_prf->key,
3738 tls12_prf->key_len,
3739 hash_alg );
3740 if( status != PSA_SUCCESS )
3741 goto cleanup;
3742
3743 status = psa_hash_update( &hmac.hash_ctx,
3744 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003745 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003746 if( status != PSA_SUCCESS )
3747 goto cleanup;
3748
3749 status = psa_hmac_finish_internal( &hmac,
3750 tls12_prf->output_block,
3751 hash_length );
3752 if( status != PSA_SUCCESS )
3753 goto cleanup;
3754
3755cleanup:
3756
3757 cleanup_status = psa_hmac_abort_internal( &hmac );
3758 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3759 status = cleanup_status;
3760
3761 return( status );
3762}
3763
3764/* Read some bytes from an TLS-1.2-PRF-based generator.
3765 * See Section 5 of RFC 5246. */
3766static psa_status_t psa_generator_tls12_prf_read(
3767 psa_tls12_prf_generator_t *tls12_prf,
3768 psa_algorithm_t alg,
3769 uint8_t *output,
3770 size_t output_length )
3771{
3772 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3773 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3774 psa_status_t status;
3775
3776 while( output_length != 0 )
3777 {
3778 /* Copy what remains of the current block */
3779 uint8_t n = hash_length - tls12_prf->offset_in_block;
3780
3781 /* Check if we have fully processed the current block. */
3782 if( n == 0 )
3783 {
3784 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3785 alg );
3786 if( status != PSA_SUCCESS )
3787 return( status );
3788
3789 continue;
3790 }
3791
3792 if( n > output_length )
3793 n = (uint8_t) output_length;
3794 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3795 n );
3796 output += n;
3797 output_length -= n;
3798 tls12_prf->offset_in_block += n;
3799 }
3800
3801 return( PSA_SUCCESS );
3802}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003803#endif /* MBEDTLS_MD_C */
3804
Gilles Peskineeab56e42018-07-12 17:12:33 +02003805psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3806 uint8_t *output,
3807 size_t output_length )
3808{
3809 psa_status_t status;
3810
3811 if( output_length > generator->capacity )
3812 {
3813 generator->capacity = 0;
3814 /* Go through the error path to wipe all confidential data now
3815 * that the generator object is useless. */
3816 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3817 goto exit;
3818 }
3819 if( output_length == 0 &&
3820 generator->capacity == 0 && generator->alg == 0 )
3821 {
3822 /* Edge case: this is a blank or finished generator, and 0
3823 * bytes were requested. The right error in this case could
3824 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3825 * INSUFFICIENT_CAPACITY, which is right for a finished
3826 * generator, for consistency with the case when
3827 * output_length > 0. */
3828 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3829 }
3830 generator->capacity -= output_length;
3831
Gilles Peskine751d9652018-09-18 12:05:44 +02003832 if( generator->alg == PSA_ALG_SELECT_RAW )
3833 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003834 /* Initially, the capacity of a selection generator is always
3835 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3836 * abbreviated in this comment as `size`. When the remaining
3837 * capacity is `c`, the next bytes to serve start `c` bytes
3838 * from the end of the buffer, i.e. `size - c` from the
3839 * beginning of the buffer. Since `generator->capacity` was just
3840 * decremented above, we need to serve the bytes from
3841 * `size - generator->capacity - output_length` to
3842 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003843 size_t offset =
3844 generator->ctx.buffer.size - generator->capacity - output_length;
3845 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3846 status = PSA_SUCCESS;
3847 }
3848 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003849#if defined(MBEDTLS_MD_C)
3850 if( PSA_ALG_IS_HKDF( generator->alg ) )
3851 {
3852 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3853 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3854 output, output_length );
3855 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003856 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3857 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003858 {
3859 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3860 generator->alg, output,
3861 output_length );
3862 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003863 else
3864#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003865 {
3866 return( PSA_ERROR_BAD_STATE );
3867 }
3868
3869exit:
3870 if( status != PSA_SUCCESS )
3871 {
3872 psa_generator_abort( generator );
3873 memset( output, '!', output_length );
3874 }
3875 return( status );
3876}
3877
Gilles Peskine08542d82018-07-19 17:05:42 +02003878#if defined(MBEDTLS_DES_C)
3879static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3880{
3881 if( data_size >= 8 )
3882 mbedtls_des_key_set_parity( data );
3883 if( data_size >= 16 )
3884 mbedtls_des_key_set_parity( data + 8 );
3885 if( data_size >= 24 )
3886 mbedtls_des_key_set_parity( data + 16 );
3887}
3888#endif /* MBEDTLS_DES_C */
3889
Gilles Peskineeab56e42018-07-12 17:12:33 +02003890psa_status_t psa_generator_import_key( psa_key_slot_t key,
3891 psa_key_type_t type,
3892 size_t bits,
3893 psa_crypto_generator_t *generator )
3894{
3895 uint8_t *data = NULL;
3896 size_t bytes = PSA_BITS_TO_BYTES( bits );
3897 psa_status_t status;
3898
3899 if( ! key_type_is_raw_bytes( type ) )
3900 return( PSA_ERROR_INVALID_ARGUMENT );
3901 if( bits % 8 != 0 )
3902 return( PSA_ERROR_INVALID_ARGUMENT );
3903 data = mbedtls_calloc( 1, bytes );
3904 if( data == NULL )
3905 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3906
3907 status = psa_generator_read( generator, data, bytes );
3908 if( status != PSA_SUCCESS )
3909 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003910#if defined(MBEDTLS_DES_C)
3911 if( type == PSA_KEY_TYPE_DES )
3912 psa_des_set_key_parity( data, bytes );
3913#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003914 status = psa_import_key( key, type, data, bytes );
3915
3916exit:
3917 mbedtls_free( data );
3918 return( status );
3919}
3920
3921
3922
3923/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003924/* Key derivation */
3925/****************************************************************/
3926
Gilles Peskinea05219c2018-11-16 16:02:56 +01003927#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003928/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003929 * of the HKDF algorithm.
3930 *
3931 * Note that if this function fails, you must call psa_generator_abort()
3932 * to potentially free embedded data structures and wipe confidential data.
3933 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003934static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003935 const uint8_t *secret,
3936 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003937 psa_algorithm_t hash_alg,
3938 const uint8_t *salt,
3939 size_t salt_length,
3940 const uint8_t *label,
3941 size_t label_length )
3942{
3943 psa_status_t status;
3944 status = psa_hmac_setup_internal( &hkdf->hmac,
3945 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003946 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003947 if( status != PSA_SUCCESS )
3948 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003949 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003950 if( status != PSA_SUCCESS )
3951 return( status );
3952 status = psa_hmac_finish_internal( &hkdf->hmac,
3953 hkdf->prk,
3954 sizeof( hkdf->prk ) );
3955 if( status != PSA_SUCCESS )
3956 return( status );
3957 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3958 hkdf->block_number = 0;
3959 hkdf->info_length = label_length;
3960 if( label_length != 0 )
3961 {
3962 hkdf->info = mbedtls_calloc( 1, label_length );
3963 if( hkdf->info == NULL )
3964 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3965 memcpy( hkdf->info, label, label_length );
3966 }
3967 return( PSA_SUCCESS );
3968}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003969#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003970
Gilles Peskinea05219c2018-11-16 16:02:56 +01003971#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003972/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3973 *
3974 * Note that if this function fails, you must call psa_generator_abort()
3975 * to potentially free embedded data structures and wipe confidential data.
3976 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003977static psa_status_t psa_generator_tls12_prf_setup(
3978 psa_tls12_prf_generator_t *tls12_prf,
3979 const unsigned char *key,
3980 size_t key_len,
3981 psa_algorithm_t hash_alg,
3982 const uint8_t *salt,
3983 size_t salt_length,
3984 const uint8_t *label,
3985 size_t label_length )
3986{
3987 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003988 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3989 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003990
3991 tls12_prf->key = mbedtls_calloc( 1, key_len );
3992 if( tls12_prf->key == NULL )
3993 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3994 tls12_prf->key_len = key_len;
3995 memcpy( tls12_prf->key, key, key_len );
3996
Hanno Becker580fba12018-11-13 20:50:45 +00003997 overflow = ( salt_length + label_length < salt_length ) ||
3998 ( salt_length + label_length + hash_length < hash_length );
3999 if( overflow )
4000 return( PSA_ERROR_INVALID_ARGUMENT );
4001
4002 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4003 if( tls12_prf->Ai_with_seed == NULL )
4004 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4005 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4006
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004007 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4008 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004009 if( label_length != 0 )
4010 {
4011 memcpy( tls12_prf->Ai_with_seed + hash_length,
4012 label, label_length );
4013 }
4014
4015 if( salt_length != 0 )
4016 {
4017 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4018 salt, salt_length );
4019 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004020
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004021 /* The first block gets generated when
4022 * psa_generator_read() is called. */
4023 tls12_prf->block_number = 0;
4024 tls12_prf->offset_in_block = hash_length;
4025
4026 return( PSA_SUCCESS );
4027}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004028
4029/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4030static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4031 psa_tls12_prf_generator_t *tls12_prf,
4032 const unsigned char *psk,
4033 size_t psk_len,
4034 psa_algorithm_t hash_alg,
4035 const uint8_t *salt,
4036 size_t salt_length,
4037 const uint8_t *label,
4038 size_t label_length )
4039{
4040 psa_status_t status;
4041 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4042
4043 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4044 return( PSA_ERROR_INVALID_ARGUMENT );
4045
4046 /* Quoting RFC 4279, Section 2:
4047 *
4048 * The premaster secret is formed as follows: if the PSK is N octets
4049 * long, concatenate a uint16 with the value N, N zero octets, a second
4050 * uint16 with the value N, and the PSK itself.
4051 */
4052
4053 pms[0] = ( psk_len >> 8 ) & 0xff;
4054 pms[1] = ( psk_len >> 0 ) & 0xff;
4055 memset( pms + 2, 0, psk_len );
4056 pms[2 + psk_len + 0] = pms[0];
4057 pms[2 + psk_len + 1] = pms[1];
4058 memcpy( pms + 4 + psk_len, psk, psk_len );
4059
4060 status = psa_generator_tls12_prf_setup( tls12_prf,
4061 pms, 4 + 2 * psk_len,
4062 hash_alg,
4063 salt, salt_length,
4064 label, label_length );
4065
4066 mbedtls_zeroize( pms, sizeof( pms ) );
4067 return( status );
4068}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004069#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004070
Gilles Peskine346797d2018-11-16 16:05:06 +01004071/* Note that if this function fails, you must call psa_generator_abort()
4072 * to potentially free embedded data structures and wipe confidential data.
4073 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004074static psa_status_t psa_key_derivation_internal(
4075 psa_crypto_generator_t *generator,
4076 const uint8_t *secret, size_t secret_length,
4077 psa_algorithm_t alg,
4078 const uint8_t *salt, size_t salt_length,
4079 const uint8_t *label, size_t label_length,
4080 size_t capacity )
4081{
4082 psa_status_t status;
4083 size_t max_capacity;
4084
4085 /* Set generator->alg even on failure so that abort knows what to do. */
4086 generator->alg = alg;
4087
Gilles Peskine751d9652018-09-18 12:05:44 +02004088 if( alg == PSA_ALG_SELECT_RAW )
4089 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004090 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004091 if( salt_length != 0 )
4092 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004093 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004094 if( label_length != 0 )
4095 return( PSA_ERROR_INVALID_ARGUMENT );
4096 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4097 if( generator->ctx.buffer.data == NULL )
4098 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4099 memcpy( generator->ctx.buffer.data, secret, secret_length );
4100 generator->ctx.buffer.size = secret_length;
4101 max_capacity = secret_length;
4102 status = PSA_SUCCESS;
4103 }
4104 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004105#if defined(MBEDTLS_MD_C)
4106 if( PSA_ALG_IS_HKDF( alg ) )
4107 {
4108 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4109 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4110 if( hash_size == 0 )
4111 return( PSA_ERROR_NOT_SUPPORTED );
4112 max_capacity = 255 * hash_size;
4113 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4114 secret, secret_length,
4115 hash_alg,
4116 salt, salt_length,
4117 label, label_length );
4118 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004119 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4120 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4121 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004122 {
4123 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4124 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4125
4126 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4127 if( hash_alg != PSA_ALG_SHA_256 &&
4128 hash_alg != PSA_ALG_SHA_384 )
4129 {
4130 return( PSA_ERROR_NOT_SUPPORTED );
4131 }
4132
4133 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004134
4135 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4136 {
4137 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4138 secret, secret_length,
4139 hash_alg, salt, salt_length,
4140 label, label_length );
4141 }
4142 else
4143 {
4144 status = psa_generator_tls12_psk_to_ms_setup(
4145 &generator->ctx.tls12_prf,
4146 secret, secret_length,
4147 hash_alg, salt, salt_length,
4148 label, label_length );
4149 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004150 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004151 else
4152#endif
4153 {
4154 return( PSA_ERROR_NOT_SUPPORTED );
4155 }
4156
4157 if( status != PSA_SUCCESS )
4158 return( status );
4159
4160 if( capacity <= max_capacity )
4161 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004162 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4163 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004164 else
4165 return( PSA_ERROR_INVALID_ARGUMENT );
4166
4167 return( PSA_SUCCESS );
4168}
4169
Gilles Peskineea0fb492018-07-12 17:17:20 +02004170psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01004171 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004172 psa_algorithm_t alg,
4173 const uint8_t *salt,
4174 size_t salt_length,
4175 const uint8_t *label,
4176 size_t label_length,
4177 size_t capacity )
4178{
4179 key_slot_t *slot;
4180 psa_status_t status;
4181
4182 if( generator->alg != 0 )
4183 return( PSA_ERROR_BAD_STATE );
4184
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004185 /* Make sure that alg is a key derivation algorithm. This prevents
4186 * key selection algorithms, which psa_key_derivation_internal
4187 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004188 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4189 return( PSA_ERROR_INVALID_ARGUMENT );
4190
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004191 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4192 if( status != PSA_SUCCESS )
4193 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004194
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004195 if( slot->type != PSA_KEY_TYPE_DERIVE )
4196 return( PSA_ERROR_INVALID_ARGUMENT );
4197
4198 status = psa_key_derivation_internal( generator,
4199 slot->data.raw.data,
4200 slot->data.raw.bytes,
4201 alg,
4202 salt, salt_length,
4203 label, label_length,
4204 capacity );
4205 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004206 psa_generator_abort( generator );
4207 return( status );
4208}
4209
4210
4211
4212/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004213/* Key agreement */
4214/****************************************************************/
4215
Gilles Peskinea05219c2018-11-16 16:02:56 +01004216#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004217static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4218 size_t peer_key_length,
4219 const mbedtls_ecp_keypair *our_key,
4220 uint8_t *shared_secret,
4221 size_t shared_secret_size,
4222 size_t *shared_secret_length )
4223{
4224 mbedtls_pk_context pk;
4225 mbedtls_ecp_keypair *their_key = NULL;
4226 mbedtls_ecdh_context ecdh;
4227 int ret;
4228 mbedtls_ecdh_init( &ecdh );
4229 mbedtls_pk_init( &pk );
4230
4231 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4232 if( ret != 0 )
4233 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004234 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004235 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004236 case MBEDTLS_PK_ECKEY:
4237 case MBEDTLS_PK_ECKEY_DH:
4238 break;
4239 default:
4240 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4241 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004242 }
4243 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004244 if( their_key->grp.id != our_key->grp.id )
4245 {
4246 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4247 goto exit;
4248 }
4249
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004250 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4251 if( ret != 0 )
4252 goto exit;
4253 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4254 if( ret != 0 )
4255 goto exit;
4256
4257 ret = mbedtls_ecdh_calc_secret( &ecdh,
4258 shared_secret_length,
4259 shared_secret, shared_secret_size,
4260 mbedtls_ctr_drbg_random,
4261 &global_data.ctr_drbg );
4262
4263exit:
4264 mbedtls_pk_free( &pk );
4265 mbedtls_ecdh_free( &ecdh );
4266 return( mbedtls_to_psa_error( ret ) );
4267}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004268#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004269
Gilles Peskine01d718c2018-09-18 12:01:02 +02004270#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4271
Gilles Peskine346797d2018-11-16 16:05:06 +01004272/* Note that if this function fails, you must call psa_generator_abort()
4273 * to potentially free embedded data structures and wipe confidential data.
4274 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004275static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4276 key_slot_t *private_key,
4277 const uint8_t *peer_key,
4278 size_t peer_key_length,
4279 psa_algorithm_t alg )
4280{
4281 psa_status_t status;
4282 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4283 size_t shared_secret_length = 0;
4284
4285 /* Step 1: run the secret agreement algorithm to generate the shared
4286 * secret. */
4287 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4288 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004289#if defined(MBEDTLS_ECDH_C)
4290 case PSA_ALG_ECDH_BASE:
4291 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4292 return( PSA_ERROR_INVALID_ARGUMENT );
4293 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4294 private_key->data.ecp,
4295 shared_secret,
4296 sizeof( shared_secret ),
4297 &shared_secret_length );
4298 break;
4299#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004300 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004301 (void) private_key;
4302 (void) peer_key;
4303 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004304 return( PSA_ERROR_NOT_SUPPORTED );
4305 }
4306 if( status != PSA_SUCCESS )
4307 goto exit;
4308
4309 /* Step 2: set up the key derivation to generate key material from
4310 * the shared secret. */
4311 status = psa_key_derivation_internal( generator,
4312 shared_secret, shared_secret_length,
4313 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4314 NULL, 0, NULL, 0,
4315 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4316exit:
4317 mbedtls_zeroize( shared_secret, shared_secret_length );
4318 return( status );
4319}
4320
4321psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4322 psa_key_slot_t private_key,
4323 const uint8_t *peer_key,
4324 size_t peer_key_length,
4325 psa_algorithm_t alg )
4326{
4327 key_slot_t *slot;
4328 psa_status_t status;
4329 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4330 return( PSA_ERROR_INVALID_ARGUMENT );
4331 status = psa_get_key_from_slot( private_key, &slot,
4332 PSA_KEY_USAGE_DERIVE, alg );
4333 if( status != PSA_SUCCESS )
4334 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004335 status = psa_key_agreement_internal( generator,
4336 slot,
4337 peer_key, peer_key_length,
4338 alg );
4339 if( status != PSA_SUCCESS )
4340 psa_generator_abort( generator );
4341 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004342}
4343
4344
4345
4346/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004347/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004348/****************************************************************/
4349
4350psa_status_t psa_generate_random( uint8_t *output,
4351 size_t output_size )
4352{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004353 int ret;
4354 GUARD_MODULE_INITIALIZED;
4355
4356 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004357 return( mbedtls_to_psa_error( ret ) );
4358}
4359
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004360#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004361
4362/* Support function for error conversion between psa_its error codes to psa crypto */
4363static psa_status_t its_to_psa_error( psa_its_status_t ret )
4364{
4365 switch( ret )
4366 {
4367 case PSA_ITS_SUCCESS:
4368 return( PSA_SUCCESS );
4369
4370 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4371 return( PSA_ERROR_EMPTY_SLOT );
4372
4373 case PSA_ITS_ERROR_STORAGE_FAILURE:
4374 return( PSA_ERROR_STORAGE_FAILURE );
4375
4376 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4377 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4378
4379 case PSA_ITS_ERROR_INVALID_KEY:
4380 case PSA_PS_ERROR_OFFSET_INVALID:
4381 case PSA_ITS_ERROR_INCORRECT_SIZE:
4382 case PSA_ITS_ERROR_BAD_POINTER:
4383 return( PSA_ERROR_INVALID_ARGUMENT );
4384
4385 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4386 return( PSA_ERROR_NOT_SUPPORTED );
4387
4388 case PSA_ITS_ERROR_WRITE_ONCE:
4389 return( PSA_ERROR_OCCUPIED_SLOT );
4390
4391 default:
4392 return( PSA_ERROR_UNKNOWN_ERROR );
4393 }
4394}
4395
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004396psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4397 size_t seed_size )
4398{
4399 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004400 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004401 struct psa_its_info_t p_info;
4402 if( global_data.initialized )
4403 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004404
4405 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4406 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4407 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4408 return( PSA_ERROR_INVALID_ARGUMENT );
4409
avolinski0d2c2662018-11-21 17:31:07 +02004410 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004411 status = its_to_psa_error( its_status );
4412
4413 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004414 {
avolinski0d2c2662018-11-21 17:31:07 +02004415 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004416 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004417 }
avolinski13beb102018-11-20 16:51:49 +02004418 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004419 {
4420 /* You should not be here. Seed needs to be injected only once */
4421 status = PSA_ERROR_NOT_PERMITTED;
4422 }
4423 return( status );
4424}
4425#endif
4426
Gilles Peskine05d69892018-06-19 22:00:52 +02004427psa_status_t psa_generate_key( psa_key_slot_t key,
4428 psa_key_type_t type,
4429 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004430 const void *extra,
4431 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004432{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004433 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004434 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004435
Gilles Peskine53d991e2018-07-12 01:14:59 +02004436 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004437 return( PSA_ERROR_INVALID_ARGUMENT );
4438
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004439 status = psa_get_empty_key_slot( key, &slot );
4440 if( status != PSA_SUCCESS )
4441 return( status );
4442
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004443 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004444 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004445 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004446 if( status != PSA_SUCCESS )
4447 return( status );
4448 status = psa_generate_random( slot->data.raw.data,
4449 slot->data.raw.bytes );
4450 if( status != PSA_SUCCESS )
4451 {
4452 mbedtls_free( slot->data.raw.data );
4453 return( status );
4454 }
4455#if defined(MBEDTLS_DES_C)
4456 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004457 psa_des_set_key_parity( slot->data.raw.data,
4458 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004459#endif /* MBEDTLS_DES_C */
4460 }
4461 else
4462
4463#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4464 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4465 {
4466 mbedtls_rsa_context *rsa;
4467 int ret;
4468 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004469 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4470 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004471 /* Accept only byte-aligned keys, for the same reasons as
4472 * in psa_import_rsa_key(). */
4473 if( bits % 8 != 0 )
4474 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004475 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004476 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004477 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004478 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004479 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004480#if INT_MAX < 0xffffffff
4481 /* Check that the uint32_t value passed by the caller fits
4482 * in the range supported by this implementation. */
4483 if( p->e > INT_MAX )
4484 return( PSA_ERROR_NOT_SUPPORTED );
4485#endif
4486 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004487 }
4488 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4489 if( rsa == NULL )
4490 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4491 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4492 ret = mbedtls_rsa_gen_key( rsa,
4493 mbedtls_ctr_drbg_random,
4494 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004495 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004496 exponent );
4497 if( ret != 0 )
4498 {
4499 mbedtls_rsa_free( rsa );
4500 mbedtls_free( rsa );
4501 return( mbedtls_to_psa_error( ret ) );
4502 }
4503 slot->data.rsa = rsa;
4504 }
4505 else
4506#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4507
4508#if defined(MBEDTLS_ECP_C)
4509 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4510 {
4511 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4512 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4513 const mbedtls_ecp_curve_info *curve_info =
4514 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4515 mbedtls_ecp_keypair *ecp;
4516 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004517 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004518 return( PSA_ERROR_NOT_SUPPORTED );
4519 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4520 return( PSA_ERROR_NOT_SUPPORTED );
4521 if( curve_info->bit_size != bits )
4522 return( PSA_ERROR_INVALID_ARGUMENT );
4523 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4524 if( ecp == NULL )
4525 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4526 mbedtls_ecp_keypair_init( ecp );
4527 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4528 mbedtls_ctr_drbg_random,
4529 &global_data.ctr_drbg );
4530 if( ret != 0 )
4531 {
4532 mbedtls_ecp_keypair_free( ecp );
4533 mbedtls_free( ecp );
4534 return( mbedtls_to_psa_error( ret ) );
4535 }
4536 slot->data.ecp = ecp;
4537 }
4538 else
4539#endif /* MBEDTLS_ECP_C */
4540
4541 return( PSA_ERROR_NOT_SUPPORTED );
4542
4543 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004544
4545#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4546 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4547 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004548 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004549 }
4550#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4551
4552 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004553}
4554
4555
4556/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004557/* Module setup */
4558/****************************************************************/
4559
Gilles Peskine5e769522018-11-20 21:59:56 +01004560psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4561 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4562 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4563{
4564 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4565 return( PSA_ERROR_BAD_STATE );
4566 global_data.entropy_init = entropy_init;
4567 global_data.entropy_free = entropy_free;
4568 return( PSA_SUCCESS );
4569}
4570
Gilles Peskinee59236f2018-01-27 23:32:46 +01004571void mbedtls_psa_crypto_free( void )
4572{
Gilles Peskinec6b69072018-11-20 21:42:52 +01004573 if( global_data.key_slots_initialized )
Darryl Green40225ba2018-11-15 14:48:15 +00004574 {
Gilles Peskined7c75702018-12-03 10:36:46 +01004575 psa_key_slot_t key;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004576 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
4577 {
Gilles Peskined7c75702018-12-03 10:36:46 +01004578 key_slot_t *slot = &global_data.key_slots[key - 1];
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01004579 (void) psa_wipe_key_slot( slot );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004580 }
Darryl Green40225ba2018-11-15 14:48:15 +00004581 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01004582 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4583 {
4584 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004585 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004586 }
4587 /* Wipe all remaining data, including configuration.
4588 * In particular, this sets all state indicator to the value
4589 * indicating "uninitialized". */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004590 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4591}
4592
4593psa_status_t psa_crypto_init( void )
4594{
4595 int ret;
4596 const unsigned char drbg_seed[] = "PSA";
4597
Gilles Peskinec6b69072018-11-20 21:42:52 +01004598 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004599 if( global_data.initialized != 0 )
4600 return( PSA_SUCCESS );
4601
Gilles Peskine5e769522018-11-20 21:59:56 +01004602 /* Set default configuration if
4603 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4604 if( global_data.entropy_init == NULL )
4605 global_data.entropy_init = mbedtls_entropy_init;
4606 if( global_data.entropy_free == NULL )
4607 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004608
Gilles Peskinec6b69072018-11-20 21:42:52 +01004609 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004610 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004611 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004612 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004613 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4614 mbedtls_entropy_func,
4615 &global_data.entropy,
4616 drbg_seed, sizeof( drbg_seed ) - 1 );
4617 if( ret != 0 )
4618 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004619 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004620
Gilles Peskinec6b69072018-11-20 21:42:52 +01004621 /* Initialize the key slots. Zero-initialization has made all key
4622 * slots empty, so there is nothing to do. In a future version we will
4623 * load data from storage. */
4624 global_data.key_slots_initialized = 1;
4625
4626 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004627 global_data.initialized = 1;
4628
Gilles Peskinee59236f2018-01-27 23:32:46 +01004629exit:
4630 if( ret != 0 )
4631 mbedtls_psa_crypto_free( );
4632 return( mbedtls_to_psa_error( ret ) );
4633}
4634
4635#endif /* MBEDTLS_PSA_CRYPTO_C */