blob: 82cb158026253a390942c653fb0e4dc65f36d460 [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/*
itayzafrir14e76782019-01-16 11:16:39 +020030 * When MBEDTLS_PSA_CRYPTO_SPM is defined, the code is being built for SPM
31 * (Secure Partition Manager) integration which separates the code into two
32 * parts: NSPE (Non-Secure Processing Environment) and SPE (Secure Processing
33 * Environment). When building for the SPE, an additional header file should be
34 * included.
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030035 */
mohammad160327010052018-07-03 13:16:15 +030036#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030037/*
itayzafrir14e76782019-01-16 11:16:39 +020038 * PSA_CRYPTO_SECURE means that this file is compiled for the SPE.
39 * Some headers will be affected by this flag.
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030040 */
mohammad160327010052018-07-03 13:16:15 +030041#define PSA_CRYPTO_SECURE 1
42#include "crypto_spe.h"
43#endif
44
Gilles Peskinee59236f2018-01-27 23:32:46 +010045#include "psa/crypto.h"
46
Gilles Peskine039b90c2018-12-07 18:24:41 +010047#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010048#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010049#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010050/* Include internal declarations that are useful for implementing persistently
51 * stored keys. */
52#include "psa_crypto_storage.h"
53
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include <stdlib.h>
55#include <string.h>
56#if defined(MBEDTLS_PLATFORM_C)
57#include "mbedtls/platform.h"
58#else
59#define mbedtls_calloc calloc
60#define mbedtls_free free
61#endif
62
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020064#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020065#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/blowfish.h"
67#include "mbedtls/camellia.h"
68#include "mbedtls/cipher.h"
69#include "mbedtls/ccm.h"
70#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010071#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020073#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010074#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010075#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020076#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/error.h"
78#include "mbedtls/gcm.h"
79#include "mbedtls/md2.h"
80#include "mbedtls/md4.h"
81#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010082#include "mbedtls/md.h"
83#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010084#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010085#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010086#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010087#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010088#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010089#include "mbedtls/sha1.h"
90#include "mbedtls/sha256.h"
91#include "mbedtls/sha512.h"
92#include "mbedtls/xtea.h"
93
Netanel Gonen2bcd3122018-11-19 11:53:02 +020094#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
95#include "psa_prot_internal_storage.h"
96#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010097
Gilles Peskine996deb12018-08-01 15:45:45 +020098#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
99
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100100/* constant-time buffer comparison */
101static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
102{
103 size_t i;
104 unsigned char diff = 0;
105
106 for( i = 0; i < n; i++ )
107 diff |= a[i] ^ b[i];
108
109 return( diff );
110}
111
112
113
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100114/****************************************************************/
115/* Global data, support functions and library management */
116/****************************************************************/
117
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200118static int key_type_is_raw_bytes( psa_key_type_t type )
119{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200120 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200121}
122
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100123/* Values for psa_global_data_t::rng_state */
124#define RNG_NOT_INITIALIZED 0
125#define RNG_INITIALIZED 1
126#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100127
Gilles Peskine2d277862018-06-18 15:41:12 +0200128typedef struct
129{
Gilles Peskine5e769522018-11-20 21:59:56 +0100130 void (* entropy_init )( mbedtls_entropy_context *ctx );
131 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132 mbedtls_entropy_context entropy;
133 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100134 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100135 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136} psa_global_data_t;
137
138static psa_global_data_t global_data;
139
itayzafrir0adf0fc2018-09-06 16:24:41 +0300140#define GUARD_MODULE_INITIALIZED \
141 if( global_data.initialized == 0 ) \
142 return( PSA_ERROR_BAD_STATE );
143
Gilles Peskinee59236f2018-01-27 23:32:46 +0100144static psa_status_t mbedtls_to_psa_error( int ret )
145{
Gilles Peskinea5905292018-02-07 20:59:33 +0100146 /* If there's both a high-level code and low-level code, dispatch on
147 * the high-level code. */
148 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100149 {
150 case 0:
151 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100152
153 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
154 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
155 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
156 return( PSA_ERROR_NOT_SUPPORTED );
157 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
158 return( PSA_ERROR_HARDWARE_FAILURE );
159
160 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
161 return( PSA_ERROR_HARDWARE_FAILURE );
162
Gilles Peskine9a944802018-06-21 09:35:35 +0200163 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
164 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
165 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
166 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
167 case MBEDTLS_ERR_ASN1_INVALID_DATA:
168 return( PSA_ERROR_INVALID_ARGUMENT );
169 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
170 return( PSA_ERROR_INSUFFICIENT_MEMORY );
171 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
172 return( PSA_ERROR_BUFFER_TOO_SMALL );
173
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
175 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
181 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
182 return( PSA_ERROR_NOT_SUPPORTED );
183 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
186 case MBEDTLS_ERR_CCM_BAD_INPUT:
187 return( PSA_ERROR_INVALID_ARGUMENT );
188 case MBEDTLS_ERR_CCM_AUTH_FAILED:
189 return( PSA_ERROR_INVALID_SIGNATURE );
190 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
191 return( PSA_ERROR_HARDWARE_FAILURE );
192
193 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
194 return( PSA_ERROR_NOT_SUPPORTED );
195 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
200 return( PSA_ERROR_INVALID_PADDING );
201 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
202 return( PSA_ERROR_BAD_STATE );
203 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
206 return( PSA_ERROR_TAMPERING_DETECTED );
207 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
210 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
216 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220
221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
227 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
228 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
229 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200234 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
243 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
244 return( PSA_ERROR_NOT_SUPPORTED );
245 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MD_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
261 return( PSA_ERROR_BUFFER_TOO_SMALL );
262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
285 return( PSA_ERROR_NOT_PERMITTED );
286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
291 return( PSA_ERROR_NOT_SUPPORTED );
292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296
297 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
300 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
301 return( PSA_ERROR_INVALID_ARGUMENT );
302 case MBEDTLS_ERR_RSA_INVALID_PADDING:
303 return( PSA_ERROR_INVALID_PADDING );
304 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
307 return( PSA_ERROR_INVALID_ARGUMENT );
308 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
309 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
310 return( PSA_ERROR_TAMPERING_DETECTED );
311 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
312 return( PSA_ERROR_INVALID_SIGNATURE );
313 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
314 return( PSA_ERROR_BUFFER_TOO_SMALL );
315 case MBEDTLS_ERR_RSA_RNG_FAILED:
316 return( PSA_ERROR_INSUFFICIENT_MEMORY );
317 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
318 return( PSA_ERROR_NOT_SUPPORTED );
319 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
322 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
323 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
324 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
327 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
328 return( PSA_ERROR_INVALID_ARGUMENT );
329 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
itayzafrir5c753392018-05-08 11:18:38 +0300332 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300333 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300334 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300335 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
336 return( PSA_ERROR_BUFFER_TOO_SMALL );
337 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
338 return( PSA_ERROR_NOT_SUPPORTED );
339 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
340 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
341 return( PSA_ERROR_INVALID_SIGNATURE );
342 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
343 return( PSA_ERROR_INSUFFICIENT_MEMORY );
344 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
345 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300346
Gilles Peskinee59236f2018-01-27 23:32:46 +0100347 default:
348 return( PSA_ERROR_UNKNOWN_ERROR );
349 }
350}
351
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200352
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200353
354
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100355/****************************************************************/
356/* Key management */
357/****************************************************************/
358
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100359#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200360static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
361{
362 switch( grpid )
363 {
364 case MBEDTLS_ECP_DP_SECP192R1:
365 return( PSA_ECC_CURVE_SECP192R1 );
366 case MBEDTLS_ECP_DP_SECP224R1:
367 return( PSA_ECC_CURVE_SECP224R1 );
368 case MBEDTLS_ECP_DP_SECP256R1:
369 return( PSA_ECC_CURVE_SECP256R1 );
370 case MBEDTLS_ECP_DP_SECP384R1:
371 return( PSA_ECC_CURVE_SECP384R1 );
372 case MBEDTLS_ECP_DP_SECP521R1:
373 return( PSA_ECC_CURVE_SECP521R1 );
374 case MBEDTLS_ECP_DP_BP256R1:
375 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
376 case MBEDTLS_ECP_DP_BP384R1:
377 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
378 case MBEDTLS_ECP_DP_BP512R1:
379 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
380 case MBEDTLS_ECP_DP_CURVE25519:
381 return( PSA_ECC_CURVE_CURVE25519 );
382 case MBEDTLS_ECP_DP_SECP192K1:
383 return( PSA_ECC_CURVE_SECP192K1 );
384 case MBEDTLS_ECP_DP_SECP224K1:
385 return( PSA_ECC_CURVE_SECP224K1 );
386 case MBEDTLS_ECP_DP_SECP256K1:
387 return( PSA_ECC_CURVE_SECP256K1 );
388 case MBEDTLS_ECP_DP_CURVE448:
389 return( PSA_ECC_CURVE_CURVE448 );
390 default:
391 return( 0 );
392 }
393}
394
Gilles Peskine12313cd2018-06-20 00:20:32 +0200395static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
396{
397 switch( curve )
398 {
399 case PSA_ECC_CURVE_SECP192R1:
400 return( MBEDTLS_ECP_DP_SECP192R1 );
401 case PSA_ECC_CURVE_SECP224R1:
402 return( MBEDTLS_ECP_DP_SECP224R1 );
403 case PSA_ECC_CURVE_SECP256R1:
404 return( MBEDTLS_ECP_DP_SECP256R1 );
405 case PSA_ECC_CURVE_SECP384R1:
406 return( MBEDTLS_ECP_DP_SECP384R1 );
407 case PSA_ECC_CURVE_SECP521R1:
408 return( MBEDTLS_ECP_DP_SECP521R1 );
409 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
410 return( MBEDTLS_ECP_DP_BP256R1 );
411 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
412 return( MBEDTLS_ECP_DP_BP384R1 );
413 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
414 return( MBEDTLS_ECP_DP_BP512R1 );
415 case PSA_ECC_CURVE_CURVE25519:
416 return( MBEDTLS_ECP_DP_CURVE25519 );
417 case PSA_ECC_CURVE_SECP192K1:
418 return( MBEDTLS_ECP_DP_SECP192K1 );
419 case PSA_ECC_CURVE_SECP224K1:
420 return( MBEDTLS_ECP_DP_SECP224K1 );
421 case PSA_ECC_CURVE_SECP256K1:
422 return( MBEDTLS_ECP_DP_SECP256K1 );
423 case PSA_ECC_CURVE_CURVE448:
424 return( MBEDTLS_ECP_DP_CURVE448 );
425 default:
426 return( MBEDTLS_ECP_DP_NONE );
427 }
428}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100429#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200430
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200431static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
432 size_t bits,
433 struct raw_data *raw )
434{
435 /* Check that the bit size is acceptable for the key type */
436 switch( type )
437 {
438 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200439 if( bits == 0 )
440 {
441 raw->bytes = 0;
442 raw->data = NULL;
443 return( PSA_SUCCESS );
444 }
445 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446#if defined(MBEDTLS_MD_C)
447 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200448#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200449 case PSA_KEY_TYPE_DERIVE:
450 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200451#if defined(MBEDTLS_AES_C)
452 case PSA_KEY_TYPE_AES:
453 if( bits != 128 && bits != 192 && bits != 256 )
454 return( PSA_ERROR_INVALID_ARGUMENT );
455 break;
456#endif
457#if defined(MBEDTLS_CAMELLIA_C)
458 case PSA_KEY_TYPE_CAMELLIA:
459 if( bits != 128 && bits != 192 && bits != 256 )
460 return( PSA_ERROR_INVALID_ARGUMENT );
461 break;
462#endif
463#if defined(MBEDTLS_DES_C)
464 case PSA_KEY_TYPE_DES:
465 if( bits != 64 && bits != 128 && bits != 192 )
466 return( PSA_ERROR_INVALID_ARGUMENT );
467 break;
468#endif
469#if defined(MBEDTLS_ARC4_C)
470 case PSA_KEY_TYPE_ARC4:
471 if( bits < 8 || bits > 2048 )
472 return( PSA_ERROR_INVALID_ARGUMENT );
473 break;
474#endif
475 default:
476 return( PSA_ERROR_NOT_SUPPORTED );
477 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200478 if( bits % 8 != 0 )
479 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200480
481 /* Allocate memory for the key */
482 raw->bytes = PSA_BITS_TO_BYTES( bits );
483 raw->data = mbedtls_calloc( 1, raw->bytes );
484 if( raw->data == NULL )
485 {
486 raw->bytes = 0;
487 return( PSA_ERROR_INSUFFICIENT_MEMORY );
488 }
489 return( PSA_SUCCESS );
490}
491
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200492#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100493/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
494 * that are not a multiple of 8) well. For example, there is only
495 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
496 * way to return the exact bit size of a key.
497 * To keep things simple, reject non-byte-aligned key sizes. */
498static psa_status_t psa_check_rsa_key_byte_aligned(
499 const mbedtls_rsa_context *rsa )
500{
501 mbedtls_mpi n;
502 psa_status_t status;
503 mbedtls_mpi_init( &n );
504 status = mbedtls_to_psa_error(
505 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
506 if( status == PSA_SUCCESS )
507 {
508 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
509 status = PSA_ERROR_NOT_SUPPORTED;
510 }
511 mbedtls_mpi_free( &n );
512 return( status );
513}
514
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200515static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
516 mbedtls_rsa_context **p_rsa )
517{
518 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
519 return( PSA_ERROR_INVALID_ARGUMENT );
520 else
521 {
522 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100523 /* The size of an RSA key doesn't have to be a multiple of 8.
524 * Mbed TLS supports non-byte-aligned key sizes, but not well.
525 * For example, mbedtls_rsa_get_len() returns the key size in
526 * bytes, not in bits. */
527 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100528 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200529 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
530 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100531 status = psa_check_rsa_key_byte_aligned( rsa );
532 if( status != PSA_SUCCESS )
533 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534 *p_rsa = rsa;
535 return( PSA_SUCCESS );
536 }
537}
538#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
539
540#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100541/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200542static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
543 mbedtls_pk_context *pk,
544 mbedtls_ecp_keypair **p_ecp )
545{
546 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
547 return( PSA_ERROR_INVALID_ARGUMENT );
548 else
549 {
550 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
551 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
552 if( actual_curve != expected_curve )
553 return( PSA_ERROR_INVALID_ARGUMENT );
554 *p_ecp = ecp;
555 return( PSA_SUCCESS );
556 }
557}
558#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
559
Gilles Peskinef76aa772018-10-29 19:24:33 +0100560#if defined(MBEDTLS_ECP_C)
561/* Import a private key given as a byte string which is the private value
562 * in big-endian order. */
563static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
564 const uint8_t *data,
565 size_t data_length,
566 mbedtls_ecp_keypair **p_ecp )
567{
568 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
569 mbedtls_ecp_keypair *ecp = NULL;
570 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
571
572 *p_ecp = NULL;
573 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
574 if( ecp == NULL )
575 return( PSA_ERROR_INSUFFICIENT_MEMORY );
576
577 /* Load the group. */
578 status = mbedtls_to_psa_error(
579 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
580 if( status != PSA_SUCCESS )
581 goto exit;
582 /* Load the secret value. */
583 status = mbedtls_to_psa_error(
584 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
585 if( status != PSA_SUCCESS )
586 goto exit;
587 /* Validate the private key. */
588 status = mbedtls_to_psa_error(
589 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
590 if( status != PSA_SUCCESS )
591 goto exit;
592 /* Calculate the public key from the private key. */
593 status = mbedtls_to_psa_error(
594 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
595 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
596 if( status != PSA_SUCCESS )
597 goto exit;
598
599 *p_ecp = ecp;
600 return( PSA_SUCCESS );
601
602exit:
603 if( ecp != NULL )
604 {
605 mbedtls_ecp_keypair_free( ecp );
606 mbedtls_free( ecp );
607 }
608 return( status );
609}
610#endif /* defined(MBEDTLS_ECP_C) */
611
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100612/** Import key data into a slot. `slot->type` must have been set
613 * previously. This function assumes that the slot does not contain
614 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100615psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
616 const uint8_t *data,
617 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200619 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100620
Darryl Green940d72c2018-07-13 13:18:51 +0100621 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100622 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100623 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624 if( data_length > SIZE_MAX / 8 )
625 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100626 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200627 PSA_BYTES_TO_BITS( data_length ),
628 &slot->data.raw );
629 if( status != PSA_SUCCESS )
630 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200631 if( data_length != 0 )
632 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100633 }
634 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100635#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100636 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100637 {
Darryl Green940d72c2018-07-13 13:18:51 +0100638 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100639 data, data_length,
640 &slot->data.ecp );
641 if( status != PSA_SUCCESS )
642 return( status );
643 }
644 else
645#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100646#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100647 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
648 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649 {
650 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100651 mbedtls_pk_context pk;
652 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200653
654 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100655 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100656 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
657 else
658 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659 if( ret != 0 )
660 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200661
662 /* We have something that the pkparse module recognizes.
663 * If it has the expected type and passes any type-specific
664 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100665#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100666 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200667 status = psa_import_rsa_key( &pk, &slot->data.rsa );
668 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100669#endif /* MBEDTLS_RSA_C */
670#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100671 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
672 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200673 &pk, &slot->data.ecp );
674 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100675#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200676 {
677 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200678 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200679
Gilles Peskinec648d692018-06-28 08:46:13 +0200680 /* Free the content of the pk object only on error. On success,
681 * the content of the object has been stored in the slot. */
682 if( status != PSA_SUCCESS )
683 {
684 mbedtls_pk_free( &pk );
685 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100686 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687 }
688 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100689#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690 {
691 return( PSA_ERROR_NOT_SUPPORTED );
692 }
Darryl Green940d72c2018-07-13 13:18:51 +0100693 return( PSA_SUCCESS );
694}
695
Darryl Green06fd18d2018-07-16 11:21:11 +0100696/* Retrieve an empty key slot (slot with no key data, but possibly
697 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100698static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100699 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100700{
701 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100702 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100703
704 *p_slot = NULL;
705
Gilles Peskinec5487a82018-12-03 18:08:14 +0100706 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100707 if( status != PSA_SUCCESS )
708 return( status );
709
710 if( slot->type != PSA_KEY_TYPE_NONE )
711 return( PSA_ERROR_OCCUPIED_SLOT );
712
713 *p_slot = slot;
714 return( status );
715}
716
717/** Retrieve a slot which must contain a key. The key must have allow all the
718 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
719 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100720static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100721 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100722 psa_key_usage_t usage,
723 psa_algorithm_t alg )
724{
725 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100726 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100727
728 *p_slot = NULL;
729
Gilles Peskinec5487a82018-12-03 18:08:14 +0100730 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100731 if( status != PSA_SUCCESS )
732 return( status );
733 if( slot->type == PSA_KEY_TYPE_NONE )
734 return( PSA_ERROR_EMPTY_SLOT );
735
736 /* Enforce that usage policy for the key slot contains all the flags
737 * required by the usage parameter. There is one exception: public
738 * keys can always be exported, so we treat public key objects as
739 * if they had the export flag. */
740 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
741 usage &= ~PSA_KEY_USAGE_EXPORT;
742 if( ( slot->policy.usage & usage ) != usage )
743 return( PSA_ERROR_NOT_PERMITTED );
744 if( alg != 0 && ( alg != slot->policy.alg ) )
745 return( PSA_ERROR_NOT_PERMITTED );
746
747 *p_slot = slot;
748 return( PSA_SUCCESS );
749}
Darryl Green940d72c2018-07-13 13:18:51 +0100750
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100751/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100752static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000753{
754 if( slot->type == PSA_KEY_TYPE_NONE )
755 {
756 /* No key material to clean. */
757 }
758 else if( key_type_is_raw_bytes( slot->type ) )
759 {
760 mbedtls_free( slot->data.raw.data );
761 }
762 else
763#if defined(MBEDTLS_RSA_C)
764 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
765 {
766 mbedtls_rsa_free( slot->data.rsa );
767 mbedtls_free( slot->data.rsa );
768 }
769 else
770#endif /* defined(MBEDTLS_RSA_C) */
771#if defined(MBEDTLS_ECP_C)
772 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
773 {
774 mbedtls_ecp_keypair_free( slot->data.ecp );
775 mbedtls_free( slot->data.ecp );
776 }
777 else
778#endif /* defined(MBEDTLS_ECP_C) */
779 {
780 /* Shouldn't happen: the key type is not any type that we
781 * put in. */
782 return( PSA_ERROR_TAMPERING_DETECTED );
783 }
784
785 return( PSA_SUCCESS );
786}
787
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100788/** Completely wipe a slot in memory, including its policy.
789 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100790psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100791{
792 psa_status_t status = psa_remove_key_data_from_memory( slot );
793 /* At this point, key material and other type-specific content has
794 * been wiped. Clear remaining metadata. We can call memset and not
795 * zeroize because the metadata is not particularly sensitive. */
796 memset( slot, 0, sizeof( *slot ) );
797 return( status );
798}
799
Gilles Peskinec5487a82018-12-03 18:08:14 +0100800psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100801 psa_key_type_t type,
802 const uint8_t *data,
803 size_t data_length )
804{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100805 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100806 psa_status_t status;
807
Gilles Peskinec5487a82018-12-03 18:08:14 +0100808 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100809 if( status != PSA_SUCCESS )
810 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811
812 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100813
814 status = psa_import_key_into_slot( slot, data, data_length );
815 if( status != PSA_SUCCESS )
816 {
817 slot->type = PSA_KEY_TYPE_NONE;
818 return( status );
819 }
820
Darryl Greend49a4992018-06-18 17:27:26 +0100821#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
822 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
823 {
824 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100825 status = psa_save_persistent_key( slot->persistent_storage_id,
826 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100827 data_length );
828 if( status != PSA_SUCCESS )
829 {
830 (void) psa_remove_key_data_from_memory( slot );
831 slot->type = PSA_KEY_TYPE_NONE;
832 }
833 }
834#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
835
836 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837}
838
Gilles Peskinec5487a82018-12-03 18:08:14 +0100839psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100841 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100842 psa_status_t status = PSA_SUCCESS;
843 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100844
Gilles Peskinec5487a82018-12-03 18:08:14 +0100845 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200846 if( status != PSA_SUCCESS )
847 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100848#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
849 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
850 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100851 storage_status =
852 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100853 }
854#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100855 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100856 if( status != PSA_SUCCESS )
857 return( status );
858 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859}
860
Gilles Peskineb870b182018-07-06 16:02:09 +0200861/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100862static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200863{
864 if( key_type_is_raw_bytes( slot->type ) )
865 return( slot->data.raw.bytes * 8 );
866#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200867 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100868 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200869#endif /* defined(MBEDTLS_RSA_C) */
870#if defined(MBEDTLS_ECP_C)
871 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
872 return( slot->data.ecp->grp.pbits );
873#endif /* defined(MBEDTLS_ECP_C) */
874 /* Shouldn't happen except on an empty slot. */
875 return( 0 );
876}
877
Gilles Peskinec5487a82018-12-03 18:08:14 +0100878psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200879 psa_key_type_t *type,
880 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100881{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100882 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200883 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100884
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200886 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887 if( bits != NULL )
888 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100889 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200890 if( status != PSA_SUCCESS )
891 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200892
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100893 if( slot->type == PSA_KEY_TYPE_NONE )
894 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200895 if( type != NULL )
896 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200897 if( bits != NULL )
898 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100899 return( PSA_SUCCESS );
900}
901
Gilles Peskine2f060a82018-12-04 17:12:32 +0100902static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200903 uint8_t *data,
904 size_t data_size,
905 size_t *data_length,
906 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100908 *data_length = 0;
909
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200910 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300911 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300912
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200913 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100914 {
915 if( slot->data.raw.bytes > data_size )
916 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100917 if( data_size != 0 )
918 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200919 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100920 memset( data + slot->data.raw.bytes, 0,
921 data_size - slot->data.raw.bytes );
922 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100923 *data_length = slot->data.raw.bytes;
924 return( PSA_SUCCESS );
925 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100926#if defined(MBEDTLS_ECP_C)
927 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
928 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000929 psa_status_t status;
930
Gilles Peskine188c71e2018-10-29 19:26:02 +0100931 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
932 if( bytes > data_size )
933 return( PSA_ERROR_BUFFER_TOO_SMALL );
934 status = mbedtls_to_psa_error(
935 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
936 if( status != PSA_SUCCESS )
937 return( status );
938 memset( data + bytes, 0, data_size - bytes );
939 *data_length = bytes;
940 return( PSA_SUCCESS );
941 }
942#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943 else
Moran Peker17e36e12018-05-02 12:55:20 +0300944 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100945#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200946 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300947 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100948 {
Moran Pekera998bc62018-04-16 18:16:20 +0300949 mbedtls_pk_context pk;
950 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200951 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300952 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100953#if defined(MBEDTLS_RSA_C)
954 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300955 pk.pk_info = &mbedtls_rsa_info;
956 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100957#else
958 return( PSA_ERROR_NOT_SUPPORTED );
959#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300960 }
961 else
962 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100963#if defined(MBEDTLS_ECP_C)
964 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300965 pk.pk_info = &mbedtls_eckey_info;
966 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100967#else
968 return( PSA_ERROR_NOT_SUPPORTED );
969#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300970 }
Moran Pekerd7326592018-05-29 16:56:39 +0300971 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300972 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300973 else
974 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300975 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200976 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200977 /* If data_size is 0 then data may be NULL and then the
978 * call to memset would have undefined behavior. */
979 if( data_size != 0 )
980 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300981 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200982 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200983 /* The mbedtls_pk_xxx functions write to the end of the buffer.
984 * Move the data to the beginning and erase remaining data
985 * at the original location. */
986 if( 2 * (size_t) ret <= data_size )
987 {
988 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200989 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200990 }
991 else if( (size_t) ret < data_size )
992 {
993 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200994 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200995 }
Moran Pekera998bc62018-04-16 18:16:20 +0300996 *data_length = ret;
997 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100998 }
999 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001000#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001001 {
1002 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001003 it is valid for a special-purpose implementation to omit
1004 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001005 return( PSA_ERROR_NOT_SUPPORTED );
1006 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007 }
1008}
1009
Gilles Peskinec5487a82018-12-03 18:08:14 +01001010psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001011 uint8_t *data,
1012 size_t data_size,
1013 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001014{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001015 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001016 psa_status_t status;
1017
1018 /* Set the key to empty now, so that even when there are errors, we always
1019 * set data_length to a value between 0 and data_size. On error, setting
1020 * the key to empty is a good choice because an empty key representation is
1021 * unlikely to be accepted anywhere. */
1022 *data_length = 0;
1023
1024 /* Export requires the EXPORT flag. There is an exception for public keys,
1025 * which don't require any flag, but psa_get_key_from_slot takes
1026 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001027 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001028 if( status != PSA_SUCCESS )
1029 return( status );
1030 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001031 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001032}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033
Gilles Peskinec5487a82018-12-03 18:08:14 +01001034psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001035 uint8_t *data,
1036 size_t data_size,
1037 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001038{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001039 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001040 psa_status_t status;
1041
1042 /* Set the key to empty now, so that even when there are errors, we always
1043 * set data_length to a value between 0 and data_size. On error, setting
1044 * the key to empty is a good choice because an empty key representation is
1045 * unlikely to be accepted anywhere. */
1046 *data_length = 0;
1047
1048 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001049 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001050 if( status != PSA_SUCCESS )
1051 return( status );
1052 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001053 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001054}
1055
Darryl Green0c6575a2018-11-07 16:05:30 +00001056#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001057static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001058 size_t bits )
1059{
1060 psa_status_t status;
1061 uint8_t *data;
1062 size_t key_length;
1063 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1064 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001065 if( data == NULL )
1066 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001067 /* Get key data in export format */
1068 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1069 if( status != PSA_SUCCESS )
1070 {
1071 slot->type = PSA_KEY_TYPE_NONE;
1072 goto exit;
1073 }
1074 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001075 status = psa_save_persistent_key( slot->persistent_storage_id,
1076 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001077 data, key_length );
1078 if( status != PSA_SUCCESS )
1079 {
1080 slot->type = PSA_KEY_TYPE_NONE;
1081 }
1082exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001083 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001084 mbedtls_free( data );
1085 return( status );
1086}
1087#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1088
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001089
1090
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001092/* Message digests */
1093/****************************************************************/
1094
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001095static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001096{
1097 switch( alg )
1098 {
1099#if defined(MBEDTLS_MD2_C)
1100 case PSA_ALG_MD2:
1101 return( &mbedtls_md2_info );
1102#endif
1103#if defined(MBEDTLS_MD4_C)
1104 case PSA_ALG_MD4:
1105 return( &mbedtls_md4_info );
1106#endif
1107#if defined(MBEDTLS_MD5_C)
1108 case PSA_ALG_MD5:
1109 return( &mbedtls_md5_info );
1110#endif
1111#if defined(MBEDTLS_RIPEMD160_C)
1112 case PSA_ALG_RIPEMD160:
1113 return( &mbedtls_ripemd160_info );
1114#endif
1115#if defined(MBEDTLS_SHA1_C)
1116 case PSA_ALG_SHA_1:
1117 return( &mbedtls_sha1_info );
1118#endif
1119#if defined(MBEDTLS_SHA256_C)
1120 case PSA_ALG_SHA_224:
1121 return( &mbedtls_sha224_info );
1122 case PSA_ALG_SHA_256:
1123 return( &mbedtls_sha256_info );
1124#endif
1125#if defined(MBEDTLS_SHA512_C)
1126 case PSA_ALG_SHA_384:
1127 return( &mbedtls_sha384_info );
1128 case PSA_ALG_SHA_512:
1129 return( &mbedtls_sha512_info );
1130#endif
1131 default:
1132 return( NULL );
1133 }
1134}
1135
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001136psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1137{
1138 switch( operation->alg )
1139 {
Gilles Peskine81736312018-06-26 15:04:31 +02001140 case 0:
1141 /* The object has (apparently) been initialized but it is not
1142 * in use. It's ok to call abort on such an object, and there's
1143 * nothing to do. */
1144 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001145#if defined(MBEDTLS_MD2_C)
1146 case PSA_ALG_MD2:
1147 mbedtls_md2_free( &operation->ctx.md2 );
1148 break;
1149#endif
1150#if defined(MBEDTLS_MD4_C)
1151 case PSA_ALG_MD4:
1152 mbedtls_md4_free( &operation->ctx.md4 );
1153 break;
1154#endif
1155#if defined(MBEDTLS_MD5_C)
1156 case PSA_ALG_MD5:
1157 mbedtls_md5_free( &operation->ctx.md5 );
1158 break;
1159#endif
1160#if defined(MBEDTLS_RIPEMD160_C)
1161 case PSA_ALG_RIPEMD160:
1162 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1163 break;
1164#endif
1165#if defined(MBEDTLS_SHA1_C)
1166 case PSA_ALG_SHA_1:
1167 mbedtls_sha1_free( &operation->ctx.sha1 );
1168 break;
1169#endif
1170#if defined(MBEDTLS_SHA256_C)
1171 case PSA_ALG_SHA_224:
1172 case PSA_ALG_SHA_256:
1173 mbedtls_sha256_free( &operation->ctx.sha256 );
1174 break;
1175#endif
1176#if defined(MBEDTLS_SHA512_C)
1177 case PSA_ALG_SHA_384:
1178 case PSA_ALG_SHA_512:
1179 mbedtls_sha512_free( &operation->ctx.sha512 );
1180 break;
1181#endif
1182 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001183 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001184 }
1185 operation->alg = 0;
1186 return( PSA_SUCCESS );
1187}
1188
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001189psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001190 psa_algorithm_t alg )
1191{
1192 int ret;
1193 operation->alg = 0;
1194 switch( alg )
1195 {
1196#if defined(MBEDTLS_MD2_C)
1197 case PSA_ALG_MD2:
1198 mbedtls_md2_init( &operation->ctx.md2 );
1199 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1200 break;
1201#endif
1202#if defined(MBEDTLS_MD4_C)
1203 case PSA_ALG_MD4:
1204 mbedtls_md4_init( &operation->ctx.md4 );
1205 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1206 break;
1207#endif
1208#if defined(MBEDTLS_MD5_C)
1209 case PSA_ALG_MD5:
1210 mbedtls_md5_init( &operation->ctx.md5 );
1211 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1212 break;
1213#endif
1214#if defined(MBEDTLS_RIPEMD160_C)
1215 case PSA_ALG_RIPEMD160:
1216 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1217 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1218 break;
1219#endif
1220#if defined(MBEDTLS_SHA1_C)
1221 case PSA_ALG_SHA_1:
1222 mbedtls_sha1_init( &operation->ctx.sha1 );
1223 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1224 break;
1225#endif
1226#if defined(MBEDTLS_SHA256_C)
1227 case PSA_ALG_SHA_224:
1228 mbedtls_sha256_init( &operation->ctx.sha256 );
1229 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1230 break;
1231 case PSA_ALG_SHA_256:
1232 mbedtls_sha256_init( &operation->ctx.sha256 );
1233 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1234 break;
1235#endif
1236#if defined(MBEDTLS_SHA512_C)
1237 case PSA_ALG_SHA_384:
1238 mbedtls_sha512_init( &operation->ctx.sha512 );
1239 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1240 break;
1241 case PSA_ALG_SHA_512:
1242 mbedtls_sha512_init( &operation->ctx.sha512 );
1243 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1244 break;
1245#endif
1246 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001247 return( PSA_ALG_IS_HASH( alg ) ?
1248 PSA_ERROR_NOT_SUPPORTED :
1249 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001250 }
1251 if( ret == 0 )
1252 operation->alg = alg;
1253 else
1254 psa_hash_abort( operation );
1255 return( mbedtls_to_psa_error( ret ) );
1256}
1257
1258psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1259 const uint8_t *input,
1260 size_t input_length )
1261{
1262 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001263
1264 /* Don't require hash implementations to behave correctly on a
1265 * zero-length input, which may have an invalid pointer. */
1266 if( input_length == 0 )
1267 return( PSA_SUCCESS );
1268
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001269 switch( operation->alg )
1270 {
1271#if defined(MBEDTLS_MD2_C)
1272 case PSA_ALG_MD2:
1273 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1274 input, input_length );
1275 break;
1276#endif
1277#if defined(MBEDTLS_MD4_C)
1278 case PSA_ALG_MD4:
1279 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1280 input, input_length );
1281 break;
1282#endif
1283#if defined(MBEDTLS_MD5_C)
1284 case PSA_ALG_MD5:
1285 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1286 input, input_length );
1287 break;
1288#endif
1289#if defined(MBEDTLS_RIPEMD160_C)
1290 case PSA_ALG_RIPEMD160:
1291 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1292 input, input_length );
1293 break;
1294#endif
1295#if defined(MBEDTLS_SHA1_C)
1296 case PSA_ALG_SHA_1:
1297 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1298 input, input_length );
1299 break;
1300#endif
1301#if defined(MBEDTLS_SHA256_C)
1302 case PSA_ALG_SHA_224:
1303 case PSA_ALG_SHA_256:
1304 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1305 input, input_length );
1306 break;
1307#endif
1308#if defined(MBEDTLS_SHA512_C)
1309 case PSA_ALG_SHA_384:
1310 case PSA_ALG_SHA_512:
1311 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1312 input, input_length );
1313 break;
1314#endif
1315 default:
1316 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1317 break;
1318 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001319
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001320 if( ret != 0 )
1321 psa_hash_abort( operation );
1322 return( mbedtls_to_psa_error( ret ) );
1323}
1324
1325psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1326 uint8_t *hash,
1327 size_t hash_size,
1328 size_t *hash_length )
1329{
itayzafrir40835d42018-08-02 13:14:17 +03001330 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001331 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001332 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001333
1334 /* Fill the output buffer with something that isn't a valid hash
1335 * (barring an attack on the hash and deliberately-crafted input),
1336 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001337 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001338 /* If hash_size is 0 then hash may be NULL and then the
1339 * call to memset would have undefined behavior. */
1340 if( hash_size != 0 )
1341 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001342
1343 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001344 {
1345 status = PSA_ERROR_BUFFER_TOO_SMALL;
1346 goto exit;
1347 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001348
1349 switch( operation->alg )
1350 {
1351#if defined(MBEDTLS_MD2_C)
1352 case PSA_ALG_MD2:
1353 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1354 break;
1355#endif
1356#if defined(MBEDTLS_MD4_C)
1357 case PSA_ALG_MD4:
1358 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1359 break;
1360#endif
1361#if defined(MBEDTLS_MD5_C)
1362 case PSA_ALG_MD5:
1363 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1364 break;
1365#endif
1366#if defined(MBEDTLS_RIPEMD160_C)
1367 case PSA_ALG_RIPEMD160:
1368 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1369 break;
1370#endif
1371#if defined(MBEDTLS_SHA1_C)
1372 case PSA_ALG_SHA_1:
1373 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1374 break;
1375#endif
1376#if defined(MBEDTLS_SHA256_C)
1377 case PSA_ALG_SHA_224:
1378 case PSA_ALG_SHA_256:
1379 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1380 break;
1381#endif
1382#if defined(MBEDTLS_SHA512_C)
1383 case PSA_ALG_SHA_384:
1384 case PSA_ALG_SHA_512:
1385 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1386 break;
1387#endif
1388 default:
1389 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1390 break;
1391 }
itayzafrir40835d42018-08-02 13:14:17 +03001392 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001393
itayzafrir40835d42018-08-02 13:14:17 +03001394exit:
1395 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001396 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001397 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001398 return( psa_hash_abort( operation ) );
1399 }
1400 else
1401 {
1402 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001403 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001404 }
1405}
1406
Gilles Peskine2d277862018-06-18 15:41:12 +02001407psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1408 const uint8_t *hash,
1409 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001410{
1411 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1412 size_t actual_hash_length;
1413 psa_status_t status = psa_hash_finish( operation,
1414 actual_hash, sizeof( actual_hash ),
1415 &actual_hash_length );
1416 if( status != PSA_SUCCESS )
1417 return( status );
1418 if( actual_hash_length != hash_length )
1419 return( PSA_ERROR_INVALID_SIGNATURE );
1420 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1421 return( PSA_ERROR_INVALID_SIGNATURE );
1422 return( PSA_SUCCESS );
1423}
1424
1425
1426
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001427/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001428/* MAC */
1429/****************************************************************/
1430
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001431static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001432 psa_algorithm_t alg,
1433 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001434 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001435 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001436{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001437 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001438 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001439
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001440 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001441 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001442
Gilles Peskine8c9def32018-02-08 10:02:12 +01001443 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1444 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001445 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001446 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001447 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001448 mode = MBEDTLS_MODE_STREAM;
1449 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001450 case PSA_ALG_CTR:
1451 mode = MBEDTLS_MODE_CTR;
1452 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001453 case PSA_ALG_CFB:
1454 mode = MBEDTLS_MODE_CFB;
1455 break;
1456 case PSA_ALG_OFB:
1457 mode = MBEDTLS_MODE_OFB;
1458 break;
1459 case PSA_ALG_CBC_NO_PADDING:
1460 mode = MBEDTLS_MODE_CBC;
1461 break;
1462 case PSA_ALG_CBC_PKCS7:
1463 mode = MBEDTLS_MODE_CBC;
1464 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001465 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001466 mode = MBEDTLS_MODE_CCM;
1467 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001468 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001469 mode = MBEDTLS_MODE_GCM;
1470 break;
1471 default:
1472 return( NULL );
1473 }
1474 }
1475 else if( alg == PSA_ALG_CMAC )
1476 mode = MBEDTLS_MODE_ECB;
1477 else if( alg == PSA_ALG_GMAC )
1478 mode = MBEDTLS_MODE_GCM;
1479 else
1480 return( NULL );
1481
1482 switch( key_type )
1483 {
1484 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001485 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001486 break;
1487 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001488 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1489 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001490 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001491 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001492 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001493 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001494 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1495 * but two-key Triple-DES is functionally three-key Triple-DES
1496 * with K1=K3, so that's how we present it to mbedtls. */
1497 if( key_bits == 128 )
1498 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001499 break;
1500 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001501 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001502 break;
1503 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001504 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001505 break;
1506 default:
1507 return( NULL );
1508 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001509 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001510 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001511
Jaeden Amero23bbb752018-06-26 14:16:54 +01001512 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1513 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001514}
1515
Gilles Peskinea05219c2018-11-16 16:02:56 +01001516#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001517static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001518{
Gilles Peskine2d277862018-06-18 15:41:12 +02001519 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001520 {
1521 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001522 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001523 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001524 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001525 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001526 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001527 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001528 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001529 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001530 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001531 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001532 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001533 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001534 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001535 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001536 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001537 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001538 return( 128 );
1539 default:
1540 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001541 }
1542}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001543#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001544
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001545/* Initialize the MAC operation structure. Once this function has been
1546 * called, psa_mac_abort can run and will do the right thing. */
1547static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1548 psa_algorithm_t alg )
1549{
1550 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1551
1552 operation->alg = alg;
1553 operation->key_set = 0;
1554 operation->iv_set = 0;
1555 operation->iv_required = 0;
1556 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001557 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001558
1559#if defined(MBEDTLS_CMAC_C)
1560 if( alg == PSA_ALG_CMAC )
1561 {
1562 operation->iv_required = 0;
1563 mbedtls_cipher_init( &operation->ctx.cmac );
1564 status = PSA_SUCCESS;
1565 }
1566 else
1567#endif /* MBEDTLS_CMAC_C */
1568#if defined(MBEDTLS_MD_C)
1569 if( PSA_ALG_IS_HMAC( operation->alg ) )
1570 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001571 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1572 operation->ctx.hmac.hash_ctx.alg = 0;
1573 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001574 }
1575 else
1576#endif /* MBEDTLS_MD_C */
1577 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001578 if( ! PSA_ALG_IS_MAC( alg ) )
1579 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001580 }
1581
1582 if( status != PSA_SUCCESS )
1583 memset( operation, 0, sizeof( *operation ) );
1584 return( status );
1585}
1586
Gilles Peskine01126fa2018-07-12 17:04:55 +02001587#if defined(MBEDTLS_MD_C)
1588static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1589{
Gilles Peskine3f108122018-12-07 18:14:53 +01001590 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001591 return( psa_hash_abort( &hmac->hash_ctx ) );
1592}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001593
1594static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1595{
1596 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1597 memset( hmac, 0, sizeof( *hmac ) );
1598}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001599#endif /* MBEDTLS_MD_C */
1600
Gilles Peskine8c9def32018-02-08 10:02:12 +01001601psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1602{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001603 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001604 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001605 /* The object has (apparently) been initialized but it is not
1606 * in use. It's ok to call abort on such an object, and there's
1607 * nothing to do. */
1608 return( PSA_SUCCESS );
1609 }
1610 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001611#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001612 if( operation->alg == PSA_ALG_CMAC )
1613 {
1614 mbedtls_cipher_free( &operation->ctx.cmac );
1615 }
1616 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001617#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001618#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001619 if( PSA_ALG_IS_HMAC( operation->alg ) )
1620 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001621 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001622 }
1623 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001625 {
1626 /* Sanity check (shouldn't happen: operation->alg should
1627 * always have been initialized to a valid value). */
1628 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001629 }
Moran Peker41deec42018-04-04 15:43:05 +03001630
Gilles Peskine8c9def32018-02-08 10:02:12 +01001631 operation->alg = 0;
1632 operation->key_set = 0;
1633 operation->iv_set = 0;
1634 operation->iv_required = 0;
1635 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001636 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001637
Gilles Peskine8c9def32018-02-08 10:02:12 +01001638 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001639
1640bad_state:
1641 /* If abort is called on an uninitialized object, we can't trust
1642 * anything. Wipe the object in case it contains confidential data.
1643 * This may result in a memory leak if a pointer gets overwritten,
1644 * but it's too late to do anything about this. */
1645 memset( operation, 0, sizeof( *operation ) );
1646 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001647}
1648
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001649#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001650static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001651 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001652 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001653 const mbedtls_cipher_info_t *cipher_info )
1654{
1655 int ret;
1656
1657 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001658
1659 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1660 if( ret != 0 )
1661 return( ret );
1662
1663 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1664 slot->data.raw.data,
1665 key_bits );
1666 return( ret );
1667}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001668#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001669
Gilles Peskine248051a2018-06-20 16:09:38 +02001670#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001671static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1672 const uint8_t *key,
1673 size_t key_length,
1674 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001675{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001676 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001677 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001678 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001679 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001680 psa_status_t status;
1681
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001682 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1683 * overflow below. This should never trigger if the hash algorithm
1684 * is implemented correctly. */
1685 /* The size checks against the ipad and opad buffers cannot be written
1686 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1687 * because that triggers -Wlogical-op on GCC 7.3. */
1688 if( block_size > sizeof( ipad ) )
1689 return( PSA_ERROR_NOT_SUPPORTED );
1690 if( block_size > sizeof( hmac->opad ) )
1691 return( PSA_ERROR_NOT_SUPPORTED );
1692 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001693 return( PSA_ERROR_NOT_SUPPORTED );
1694
Gilles Peskined223b522018-06-11 18:12:58 +02001695 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001696 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001697 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001698 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001699 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001700 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001701 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001702 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001703 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001704 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001705 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001706 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001707 }
Gilles Peskine96889972018-07-12 17:07:03 +02001708 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1709 * but it is permitted. It is common when HMAC is used in HKDF, for
1710 * example. Don't call `memcpy` in the 0-length because `key` could be
1711 * an invalid pointer which would make the behavior undefined. */
1712 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001713 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001714
Gilles Peskined223b522018-06-11 18:12:58 +02001715 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1716 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001717 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001718 ipad[i] ^= 0x36;
1719 memset( ipad + key_length, 0x36, block_size - key_length );
1720
1721 /* Copy the key material from ipad to opad, flipping the requisite bits,
1722 * and filling the rest of opad with the requisite constant. */
1723 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001724 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1725 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001726
Gilles Peskine01126fa2018-07-12 17:04:55 +02001727 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001728 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001729 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001730
Gilles Peskine01126fa2018-07-12 17:04:55 +02001731 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001732
1733cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001734 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001735
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001736 return( status );
1737}
Gilles Peskine248051a2018-06-20 16:09:38 +02001738#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001739
Gilles Peskine89167cb2018-07-08 20:12:23 +02001740static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001741 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001742 psa_algorithm_t alg,
1743 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001744{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001745 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001746 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001747 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001748 psa_key_usage_t usage =
1749 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001750 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001751 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001752
Gilles Peskined911eb72018-08-14 15:18:45 +02001753 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001754 if( status != PSA_SUCCESS )
1755 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001756 if( is_sign )
1757 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001758
Gilles Peskinec5487a82018-12-03 18:08:14 +01001759 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001760 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001761 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001762 key_bits = psa_get_key_bits( slot );
1763
Gilles Peskine8c9def32018-02-08 10:02:12 +01001764#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001765 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001766 {
1767 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001768 mbedtls_cipher_info_from_psa( full_length_alg,
1769 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001770 int ret;
1771 if( cipher_info == NULL )
1772 {
1773 status = PSA_ERROR_NOT_SUPPORTED;
1774 goto exit;
1775 }
1776 operation->mac_size = cipher_info->block_size;
1777 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1778 status = mbedtls_to_psa_error( ret );
1779 }
1780 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001781#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001782#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001783 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001784 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001785 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001786 if( hash_alg == 0 )
1787 {
1788 status = PSA_ERROR_NOT_SUPPORTED;
1789 goto exit;
1790 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001791
1792 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1793 /* Sanity check. This shouldn't fail on a valid configuration. */
1794 if( operation->mac_size == 0 ||
1795 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1796 {
1797 status = PSA_ERROR_NOT_SUPPORTED;
1798 goto exit;
1799 }
1800
Gilles Peskine01126fa2018-07-12 17:04:55 +02001801 if( slot->type != PSA_KEY_TYPE_HMAC )
1802 {
1803 status = PSA_ERROR_INVALID_ARGUMENT;
1804 goto exit;
1805 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001806
Gilles Peskine01126fa2018-07-12 17:04:55 +02001807 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1808 slot->data.raw.data,
1809 slot->data.raw.bytes,
1810 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001811 }
1812 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001813#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001814 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001815 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001816 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001817 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001818
Gilles Peskined911eb72018-08-14 15:18:45 +02001819 if( truncated == 0 )
1820 {
1821 /* The "normal" case: untruncated algorithm. Nothing to do. */
1822 }
1823 else if( truncated < 4 )
1824 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001825 /* A very short MAC is too short for security since it can be
1826 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1827 * so we make this our minimum, even though 32 bits is still
1828 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001829 status = PSA_ERROR_NOT_SUPPORTED;
1830 }
1831 else if( truncated > operation->mac_size )
1832 {
1833 /* It's impossible to "truncate" to a larger length. */
1834 status = PSA_ERROR_INVALID_ARGUMENT;
1835 }
1836 else
1837 operation->mac_size = truncated;
1838
Gilles Peskinefbfac682018-07-08 20:51:54 +02001839exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001840 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001841 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001842 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001843 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001844 else
1845 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001846 operation->key_set = 1;
1847 }
1848 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001849}
1850
Gilles Peskine89167cb2018-07-08 20:12:23 +02001851psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001852 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001853 psa_algorithm_t alg )
1854{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001855 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001856}
1857
1858psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001859 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001860 psa_algorithm_t alg )
1861{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001862 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001863}
1864
Gilles Peskine8c9def32018-02-08 10:02:12 +01001865psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1866 const uint8_t *input,
1867 size_t input_length )
1868{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001869 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001870 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001871 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001872 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001873 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001874 operation->has_input = 1;
1875
Gilles Peskine8c9def32018-02-08 10:02:12 +01001876#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001877 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001878 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001879 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1880 input, input_length );
1881 status = mbedtls_to_psa_error( ret );
1882 }
1883 else
1884#endif /* MBEDTLS_CMAC_C */
1885#if defined(MBEDTLS_MD_C)
1886 if( PSA_ALG_IS_HMAC( operation->alg ) )
1887 {
1888 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1889 input_length );
1890 }
1891 else
1892#endif /* MBEDTLS_MD_C */
1893 {
1894 /* This shouldn't happen if `operation` was initialized by
1895 * a setup function. */
1896 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001897 }
1898
Gilles Peskinefbfac682018-07-08 20:51:54 +02001899cleanup:
1900 if( status != PSA_SUCCESS )
1901 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001902 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001903}
1904
Gilles Peskine01126fa2018-07-12 17:04:55 +02001905#if defined(MBEDTLS_MD_C)
1906static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1907 uint8_t *mac,
1908 size_t mac_size )
1909{
1910 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1911 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1912 size_t hash_size = 0;
1913 size_t block_size = psa_get_hash_block_size( hash_alg );
1914 psa_status_t status;
1915
Gilles Peskine01126fa2018-07-12 17:04:55 +02001916 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1917 if( status != PSA_SUCCESS )
1918 return( status );
1919 /* From here on, tmp needs to be wiped. */
1920
1921 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1922 if( status != PSA_SUCCESS )
1923 goto exit;
1924
1925 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1926 if( status != PSA_SUCCESS )
1927 goto exit;
1928
1929 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1930 if( status != PSA_SUCCESS )
1931 goto exit;
1932
Gilles Peskined911eb72018-08-14 15:18:45 +02001933 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1934 if( status != PSA_SUCCESS )
1935 goto exit;
1936
1937 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001938
1939exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001940 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001941 return( status );
1942}
1943#endif /* MBEDTLS_MD_C */
1944
mohammad16036df908f2018-04-02 08:34:15 -07001945static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001946 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001947 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001948{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001949 if( ! operation->key_set )
1950 return( PSA_ERROR_BAD_STATE );
1951 if( operation->iv_required && ! operation->iv_set )
1952 return( PSA_ERROR_BAD_STATE );
1953
Gilles Peskine8c9def32018-02-08 10:02:12 +01001954 if( mac_size < operation->mac_size )
1955 return( PSA_ERROR_BUFFER_TOO_SMALL );
1956
Gilles Peskine8c9def32018-02-08 10:02:12 +01001957#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001958 if( operation->alg == PSA_ALG_CMAC )
1959 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001960 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1961 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1962 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02001963 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01001964 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001965 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001966 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001967 else
1968#endif /* MBEDTLS_CMAC_C */
1969#if defined(MBEDTLS_MD_C)
1970 if( PSA_ALG_IS_HMAC( operation->alg ) )
1971 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001972 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001973 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001974 }
1975 else
1976#endif /* MBEDTLS_MD_C */
1977 {
1978 /* This shouldn't happen if `operation` was initialized by
1979 * a setup function. */
1980 return( PSA_ERROR_BAD_STATE );
1981 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001982}
1983
Gilles Peskineacd4be32018-07-08 19:56:25 +02001984psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1985 uint8_t *mac,
1986 size_t mac_size,
1987 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001988{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001989 psa_status_t status;
1990
1991 /* Fill the output buffer with something that isn't a valid mac
1992 * (barring an attack on the mac and deliberately-crafted input),
1993 * in case the caller doesn't check the return status properly. */
1994 *mac_length = mac_size;
1995 /* If mac_size is 0 then mac may be NULL and then the
1996 * call to memset would have undefined behavior. */
1997 if( mac_size != 0 )
1998 memset( mac, '!', mac_size );
1999
Gilles Peskine89167cb2018-07-08 20:12:23 +02002000 if( ! operation->is_sign )
2001 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002002 status = PSA_ERROR_BAD_STATE;
2003 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002004 }
mohammad16036df908f2018-04-02 08:34:15 -07002005
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002006 status = psa_mac_finish_internal( operation, mac, mac_size );
2007
2008cleanup:
2009 if( status == PSA_SUCCESS )
2010 {
2011 status = psa_mac_abort( operation );
2012 if( status == PSA_SUCCESS )
2013 *mac_length = operation->mac_size;
2014 else
2015 memset( mac, '!', mac_size );
2016 }
2017 else
2018 psa_mac_abort( operation );
2019 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002020}
2021
Gilles Peskineacd4be32018-07-08 19:56:25 +02002022psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2023 const uint8_t *mac,
2024 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002025{
Gilles Peskine828ed142018-06-18 23:25:51 +02002026 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002027 psa_status_t status;
2028
Gilles Peskine89167cb2018-07-08 20:12:23 +02002029 if( operation->is_sign )
2030 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002031 status = PSA_ERROR_BAD_STATE;
2032 goto cleanup;
2033 }
2034 if( operation->mac_size != mac_length )
2035 {
2036 status = PSA_ERROR_INVALID_SIGNATURE;
2037 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002038 }
mohammad16036df908f2018-04-02 08:34:15 -07002039
2040 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002041 actual_mac, sizeof( actual_mac ) );
2042
2043 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2044 status = PSA_ERROR_INVALID_SIGNATURE;
2045
2046cleanup:
2047 if( status == PSA_SUCCESS )
2048 status = psa_mac_abort( operation );
2049 else
2050 psa_mac_abort( operation );
2051
Gilles Peskine3f108122018-12-07 18:14:53 +01002052 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002053
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002054 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002055}
2056
2057
Gilles Peskine20035e32018-02-03 22:44:14 +01002058
Gilles Peskine20035e32018-02-03 22:44:14 +01002059/****************************************************************/
2060/* Asymmetric cryptography */
2061/****************************************************************/
2062
Gilles Peskine2b450e32018-06-27 15:42:46 +02002063#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002064/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002065 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002066static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2067 size_t hash_length,
2068 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002069{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002070 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002071 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002072 *md_alg = mbedtls_md_get_type( md_info );
2073
2074 /* The Mbed TLS RSA module uses an unsigned int for hash length
2075 * parameters. Validate that it fits so that we don't risk an
2076 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002077#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002078 if( hash_length > UINT_MAX )
2079 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002080#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002081
2082#if defined(MBEDTLS_PKCS1_V15)
2083 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2084 * must be correct. */
2085 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2086 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002087 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002088 if( md_info == NULL )
2089 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002090 if( mbedtls_md_get_size( md_info ) != hash_length )
2091 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002092 }
2093#endif /* MBEDTLS_PKCS1_V15 */
2094
2095#if defined(MBEDTLS_PKCS1_V21)
2096 /* PSS requires a hash internally. */
2097 if( PSA_ALG_IS_RSA_PSS( alg ) )
2098 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002099 if( md_info == NULL )
2100 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002101 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002102#endif /* MBEDTLS_PKCS1_V21 */
2103
Gilles Peskine61b91d42018-06-08 16:09:36 +02002104 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002105}
2106
Gilles Peskine2b450e32018-06-27 15:42:46 +02002107static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2108 psa_algorithm_t alg,
2109 const uint8_t *hash,
2110 size_t hash_length,
2111 uint8_t *signature,
2112 size_t signature_size,
2113 size_t *signature_length )
2114{
2115 psa_status_t status;
2116 int ret;
2117 mbedtls_md_type_t md_alg;
2118
2119 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2120 if( status != PSA_SUCCESS )
2121 return( status );
2122
Gilles Peskine630a18a2018-06-29 17:49:35 +02002123 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002124 return( PSA_ERROR_BUFFER_TOO_SMALL );
2125
2126#if defined(MBEDTLS_PKCS1_V15)
2127 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2128 {
2129 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2130 MBEDTLS_MD_NONE );
2131 ret = mbedtls_rsa_pkcs1_sign( rsa,
2132 mbedtls_ctr_drbg_random,
2133 &global_data.ctr_drbg,
2134 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002135 md_alg,
2136 (unsigned int) hash_length,
2137 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002138 signature );
2139 }
2140 else
2141#endif /* MBEDTLS_PKCS1_V15 */
2142#if defined(MBEDTLS_PKCS1_V21)
2143 if( PSA_ALG_IS_RSA_PSS( alg ) )
2144 {
2145 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2146 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2147 mbedtls_ctr_drbg_random,
2148 &global_data.ctr_drbg,
2149 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002150 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002151 (unsigned int) hash_length,
2152 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002153 signature );
2154 }
2155 else
2156#endif /* MBEDTLS_PKCS1_V21 */
2157 {
2158 return( PSA_ERROR_INVALID_ARGUMENT );
2159 }
2160
2161 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002162 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002163 return( mbedtls_to_psa_error( ret ) );
2164}
2165
2166static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2167 psa_algorithm_t alg,
2168 const uint8_t *hash,
2169 size_t hash_length,
2170 const uint8_t *signature,
2171 size_t signature_length )
2172{
2173 psa_status_t status;
2174 int ret;
2175 mbedtls_md_type_t md_alg;
2176
2177 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2178 if( status != PSA_SUCCESS )
2179 return( status );
2180
Gilles Peskine630a18a2018-06-29 17:49:35 +02002181 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002182 return( PSA_ERROR_BUFFER_TOO_SMALL );
2183
2184#if defined(MBEDTLS_PKCS1_V15)
2185 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2186 {
2187 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2188 MBEDTLS_MD_NONE );
2189 ret = mbedtls_rsa_pkcs1_verify( rsa,
2190 mbedtls_ctr_drbg_random,
2191 &global_data.ctr_drbg,
2192 MBEDTLS_RSA_PUBLIC,
2193 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002194 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002195 hash,
2196 signature );
2197 }
2198 else
2199#endif /* MBEDTLS_PKCS1_V15 */
2200#if defined(MBEDTLS_PKCS1_V21)
2201 if( PSA_ALG_IS_RSA_PSS( alg ) )
2202 {
2203 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2204 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2205 mbedtls_ctr_drbg_random,
2206 &global_data.ctr_drbg,
2207 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002208 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002209 (unsigned int) hash_length,
2210 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002211 signature );
2212 }
2213 else
2214#endif /* MBEDTLS_PKCS1_V21 */
2215 {
2216 return( PSA_ERROR_INVALID_ARGUMENT );
2217 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002218
2219 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2220 * the rest of the signature is invalid". This has little use in
2221 * practice and PSA doesn't report this distinction. */
2222 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2223 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002224 return( mbedtls_to_psa_error( ret ) );
2225}
2226#endif /* MBEDTLS_RSA_C */
2227
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002228#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002229/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2230 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2231 * (even though these functions don't modify it). */
2232static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2233 psa_algorithm_t alg,
2234 const uint8_t *hash,
2235 size_t hash_length,
2236 uint8_t *signature,
2237 size_t signature_size,
2238 size_t *signature_length )
2239{
2240 int ret;
2241 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002242 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002243 mbedtls_mpi_init( &r );
2244 mbedtls_mpi_init( &s );
2245
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002246 if( signature_size < 2 * curve_bytes )
2247 {
2248 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2249 goto cleanup;
2250 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002251
Gilles Peskinea05219c2018-11-16 16:02:56 +01002252#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002253 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2254 {
2255 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2256 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2257 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2258 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2259 hash, hash_length,
2260 md_alg ) );
2261 }
2262 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002263#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002264 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002265 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002266 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2267 hash, hash_length,
2268 mbedtls_ctr_drbg_random,
2269 &global_data.ctr_drbg ) );
2270 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002271
2272 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2273 signature,
2274 curve_bytes ) );
2275 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2276 signature + curve_bytes,
2277 curve_bytes ) );
2278
2279cleanup:
2280 mbedtls_mpi_free( &r );
2281 mbedtls_mpi_free( &s );
2282 if( ret == 0 )
2283 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002284 return( mbedtls_to_psa_error( ret ) );
2285}
2286
2287static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2288 const uint8_t *hash,
2289 size_t hash_length,
2290 const uint8_t *signature,
2291 size_t signature_length )
2292{
2293 int ret;
2294 mbedtls_mpi r, s;
2295 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2296 mbedtls_mpi_init( &r );
2297 mbedtls_mpi_init( &s );
2298
2299 if( signature_length != 2 * curve_bytes )
2300 return( PSA_ERROR_INVALID_SIGNATURE );
2301
2302 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2303 signature,
2304 curve_bytes ) );
2305 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2306 signature + curve_bytes,
2307 curve_bytes ) );
2308
2309 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2310 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002311
2312cleanup:
2313 mbedtls_mpi_free( &r );
2314 mbedtls_mpi_free( &s );
2315 return( mbedtls_to_psa_error( ret ) );
2316}
2317#endif /* MBEDTLS_ECDSA_C */
2318
Gilles Peskinec5487a82018-12-03 18:08:14 +01002319psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002320 psa_algorithm_t alg,
2321 const uint8_t *hash,
2322 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002323 uint8_t *signature,
2324 size_t signature_size,
2325 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002326{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002327 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002328 psa_status_t status;
2329
2330 *signature_length = signature_size;
2331
Gilles Peskinec5487a82018-12-03 18:08:14 +01002332 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002333 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002334 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002335 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002336 {
2337 status = PSA_ERROR_INVALID_ARGUMENT;
2338 goto exit;
2339 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002340
Gilles Peskine20035e32018-02-03 22:44:14 +01002341#if defined(MBEDTLS_RSA_C)
2342 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2343 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002344 status = psa_rsa_sign( slot->data.rsa,
2345 alg,
2346 hash, hash_length,
2347 signature, signature_size,
2348 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002349 }
2350 else
2351#endif /* defined(MBEDTLS_RSA_C) */
2352#if defined(MBEDTLS_ECP_C)
2353 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2354 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002355#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002356 if(
2357#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2358 PSA_ALG_IS_ECDSA( alg )
2359#else
2360 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2361#endif
2362 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002363 status = psa_ecdsa_sign( slot->data.ecp,
2364 alg,
2365 hash, hash_length,
2366 signature, signature_size,
2367 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002368 else
2369#endif /* defined(MBEDTLS_ECDSA_C) */
2370 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002371 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002372 }
itayzafrir5c753392018-05-08 11:18:38 +03002373 }
2374 else
2375#endif /* defined(MBEDTLS_ECP_C) */
2376 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002377 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002378 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002379
2380exit:
2381 /* Fill the unused part of the output buffer (the whole buffer on error,
2382 * the trailing part on success) with something that isn't a valid mac
2383 * (barring an attack on the mac and deliberately-crafted input),
2384 * in case the caller doesn't check the return status properly. */
2385 if( status == PSA_SUCCESS )
2386 memset( signature + *signature_length, '!',
2387 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002388 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002389 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002390 /* If signature_size is 0 then we have nothing to do. We must not call
2391 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002392 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002393}
2394
Gilles Peskinec5487a82018-12-03 18:08:14 +01002395psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002396 psa_algorithm_t alg,
2397 const uint8_t *hash,
2398 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002399 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002400 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002401{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002402 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002403 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002404
Gilles Peskinec5487a82018-12-03 18:08:14 +01002405 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002406 if( status != PSA_SUCCESS )
2407 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002408
Gilles Peskine61b91d42018-06-08 16:09:36 +02002409#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002410 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002411 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002412 return( psa_rsa_verify( slot->data.rsa,
2413 alg,
2414 hash, hash_length,
2415 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002416 }
2417 else
2418#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002419#if defined(MBEDTLS_ECP_C)
2420 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2421 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002422#if defined(MBEDTLS_ECDSA_C)
2423 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002424 return( psa_ecdsa_verify( slot->data.ecp,
2425 hash, hash_length,
2426 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002427 else
2428#endif /* defined(MBEDTLS_ECDSA_C) */
2429 {
2430 return( PSA_ERROR_INVALID_ARGUMENT );
2431 }
itayzafrir5c753392018-05-08 11:18:38 +03002432 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002433 else
2434#endif /* defined(MBEDTLS_ECP_C) */
2435 {
2436 return( PSA_ERROR_NOT_SUPPORTED );
2437 }
2438}
2439
Gilles Peskine072ac562018-06-30 00:21:29 +02002440#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2441static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2442 mbedtls_rsa_context *rsa )
2443{
2444 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2445 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2446 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2447 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2448}
2449#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2450
Gilles Peskinec5487a82018-12-03 18:08:14 +01002451psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002452 psa_algorithm_t alg,
2453 const uint8_t *input,
2454 size_t input_length,
2455 const uint8_t *salt,
2456 size_t salt_length,
2457 uint8_t *output,
2458 size_t output_size,
2459 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002460{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002461 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002462 psa_status_t status;
2463
Darryl Green5cc689a2018-07-24 15:34:10 +01002464 (void) input;
2465 (void) input_length;
2466 (void) salt;
2467 (void) output;
2468 (void) output_size;
2469
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002470 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002471
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002472 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2473 return( PSA_ERROR_INVALID_ARGUMENT );
2474
Gilles Peskinec5487a82018-12-03 18:08:14 +01002475 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002476 if( status != PSA_SUCCESS )
2477 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002478 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2479 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002480 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002481
2482#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002483 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002484 {
2485 mbedtls_rsa_context *rsa = slot->data.rsa;
2486 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002487 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002488 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002489#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002490 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002491 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002492 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2493 mbedtls_ctr_drbg_random,
2494 &global_data.ctr_drbg,
2495 MBEDTLS_RSA_PUBLIC,
2496 input_length,
2497 input,
2498 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002499 }
2500 else
2501#endif /* MBEDTLS_PKCS1_V15 */
2502#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002503 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002504 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002505 psa_rsa_oaep_set_padding_mode( alg, rsa );
2506 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2507 mbedtls_ctr_drbg_random,
2508 &global_data.ctr_drbg,
2509 MBEDTLS_RSA_PUBLIC,
2510 salt, salt_length,
2511 input_length,
2512 input,
2513 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002514 }
2515 else
2516#endif /* MBEDTLS_PKCS1_V21 */
2517 {
2518 return( PSA_ERROR_INVALID_ARGUMENT );
2519 }
2520 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002521 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002522 return( mbedtls_to_psa_error( ret ) );
2523 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002524 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002525#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002526 {
2527 return( PSA_ERROR_NOT_SUPPORTED );
2528 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002529}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002530
Gilles Peskinec5487a82018-12-03 18:08:14 +01002531psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002532 psa_algorithm_t alg,
2533 const uint8_t *input,
2534 size_t input_length,
2535 const uint8_t *salt,
2536 size_t salt_length,
2537 uint8_t *output,
2538 size_t output_size,
2539 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002540{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002541 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002542 psa_status_t status;
2543
Darryl Green5cc689a2018-07-24 15:34:10 +01002544 (void) input;
2545 (void) input_length;
2546 (void) salt;
2547 (void) output;
2548 (void) output_size;
2549
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002550 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002551
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002552 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2553 return( PSA_ERROR_INVALID_ARGUMENT );
2554
Gilles Peskinec5487a82018-12-03 18:08:14 +01002555 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002556 if( status != PSA_SUCCESS )
2557 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002558 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2559 return( PSA_ERROR_INVALID_ARGUMENT );
2560
2561#if defined(MBEDTLS_RSA_C)
2562 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2563 {
2564 mbedtls_rsa_context *rsa = slot->data.rsa;
2565 int ret;
2566
Gilles Peskine630a18a2018-06-29 17:49:35 +02002567 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002568 return( PSA_ERROR_INVALID_ARGUMENT );
2569
2570#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002571 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002572 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002573 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2574 mbedtls_ctr_drbg_random,
2575 &global_data.ctr_drbg,
2576 MBEDTLS_RSA_PRIVATE,
2577 output_length,
2578 input,
2579 output,
2580 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002581 }
2582 else
2583#endif /* MBEDTLS_PKCS1_V15 */
2584#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002585 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002586 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002587 psa_rsa_oaep_set_padding_mode( alg, rsa );
2588 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2589 mbedtls_ctr_drbg_random,
2590 &global_data.ctr_drbg,
2591 MBEDTLS_RSA_PRIVATE,
2592 salt, salt_length,
2593 output_length,
2594 input,
2595 output,
2596 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002597 }
2598 else
2599#endif /* MBEDTLS_PKCS1_V21 */
2600 {
2601 return( PSA_ERROR_INVALID_ARGUMENT );
2602 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002603
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002604 return( mbedtls_to_psa_error( ret ) );
2605 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002606 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002607#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002608 {
2609 return( PSA_ERROR_NOT_SUPPORTED );
2610 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002611}
Gilles Peskine20035e32018-02-03 22:44:14 +01002612
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002613
2614
mohammad1603503973b2018-03-12 15:59:30 +02002615/****************************************************************/
2616/* Symmetric cryptography */
2617/****************************************************************/
2618
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619/* Initialize the cipher operation structure. Once this function has been
2620 * called, psa_cipher_abort can run and will do the right thing. */
2621static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2622 psa_algorithm_t alg )
2623{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002624 if( ! PSA_ALG_IS_CIPHER( alg ) )
2625 {
2626 memset( operation, 0, sizeof( *operation ) );
2627 return( PSA_ERROR_INVALID_ARGUMENT );
2628 }
2629
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002630 operation->alg = alg;
2631 operation->key_set = 0;
2632 operation->iv_set = 0;
2633 operation->iv_required = 1;
2634 operation->iv_size = 0;
2635 operation->block_size = 0;
2636 mbedtls_cipher_init( &operation->ctx.cipher );
2637 return( PSA_SUCCESS );
2638}
2639
Gilles Peskinee553c652018-06-04 16:22:46 +02002640static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002641 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002642 psa_algorithm_t alg,
2643 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002644{
2645 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2646 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002647 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002648 size_t key_bits;
2649 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002650 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2651 PSA_KEY_USAGE_ENCRYPT :
2652 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002653
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002654 status = psa_cipher_init( operation, alg );
2655 if( status != PSA_SUCCESS )
2656 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002657
Gilles Peskinec5487a82018-12-03 18:08:14 +01002658 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002659 if( status != PSA_SUCCESS )
2660 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002661 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002662
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002663 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002664 if( cipher_info == NULL )
2665 return( PSA_ERROR_NOT_SUPPORTED );
2666
mohammad1603503973b2018-03-12 15:59:30 +02002667 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002668 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002669 {
2670 psa_cipher_abort( operation );
2671 return( mbedtls_to_psa_error( ret ) );
2672 }
2673
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002674#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002675 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002676 {
2677 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2678 unsigned char keys[24];
2679 memcpy( keys, slot->data.raw.data, 16 );
2680 memcpy( keys + 16, slot->data.raw.data, 8 );
2681 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2682 keys,
2683 192, cipher_operation );
2684 }
2685 else
2686#endif
2687 {
2688 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2689 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002690 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002691 }
Moran Peker41deec42018-04-04 15:43:05 +03002692 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002693 {
2694 psa_cipher_abort( operation );
2695 return( mbedtls_to_psa_error( ret ) );
2696 }
2697
mohammad16038481e742018-03-18 13:57:31 +02002698#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002699 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002700 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002701 case PSA_ALG_CBC_NO_PADDING:
2702 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2703 MBEDTLS_PADDING_NONE );
2704 break;
2705 case PSA_ALG_CBC_PKCS7:
2706 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2707 MBEDTLS_PADDING_PKCS7 );
2708 break;
2709 default:
2710 /* The algorithm doesn't involve padding. */
2711 ret = 0;
2712 break;
2713 }
2714 if( ret != 0 )
2715 {
2716 psa_cipher_abort( operation );
2717 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002718 }
2719#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2720
mohammad1603503973b2018-03-12 15:59:30 +02002721 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002722 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2723 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2724 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002725 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002726 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002727 }
mohammad1603503973b2018-03-12 15:59:30 +02002728
Moran Peker395db872018-05-31 14:07:14 +03002729 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002730}
2731
Gilles Peskinefe119512018-07-08 21:39:34 +02002732psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002733 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002734 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002735{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002736 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002737}
2738
Gilles Peskinefe119512018-07-08 21:39:34 +02002739psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002740 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002741 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002742{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002743 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002744}
2745
Gilles Peskinefe119512018-07-08 21:39:34 +02002746psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2747 unsigned char *iv,
2748 size_t iv_size,
2749 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002750{
itayzafrir534bd7c2018-08-02 13:56:32 +03002751 psa_status_t status;
2752 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002753 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002754 {
2755 status = PSA_ERROR_BAD_STATE;
2756 goto exit;
2757 }
Moran Peker41deec42018-04-04 15:43:05 +03002758 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002759 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002760 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002761 goto exit;
2762 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002763 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2764 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002765 if( ret != 0 )
2766 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002767 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002768 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002769 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002770
mohammad16038481e742018-03-18 13:57:31 +02002771 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002772 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002773
Moran Peker395db872018-05-31 14:07:14 +03002774exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002775 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002776 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002777 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002778}
2779
Gilles Peskinefe119512018-07-08 21:39:34 +02002780psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2781 const unsigned char *iv,
2782 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002783{
itayzafrir534bd7c2018-08-02 13:56:32 +03002784 psa_status_t status;
2785 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002786 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002787 {
2788 status = PSA_ERROR_BAD_STATE;
2789 goto exit;
2790 }
Moran Pekera28258c2018-05-29 16:25:04 +03002791 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002792 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002793 status = PSA_ERROR_INVALID_ARGUMENT;
2794 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002795 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002796 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2797 status = mbedtls_to_psa_error( ret );
2798exit:
2799 if( status == PSA_SUCCESS )
2800 operation->iv_set = 1;
2801 else
mohammad1603503973b2018-03-12 15:59:30 +02002802 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002803 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002804}
2805
Gilles Peskinee553c652018-06-04 16:22:46 +02002806psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2807 const uint8_t *input,
2808 size_t input_length,
2809 unsigned char *output,
2810 size_t output_size,
2811 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002812{
itayzafrir534bd7c2018-08-02 13:56:32 +03002813 psa_status_t status;
2814 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002815 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002816 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002817 {
2818 /* Take the unprocessed partial block left over from previous
2819 * update calls, if any, plus the input to this call. Remove
2820 * the last partial block, if any. You get the data that will be
2821 * output in this call. */
2822 expected_output_size =
2823 ( operation->ctx.cipher.unprocessed_len + input_length )
2824 / operation->block_size * operation->block_size;
2825 }
2826 else
2827 {
2828 expected_output_size = input_length;
2829 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002830
Gilles Peskine89d789c2018-06-04 17:17:16 +02002831 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002832 {
2833 status = PSA_ERROR_BUFFER_TOO_SMALL;
2834 goto exit;
2835 }
mohammad160382759612018-03-12 18:16:40 +02002836
mohammad1603503973b2018-03-12 15:59:30 +02002837 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002838 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002839 status = mbedtls_to_psa_error( ret );
2840exit:
2841 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002842 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002843 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002844}
2845
Gilles Peskinee553c652018-06-04 16:22:46 +02002846psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2847 uint8_t *output,
2848 size_t output_size,
2849 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002850{
Janos Follath315b51c2018-07-09 16:04:51 +01002851 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2852 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002853 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002854
mohammad1603503973b2018-03-12 15:59:30 +02002855 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002856 {
Janos Follath315b51c2018-07-09 16:04:51 +01002857 status = PSA_ERROR_BAD_STATE;
2858 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002859 }
2860 if( operation->iv_required && ! operation->iv_set )
2861 {
Janos Follath315b51c2018-07-09 16:04:51 +01002862 status = PSA_ERROR_BAD_STATE;
2863 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002864 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002865
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002866 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002867 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2868 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002869 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002870 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002871 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002872 }
2873
Janos Follath315b51c2018-07-09 16:04:51 +01002874 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2875 temp_output_buffer,
2876 output_length );
2877 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002878 {
Janos Follath315b51c2018-07-09 16:04:51 +01002879 status = mbedtls_to_psa_error( cipher_ret );
2880 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002881 }
Janos Follath315b51c2018-07-09 16:04:51 +01002882
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002883 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002884 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002885 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002886 memcpy( output, temp_output_buffer, *output_length );
2887 else
2888 {
Janos Follath315b51c2018-07-09 16:04:51 +01002889 status = PSA_ERROR_BUFFER_TOO_SMALL;
2890 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002891 }
mohammad1603503973b2018-03-12 15:59:30 +02002892
Gilles Peskine3f108122018-12-07 18:14:53 +01002893 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002894 status = psa_cipher_abort( operation );
2895
2896 return( status );
2897
2898error:
2899
2900 *output_length = 0;
2901
Gilles Peskine3f108122018-12-07 18:14:53 +01002902 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002903 (void) psa_cipher_abort( operation );
2904
2905 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002906}
2907
Gilles Peskinee553c652018-06-04 16:22:46 +02002908psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2909{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002910 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002911 {
2912 /* The object has (apparently) been initialized but it is not
2913 * in use. It's ok to call abort on such an object, and there's
2914 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002915 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002916 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002917
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002918 /* Sanity check (shouldn't happen: operation->alg should
2919 * always have been initialized to a valid value). */
2920 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2921 return( PSA_ERROR_BAD_STATE );
2922
mohammad1603503973b2018-03-12 15:59:30 +02002923 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002924
Moran Peker41deec42018-04-04 15:43:05 +03002925 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002926 operation->key_set = 0;
2927 operation->iv_set = 0;
2928 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002929 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002930 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002931
Moran Peker395db872018-05-31 14:07:14 +03002932 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002933}
2934
Gilles Peskinea0655c32018-04-30 17:06:50 +02002935
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002936
mohammad16038cc1cee2018-03-28 01:21:33 +03002937/****************************************************************/
2938/* Key Policy */
2939/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002940
mohammad160327010052018-07-03 13:16:15 +03002941#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002942void psa_key_policy_set_usage( psa_key_policy_t *policy,
2943 psa_key_usage_t usage,
2944 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002945{
mohammad16034eed7572018-03-28 05:14:59 -07002946 policy->usage = usage;
2947 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002948}
2949
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002950psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002951{
mohammad16036df908f2018-04-02 08:34:15 -07002952 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002953}
2954
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002955psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002956{
mohammad16036df908f2018-04-02 08:34:15 -07002957 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002958}
mohammad160327010052018-07-03 13:16:15 +03002959#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002960
Gilles Peskinec5487a82018-12-03 18:08:14 +01002961psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02002962 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002963{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002964 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002965 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002966
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002967 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002968 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002969
Gilles Peskinec5487a82018-12-03 18:08:14 +01002970 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002971 if( status != PSA_SUCCESS )
2972 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002973
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002974 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2975 PSA_KEY_USAGE_ENCRYPT |
2976 PSA_KEY_USAGE_DECRYPT |
2977 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002978 PSA_KEY_USAGE_VERIFY |
2979 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002980 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002981
mohammad16036df908f2018-04-02 08:34:15 -07002982 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002983
2984 return( PSA_SUCCESS );
2985}
2986
Gilles Peskinec5487a82018-12-03 18:08:14 +01002987psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02002988 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002989{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002990 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002991 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002992
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002993 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002994 return( PSA_ERROR_INVALID_ARGUMENT );
2995
Gilles Peskinec5487a82018-12-03 18:08:14 +01002996 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002997 if( status != PSA_SUCCESS )
2998 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002999
mohammad16036df908f2018-04-02 08:34:15 -07003000 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003001
3002 return( PSA_SUCCESS );
3003}
Gilles Peskine20035e32018-02-03 22:44:14 +01003004
Gilles Peskinea0655c32018-04-30 17:06:50 +02003005
3006
mohammad1603804cd712018-03-20 22:44:08 +02003007/****************************************************************/
3008/* Key Lifetime */
3009/****************************************************************/
3010
Gilles Peskinec5487a82018-12-03 18:08:14 +01003011psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003012 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003013{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003014 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003015 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003016
Gilles Peskinec5487a82018-12-03 18:08:14 +01003017 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003018 if( status != PSA_SUCCESS )
3019 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003020
mohammad1603804cd712018-03-20 22:44:08 +02003021 *lifetime = slot->lifetime;
3022
3023 return( PSA_SUCCESS );
3024}
3025
Gilles Peskine20035e32018-02-03 22:44:14 +01003026
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003027
mohammad16035955c982018-04-26 00:53:03 +03003028/****************************************************************/
3029/* AEAD */
3030/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003031
Gilles Peskineedf9a652018-08-17 18:11:56 +02003032typedef struct
3033{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003034 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003035 const mbedtls_cipher_info_t *cipher_info;
3036 union
3037 {
3038#if defined(MBEDTLS_CCM_C)
3039 mbedtls_ccm_context ccm;
3040#endif /* MBEDTLS_CCM_C */
3041#if defined(MBEDTLS_GCM_C)
3042 mbedtls_gcm_context gcm;
3043#endif /* MBEDTLS_GCM_C */
3044 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003045 psa_algorithm_t core_alg;
3046 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003047 uint8_t tag_length;
3048} aead_operation_t;
3049
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003050static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003051{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003052 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003053 {
3054#if defined(MBEDTLS_CCM_C)
3055 case PSA_ALG_CCM:
3056 mbedtls_ccm_free( &operation->ctx.ccm );
3057 break;
3058#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003059#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003060 case PSA_ALG_GCM:
3061 mbedtls_gcm_free( &operation->ctx.gcm );
3062 break;
3063#endif /* MBEDTLS_GCM_C */
3064 }
3065}
3066
3067static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003068 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003069 psa_key_usage_t usage,
3070 psa_algorithm_t alg )
3071{
3072 psa_status_t status;
3073 size_t key_bits;
3074 mbedtls_cipher_id_t cipher_id;
3075
Gilles Peskinec5487a82018-12-03 18:08:14 +01003076 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003077 if( status != PSA_SUCCESS )
3078 return( status );
3079
3080 key_bits = psa_get_key_bits( operation->slot );
3081
3082 operation->cipher_info =
3083 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3084 &cipher_id );
3085 if( operation->cipher_info == NULL )
3086 return( PSA_ERROR_NOT_SUPPORTED );
3087
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003088 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003089 {
3090#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003091 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3092 operation->core_alg = PSA_ALG_CCM;
3093 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003094 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3095 return( PSA_ERROR_INVALID_ARGUMENT );
3096 mbedtls_ccm_init( &operation->ctx.ccm );
3097 status = mbedtls_to_psa_error(
3098 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3099 operation->slot->data.raw.data,
3100 (unsigned int) key_bits ) );
3101 if( status != 0 )
3102 goto cleanup;
3103 break;
3104#endif /* MBEDTLS_CCM_C */
3105
3106#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003107 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3108 operation->core_alg = PSA_ALG_GCM;
3109 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003110 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3111 return( PSA_ERROR_INVALID_ARGUMENT );
3112 mbedtls_gcm_init( &operation->ctx.gcm );
3113 status = mbedtls_to_psa_error(
3114 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3115 operation->slot->data.raw.data,
3116 (unsigned int) key_bits ) );
3117 break;
3118#endif /* MBEDTLS_GCM_C */
3119
3120 default:
3121 return( PSA_ERROR_NOT_SUPPORTED );
3122 }
3123
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003124 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3125 {
3126 status = PSA_ERROR_INVALID_ARGUMENT;
3127 goto cleanup;
3128 }
3129 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3130 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3131 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3132 * In both cases, mbedtls_xxx will validate the tag length below. */
3133
Gilles Peskineedf9a652018-08-17 18:11:56 +02003134 return( PSA_SUCCESS );
3135
3136cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003137 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003138 return( status );
3139}
3140
Gilles Peskinec5487a82018-12-03 18:08:14 +01003141psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003142 psa_algorithm_t alg,
3143 const uint8_t *nonce,
3144 size_t nonce_length,
3145 const uint8_t *additional_data,
3146 size_t additional_data_length,
3147 const uint8_t *plaintext,
3148 size_t plaintext_length,
3149 uint8_t *ciphertext,
3150 size_t ciphertext_size,
3151 size_t *ciphertext_length )
3152{
mohammad16035955c982018-04-26 00:53:03 +03003153 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003154 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003155 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003156
mohammad1603f08a5502018-06-03 15:05:47 +03003157 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003158
Gilles Peskinec5487a82018-12-03 18:08:14 +01003159 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003160 if( status != PSA_SUCCESS )
3161 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003162
Gilles Peskineedf9a652018-08-17 18:11:56 +02003163 /* For all currently supported modes, the tag is at the end of the
3164 * ciphertext. */
3165 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3166 {
3167 status = PSA_ERROR_BUFFER_TOO_SMALL;
3168 goto exit;
3169 }
3170 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003171
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003172#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003173 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003174 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003175 status = mbedtls_to_psa_error(
3176 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3177 MBEDTLS_GCM_ENCRYPT,
3178 plaintext_length,
3179 nonce, nonce_length,
3180 additional_data, additional_data_length,
3181 plaintext, ciphertext,
3182 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003183 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003184 else
3185#endif /* MBEDTLS_GCM_C */
3186#if defined(MBEDTLS_CCM_C)
3187 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003188 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003189 status = mbedtls_to_psa_error(
3190 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3191 plaintext_length,
3192 nonce, nonce_length,
3193 additional_data,
3194 additional_data_length,
3195 plaintext, ciphertext,
3196 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003197 }
mohammad16035c8845f2018-05-09 05:40:09 -07003198 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003199#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003200 {
mohammad1603554faad2018-06-03 15:07:38 +03003201 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003202 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003203
Gilles Peskineedf9a652018-08-17 18:11:56 +02003204 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3205 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003206
Gilles Peskineedf9a652018-08-17 18:11:56 +02003207exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003208 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003209 if( status == PSA_SUCCESS )
3210 *ciphertext_length = plaintext_length + operation.tag_length;
3211 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003212}
3213
Gilles Peskineee652a32018-06-01 19:23:52 +02003214/* Locate the tag in a ciphertext buffer containing the encrypted data
3215 * followed by the tag. Return the length of the part preceding the tag in
3216 * *plaintext_length. This is the size of the plaintext in modes where
3217 * the encrypted data has the same size as the plaintext, such as
3218 * CCM and GCM. */
3219static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3220 const uint8_t *ciphertext,
3221 size_t ciphertext_length,
3222 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003223 const uint8_t **p_tag )
3224{
3225 size_t payload_length;
3226 if( tag_length > ciphertext_length )
3227 return( PSA_ERROR_INVALID_ARGUMENT );
3228 payload_length = ciphertext_length - tag_length;
3229 if( payload_length > plaintext_size )
3230 return( PSA_ERROR_BUFFER_TOO_SMALL );
3231 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003232 return( PSA_SUCCESS );
3233}
3234
Gilles Peskinec5487a82018-12-03 18:08:14 +01003235psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003236 psa_algorithm_t alg,
3237 const uint8_t *nonce,
3238 size_t nonce_length,
3239 const uint8_t *additional_data,
3240 size_t additional_data_length,
3241 const uint8_t *ciphertext,
3242 size_t ciphertext_length,
3243 uint8_t *plaintext,
3244 size_t plaintext_size,
3245 size_t *plaintext_length )
3246{
mohammad16035955c982018-04-26 00:53:03 +03003247 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003248 aead_operation_t operation;
3249 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003250
Gilles Peskineee652a32018-06-01 19:23:52 +02003251 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003252
Gilles Peskinec5487a82018-12-03 18:08:14 +01003253 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003254 if( status != PSA_SUCCESS )
3255 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003256
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003257#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003258 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003259 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003260 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003261 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003262 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003263 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003264 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003265
Gilles Peskineedf9a652018-08-17 18:11:56 +02003266 status = mbedtls_to_psa_error(
3267 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3268 ciphertext_length - operation.tag_length,
3269 nonce, nonce_length,
3270 additional_data,
3271 additional_data_length,
3272 tag, operation.tag_length,
3273 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003274 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003275 else
3276#endif /* MBEDTLS_GCM_C */
3277#if defined(MBEDTLS_CCM_C)
3278 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003279 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003280 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003281 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003282 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003283 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003284 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003285
Gilles Peskineedf9a652018-08-17 18:11:56 +02003286 status = mbedtls_to_psa_error(
3287 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3288 ciphertext_length - operation.tag_length,
3289 nonce, nonce_length,
3290 additional_data,
3291 additional_data_length,
3292 ciphertext, plaintext,
3293 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003294 }
mohammad160339574652018-06-01 04:39:53 -07003295 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003296#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003297 {
mohammad1603554faad2018-06-03 15:07:38 +03003298 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003299 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003300
Gilles Peskineedf9a652018-08-17 18:11:56 +02003301 if( status != PSA_SUCCESS && plaintext_size != 0 )
3302 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003303
Gilles Peskineedf9a652018-08-17 18:11:56 +02003304exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003305 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003306 if( status == PSA_SUCCESS )
3307 *plaintext_length = ciphertext_length - operation.tag_length;
3308 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003309}
3310
Gilles Peskinea0655c32018-04-30 17:06:50 +02003311
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003312
Gilles Peskine20035e32018-02-03 22:44:14 +01003313/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003314/* Generators */
3315/****************************************************************/
3316
3317psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3318{
3319 psa_status_t status = PSA_SUCCESS;
3320 if( generator->alg == 0 )
3321 {
3322 /* The object has (apparently) been initialized but it is not
3323 * in use. It's ok to call abort on such an object, and there's
3324 * nothing to do. */
3325 }
3326 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003327 if( generator->alg == PSA_ALG_SELECT_RAW )
3328 {
3329 if( generator->ctx.buffer.data != NULL )
3330 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003331 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003332 generator->ctx.buffer.size );
3333 mbedtls_free( generator->ctx.buffer.data );
3334 }
3335 }
3336 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003337#if defined(MBEDTLS_MD_C)
3338 if( PSA_ALG_IS_HKDF( generator->alg ) )
3339 {
3340 mbedtls_free( generator->ctx.hkdf.info );
3341 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3342 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003343 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3344 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3345 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003346 {
3347 if( generator->ctx.tls12_prf.key != NULL )
3348 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003349 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003350 generator->ctx.tls12_prf.key_len );
3351 mbedtls_free( generator->ctx.tls12_prf.key );
3352 }
Hanno Becker580fba12018-11-13 20:50:45 +00003353
3354 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3355 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003356 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003357 generator->ctx.tls12_prf.Ai_with_seed_len );
3358 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3359 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003360 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003361 else
3362#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003363 {
3364 status = PSA_ERROR_BAD_STATE;
3365 }
3366 memset( generator, 0, sizeof( *generator ) );
3367 return( status );
3368}
3369
3370
3371psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3372 size_t *capacity)
3373{
3374 *capacity = generator->capacity;
3375 return( PSA_SUCCESS );
3376}
3377
Gilles Peskinebef7f142018-07-12 17:22:21 +02003378#if defined(MBEDTLS_MD_C)
3379/* Read some bytes from an HKDF-based generator. This performs a chunk
3380 * of the expand phase of the HKDF algorithm. */
3381static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3382 psa_algorithm_t hash_alg,
3383 uint8_t *output,
3384 size_t output_length )
3385{
3386 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3387 psa_status_t status;
3388
3389 while( output_length != 0 )
3390 {
3391 /* Copy what remains of the current block */
3392 uint8_t n = hash_length - hkdf->offset_in_block;
3393 if( n > output_length )
3394 n = (uint8_t) output_length;
3395 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3396 output += n;
3397 output_length -= n;
3398 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003399 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003400 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003401 /* We can't be wanting more output after block 0xff, otherwise
3402 * the capacity check in psa_generator_read() would have
3403 * prevented this call. It could happen only if the generator
3404 * object was corrupted or if this function is called directly
3405 * inside the library. */
3406 if( hkdf->block_number == 0xff )
3407 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003408
3409 /* We need a new block */
3410 ++hkdf->block_number;
3411 hkdf->offset_in_block = 0;
3412 status = psa_hmac_setup_internal( &hkdf->hmac,
3413 hkdf->prk, hash_length,
3414 hash_alg );
3415 if( status != PSA_SUCCESS )
3416 return( status );
3417 if( hkdf->block_number != 1 )
3418 {
3419 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3420 hkdf->output_block,
3421 hash_length );
3422 if( status != PSA_SUCCESS )
3423 return( status );
3424 }
3425 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3426 hkdf->info,
3427 hkdf->info_length );
3428 if( status != PSA_SUCCESS )
3429 return( status );
3430 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3431 &hkdf->block_number, 1 );
3432 if( status != PSA_SUCCESS )
3433 return( status );
3434 status = psa_hmac_finish_internal( &hkdf->hmac,
3435 hkdf->output_block,
3436 sizeof( hkdf->output_block ) );
3437 if( status != PSA_SUCCESS )
3438 return( status );
3439 }
3440
3441 return( PSA_SUCCESS );
3442}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003443
3444static psa_status_t psa_generator_tls12_prf_generate_next_block(
3445 psa_tls12_prf_generator_t *tls12_prf,
3446 psa_algorithm_t alg )
3447{
3448 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3449 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3450 psa_hmac_internal_data hmac;
3451 psa_status_t status, cleanup_status;
3452
Hanno Becker3b339e22018-11-13 20:56:14 +00003453 unsigned char *Ai;
3454 size_t Ai_len;
3455
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003456 /* We can't be wanting more output after block 0xff, otherwise
3457 * the capacity check in psa_generator_read() would have
3458 * prevented this call. It could happen only if the generator
3459 * object was corrupted or if this function is called directly
3460 * inside the library. */
3461 if( tls12_prf->block_number == 0xff )
3462 return( PSA_ERROR_BAD_STATE );
3463
3464 /* We need a new block */
3465 ++tls12_prf->block_number;
3466 tls12_prf->offset_in_block = 0;
3467
3468 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3469 *
3470 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3471 *
3472 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3473 * HMAC_hash(secret, A(2) + seed) +
3474 * HMAC_hash(secret, A(3) + seed) + ...
3475 *
3476 * A(0) = seed
3477 * A(i) = HMAC_hash( secret, A(i-1) )
3478 *
3479 * The `psa_tls12_prf_generator` structures saves the block
3480 * `HMAC_hash(secret, A(i) + seed)` from which the output
3481 * is currently extracted as `output_block`, while
3482 * `A(i) + seed` is stored in `Ai_with_seed`.
3483 *
3484 * Generating a new block means recalculating `Ai_with_seed`
3485 * from the A(i)-part of it, and afterwards recalculating
3486 * `output_block`.
3487 *
3488 * A(0) is computed at setup time.
3489 *
3490 */
3491
3492 psa_hmac_init_internal( &hmac );
3493
3494 /* We must distinguish the calculation of A(1) from those
3495 * of A(2) and higher, because A(0)=seed has a different
3496 * length than the other A(i). */
3497 if( tls12_prf->block_number == 1 )
3498 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003499 Ai = tls12_prf->Ai_with_seed + hash_length;
3500 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003501 }
3502 else
3503 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003504 Ai = tls12_prf->Ai_with_seed;
3505 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003506 }
3507
Hanno Becker3b339e22018-11-13 20:56:14 +00003508 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3509 status = psa_hmac_setup_internal( &hmac,
3510 tls12_prf->key,
3511 tls12_prf->key_len,
3512 hash_alg );
3513 if( status != PSA_SUCCESS )
3514 goto cleanup;
3515
3516 status = psa_hash_update( &hmac.hash_ctx,
3517 Ai, Ai_len );
3518 if( status != PSA_SUCCESS )
3519 goto cleanup;
3520
3521 status = psa_hmac_finish_internal( &hmac,
3522 tls12_prf->Ai_with_seed,
3523 hash_length );
3524 if( status != PSA_SUCCESS )
3525 goto cleanup;
3526
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003527 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3528 status = psa_hmac_setup_internal( &hmac,
3529 tls12_prf->key,
3530 tls12_prf->key_len,
3531 hash_alg );
3532 if( status != PSA_SUCCESS )
3533 goto cleanup;
3534
3535 status = psa_hash_update( &hmac.hash_ctx,
3536 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003537 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003538 if( status != PSA_SUCCESS )
3539 goto cleanup;
3540
3541 status = psa_hmac_finish_internal( &hmac,
3542 tls12_prf->output_block,
3543 hash_length );
3544 if( status != PSA_SUCCESS )
3545 goto cleanup;
3546
3547cleanup:
3548
3549 cleanup_status = psa_hmac_abort_internal( &hmac );
3550 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3551 status = cleanup_status;
3552
3553 return( status );
3554}
3555
3556/* Read some bytes from an TLS-1.2-PRF-based generator.
3557 * See Section 5 of RFC 5246. */
3558static psa_status_t psa_generator_tls12_prf_read(
3559 psa_tls12_prf_generator_t *tls12_prf,
3560 psa_algorithm_t alg,
3561 uint8_t *output,
3562 size_t output_length )
3563{
3564 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3565 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3566 psa_status_t status;
3567
3568 while( output_length != 0 )
3569 {
3570 /* Copy what remains of the current block */
3571 uint8_t n = hash_length - tls12_prf->offset_in_block;
3572
3573 /* Check if we have fully processed the current block. */
3574 if( n == 0 )
3575 {
3576 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3577 alg );
3578 if( status != PSA_SUCCESS )
3579 return( status );
3580
3581 continue;
3582 }
3583
3584 if( n > output_length )
3585 n = (uint8_t) output_length;
3586 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3587 n );
3588 output += n;
3589 output_length -= n;
3590 tls12_prf->offset_in_block += n;
3591 }
3592
3593 return( PSA_SUCCESS );
3594}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003595#endif /* MBEDTLS_MD_C */
3596
Gilles Peskineeab56e42018-07-12 17:12:33 +02003597psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3598 uint8_t *output,
3599 size_t output_length )
3600{
3601 psa_status_t status;
3602
3603 if( output_length > generator->capacity )
3604 {
3605 generator->capacity = 0;
3606 /* Go through the error path to wipe all confidential data now
3607 * that the generator object is useless. */
3608 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3609 goto exit;
3610 }
3611 if( output_length == 0 &&
3612 generator->capacity == 0 && generator->alg == 0 )
3613 {
3614 /* Edge case: this is a blank or finished generator, and 0
3615 * bytes were requested. The right error in this case could
3616 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3617 * INSUFFICIENT_CAPACITY, which is right for a finished
3618 * generator, for consistency with the case when
3619 * output_length > 0. */
3620 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3621 }
3622 generator->capacity -= output_length;
3623
Gilles Peskine751d9652018-09-18 12:05:44 +02003624 if( generator->alg == PSA_ALG_SELECT_RAW )
3625 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003626 /* Initially, the capacity of a selection generator is always
3627 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3628 * abbreviated in this comment as `size`. When the remaining
3629 * capacity is `c`, the next bytes to serve start `c` bytes
3630 * from the end of the buffer, i.e. `size - c` from the
3631 * beginning of the buffer. Since `generator->capacity` was just
3632 * decremented above, we need to serve the bytes from
3633 * `size - generator->capacity - output_length` to
3634 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003635 size_t offset =
3636 generator->ctx.buffer.size - generator->capacity - output_length;
3637 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3638 status = PSA_SUCCESS;
3639 }
3640 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003641#if defined(MBEDTLS_MD_C)
3642 if( PSA_ALG_IS_HKDF( generator->alg ) )
3643 {
3644 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3645 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3646 output, output_length );
3647 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003648 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3649 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003650 {
3651 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3652 generator->alg, output,
3653 output_length );
3654 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003655 else
3656#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003657 {
3658 return( PSA_ERROR_BAD_STATE );
3659 }
3660
3661exit:
3662 if( status != PSA_SUCCESS )
3663 {
3664 psa_generator_abort( generator );
3665 memset( output, '!', output_length );
3666 }
3667 return( status );
3668}
3669
Gilles Peskine08542d82018-07-19 17:05:42 +02003670#if defined(MBEDTLS_DES_C)
3671static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3672{
3673 if( data_size >= 8 )
3674 mbedtls_des_key_set_parity( data );
3675 if( data_size >= 16 )
3676 mbedtls_des_key_set_parity( data + 8 );
3677 if( data_size >= 24 )
3678 mbedtls_des_key_set_parity( data + 16 );
3679}
3680#endif /* MBEDTLS_DES_C */
3681
Gilles Peskinec5487a82018-12-03 18:08:14 +01003682psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003683 psa_key_type_t type,
3684 size_t bits,
3685 psa_crypto_generator_t *generator )
3686{
3687 uint8_t *data = NULL;
3688 size_t bytes = PSA_BITS_TO_BYTES( bits );
3689 psa_status_t status;
3690
3691 if( ! key_type_is_raw_bytes( type ) )
3692 return( PSA_ERROR_INVALID_ARGUMENT );
3693 if( bits % 8 != 0 )
3694 return( PSA_ERROR_INVALID_ARGUMENT );
3695 data = mbedtls_calloc( 1, bytes );
3696 if( data == NULL )
3697 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3698
3699 status = psa_generator_read( generator, data, bytes );
3700 if( status != PSA_SUCCESS )
3701 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003702#if defined(MBEDTLS_DES_C)
3703 if( type == PSA_KEY_TYPE_DES )
3704 psa_des_set_key_parity( data, bytes );
3705#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003706 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003707
3708exit:
3709 mbedtls_free( data );
3710 return( status );
3711}
3712
3713
3714
3715/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003716/* Key derivation */
3717/****************************************************************/
3718
Gilles Peskinea05219c2018-11-16 16:02:56 +01003719#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003720/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003721 * of the HKDF algorithm.
3722 *
3723 * Note that if this function fails, you must call psa_generator_abort()
3724 * to potentially free embedded data structures and wipe confidential data.
3725 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003726static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003727 const uint8_t *secret,
3728 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003729 psa_algorithm_t hash_alg,
3730 const uint8_t *salt,
3731 size_t salt_length,
3732 const uint8_t *label,
3733 size_t label_length )
3734{
3735 psa_status_t status;
3736 status = psa_hmac_setup_internal( &hkdf->hmac,
3737 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003738 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003739 if( status != PSA_SUCCESS )
3740 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003741 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003742 if( status != PSA_SUCCESS )
3743 return( status );
3744 status = psa_hmac_finish_internal( &hkdf->hmac,
3745 hkdf->prk,
3746 sizeof( hkdf->prk ) );
3747 if( status != PSA_SUCCESS )
3748 return( status );
3749 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3750 hkdf->block_number = 0;
3751 hkdf->info_length = label_length;
3752 if( label_length != 0 )
3753 {
3754 hkdf->info = mbedtls_calloc( 1, label_length );
3755 if( hkdf->info == NULL )
3756 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3757 memcpy( hkdf->info, label, label_length );
3758 }
3759 return( PSA_SUCCESS );
3760}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003761#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003762
Gilles Peskinea05219c2018-11-16 16:02:56 +01003763#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003764/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3765 *
3766 * Note that if this function fails, you must call psa_generator_abort()
3767 * to potentially free embedded data structures and wipe confidential data.
3768 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003769static psa_status_t psa_generator_tls12_prf_setup(
3770 psa_tls12_prf_generator_t *tls12_prf,
3771 const unsigned char *key,
3772 size_t key_len,
3773 psa_algorithm_t hash_alg,
3774 const uint8_t *salt,
3775 size_t salt_length,
3776 const uint8_t *label,
3777 size_t label_length )
3778{
3779 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003780 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3781 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003782
3783 tls12_prf->key = mbedtls_calloc( 1, key_len );
3784 if( tls12_prf->key == NULL )
3785 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3786 tls12_prf->key_len = key_len;
3787 memcpy( tls12_prf->key, key, key_len );
3788
Hanno Becker580fba12018-11-13 20:50:45 +00003789 overflow = ( salt_length + label_length < salt_length ) ||
3790 ( salt_length + label_length + hash_length < hash_length );
3791 if( overflow )
3792 return( PSA_ERROR_INVALID_ARGUMENT );
3793
3794 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3795 if( tls12_prf->Ai_with_seed == NULL )
3796 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3797 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3798
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003799 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3800 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003801 if( label_length != 0 )
3802 {
3803 memcpy( tls12_prf->Ai_with_seed + hash_length,
3804 label, label_length );
3805 }
3806
3807 if( salt_length != 0 )
3808 {
3809 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3810 salt, salt_length );
3811 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003812
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003813 /* The first block gets generated when
3814 * psa_generator_read() is called. */
3815 tls12_prf->block_number = 0;
3816 tls12_prf->offset_in_block = hash_length;
3817
3818 return( PSA_SUCCESS );
3819}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003820
3821/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3822static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3823 psa_tls12_prf_generator_t *tls12_prf,
3824 const unsigned char *psk,
3825 size_t psk_len,
3826 psa_algorithm_t hash_alg,
3827 const uint8_t *salt,
3828 size_t salt_length,
3829 const uint8_t *label,
3830 size_t label_length )
3831{
3832 psa_status_t status;
3833 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3834
3835 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3836 return( PSA_ERROR_INVALID_ARGUMENT );
3837
3838 /* Quoting RFC 4279, Section 2:
3839 *
3840 * The premaster secret is formed as follows: if the PSK is N octets
3841 * long, concatenate a uint16 with the value N, N zero octets, a second
3842 * uint16 with the value N, and the PSK itself.
3843 */
3844
3845 pms[0] = ( psk_len >> 8 ) & 0xff;
3846 pms[1] = ( psk_len >> 0 ) & 0xff;
3847 memset( pms + 2, 0, psk_len );
3848 pms[2 + psk_len + 0] = pms[0];
3849 pms[2 + psk_len + 1] = pms[1];
3850 memcpy( pms + 4 + psk_len, psk, psk_len );
3851
3852 status = psa_generator_tls12_prf_setup( tls12_prf,
3853 pms, 4 + 2 * psk_len,
3854 hash_alg,
3855 salt, salt_length,
3856 label, label_length );
3857
Gilles Peskine3f108122018-12-07 18:14:53 +01003858 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00003859 return( status );
3860}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003861#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003862
Gilles Peskine346797d2018-11-16 16:05:06 +01003863/* Note that if this function fails, you must call psa_generator_abort()
3864 * to potentially free embedded data structures and wipe confidential data.
3865 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003866static psa_status_t psa_key_derivation_internal(
3867 psa_crypto_generator_t *generator,
3868 const uint8_t *secret, size_t secret_length,
3869 psa_algorithm_t alg,
3870 const uint8_t *salt, size_t salt_length,
3871 const uint8_t *label, size_t label_length,
3872 size_t capacity )
3873{
3874 psa_status_t status;
3875 size_t max_capacity;
3876
3877 /* Set generator->alg even on failure so that abort knows what to do. */
3878 generator->alg = alg;
3879
Gilles Peskine751d9652018-09-18 12:05:44 +02003880 if( alg == PSA_ALG_SELECT_RAW )
3881 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003882 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003883 if( salt_length != 0 )
3884 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003885 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003886 if( label_length != 0 )
3887 return( PSA_ERROR_INVALID_ARGUMENT );
3888 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3889 if( generator->ctx.buffer.data == NULL )
3890 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3891 memcpy( generator->ctx.buffer.data, secret, secret_length );
3892 generator->ctx.buffer.size = secret_length;
3893 max_capacity = secret_length;
3894 status = PSA_SUCCESS;
3895 }
3896 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003897#if defined(MBEDTLS_MD_C)
3898 if( PSA_ALG_IS_HKDF( alg ) )
3899 {
3900 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3901 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3902 if( hash_size == 0 )
3903 return( PSA_ERROR_NOT_SUPPORTED );
3904 max_capacity = 255 * hash_size;
3905 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3906 secret, secret_length,
3907 hash_alg,
3908 salt, salt_length,
3909 label, label_length );
3910 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003911 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
3912 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
3913 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003914 {
3915 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3916 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3917
3918 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
3919 if( hash_alg != PSA_ALG_SHA_256 &&
3920 hash_alg != PSA_ALG_SHA_384 )
3921 {
3922 return( PSA_ERROR_NOT_SUPPORTED );
3923 }
3924
3925 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00003926
3927 if( PSA_ALG_IS_TLS12_PRF( alg ) )
3928 {
3929 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
3930 secret, secret_length,
3931 hash_alg, salt, salt_length,
3932 label, label_length );
3933 }
3934 else
3935 {
3936 status = psa_generator_tls12_psk_to_ms_setup(
3937 &generator->ctx.tls12_prf,
3938 secret, secret_length,
3939 hash_alg, salt, salt_length,
3940 label, label_length );
3941 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003942 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003943 else
3944#endif
3945 {
3946 return( PSA_ERROR_NOT_SUPPORTED );
3947 }
3948
3949 if( status != PSA_SUCCESS )
3950 return( status );
3951
3952 if( capacity <= max_capacity )
3953 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003954 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
3955 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003956 else
3957 return( PSA_ERROR_INVALID_ARGUMENT );
3958
3959 return( PSA_SUCCESS );
3960}
3961
Gilles Peskineea0fb492018-07-12 17:17:20 +02003962psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003963 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003964 psa_algorithm_t alg,
3965 const uint8_t *salt,
3966 size_t salt_length,
3967 const uint8_t *label,
3968 size_t label_length,
3969 size_t capacity )
3970{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003971 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003972 psa_status_t status;
3973
3974 if( generator->alg != 0 )
3975 return( PSA_ERROR_BAD_STATE );
3976
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003977 /* Make sure that alg is a key derivation algorithm. This prevents
3978 * key selection algorithms, which psa_key_derivation_internal
3979 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003980 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3981 return( PSA_ERROR_INVALID_ARGUMENT );
3982
Gilles Peskinec5487a82018-12-03 18:08:14 +01003983 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003984 if( status != PSA_SUCCESS )
3985 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003986
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003987 if( slot->type != PSA_KEY_TYPE_DERIVE )
3988 return( PSA_ERROR_INVALID_ARGUMENT );
3989
3990 status = psa_key_derivation_internal( generator,
3991 slot->data.raw.data,
3992 slot->data.raw.bytes,
3993 alg,
3994 salt, salt_length,
3995 label, label_length,
3996 capacity );
3997 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003998 psa_generator_abort( generator );
3999 return( status );
4000}
4001
4002
4003
4004/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004005/* Key agreement */
4006/****************************************************************/
4007
Gilles Peskinea05219c2018-11-16 16:02:56 +01004008#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004009static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4010 size_t peer_key_length,
4011 const mbedtls_ecp_keypair *our_key,
4012 uint8_t *shared_secret,
4013 size_t shared_secret_size,
4014 size_t *shared_secret_length )
4015{
4016 mbedtls_pk_context pk;
4017 mbedtls_ecp_keypair *their_key = NULL;
4018 mbedtls_ecdh_context ecdh;
4019 int ret;
4020 mbedtls_ecdh_init( &ecdh );
4021 mbedtls_pk_init( &pk );
4022
4023 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4024 if( ret != 0 )
4025 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004026 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004027 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004028 case MBEDTLS_PK_ECKEY:
4029 case MBEDTLS_PK_ECKEY_DH:
4030 break;
4031 default:
4032 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4033 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004034 }
4035 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004036 if( their_key->grp.id != our_key->grp.id )
4037 {
4038 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4039 goto exit;
4040 }
4041
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004042 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4043 if( ret != 0 )
4044 goto exit;
4045 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4046 if( ret != 0 )
4047 goto exit;
4048
4049 ret = mbedtls_ecdh_calc_secret( &ecdh,
4050 shared_secret_length,
4051 shared_secret, shared_secret_size,
4052 mbedtls_ctr_drbg_random,
4053 &global_data.ctr_drbg );
4054
4055exit:
4056 mbedtls_pk_free( &pk );
4057 mbedtls_ecdh_free( &ecdh );
4058 return( mbedtls_to_psa_error( ret ) );
4059}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004060#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004061
Gilles Peskine01d718c2018-09-18 12:01:02 +02004062#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4063
Gilles Peskine346797d2018-11-16 16:05:06 +01004064/* Note that if this function fails, you must call psa_generator_abort()
4065 * to potentially free embedded data structures and wipe confidential data.
4066 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004067static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004068 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004069 const uint8_t *peer_key,
4070 size_t peer_key_length,
4071 psa_algorithm_t alg )
4072{
4073 psa_status_t status;
4074 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4075 size_t shared_secret_length = 0;
4076
4077 /* Step 1: run the secret agreement algorithm to generate the shared
4078 * secret. */
4079 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4080 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004081#if defined(MBEDTLS_ECDH_C)
4082 case PSA_ALG_ECDH_BASE:
4083 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4084 return( PSA_ERROR_INVALID_ARGUMENT );
4085 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4086 private_key->data.ecp,
4087 shared_secret,
4088 sizeof( shared_secret ),
4089 &shared_secret_length );
4090 break;
4091#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004092 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004093 (void) private_key;
4094 (void) peer_key;
4095 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004096 return( PSA_ERROR_NOT_SUPPORTED );
4097 }
4098 if( status != PSA_SUCCESS )
4099 goto exit;
4100
4101 /* Step 2: set up the key derivation to generate key material from
4102 * the shared secret. */
4103 status = psa_key_derivation_internal( generator,
4104 shared_secret, shared_secret_length,
4105 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4106 NULL, 0, NULL, 0,
4107 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4108exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004109 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004110 return( status );
4111}
4112
4113psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004114 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004115 const uint8_t *peer_key,
4116 size_t peer_key_length,
4117 psa_algorithm_t alg )
4118{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004119 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004120 psa_status_t status;
4121 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4122 return( PSA_ERROR_INVALID_ARGUMENT );
4123 status = psa_get_key_from_slot( private_key, &slot,
4124 PSA_KEY_USAGE_DERIVE, alg );
4125 if( status != PSA_SUCCESS )
4126 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004127 status = psa_key_agreement_internal( generator,
4128 slot,
4129 peer_key, peer_key_length,
4130 alg );
4131 if( status != PSA_SUCCESS )
4132 psa_generator_abort( generator );
4133 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004134}
4135
4136
4137
4138/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004139/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004140/****************************************************************/
4141
4142psa_status_t psa_generate_random( uint8_t *output,
4143 size_t output_size )
4144{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004145 int ret;
4146 GUARD_MODULE_INITIALIZED;
4147
4148 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004149 return( mbedtls_to_psa_error( ret ) );
4150}
4151
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004152#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004153
4154/* Support function for error conversion between psa_its error codes to psa crypto */
4155static psa_status_t its_to_psa_error( psa_its_status_t ret )
4156{
4157 switch( ret )
4158 {
4159 case PSA_ITS_SUCCESS:
4160 return( PSA_SUCCESS );
4161
4162 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4163 return( PSA_ERROR_EMPTY_SLOT );
4164
4165 case PSA_ITS_ERROR_STORAGE_FAILURE:
4166 return( PSA_ERROR_STORAGE_FAILURE );
4167
4168 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4169 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4170
4171 case PSA_ITS_ERROR_INVALID_KEY:
Jaeden Amero58600552018-11-30 12:04:38 +00004172 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004173 case PSA_ITS_ERROR_INCORRECT_SIZE:
4174 case PSA_ITS_ERROR_BAD_POINTER:
4175 return( PSA_ERROR_INVALID_ARGUMENT );
4176
4177 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4178 return( PSA_ERROR_NOT_SUPPORTED );
4179
4180 case PSA_ITS_ERROR_WRITE_ONCE:
4181 return( PSA_ERROR_OCCUPIED_SLOT );
4182
4183 default:
4184 return( PSA_ERROR_UNKNOWN_ERROR );
4185 }
4186}
4187
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004188psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4189 size_t seed_size )
4190{
4191 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004192 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004193 struct psa_its_info_t p_info;
4194 if( global_data.initialized )
4195 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004196
4197 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4198 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4199 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4200 return( PSA_ERROR_INVALID_ARGUMENT );
4201
avolinski0d2c2662018-11-21 17:31:07 +02004202 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004203 status = its_to_psa_error( its_status );
4204
4205 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004206 {
avolinski0d2c2662018-11-21 17:31:07 +02004207 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004208 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004209 }
avolinski13beb102018-11-20 16:51:49 +02004210 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004211 {
4212 /* You should not be here. Seed needs to be injected only once */
4213 status = PSA_ERROR_NOT_PERMITTED;
4214 }
4215 return( status );
4216}
4217#endif
4218
Gilles Peskinec5487a82018-12-03 18:08:14 +01004219psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004220 psa_key_type_t type,
4221 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004222 const void *extra,
4223 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004224{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004225 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004226 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004227
Gilles Peskine53d991e2018-07-12 01:14:59 +02004228 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004229 return( PSA_ERROR_INVALID_ARGUMENT );
4230
Gilles Peskinec5487a82018-12-03 18:08:14 +01004231 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004232 if( status != PSA_SUCCESS )
4233 return( status );
4234
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004235 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004236 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004237 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004238 if( status != PSA_SUCCESS )
4239 return( status );
4240 status = psa_generate_random( slot->data.raw.data,
4241 slot->data.raw.bytes );
4242 if( status != PSA_SUCCESS )
4243 {
4244 mbedtls_free( slot->data.raw.data );
4245 return( status );
4246 }
4247#if defined(MBEDTLS_DES_C)
4248 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004249 psa_des_set_key_parity( slot->data.raw.data,
4250 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004251#endif /* MBEDTLS_DES_C */
4252 }
4253 else
4254
4255#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4256 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4257 {
4258 mbedtls_rsa_context *rsa;
4259 int ret;
4260 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004261 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4262 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004263 /* Accept only byte-aligned keys, for the same reasons as
4264 * in psa_import_rsa_key(). */
4265 if( bits % 8 != 0 )
4266 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004267 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004268 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004269 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004270 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004271 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004272#if INT_MAX < 0xffffffff
4273 /* Check that the uint32_t value passed by the caller fits
4274 * in the range supported by this implementation. */
4275 if( p->e > INT_MAX )
4276 return( PSA_ERROR_NOT_SUPPORTED );
4277#endif
4278 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004279 }
4280 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4281 if( rsa == NULL )
4282 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4283 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4284 ret = mbedtls_rsa_gen_key( rsa,
4285 mbedtls_ctr_drbg_random,
4286 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004287 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004288 exponent );
4289 if( ret != 0 )
4290 {
4291 mbedtls_rsa_free( rsa );
4292 mbedtls_free( rsa );
4293 return( mbedtls_to_psa_error( ret ) );
4294 }
4295 slot->data.rsa = rsa;
4296 }
4297 else
4298#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4299
4300#if defined(MBEDTLS_ECP_C)
4301 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4302 {
4303 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4304 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4305 const mbedtls_ecp_curve_info *curve_info =
4306 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4307 mbedtls_ecp_keypair *ecp;
4308 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004309 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004310 return( PSA_ERROR_NOT_SUPPORTED );
4311 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4312 return( PSA_ERROR_NOT_SUPPORTED );
4313 if( curve_info->bit_size != bits )
4314 return( PSA_ERROR_INVALID_ARGUMENT );
4315 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4316 if( ecp == NULL )
4317 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4318 mbedtls_ecp_keypair_init( ecp );
4319 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4320 mbedtls_ctr_drbg_random,
4321 &global_data.ctr_drbg );
4322 if( ret != 0 )
4323 {
4324 mbedtls_ecp_keypair_free( ecp );
4325 mbedtls_free( ecp );
4326 return( mbedtls_to_psa_error( ret ) );
4327 }
4328 slot->data.ecp = ecp;
4329 }
4330 else
4331#endif /* MBEDTLS_ECP_C */
4332
4333 return( PSA_ERROR_NOT_SUPPORTED );
4334
4335 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004336
4337#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4338 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4339 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004340 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004341 }
4342#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4343
4344 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004345}
4346
4347
4348/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004349/* Module setup */
4350/****************************************************************/
4351
Gilles Peskine5e769522018-11-20 21:59:56 +01004352psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4353 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4354 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4355{
4356 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4357 return( PSA_ERROR_BAD_STATE );
4358 global_data.entropy_init = entropy_init;
4359 global_data.entropy_free = entropy_free;
4360 return( PSA_SUCCESS );
4361}
4362
Gilles Peskinee59236f2018-01-27 23:32:46 +01004363void mbedtls_psa_crypto_free( void )
4364{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004365 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004366 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4367 {
4368 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004369 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004370 }
4371 /* Wipe all remaining data, including configuration.
4372 * In particular, this sets all state indicator to the value
4373 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004374 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004375}
4376
4377psa_status_t psa_crypto_init( void )
4378{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004379 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004380 const unsigned char drbg_seed[] = "PSA";
4381
Gilles Peskinec6b69072018-11-20 21:42:52 +01004382 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004383 if( global_data.initialized != 0 )
4384 return( PSA_SUCCESS );
4385
Gilles Peskine5e769522018-11-20 21:59:56 +01004386 /* Set default configuration if
4387 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4388 if( global_data.entropy_init == NULL )
4389 global_data.entropy_init = mbedtls_entropy_init;
4390 if( global_data.entropy_free == NULL )
4391 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004392
Gilles Peskinec6b69072018-11-20 21:42:52 +01004393 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004394 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004395 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004396 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004397 status = mbedtls_to_psa_error(
4398 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4399 mbedtls_entropy_func,
4400 &global_data.entropy,
4401 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4402 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004403 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004404 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004405
Gilles Peskine66fb1262018-12-10 16:29:04 +01004406 status = psa_initialize_key_slots( );
4407 if( status != PSA_SUCCESS )
4408 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004409
4410 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004411 global_data.initialized = 1;
4412
Gilles Peskinee59236f2018-01-27 23:32:46 +01004413exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004414 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004415 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004416 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004417}
4418
4419#endif /* MBEDTLS_PSA_CRYPTO_C */