blob: 56ae891b2ec24f12666316335175283af9fe0613 [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"
Jaeden Amero25384a22019-01-10 10:23:21 +000065#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020066#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/blowfish.h"
68#include "mbedtls/camellia.h"
69#include "mbedtls/cipher.h"
70#include "mbedtls/ccm.h"
71#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010072#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020074#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010076#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020077#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/error.h"
79#include "mbedtls/gcm.h"
80#include "mbedtls/md2.h"
81#include "mbedtls/md4.h"
82#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010083#include "mbedtls/md.h"
84#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010085#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010086#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010087#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010089#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010090#include "mbedtls/sha1.h"
91#include "mbedtls/sha256.h"
92#include "mbedtls/sha512.h"
93#include "mbedtls/xtea.h"
94
Netanel Gonen2bcd3122018-11-19 11:53:02 +020095#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
Oren Cohen23a67842019-01-24 14:32:11 +020096#include "psa/internal_trusted_storage.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020097#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010098
Gilles Peskine996deb12018-08-01 15:45:45 +020099#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
100
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100101/* constant-time buffer comparison */
102static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
103{
104 size_t i;
105 unsigned char diff = 0;
106
107 for( i = 0; i < n; i++ )
108 diff |= a[i] ^ b[i];
109
110 return( diff );
111}
112
113
114
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100115/****************************************************************/
116/* Global data, support functions and library management */
117/****************************************************************/
118
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200119static int key_type_is_raw_bytes( psa_key_type_t type )
120{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200121 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200122}
123
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100124/* Values for psa_global_data_t::rng_state */
125#define RNG_NOT_INITIALIZED 0
126#define RNG_INITIALIZED 1
127#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100128
Gilles Peskine2d277862018-06-18 15:41:12 +0200129typedef struct
130{
Gilles Peskine5e769522018-11-20 21:59:56 +0100131 void (* entropy_init )( mbedtls_entropy_context *ctx );
132 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133 mbedtls_entropy_context entropy;
134 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100135 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100136 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137} psa_global_data_t;
138
139static psa_global_data_t global_data;
140
itayzafrir0adf0fc2018-09-06 16:24:41 +0300141#define GUARD_MODULE_INITIALIZED \
142 if( global_data.initialized == 0 ) \
143 return( PSA_ERROR_BAD_STATE );
144
Gilles Peskinee59236f2018-01-27 23:32:46 +0100145static psa_status_t mbedtls_to_psa_error( int ret )
146{
Gilles Peskinea5905292018-02-07 20:59:33 +0100147 /* If there's both a high-level code and low-level code, dispatch on
148 * the high-level code. */
149 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100150 {
151 case 0:
152 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100153
154 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
155 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
156 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
157 return( PSA_ERROR_NOT_SUPPORTED );
158 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
159 return( PSA_ERROR_HARDWARE_FAILURE );
160
161 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
162 return( PSA_ERROR_HARDWARE_FAILURE );
163
Gilles Peskine9a944802018-06-21 09:35:35 +0200164 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
165 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
166 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
167 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
168 case MBEDTLS_ERR_ASN1_INVALID_DATA:
169 return( PSA_ERROR_INVALID_ARGUMENT );
170 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
171 return( PSA_ERROR_INSUFFICIENT_MEMORY );
172 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
173 return( PSA_ERROR_BUFFER_TOO_SMALL );
174
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
176 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
182 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
183 return( PSA_ERROR_NOT_SUPPORTED );
184 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
187 case MBEDTLS_ERR_CCM_BAD_INPUT:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189 case MBEDTLS_ERR_CCM_AUTH_FAILED:
190 return( PSA_ERROR_INVALID_SIGNATURE );
191 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
192 return( PSA_ERROR_HARDWARE_FAILURE );
193
194 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
195 return( PSA_ERROR_NOT_SUPPORTED );
196 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
197 return( PSA_ERROR_INVALID_ARGUMENT );
198 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
199 return( PSA_ERROR_INSUFFICIENT_MEMORY );
200 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
201 return( PSA_ERROR_INVALID_PADDING );
202 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
203 return( PSA_ERROR_BAD_STATE );
204 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
207 return( PSA_ERROR_TAMPERING_DETECTED );
208 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
211 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
215 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
216 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
217 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
218 return( PSA_ERROR_NOT_SUPPORTED );
219 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
220 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
221
222 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
223 return( PSA_ERROR_NOT_SUPPORTED );
224 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
225 return( PSA_ERROR_HARDWARE_FAILURE );
226
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
228 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
229 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
232 case MBEDTLS_ERR_GCM_AUTH_FAILED:
233 return( PSA_ERROR_INVALID_SIGNATURE );
234 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200235 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
237 return( PSA_ERROR_HARDWARE_FAILURE );
238
239 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
244 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MD_ALLOC_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
250 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
251 return( PSA_ERROR_STORAGE_FAILURE );
252 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
256 return( PSA_ERROR_STORAGE_FAILURE );
257 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
262 return( PSA_ERROR_BUFFER_TOO_SMALL );
263 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
270 return( PSA_ERROR_INSUFFICIENT_MEMORY );
271
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
275 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100278 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100279 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
280 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
283 return( PSA_ERROR_NOT_SUPPORTED );
284 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
285 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
286 return( PSA_ERROR_NOT_PERMITTED );
287 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_PK_INVALID_ALG:
290 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
291 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
292 return( PSA_ERROR_NOT_SUPPORTED );
293 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
294 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
296 return( PSA_ERROR_HARDWARE_FAILURE );
297
298 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
301 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
302 return( PSA_ERROR_INVALID_ARGUMENT );
303 case MBEDTLS_ERR_RSA_INVALID_PADDING:
304 return( PSA_ERROR_INVALID_PADDING );
305 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
310 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
311 return( PSA_ERROR_TAMPERING_DETECTED );
312 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
313 return( PSA_ERROR_INVALID_SIGNATURE );
314 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
315 return( PSA_ERROR_BUFFER_TOO_SMALL );
316 case MBEDTLS_ERR_RSA_RNG_FAILED:
317 return( PSA_ERROR_INSUFFICIENT_MEMORY );
318 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
322
323 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
324 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
325 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
329 return( PSA_ERROR_INVALID_ARGUMENT );
330 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
itayzafrir5c753392018-05-08 11:18:38 +0300333 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300334 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300335 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300336 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
337 return( PSA_ERROR_BUFFER_TOO_SMALL );
338 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
339 return( PSA_ERROR_NOT_SUPPORTED );
340 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
341 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
342 return( PSA_ERROR_INVALID_SIGNATURE );
343 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
344 return( PSA_ERROR_INSUFFICIENT_MEMORY );
345 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
346 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300347
Gilles Peskinee59236f2018-01-27 23:32:46 +0100348 default:
349 return( PSA_ERROR_UNKNOWN_ERROR );
350 }
351}
352
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200353
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200354
355
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356/****************************************************************/
357/* Key management */
358/****************************************************************/
359
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100360#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200361static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
362{
363 switch( grpid )
364 {
365 case MBEDTLS_ECP_DP_SECP192R1:
366 return( PSA_ECC_CURVE_SECP192R1 );
367 case MBEDTLS_ECP_DP_SECP224R1:
368 return( PSA_ECC_CURVE_SECP224R1 );
369 case MBEDTLS_ECP_DP_SECP256R1:
370 return( PSA_ECC_CURVE_SECP256R1 );
371 case MBEDTLS_ECP_DP_SECP384R1:
372 return( PSA_ECC_CURVE_SECP384R1 );
373 case MBEDTLS_ECP_DP_SECP521R1:
374 return( PSA_ECC_CURVE_SECP521R1 );
375 case MBEDTLS_ECP_DP_BP256R1:
376 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
377 case MBEDTLS_ECP_DP_BP384R1:
378 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
379 case MBEDTLS_ECP_DP_BP512R1:
380 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
381 case MBEDTLS_ECP_DP_CURVE25519:
382 return( PSA_ECC_CURVE_CURVE25519 );
383 case MBEDTLS_ECP_DP_SECP192K1:
384 return( PSA_ECC_CURVE_SECP192K1 );
385 case MBEDTLS_ECP_DP_SECP224K1:
386 return( PSA_ECC_CURVE_SECP224K1 );
387 case MBEDTLS_ECP_DP_SECP256K1:
388 return( PSA_ECC_CURVE_SECP256K1 );
389 case MBEDTLS_ECP_DP_CURVE448:
390 return( PSA_ECC_CURVE_CURVE448 );
391 default:
392 return( 0 );
393 }
394}
395
Gilles Peskine12313cd2018-06-20 00:20:32 +0200396static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
397{
398 switch( curve )
399 {
400 case PSA_ECC_CURVE_SECP192R1:
401 return( MBEDTLS_ECP_DP_SECP192R1 );
402 case PSA_ECC_CURVE_SECP224R1:
403 return( MBEDTLS_ECP_DP_SECP224R1 );
404 case PSA_ECC_CURVE_SECP256R1:
405 return( MBEDTLS_ECP_DP_SECP256R1 );
406 case PSA_ECC_CURVE_SECP384R1:
407 return( MBEDTLS_ECP_DP_SECP384R1 );
408 case PSA_ECC_CURVE_SECP521R1:
409 return( MBEDTLS_ECP_DP_SECP521R1 );
410 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
411 return( MBEDTLS_ECP_DP_BP256R1 );
412 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
413 return( MBEDTLS_ECP_DP_BP384R1 );
414 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
415 return( MBEDTLS_ECP_DP_BP512R1 );
416 case PSA_ECC_CURVE_CURVE25519:
417 return( MBEDTLS_ECP_DP_CURVE25519 );
418 case PSA_ECC_CURVE_SECP192K1:
419 return( MBEDTLS_ECP_DP_SECP192K1 );
420 case PSA_ECC_CURVE_SECP224K1:
421 return( MBEDTLS_ECP_DP_SECP224K1 );
422 case PSA_ECC_CURVE_SECP256K1:
423 return( MBEDTLS_ECP_DP_SECP256K1 );
424 case PSA_ECC_CURVE_CURVE448:
425 return( MBEDTLS_ECP_DP_CURVE448 );
426 default:
427 return( MBEDTLS_ECP_DP_NONE );
428 }
429}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100430#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200431
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200432static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
433 size_t bits,
434 struct raw_data *raw )
435{
436 /* Check that the bit size is acceptable for the key type */
437 switch( type )
438 {
439 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200440 if( bits == 0 )
441 {
442 raw->bytes = 0;
443 raw->data = NULL;
444 return( PSA_SUCCESS );
445 }
446 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447#if defined(MBEDTLS_MD_C)
448 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200449#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200450 case PSA_KEY_TYPE_DERIVE:
451 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200452#if defined(MBEDTLS_AES_C)
453 case PSA_KEY_TYPE_AES:
454 if( bits != 128 && bits != 192 && bits != 256 )
455 return( PSA_ERROR_INVALID_ARGUMENT );
456 break;
457#endif
458#if defined(MBEDTLS_CAMELLIA_C)
459 case PSA_KEY_TYPE_CAMELLIA:
460 if( bits != 128 && bits != 192 && bits != 256 )
461 return( PSA_ERROR_INVALID_ARGUMENT );
462 break;
463#endif
464#if defined(MBEDTLS_DES_C)
465 case PSA_KEY_TYPE_DES:
466 if( bits != 64 && bits != 128 && bits != 192 )
467 return( PSA_ERROR_INVALID_ARGUMENT );
468 break;
469#endif
470#if defined(MBEDTLS_ARC4_C)
471 case PSA_KEY_TYPE_ARC4:
472 if( bits < 8 || bits > 2048 )
473 return( PSA_ERROR_INVALID_ARGUMENT );
474 break;
475#endif
476 default:
477 return( PSA_ERROR_NOT_SUPPORTED );
478 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200479 if( bits % 8 != 0 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481
482 /* Allocate memory for the key */
483 raw->bytes = PSA_BITS_TO_BYTES( bits );
484 raw->data = mbedtls_calloc( 1, raw->bytes );
485 if( raw->data == NULL )
486 {
487 raw->bytes = 0;
488 return( PSA_ERROR_INSUFFICIENT_MEMORY );
489 }
490 return( PSA_SUCCESS );
491}
492
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200493#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100494/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
495 * that are not a multiple of 8) well. For example, there is only
496 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
497 * way to return the exact bit size of a key.
498 * To keep things simple, reject non-byte-aligned key sizes. */
499static psa_status_t psa_check_rsa_key_byte_aligned(
500 const mbedtls_rsa_context *rsa )
501{
502 mbedtls_mpi n;
503 psa_status_t status;
504 mbedtls_mpi_init( &n );
505 status = mbedtls_to_psa_error(
506 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
507 if( status == PSA_SUCCESS )
508 {
509 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
510 status = PSA_ERROR_NOT_SUPPORTED;
511 }
512 mbedtls_mpi_free( &n );
513 return( status );
514}
515
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000516static psa_status_t psa_import_rsa_key( psa_key_type_t type,
517 const uint8_t *data,
518 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200519 mbedtls_rsa_context **p_rsa )
520{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000521 psa_status_t status;
522 mbedtls_pk_context pk;
523 mbedtls_rsa_context *rsa;
524 size_t bits;
525
526 mbedtls_pk_init( &pk );
527
528 /* Parse the data. */
529 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
530 status = mbedtls_to_psa_error(
531 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200532 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000533 status = mbedtls_to_psa_error(
534 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
535 if( status != PSA_SUCCESS )
536 goto exit;
537
538 /* We have something that the pkparse module recognizes. If it is a
539 * valid RSA key, store it. */
540 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200541 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000542 status = PSA_ERROR_INVALID_ARGUMENT;
543 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200544 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000545
546 rsa = mbedtls_pk_rsa( pk );
547 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
548 * supports non-byte-aligned key sizes, but not well. For example,
549 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
550 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
551 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
552 {
553 status = PSA_ERROR_NOT_SUPPORTED;
554 goto exit;
555 }
556 status = psa_check_rsa_key_byte_aligned( rsa );
557
558exit:
559 /* Free the content of the pk object only on error. */
560 if( status != PSA_SUCCESS )
561 {
562 mbedtls_pk_free( &pk );
563 return( status );
564 }
565
566 /* On success, store the content of the object in the RSA context. */
567 *p_rsa = rsa;
568
569 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570}
571#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
572
Jaeden Ameroccdce902019-01-10 11:42:27 +0000573#if defined(MBEDTLS_ECP_C)
574
575/* Import a public key given as the uncompressed representation defined by SEC1
576 * 2.3.3 as the content of an ECPoint. */
577static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
578 const uint8_t *data,
579 size_t data_length,
580 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200581{
Jaeden Ameroccdce902019-01-10 11:42:27 +0000582 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
583 mbedtls_ecp_keypair *ecp = NULL;
584 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
585
586 *p_ecp = NULL;
587 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
588 if( ecp == NULL )
589 return( PSA_ERROR_INSUFFICIENT_MEMORY );
590 mbedtls_ecp_keypair_init( ecp );
591
592 /* Load the group. */
593 status = mbedtls_to_psa_error(
594 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
595 if( status != PSA_SUCCESS )
596 goto exit;
597 /* Load the public value. */
598 status = mbedtls_to_psa_error(
599 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
600 data, data_length ) );
601 if( status != PSA_SUCCESS )
602 goto exit;
603
604 /* Check that the point is on the curve. */
605 status = mbedtls_to_psa_error(
606 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
607 if( status != PSA_SUCCESS )
608 goto exit;
609
610 *p_ecp = ecp;
611 return( PSA_SUCCESS );
612
613exit:
614 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200615 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000616 mbedtls_ecp_keypair_free( ecp );
617 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200618 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000619 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200620}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000621#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200622
Gilles Peskinef76aa772018-10-29 19:24:33 +0100623#if defined(MBEDTLS_ECP_C)
624/* Import a private key given as a byte string which is the private value
625 * in big-endian order. */
626static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
627 const uint8_t *data,
628 size_t data_length,
629 mbedtls_ecp_keypair **p_ecp )
630{
631 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
632 mbedtls_ecp_keypair *ecp = NULL;
633 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
634
635 *p_ecp = NULL;
636 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
637 if( ecp == NULL )
638 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000639 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100640
641 /* Load the group. */
642 status = mbedtls_to_psa_error(
643 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
644 if( status != PSA_SUCCESS )
645 goto exit;
646 /* Load the secret value. */
647 status = mbedtls_to_psa_error(
648 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
649 if( status != PSA_SUCCESS )
650 goto exit;
651 /* Validate the private key. */
652 status = mbedtls_to_psa_error(
653 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
654 if( status != PSA_SUCCESS )
655 goto exit;
656 /* Calculate the public key from the private key. */
657 status = mbedtls_to_psa_error(
658 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
659 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
660 if( status != PSA_SUCCESS )
661 goto exit;
662
663 *p_ecp = ecp;
664 return( PSA_SUCCESS );
665
666exit:
667 if( ecp != NULL )
668 {
669 mbedtls_ecp_keypair_free( ecp );
670 mbedtls_free( ecp );
671 }
672 return( status );
673}
674#endif /* defined(MBEDTLS_ECP_C) */
675
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100676/** Import key data into a slot. `slot->type` must have been set
677 * previously. This function assumes that the slot does not contain
678 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100679psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
680 const uint8_t *data,
681 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200683 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684
Darryl Green940d72c2018-07-13 13:18:51 +0100685 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100687 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100688 if( data_length > SIZE_MAX / 8 )
689 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100690 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200691 PSA_BYTES_TO_BITS( data_length ),
692 &slot->data.raw );
693 if( status != PSA_SUCCESS )
694 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200695 if( data_length != 0 )
696 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100697 }
698 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100699#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100700 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100701 {
Darryl Green940d72c2018-07-13 13:18:51 +0100702 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100703 data, data_length,
704 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100705 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000706 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000708 status = psa_import_ec_public_key(
709 PSA_KEY_TYPE_GET_CURVE( slot->type ),
710 data, data_length,
711 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100712 }
713 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100714#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000715#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
716 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100717 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000718 status = psa_import_rsa_key( slot->type,
719 data, data_length,
720 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100721 }
722 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000723#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100724 {
725 return( PSA_ERROR_NOT_SUPPORTED );
726 }
Jaeden Ameroc67200d2019-01-14 13:12:39 +0000727 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100728}
729
Darryl Green06fd18d2018-07-16 11:21:11 +0100730/* Retrieve an empty key slot (slot with no key data, but possibly
731 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100732static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100733 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100734{
735 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100736 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100737
738 *p_slot = NULL;
739
Gilles Peskinec5487a82018-12-03 18:08:14 +0100740 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100741 if( status != PSA_SUCCESS )
742 return( status );
743
744 if( slot->type != PSA_KEY_TYPE_NONE )
745 return( PSA_ERROR_OCCUPIED_SLOT );
746
747 *p_slot = slot;
748 return( status );
749}
750
751/** Retrieve a slot which must contain a key. The key must have allow all the
752 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
753 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100754static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100755 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100756 psa_key_usage_t usage,
757 psa_algorithm_t alg )
758{
759 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100760 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100761
762 *p_slot = NULL;
763
Gilles Peskinec5487a82018-12-03 18:08:14 +0100764 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100765 if( status != PSA_SUCCESS )
766 return( status );
767 if( slot->type == PSA_KEY_TYPE_NONE )
768 return( PSA_ERROR_EMPTY_SLOT );
769
770 /* Enforce that usage policy for the key slot contains all the flags
771 * required by the usage parameter. There is one exception: public
772 * keys can always be exported, so we treat public key objects as
773 * if they had the export flag. */
774 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
775 usage &= ~PSA_KEY_USAGE_EXPORT;
776 if( ( slot->policy.usage & usage ) != usage )
777 return( PSA_ERROR_NOT_PERMITTED );
778 if( alg != 0 && ( alg != slot->policy.alg ) )
779 return( PSA_ERROR_NOT_PERMITTED );
780
781 *p_slot = slot;
782 return( PSA_SUCCESS );
783}
Darryl Green940d72c2018-07-13 13:18:51 +0100784
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100785/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100786static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000787{
788 if( slot->type == PSA_KEY_TYPE_NONE )
789 {
790 /* No key material to clean. */
791 }
792 else if( key_type_is_raw_bytes( slot->type ) )
793 {
794 mbedtls_free( slot->data.raw.data );
795 }
796 else
797#if defined(MBEDTLS_RSA_C)
798 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
799 {
800 mbedtls_rsa_free( slot->data.rsa );
801 mbedtls_free( slot->data.rsa );
802 }
803 else
804#endif /* defined(MBEDTLS_RSA_C) */
805#if defined(MBEDTLS_ECP_C)
806 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
807 {
808 mbedtls_ecp_keypair_free( slot->data.ecp );
809 mbedtls_free( slot->data.ecp );
810 }
811 else
812#endif /* defined(MBEDTLS_ECP_C) */
813 {
814 /* Shouldn't happen: the key type is not any type that we
815 * put in. */
816 return( PSA_ERROR_TAMPERING_DETECTED );
817 }
818
819 return( PSA_SUCCESS );
820}
821
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100822/** Completely wipe a slot in memory, including its policy.
823 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100824psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100825{
826 psa_status_t status = psa_remove_key_data_from_memory( slot );
827 /* At this point, key material and other type-specific content has
828 * been wiped. Clear remaining metadata. We can call memset and not
829 * zeroize because the metadata is not particularly sensitive. */
830 memset( slot, 0, sizeof( *slot ) );
831 return( status );
832}
833
Gilles Peskinec5487a82018-12-03 18:08:14 +0100834psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100835 psa_key_type_t type,
836 const uint8_t *data,
837 size_t data_length )
838{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100839 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100840 psa_status_t status;
841
Gilles Peskinec5487a82018-12-03 18:08:14 +0100842 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100843 if( status != PSA_SUCCESS )
844 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845
846 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100847
848 status = psa_import_key_into_slot( slot, data, data_length );
849 if( status != PSA_SUCCESS )
850 {
851 slot->type = PSA_KEY_TYPE_NONE;
852 return( status );
853 }
854
Darryl Greend49a4992018-06-18 17:27:26 +0100855#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
856 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
857 {
858 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100859 status = psa_save_persistent_key( slot->persistent_storage_id,
860 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100861 data_length );
862 if( status != PSA_SUCCESS )
863 {
864 (void) psa_remove_key_data_from_memory( slot );
865 slot->type = PSA_KEY_TYPE_NONE;
866 }
867 }
868#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
869
870 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871}
872
Gilles Peskinec5487a82018-12-03 18:08:14 +0100873psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100875 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100876 psa_status_t status = PSA_SUCCESS;
877 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878
Gilles Peskinec5487a82018-12-03 18:08:14 +0100879 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100880 if( status != PSA_SUCCESS )
881 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100882#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
883 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
884 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100885 storage_status =
886 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100887 }
888#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100889 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100890 if( status != PSA_SUCCESS )
891 return( status );
892 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100893}
894
Gilles Peskineb870b182018-07-06 16:02:09 +0200895/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100896static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200897{
898 if( key_type_is_raw_bytes( slot->type ) )
899 return( slot->data.raw.bytes * 8 );
900#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200901 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100902 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200903#endif /* defined(MBEDTLS_RSA_C) */
904#if defined(MBEDTLS_ECP_C)
905 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
906 return( slot->data.ecp->grp.pbits );
907#endif /* defined(MBEDTLS_ECP_C) */
908 /* Shouldn't happen except on an empty slot. */
909 return( 0 );
910}
911
Gilles Peskinec5487a82018-12-03 18:08:14 +0100912psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200913 psa_key_type_t *type,
914 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100915{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100916 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200917 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100918
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100919 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200920 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100921 if( bits != NULL )
922 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100923 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200924 if( status != PSA_SUCCESS )
925 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200926
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100927 if( slot->type == PSA_KEY_TYPE_NONE )
928 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200929 if( type != NULL )
930 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200931 if( bits != NULL )
932 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100933 return( PSA_SUCCESS );
934}
935
Jaeden Ameroccdce902019-01-10 11:42:27 +0000936#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +0000937static int pk_write_pubkey_simple( mbedtls_pk_context *key,
938 unsigned char *buf, size_t size )
939{
940 int ret;
941 unsigned char *c;
942 size_t len = 0;
943
944 c = buf + size;
945
946 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
947
948 return( (int) len );
949}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000950#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +0000951
Gilles Peskine2f060a82018-12-04 17:12:32 +0100952static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200953 uint8_t *data,
954 size_t data_size,
955 size_t *data_length,
956 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100957{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100958 *data_length = 0;
959
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200960 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300961 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300962
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200963 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100964 {
965 if( slot->data.raw.bytes > data_size )
966 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100967 if( data_size != 0 )
968 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200969 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100970 memset( data + slot->data.raw.bytes, 0,
971 data_size - slot->data.raw.bytes );
972 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100973 *data_length = slot->data.raw.bytes;
974 return( PSA_SUCCESS );
975 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100976#if defined(MBEDTLS_ECP_C)
977 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
978 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000979 psa_status_t status;
980
Gilles Peskine188c71e2018-10-29 19:26:02 +0100981 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
982 if( bytes > data_size )
983 return( PSA_ERROR_BUFFER_TOO_SMALL );
984 status = mbedtls_to_psa_error(
985 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
986 if( status != PSA_SUCCESS )
987 return( status );
988 memset( data + bytes, 0, data_size - bytes );
989 *data_length = bytes;
990 return( PSA_SUCCESS );
991 }
992#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993 else
Moran Peker17e36e12018-05-02 12:55:20 +0300994 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100995#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200996 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300997 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100998 {
Moran Pekera998bc62018-04-16 18:16:20 +0300999 mbedtls_pk_context pk;
1000 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001001 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001002 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001003#if defined(MBEDTLS_RSA_C)
1004 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001005 pk.pk_info = &mbedtls_rsa_info;
1006 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001007#else
1008 return( PSA_ERROR_NOT_SUPPORTED );
1009#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001010 }
1011 else
1012 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001013#if defined(MBEDTLS_ECP_C)
1014 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001015 pk.pk_info = &mbedtls_eckey_info;
1016 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001017#else
1018 return( PSA_ERROR_NOT_SUPPORTED );
1019#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001020 }
Moran Pekerd7326592018-05-29 16:56:39 +03001021 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001022 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001023 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001024 }
Moran Peker17e36e12018-05-02 12:55:20 +03001025 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001026 {
Moran Peker17e36e12018-05-02 12:55:20 +03001027 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001028 }
Moran Peker60364322018-04-29 11:34:58 +03001029 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001030 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001031 /* If data_size is 0 then data may be NULL and then the
1032 * call to memset would have undefined behavior. */
1033 if( data_size != 0 )
1034 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001035 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001036 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001037 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1038 * Move the data to the beginning and erase remaining data
1039 * at the original location. */
1040 if( 2 * (size_t) ret <= data_size )
1041 {
1042 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001043 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001044 }
1045 else if( (size_t) ret < data_size )
1046 {
1047 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001048 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001049 }
Moran Pekera998bc62018-04-16 18:16:20 +03001050 *data_length = ret;
1051 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001052 }
1053 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001054#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001055 {
1056 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001057 it is valid for a special-purpose implementation to omit
1058 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001059 return( PSA_ERROR_NOT_SUPPORTED );
1060 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001061 }
1062}
1063
Gilles Peskinec5487a82018-12-03 18:08:14 +01001064psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001065 uint8_t *data,
1066 size_t data_size,
1067 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001068{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001069 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001070 psa_status_t status;
1071
1072 /* Set the key to empty now, so that even when there are errors, we always
1073 * set data_length to a value between 0 and data_size. On error, setting
1074 * the key to empty is a good choice because an empty key representation is
1075 * unlikely to be accepted anywhere. */
1076 *data_length = 0;
1077
1078 /* Export requires the EXPORT flag. There is an exception for public keys,
1079 * which don't require any flag, but psa_get_key_from_slot takes
1080 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001081 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001082 if( status != PSA_SUCCESS )
1083 return( status );
1084 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001085 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001086}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087
Gilles Peskinec5487a82018-12-03 18:08:14 +01001088psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001089 uint8_t *data,
1090 size_t data_size,
1091 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001092{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001093 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001094 psa_status_t status;
1095
1096 /* Set the key to empty now, so that even when there are errors, we always
1097 * set data_length to a value between 0 and data_size. On error, setting
1098 * the key to empty is a good choice because an empty key representation is
1099 * unlikely to be accepted anywhere. */
1100 *data_length = 0;
1101
1102 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001103 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001104 if( status != PSA_SUCCESS )
1105 return( status );
1106 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001107 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001108}
1109
Darryl Green0c6575a2018-11-07 16:05:30 +00001110#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001111static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001112 size_t bits )
1113{
1114 psa_status_t status;
1115 uint8_t *data;
1116 size_t key_length;
1117 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1118 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001119 if( data == NULL )
1120 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001121 /* Get key data in export format */
1122 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1123 if( status != PSA_SUCCESS )
1124 {
1125 slot->type = PSA_KEY_TYPE_NONE;
1126 goto exit;
1127 }
1128 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001129 status = psa_save_persistent_key( slot->persistent_storage_id,
1130 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001131 data, key_length );
1132 if( status != PSA_SUCCESS )
1133 {
1134 slot->type = PSA_KEY_TYPE_NONE;
1135 }
1136exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001137 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001138 mbedtls_free( data );
1139 return( status );
1140}
1141#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1142
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001143
1144
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001145/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001146/* Message digests */
1147/****************************************************************/
1148
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001149static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001150{
1151 switch( alg )
1152 {
1153#if defined(MBEDTLS_MD2_C)
1154 case PSA_ALG_MD2:
1155 return( &mbedtls_md2_info );
1156#endif
1157#if defined(MBEDTLS_MD4_C)
1158 case PSA_ALG_MD4:
1159 return( &mbedtls_md4_info );
1160#endif
1161#if defined(MBEDTLS_MD5_C)
1162 case PSA_ALG_MD5:
1163 return( &mbedtls_md5_info );
1164#endif
1165#if defined(MBEDTLS_RIPEMD160_C)
1166 case PSA_ALG_RIPEMD160:
1167 return( &mbedtls_ripemd160_info );
1168#endif
1169#if defined(MBEDTLS_SHA1_C)
1170 case PSA_ALG_SHA_1:
1171 return( &mbedtls_sha1_info );
1172#endif
1173#if defined(MBEDTLS_SHA256_C)
1174 case PSA_ALG_SHA_224:
1175 return( &mbedtls_sha224_info );
1176 case PSA_ALG_SHA_256:
1177 return( &mbedtls_sha256_info );
1178#endif
1179#if defined(MBEDTLS_SHA512_C)
1180 case PSA_ALG_SHA_384:
1181 return( &mbedtls_sha384_info );
1182 case PSA_ALG_SHA_512:
1183 return( &mbedtls_sha512_info );
1184#endif
1185 default:
1186 return( NULL );
1187 }
1188}
1189
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001190psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1191{
1192 switch( operation->alg )
1193 {
Gilles Peskine81736312018-06-26 15:04:31 +02001194 case 0:
1195 /* The object has (apparently) been initialized but it is not
1196 * in use. It's ok to call abort on such an object, and there's
1197 * nothing to do. */
1198 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001199#if defined(MBEDTLS_MD2_C)
1200 case PSA_ALG_MD2:
1201 mbedtls_md2_free( &operation->ctx.md2 );
1202 break;
1203#endif
1204#if defined(MBEDTLS_MD4_C)
1205 case PSA_ALG_MD4:
1206 mbedtls_md4_free( &operation->ctx.md4 );
1207 break;
1208#endif
1209#if defined(MBEDTLS_MD5_C)
1210 case PSA_ALG_MD5:
1211 mbedtls_md5_free( &operation->ctx.md5 );
1212 break;
1213#endif
1214#if defined(MBEDTLS_RIPEMD160_C)
1215 case PSA_ALG_RIPEMD160:
1216 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1217 break;
1218#endif
1219#if defined(MBEDTLS_SHA1_C)
1220 case PSA_ALG_SHA_1:
1221 mbedtls_sha1_free( &operation->ctx.sha1 );
1222 break;
1223#endif
1224#if defined(MBEDTLS_SHA256_C)
1225 case PSA_ALG_SHA_224:
1226 case PSA_ALG_SHA_256:
1227 mbedtls_sha256_free( &operation->ctx.sha256 );
1228 break;
1229#endif
1230#if defined(MBEDTLS_SHA512_C)
1231 case PSA_ALG_SHA_384:
1232 case PSA_ALG_SHA_512:
1233 mbedtls_sha512_free( &operation->ctx.sha512 );
1234 break;
1235#endif
1236 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001237 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001238 }
1239 operation->alg = 0;
1240 return( PSA_SUCCESS );
1241}
1242
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001243psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001244 psa_algorithm_t alg )
1245{
1246 int ret;
1247 operation->alg = 0;
1248 switch( alg )
1249 {
1250#if defined(MBEDTLS_MD2_C)
1251 case PSA_ALG_MD2:
1252 mbedtls_md2_init( &operation->ctx.md2 );
1253 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1254 break;
1255#endif
1256#if defined(MBEDTLS_MD4_C)
1257 case PSA_ALG_MD4:
1258 mbedtls_md4_init( &operation->ctx.md4 );
1259 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1260 break;
1261#endif
1262#if defined(MBEDTLS_MD5_C)
1263 case PSA_ALG_MD5:
1264 mbedtls_md5_init( &operation->ctx.md5 );
1265 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1266 break;
1267#endif
1268#if defined(MBEDTLS_RIPEMD160_C)
1269 case PSA_ALG_RIPEMD160:
1270 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1271 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1272 break;
1273#endif
1274#if defined(MBEDTLS_SHA1_C)
1275 case PSA_ALG_SHA_1:
1276 mbedtls_sha1_init( &operation->ctx.sha1 );
1277 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1278 break;
1279#endif
1280#if defined(MBEDTLS_SHA256_C)
1281 case PSA_ALG_SHA_224:
1282 mbedtls_sha256_init( &operation->ctx.sha256 );
1283 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1284 break;
1285 case PSA_ALG_SHA_256:
1286 mbedtls_sha256_init( &operation->ctx.sha256 );
1287 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1288 break;
1289#endif
1290#if defined(MBEDTLS_SHA512_C)
1291 case PSA_ALG_SHA_384:
1292 mbedtls_sha512_init( &operation->ctx.sha512 );
1293 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1294 break;
1295 case PSA_ALG_SHA_512:
1296 mbedtls_sha512_init( &operation->ctx.sha512 );
1297 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1298 break;
1299#endif
1300 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001301 return( PSA_ALG_IS_HASH( alg ) ?
1302 PSA_ERROR_NOT_SUPPORTED :
1303 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001304 }
1305 if( ret == 0 )
1306 operation->alg = alg;
1307 else
1308 psa_hash_abort( operation );
1309 return( mbedtls_to_psa_error( ret ) );
1310}
1311
1312psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1313 const uint8_t *input,
1314 size_t input_length )
1315{
1316 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001317
1318 /* Don't require hash implementations to behave correctly on a
1319 * zero-length input, which may have an invalid pointer. */
1320 if( input_length == 0 )
1321 return( PSA_SUCCESS );
1322
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001323 switch( operation->alg )
1324 {
1325#if defined(MBEDTLS_MD2_C)
1326 case PSA_ALG_MD2:
1327 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1328 input, input_length );
1329 break;
1330#endif
1331#if defined(MBEDTLS_MD4_C)
1332 case PSA_ALG_MD4:
1333 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1334 input, input_length );
1335 break;
1336#endif
1337#if defined(MBEDTLS_MD5_C)
1338 case PSA_ALG_MD5:
1339 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1340 input, input_length );
1341 break;
1342#endif
1343#if defined(MBEDTLS_RIPEMD160_C)
1344 case PSA_ALG_RIPEMD160:
1345 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1346 input, input_length );
1347 break;
1348#endif
1349#if defined(MBEDTLS_SHA1_C)
1350 case PSA_ALG_SHA_1:
1351 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1352 input, input_length );
1353 break;
1354#endif
1355#if defined(MBEDTLS_SHA256_C)
1356 case PSA_ALG_SHA_224:
1357 case PSA_ALG_SHA_256:
1358 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1359 input, input_length );
1360 break;
1361#endif
1362#if defined(MBEDTLS_SHA512_C)
1363 case PSA_ALG_SHA_384:
1364 case PSA_ALG_SHA_512:
1365 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1366 input, input_length );
1367 break;
1368#endif
1369 default:
1370 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1371 break;
1372 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001373
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001374 if( ret != 0 )
1375 psa_hash_abort( operation );
1376 return( mbedtls_to_psa_error( ret ) );
1377}
1378
1379psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1380 uint8_t *hash,
1381 size_t hash_size,
1382 size_t *hash_length )
1383{
itayzafrir40835d42018-08-02 13:14:17 +03001384 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001385 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001386 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001387
1388 /* Fill the output buffer with something that isn't a valid hash
1389 * (barring an attack on the hash and deliberately-crafted input),
1390 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001391 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001392 /* If hash_size is 0 then hash may be NULL and then the
1393 * call to memset would have undefined behavior. */
1394 if( hash_size != 0 )
1395 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001396
1397 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001398 {
1399 status = PSA_ERROR_BUFFER_TOO_SMALL;
1400 goto exit;
1401 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001402
1403 switch( operation->alg )
1404 {
1405#if defined(MBEDTLS_MD2_C)
1406 case PSA_ALG_MD2:
1407 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1408 break;
1409#endif
1410#if defined(MBEDTLS_MD4_C)
1411 case PSA_ALG_MD4:
1412 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1413 break;
1414#endif
1415#if defined(MBEDTLS_MD5_C)
1416 case PSA_ALG_MD5:
1417 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1418 break;
1419#endif
1420#if defined(MBEDTLS_RIPEMD160_C)
1421 case PSA_ALG_RIPEMD160:
1422 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1423 break;
1424#endif
1425#if defined(MBEDTLS_SHA1_C)
1426 case PSA_ALG_SHA_1:
1427 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1428 break;
1429#endif
1430#if defined(MBEDTLS_SHA256_C)
1431 case PSA_ALG_SHA_224:
1432 case PSA_ALG_SHA_256:
1433 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1434 break;
1435#endif
1436#if defined(MBEDTLS_SHA512_C)
1437 case PSA_ALG_SHA_384:
1438 case PSA_ALG_SHA_512:
1439 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1440 break;
1441#endif
1442 default:
1443 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1444 break;
1445 }
itayzafrir40835d42018-08-02 13:14:17 +03001446 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001447
itayzafrir40835d42018-08-02 13:14:17 +03001448exit:
1449 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001450 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001451 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001452 return( psa_hash_abort( operation ) );
1453 }
1454 else
1455 {
1456 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001457 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001458 }
1459}
1460
Gilles Peskine2d277862018-06-18 15:41:12 +02001461psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1462 const uint8_t *hash,
1463 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001464{
1465 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1466 size_t actual_hash_length;
1467 psa_status_t status = psa_hash_finish( operation,
1468 actual_hash, sizeof( actual_hash ),
1469 &actual_hash_length );
1470 if( status != PSA_SUCCESS )
1471 return( status );
1472 if( actual_hash_length != hash_length )
1473 return( PSA_ERROR_INVALID_SIGNATURE );
1474 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1475 return( PSA_ERROR_INVALID_SIGNATURE );
1476 return( PSA_SUCCESS );
1477}
1478
Gilles Peskineeb35d782019-01-22 17:56:16 +01001479psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1480 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001481{
1482 if( target_operation->alg != 0 )
1483 return( PSA_ERROR_BAD_STATE );
1484
1485 switch( source_operation->alg )
1486 {
1487 case 0:
1488 return( PSA_ERROR_BAD_STATE );
1489#if defined(MBEDTLS_MD2_C)
1490 case PSA_ALG_MD2:
1491 mbedtls_md2_clone( &target_operation->ctx.md2,
1492 &source_operation->ctx.md2 );
1493 break;
1494#endif
1495#if defined(MBEDTLS_MD4_C)
1496 case PSA_ALG_MD4:
1497 mbedtls_md4_clone( &target_operation->ctx.md4,
1498 &source_operation->ctx.md4 );
1499 break;
1500#endif
1501#if defined(MBEDTLS_MD5_C)
1502 case PSA_ALG_MD5:
1503 mbedtls_md5_clone( &target_operation->ctx.md5,
1504 &source_operation->ctx.md5 );
1505 break;
1506#endif
1507#if defined(MBEDTLS_RIPEMD160_C)
1508 case PSA_ALG_RIPEMD160:
1509 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1510 &source_operation->ctx.ripemd160 );
1511 break;
1512#endif
1513#if defined(MBEDTLS_SHA1_C)
1514 case PSA_ALG_SHA_1:
1515 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1516 &source_operation->ctx.sha1 );
1517 break;
1518#endif
1519#if defined(MBEDTLS_SHA256_C)
1520 case PSA_ALG_SHA_224:
1521 case PSA_ALG_SHA_256:
1522 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1523 &source_operation->ctx.sha256 );
1524 break;
1525#endif
1526#if defined(MBEDTLS_SHA512_C)
1527 case PSA_ALG_SHA_384:
1528 case PSA_ALG_SHA_512:
1529 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1530 &source_operation->ctx.sha512 );
1531 break;
1532#endif
1533 default:
1534 return( PSA_ERROR_NOT_SUPPORTED );
1535 }
1536
1537 target_operation->alg = source_operation->alg;
1538 return( PSA_SUCCESS );
1539}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001540
1541
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001542/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001543/* MAC */
1544/****************************************************************/
1545
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001546static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547 psa_algorithm_t alg,
1548 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001549 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001550 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001551{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001552 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001553 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001554
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001555 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001556 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001557
Gilles Peskine8c9def32018-02-08 10:02:12 +01001558 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1559 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001560 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001561 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001562 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001563 mode = MBEDTLS_MODE_STREAM;
1564 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001565 case PSA_ALG_CTR:
1566 mode = MBEDTLS_MODE_CTR;
1567 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001568 case PSA_ALG_CFB:
1569 mode = MBEDTLS_MODE_CFB;
1570 break;
1571 case PSA_ALG_OFB:
1572 mode = MBEDTLS_MODE_OFB;
1573 break;
1574 case PSA_ALG_CBC_NO_PADDING:
1575 mode = MBEDTLS_MODE_CBC;
1576 break;
1577 case PSA_ALG_CBC_PKCS7:
1578 mode = MBEDTLS_MODE_CBC;
1579 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001580 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001581 mode = MBEDTLS_MODE_CCM;
1582 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001583 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001584 mode = MBEDTLS_MODE_GCM;
1585 break;
1586 default:
1587 return( NULL );
1588 }
1589 }
1590 else if( alg == PSA_ALG_CMAC )
1591 mode = MBEDTLS_MODE_ECB;
1592 else if( alg == PSA_ALG_GMAC )
1593 mode = MBEDTLS_MODE_GCM;
1594 else
1595 return( NULL );
1596
1597 switch( key_type )
1598 {
1599 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001600 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001601 break;
1602 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001603 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1604 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001605 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001606 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001607 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001608 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001609 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1610 * but two-key Triple-DES is functionally three-key Triple-DES
1611 * with K1=K3, so that's how we present it to mbedtls. */
1612 if( key_bits == 128 )
1613 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001614 break;
1615 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001616 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001617 break;
1618 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001619 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001620 break;
1621 default:
1622 return( NULL );
1623 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001624 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001625 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001626
Jaeden Amero23bbb752018-06-26 14:16:54 +01001627 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1628 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001629}
1630
Gilles Peskinea05219c2018-11-16 16:02:56 +01001631#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001632static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001633{
Gilles Peskine2d277862018-06-18 15:41:12 +02001634 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001635 {
1636 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001637 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001638 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001639 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001640 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001641 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001642 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001643 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001644 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001645 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001646 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001647 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001648 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001649 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001650 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001651 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001652 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001653 return( 128 );
1654 default:
1655 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001656 }
1657}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001658#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001659
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001660/* Initialize the MAC operation structure. Once this function has been
1661 * called, psa_mac_abort can run and will do the right thing. */
1662static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1663 psa_algorithm_t alg )
1664{
1665 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1666
1667 operation->alg = alg;
1668 operation->key_set = 0;
1669 operation->iv_set = 0;
1670 operation->iv_required = 0;
1671 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001672 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001673
1674#if defined(MBEDTLS_CMAC_C)
1675 if( alg == PSA_ALG_CMAC )
1676 {
1677 operation->iv_required = 0;
1678 mbedtls_cipher_init( &operation->ctx.cmac );
1679 status = PSA_SUCCESS;
1680 }
1681 else
1682#endif /* MBEDTLS_CMAC_C */
1683#if defined(MBEDTLS_MD_C)
1684 if( PSA_ALG_IS_HMAC( operation->alg ) )
1685 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001686 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1687 operation->ctx.hmac.hash_ctx.alg = 0;
1688 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001689 }
1690 else
1691#endif /* MBEDTLS_MD_C */
1692 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001693 if( ! PSA_ALG_IS_MAC( alg ) )
1694 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001695 }
1696
1697 if( status != PSA_SUCCESS )
1698 memset( operation, 0, sizeof( *operation ) );
1699 return( status );
1700}
1701
Gilles Peskine01126fa2018-07-12 17:04:55 +02001702#if defined(MBEDTLS_MD_C)
1703static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1704{
Gilles Peskine3f108122018-12-07 18:14:53 +01001705 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001706 return( psa_hash_abort( &hmac->hash_ctx ) );
1707}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001708
1709static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1710{
1711 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1712 memset( hmac, 0, sizeof( *hmac ) );
1713}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001714#endif /* MBEDTLS_MD_C */
1715
Gilles Peskine8c9def32018-02-08 10:02:12 +01001716psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1717{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001718 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001719 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001720 /* The object has (apparently) been initialized but it is not
1721 * in use. It's ok to call abort on such an object, and there's
1722 * nothing to do. */
1723 return( PSA_SUCCESS );
1724 }
1725 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001726#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001727 if( operation->alg == PSA_ALG_CMAC )
1728 {
1729 mbedtls_cipher_free( &operation->ctx.cmac );
1730 }
1731 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001732#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001733#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001734 if( PSA_ALG_IS_HMAC( operation->alg ) )
1735 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001736 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001737 }
1738 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001739#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001740 {
1741 /* Sanity check (shouldn't happen: operation->alg should
1742 * always have been initialized to a valid value). */
1743 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001744 }
Moran Peker41deec42018-04-04 15:43:05 +03001745
Gilles Peskine8c9def32018-02-08 10:02:12 +01001746 operation->alg = 0;
1747 operation->key_set = 0;
1748 operation->iv_set = 0;
1749 operation->iv_required = 0;
1750 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001751 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001752
Gilles Peskine8c9def32018-02-08 10:02:12 +01001753 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001754
1755bad_state:
1756 /* If abort is called on an uninitialized object, we can't trust
1757 * anything. Wipe the object in case it contains confidential data.
1758 * This may result in a memory leak if a pointer gets overwritten,
1759 * but it's too late to do anything about this. */
1760 memset( operation, 0, sizeof( *operation ) );
1761 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001762}
1763
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001764#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001765static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001766 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001767 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001768 const mbedtls_cipher_info_t *cipher_info )
1769{
1770 int ret;
1771
1772 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001773
1774 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1775 if( ret != 0 )
1776 return( ret );
1777
1778 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1779 slot->data.raw.data,
1780 key_bits );
1781 return( ret );
1782}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001783#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001784
Gilles Peskine248051a2018-06-20 16:09:38 +02001785#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001786static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1787 const uint8_t *key,
1788 size_t key_length,
1789 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001790{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001791 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001792 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001793 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001794 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001795 psa_status_t status;
1796
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001797 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1798 * overflow below. This should never trigger if the hash algorithm
1799 * is implemented correctly. */
1800 /* The size checks against the ipad and opad buffers cannot be written
1801 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1802 * because that triggers -Wlogical-op on GCC 7.3. */
1803 if( block_size > sizeof( ipad ) )
1804 return( PSA_ERROR_NOT_SUPPORTED );
1805 if( block_size > sizeof( hmac->opad ) )
1806 return( PSA_ERROR_NOT_SUPPORTED );
1807 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001808 return( PSA_ERROR_NOT_SUPPORTED );
1809
Gilles Peskined223b522018-06-11 18:12:58 +02001810 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001811 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001812 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001813 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001814 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001815 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001816 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001817 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001818 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001819 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001820 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001821 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001822 }
Gilles Peskine96889972018-07-12 17:07:03 +02001823 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1824 * but it is permitted. It is common when HMAC is used in HKDF, for
1825 * example. Don't call `memcpy` in the 0-length because `key` could be
1826 * an invalid pointer which would make the behavior undefined. */
1827 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001828 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001829
Gilles Peskined223b522018-06-11 18:12:58 +02001830 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1831 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001832 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001833 ipad[i] ^= 0x36;
1834 memset( ipad + key_length, 0x36, block_size - key_length );
1835
1836 /* Copy the key material from ipad to opad, flipping the requisite bits,
1837 * and filling the rest of opad with the requisite constant. */
1838 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001839 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1840 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001841
Gilles Peskine01126fa2018-07-12 17:04:55 +02001842 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001843 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001844 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001845
Gilles Peskine01126fa2018-07-12 17:04:55 +02001846 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001847
1848cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001849 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001850
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001851 return( status );
1852}
Gilles Peskine248051a2018-06-20 16:09:38 +02001853#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001854
Gilles Peskine89167cb2018-07-08 20:12:23 +02001855static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001856 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001857 psa_algorithm_t alg,
1858 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001859{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001860 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001861 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001862 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001863 psa_key_usage_t usage =
1864 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001865 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001866 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001867
Gilles Peskined911eb72018-08-14 15:18:45 +02001868 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001869 if( status != PSA_SUCCESS )
1870 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001871 if( is_sign )
1872 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001873
Gilles Peskinec5487a82018-12-03 18:08:14 +01001874 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001875 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001876 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001877 key_bits = psa_get_key_bits( slot );
1878
Gilles Peskine8c9def32018-02-08 10:02:12 +01001879#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001880 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001881 {
1882 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001883 mbedtls_cipher_info_from_psa( full_length_alg,
1884 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001885 int ret;
1886 if( cipher_info == NULL )
1887 {
1888 status = PSA_ERROR_NOT_SUPPORTED;
1889 goto exit;
1890 }
1891 operation->mac_size = cipher_info->block_size;
1892 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1893 status = mbedtls_to_psa_error( ret );
1894 }
1895 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001896#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001897#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001898 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001899 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001900 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001901 if( hash_alg == 0 )
1902 {
1903 status = PSA_ERROR_NOT_SUPPORTED;
1904 goto exit;
1905 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001906
1907 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1908 /* Sanity check. This shouldn't fail on a valid configuration. */
1909 if( operation->mac_size == 0 ||
1910 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1911 {
1912 status = PSA_ERROR_NOT_SUPPORTED;
1913 goto exit;
1914 }
1915
Gilles Peskine01126fa2018-07-12 17:04:55 +02001916 if( slot->type != PSA_KEY_TYPE_HMAC )
1917 {
1918 status = PSA_ERROR_INVALID_ARGUMENT;
1919 goto exit;
1920 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001921
Gilles Peskine01126fa2018-07-12 17:04:55 +02001922 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1923 slot->data.raw.data,
1924 slot->data.raw.bytes,
1925 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001926 }
1927 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001928#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001929 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001930 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001931 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001932 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001933
Gilles Peskined911eb72018-08-14 15:18:45 +02001934 if( truncated == 0 )
1935 {
1936 /* The "normal" case: untruncated algorithm. Nothing to do. */
1937 }
1938 else if( truncated < 4 )
1939 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001940 /* A very short MAC is too short for security since it can be
1941 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1942 * so we make this our minimum, even though 32 bits is still
1943 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001944 status = PSA_ERROR_NOT_SUPPORTED;
1945 }
1946 else if( truncated > operation->mac_size )
1947 {
1948 /* It's impossible to "truncate" to a larger length. */
1949 status = PSA_ERROR_INVALID_ARGUMENT;
1950 }
1951 else
1952 operation->mac_size = truncated;
1953
Gilles Peskinefbfac682018-07-08 20:51:54 +02001954exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001955 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001956 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001957 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001958 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001959 else
1960 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001961 operation->key_set = 1;
1962 }
1963 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001964}
1965
Gilles Peskine89167cb2018-07-08 20:12:23 +02001966psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001967 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001968 psa_algorithm_t alg )
1969{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001970 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001971}
1972
1973psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001974 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001975 psa_algorithm_t alg )
1976{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001977 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001978}
1979
Gilles Peskine8c9def32018-02-08 10:02:12 +01001980psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1981 const uint8_t *input,
1982 size_t input_length )
1983{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001984 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001985 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001986 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001987 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001988 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001989 operation->has_input = 1;
1990
Gilles Peskine8c9def32018-02-08 10:02:12 +01001991#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001992 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001993 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001994 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1995 input, input_length );
1996 status = mbedtls_to_psa_error( ret );
1997 }
1998 else
1999#endif /* MBEDTLS_CMAC_C */
2000#if defined(MBEDTLS_MD_C)
2001 if( PSA_ALG_IS_HMAC( operation->alg ) )
2002 {
2003 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2004 input_length );
2005 }
2006 else
2007#endif /* MBEDTLS_MD_C */
2008 {
2009 /* This shouldn't happen if `operation` was initialized by
2010 * a setup function. */
2011 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002012 }
2013
Gilles Peskinefbfac682018-07-08 20:51:54 +02002014cleanup:
2015 if( status != PSA_SUCCESS )
2016 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002017 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018}
2019
Gilles Peskine01126fa2018-07-12 17:04:55 +02002020#if defined(MBEDTLS_MD_C)
2021static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2022 uint8_t *mac,
2023 size_t mac_size )
2024{
2025 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2026 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2027 size_t hash_size = 0;
2028 size_t block_size = psa_get_hash_block_size( hash_alg );
2029 psa_status_t status;
2030
Gilles Peskine01126fa2018-07-12 17:04:55 +02002031 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2032 if( status != PSA_SUCCESS )
2033 return( status );
2034 /* From here on, tmp needs to be wiped. */
2035
2036 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2037 if( status != PSA_SUCCESS )
2038 goto exit;
2039
2040 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2041 if( status != PSA_SUCCESS )
2042 goto exit;
2043
2044 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2045 if( status != PSA_SUCCESS )
2046 goto exit;
2047
Gilles Peskined911eb72018-08-14 15:18:45 +02002048 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2049 if( status != PSA_SUCCESS )
2050 goto exit;
2051
2052 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002053
2054exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002055 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002056 return( status );
2057}
2058#endif /* MBEDTLS_MD_C */
2059
mohammad16036df908f2018-04-02 08:34:15 -07002060static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002061 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002062 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002063{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002064 if( ! operation->key_set )
2065 return( PSA_ERROR_BAD_STATE );
2066 if( operation->iv_required && ! operation->iv_set )
2067 return( PSA_ERROR_BAD_STATE );
2068
Gilles Peskine8c9def32018-02-08 10:02:12 +01002069 if( mac_size < operation->mac_size )
2070 return( PSA_ERROR_BUFFER_TOO_SMALL );
2071
Gilles Peskine8c9def32018-02-08 10:02:12 +01002072#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002073 if( operation->alg == PSA_ALG_CMAC )
2074 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002075 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2076 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2077 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002078 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002079 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002080 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002081 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002082 else
2083#endif /* MBEDTLS_CMAC_C */
2084#if defined(MBEDTLS_MD_C)
2085 if( PSA_ALG_IS_HMAC( operation->alg ) )
2086 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002087 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002088 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002089 }
2090 else
2091#endif /* MBEDTLS_MD_C */
2092 {
2093 /* This shouldn't happen if `operation` was initialized by
2094 * a setup function. */
2095 return( PSA_ERROR_BAD_STATE );
2096 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002097}
2098
Gilles Peskineacd4be32018-07-08 19:56:25 +02002099psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2100 uint8_t *mac,
2101 size_t mac_size,
2102 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002103{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002104 psa_status_t status;
2105
2106 /* Fill the output buffer with something that isn't a valid mac
2107 * (barring an attack on the mac and deliberately-crafted input),
2108 * in case the caller doesn't check the return status properly. */
2109 *mac_length = mac_size;
2110 /* If mac_size is 0 then mac may be NULL and then the
2111 * call to memset would have undefined behavior. */
2112 if( mac_size != 0 )
2113 memset( mac, '!', mac_size );
2114
Gilles Peskine89167cb2018-07-08 20:12:23 +02002115 if( ! operation->is_sign )
2116 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002117 status = PSA_ERROR_BAD_STATE;
2118 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002119 }
mohammad16036df908f2018-04-02 08:34:15 -07002120
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002121 status = psa_mac_finish_internal( operation, mac, mac_size );
2122
2123cleanup:
2124 if( status == PSA_SUCCESS )
2125 {
2126 status = psa_mac_abort( operation );
2127 if( status == PSA_SUCCESS )
2128 *mac_length = operation->mac_size;
2129 else
2130 memset( mac, '!', mac_size );
2131 }
2132 else
2133 psa_mac_abort( operation );
2134 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002135}
2136
Gilles Peskineacd4be32018-07-08 19:56:25 +02002137psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2138 const uint8_t *mac,
2139 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002140{
Gilles Peskine828ed142018-06-18 23:25:51 +02002141 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002142 psa_status_t status;
2143
Gilles Peskine89167cb2018-07-08 20:12:23 +02002144 if( operation->is_sign )
2145 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002146 status = PSA_ERROR_BAD_STATE;
2147 goto cleanup;
2148 }
2149 if( operation->mac_size != mac_length )
2150 {
2151 status = PSA_ERROR_INVALID_SIGNATURE;
2152 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002153 }
mohammad16036df908f2018-04-02 08:34:15 -07002154
2155 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002156 actual_mac, sizeof( actual_mac ) );
2157
2158 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2159 status = PSA_ERROR_INVALID_SIGNATURE;
2160
2161cleanup:
2162 if( status == PSA_SUCCESS )
2163 status = psa_mac_abort( operation );
2164 else
2165 psa_mac_abort( operation );
2166
Gilles Peskine3f108122018-12-07 18:14:53 +01002167 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002168
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002169 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002170}
2171
2172
Gilles Peskine20035e32018-02-03 22:44:14 +01002173
Gilles Peskine20035e32018-02-03 22:44:14 +01002174/****************************************************************/
2175/* Asymmetric cryptography */
2176/****************************************************************/
2177
Gilles Peskine2b450e32018-06-27 15:42:46 +02002178#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002179/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002180 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002181static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2182 size_t hash_length,
2183 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002184{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002185 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002186 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002187 *md_alg = mbedtls_md_get_type( md_info );
2188
2189 /* The Mbed TLS RSA module uses an unsigned int for hash length
2190 * parameters. Validate that it fits so that we don't risk an
2191 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002192#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002193 if( hash_length > UINT_MAX )
2194 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002195#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002196
2197#if defined(MBEDTLS_PKCS1_V15)
2198 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2199 * must be correct. */
2200 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2201 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002202 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002203 if( md_info == NULL )
2204 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002205 if( mbedtls_md_get_size( md_info ) != hash_length )
2206 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002207 }
2208#endif /* MBEDTLS_PKCS1_V15 */
2209
2210#if defined(MBEDTLS_PKCS1_V21)
2211 /* PSS requires a hash internally. */
2212 if( PSA_ALG_IS_RSA_PSS( alg ) )
2213 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002214 if( md_info == NULL )
2215 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002216 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002217#endif /* MBEDTLS_PKCS1_V21 */
2218
Gilles Peskine61b91d42018-06-08 16:09:36 +02002219 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002220}
2221
Gilles Peskine2b450e32018-06-27 15:42:46 +02002222static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2223 psa_algorithm_t alg,
2224 const uint8_t *hash,
2225 size_t hash_length,
2226 uint8_t *signature,
2227 size_t signature_size,
2228 size_t *signature_length )
2229{
2230 psa_status_t status;
2231 int ret;
2232 mbedtls_md_type_t md_alg;
2233
2234 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2235 if( status != PSA_SUCCESS )
2236 return( status );
2237
Gilles Peskine630a18a2018-06-29 17:49:35 +02002238 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002239 return( PSA_ERROR_BUFFER_TOO_SMALL );
2240
2241#if defined(MBEDTLS_PKCS1_V15)
2242 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2243 {
2244 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2245 MBEDTLS_MD_NONE );
2246 ret = mbedtls_rsa_pkcs1_sign( rsa,
2247 mbedtls_ctr_drbg_random,
2248 &global_data.ctr_drbg,
2249 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002250 md_alg,
2251 (unsigned int) hash_length,
2252 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002253 signature );
2254 }
2255 else
2256#endif /* MBEDTLS_PKCS1_V15 */
2257#if defined(MBEDTLS_PKCS1_V21)
2258 if( PSA_ALG_IS_RSA_PSS( alg ) )
2259 {
2260 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2261 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2262 mbedtls_ctr_drbg_random,
2263 &global_data.ctr_drbg,
2264 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002265 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002266 (unsigned int) hash_length,
2267 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002268 signature );
2269 }
2270 else
2271#endif /* MBEDTLS_PKCS1_V21 */
2272 {
2273 return( PSA_ERROR_INVALID_ARGUMENT );
2274 }
2275
2276 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002277 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002278 return( mbedtls_to_psa_error( ret ) );
2279}
2280
2281static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2282 psa_algorithm_t alg,
2283 const uint8_t *hash,
2284 size_t hash_length,
2285 const uint8_t *signature,
2286 size_t signature_length )
2287{
2288 psa_status_t status;
2289 int ret;
2290 mbedtls_md_type_t md_alg;
2291
2292 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2293 if( status != PSA_SUCCESS )
2294 return( status );
2295
Gilles Peskine630a18a2018-06-29 17:49:35 +02002296 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002297 return( PSA_ERROR_BUFFER_TOO_SMALL );
2298
2299#if defined(MBEDTLS_PKCS1_V15)
2300 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2301 {
2302 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2303 MBEDTLS_MD_NONE );
2304 ret = mbedtls_rsa_pkcs1_verify( rsa,
2305 mbedtls_ctr_drbg_random,
2306 &global_data.ctr_drbg,
2307 MBEDTLS_RSA_PUBLIC,
2308 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002309 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002310 hash,
2311 signature );
2312 }
2313 else
2314#endif /* MBEDTLS_PKCS1_V15 */
2315#if defined(MBEDTLS_PKCS1_V21)
2316 if( PSA_ALG_IS_RSA_PSS( alg ) )
2317 {
2318 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2319 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2320 mbedtls_ctr_drbg_random,
2321 &global_data.ctr_drbg,
2322 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002323 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002324 (unsigned int) hash_length,
2325 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002326 signature );
2327 }
2328 else
2329#endif /* MBEDTLS_PKCS1_V21 */
2330 {
2331 return( PSA_ERROR_INVALID_ARGUMENT );
2332 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002333
2334 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2335 * the rest of the signature is invalid". This has little use in
2336 * practice and PSA doesn't report this distinction. */
2337 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2338 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002339 return( mbedtls_to_psa_error( ret ) );
2340}
2341#endif /* MBEDTLS_RSA_C */
2342
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002343#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002344/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2345 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2346 * (even though these functions don't modify it). */
2347static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2348 psa_algorithm_t alg,
2349 const uint8_t *hash,
2350 size_t hash_length,
2351 uint8_t *signature,
2352 size_t signature_size,
2353 size_t *signature_length )
2354{
2355 int ret;
2356 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002357 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002358 mbedtls_mpi_init( &r );
2359 mbedtls_mpi_init( &s );
2360
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002361 if( signature_size < 2 * curve_bytes )
2362 {
2363 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2364 goto cleanup;
2365 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002366
Gilles Peskinea05219c2018-11-16 16:02:56 +01002367#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002368 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2369 {
2370 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2371 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2372 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2373 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2374 hash, hash_length,
2375 md_alg ) );
2376 }
2377 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002378#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002379 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002380 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002381 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2382 hash, hash_length,
2383 mbedtls_ctr_drbg_random,
2384 &global_data.ctr_drbg ) );
2385 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002386
2387 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2388 signature,
2389 curve_bytes ) );
2390 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2391 signature + curve_bytes,
2392 curve_bytes ) );
2393
2394cleanup:
2395 mbedtls_mpi_free( &r );
2396 mbedtls_mpi_free( &s );
2397 if( ret == 0 )
2398 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002399 return( mbedtls_to_psa_error( ret ) );
2400}
2401
2402static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2403 const uint8_t *hash,
2404 size_t hash_length,
2405 const uint8_t *signature,
2406 size_t signature_length )
2407{
2408 int ret;
2409 mbedtls_mpi r, s;
2410 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2411 mbedtls_mpi_init( &r );
2412 mbedtls_mpi_init( &s );
2413
2414 if( signature_length != 2 * curve_bytes )
2415 return( PSA_ERROR_INVALID_SIGNATURE );
2416
2417 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2418 signature,
2419 curve_bytes ) );
2420 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2421 signature + curve_bytes,
2422 curve_bytes ) );
2423
2424 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2425 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002426
2427cleanup:
2428 mbedtls_mpi_free( &r );
2429 mbedtls_mpi_free( &s );
2430 return( mbedtls_to_psa_error( ret ) );
2431}
2432#endif /* MBEDTLS_ECDSA_C */
2433
Gilles Peskinec5487a82018-12-03 18:08:14 +01002434psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002435 psa_algorithm_t alg,
2436 const uint8_t *hash,
2437 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002438 uint8_t *signature,
2439 size_t signature_size,
2440 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002441{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002442 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002443 psa_status_t status;
2444
2445 *signature_length = signature_size;
2446
Gilles Peskinec5487a82018-12-03 18:08:14 +01002447 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002448 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002449 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002450 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002451 {
2452 status = PSA_ERROR_INVALID_ARGUMENT;
2453 goto exit;
2454 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002455
Gilles Peskine20035e32018-02-03 22:44:14 +01002456#if defined(MBEDTLS_RSA_C)
2457 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2458 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002459 status = psa_rsa_sign( slot->data.rsa,
2460 alg,
2461 hash, hash_length,
2462 signature, signature_size,
2463 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002464 }
2465 else
2466#endif /* defined(MBEDTLS_RSA_C) */
2467#if defined(MBEDTLS_ECP_C)
2468 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2469 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002470#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002471 if(
2472#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2473 PSA_ALG_IS_ECDSA( alg )
2474#else
2475 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2476#endif
2477 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002478 status = psa_ecdsa_sign( slot->data.ecp,
2479 alg,
2480 hash, hash_length,
2481 signature, signature_size,
2482 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002483 else
2484#endif /* defined(MBEDTLS_ECDSA_C) */
2485 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002486 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002487 }
itayzafrir5c753392018-05-08 11:18:38 +03002488 }
2489 else
2490#endif /* defined(MBEDTLS_ECP_C) */
2491 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002492 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002493 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002494
2495exit:
2496 /* Fill the unused part of the output buffer (the whole buffer on error,
2497 * the trailing part on success) with something that isn't a valid mac
2498 * (barring an attack on the mac and deliberately-crafted input),
2499 * in case the caller doesn't check the return status properly. */
2500 if( status == PSA_SUCCESS )
2501 memset( signature + *signature_length, '!',
2502 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002503 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002504 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002505 /* If signature_size is 0 then we have nothing to do. We must not call
2506 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002507 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002508}
2509
Gilles Peskinec5487a82018-12-03 18:08:14 +01002510psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002511 psa_algorithm_t alg,
2512 const uint8_t *hash,
2513 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002514 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002515 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002516{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002517 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002518 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002519
Gilles Peskinec5487a82018-12-03 18:08:14 +01002520 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002521 if( status != PSA_SUCCESS )
2522 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002523
Gilles Peskine61b91d42018-06-08 16:09:36 +02002524#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002525 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002526 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002527 return( psa_rsa_verify( slot->data.rsa,
2528 alg,
2529 hash, hash_length,
2530 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002531 }
2532 else
2533#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002534#if defined(MBEDTLS_ECP_C)
2535 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2536 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002537#if defined(MBEDTLS_ECDSA_C)
2538 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002539 return( psa_ecdsa_verify( slot->data.ecp,
2540 hash, hash_length,
2541 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002542 else
2543#endif /* defined(MBEDTLS_ECDSA_C) */
2544 {
2545 return( PSA_ERROR_INVALID_ARGUMENT );
2546 }
itayzafrir5c753392018-05-08 11:18:38 +03002547 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002548 else
2549#endif /* defined(MBEDTLS_ECP_C) */
2550 {
2551 return( PSA_ERROR_NOT_SUPPORTED );
2552 }
2553}
2554
Gilles Peskine072ac562018-06-30 00:21:29 +02002555#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2556static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2557 mbedtls_rsa_context *rsa )
2558{
2559 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2560 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2561 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2562 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2563}
2564#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2565
Gilles Peskinec5487a82018-12-03 18:08:14 +01002566psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002567 psa_algorithm_t alg,
2568 const uint8_t *input,
2569 size_t input_length,
2570 const uint8_t *salt,
2571 size_t salt_length,
2572 uint8_t *output,
2573 size_t output_size,
2574 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002575{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002576 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002577 psa_status_t status;
2578
Darryl Green5cc689a2018-07-24 15:34:10 +01002579 (void) input;
2580 (void) input_length;
2581 (void) salt;
2582 (void) output;
2583 (void) output_size;
2584
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002585 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002586
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002587 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2588 return( PSA_ERROR_INVALID_ARGUMENT );
2589
Gilles Peskinec5487a82018-12-03 18:08:14 +01002590 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002591 if( status != PSA_SUCCESS )
2592 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002593 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2594 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002595 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002596
2597#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002598 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002599 {
2600 mbedtls_rsa_context *rsa = slot->data.rsa;
2601 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002602 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002603 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002604#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002605 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002606 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002607 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2608 mbedtls_ctr_drbg_random,
2609 &global_data.ctr_drbg,
2610 MBEDTLS_RSA_PUBLIC,
2611 input_length,
2612 input,
2613 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002614 }
2615 else
2616#endif /* MBEDTLS_PKCS1_V15 */
2617#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002618 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002619 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002620 psa_rsa_oaep_set_padding_mode( alg, rsa );
2621 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2622 mbedtls_ctr_drbg_random,
2623 &global_data.ctr_drbg,
2624 MBEDTLS_RSA_PUBLIC,
2625 salt, salt_length,
2626 input_length,
2627 input,
2628 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002629 }
2630 else
2631#endif /* MBEDTLS_PKCS1_V21 */
2632 {
2633 return( PSA_ERROR_INVALID_ARGUMENT );
2634 }
2635 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002636 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002637 return( mbedtls_to_psa_error( ret ) );
2638 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002639 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002640#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002641 {
2642 return( PSA_ERROR_NOT_SUPPORTED );
2643 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002644}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002645
Gilles Peskinec5487a82018-12-03 18:08:14 +01002646psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002647 psa_algorithm_t alg,
2648 const uint8_t *input,
2649 size_t input_length,
2650 const uint8_t *salt,
2651 size_t salt_length,
2652 uint8_t *output,
2653 size_t output_size,
2654 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002655{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002656 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002657 psa_status_t status;
2658
Darryl Green5cc689a2018-07-24 15:34:10 +01002659 (void) input;
2660 (void) input_length;
2661 (void) salt;
2662 (void) output;
2663 (void) output_size;
2664
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002665 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002666
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002667 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2668 return( PSA_ERROR_INVALID_ARGUMENT );
2669
Gilles Peskinec5487a82018-12-03 18:08:14 +01002670 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002671 if( status != PSA_SUCCESS )
2672 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002673 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2674 return( PSA_ERROR_INVALID_ARGUMENT );
2675
2676#if defined(MBEDTLS_RSA_C)
2677 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2678 {
2679 mbedtls_rsa_context *rsa = slot->data.rsa;
2680 int ret;
2681
Gilles Peskine630a18a2018-06-29 17:49:35 +02002682 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002683 return( PSA_ERROR_INVALID_ARGUMENT );
2684
2685#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002686 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002687 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002688 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2689 mbedtls_ctr_drbg_random,
2690 &global_data.ctr_drbg,
2691 MBEDTLS_RSA_PRIVATE,
2692 output_length,
2693 input,
2694 output,
2695 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002696 }
2697 else
2698#endif /* MBEDTLS_PKCS1_V15 */
2699#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002700 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002701 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002702 psa_rsa_oaep_set_padding_mode( alg, rsa );
2703 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2704 mbedtls_ctr_drbg_random,
2705 &global_data.ctr_drbg,
2706 MBEDTLS_RSA_PRIVATE,
2707 salt, salt_length,
2708 output_length,
2709 input,
2710 output,
2711 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002712 }
2713 else
2714#endif /* MBEDTLS_PKCS1_V21 */
2715 {
2716 return( PSA_ERROR_INVALID_ARGUMENT );
2717 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002718
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002719 return( mbedtls_to_psa_error( ret ) );
2720 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002721 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002722#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002723 {
2724 return( PSA_ERROR_NOT_SUPPORTED );
2725 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002726}
Gilles Peskine20035e32018-02-03 22:44:14 +01002727
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002728
2729
mohammad1603503973b2018-03-12 15:59:30 +02002730/****************************************************************/
2731/* Symmetric cryptography */
2732/****************************************************************/
2733
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002734/* Initialize the cipher operation structure. Once this function has been
2735 * called, psa_cipher_abort can run and will do the right thing. */
2736static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2737 psa_algorithm_t alg )
2738{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002739 if( ! PSA_ALG_IS_CIPHER( alg ) )
2740 {
2741 memset( operation, 0, sizeof( *operation ) );
2742 return( PSA_ERROR_INVALID_ARGUMENT );
2743 }
2744
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002745 operation->alg = alg;
2746 operation->key_set = 0;
2747 operation->iv_set = 0;
2748 operation->iv_required = 1;
2749 operation->iv_size = 0;
2750 operation->block_size = 0;
2751 mbedtls_cipher_init( &operation->ctx.cipher );
2752 return( PSA_SUCCESS );
2753}
2754
Gilles Peskinee553c652018-06-04 16:22:46 +02002755static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002756 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002757 psa_algorithm_t alg,
2758 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002759{
2760 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2761 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002762 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002763 size_t key_bits;
2764 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002765 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2766 PSA_KEY_USAGE_ENCRYPT :
2767 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002768
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002769 status = psa_cipher_init( operation, alg );
2770 if( status != PSA_SUCCESS )
2771 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002772
Gilles Peskinec5487a82018-12-03 18:08:14 +01002773 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002774 if( status != PSA_SUCCESS )
2775 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002776 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002777
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002778 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002779 if( cipher_info == NULL )
2780 return( PSA_ERROR_NOT_SUPPORTED );
2781
mohammad1603503973b2018-03-12 15:59:30 +02002782 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002783 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002784 {
2785 psa_cipher_abort( operation );
2786 return( mbedtls_to_psa_error( ret ) );
2787 }
2788
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002789#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002790 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002791 {
2792 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2793 unsigned char keys[24];
2794 memcpy( keys, slot->data.raw.data, 16 );
2795 memcpy( keys + 16, slot->data.raw.data, 8 );
2796 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2797 keys,
2798 192, cipher_operation );
2799 }
2800 else
2801#endif
2802 {
2803 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2804 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002805 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002806 }
Moran Peker41deec42018-04-04 15:43:05 +03002807 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002808 {
2809 psa_cipher_abort( operation );
2810 return( mbedtls_to_psa_error( ret ) );
2811 }
2812
mohammad16038481e742018-03-18 13:57:31 +02002813#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002814 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002815 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002816 case PSA_ALG_CBC_NO_PADDING:
2817 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2818 MBEDTLS_PADDING_NONE );
2819 break;
2820 case PSA_ALG_CBC_PKCS7:
2821 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2822 MBEDTLS_PADDING_PKCS7 );
2823 break;
2824 default:
2825 /* The algorithm doesn't involve padding. */
2826 ret = 0;
2827 break;
2828 }
2829 if( ret != 0 )
2830 {
2831 psa_cipher_abort( operation );
2832 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002833 }
2834#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2835
mohammad1603503973b2018-03-12 15:59:30 +02002836 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002837 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2838 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2839 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002840 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002841 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002842 }
mohammad1603503973b2018-03-12 15:59:30 +02002843
Moran Peker395db872018-05-31 14:07:14 +03002844 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002845}
2846
Gilles Peskinefe119512018-07-08 21:39:34 +02002847psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002848 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002849 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002850{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002851 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002852}
2853
Gilles Peskinefe119512018-07-08 21:39:34 +02002854psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002855 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002856 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002857{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002858 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002859}
2860
Gilles Peskinefe119512018-07-08 21:39:34 +02002861psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2862 unsigned char *iv,
2863 size_t iv_size,
2864 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002865{
itayzafrir534bd7c2018-08-02 13:56:32 +03002866 psa_status_t status;
2867 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002868 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002869 {
2870 status = PSA_ERROR_BAD_STATE;
2871 goto exit;
2872 }
Moran Peker41deec42018-04-04 15:43:05 +03002873 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002874 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002875 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002876 goto exit;
2877 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002878 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2879 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002880 if( ret != 0 )
2881 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002882 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002883 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002884 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002885
mohammad16038481e742018-03-18 13:57:31 +02002886 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002887 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002888
Moran Peker395db872018-05-31 14:07:14 +03002889exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002890 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002891 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002892 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002893}
2894
Gilles Peskinefe119512018-07-08 21:39:34 +02002895psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2896 const unsigned char *iv,
2897 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002898{
itayzafrir534bd7c2018-08-02 13:56:32 +03002899 psa_status_t status;
2900 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002901 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002902 {
2903 status = PSA_ERROR_BAD_STATE;
2904 goto exit;
2905 }
Moran Pekera28258c2018-05-29 16:25:04 +03002906 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002907 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002908 status = PSA_ERROR_INVALID_ARGUMENT;
2909 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002910 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002911 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2912 status = mbedtls_to_psa_error( ret );
2913exit:
2914 if( status == PSA_SUCCESS )
2915 operation->iv_set = 1;
2916 else
mohammad1603503973b2018-03-12 15:59:30 +02002917 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002918 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002919}
2920
Gilles Peskinee553c652018-06-04 16:22:46 +02002921psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2922 const uint8_t *input,
2923 size_t input_length,
2924 unsigned char *output,
2925 size_t output_size,
2926 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002927{
itayzafrir534bd7c2018-08-02 13:56:32 +03002928 psa_status_t status;
2929 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002930 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002931 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002932 {
2933 /* Take the unprocessed partial block left over from previous
2934 * update calls, if any, plus the input to this call. Remove
2935 * the last partial block, if any. You get the data that will be
2936 * output in this call. */
2937 expected_output_size =
2938 ( operation->ctx.cipher.unprocessed_len + input_length )
2939 / operation->block_size * operation->block_size;
2940 }
2941 else
2942 {
2943 expected_output_size = input_length;
2944 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002945
Gilles Peskine89d789c2018-06-04 17:17:16 +02002946 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002947 {
2948 status = PSA_ERROR_BUFFER_TOO_SMALL;
2949 goto exit;
2950 }
mohammad160382759612018-03-12 18:16:40 +02002951
mohammad1603503973b2018-03-12 15:59:30 +02002952 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002953 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002954 status = mbedtls_to_psa_error( ret );
2955exit:
2956 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002957 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002958 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002959}
2960
Gilles Peskinee553c652018-06-04 16:22:46 +02002961psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2962 uint8_t *output,
2963 size_t output_size,
2964 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002965{
Janos Follath315b51c2018-07-09 16:04:51 +01002966 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2967 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002968 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002969
mohammad1603503973b2018-03-12 15:59:30 +02002970 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002971 {
Janos Follath315b51c2018-07-09 16:04:51 +01002972 status = PSA_ERROR_BAD_STATE;
2973 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002974 }
2975 if( operation->iv_required && ! operation->iv_set )
2976 {
Janos Follath315b51c2018-07-09 16:04:51 +01002977 status = PSA_ERROR_BAD_STATE;
2978 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002979 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002980
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002981 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002982 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2983 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002984 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002985 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002986 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002987 }
2988
Janos Follath315b51c2018-07-09 16:04:51 +01002989 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2990 temp_output_buffer,
2991 output_length );
2992 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002993 {
Janos Follath315b51c2018-07-09 16:04:51 +01002994 status = mbedtls_to_psa_error( cipher_ret );
2995 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002996 }
Janos Follath315b51c2018-07-09 16:04:51 +01002997
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002998 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002999 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003000 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003001 memcpy( output, temp_output_buffer, *output_length );
3002 else
3003 {
Janos Follath315b51c2018-07-09 16:04:51 +01003004 status = PSA_ERROR_BUFFER_TOO_SMALL;
3005 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003006 }
mohammad1603503973b2018-03-12 15:59:30 +02003007
Gilles Peskine3f108122018-12-07 18:14:53 +01003008 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003009 status = psa_cipher_abort( operation );
3010
3011 return( status );
3012
3013error:
3014
3015 *output_length = 0;
3016
Gilles Peskine3f108122018-12-07 18:14:53 +01003017 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003018 (void) psa_cipher_abort( operation );
3019
3020 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003021}
3022
Gilles Peskinee553c652018-06-04 16:22:46 +02003023psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3024{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003025 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003026 {
3027 /* The object has (apparently) been initialized but it is not
3028 * in use. It's ok to call abort on such an object, and there's
3029 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003030 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003031 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003032
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003033 /* Sanity check (shouldn't happen: operation->alg should
3034 * always have been initialized to a valid value). */
3035 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3036 return( PSA_ERROR_BAD_STATE );
3037
mohammad1603503973b2018-03-12 15:59:30 +02003038 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003039
Moran Peker41deec42018-04-04 15:43:05 +03003040 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003041 operation->key_set = 0;
3042 operation->iv_set = 0;
3043 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003044 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003045 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003046
Moran Peker395db872018-05-31 14:07:14 +03003047 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003048}
3049
Gilles Peskinea0655c32018-04-30 17:06:50 +02003050
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003051
mohammad16038cc1cee2018-03-28 01:21:33 +03003052/****************************************************************/
3053/* Key Policy */
3054/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003055
mohammad160327010052018-07-03 13:16:15 +03003056#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003057void psa_key_policy_set_usage( psa_key_policy_t *policy,
3058 psa_key_usage_t usage,
3059 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003060{
mohammad16034eed7572018-03-28 05:14:59 -07003061 policy->usage = usage;
3062 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003063}
3064
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003065psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003066{
mohammad16036df908f2018-04-02 08:34:15 -07003067 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003068}
3069
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003070psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003071{
mohammad16036df908f2018-04-02 08:34:15 -07003072 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003073}
mohammad160327010052018-07-03 13:16:15 +03003074#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003075
Gilles Peskinec5487a82018-12-03 18:08:14 +01003076psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003077 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003078{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003079 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003080 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003081
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003082 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003083 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003084
Gilles Peskinec5487a82018-12-03 18:08:14 +01003085 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003086 if( status != PSA_SUCCESS )
3087 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003088
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003089 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3090 PSA_KEY_USAGE_ENCRYPT |
3091 PSA_KEY_USAGE_DECRYPT |
3092 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003093 PSA_KEY_USAGE_VERIFY |
3094 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003095 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003096
mohammad16036df908f2018-04-02 08:34:15 -07003097 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003098
3099 return( PSA_SUCCESS );
3100}
3101
Gilles Peskinec5487a82018-12-03 18:08:14 +01003102psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003103 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003104{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003105 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003106 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003107
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003108 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003109 return( PSA_ERROR_INVALID_ARGUMENT );
3110
Gilles Peskinec5487a82018-12-03 18:08:14 +01003111 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003112 if( status != PSA_SUCCESS )
3113 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003114
mohammad16036df908f2018-04-02 08:34:15 -07003115 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003116
3117 return( PSA_SUCCESS );
3118}
Gilles Peskine20035e32018-02-03 22:44:14 +01003119
Gilles Peskinea0655c32018-04-30 17:06:50 +02003120
3121
mohammad1603804cd712018-03-20 22:44:08 +02003122/****************************************************************/
3123/* Key Lifetime */
3124/****************************************************************/
3125
Gilles Peskinec5487a82018-12-03 18:08:14 +01003126psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003127 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003128{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003129 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003130 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003131
Gilles Peskinec5487a82018-12-03 18:08:14 +01003132 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003133 if( status != PSA_SUCCESS )
3134 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003135
mohammad1603804cd712018-03-20 22:44:08 +02003136 *lifetime = slot->lifetime;
3137
3138 return( PSA_SUCCESS );
3139}
3140
Gilles Peskine20035e32018-02-03 22:44:14 +01003141
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003142
mohammad16035955c982018-04-26 00:53:03 +03003143/****************************************************************/
3144/* AEAD */
3145/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003146
Gilles Peskineedf9a652018-08-17 18:11:56 +02003147typedef struct
3148{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003149 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003150 const mbedtls_cipher_info_t *cipher_info;
3151 union
3152 {
3153#if defined(MBEDTLS_CCM_C)
3154 mbedtls_ccm_context ccm;
3155#endif /* MBEDTLS_CCM_C */
3156#if defined(MBEDTLS_GCM_C)
3157 mbedtls_gcm_context gcm;
3158#endif /* MBEDTLS_GCM_C */
3159 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003160 psa_algorithm_t core_alg;
3161 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003162 uint8_t tag_length;
3163} aead_operation_t;
3164
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003165static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003166{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003167 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003168 {
3169#if defined(MBEDTLS_CCM_C)
3170 case PSA_ALG_CCM:
3171 mbedtls_ccm_free( &operation->ctx.ccm );
3172 break;
3173#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003174#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003175 case PSA_ALG_GCM:
3176 mbedtls_gcm_free( &operation->ctx.gcm );
3177 break;
3178#endif /* MBEDTLS_GCM_C */
3179 }
3180}
3181
3182static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003183 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003184 psa_key_usage_t usage,
3185 psa_algorithm_t alg )
3186{
3187 psa_status_t status;
3188 size_t key_bits;
3189 mbedtls_cipher_id_t cipher_id;
3190
Gilles Peskinec5487a82018-12-03 18:08:14 +01003191 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003192 if( status != PSA_SUCCESS )
3193 return( status );
3194
3195 key_bits = psa_get_key_bits( operation->slot );
3196
3197 operation->cipher_info =
3198 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3199 &cipher_id );
3200 if( operation->cipher_info == NULL )
3201 return( PSA_ERROR_NOT_SUPPORTED );
3202
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003203 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003204 {
3205#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003206 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3207 operation->core_alg = PSA_ALG_CCM;
3208 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003209 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3210 return( PSA_ERROR_INVALID_ARGUMENT );
3211 mbedtls_ccm_init( &operation->ctx.ccm );
3212 status = mbedtls_to_psa_error(
3213 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3214 operation->slot->data.raw.data,
3215 (unsigned int) key_bits ) );
3216 if( status != 0 )
3217 goto cleanup;
3218 break;
3219#endif /* MBEDTLS_CCM_C */
3220
3221#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003222 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3223 operation->core_alg = PSA_ALG_GCM;
3224 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003225 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3226 return( PSA_ERROR_INVALID_ARGUMENT );
3227 mbedtls_gcm_init( &operation->ctx.gcm );
3228 status = mbedtls_to_psa_error(
3229 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3230 operation->slot->data.raw.data,
3231 (unsigned int) key_bits ) );
3232 break;
3233#endif /* MBEDTLS_GCM_C */
3234
3235 default:
3236 return( PSA_ERROR_NOT_SUPPORTED );
3237 }
3238
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003239 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3240 {
3241 status = PSA_ERROR_INVALID_ARGUMENT;
3242 goto cleanup;
3243 }
3244 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3245 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3246 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3247 * In both cases, mbedtls_xxx will validate the tag length below. */
3248
Gilles Peskineedf9a652018-08-17 18:11:56 +02003249 return( PSA_SUCCESS );
3250
3251cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003252 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003253 return( status );
3254}
3255
Gilles Peskinec5487a82018-12-03 18:08:14 +01003256psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003257 psa_algorithm_t alg,
3258 const uint8_t *nonce,
3259 size_t nonce_length,
3260 const uint8_t *additional_data,
3261 size_t additional_data_length,
3262 const uint8_t *plaintext,
3263 size_t plaintext_length,
3264 uint8_t *ciphertext,
3265 size_t ciphertext_size,
3266 size_t *ciphertext_length )
3267{
mohammad16035955c982018-04-26 00:53:03 +03003268 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003269 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003270 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003271
mohammad1603f08a5502018-06-03 15:05:47 +03003272 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003273
Gilles Peskinec5487a82018-12-03 18:08:14 +01003274 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003275 if( status != PSA_SUCCESS )
3276 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003277
Gilles Peskineedf9a652018-08-17 18:11:56 +02003278 /* For all currently supported modes, the tag is at the end of the
3279 * ciphertext. */
3280 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3281 {
3282 status = PSA_ERROR_BUFFER_TOO_SMALL;
3283 goto exit;
3284 }
3285 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003286
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003287#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003288 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003289 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003290 status = mbedtls_to_psa_error(
3291 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3292 MBEDTLS_GCM_ENCRYPT,
3293 plaintext_length,
3294 nonce, nonce_length,
3295 additional_data, additional_data_length,
3296 plaintext, ciphertext,
3297 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003298 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003299 else
3300#endif /* MBEDTLS_GCM_C */
3301#if defined(MBEDTLS_CCM_C)
3302 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003303 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003304 status = mbedtls_to_psa_error(
3305 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3306 plaintext_length,
3307 nonce, nonce_length,
3308 additional_data,
3309 additional_data_length,
3310 plaintext, ciphertext,
3311 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003312 }
mohammad16035c8845f2018-05-09 05:40:09 -07003313 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003314#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003315 {
mohammad1603554faad2018-06-03 15:07:38 +03003316 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003317 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003318
Gilles Peskineedf9a652018-08-17 18:11:56 +02003319 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3320 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003321
Gilles Peskineedf9a652018-08-17 18:11:56 +02003322exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003323 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003324 if( status == PSA_SUCCESS )
3325 *ciphertext_length = plaintext_length + operation.tag_length;
3326 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003327}
3328
Gilles Peskineee652a32018-06-01 19:23:52 +02003329/* Locate the tag in a ciphertext buffer containing the encrypted data
3330 * followed by the tag. Return the length of the part preceding the tag in
3331 * *plaintext_length. This is the size of the plaintext in modes where
3332 * the encrypted data has the same size as the plaintext, such as
3333 * CCM and GCM. */
3334static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3335 const uint8_t *ciphertext,
3336 size_t ciphertext_length,
3337 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003338 const uint8_t **p_tag )
3339{
3340 size_t payload_length;
3341 if( tag_length > ciphertext_length )
3342 return( PSA_ERROR_INVALID_ARGUMENT );
3343 payload_length = ciphertext_length - tag_length;
3344 if( payload_length > plaintext_size )
3345 return( PSA_ERROR_BUFFER_TOO_SMALL );
3346 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003347 return( PSA_SUCCESS );
3348}
3349
Gilles Peskinec5487a82018-12-03 18:08:14 +01003350psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003351 psa_algorithm_t alg,
3352 const uint8_t *nonce,
3353 size_t nonce_length,
3354 const uint8_t *additional_data,
3355 size_t additional_data_length,
3356 const uint8_t *ciphertext,
3357 size_t ciphertext_length,
3358 uint8_t *plaintext,
3359 size_t plaintext_size,
3360 size_t *plaintext_length )
3361{
mohammad16035955c982018-04-26 00:53:03 +03003362 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003363 aead_operation_t operation;
3364 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003365
Gilles Peskineee652a32018-06-01 19:23:52 +02003366 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003367
Gilles Peskinec5487a82018-12-03 18:08:14 +01003368 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003369 if( status != PSA_SUCCESS )
3370 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003371
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003372#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003373 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003374 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003375 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003376 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003377 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003378 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003379 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003380
Gilles Peskineedf9a652018-08-17 18:11:56 +02003381 status = mbedtls_to_psa_error(
3382 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3383 ciphertext_length - operation.tag_length,
3384 nonce, nonce_length,
3385 additional_data,
3386 additional_data_length,
3387 tag, operation.tag_length,
3388 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003389 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003390 else
3391#endif /* MBEDTLS_GCM_C */
3392#if defined(MBEDTLS_CCM_C)
3393 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003394 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003395 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003396 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003397 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003398 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003399 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003400
Gilles Peskineedf9a652018-08-17 18:11:56 +02003401 status = mbedtls_to_psa_error(
3402 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3403 ciphertext_length - operation.tag_length,
3404 nonce, nonce_length,
3405 additional_data,
3406 additional_data_length,
3407 ciphertext, plaintext,
3408 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003409 }
mohammad160339574652018-06-01 04:39:53 -07003410 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003411#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003412 {
mohammad1603554faad2018-06-03 15:07:38 +03003413 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003414 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003415
Gilles Peskineedf9a652018-08-17 18:11:56 +02003416 if( status != PSA_SUCCESS && plaintext_size != 0 )
3417 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003418
Gilles Peskineedf9a652018-08-17 18:11:56 +02003419exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003420 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003421 if( status == PSA_SUCCESS )
3422 *plaintext_length = ciphertext_length - operation.tag_length;
3423 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003424}
3425
Gilles Peskinea0655c32018-04-30 17:06:50 +02003426
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003427
Gilles Peskine20035e32018-02-03 22:44:14 +01003428/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003429/* Generators */
3430/****************************************************************/
3431
3432psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3433{
3434 psa_status_t status = PSA_SUCCESS;
3435 if( generator->alg == 0 )
3436 {
3437 /* The object has (apparently) been initialized but it is not
3438 * in use. It's ok to call abort on such an object, and there's
3439 * nothing to do. */
3440 }
3441 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003442 if( generator->alg == PSA_ALG_SELECT_RAW )
3443 {
3444 if( generator->ctx.buffer.data != NULL )
3445 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003446 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003447 generator->ctx.buffer.size );
3448 mbedtls_free( generator->ctx.buffer.data );
3449 }
3450 }
3451 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003452#if defined(MBEDTLS_MD_C)
3453 if( PSA_ALG_IS_HKDF( generator->alg ) )
3454 {
3455 mbedtls_free( generator->ctx.hkdf.info );
3456 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3457 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003458 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3459 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3460 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003461 {
3462 if( generator->ctx.tls12_prf.key != NULL )
3463 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003464 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003465 generator->ctx.tls12_prf.key_len );
3466 mbedtls_free( generator->ctx.tls12_prf.key );
3467 }
Hanno Becker580fba12018-11-13 20:50:45 +00003468
3469 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3470 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003471 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003472 generator->ctx.tls12_prf.Ai_with_seed_len );
3473 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3474 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003475 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003476 else
3477#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003478 {
3479 status = PSA_ERROR_BAD_STATE;
3480 }
3481 memset( generator, 0, sizeof( *generator ) );
3482 return( status );
3483}
3484
3485
3486psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3487 size_t *capacity)
3488{
3489 *capacity = generator->capacity;
3490 return( PSA_SUCCESS );
3491}
3492
Gilles Peskinebef7f142018-07-12 17:22:21 +02003493#if defined(MBEDTLS_MD_C)
3494/* Read some bytes from an HKDF-based generator. This performs a chunk
3495 * of the expand phase of the HKDF algorithm. */
3496static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3497 psa_algorithm_t hash_alg,
3498 uint8_t *output,
3499 size_t output_length )
3500{
3501 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3502 psa_status_t status;
3503
3504 while( output_length != 0 )
3505 {
3506 /* Copy what remains of the current block */
3507 uint8_t n = hash_length - hkdf->offset_in_block;
3508 if( n > output_length )
3509 n = (uint8_t) output_length;
3510 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3511 output += n;
3512 output_length -= n;
3513 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003514 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003515 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003516 /* We can't be wanting more output after block 0xff, otherwise
3517 * the capacity check in psa_generator_read() would have
3518 * prevented this call. It could happen only if the generator
3519 * object was corrupted or if this function is called directly
3520 * inside the library. */
3521 if( hkdf->block_number == 0xff )
3522 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003523
3524 /* We need a new block */
3525 ++hkdf->block_number;
3526 hkdf->offset_in_block = 0;
3527 status = psa_hmac_setup_internal( &hkdf->hmac,
3528 hkdf->prk, hash_length,
3529 hash_alg );
3530 if( status != PSA_SUCCESS )
3531 return( status );
3532 if( hkdf->block_number != 1 )
3533 {
3534 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3535 hkdf->output_block,
3536 hash_length );
3537 if( status != PSA_SUCCESS )
3538 return( status );
3539 }
3540 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3541 hkdf->info,
3542 hkdf->info_length );
3543 if( status != PSA_SUCCESS )
3544 return( status );
3545 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3546 &hkdf->block_number, 1 );
3547 if( status != PSA_SUCCESS )
3548 return( status );
3549 status = psa_hmac_finish_internal( &hkdf->hmac,
3550 hkdf->output_block,
3551 sizeof( hkdf->output_block ) );
3552 if( status != PSA_SUCCESS )
3553 return( status );
3554 }
3555
3556 return( PSA_SUCCESS );
3557}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003558
3559static psa_status_t psa_generator_tls12_prf_generate_next_block(
3560 psa_tls12_prf_generator_t *tls12_prf,
3561 psa_algorithm_t alg )
3562{
3563 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3564 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3565 psa_hmac_internal_data hmac;
3566 psa_status_t status, cleanup_status;
3567
Hanno Becker3b339e22018-11-13 20:56:14 +00003568 unsigned char *Ai;
3569 size_t Ai_len;
3570
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003571 /* We can't be wanting more output after block 0xff, otherwise
3572 * the capacity check in psa_generator_read() would have
3573 * prevented this call. It could happen only if the generator
3574 * object was corrupted or if this function is called directly
3575 * inside the library. */
3576 if( tls12_prf->block_number == 0xff )
3577 return( PSA_ERROR_BAD_STATE );
3578
3579 /* We need a new block */
3580 ++tls12_prf->block_number;
3581 tls12_prf->offset_in_block = 0;
3582
3583 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3584 *
3585 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3586 *
3587 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3588 * HMAC_hash(secret, A(2) + seed) +
3589 * HMAC_hash(secret, A(3) + seed) + ...
3590 *
3591 * A(0) = seed
3592 * A(i) = HMAC_hash( secret, A(i-1) )
3593 *
3594 * The `psa_tls12_prf_generator` structures saves the block
3595 * `HMAC_hash(secret, A(i) + seed)` from which the output
3596 * is currently extracted as `output_block`, while
3597 * `A(i) + seed` is stored in `Ai_with_seed`.
3598 *
3599 * Generating a new block means recalculating `Ai_with_seed`
3600 * from the A(i)-part of it, and afterwards recalculating
3601 * `output_block`.
3602 *
3603 * A(0) is computed at setup time.
3604 *
3605 */
3606
3607 psa_hmac_init_internal( &hmac );
3608
3609 /* We must distinguish the calculation of A(1) from those
3610 * of A(2) and higher, because A(0)=seed has a different
3611 * length than the other A(i). */
3612 if( tls12_prf->block_number == 1 )
3613 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003614 Ai = tls12_prf->Ai_with_seed + hash_length;
3615 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003616 }
3617 else
3618 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003619 Ai = tls12_prf->Ai_with_seed;
3620 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003621 }
3622
Hanno Becker3b339e22018-11-13 20:56:14 +00003623 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3624 status = psa_hmac_setup_internal( &hmac,
3625 tls12_prf->key,
3626 tls12_prf->key_len,
3627 hash_alg );
3628 if( status != PSA_SUCCESS )
3629 goto cleanup;
3630
3631 status = psa_hash_update( &hmac.hash_ctx,
3632 Ai, Ai_len );
3633 if( status != PSA_SUCCESS )
3634 goto cleanup;
3635
3636 status = psa_hmac_finish_internal( &hmac,
3637 tls12_prf->Ai_with_seed,
3638 hash_length );
3639 if( status != PSA_SUCCESS )
3640 goto cleanup;
3641
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003642 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3643 status = psa_hmac_setup_internal( &hmac,
3644 tls12_prf->key,
3645 tls12_prf->key_len,
3646 hash_alg );
3647 if( status != PSA_SUCCESS )
3648 goto cleanup;
3649
3650 status = psa_hash_update( &hmac.hash_ctx,
3651 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003652 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003653 if( status != PSA_SUCCESS )
3654 goto cleanup;
3655
3656 status = psa_hmac_finish_internal( &hmac,
3657 tls12_prf->output_block,
3658 hash_length );
3659 if( status != PSA_SUCCESS )
3660 goto cleanup;
3661
3662cleanup:
3663
3664 cleanup_status = psa_hmac_abort_internal( &hmac );
3665 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3666 status = cleanup_status;
3667
3668 return( status );
3669}
3670
3671/* Read some bytes from an TLS-1.2-PRF-based generator.
3672 * See Section 5 of RFC 5246. */
3673static psa_status_t psa_generator_tls12_prf_read(
3674 psa_tls12_prf_generator_t *tls12_prf,
3675 psa_algorithm_t alg,
3676 uint8_t *output,
3677 size_t output_length )
3678{
3679 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3680 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3681 psa_status_t status;
3682
3683 while( output_length != 0 )
3684 {
3685 /* Copy what remains of the current block */
3686 uint8_t n = hash_length - tls12_prf->offset_in_block;
3687
3688 /* Check if we have fully processed the current block. */
3689 if( n == 0 )
3690 {
3691 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3692 alg );
3693 if( status != PSA_SUCCESS )
3694 return( status );
3695
3696 continue;
3697 }
3698
3699 if( n > output_length )
3700 n = (uint8_t) output_length;
3701 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3702 n );
3703 output += n;
3704 output_length -= n;
3705 tls12_prf->offset_in_block += n;
3706 }
3707
3708 return( PSA_SUCCESS );
3709}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003710#endif /* MBEDTLS_MD_C */
3711
Gilles Peskineeab56e42018-07-12 17:12:33 +02003712psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3713 uint8_t *output,
3714 size_t output_length )
3715{
3716 psa_status_t status;
3717
3718 if( output_length > generator->capacity )
3719 {
3720 generator->capacity = 0;
3721 /* Go through the error path to wipe all confidential data now
3722 * that the generator object is useless. */
3723 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3724 goto exit;
3725 }
3726 if( output_length == 0 &&
3727 generator->capacity == 0 && generator->alg == 0 )
3728 {
3729 /* Edge case: this is a blank or finished generator, and 0
3730 * bytes were requested. The right error in this case could
3731 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3732 * INSUFFICIENT_CAPACITY, which is right for a finished
3733 * generator, for consistency with the case when
3734 * output_length > 0. */
3735 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3736 }
3737 generator->capacity -= output_length;
3738
Gilles Peskine751d9652018-09-18 12:05:44 +02003739 if( generator->alg == PSA_ALG_SELECT_RAW )
3740 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003741 /* Initially, the capacity of a selection generator is always
3742 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3743 * abbreviated in this comment as `size`. When the remaining
3744 * capacity is `c`, the next bytes to serve start `c` bytes
3745 * from the end of the buffer, i.e. `size - c` from the
3746 * beginning of the buffer. Since `generator->capacity` was just
3747 * decremented above, we need to serve the bytes from
3748 * `size - generator->capacity - output_length` to
3749 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003750 size_t offset =
3751 generator->ctx.buffer.size - generator->capacity - output_length;
3752 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3753 status = PSA_SUCCESS;
3754 }
3755 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003756#if defined(MBEDTLS_MD_C)
3757 if( PSA_ALG_IS_HKDF( generator->alg ) )
3758 {
3759 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3760 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3761 output, output_length );
3762 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003763 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3764 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003765 {
3766 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3767 generator->alg, output,
3768 output_length );
3769 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003770 else
3771#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003772 {
3773 return( PSA_ERROR_BAD_STATE );
3774 }
3775
3776exit:
3777 if( status != PSA_SUCCESS )
3778 {
3779 psa_generator_abort( generator );
3780 memset( output, '!', output_length );
3781 }
3782 return( status );
3783}
3784
Gilles Peskine08542d82018-07-19 17:05:42 +02003785#if defined(MBEDTLS_DES_C)
3786static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3787{
3788 if( data_size >= 8 )
3789 mbedtls_des_key_set_parity( data );
3790 if( data_size >= 16 )
3791 mbedtls_des_key_set_parity( data + 8 );
3792 if( data_size >= 24 )
3793 mbedtls_des_key_set_parity( data + 16 );
3794}
3795#endif /* MBEDTLS_DES_C */
3796
Gilles Peskinec5487a82018-12-03 18:08:14 +01003797psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003798 psa_key_type_t type,
3799 size_t bits,
3800 psa_crypto_generator_t *generator )
3801{
3802 uint8_t *data = NULL;
3803 size_t bytes = PSA_BITS_TO_BYTES( bits );
3804 psa_status_t status;
3805
3806 if( ! key_type_is_raw_bytes( type ) )
3807 return( PSA_ERROR_INVALID_ARGUMENT );
3808 if( bits % 8 != 0 )
3809 return( PSA_ERROR_INVALID_ARGUMENT );
3810 data = mbedtls_calloc( 1, bytes );
3811 if( data == NULL )
3812 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3813
3814 status = psa_generator_read( generator, data, bytes );
3815 if( status != PSA_SUCCESS )
3816 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003817#if defined(MBEDTLS_DES_C)
3818 if( type == PSA_KEY_TYPE_DES )
3819 psa_des_set_key_parity( data, bytes );
3820#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003821 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003822
3823exit:
3824 mbedtls_free( data );
3825 return( status );
3826}
3827
3828
3829
3830/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003831/* Key derivation */
3832/****************************************************************/
3833
Gilles Peskinea05219c2018-11-16 16:02:56 +01003834#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003835/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003836 * of the HKDF algorithm.
3837 *
3838 * Note that if this function fails, you must call psa_generator_abort()
3839 * to potentially free embedded data structures and wipe confidential data.
3840 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003841static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003842 const uint8_t *secret,
3843 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003844 psa_algorithm_t hash_alg,
3845 const uint8_t *salt,
3846 size_t salt_length,
3847 const uint8_t *label,
3848 size_t label_length )
3849{
3850 psa_status_t status;
3851 status = psa_hmac_setup_internal( &hkdf->hmac,
3852 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003853 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003854 if( status != PSA_SUCCESS )
3855 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003856 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003857 if( status != PSA_SUCCESS )
3858 return( status );
3859 status = psa_hmac_finish_internal( &hkdf->hmac,
3860 hkdf->prk,
3861 sizeof( hkdf->prk ) );
3862 if( status != PSA_SUCCESS )
3863 return( status );
3864 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3865 hkdf->block_number = 0;
3866 hkdf->info_length = label_length;
3867 if( label_length != 0 )
3868 {
3869 hkdf->info = mbedtls_calloc( 1, label_length );
3870 if( hkdf->info == NULL )
3871 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3872 memcpy( hkdf->info, label, label_length );
3873 }
3874 return( PSA_SUCCESS );
3875}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003876#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003877
Gilles Peskinea05219c2018-11-16 16:02:56 +01003878#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003879/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3880 *
3881 * Note that if this function fails, you must call psa_generator_abort()
3882 * to potentially free embedded data structures and wipe confidential data.
3883 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003884static psa_status_t psa_generator_tls12_prf_setup(
3885 psa_tls12_prf_generator_t *tls12_prf,
3886 const unsigned char *key,
3887 size_t key_len,
3888 psa_algorithm_t hash_alg,
3889 const uint8_t *salt,
3890 size_t salt_length,
3891 const uint8_t *label,
3892 size_t label_length )
3893{
3894 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003895 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3896 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003897
3898 tls12_prf->key = mbedtls_calloc( 1, key_len );
3899 if( tls12_prf->key == NULL )
3900 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3901 tls12_prf->key_len = key_len;
3902 memcpy( tls12_prf->key, key, key_len );
3903
Hanno Becker580fba12018-11-13 20:50:45 +00003904 overflow = ( salt_length + label_length < salt_length ) ||
3905 ( salt_length + label_length + hash_length < hash_length );
3906 if( overflow )
3907 return( PSA_ERROR_INVALID_ARGUMENT );
3908
3909 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3910 if( tls12_prf->Ai_with_seed == NULL )
3911 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3912 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3913
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003914 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3915 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003916 if( label_length != 0 )
3917 {
3918 memcpy( tls12_prf->Ai_with_seed + hash_length,
3919 label, label_length );
3920 }
3921
3922 if( salt_length != 0 )
3923 {
3924 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3925 salt, salt_length );
3926 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003927
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003928 /* The first block gets generated when
3929 * psa_generator_read() is called. */
3930 tls12_prf->block_number = 0;
3931 tls12_prf->offset_in_block = hash_length;
3932
3933 return( PSA_SUCCESS );
3934}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003935
3936/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3937static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3938 psa_tls12_prf_generator_t *tls12_prf,
3939 const unsigned char *psk,
3940 size_t psk_len,
3941 psa_algorithm_t hash_alg,
3942 const uint8_t *salt,
3943 size_t salt_length,
3944 const uint8_t *label,
3945 size_t label_length )
3946{
3947 psa_status_t status;
3948 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3949
3950 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3951 return( PSA_ERROR_INVALID_ARGUMENT );
3952
3953 /* Quoting RFC 4279, Section 2:
3954 *
3955 * The premaster secret is formed as follows: if the PSK is N octets
3956 * long, concatenate a uint16 with the value N, N zero octets, a second
3957 * uint16 with the value N, and the PSK itself.
3958 */
3959
3960 pms[0] = ( psk_len >> 8 ) & 0xff;
3961 pms[1] = ( psk_len >> 0 ) & 0xff;
3962 memset( pms + 2, 0, psk_len );
3963 pms[2 + psk_len + 0] = pms[0];
3964 pms[2 + psk_len + 1] = pms[1];
3965 memcpy( pms + 4 + psk_len, psk, psk_len );
3966
3967 status = psa_generator_tls12_prf_setup( tls12_prf,
3968 pms, 4 + 2 * psk_len,
3969 hash_alg,
3970 salt, salt_length,
3971 label, label_length );
3972
Gilles Peskine3f108122018-12-07 18:14:53 +01003973 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00003974 return( status );
3975}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003976#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003977
Gilles Peskine346797d2018-11-16 16:05:06 +01003978/* Note that if this function fails, you must call psa_generator_abort()
3979 * to potentially free embedded data structures and wipe confidential data.
3980 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003981static psa_status_t psa_key_derivation_internal(
3982 psa_crypto_generator_t *generator,
3983 const uint8_t *secret, size_t secret_length,
3984 psa_algorithm_t alg,
3985 const uint8_t *salt, size_t salt_length,
3986 const uint8_t *label, size_t label_length,
3987 size_t capacity )
3988{
3989 psa_status_t status;
3990 size_t max_capacity;
3991
3992 /* Set generator->alg even on failure so that abort knows what to do. */
3993 generator->alg = alg;
3994
Gilles Peskine751d9652018-09-18 12:05:44 +02003995 if( alg == PSA_ALG_SELECT_RAW )
3996 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003997 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003998 if( salt_length != 0 )
3999 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004000 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004001 if( label_length != 0 )
4002 return( PSA_ERROR_INVALID_ARGUMENT );
4003 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4004 if( generator->ctx.buffer.data == NULL )
4005 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4006 memcpy( generator->ctx.buffer.data, secret, secret_length );
4007 generator->ctx.buffer.size = secret_length;
4008 max_capacity = secret_length;
4009 status = PSA_SUCCESS;
4010 }
4011 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004012#if defined(MBEDTLS_MD_C)
4013 if( PSA_ALG_IS_HKDF( alg ) )
4014 {
4015 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4016 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4017 if( hash_size == 0 )
4018 return( PSA_ERROR_NOT_SUPPORTED );
4019 max_capacity = 255 * hash_size;
4020 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4021 secret, secret_length,
4022 hash_alg,
4023 salt, salt_length,
4024 label, label_length );
4025 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004026 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4027 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4028 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004029 {
4030 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4031 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4032
4033 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4034 if( hash_alg != PSA_ALG_SHA_256 &&
4035 hash_alg != PSA_ALG_SHA_384 )
4036 {
4037 return( PSA_ERROR_NOT_SUPPORTED );
4038 }
4039
4040 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004041
4042 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4043 {
4044 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4045 secret, secret_length,
4046 hash_alg, salt, salt_length,
4047 label, label_length );
4048 }
4049 else
4050 {
4051 status = psa_generator_tls12_psk_to_ms_setup(
4052 &generator->ctx.tls12_prf,
4053 secret, secret_length,
4054 hash_alg, salt, salt_length,
4055 label, label_length );
4056 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004057 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004058 else
4059#endif
4060 {
4061 return( PSA_ERROR_NOT_SUPPORTED );
4062 }
4063
4064 if( status != PSA_SUCCESS )
4065 return( status );
4066
4067 if( capacity <= max_capacity )
4068 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004069 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4070 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004071 else
4072 return( PSA_ERROR_INVALID_ARGUMENT );
4073
4074 return( PSA_SUCCESS );
4075}
4076
Gilles Peskineea0fb492018-07-12 17:17:20 +02004077psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004078 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004079 psa_algorithm_t alg,
4080 const uint8_t *salt,
4081 size_t salt_length,
4082 const uint8_t *label,
4083 size_t label_length,
4084 size_t capacity )
4085{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004086 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004087 psa_status_t status;
4088
4089 if( generator->alg != 0 )
4090 return( PSA_ERROR_BAD_STATE );
4091
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004092 /* Make sure that alg is a key derivation algorithm. This prevents
4093 * key selection algorithms, which psa_key_derivation_internal
4094 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004095 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4096 return( PSA_ERROR_INVALID_ARGUMENT );
4097
Gilles Peskinec5487a82018-12-03 18:08:14 +01004098 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004099 if( status != PSA_SUCCESS )
4100 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004101
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004102 if( slot->type != PSA_KEY_TYPE_DERIVE )
4103 return( PSA_ERROR_INVALID_ARGUMENT );
4104
4105 status = psa_key_derivation_internal( generator,
4106 slot->data.raw.data,
4107 slot->data.raw.bytes,
4108 alg,
4109 salt, salt_length,
4110 label, label_length,
4111 capacity );
4112 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004113 psa_generator_abort( generator );
4114 return( status );
4115}
4116
4117
4118
4119/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004120/* Key agreement */
4121/****************************************************************/
4122
Gilles Peskinea05219c2018-11-16 16:02:56 +01004123#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004124static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4125 size_t peer_key_length,
4126 const mbedtls_ecp_keypair *our_key,
4127 uint8_t *shared_secret,
4128 size_t shared_secret_size,
4129 size_t *shared_secret_length )
4130{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004131 mbedtls_ecp_keypair *their_key = NULL;
4132 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004133 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004134 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004135
Jaeden Ameroccdce902019-01-10 11:42:27 +00004136 status = psa_import_ec_public_key(
4137 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4138 peer_key, peer_key_length,
4139 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004140 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004141 goto exit;
4142
Jaeden Amero97271b32019-01-10 19:38:51 +00004143 status = mbedtls_to_psa_error(
4144 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4145 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004146 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004147 status = mbedtls_to_psa_error(
4148 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4149 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004150 goto exit;
4151
Jaeden Amero97271b32019-01-10 19:38:51 +00004152 status = mbedtls_to_psa_error(
4153 mbedtls_ecdh_calc_secret( &ecdh,
4154 shared_secret_length,
4155 shared_secret, shared_secret_size,
4156 mbedtls_ctr_drbg_random,
4157 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004158
4159exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004160 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004161 mbedtls_ecp_keypair_free( their_key );
4162 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004163 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004164}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004165#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004166
Gilles Peskine01d718c2018-09-18 12:01:02 +02004167#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4168
Gilles Peskine346797d2018-11-16 16:05:06 +01004169/* Note that if this function fails, you must call psa_generator_abort()
4170 * to potentially free embedded data structures and wipe confidential data.
4171 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004172static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004173 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004174 const uint8_t *peer_key,
4175 size_t peer_key_length,
4176 psa_algorithm_t alg )
4177{
4178 psa_status_t status;
4179 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4180 size_t shared_secret_length = 0;
4181
4182 /* Step 1: run the secret agreement algorithm to generate the shared
4183 * secret. */
4184 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4185 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004186#if defined(MBEDTLS_ECDH_C)
4187 case PSA_ALG_ECDH_BASE:
4188 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4189 return( PSA_ERROR_INVALID_ARGUMENT );
4190 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4191 private_key->data.ecp,
4192 shared_secret,
4193 sizeof( shared_secret ),
4194 &shared_secret_length );
4195 break;
4196#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004197 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004198 (void) private_key;
4199 (void) peer_key;
4200 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004201 return( PSA_ERROR_NOT_SUPPORTED );
4202 }
4203 if( status != PSA_SUCCESS )
4204 goto exit;
4205
4206 /* Step 2: set up the key derivation to generate key material from
4207 * the shared secret. */
4208 status = psa_key_derivation_internal( generator,
4209 shared_secret, shared_secret_length,
4210 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4211 NULL, 0, NULL, 0,
4212 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4213exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004214 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004215 return( status );
4216}
4217
4218psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004219 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004220 const uint8_t *peer_key,
4221 size_t peer_key_length,
4222 psa_algorithm_t alg )
4223{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004224 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004225 psa_status_t status;
4226 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4227 return( PSA_ERROR_INVALID_ARGUMENT );
4228 status = psa_get_key_from_slot( private_key, &slot,
4229 PSA_KEY_USAGE_DERIVE, alg );
4230 if( status != PSA_SUCCESS )
4231 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004232 status = psa_key_agreement_internal( generator,
4233 slot,
4234 peer_key, peer_key_length,
4235 alg );
4236 if( status != PSA_SUCCESS )
4237 psa_generator_abort( generator );
4238 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004239}
4240
4241
4242
4243/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004244/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004245/****************************************************************/
4246
4247psa_status_t psa_generate_random( uint8_t *output,
4248 size_t output_size )
4249{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004250 int ret;
4251 GUARD_MODULE_INITIALIZED;
4252
4253 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004254 return( mbedtls_to_psa_error( ret ) );
4255}
4256
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004257#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004258
4259/* Support function for error conversion between psa_its error codes to psa crypto */
4260static psa_status_t its_to_psa_error( psa_its_status_t ret )
4261{
4262 switch( ret )
4263 {
4264 case PSA_ITS_SUCCESS:
4265 return( PSA_SUCCESS );
4266
Oren Cohen23a67842019-01-24 14:32:11 +02004267 case PSA_ITS_ERROR_UID_NOT_FOUND:
avolinski13beb102018-11-20 16:51:49 +02004268 return( PSA_ERROR_EMPTY_SLOT );
4269
4270 case PSA_ITS_ERROR_STORAGE_FAILURE:
4271 return( PSA_ERROR_STORAGE_FAILURE );
4272
4273 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4274 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4275
Jaeden Amero58600552018-11-30 12:04:38 +00004276 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004277 case PSA_ITS_ERROR_INCORRECT_SIZE:
Oren Cohen23a67842019-01-24 14:32:11 +02004278 case PSA_ITS_ERROR_INVALID_ARGUMENTS:
avolinski13beb102018-11-20 16:51:49 +02004279 return( PSA_ERROR_INVALID_ARGUMENT );
4280
4281 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4282 return( PSA_ERROR_NOT_SUPPORTED );
4283
4284 case PSA_ITS_ERROR_WRITE_ONCE:
4285 return( PSA_ERROR_OCCUPIED_SLOT );
4286
4287 default:
4288 return( PSA_ERROR_UNKNOWN_ERROR );
4289 }
4290}
4291
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004292psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4293 size_t seed_size )
4294{
4295 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004296 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004297 struct psa_its_info_t p_info;
4298 if( global_data.initialized )
4299 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004300
4301 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4302 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4303 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4304 return( PSA_ERROR_INVALID_ARGUMENT );
4305
avolinski0d2c2662018-11-21 17:31:07 +02004306 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004307 status = its_to_psa_error( its_status );
4308
Oren Cohen23a67842019-01-24 14:32:11 +02004309 if( PSA_ITS_ERROR_UID_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004310 {
avolinski0d2c2662018-11-21 17:31:07 +02004311 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004312 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004313 }
avolinski13beb102018-11-20 16:51:49 +02004314 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004315 {
4316 /* You should not be here. Seed needs to be injected only once */
4317 status = PSA_ERROR_NOT_PERMITTED;
4318 }
4319 return( status );
4320}
4321#endif
4322
Gilles Peskinec5487a82018-12-03 18:08:14 +01004323psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004324 psa_key_type_t type,
4325 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004326 const void *extra,
4327 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004328{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004329 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004330 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004331
Gilles Peskine53d991e2018-07-12 01:14:59 +02004332 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004333 return( PSA_ERROR_INVALID_ARGUMENT );
4334
Gilles Peskinec5487a82018-12-03 18:08:14 +01004335 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004336 if( status != PSA_SUCCESS )
4337 return( status );
4338
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004339 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004340 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004341 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004342 if( status != PSA_SUCCESS )
4343 return( status );
4344 status = psa_generate_random( slot->data.raw.data,
4345 slot->data.raw.bytes );
4346 if( status != PSA_SUCCESS )
4347 {
4348 mbedtls_free( slot->data.raw.data );
4349 return( status );
4350 }
4351#if defined(MBEDTLS_DES_C)
4352 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004353 psa_des_set_key_parity( slot->data.raw.data,
4354 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004355#endif /* MBEDTLS_DES_C */
4356 }
4357 else
4358
4359#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4360 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4361 {
4362 mbedtls_rsa_context *rsa;
4363 int ret;
4364 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004365 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4366 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004367 /* Accept only byte-aligned keys, for the same reasons as
4368 * in psa_import_rsa_key(). */
4369 if( bits % 8 != 0 )
4370 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004371 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004372 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004373 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004374 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004375 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004376#if INT_MAX < 0xffffffff
4377 /* Check that the uint32_t value passed by the caller fits
4378 * in the range supported by this implementation. */
4379 if( p->e > INT_MAX )
4380 return( PSA_ERROR_NOT_SUPPORTED );
4381#endif
4382 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004383 }
4384 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4385 if( rsa == NULL )
4386 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4387 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4388 ret = mbedtls_rsa_gen_key( rsa,
4389 mbedtls_ctr_drbg_random,
4390 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004391 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004392 exponent );
4393 if( ret != 0 )
4394 {
4395 mbedtls_rsa_free( rsa );
4396 mbedtls_free( rsa );
4397 return( mbedtls_to_psa_error( ret ) );
4398 }
4399 slot->data.rsa = rsa;
4400 }
4401 else
4402#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4403
4404#if defined(MBEDTLS_ECP_C)
4405 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4406 {
4407 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4408 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4409 const mbedtls_ecp_curve_info *curve_info =
4410 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4411 mbedtls_ecp_keypair *ecp;
4412 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004413 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004414 return( PSA_ERROR_NOT_SUPPORTED );
4415 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4416 return( PSA_ERROR_NOT_SUPPORTED );
4417 if( curve_info->bit_size != bits )
4418 return( PSA_ERROR_INVALID_ARGUMENT );
4419 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4420 if( ecp == NULL )
4421 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4422 mbedtls_ecp_keypair_init( ecp );
4423 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4424 mbedtls_ctr_drbg_random,
4425 &global_data.ctr_drbg );
4426 if( ret != 0 )
4427 {
4428 mbedtls_ecp_keypair_free( ecp );
4429 mbedtls_free( ecp );
4430 return( mbedtls_to_psa_error( ret ) );
4431 }
4432 slot->data.ecp = ecp;
4433 }
4434 else
4435#endif /* MBEDTLS_ECP_C */
4436
4437 return( PSA_ERROR_NOT_SUPPORTED );
4438
4439 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004440
4441#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4442 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4443 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004444 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004445 }
4446#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4447
4448 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004449}
4450
4451
4452/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004453/* Module setup */
4454/****************************************************************/
4455
Gilles Peskine5e769522018-11-20 21:59:56 +01004456psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4457 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4458 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4459{
4460 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4461 return( PSA_ERROR_BAD_STATE );
4462 global_data.entropy_init = entropy_init;
4463 global_data.entropy_free = entropy_free;
4464 return( PSA_SUCCESS );
4465}
4466
Gilles Peskinee59236f2018-01-27 23:32:46 +01004467void mbedtls_psa_crypto_free( void )
4468{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004469 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004470 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4471 {
4472 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004473 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004474 }
4475 /* Wipe all remaining data, including configuration.
4476 * In particular, this sets all state indicator to the value
4477 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004478 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004479}
4480
4481psa_status_t psa_crypto_init( void )
4482{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004483 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004484 const unsigned char drbg_seed[] = "PSA";
4485
Gilles Peskinec6b69072018-11-20 21:42:52 +01004486 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004487 if( global_data.initialized != 0 )
4488 return( PSA_SUCCESS );
4489
Gilles Peskine5e769522018-11-20 21:59:56 +01004490 /* Set default configuration if
4491 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4492 if( global_data.entropy_init == NULL )
4493 global_data.entropy_init = mbedtls_entropy_init;
4494 if( global_data.entropy_free == NULL )
4495 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004496
Gilles Peskinec6b69072018-11-20 21:42:52 +01004497 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004498 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004499 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004500 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004501 status = mbedtls_to_psa_error(
4502 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4503 mbedtls_entropy_func,
4504 &global_data.entropy,
4505 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4506 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004507 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004508 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004509
Gilles Peskine66fb1262018-12-10 16:29:04 +01004510 status = psa_initialize_key_slots( );
4511 if( status != PSA_SUCCESS )
4512 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004513
4514 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004515 global_data.initialized = 1;
4516
Gilles Peskinee59236f2018-01-27 23:32:46 +01004517exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004518 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004519 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004520 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004521}
4522
4523#endif /* MBEDTLS_PSA_CRYPTO_C */