blob: 3a97f44b9d33ad82ecf54552203d87abcae6c7ab [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030029/*
30 * In case MBEDTLS_PSA_CRYPTO_SPM is defined the code is built for SPM (Secure
31 * Partition Manager) integration which separate the code into two parts
32 * NSPE (Non-Secure Process Environment) and SPE (Secure Process Environment).
33 * In this mode an additional header file should be included.
34 */
mohammad160327010052018-07-03 13:16:15 +030035#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030036/*
37 * PSA_CRYPTO_SECURE means that this file is compiled to the SPE side.
38 * some headers will be affected by this flag.
39 */
mohammad160327010052018-07-03 13:16:15 +030040#define PSA_CRYPTO_SECURE 1
41#include "crypto_spe.h"
42#endif
43
Gilles Peskinee59236f2018-01-27 23:32:46 +010044#include "psa/crypto.h"
45
Gilles Peskine039b90c2018-12-07 18:24:41 +010046#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010047#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010048#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010049/* Include internal declarations that are useful for implementing persistently
50 * stored keys. */
51#include "psa_crypto_storage.h"
52
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
55#if defined(MBEDTLS_PLATFORM_C)
56#include "mbedtls/platform.h"
57#else
58#define mbedtls_calloc calloc
59#define mbedtls_free free
60#endif
61
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020063#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020064#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/blowfish.h"
66#include "mbedtls/camellia.h"
67#include "mbedtls/cipher.h"
68#include "mbedtls/ccm.h"
69#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020072#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010073#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010074#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020075#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/error.h"
77#include "mbedtls/gcm.h"
78#include "mbedtls/md2.h"
79#include "mbedtls/md4.h"
80#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010081#include "mbedtls/md.h"
82#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010083#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010084#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010085#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010087#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088#include "mbedtls/sha1.h"
89#include "mbedtls/sha256.h"
90#include "mbedtls/sha512.h"
91#include "mbedtls/xtea.h"
92
Netanel Gonen2bcd3122018-11-19 11:53:02 +020093#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
94#include "psa_prot_internal_storage.h"
95#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010096
Gilles Peskine996deb12018-08-01 15:45:45 +020097#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
98
Gilles Peskine9ef733f2018-02-07 21:05:37 +010099/* constant-time buffer comparison */
100static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
101{
102 size_t i;
103 unsigned char diff = 0;
104
105 for( i = 0; i < n; i++ )
106 diff |= a[i] ^ b[i];
107
108 return( diff );
109}
110
111
112
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100113/****************************************************************/
114/* Global data, support functions and library management */
115/****************************************************************/
116
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200117static int key_type_is_raw_bytes( psa_key_type_t type )
118{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200119 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200120}
121
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122/* Values for psa_global_data_t::rng_state */
123#define RNG_NOT_INITIALIZED 0
124#define RNG_INITIALIZED 1
125#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100126
Gilles Peskine2d277862018-06-18 15:41:12 +0200127typedef struct
128{
Gilles Peskine5e769522018-11-20 21:59:56 +0100129 void (* entropy_init )( mbedtls_entropy_context *ctx );
130 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131 mbedtls_entropy_context entropy;
132 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100133 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100134 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135} psa_global_data_t;
136
137static psa_global_data_t global_data;
138
itayzafrir0adf0fc2018-09-06 16:24:41 +0300139#define GUARD_MODULE_INITIALIZED \
140 if( global_data.initialized == 0 ) \
141 return( PSA_ERROR_BAD_STATE );
142
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143static psa_status_t mbedtls_to_psa_error( int ret )
144{
Gilles Peskinea5905292018-02-07 20:59:33 +0100145 /* If there's both a high-level code and low-level code, dispatch on
146 * the high-level code. */
147 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100148 {
149 case 0:
150 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100151
152 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
153 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
154 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
155 return( PSA_ERROR_NOT_SUPPORTED );
156 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
157 return( PSA_ERROR_HARDWARE_FAILURE );
158
159 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
160 return( PSA_ERROR_HARDWARE_FAILURE );
161
Gilles Peskine9a944802018-06-21 09:35:35 +0200162 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
163 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
164 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
165 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
166 case MBEDTLS_ERR_ASN1_INVALID_DATA:
167 return( PSA_ERROR_INVALID_ARGUMENT );
168 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
169 return( PSA_ERROR_INSUFFICIENT_MEMORY );
170 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
171 return( PSA_ERROR_BUFFER_TOO_SMALL );
172
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
174 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
175 return( PSA_ERROR_NOT_SUPPORTED );
176 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
179 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
180 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
181 return( PSA_ERROR_NOT_SUPPORTED );
182 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
183 return( PSA_ERROR_HARDWARE_FAILURE );
184
185 case MBEDTLS_ERR_CCM_BAD_INPUT:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187 case MBEDTLS_ERR_CCM_AUTH_FAILED:
188 return( PSA_ERROR_INVALID_SIGNATURE );
189 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
190 return( PSA_ERROR_HARDWARE_FAILURE );
191
192 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
193 return( PSA_ERROR_NOT_SUPPORTED );
194 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
195 return( PSA_ERROR_INVALID_ARGUMENT );
196 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
197 return( PSA_ERROR_INSUFFICIENT_MEMORY );
198 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
199 return( PSA_ERROR_INVALID_PADDING );
200 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
201 return( PSA_ERROR_BAD_STATE );
202 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
203 return( PSA_ERROR_INVALID_SIGNATURE );
204 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
205 return( PSA_ERROR_TAMPERING_DETECTED );
206 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
207 return( PSA_ERROR_HARDWARE_FAILURE );
208
209 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
213 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
214 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
215 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
216 return( PSA_ERROR_NOT_SUPPORTED );
217 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219
220 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
223 return( PSA_ERROR_HARDWARE_FAILURE );
224
Gilles Peskinee59236f2018-01-27 23:32:46 +0100225 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
226 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
227 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
228 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
230 case MBEDTLS_ERR_GCM_AUTH_FAILED:
231 return( PSA_ERROR_INVALID_SIGNATURE );
232 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200233 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
235 return( PSA_ERROR_HARDWARE_FAILURE );
236
237 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
238 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
243 return( PSA_ERROR_NOT_SUPPORTED );
244 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
245 return( PSA_ERROR_INVALID_ARGUMENT );
246 case MBEDTLS_ERR_MD_ALLOC_FAILED:
247 return( PSA_ERROR_INSUFFICIENT_MEMORY );
248 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
249 return( PSA_ERROR_STORAGE_FAILURE );
250 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
251 return( PSA_ERROR_HARDWARE_FAILURE );
252
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
256 return( PSA_ERROR_INVALID_ARGUMENT );
257 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
260 return( PSA_ERROR_BUFFER_TOO_SMALL );
261 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
268 return( PSA_ERROR_INSUFFICIENT_MEMORY );
269
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
273 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100276 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
278 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
283 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
284 return( PSA_ERROR_NOT_PERMITTED );
285 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_PK_INVALID_ALG:
288 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
289 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
290 return( PSA_ERROR_NOT_SUPPORTED );
291 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
292 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
294 return( PSA_ERROR_HARDWARE_FAILURE );
295
296 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298
299 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
300 return( PSA_ERROR_INVALID_ARGUMENT );
301 case MBEDTLS_ERR_RSA_INVALID_PADDING:
302 return( PSA_ERROR_INVALID_PADDING );
303 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
304 return( PSA_ERROR_HARDWARE_FAILURE );
305 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
308 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
309 return( PSA_ERROR_TAMPERING_DETECTED );
310 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
311 return( PSA_ERROR_INVALID_SIGNATURE );
312 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
313 return( PSA_ERROR_BUFFER_TOO_SMALL );
314 case MBEDTLS_ERR_RSA_RNG_FAILED:
315 return( PSA_ERROR_INSUFFICIENT_MEMORY );
316 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
317 return( PSA_ERROR_NOT_SUPPORTED );
318 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
319 return( PSA_ERROR_HARDWARE_FAILURE );
320
321 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
322 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
323 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325
326 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
327 return( PSA_ERROR_INVALID_ARGUMENT );
328 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
itayzafrir5c753392018-05-08 11:18:38 +0300331 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300332 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300333 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300334 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
335 return( PSA_ERROR_BUFFER_TOO_SMALL );
336 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
337 return( PSA_ERROR_NOT_SUPPORTED );
338 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
339 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
340 return( PSA_ERROR_INVALID_SIGNATURE );
341 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
342 return( PSA_ERROR_INSUFFICIENT_MEMORY );
343 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
344 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300345
Gilles Peskinee59236f2018-01-27 23:32:46 +0100346 default:
347 return( PSA_ERROR_UNKNOWN_ERROR );
348 }
349}
350
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200351
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200352
353
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100354/****************************************************************/
355/* Key management */
356/****************************************************************/
357
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100358#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200359static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
360{
361 switch( grpid )
362 {
363 case MBEDTLS_ECP_DP_SECP192R1:
364 return( PSA_ECC_CURVE_SECP192R1 );
365 case MBEDTLS_ECP_DP_SECP224R1:
366 return( PSA_ECC_CURVE_SECP224R1 );
367 case MBEDTLS_ECP_DP_SECP256R1:
368 return( PSA_ECC_CURVE_SECP256R1 );
369 case MBEDTLS_ECP_DP_SECP384R1:
370 return( PSA_ECC_CURVE_SECP384R1 );
371 case MBEDTLS_ECP_DP_SECP521R1:
372 return( PSA_ECC_CURVE_SECP521R1 );
373 case MBEDTLS_ECP_DP_BP256R1:
374 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
375 case MBEDTLS_ECP_DP_BP384R1:
376 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
377 case MBEDTLS_ECP_DP_BP512R1:
378 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
379 case MBEDTLS_ECP_DP_CURVE25519:
380 return( PSA_ECC_CURVE_CURVE25519 );
381 case MBEDTLS_ECP_DP_SECP192K1:
382 return( PSA_ECC_CURVE_SECP192K1 );
383 case MBEDTLS_ECP_DP_SECP224K1:
384 return( PSA_ECC_CURVE_SECP224K1 );
385 case MBEDTLS_ECP_DP_SECP256K1:
386 return( PSA_ECC_CURVE_SECP256K1 );
387 case MBEDTLS_ECP_DP_CURVE448:
388 return( PSA_ECC_CURVE_CURVE448 );
389 default:
390 return( 0 );
391 }
392}
393
Gilles Peskine12313cd2018-06-20 00:20:32 +0200394static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
395{
396 switch( curve )
397 {
398 case PSA_ECC_CURVE_SECP192R1:
399 return( MBEDTLS_ECP_DP_SECP192R1 );
400 case PSA_ECC_CURVE_SECP224R1:
401 return( MBEDTLS_ECP_DP_SECP224R1 );
402 case PSA_ECC_CURVE_SECP256R1:
403 return( MBEDTLS_ECP_DP_SECP256R1 );
404 case PSA_ECC_CURVE_SECP384R1:
405 return( MBEDTLS_ECP_DP_SECP384R1 );
406 case PSA_ECC_CURVE_SECP521R1:
407 return( MBEDTLS_ECP_DP_SECP521R1 );
408 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
409 return( MBEDTLS_ECP_DP_BP256R1 );
410 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
411 return( MBEDTLS_ECP_DP_BP384R1 );
412 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
413 return( MBEDTLS_ECP_DP_BP512R1 );
414 case PSA_ECC_CURVE_CURVE25519:
415 return( MBEDTLS_ECP_DP_CURVE25519 );
416 case PSA_ECC_CURVE_SECP192K1:
417 return( MBEDTLS_ECP_DP_SECP192K1 );
418 case PSA_ECC_CURVE_SECP224K1:
419 return( MBEDTLS_ECP_DP_SECP224K1 );
420 case PSA_ECC_CURVE_SECP256K1:
421 return( MBEDTLS_ECP_DP_SECP256K1 );
422 case PSA_ECC_CURVE_CURVE448:
423 return( MBEDTLS_ECP_DP_CURVE448 );
424 default:
425 return( MBEDTLS_ECP_DP_NONE );
426 }
427}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100428#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200429
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200430static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
431 size_t bits,
432 struct raw_data *raw )
433{
434 /* Check that the bit size is acceptable for the key type */
435 switch( type )
436 {
437 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200438 if( bits == 0 )
439 {
440 raw->bytes = 0;
441 raw->data = NULL;
442 return( PSA_SUCCESS );
443 }
444 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445#if defined(MBEDTLS_MD_C)
446 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200447#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200448 case PSA_KEY_TYPE_DERIVE:
449 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450#if defined(MBEDTLS_AES_C)
451 case PSA_KEY_TYPE_AES:
452 if( bits != 128 && bits != 192 && bits != 256 )
453 return( PSA_ERROR_INVALID_ARGUMENT );
454 break;
455#endif
456#if defined(MBEDTLS_CAMELLIA_C)
457 case PSA_KEY_TYPE_CAMELLIA:
458 if( bits != 128 && bits != 192 && bits != 256 )
459 return( PSA_ERROR_INVALID_ARGUMENT );
460 break;
461#endif
462#if defined(MBEDTLS_DES_C)
463 case PSA_KEY_TYPE_DES:
464 if( bits != 64 && bits != 128 && bits != 192 )
465 return( PSA_ERROR_INVALID_ARGUMENT );
466 break;
467#endif
468#if defined(MBEDTLS_ARC4_C)
469 case PSA_KEY_TYPE_ARC4:
470 if( bits < 8 || bits > 2048 )
471 return( PSA_ERROR_INVALID_ARGUMENT );
472 break;
473#endif
474 default:
475 return( PSA_ERROR_NOT_SUPPORTED );
476 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200477 if( bits % 8 != 0 )
478 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200479
480 /* Allocate memory for the key */
481 raw->bytes = PSA_BITS_TO_BYTES( bits );
482 raw->data = mbedtls_calloc( 1, raw->bytes );
483 if( raw->data == NULL )
484 {
485 raw->bytes = 0;
486 return( PSA_ERROR_INSUFFICIENT_MEMORY );
487 }
488 return( PSA_SUCCESS );
489}
490
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200491#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100492/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
493 * that are not a multiple of 8) well. For example, there is only
494 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
495 * way to return the exact bit size of a key.
496 * To keep things simple, reject non-byte-aligned key sizes. */
497static psa_status_t psa_check_rsa_key_byte_aligned(
498 const mbedtls_rsa_context *rsa )
499{
500 mbedtls_mpi n;
501 psa_status_t status;
502 mbedtls_mpi_init( &n );
503 status = mbedtls_to_psa_error(
504 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
505 if( status == PSA_SUCCESS )
506 {
507 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
508 status = PSA_ERROR_NOT_SUPPORTED;
509 }
510 mbedtls_mpi_free( &n );
511 return( status );
512}
513
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200514static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
515 mbedtls_rsa_context **p_rsa )
516{
517 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 else
520 {
521 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100522 /* The size of an RSA key doesn't have to be a multiple of 8.
523 * Mbed TLS supports non-byte-aligned key sizes, but not well.
524 * For example, mbedtls_rsa_get_len() returns the key size in
525 * bytes, not in bits. */
526 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100527 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200528 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
529 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100530 status = psa_check_rsa_key_byte_aligned( rsa );
531 if( status != PSA_SUCCESS )
532 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 *p_rsa = rsa;
534 return( PSA_SUCCESS );
535 }
536}
537#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
538
539#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100540/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200541static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
542 mbedtls_pk_context *pk,
543 mbedtls_ecp_keypair **p_ecp )
544{
545 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
546 return( PSA_ERROR_INVALID_ARGUMENT );
547 else
548 {
549 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
550 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
551 if( actual_curve != expected_curve )
552 return( PSA_ERROR_INVALID_ARGUMENT );
553 *p_ecp = ecp;
554 return( PSA_SUCCESS );
555 }
556}
557#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
558
Gilles Peskinef76aa772018-10-29 19:24:33 +0100559#if defined(MBEDTLS_ECP_C)
560/* Import a private key given as a byte string which is the private value
561 * in big-endian order. */
562static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
563 const uint8_t *data,
564 size_t data_length,
565 mbedtls_ecp_keypair **p_ecp )
566{
567 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
568 mbedtls_ecp_keypair *ecp = NULL;
569 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
570
571 *p_ecp = NULL;
572 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
573 if( ecp == NULL )
574 return( PSA_ERROR_INSUFFICIENT_MEMORY );
575
576 /* Load the group. */
577 status = mbedtls_to_psa_error(
578 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
579 if( status != PSA_SUCCESS )
580 goto exit;
581 /* Load the secret value. */
582 status = mbedtls_to_psa_error(
583 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
584 if( status != PSA_SUCCESS )
585 goto exit;
586 /* Validate the private key. */
587 status = mbedtls_to_psa_error(
588 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
589 if( status != PSA_SUCCESS )
590 goto exit;
591 /* Calculate the public key from the private key. */
592 status = mbedtls_to_psa_error(
593 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
594 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
595 if( status != PSA_SUCCESS )
596 goto exit;
597
598 *p_ecp = ecp;
599 return( PSA_SUCCESS );
600
601exit:
602 if( ecp != NULL )
603 {
604 mbedtls_ecp_keypair_free( ecp );
605 mbedtls_free( ecp );
606 }
607 return( status );
608}
609#endif /* defined(MBEDTLS_ECP_C) */
610
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100611/** Import key data into a slot. `slot->type` must have been set
612 * previously. This function assumes that the slot does not contain
613 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100614psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
615 const uint8_t *data,
616 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100617{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200618 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619
Darryl Green940d72c2018-07-13 13:18:51 +0100620 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100621 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100622 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623 if( data_length > SIZE_MAX / 8 )
624 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100625 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200626 PSA_BYTES_TO_BITS( data_length ),
627 &slot->data.raw );
628 if( status != PSA_SUCCESS )
629 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200630 if( data_length != 0 )
631 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100632 }
633 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100634#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100635 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100636 {
Darryl Green940d72c2018-07-13 13:18:51 +0100637 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100638 data, data_length,
639 &slot->data.ecp );
640 if( status != PSA_SUCCESS )
641 return( status );
642 }
643 else
644#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100645#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100646 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
647 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648 {
649 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100650 mbedtls_pk_context pk;
651 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200652
653 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100654 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100655 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
656 else
657 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658 if( ret != 0 )
659 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200660
661 /* We have something that the pkparse module recognizes.
662 * If it has the expected type and passes any type-specific
663 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100664#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100665 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200666 status = psa_import_rsa_key( &pk, &slot->data.rsa );
667 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100668#endif /* MBEDTLS_RSA_C */
669#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100670 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
671 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200672 &pk, &slot->data.ecp );
673 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100674#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200675 {
676 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200677 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200678
Gilles Peskinec648d692018-06-28 08:46:13 +0200679 /* Free the content of the pk object only on error. On success,
680 * the content of the object has been stored in the slot. */
681 if( status != PSA_SUCCESS )
682 {
683 mbedtls_pk_free( &pk );
684 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100685 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 }
687 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100688#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100689 {
690 return( PSA_ERROR_NOT_SUPPORTED );
691 }
Darryl Green940d72c2018-07-13 13:18:51 +0100692 return( PSA_SUCCESS );
693}
694
Darryl Green06fd18d2018-07-16 11:21:11 +0100695/* Retrieve an empty key slot (slot with no key data, but possibly
696 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100697static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100698 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100699{
700 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100701 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100702
703 *p_slot = NULL;
704
Gilles Peskinec5487a82018-12-03 18:08:14 +0100705 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100706 if( status != PSA_SUCCESS )
707 return( status );
708
709 if( slot->type != PSA_KEY_TYPE_NONE )
710 return( PSA_ERROR_OCCUPIED_SLOT );
711
712 *p_slot = slot;
713 return( status );
714}
715
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100716/** Test whether a policy permits an algorithm.
717 *
718 * The caller must test usage flags separately.
719 */
720static int psa_key_policy_permits( const psa_key_policy_t *policy,
721 psa_algorithm_t alg )
722{
723 /* Common case: the policy only allows alg. */
724 if( alg == policy->alg )
725 return( 1 );
726 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
727 * and alg is the same hash-and-sign family with any hash,
728 * then alg is compliant with policy->alg. */
729 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
730 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
731 {
732 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
733 ( alg & ~PSA_ALG_HASH_MASK ) );
734 }
735 /* If it isn't permitted, it's forbidden. */
736 return( 0 );
737}
738
Darryl Green06fd18d2018-07-16 11:21:11 +0100739/** Retrieve a slot which must contain a key. The key must have allow all the
740 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
741 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100742static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100743 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100744 psa_key_usage_t usage,
745 psa_algorithm_t alg )
746{
747 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100748 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100749
750 *p_slot = NULL;
751
Gilles Peskinec5487a82018-12-03 18:08:14 +0100752 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100753 if( status != PSA_SUCCESS )
754 return( status );
755 if( slot->type == PSA_KEY_TYPE_NONE )
756 return( PSA_ERROR_EMPTY_SLOT );
757
758 /* Enforce that usage policy for the key slot contains all the flags
759 * required by the usage parameter. There is one exception: public
760 * keys can always be exported, so we treat public key objects as
761 * if they had the export flag. */
762 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
763 usage &= ~PSA_KEY_USAGE_EXPORT;
764 if( ( slot->policy.usage & usage ) != usage )
765 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100766
767 /* Enforce that the usage policy permits the requested algortihm. */
768 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100769 return( PSA_ERROR_NOT_PERMITTED );
770
771 *p_slot = slot;
772 return( PSA_SUCCESS );
773}
Darryl Green940d72c2018-07-13 13:18:51 +0100774
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100775/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100776static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000777{
778 if( slot->type == PSA_KEY_TYPE_NONE )
779 {
780 /* No key material to clean. */
781 }
782 else if( key_type_is_raw_bytes( slot->type ) )
783 {
784 mbedtls_free( slot->data.raw.data );
785 }
786 else
787#if defined(MBEDTLS_RSA_C)
788 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
789 {
790 mbedtls_rsa_free( slot->data.rsa );
791 mbedtls_free( slot->data.rsa );
792 }
793 else
794#endif /* defined(MBEDTLS_RSA_C) */
795#if defined(MBEDTLS_ECP_C)
796 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
797 {
798 mbedtls_ecp_keypair_free( slot->data.ecp );
799 mbedtls_free( slot->data.ecp );
800 }
801 else
802#endif /* defined(MBEDTLS_ECP_C) */
803 {
804 /* Shouldn't happen: the key type is not any type that we
805 * put in. */
806 return( PSA_ERROR_TAMPERING_DETECTED );
807 }
808
809 return( PSA_SUCCESS );
810}
811
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100812/** Completely wipe a slot in memory, including its policy.
813 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100814psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100815{
816 psa_status_t status = psa_remove_key_data_from_memory( slot );
817 /* At this point, key material and other type-specific content has
818 * been wiped. Clear remaining metadata. We can call memset and not
819 * zeroize because the metadata is not particularly sensitive. */
820 memset( slot, 0, sizeof( *slot ) );
821 return( status );
822}
823
Gilles Peskinec5487a82018-12-03 18:08:14 +0100824psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100825 psa_key_type_t type,
826 const uint8_t *data,
827 size_t data_length )
828{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100829 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100830 psa_status_t status;
831
Gilles Peskinec5487a82018-12-03 18:08:14 +0100832 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100833 if( status != PSA_SUCCESS )
834 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835
836 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100837
838 status = psa_import_key_into_slot( slot, data, data_length );
839 if( status != PSA_SUCCESS )
840 {
841 slot->type = PSA_KEY_TYPE_NONE;
842 return( status );
843 }
844
Darryl Greend49a4992018-06-18 17:27:26 +0100845#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
846 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
847 {
848 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100849 status = psa_save_persistent_key( slot->persistent_storage_id,
850 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100851 data_length );
852 if( status != PSA_SUCCESS )
853 {
854 (void) psa_remove_key_data_from_memory( slot );
855 slot->type = PSA_KEY_TYPE_NONE;
856 }
857 }
858#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
859
860 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861}
862
Gilles Peskinec5487a82018-12-03 18:08:14 +0100863psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100864{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100865 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100866 psa_status_t status = PSA_SUCCESS;
867 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100868
Gilles Peskinec5487a82018-12-03 18:08:14 +0100869 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200870 if( status != PSA_SUCCESS )
871 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100872#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
873 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
874 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100875 storage_status =
876 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100877 }
878#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100879 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100880 if( status != PSA_SUCCESS )
881 return( status );
882 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883}
884
Gilles Peskineb870b182018-07-06 16:02:09 +0200885/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100886static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200887{
888 if( key_type_is_raw_bytes( slot->type ) )
889 return( slot->data.raw.bytes * 8 );
890#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200891 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100892 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200893#endif /* defined(MBEDTLS_RSA_C) */
894#if defined(MBEDTLS_ECP_C)
895 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
896 return( slot->data.ecp->grp.pbits );
897#endif /* defined(MBEDTLS_ECP_C) */
898 /* Shouldn't happen except on an empty slot. */
899 return( 0 );
900}
901
Gilles Peskinec5487a82018-12-03 18:08:14 +0100902psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200903 psa_key_type_t *type,
904 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100905{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100906 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200907 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100908
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100909 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200910 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100911 if( bits != NULL )
912 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100913 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200914 if( status != PSA_SUCCESS )
915 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200916
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100917 if( slot->type == PSA_KEY_TYPE_NONE )
918 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200919 if( type != NULL )
920 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200921 if( bits != NULL )
922 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100923 return( PSA_SUCCESS );
924}
925
Gilles Peskine2f060a82018-12-04 17:12:32 +0100926static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200927 uint8_t *data,
928 size_t data_size,
929 size_t *data_length,
930 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100931{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100932 *data_length = 0;
933
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200934 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300935 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300936
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200937 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100938 {
939 if( slot->data.raw.bytes > data_size )
940 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100941 if( data_size != 0 )
942 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200943 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100944 memset( data + slot->data.raw.bytes, 0,
945 data_size - slot->data.raw.bytes );
946 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947 *data_length = slot->data.raw.bytes;
948 return( PSA_SUCCESS );
949 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100950#if defined(MBEDTLS_ECP_C)
951 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
952 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000953 psa_status_t status;
954
Gilles Peskine188c71e2018-10-29 19:26:02 +0100955 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
956 if( bytes > data_size )
957 return( PSA_ERROR_BUFFER_TOO_SMALL );
958 status = mbedtls_to_psa_error(
959 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
960 if( status != PSA_SUCCESS )
961 return( status );
962 memset( data + bytes, 0, data_size - bytes );
963 *data_length = bytes;
964 return( PSA_SUCCESS );
965 }
966#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100967 else
Moran Peker17e36e12018-05-02 12:55:20 +0300968 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100969#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200970 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300971 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100972 {
Moran Pekera998bc62018-04-16 18:16:20 +0300973 mbedtls_pk_context pk;
974 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200975 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300976 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100977#if defined(MBEDTLS_RSA_C)
978 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300979 pk.pk_info = &mbedtls_rsa_info;
980 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100981#else
982 return( PSA_ERROR_NOT_SUPPORTED );
983#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300984 }
985 else
986 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100987#if defined(MBEDTLS_ECP_C)
988 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300989 pk.pk_info = &mbedtls_eckey_info;
990 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100991#else
992 return( PSA_ERROR_NOT_SUPPORTED );
993#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300994 }
Moran Pekerd7326592018-05-29 16:56:39 +0300995 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300996 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300997 else
998 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300999 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001000 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001001 /* If data_size is 0 then data may be NULL and then the
1002 * call to memset would have undefined behavior. */
1003 if( data_size != 0 )
1004 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001005 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001006 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001007 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1008 * Move the data to the beginning and erase remaining data
1009 * at the original location. */
1010 if( 2 * (size_t) ret <= data_size )
1011 {
1012 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001013 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001014 }
1015 else if( (size_t) ret < data_size )
1016 {
1017 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001018 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001019 }
Moran Pekera998bc62018-04-16 18:16:20 +03001020 *data_length = ret;
1021 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001022 }
1023 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001024#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001025 {
1026 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001027 it is valid for a special-purpose implementation to omit
1028 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001029 return( PSA_ERROR_NOT_SUPPORTED );
1030 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 }
1032}
1033
Gilles Peskinec5487a82018-12-03 18:08:14 +01001034psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001035 uint8_t *data,
1036 size_t data_size,
1037 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001038{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001039 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001040 psa_status_t status;
1041
1042 /* Set the key to empty now, so that even when there are errors, we always
1043 * set data_length to a value between 0 and data_size. On error, setting
1044 * the key to empty is a good choice because an empty key representation is
1045 * unlikely to be accepted anywhere. */
1046 *data_length = 0;
1047
1048 /* Export requires the EXPORT flag. There is an exception for public keys,
1049 * which don't require any flag, but psa_get_key_from_slot takes
1050 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001051 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001052 if( status != PSA_SUCCESS )
1053 return( status );
1054 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001055 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001056}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057
Gilles Peskinec5487a82018-12-03 18:08:14 +01001058psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001059 uint8_t *data,
1060 size_t data_size,
1061 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001062{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001063 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001064 psa_status_t status;
1065
1066 /* Set the key to empty now, so that even when there are errors, we always
1067 * set data_length to a value between 0 and data_size. On error, setting
1068 * the key to empty is a good choice because an empty key representation is
1069 * unlikely to be accepted anywhere. */
1070 *data_length = 0;
1071
1072 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001073 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001074 if( status != PSA_SUCCESS )
1075 return( status );
1076 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001077 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001078}
1079
Darryl Green0c6575a2018-11-07 16:05:30 +00001080#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001081static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001082 size_t bits )
1083{
1084 psa_status_t status;
1085 uint8_t *data;
1086 size_t key_length;
1087 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1088 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001089 if( data == NULL )
1090 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001091 /* Get key data in export format */
1092 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1093 if( status != PSA_SUCCESS )
1094 {
1095 slot->type = PSA_KEY_TYPE_NONE;
1096 goto exit;
1097 }
1098 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001099 status = psa_save_persistent_key( slot->persistent_storage_id,
1100 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001101 data, key_length );
1102 if( status != PSA_SUCCESS )
1103 {
1104 slot->type = PSA_KEY_TYPE_NONE;
1105 }
1106exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001107 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001108 mbedtls_free( data );
1109 return( status );
1110}
1111#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1112
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001113
1114
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001115/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001116/* Message digests */
1117/****************************************************************/
1118
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001119static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001120{
1121 switch( alg )
1122 {
1123#if defined(MBEDTLS_MD2_C)
1124 case PSA_ALG_MD2:
1125 return( &mbedtls_md2_info );
1126#endif
1127#if defined(MBEDTLS_MD4_C)
1128 case PSA_ALG_MD4:
1129 return( &mbedtls_md4_info );
1130#endif
1131#if defined(MBEDTLS_MD5_C)
1132 case PSA_ALG_MD5:
1133 return( &mbedtls_md5_info );
1134#endif
1135#if defined(MBEDTLS_RIPEMD160_C)
1136 case PSA_ALG_RIPEMD160:
1137 return( &mbedtls_ripemd160_info );
1138#endif
1139#if defined(MBEDTLS_SHA1_C)
1140 case PSA_ALG_SHA_1:
1141 return( &mbedtls_sha1_info );
1142#endif
1143#if defined(MBEDTLS_SHA256_C)
1144 case PSA_ALG_SHA_224:
1145 return( &mbedtls_sha224_info );
1146 case PSA_ALG_SHA_256:
1147 return( &mbedtls_sha256_info );
1148#endif
1149#if defined(MBEDTLS_SHA512_C)
1150 case PSA_ALG_SHA_384:
1151 return( &mbedtls_sha384_info );
1152 case PSA_ALG_SHA_512:
1153 return( &mbedtls_sha512_info );
1154#endif
1155 default:
1156 return( NULL );
1157 }
1158}
1159
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001160psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1161{
1162 switch( operation->alg )
1163 {
Gilles Peskine81736312018-06-26 15:04:31 +02001164 case 0:
1165 /* The object has (apparently) been initialized but it is not
1166 * in use. It's ok to call abort on such an object, and there's
1167 * nothing to do. */
1168 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001169#if defined(MBEDTLS_MD2_C)
1170 case PSA_ALG_MD2:
1171 mbedtls_md2_free( &operation->ctx.md2 );
1172 break;
1173#endif
1174#if defined(MBEDTLS_MD4_C)
1175 case PSA_ALG_MD4:
1176 mbedtls_md4_free( &operation->ctx.md4 );
1177 break;
1178#endif
1179#if defined(MBEDTLS_MD5_C)
1180 case PSA_ALG_MD5:
1181 mbedtls_md5_free( &operation->ctx.md5 );
1182 break;
1183#endif
1184#if defined(MBEDTLS_RIPEMD160_C)
1185 case PSA_ALG_RIPEMD160:
1186 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1187 break;
1188#endif
1189#if defined(MBEDTLS_SHA1_C)
1190 case PSA_ALG_SHA_1:
1191 mbedtls_sha1_free( &operation->ctx.sha1 );
1192 break;
1193#endif
1194#if defined(MBEDTLS_SHA256_C)
1195 case PSA_ALG_SHA_224:
1196 case PSA_ALG_SHA_256:
1197 mbedtls_sha256_free( &operation->ctx.sha256 );
1198 break;
1199#endif
1200#if defined(MBEDTLS_SHA512_C)
1201 case PSA_ALG_SHA_384:
1202 case PSA_ALG_SHA_512:
1203 mbedtls_sha512_free( &operation->ctx.sha512 );
1204 break;
1205#endif
1206 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001207 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001208 }
1209 operation->alg = 0;
1210 return( PSA_SUCCESS );
1211}
1212
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001213psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001214 psa_algorithm_t alg )
1215{
1216 int ret;
1217 operation->alg = 0;
1218 switch( alg )
1219 {
1220#if defined(MBEDTLS_MD2_C)
1221 case PSA_ALG_MD2:
1222 mbedtls_md2_init( &operation->ctx.md2 );
1223 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1224 break;
1225#endif
1226#if defined(MBEDTLS_MD4_C)
1227 case PSA_ALG_MD4:
1228 mbedtls_md4_init( &operation->ctx.md4 );
1229 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1230 break;
1231#endif
1232#if defined(MBEDTLS_MD5_C)
1233 case PSA_ALG_MD5:
1234 mbedtls_md5_init( &operation->ctx.md5 );
1235 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1236 break;
1237#endif
1238#if defined(MBEDTLS_RIPEMD160_C)
1239 case PSA_ALG_RIPEMD160:
1240 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1241 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1242 break;
1243#endif
1244#if defined(MBEDTLS_SHA1_C)
1245 case PSA_ALG_SHA_1:
1246 mbedtls_sha1_init( &operation->ctx.sha1 );
1247 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1248 break;
1249#endif
1250#if defined(MBEDTLS_SHA256_C)
1251 case PSA_ALG_SHA_224:
1252 mbedtls_sha256_init( &operation->ctx.sha256 );
1253 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1254 break;
1255 case PSA_ALG_SHA_256:
1256 mbedtls_sha256_init( &operation->ctx.sha256 );
1257 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1258 break;
1259#endif
1260#if defined(MBEDTLS_SHA512_C)
1261 case PSA_ALG_SHA_384:
1262 mbedtls_sha512_init( &operation->ctx.sha512 );
1263 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1264 break;
1265 case PSA_ALG_SHA_512:
1266 mbedtls_sha512_init( &operation->ctx.sha512 );
1267 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1268 break;
1269#endif
1270 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001271 return( PSA_ALG_IS_HASH( alg ) ?
1272 PSA_ERROR_NOT_SUPPORTED :
1273 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001274 }
1275 if( ret == 0 )
1276 operation->alg = alg;
1277 else
1278 psa_hash_abort( operation );
1279 return( mbedtls_to_psa_error( ret ) );
1280}
1281
1282psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1283 const uint8_t *input,
1284 size_t input_length )
1285{
1286 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001287
1288 /* Don't require hash implementations to behave correctly on a
1289 * zero-length input, which may have an invalid pointer. */
1290 if( input_length == 0 )
1291 return( PSA_SUCCESS );
1292
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001293 switch( operation->alg )
1294 {
1295#if defined(MBEDTLS_MD2_C)
1296 case PSA_ALG_MD2:
1297 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1298 input, input_length );
1299 break;
1300#endif
1301#if defined(MBEDTLS_MD4_C)
1302 case PSA_ALG_MD4:
1303 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1304 input, input_length );
1305 break;
1306#endif
1307#if defined(MBEDTLS_MD5_C)
1308 case PSA_ALG_MD5:
1309 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1310 input, input_length );
1311 break;
1312#endif
1313#if defined(MBEDTLS_RIPEMD160_C)
1314 case PSA_ALG_RIPEMD160:
1315 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1316 input, input_length );
1317 break;
1318#endif
1319#if defined(MBEDTLS_SHA1_C)
1320 case PSA_ALG_SHA_1:
1321 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1322 input, input_length );
1323 break;
1324#endif
1325#if defined(MBEDTLS_SHA256_C)
1326 case PSA_ALG_SHA_224:
1327 case PSA_ALG_SHA_256:
1328 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1329 input, input_length );
1330 break;
1331#endif
1332#if defined(MBEDTLS_SHA512_C)
1333 case PSA_ALG_SHA_384:
1334 case PSA_ALG_SHA_512:
1335 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1336 input, input_length );
1337 break;
1338#endif
1339 default:
1340 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1341 break;
1342 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001343
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001344 if( ret != 0 )
1345 psa_hash_abort( operation );
1346 return( mbedtls_to_psa_error( ret ) );
1347}
1348
1349psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1350 uint8_t *hash,
1351 size_t hash_size,
1352 size_t *hash_length )
1353{
itayzafrir40835d42018-08-02 13:14:17 +03001354 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001355 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001356 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001357
1358 /* Fill the output buffer with something that isn't a valid hash
1359 * (barring an attack on the hash and deliberately-crafted input),
1360 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001361 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001362 /* If hash_size is 0 then hash may be NULL and then the
1363 * call to memset would have undefined behavior. */
1364 if( hash_size != 0 )
1365 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001366
1367 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001368 {
1369 status = PSA_ERROR_BUFFER_TOO_SMALL;
1370 goto exit;
1371 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001372
1373 switch( operation->alg )
1374 {
1375#if defined(MBEDTLS_MD2_C)
1376 case PSA_ALG_MD2:
1377 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1378 break;
1379#endif
1380#if defined(MBEDTLS_MD4_C)
1381 case PSA_ALG_MD4:
1382 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1383 break;
1384#endif
1385#if defined(MBEDTLS_MD5_C)
1386 case PSA_ALG_MD5:
1387 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1388 break;
1389#endif
1390#if defined(MBEDTLS_RIPEMD160_C)
1391 case PSA_ALG_RIPEMD160:
1392 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1393 break;
1394#endif
1395#if defined(MBEDTLS_SHA1_C)
1396 case PSA_ALG_SHA_1:
1397 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1398 break;
1399#endif
1400#if defined(MBEDTLS_SHA256_C)
1401 case PSA_ALG_SHA_224:
1402 case PSA_ALG_SHA_256:
1403 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1404 break;
1405#endif
1406#if defined(MBEDTLS_SHA512_C)
1407 case PSA_ALG_SHA_384:
1408 case PSA_ALG_SHA_512:
1409 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1410 break;
1411#endif
1412 default:
1413 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1414 break;
1415 }
itayzafrir40835d42018-08-02 13:14:17 +03001416 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001417
itayzafrir40835d42018-08-02 13:14:17 +03001418exit:
1419 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001420 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001421 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001422 return( psa_hash_abort( operation ) );
1423 }
1424 else
1425 {
1426 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001427 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001428 }
1429}
1430
Gilles Peskine2d277862018-06-18 15:41:12 +02001431psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1432 const uint8_t *hash,
1433 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001434{
1435 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1436 size_t actual_hash_length;
1437 psa_status_t status = psa_hash_finish( operation,
1438 actual_hash, sizeof( actual_hash ),
1439 &actual_hash_length );
1440 if( status != PSA_SUCCESS )
1441 return( status );
1442 if( actual_hash_length != hash_length )
1443 return( PSA_ERROR_INVALID_SIGNATURE );
1444 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1445 return( PSA_ERROR_INVALID_SIGNATURE );
1446 return( PSA_SUCCESS );
1447}
1448
1449
1450
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001451/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001452/* MAC */
1453/****************************************************************/
1454
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001455static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001456 psa_algorithm_t alg,
1457 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001458 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001459 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001460{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001461 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001462 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001463
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001464 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001465 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001466
Gilles Peskine8c9def32018-02-08 10:02:12 +01001467 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1468 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001469 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001470 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001471 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001472 mode = MBEDTLS_MODE_STREAM;
1473 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001474 case PSA_ALG_CTR:
1475 mode = MBEDTLS_MODE_CTR;
1476 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001477 case PSA_ALG_CFB:
1478 mode = MBEDTLS_MODE_CFB;
1479 break;
1480 case PSA_ALG_OFB:
1481 mode = MBEDTLS_MODE_OFB;
1482 break;
1483 case PSA_ALG_CBC_NO_PADDING:
1484 mode = MBEDTLS_MODE_CBC;
1485 break;
1486 case PSA_ALG_CBC_PKCS7:
1487 mode = MBEDTLS_MODE_CBC;
1488 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001489 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001490 mode = MBEDTLS_MODE_CCM;
1491 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001492 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001493 mode = MBEDTLS_MODE_GCM;
1494 break;
1495 default:
1496 return( NULL );
1497 }
1498 }
1499 else if( alg == PSA_ALG_CMAC )
1500 mode = MBEDTLS_MODE_ECB;
1501 else if( alg == PSA_ALG_GMAC )
1502 mode = MBEDTLS_MODE_GCM;
1503 else
1504 return( NULL );
1505
1506 switch( key_type )
1507 {
1508 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001509 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001510 break;
1511 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001512 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1513 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001514 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001515 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001516 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001517 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001518 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1519 * but two-key Triple-DES is functionally three-key Triple-DES
1520 * with K1=K3, so that's how we present it to mbedtls. */
1521 if( key_bits == 128 )
1522 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001523 break;
1524 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001525 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526 break;
1527 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001528 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001529 break;
1530 default:
1531 return( NULL );
1532 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001533 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001534 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001535
Jaeden Amero23bbb752018-06-26 14:16:54 +01001536 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1537 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001538}
1539
Gilles Peskinea05219c2018-11-16 16:02:56 +01001540#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001541static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001542{
Gilles Peskine2d277862018-06-18 15:41:12 +02001543 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001544 {
1545 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001546 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001547 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001548 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001549 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001550 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001551 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001552 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001553 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001554 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001555 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001556 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001557 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001558 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001559 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001560 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001561 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001562 return( 128 );
1563 default:
1564 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001565 }
1566}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001567#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001568
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001569/* Initialize the MAC operation structure. Once this function has been
1570 * called, psa_mac_abort can run and will do the right thing. */
1571static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1572 psa_algorithm_t alg )
1573{
1574 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1575
1576 operation->alg = alg;
1577 operation->key_set = 0;
1578 operation->iv_set = 0;
1579 operation->iv_required = 0;
1580 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001581 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001582
1583#if defined(MBEDTLS_CMAC_C)
1584 if( alg == PSA_ALG_CMAC )
1585 {
1586 operation->iv_required = 0;
1587 mbedtls_cipher_init( &operation->ctx.cmac );
1588 status = PSA_SUCCESS;
1589 }
1590 else
1591#endif /* MBEDTLS_CMAC_C */
1592#if defined(MBEDTLS_MD_C)
1593 if( PSA_ALG_IS_HMAC( operation->alg ) )
1594 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001595 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1596 operation->ctx.hmac.hash_ctx.alg = 0;
1597 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001598 }
1599 else
1600#endif /* MBEDTLS_MD_C */
1601 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001602 if( ! PSA_ALG_IS_MAC( alg ) )
1603 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001604 }
1605
1606 if( status != PSA_SUCCESS )
1607 memset( operation, 0, sizeof( *operation ) );
1608 return( status );
1609}
1610
Gilles Peskine01126fa2018-07-12 17:04:55 +02001611#if defined(MBEDTLS_MD_C)
1612static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1613{
Gilles Peskine3f108122018-12-07 18:14:53 +01001614 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001615 return( psa_hash_abort( &hmac->hash_ctx ) );
1616}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001617
1618static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1619{
1620 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1621 memset( hmac, 0, sizeof( *hmac ) );
1622}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001623#endif /* MBEDTLS_MD_C */
1624
Gilles Peskine8c9def32018-02-08 10:02:12 +01001625psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1626{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001627 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001628 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001629 /* The object has (apparently) been initialized but it is not
1630 * in use. It's ok to call abort on such an object, and there's
1631 * nothing to do. */
1632 return( PSA_SUCCESS );
1633 }
1634 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001635#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001636 if( operation->alg == PSA_ALG_CMAC )
1637 {
1638 mbedtls_cipher_free( &operation->ctx.cmac );
1639 }
1640 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001641#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001642#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001643 if( PSA_ALG_IS_HMAC( operation->alg ) )
1644 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001645 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001646 }
1647 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001648#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001649 {
1650 /* Sanity check (shouldn't happen: operation->alg should
1651 * always have been initialized to a valid value). */
1652 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001653 }
Moran Peker41deec42018-04-04 15:43:05 +03001654
Gilles Peskine8c9def32018-02-08 10:02:12 +01001655 operation->alg = 0;
1656 operation->key_set = 0;
1657 operation->iv_set = 0;
1658 operation->iv_required = 0;
1659 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001660 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001661
Gilles Peskine8c9def32018-02-08 10:02:12 +01001662 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001663
1664bad_state:
1665 /* If abort is called on an uninitialized object, we can't trust
1666 * anything. Wipe the object in case it contains confidential data.
1667 * This may result in a memory leak if a pointer gets overwritten,
1668 * but it's too late to do anything about this. */
1669 memset( operation, 0, sizeof( *operation ) );
1670 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001671}
1672
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001673#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001674static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001675 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001676 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001677 const mbedtls_cipher_info_t *cipher_info )
1678{
1679 int ret;
1680
1681 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001682
1683 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1684 if( ret != 0 )
1685 return( ret );
1686
1687 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1688 slot->data.raw.data,
1689 key_bits );
1690 return( ret );
1691}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001692#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001693
Gilles Peskine248051a2018-06-20 16:09:38 +02001694#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001695static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1696 const uint8_t *key,
1697 size_t key_length,
1698 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001699{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001700 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001701 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001702 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001703 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001704 psa_status_t status;
1705
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001706 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1707 * overflow below. This should never trigger if the hash algorithm
1708 * is implemented correctly. */
1709 /* The size checks against the ipad and opad buffers cannot be written
1710 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1711 * because that triggers -Wlogical-op on GCC 7.3. */
1712 if( block_size > sizeof( ipad ) )
1713 return( PSA_ERROR_NOT_SUPPORTED );
1714 if( block_size > sizeof( hmac->opad ) )
1715 return( PSA_ERROR_NOT_SUPPORTED );
1716 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001717 return( PSA_ERROR_NOT_SUPPORTED );
1718
Gilles Peskined223b522018-06-11 18:12:58 +02001719 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001720 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001721 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001722 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001723 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001724 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001725 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001726 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001727 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001728 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001729 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001730 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001731 }
Gilles Peskine96889972018-07-12 17:07:03 +02001732 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1733 * but it is permitted. It is common when HMAC is used in HKDF, for
1734 * example. Don't call `memcpy` in the 0-length because `key` could be
1735 * an invalid pointer which would make the behavior undefined. */
1736 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001737 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001738
Gilles Peskined223b522018-06-11 18:12:58 +02001739 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1740 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001741 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001742 ipad[i] ^= 0x36;
1743 memset( ipad + key_length, 0x36, block_size - key_length );
1744
1745 /* Copy the key material from ipad to opad, flipping the requisite bits,
1746 * and filling the rest of opad with the requisite constant. */
1747 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001748 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1749 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001750
Gilles Peskine01126fa2018-07-12 17:04:55 +02001751 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001752 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001753 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001754
Gilles Peskine01126fa2018-07-12 17:04:55 +02001755 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001756
1757cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001758 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001759
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001760 return( status );
1761}
Gilles Peskine248051a2018-06-20 16:09:38 +02001762#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001763
Gilles Peskine89167cb2018-07-08 20:12:23 +02001764static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001765 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001766 psa_algorithm_t alg,
1767 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001768{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001769 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001770 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001771 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001772 psa_key_usage_t usage =
1773 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001774 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001775 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001776
Gilles Peskined911eb72018-08-14 15:18:45 +02001777 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001778 if( status != PSA_SUCCESS )
1779 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001780 if( is_sign )
1781 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001782
Gilles Peskinec5487a82018-12-03 18:08:14 +01001783 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001784 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001785 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001786 key_bits = psa_get_key_bits( slot );
1787
Gilles Peskine8c9def32018-02-08 10:02:12 +01001788#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001789 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001790 {
1791 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001792 mbedtls_cipher_info_from_psa( full_length_alg,
1793 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001794 int ret;
1795 if( cipher_info == NULL )
1796 {
1797 status = PSA_ERROR_NOT_SUPPORTED;
1798 goto exit;
1799 }
1800 operation->mac_size = cipher_info->block_size;
1801 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1802 status = mbedtls_to_psa_error( ret );
1803 }
1804 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001805#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001806#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001807 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001808 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001809 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001810 if( hash_alg == 0 )
1811 {
1812 status = PSA_ERROR_NOT_SUPPORTED;
1813 goto exit;
1814 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001815
1816 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1817 /* Sanity check. This shouldn't fail on a valid configuration. */
1818 if( operation->mac_size == 0 ||
1819 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1820 {
1821 status = PSA_ERROR_NOT_SUPPORTED;
1822 goto exit;
1823 }
1824
Gilles Peskine01126fa2018-07-12 17:04:55 +02001825 if( slot->type != PSA_KEY_TYPE_HMAC )
1826 {
1827 status = PSA_ERROR_INVALID_ARGUMENT;
1828 goto exit;
1829 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001830
Gilles Peskine01126fa2018-07-12 17:04:55 +02001831 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1832 slot->data.raw.data,
1833 slot->data.raw.bytes,
1834 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001835 }
1836 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001837#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001838 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001839 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001840 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001841 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001842
Gilles Peskined911eb72018-08-14 15:18:45 +02001843 if( truncated == 0 )
1844 {
1845 /* The "normal" case: untruncated algorithm. Nothing to do. */
1846 }
1847 else if( truncated < 4 )
1848 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001849 /* A very short MAC is too short for security since it can be
1850 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1851 * so we make this our minimum, even though 32 bits is still
1852 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001853 status = PSA_ERROR_NOT_SUPPORTED;
1854 }
1855 else if( truncated > operation->mac_size )
1856 {
1857 /* It's impossible to "truncate" to a larger length. */
1858 status = PSA_ERROR_INVALID_ARGUMENT;
1859 }
1860 else
1861 operation->mac_size = truncated;
1862
Gilles Peskinefbfac682018-07-08 20:51:54 +02001863exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001864 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001865 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001866 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001867 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001868 else
1869 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001870 operation->key_set = 1;
1871 }
1872 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001873}
1874
Gilles Peskine89167cb2018-07-08 20:12:23 +02001875psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001876 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001877 psa_algorithm_t alg )
1878{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001879 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001880}
1881
1882psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001883 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001884 psa_algorithm_t alg )
1885{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001886 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001887}
1888
Gilles Peskine8c9def32018-02-08 10:02:12 +01001889psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1890 const uint8_t *input,
1891 size_t input_length )
1892{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001893 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001894 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001895 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001896 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001897 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001898 operation->has_input = 1;
1899
Gilles Peskine8c9def32018-02-08 10:02:12 +01001900#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001901 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001902 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001903 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1904 input, input_length );
1905 status = mbedtls_to_psa_error( ret );
1906 }
1907 else
1908#endif /* MBEDTLS_CMAC_C */
1909#if defined(MBEDTLS_MD_C)
1910 if( PSA_ALG_IS_HMAC( operation->alg ) )
1911 {
1912 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1913 input_length );
1914 }
1915 else
1916#endif /* MBEDTLS_MD_C */
1917 {
1918 /* This shouldn't happen if `operation` was initialized by
1919 * a setup function. */
1920 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001921 }
1922
Gilles Peskinefbfac682018-07-08 20:51:54 +02001923cleanup:
1924 if( status != PSA_SUCCESS )
1925 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001926 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001927}
1928
Gilles Peskine01126fa2018-07-12 17:04:55 +02001929#if defined(MBEDTLS_MD_C)
1930static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1931 uint8_t *mac,
1932 size_t mac_size )
1933{
1934 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1935 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1936 size_t hash_size = 0;
1937 size_t block_size = psa_get_hash_block_size( hash_alg );
1938 psa_status_t status;
1939
Gilles Peskine01126fa2018-07-12 17:04:55 +02001940 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1941 if( status != PSA_SUCCESS )
1942 return( status );
1943 /* From here on, tmp needs to be wiped. */
1944
1945 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1946 if( status != PSA_SUCCESS )
1947 goto exit;
1948
1949 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1950 if( status != PSA_SUCCESS )
1951 goto exit;
1952
1953 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1954 if( status != PSA_SUCCESS )
1955 goto exit;
1956
Gilles Peskined911eb72018-08-14 15:18:45 +02001957 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1958 if( status != PSA_SUCCESS )
1959 goto exit;
1960
1961 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001962
1963exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001964 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001965 return( status );
1966}
1967#endif /* MBEDTLS_MD_C */
1968
mohammad16036df908f2018-04-02 08:34:15 -07001969static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001970 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001971 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001972{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001973 if( ! operation->key_set )
1974 return( PSA_ERROR_BAD_STATE );
1975 if( operation->iv_required && ! operation->iv_set )
1976 return( PSA_ERROR_BAD_STATE );
1977
Gilles Peskine8c9def32018-02-08 10:02:12 +01001978 if( mac_size < operation->mac_size )
1979 return( PSA_ERROR_BUFFER_TOO_SMALL );
1980
Gilles Peskine8c9def32018-02-08 10:02:12 +01001981#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001982 if( operation->alg == PSA_ALG_CMAC )
1983 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001984 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1985 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1986 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02001987 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01001988 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001989 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001990 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001991 else
1992#endif /* MBEDTLS_CMAC_C */
1993#if defined(MBEDTLS_MD_C)
1994 if( PSA_ALG_IS_HMAC( operation->alg ) )
1995 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001996 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001997 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001998 }
1999 else
2000#endif /* MBEDTLS_MD_C */
2001 {
2002 /* This shouldn't happen if `operation` was initialized by
2003 * a setup function. */
2004 return( PSA_ERROR_BAD_STATE );
2005 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002006}
2007
Gilles Peskineacd4be32018-07-08 19:56:25 +02002008psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2009 uint8_t *mac,
2010 size_t mac_size,
2011 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002012{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002013 psa_status_t status;
2014
2015 /* Fill the output buffer with something that isn't a valid mac
2016 * (barring an attack on the mac and deliberately-crafted input),
2017 * in case the caller doesn't check the return status properly. */
2018 *mac_length = mac_size;
2019 /* If mac_size is 0 then mac may be NULL and then the
2020 * call to memset would have undefined behavior. */
2021 if( mac_size != 0 )
2022 memset( mac, '!', mac_size );
2023
Gilles Peskine89167cb2018-07-08 20:12:23 +02002024 if( ! operation->is_sign )
2025 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002026 status = PSA_ERROR_BAD_STATE;
2027 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002028 }
mohammad16036df908f2018-04-02 08:34:15 -07002029
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002030 status = psa_mac_finish_internal( operation, mac, mac_size );
2031
2032cleanup:
2033 if( status == PSA_SUCCESS )
2034 {
2035 status = psa_mac_abort( operation );
2036 if( status == PSA_SUCCESS )
2037 *mac_length = operation->mac_size;
2038 else
2039 memset( mac, '!', mac_size );
2040 }
2041 else
2042 psa_mac_abort( operation );
2043 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002044}
2045
Gilles Peskineacd4be32018-07-08 19:56:25 +02002046psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2047 const uint8_t *mac,
2048 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002049{
Gilles Peskine828ed142018-06-18 23:25:51 +02002050 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002051 psa_status_t status;
2052
Gilles Peskine89167cb2018-07-08 20:12:23 +02002053 if( operation->is_sign )
2054 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002055 status = PSA_ERROR_BAD_STATE;
2056 goto cleanup;
2057 }
2058 if( operation->mac_size != mac_length )
2059 {
2060 status = PSA_ERROR_INVALID_SIGNATURE;
2061 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002062 }
mohammad16036df908f2018-04-02 08:34:15 -07002063
2064 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002065 actual_mac, sizeof( actual_mac ) );
2066
2067 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2068 status = PSA_ERROR_INVALID_SIGNATURE;
2069
2070cleanup:
2071 if( status == PSA_SUCCESS )
2072 status = psa_mac_abort( operation );
2073 else
2074 psa_mac_abort( operation );
2075
Gilles Peskine3f108122018-12-07 18:14:53 +01002076 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002077
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002078 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002079}
2080
2081
Gilles Peskine20035e32018-02-03 22:44:14 +01002082
Gilles Peskine20035e32018-02-03 22:44:14 +01002083/****************************************************************/
2084/* Asymmetric cryptography */
2085/****************************************************************/
2086
Gilles Peskine2b450e32018-06-27 15:42:46 +02002087#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002088/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002089 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002090static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2091 size_t hash_length,
2092 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002093{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002094 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002095 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002096 *md_alg = mbedtls_md_get_type( md_info );
2097
2098 /* The Mbed TLS RSA module uses an unsigned int for hash length
2099 * parameters. Validate that it fits so that we don't risk an
2100 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002101#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002102 if( hash_length > UINT_MAX )
2103 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002104#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002105
2106#if defined(MBEDTLS_PKCS1_V15)
2107 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2108 * must be correct. */
2109 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2110 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002111 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002112 if( md_info == NULL )
2113 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002114 if( mbedtls_md_get_size( md_info ) != hash_length )
2115 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002116 }
2117#endif /* MBEDTLS_PKCS1_V15 */
2118
2119#if defined(MBEDTLS_PKCS1_V21)
2120 /* PSS requires a hash internally. */
2121 if( PSA_ALG_IS_RSA_PSS( alg ) )
2122 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002123 if( md_info == NULL )
2124 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002125 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002126#endif /* MBEDTLS_PKCS1_V21 */
2127
Gilles Peskine61b91d42018-06-08 16:09:36 +02002128 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002129}
2130
Gilles Peskine2b450e32018-06-27 15:42:46 +02002131static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2132 psa_algorithm_t alg,
2133 const uint8_t *hash,
2134 size_t hash_length,
2135 uint8_t *signature,
2136 size_t signature_size,
2137 size_t *signature_length )
2138{
2139 psa_status_t status;
2140 int ret;
2141 mbedtls_md_type_t md_alg;
2142
2143 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2144 if( status != PSA_SUCCESS )
2145 return( status );
2146
Gilles Peskine630a18a2018-06-29 17:49:35 +02002147 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002148 return( PSA_ERROR_BUFFER_TOO_SMALL );
2149
2150#if defined(MBEDTLS_PKCS1_V15)
2151 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2152 {
2153 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2154 MBEDTLS_MD_NONE );
2155 ret = mbedtls_rsa_pkcs1_sign( rsa,
2156 mbedtls_ctr_drbg_random,
2157 &global_data.ctr_drbg,
2158 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002159 md_alg,
2160 (unsigned int) hash_length,
2161 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002162 signature );
2163 }
2164 else
2165#endif /* MBEDTLS_PKCS1_V15 */
2166#if defined(MBEDTLS_PKCS1_V21)
2167 if( PSA_ALG_IS_RSA_PSS( alg ) )
2168 {
2169 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2170 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2171 mbedtls_ctr_drbg_random,
2172 &global_data.ctr_drbg,
2173 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002174 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002175 (unsigned int) hash_length,
2176 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002177 signature );
2178 }
2179 else
2180#endif /* MBEDTLS_PKCS1_V21 */
2181 {
2182 return( PSA_ERROR_INVALID_ARGUMENT );
2183 }
2184
2185 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002186 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002187 return( mbedtls_to_psa_error( ret ) );
2188}
2189
2190static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2191 psa_algorithm_t alg,
2192 const uint8_t *hash,
2193 size_t hash_length,
2194 const uint8_t *signature,
2195 size_t signature_length )
2196{
2197 psa_status_t status;
2198 int ret;
2199 mbedtls_md_type_t md_alg;
2200
2201 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2202 if( status != PSA_SUCCESS )
2203 return( status );
2204
Gilles Peskine630a18a2018-06-29 17:49:35 +02002205 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002206 return( PSA_ERROR_BUFFER_TOO_SMALL );
2207
2208#if defined(MBEDTLS_PKCS1_V15)
2209 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2210 {
2211 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2212 MBEDTLS_MD_NONE );
2213 ret = mbedtls_rsa_pkcs1_verify( rsa,
2214 mbedtls_ctr_drbg_random,
2215 &global_data.ctr_drbg,
2216 MBEDTLS_RSA_PUBLIC,
2217 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002218 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002219 hash,
2220 signature );
2221 }
2222 else
2223#endif /* MBEDTLS_PKCS1_V15 */
2224#if defined(MBEDTLS_PKCS1_V21)
2225 if( PSA_ALG_IS_RSA_PSS( alg ) )
2226 {
2227 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2228 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2229 mbedtls_ctr_drbg_random,
2230 &global_data.ctr_drbg,
2231 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002232 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002233 (unsigned int) hash_length,
2234 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002235 signature );
2236 }
2237 else
2238#endif /* MBEDTLS_PKCS1_V21 */
2239 {
2240 return( PSA_ERROR_INVALID_ARGUMENT );
2241 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002242
2243 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2244 * the rest of the signature is invalid". This has little use in
2245 * practice and PSA doesn't report this distinction. */
2246 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2247 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002248 return( mbedtls_to_psa_error( ret ) );
2249}
2250#endif /* MBEDTLS_RSA_C */
2251
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002252#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002253/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2254 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2255 * (even though these functions don't modify it). */
2256static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2257 psa_algorithm_t alg,
2258 const uint8_t *hash,
2259 size_t hash_length,
2260 uint8_t *signature,
2261 size_t signature_size,
2262 size_t *signature_length )
2263{
2264 int ret;
2265 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002266 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002267 mbedtls_mpi_init( &r );
2268 mbedtls_mpi_init( &s );
2269
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002270 if( signature_size < 2 * curve_bytes )
2271 {
2272 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2273 goto cleanup;
2274 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002275
Gilles Peskinea05219c2018-11-16 16:02:56 +01002276#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002277 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2278 {
2279 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2280 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2281 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2282 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2283 hash, hash_length,
2284 md_alg ) );
2285 }
2286 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002287#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002288 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002289 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002290 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2291 hash, hash_length,
2292 mbedtls_ctr_drbg_random,
2293 &global_data.ctr_drbg ) );
2294 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002295
2296 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2297 signature,
2298 curve_bytes ) );
2299 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2300 signature + curve_bytes,
2301 curve_bytes ) );
2302
2303cleanup:
2304 mbedtls_mpi_free( &r );
2305 mbedtls_mpi_free( &s );
2306 if( ret == 0 )
2307 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002308 return( mbedtls_to_psa_error( ret ) );
2309}
2310
2311static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2312 const uint8_t *hash,
2313 size_t hash_length,
2314 const uint8_t *signature,
2315 size_t signature_length )
2316{
2317 int ret;
2318 mbedtls_mpi r, s;
2319 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2320 mbedtls_mpi_init( &r );
2321 mbedtls_mpi_init( &s );
2322
2323 if( signature_length != 2 * curve_bytes )
2324 return( PSA_ERROR_INVALID_SIGNATURE );
2325
2326 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2327 signature,
2328 curve_bytes ) );
2329 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2330 signature + curve_bytes,
2331 curve_bytes ) );
2332
2333 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2334 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002335
2336cleanup:
2337 mbedtls_mpi_free( &r );
2338 mbedtls_mpi_free( &s );
2339 return( mbedtls_to_psa_error( ret ) );
2340}
2341#endif /* MBEDTLS_ECDSA_C */
2342
Gilles Peskinec5487a82018-12-03 18:08:14 +01002343psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002344 psa_algorithm_t alg,
2345 const uint8_t *hash,
2346 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002347 uint8_t *signature,
2348 size_t signature_size,
2349 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002350{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002351 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002352 psa_status_t status;
2353
2354 *signature_length = signature_size;
2355
Gilles Peskinec5487a82018-12-03 18:08:14 +01002356 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002357 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002358 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002359 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002360 {
2361 status = PSA_ERROR_INVALID_ARGUMENT;
2362 goto exit;
2363 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002364
Gilles Peskine20035e32018-02-03 22:44:14 +01002365#if defined(MBEDTLS_RSA_C)
2366 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2367 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002368 status = psa_rsa_sign( slot->data.rsa,
2369 alg,
2370 hash, hash_length,
2371 signature, signature_size,
2372 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002373 }
2374 else
2375#endif /* defined(MBEDTLS_RSA_C) */
2376#if defined(MBEDTLS_ECP_C)
2377 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2378 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002379#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002380 if(
2381#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2382 PSA_ALG_IS_ECDSA( alg )
2383#else
2384 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2385#endif
2386 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002387 status = psa_ecdsa_sign( slot->data.ecp,
2388 alg,
2389 hash, hash_length,
2390 signature, signature_size,
2391 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002392 else
2393#endif /* defined(MBEDTLS_ECDSA_C) */
2394 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002395 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002396 }
itayzafrir5c753392018-05-08 11:18:38 +03002397 }
2398 else
2399#endif /* defined(MBEDTLS_ECP_C) */
2400 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002401 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002402 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002403
2404exit:
2405 /* Fill the unused part of the output buffer (the whole buffer on error,
2406 * the trailing part on success) with something that isn't a valid mac
2407 * (barring an attack on the mac and deliberately-crafted input),
2408 * in case the caller doesn't check the return status properly. */
2409 if( status == PSA_SUCCESS )
2410 memset( signature + *signature_length, '!',
2411 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002412 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002413 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002414 /* If signature_size is 0 then we have nothing to do. We must not call
2415 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002416 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002417}
2418
Gilles Peskinec5487a82018-12-03 18:08:14 +01002419psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002420 psa_algorithm_t alg,
2421 const uint8_t *hash,
2422 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002423 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002424 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002425{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002426 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002427 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002428
Gilles Peskinec5487a82018-12-03 18:08:14 +01002429 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002430 if( status != PSA_SUCCESS )
2431 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002432
Gilles Peskine61b91d42018-06-08 16:09:36 +02002433#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002434 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002435 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002436 return( psa_rsa_verify( slot->data.rsa,
2437 alg,
2438 hash, hash_length,
2439 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002440 }
2441 else
2442#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002443#if defined(MBEDTLS_ECP_C)
2444 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2445 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002446#if defined(MBEDTLS_ECDSA_C)
2447 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002448 return( psa_ecdsa_verify( slot->data.ecp,
2449 hash, hash_length,
2450 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002451 else
2452#endif /* defined(MBEDTLS_ECDSA_C) */
2453 {
2454 return( PSA_ERROR_INVALID_ARGUMENT );
2455 }
itayzafrir5c753392018-05-08 11:18:38 +03002456 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002457 else
2458#endif /* defined(MBEDTLS_ECP_C) */
2459 {
2460 return( PSA_ERROR_NOT_SUPPORTED );
2461 }
2462}
2463
Gilles Peskine072ac562018-06-30 00:21:29 +02002464#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2465static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2466 mbedtls_rsa_context *rsa )
2467{
2468 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2469 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2470 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2471 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2472}
2473#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2474
Gilles Peskinec5487a82018-12-03 18:08:14 +01002475psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002476 psa_algorithm_t alg,
2477 const uint8_t *input,
2478 size_t input_length,
2479 const uint8_t *salt,
2480 size_t salt_length,
2481 uint8_t *output,
2482 size_t output_size,
2483 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002484{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002485 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002486 psa_status_t status;
2487
Darryl Green5cc689a2018-07-24 15:34:10 +01002488 (void) input;
2489 (void) input_length;
2490 (void) salt;
2491 (void) output;
2492 (void) output_size;
2493
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002494 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002495
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002496 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2497 return( PSA_ERROR_INVALID_ARGUMENT );
2498
Gilles Peskinec5487a82018-12-03 18:08:14 +01002499 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002500 if( status != PSA_SUCCESS )
2501 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002502 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2503 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002504 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002505
2506#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002507 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002508 {
2509 mbedtls_rsa_context *rsa = slot->data.rsa;
2510 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002511 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002512 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002513#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002514 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002515 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002516 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2517 mbedtls_ctr_drbg_random,
2518 &global_data.ctr_drbg,
2519 MBEDTLS_RSA_PUBLIC,
2520 input_length,
2521 input,
2522 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002523 }
2524 else
2525#endif /* MBEDTLS_PKCS1_V15 */
2526#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002527 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002528 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002529 psa_rsa_oaep_set_padding_mode( alg, rsa );
2530 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2531 mbedtls_ctr_drbg_random,
2532 &global_data.ctr_drbg,
2533 MBEDTLS_RSA_PUBLIC,
2534 salt, salt_length,
2535 input_length,
2536 input,
2537 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002538 }
2539 else
2540#endif /* MBEDTLS_PKCS1_V21 */
2541 {
2542 return( PSA_ERROR_INVALID_ARGUMENT );
2543 }
2544 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002545 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002546 return( mbedtls_to_psa_error( ret ) );
2547 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002548 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002549#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002550 {
2551 return( PSA_ERROR_NOT_SUPPORTED );
2552 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002553}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002554
Gilles Peskinec5487a82018-12-03 18:08:14 +01002555psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002556 psa_algorithm_t alg,
2557 const uint8_t *input,
2558 size_t input_length,
2559 const uint8_t *salt,
2560 size_t salt_length,
2561 uint8_t *output,
2562 size_t output_size,
2563 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002564{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002565 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002566 psa_status_t status;
2567
Darryl Green5cc689a2018-07-24 15:34:10 +01002568 (void) input;
2569 (void) input_length;
2570 (void) salt;
2571 (void) output;
2572 (void) output_size;
2573
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002574 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002575
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002576 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2577 return( PSA_ERROR_INVALID_ARGUMENT );
2578
Gilles Peskinec5487a82018-12-03 18:08:14 +01002579 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002580 if( status != PSA_SUCCESS )
2581 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002582 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2583 return( PSA_ERROR_INVALID_ARGUMENT );
2584
2585#if defined(MBEDTLS_RSA_C)
2586 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2587 {
2588 mbedtls_rsa_context *rsa = slot->data.rsa;
2589 int ret;
2590
Gilles Peskine630a18a2018-06-29 17:49:35 +02002591 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002592 return( PSA_ERROR_INVALID_ARGUMENT );
2593
2594#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002595 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002596 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002597 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2598 mbedtls_ctr_drbg_random,
2599 &global_data.ctr_drbg,
2600 MBEDTLS_RSA_PRIVATE,
2601 output_length,
2602 input,
2603 output,
2604 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002605 }
2606 else
2607#endif /* MBEDTLS_PKCS1_V15 */
2608#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002609 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002610 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002611 psa_rsa_oaep_set_padding_mode( alg, rsa );
2612 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2613 mbedtls_ctr_drbg_random,
2614 &global_data.ctr_drbg,
2615 MBEDTLS_RSA_PRIVATE,
2616 salt, salt_length,
2617 output_length,
2618 input,
2619 output,
2620 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002621 }
2622 else
2623#endif /* MBEDTLS_PKCS1_V21 */
2624 {
2625 return( PSA_ERROR_INVALID_ARGUMENT );
2626 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002627
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002628 return( mbedtls_to_psa_error( ret ) );
2629 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002630 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002631#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002632 {
2633 return( PSA_ERROR_NOT_SUPPORTED );
2634 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002635}
Gilles Peskine20035e32018-02-03 22:44:14 +01002636
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002637
2638
mohammad1603503973b2018-03-12 15:59:30 +02002639/****************************************************************/
2640/* Symmetric cryptography */
2641/****************************************************************/
2642
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002643/* Initialize the cipher operation structure. Once this function has been
2644 * called, psa_cipher_abort can run and will do the right thing. */
2645static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2646 psa_algorithm_t alg )
2647{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002648 if( ! PSA_ALG_IS_CIPHER( alg ) )
2649 {
2650 memset( operation, 0, sizeof( *operation ) );
2651 return( PSA_ERROR_INVALID_ARGUMENT );
2652 }
2653
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002654 operation->alg = alg;
2655 operation->key_set = 0;
2656 operation->iv_set = 0;
2657 operation->iv_required = 1;
2658 operation->iv_size = 0;
2659 operation->block_size = 0;
2660 mbedtls_cipher_init( &operation->ctx.cipher );
2661 return( PSA_SUCCESS );
2662}
2663
Gilles Peskinee553c652018-06-04 16:22:46 +02002664static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002665 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002666 psa_algorithm_t alg,
2667 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002668{
2669 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2670 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002671 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002672 size_t key_bits;
2673 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002674 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2675 PSA_KEY_USAGE_ENCRYPT :
2676 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002677
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002678 status = psa_cipher_init( operation, alg );
2679 if( status != PSA_SUCCESS )
2680 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002681
Gilles Peskinec5487a82018-12-03 18:08:14 +01002682 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002683 if( status != PSA_SUCCESS )
2684 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002685 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002686
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002687 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002688 if( cipher_info == NULL )
2689 return( PSA_ERROR_NOT_SUPPORTED );
2690
mohammad1603503973b2018-03-12 15:59:30 +02002691 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002692 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002693 {
2694 psa_cipher_abort( operation );
2695 return( mbedtls_to_psa_error( ret ) );
2696 }
2697
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002698#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002699 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002700 {
2701 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2702 unsigned char keys[24];
2703 memcpy( keys, slot->data.raw.data, 16 );
2704 memcpy( keys + 16, slot->data.raw.data, 8 );
2705 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2706 keys,
2707 192, cipher_operation );
2708 }
2709 else
2710#endif
2711 {
2712 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2713 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002714 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002715 }
Moran Peker41deec42018-04-04 15:43:05 +03002716 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002717 {
2718 psa_cipher_abort( operation );
2719 return( mbedtls_to_psa_error( ret ) );
2720 }
2721
mohammad16038481e742018-03-18 13:57:31 +02002722#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002723 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002724 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002725 case PSA_ALG_CBC_NO_PADDING:
2726 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2727 MBEDTLS_PADDING_NONE );
2728 break;
2729 case PSA_ALG_CBC_PKCS7:
2730 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2731 MBEDTLS_PADDING_PKCS7 );
2732 break;
2733 default:
2734 /* The algorithm doesn't involve padding. */
2735 ret = 0;
2736 break;
2737 }
2738 if( ret != 0 )
2739 {
2740 psa_cipher_abort( operation );
2741 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002742 }
2743#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2744
mohammad1603503973b2018-03-12 15:59:30 +02002745 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002746 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2747 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2748 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002749 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002750 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002751 }
mohammad1603503973b2018-03-12 15:59:30 +02002752
Moran Peker395db872018-05-31 14:07:14 +03002753 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002754}
2755
Gilles Peskinefe119512018-07-08 21:39:34 +02002756psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002757 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002758 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002759{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002760 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002761}
2762
Gilles Peskinefe119512018-07-08 21:39:34 +02002763psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002764 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002765 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002766{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002767 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002768}
2769
Gilles Peskinefe119512018-07-08 21:39:34 +02002770psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2771 unsigned char *iv,
2772 size_t iv_size,
2773 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002774{
itayzafrir534bd7c2018-08-02 13:56:32 +03002775 psa_status_t status;
2776 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002777 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002778 {
2779 status = PSA_ERROR_BAD_STATE;
2780 goto exit;
2781 }
Moran Peker41deec42018-04-04 15:43:05 +03002782 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002783 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002784 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002785 goto exit;
2786 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002787 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2788 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002789 if( ret != 0 )
2790 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002791 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002792 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002793 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002794
mohammad16038481e742018-03-18 13:57:31 +02002795 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002796 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002797
Moran Peker395db872018-05-31 14:07:14 +03002798exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002799 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002800 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002801 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002802}
2803
Gilles Peskinefe119512018-07-08 21:39:34 +02002804psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2805 const unsigned char *iv,
2806 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002807{
itayzafrir534bd7c2018-08-02 13:56:32 +03002808 psa_status_t status;
2809 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002810 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002811 {
2812 status = PSA_ERROR_BAD_STATE;
2813 goto exit;
2814 }
Moran Pekera28258c2018-05-29 16:25:04 +03002815 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002816 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002817 status = PSA_ERROR_INVALID_ARGUMENT;
2818 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002819 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002820 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2821 status = mbedtls_to_psa_error( ret );
2822exit:
2823 if( status == PSA_SUCCESS )
2824 operation->iv_set = 1;
2825 else
mohammad1603503973b2018-03-12 15:59:30 +02002826 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002827 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002828}
2829
Gilles Peskinee553c652018-06-04 16:22:46 +02002830psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2831 const uint8_t *input,
2832 size_t input_length,
2833 unsigned char *output,
2834 size_t output_size,
2835 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002836{
itayzafrir534bd7c2018-08-02 13:56:32 +03002837 psa_status_t status;
2838 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002839 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002840 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002841 {
2842 /* Take the unprocessed partial block left over from previous
2843 * update calls, if any, plus the input to this call. Remove
2844 * the last partial block, if any. You get the data that will be
2845 * output in this call. */
2846 expected_output_size =
2847 ( operation->ctx.cipher.unprocessed_len + input_length )
2848 / operation->block_size * operation->block_size;
2849 }
2850 else
2851 {
2852 expected_output_size = input_length;
2853 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002854
Gilles Peskine89d789c2018-06-04 17:17:16 +02002855 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002856 {
2857 status = PSA_ERROR_BUFFER_TOO_SMALL;
2858 goto exit;
2859 }
mohammad160382759612018-03-12 18:16:40 +02002860
mohammad1603503973b2018-03-12 15:59:30 +02002861 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002862 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002863 status = mbedtls_to_psa_error( ret );
2864exit:
2865 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002866 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002867 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002868}
2869
Gilles Peskinee553c652018-06-04 16:22:46 +02002870psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2871 uint8_t *output,
2872 size_t output_size,
2873 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002874{
Janos Follath315b51c2018-07-09 16:04:51 +01002875 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2876 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002877 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002878
mohammad1603503973b2018-03-12 15:59:30 +02002879 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002880 {
Janos Follath315b51c2018-07-09 16:04:51 +01002881 status = PSA_ERROR_BAD_STATE;
2882 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002883 }
2884 if( operation->iv_required && ! operation->iv_set )
2885 {
Janos Follath315b51c2018-07-09 16:04:51 +01002886 status = PSA_ERROR_BAD_STATE;
2887 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002888 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002889
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002890 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002891 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2892 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002893 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002894 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002895 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002896 }
2897
Janos Follath315b51c2018-07-09 16:04:51 +01002898 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2899 temp_output_buffer,
2900 output_length );
2901 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002902 {
Janos Follath315b51c2018-07-09 16:04:51 +01002903 status = mbedtls_to_psa_error( cipher_ret );
2904 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002905 }
Janos Follath315b51c2018-07-09 16:04:51 +01002906
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002907 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002908 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002909 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002910 memcpy( output, temp_output_buffer, *output_length );
2911 else
2912 {
Janos Follath315b51c2018-07-09 16:04:51 +01002913 status = PSA_ERROR_BUFFER_TOO_SMALL;
2914 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002915 }
mohammad1603503973b2018-03-12 15:59:30 +02002916
Gilles Peskine3f108122018-12-07 18:14:53 +01002917 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002918 status = psa_cipher_abort( operation );
2919
2920 return( status );
2921
2922error:
2923
2924 *output_length = 0;
2925
Gilles Peskine3f108122018-12-07 18:14:53 +01002926 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002927 (void) psa_cipher_abort( operation );
2928
2929 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002930}
2931
Gilles Peskinee553c652018-06-04 16:22:46 +02002932psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2933{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002934 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002935 {
2936 /* The object has (apparently) been initialized but it is not
2937 * in use. It's ok to call abort on such an object, and there's
2938 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002939 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002940 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002941
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002942 /* Sanity check (shouldn't happen: operation->alg should
2943 * always have been initialized to a valid value). */
2944 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2945 return( PSA_ERROR_BAD_STATE );
2946
mohammad1603503973b2018-03-12 15:59:30 +02002947 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002948
Moran Peker41deec42018-04-04 15:43:05 +03002949 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002950 operation->key_set = 0;
2951 operation->iv_set = 0;
2952 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002953 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002954 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002955
Moran Peker395db872018-05-31 14:07:14 +03002956 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002957}
2958
Gilles Peskinea0655c32018-04-30 17:06:50 +02002959
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002960
mohammad16038cc1cee2018-03-28 01:21:33 +03002961/****************************************************************/
2962/* Key Policy */
2963/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002964
mohammad160327010052018-07-03 13:16:15 +03002965#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002966void psa_key_policy_set_usage( psa_key_policy_t *policy,
2967 psa_key_usage_t usage,
2968 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002969{
mohammad16034eed7572018-03-28 05:14:59 -07002970 policy->usage = usage;
2971 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002972}
2973
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002974psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002975{
mohammad16036df908f2018-04-02 08:34:15 -07002976 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002977}
2978
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002979psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002980{
mohammad16036df908f2018-04-02 08:34:15 -07002981 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002982}
mohammad160327010052018-07-03 13:16:15 +03002983#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002984
Gilles Peskinec5487a82018-12-03 18:08:14 +01002985psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02002986 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002987{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002988 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002989 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002990
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002991 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002992 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002993
Gilles Peskinec5487a82018-12-03 18:08:14 +01002994 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002995 if( status != PSA_SUCCESS )
2996 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002997
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002998 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2999 PSA_KEY_USAGE_ENCRYPT |
3000 PSA_KEY_USAGE_DECRYPT |
3001 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003002 PSA_KEY_USAGE_VERIFY |
3003 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003004 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003005
mohammad16036df908f2018-04-02 08:34:15 -07003006 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003007
3008 return( PSA_SUCCESS );
3009}
3010
Gilles Peskinec5487a82018-12-03 18:08:14 +01003011psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003012 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003013{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003014 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003015 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003016
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003017 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003018 return( PSA_ERROR_INVALID_ARGUMENT );
3019
Gilles Peskinec5487a82018-12-03 18:08:14 +01003020 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003021 if( status != PSA_SUCCESS )
3022 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003023
mohammad16036df908f2018-04-02 08:34:15 -07003024 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003025
3026 return( PSA_SUCCESS );
3027}
Gilles Peskine20035e32018-02-03 22:44:14 +01003028
Gilles Peskinea0655c32018-04-30 17:06:50 +02003029
3030
mohammad1603804cd712018-03-20 22:44:08 +02003031/****************************************************************/
3032/* Key Lifetime */
3033/****************************************************************/
3034
Gilles Peskinec5487a82018-12-03 18:08:14 +01003035psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003036 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003037{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003038 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003039 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003040
Gilles Peskinec5487a82018-12-03 18:08:14 +01003041 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003042 if( status != PSA_SUCCESS )
3043 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003044
mohammad1603804cd712018-03-20 22:44:08 +02003045 *lifetime = slot->lifetime;
3046
3047 return( PSA_SUCCESS );
3048}
3049
Gilles Peskine20035e32018-02-03 22:44:14 +01003050
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003051
mohammad16035955c982018-04-26 00:53:03 +03003052/****************************************************************/
3053/* AEAD */
3054/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003055
Gilles Peskineedf9a652018-08-17 18:11:56 +02003056typedef struct
3057{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003058 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003059 const mbedtls_cipher_info_t *cipher_info;
3060 union
3061 {
3062#if defined(MBEDTLS_CCM_C)
3063 mbedtls_ccm_context ccm;
3064#endif /* MBEDTLS_CCM_C */
3065#if defined(MBEDTLS_GCM_C)
3066 mbedtls_gcm_context gcm;
3067#endif /* MBEDTLS_GCM_C */
3068 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003069 psa_algorithm_t core_alg;
3070 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003071 uint8_t tag_length;
3072} aead_operation_t;
3073
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003074static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003075{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003076 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003077 {
3078#if defined(MBEDTLS_CCM_C)
3079 case PSA_ALG_CCM:
3080 mbedtls_ccm_free( &operation->ctx.ccm );
3081 break;
3082#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003083#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003084 case PSA_ALG_GCM:
3085 mbedtls_gcm_free( &operation->ctx.gcm );
3086 break;
3087#endif /* MBEDTLS_GCM_C */
3088 }
3089}
3090
3091static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003092 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003093 psa_key_usage_t usage,
3094 psa_algorithm_t alg )
3095{
3096 psa_status_t status;
3097 size_t key_bits;
3098 mbedtls_cipher_id_t cipher_id;
3099
Gilles Peskinec5487a82018-12-03 18:08:14 +01003100 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003101 if( status != PSA_SUCCESS )
3102 return( status );
3103
3104 key_bits = psa_get_key_bits( operation->slot );
3105
3106 operation->cipher_info =
3107 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3108 &cipher_id );
3109 if( operation->cipher_info == NULL )
3110 return( PSA_ERROR_NOT_SUPPORTED );
3111
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003112 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003113 {
3114#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003115 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3116 operation->core_alg = PSA_ALG_CCM;
3117 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003118 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3119 return( PSA_ERROR_INVALID_ARGUMENT );
3120 mbedtls_ccm_init( &operation->ctx.ccm );
3121 status = mbedtls_to_psa_error(
3122 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3123 operation->slot->data.raw.data,
3124 (unsigned int) key_bits ) );
3125 if( status != 0 )
3126 goto cleanup;
3127 break;
3128#endif /* MBEDTLS_CCM_C */
3129
3130#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003131 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3132 operation->core_alg = PSA_ALG_GCM;
3133 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003134 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3135 return( PSA_ERROR_INVALID_ARGUMENT );
3136 mbedtls_gcm_init( &operation->ctx.gcm );
3137 status = mbedtls_to_psa_error(
3138 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3139 operation->slot->data.raw.data,
3140 (unsigned int) key_bits ) );
3141 break;
3142#endif /* MBEDTLS_GCM_C */
3143
3144 default:
3145 return( PSA_ERROR_NOT_SUPPORTED );
3146 }
3147
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003148 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3149 {
3150 status = PSA_ERROR_INVALID_ARGUMENT;
3151 goto cleanup;
3152 }
3153 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3154 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3155 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3156 * In both cases, mbedtls_xxx will validate the tag length below. */
3157
Gilles Peskineedf9a652018-08-17 18:11:56 +02003158 return( PSA_SUCCESS );
3159
3160cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003161 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003162 return( status );
3163}
3164
Gilles Peskinec5487a82018-12-03 18:08:14 +01003165psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003166 psa_algorithm_t alg,
3167 const uint8_t *nonce,
3168 size_t nonce_length,
3169 const uint8_t *additional_data,
3170 size_t additional_data_length,
3171 const uint8_t *plaintext,
3172 size_t plaintext_length,
3173 uint8_t *ciphertext,
3174 size_t ciphertext_size,
3175 size_t *ciphertext_length )
3176{
mohammad16035955c982018-04-26 00:53:03 +03003177 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003178 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003179 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003180
mohammad1603f08a5502018-06-03 15:05:47 +03003181 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003182
Gilles Peskinec5487a82018-12-03 18:08:14 +01003183 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003184 if( status != PSA_SUCCESS )
3185 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003186
Gilles Peskineedf9a652018-08-17 18:11:56 +02003187 /* For all currently supported modes, the tag is at the end of the
3188 * ciphertext. */
3189 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3190 {
3191 status = PSA_ERROR_BUFFER_TOO_SMALL;
3192 goto exit;
3193 }
3194 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003195
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003196#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003197 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003198 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003199 status = mbedtls_to_psa_error(
3200 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3201 MBEDTLS_GCM_ENCRYPT,
3202 plaintext_length,
3203 nonce, nonce_length,
3204 additional_data, additional_data_length,
3205 plaintext, ciphertext,
3206 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003207 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003208 else
3209#endif /* MBEDTLS_GCM_C */
3210#if defined(MBEDTLS_CCM_C)
3211 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003212 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003213 status = mbedtls_to_psa_error(
3214 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3215 plaintext_length,
3216 nonce, nonce_length,
3217 additional_data,
3218 additional_data_length,
3219 plaintext, ciphertext,
3220 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003221 }
mohammad16035c8845f2018-05-09 05:40:09 -07003222 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003223#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003224 {
mohammad1603554faad2018-06-03 15:07:38 +03003225 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003226 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003227
Gilles Peskineedf9a652018-08-17 18:11:56 +02003228 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3229 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003230
Gilles Peskineedf9a652018-08-17 18:11:56 +02003231exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003232 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003233 if( status == PSA_SUCCESS )
3234 *ciphertext_length = plaintext_length + operation.tag_length;
3235 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003236}
3237
Gilles Peskineee652a32018-06-01 19:23:52 +02003238/* Locate the tag in a ciphertext buffer containing the encrypted data
3239 * followed by the tag. Return the length of the part preceding the tag in
3240 * *plaintext_length. This is the size of the plaintext in modes where
3241 * the encrypted data has the same size as the plaintext, such as
3242 * CCM and GCM. */
3243static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3244 const uint8_t *ciphertext,
3245 size_t ciphertext_length,
3246 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003247 const uint8_t **p_tag )
3248{
3249 size_t payload_length;
3250 if( tag_length > ciphertext_length )
3251 return( PSA_ERROR_INVALID_ARGUMENT );
3252 payload_length = ciphertext_length - tag_length;
3253 if( payload_length > plaintext_size )
3254 return( PSA_ERROR_BUFFER_TOO_SMALL );
3255 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003256 return( PSA_SUCCESS );
3257}
3258
Gilles Peskinec5487a82018-12-03 18:08:14 +01003259psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003260 psa_algorithm_t alg,
3261 const uint8_t *nonce,
3262 size_t nonce_length,
3263 const uint8_t *additional_data,
3264 size_t additional_data_length,
3265 const uint8_t *ciphertext,
3266 size_t ciphertext_length,
3267 uint8_t *plaintext,
3268 size_t plaintext_size,
3269 size_t *plaintext_length )
3270{
mohammad16035955c982018-04-26 00:53:03 +03003271 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003272 aead_operation_t operation;
3273 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003274
Gilles Peskineee652a32018-06-01 19:23:52 +02003275 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003276
Gilles Peskinec5487a82018-12-03 18:08:14 +01003277 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003278 if( status != PSA_SUCCESS )
3279 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003280
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003281#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003282 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003283 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003284 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003285 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003286 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003287 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003288 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003289
Gilles Peskineedf9a652018-08-17 18:11:56 +02003290 status = mbedtls_to_psa_error(
3291 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3292 ciphertext_length - operation.tag_length,
3293 nonce, nonce_length,
3294 additional_data,
3295 additional_data_length,
3296 tag, operation.tag_length,
3297 ciphertext, plaintext ) );
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 = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003305 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003306 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003307 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003308 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003309
Gilles Peskineedf9a652018-08-17 18:11:56 +02003310 status = mbedtls_to_psa_error(
3311 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3312 ciphertext_length - operation.tag_length,
3313 nonce, nonce_length,
3314 additional_data,
3315 additional_data_length,
3316 ciphertext, plaintext,
3317 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003318 }
mohammad160339574652018-06-01 04:39:53 -07003319 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003320#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003321 {
mohammad1603554faad2018-06-03 15:07:38 +03003322 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003323 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003324
Gilles Peskineedf9a652018-08-17 18:11:56 +02003325 if( status != PSA_SUCCESS && plaintext_size != 0 )
3326 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003327
Gilles Peskineedf9a652018-08-17 18:11:56 +02003328exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003329 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003330 if( status == PSA_SUCCESS )
3331 *plaintext_length = ciphertext_length - operation.tag_length;
3332 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003333}
3334
Gilles Peskinea0655c32018-04-30 17:06:50 +02003335
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003336
Gilles Peskine20035e32018-02-03 22:44:14 +01003337/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003338/* Generators */
3339/****************************************************************/
3340
3341psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3342{
3343 psa_status_t status = PSA_SUCCESS;
3344 if( generator->alg == 0 )
3345 {
3346 /* The object has (apparently) been initialized but it is not
3347 * in use. It's ok to call abort on such an object, and there's
3348 * nothing to do. */
3349 }
3350 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003351 if( generator->alg == PSA_ALG_SELECT_RAW )
3352 {
3353 if( generator->ctx.buffer.data != NULL )
3354 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003355 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003356 generator->ctx.buffer.size );
3357 mbedtls_free( generator->ctx.buffer.data );
3358 }
3359 }
3360 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003361#if defined(MBEDTLS_MD_C)
3362 if( PSA_ALG_IS_HKDF( generator->alg ) )
3363 {
3364 mbedtls_free( generator->ctx.hkdf.info );
3365 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3366 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003367 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3368 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3369 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003370 {
3371 if( generator->ctx.tls12_prf.key != NULL )
3372 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003373 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003374 generator->ctx.tls12_prf.key_len );
3375 mbedtls_free( generator->ctx.tls12_prf.key );
3376 }
Hanno Becker580fba12018-11-13 20:50:45 +00003377
3378 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3379 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003380 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003381 generator->ctx.tls12_prf.Ai_with_seed_len );
3382 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3383 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003384 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003385 else
3386#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003387 {
3388 status = PSA_ERROR_BAD_STATE;
3389 }
3390 memset( generator, 0, sizeof( *generator ) );
3391 return( status );
3392}
3393
3394
3395psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3396 size_t *capacity)
3397{
3398 *capacity = generator->capacity;
3399 return( PSA_SUCCESS );
3400}
3401
Gilles Peskinebef7f142018-07-12 17:22:21 +02003402#if defined(MBEDTLS_MD_C)
3403/* Read some bytes from an HKDF-based generator. This performs a chunk
3404 * of the expand phase of the HKDF algorithm. */
3405static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3406 psa_algorithm_t hash_alg,
3407 uint8_t *output,
3408 size_t output_length )
3409{
3410 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3411 psa_status_t status;
3412
3413 while( output_length != 0 )
3414 {
3415 /* Copy what remains of the current block */
3416 uint8_t n = hash_length - hkdf->offset_in_block;
3417 if( n > output_length )
3418 n = (uint8_t) output_length;
3419 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3420 output += n;
3421 output_length -= n;
3422 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003423 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003424 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003425 /* We can't be wanting more output after block 0xff, otherwise
3426 * the capacity check in psa_generator_read() would have
3427 * prevented this call. It could happen only if the generator
3428 * object was corrupted or if this function is called directly
3429 * inside the library. */
3430 if( hkdf->block_number == 0xff )
3431 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003432
3433 /* We need a new block */
3434 ++hkdf->block_number;
3435 hkdf->offset_in_block = 0;
3436 status = psa_hmac_setup_internal( &hkdf->hmac,
3437 hkdf->prk, hash_length,
3438 hash_alg );
3439 if( status != PSA_SUCCESS )
3440 return( status );
3441 if( hkdf->block_number != 1 )
3442 {
3443 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3444 hkdf->output_block,
3445 hash_length );
3446 if( status != PSA_SUCCESS )
3447 return( status );
3448 }
3449 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3450 hkdf->info,
3451 hkdf->info_length );
3452 if( status != PSA_SUCCESS )
3453 return( status );
3454 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3455 &hkdf->block_number, 1 );
3456 if( status != PSA_SUCCESS )
3457 return( status );
3458 status = psa_hmac_finish_internal( &hkdf->hmac,
3459 hkdf->output_block,
3460 sizeof( hkdf->output_block ) );
3461 if( status != PSA_SUCCESS )
3462 return( status );
3463 }
3464
3465 return( PSA_SUCCESS );
3466}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003467
3468static psa_status_t psa_generator_tls12_prf_generate_next_block(
3469 psa_tls12_prf_generator_t *tls12_prf,
3470 psa_algorithm_t alg )
3471{
3472 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3473 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3474 psa_hmac_internal_data hmac;
3475 psa_status_t status, cleanup_status;
3476
Hanno Becker3b339e22018-11-13 20:56:14 +00003477 unsigned char *Ai;
3478 size_t Ai_len;
3479
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003480 /* We can't be wanting more output after block 0xff, otherwise
3481 * the capacity check in psa_generator_read() would have
3482 * prevented this call. It could happen only if the generator
3483 * object was corrupted or if this function is called directly
3484 * inside the library. */
3485 if( tls12_prf->block_number == 0xff )
3486 return( PSA_ERROR_BAD_STATE );
3487
3488 /* We need a new block */
3489 ++tls12_prf->block_number;
3490 tls12_prf->offset_in_block = 0;
3491
3492 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3493 *
3494 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3495 *
3496 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3497 * HMAC_hash(secret, A(2) + seed) +
3498 * HMAC_hash(secret, A(3) + seed) + ...
3499 *
3500 * A(0) = seed
3501 * A(i) = HMAC_hash( secret, A(i-1) )
3502 *
3503 * The `psa_tls12_prf_generator` structures saves the block
3504 * `HMAC_hash(secret, A(i) + seed)` from which the output
3505 * is currently extracted as `output_block`, while
3506 * `A(i) + seed` is stored in `Ai_with_seed`.
3507 *
3508 * Generating a new block means recalculating `Ai_with_seed`
3509 * from the A(i)-part of it, and afterwards recalculating
3510 * `output_block`.
3511 *
3512 * A(0) is computed at setup time.
3513 *
3514 */
3515
3516 psa_hmac_init_internal( &hmac );
3517
3518 /* We must distinguish the calculation of A(1) from those
3519 * of A(2) and higher, because A(0)=seed has a different
3520 * length than the other A(i). */
3521 if( tls12_prf->block_number == 1 )
3522 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003523 Ai = tls12_prf->Ai_with_seed + hash_length;
3524 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003525 }
3526 else
3527 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003528 Ai = tls12_prf->Ai_with_seed;
3529 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003530 }
3531
Hanno Becker3b339e22018-11-13 20:56:14 +00003532 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3533 status = psa_hmac_setup_internal( &hmac,
3534 tls12_prf->key,
3535 tls12_prf->key_len,
3536 hash_alg );
3537 if( status != PSA_SUCCESS )
3538 goto cleanup;
3539
3540 status = psa_hash_update( &hmac.hash_ctx,
3541 Ai, Ai_len );
3542 if( status != PSA_SUCCESS )
3543 goto cleanup;
3544
3545 status = psa_hmac_finish_internal( &hmac,
3546 tls12_prf->Ai_with_seed,
3547 hash_length );
3548 if( status != PSA_SUCCESS )
3549 goto cleanup;
3550
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003551 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3552 status = psa_hmac_setup_internal( &hmac,
3553 tls12_prf->key,
3554 tls12_prf->key_len,
3555 hash_alg );
3556 if( status != PSA_SUCCESS )
3557 goto cleanup;
3558
3559 status = psa_hash_update( &hmac.hash_ctx,
3560 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003561 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003562 if( status != PSA_SUCCESS )
3563 goto cleanup;
3564
3565 status = psa_hmac_finish_internal( &hmac,
3566 tls12_prf->output_block,
3567 hash_length );
3568 if( status != PSA_SUCCESS )
3569 goto cleanup;
3570
3571cleanup:
3572
3573 cleanup_status = psa_hmac_abort_internal( &hmac );
3574 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3575 status = cleanup_status;
3576
3577 return( status );
3578}
3579
3580/* Read some bytes from an TLS-1.2-PRF-based generator.
3581 * See Section 5 of RFC 5246. */
3582static psa_status_t psa_generator_tls12_prf_read(
3583 psa_tls12_prf_generator_t *tls12_prf,
3584 psa_algorithm_t alg,
3585 uint8_t *output,
3586 size_t output_length )
3587{
3588 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3589 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3590 psa_status_t status;
3591
3592 while( output_length != 0 )
3593 {
3594 /* Copy what remains of the current block */
3595 uint8_t n = hash_length - tls12_prf->offset_in_block;
3596
3597 /* Check if we have fully processed the current block. */
3598 if( n == 0 )
3599 {
3600 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3601 alg );
3602 if( status != PSA_SUCCESS )
3603 return( status );
3604
3605 continue;
3606 }
3607
3608 if( n > output_length )
3609 n = (uint8_t) output_length;
3610 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3611 n );
3612 output += n;
3613 output_length -= n;
3614 tls12_prf->offset_in_block += n;
3615 }
3616
3617 return( PSA_SUCCESS );
3618}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003619#endif /* MBEDTLS_MD_C */
3620
Gilles Peskineeab56e42018-07-12 17:12:33 +02003621psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3622 uint8_t *output,
3623 size_t output_length )
3624{
3625 psa_status_t status;
3626
3627 if( output_length > generator->capacity )
3628 {
3629 generator->capacity = 0;
3630 /* Go through the error path to wipe all confidential data now
3631 * that the generator object is useless. */
3632 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3633 goto exit;
3634 }
3635 if( output_length == 0 &&
3636 generator->capacity == 0 && generator->alg == 0 )
3637 {
3638 /* Edge case: this is a blank or finished generator, and 0
3639 * bytes were requested. The right error in this case could
3640 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3641 * INSUFFICIENT_CAPACITY, which is right for a finished
3642 * generator, for consistency with the case when
3643 * output_length > 0. */
3644 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3645 }
3646 generator->capacity -= output_length;
3647
Gilles Peskine751d9652018-09-18 12:05:44 +02003648 if( generator->alg == PSA_ALG_SELECT_RAW )
3649 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003650 /* Initially, the capacity of a selection generator is always
3651 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3652 * abbreviated in this comment as `size`. When the remaining
3653 * capacity is `c`, the next bytes to serve start `c` bytes
3654 * from the end of the buffer, i.e. `size - c` from the
3655 * beginning of the buffer. Since `generator->capacity` was just
3656 * decremented above, we need to serve the bytes from
3657 * `size - generator->capacity - output_length` to
3658 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003659 size_t offset =
3660 generator->ctx.buffer.size - generator->capacity - output_length;
3661 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3662 status = PSA_SUCCESS;
3663 }
3664 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003665#if defined(MBEDTLS_MD_C)
3666 if( PSA_ALG_IS_HKDF( generator->alg ) )
3667 {
3668 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3669 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3670 output, output_length );
3671 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003672 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3673 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003674 {
3675 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3676 generator->alg, output,
3677 output_length );
3678 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003679 else
3680#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003681 {
3682 return( PSA_ERROR_BAD_STATE );
3683 }
3684
3685exit:
3686 if( status != PSA_SUCCESS )
3687 {
3688 psa_generator_abort( generator );
3689 memset( output, '!', output_length );
3690 }
3691 return( status );
3692}
3693
Gilles Peskine08542d82018-07-19 17:05:42 +02003694#if defined(MBEDTLS_DES_C)
3695static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3696{
3697 if( data_size >= 8 )
3698 mbedtls_des_key_set_parity( data );
3699 if( data_size >= 16 )
3700 mbedtls_des_key_set_parity( data + 8 );
3701 if( data_size >= 24 )
3702 mbedtls_des_key_set_parity( data + 16 );
3703}
3704#endif /* MBEDTLS_DES_C */
3705
Gilles Peskinec5487a82018-12-03 18:08:14 +01003706psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003707 psa_key_type_t type,
3708 size_t bits,
3709 psa_crypto_generator_t *generator )
3710{
3711 uint8_t *data = NULL;
3712 size_t bytes = PSA_BITS_TO_BYTES( bits );
3713 psa_status_t status;
3714
3715 if( ! key_type_is_raw_bytes( type ) )
3716 return( PSA_ERROR_INVALID_ARGUMENT );
3717 if( bits % 8 != 0 )
3718 return( PSA_ERROR_INVALID_ARGUMENT );
3719 data = mbedtls_calloc( 1, bytes );
3720 if( data == NULL )
3721 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3722
3723 status = psa_generator_read( generator, data, bytes );
3724 if( status != PSA_SUCCESS )
3725 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003726#if defined(MBEDTLS_DES_C)
3727 if( type == PSA_KEY_TYPE_DES )
3728 psa_des_set_key_parity( data, bytes );
3729#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003730 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003731
3732exit:
3733 mbedtls_free( data );
3734 return( status );
3735}
3736
3737
3738
3739/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003740/* Key derivation */
3741/****************************************************************/
3742
Gilles Peskinea05219c2018-11-16 16:02:56 +01003743#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003744/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003745 * of the HKDF algorithm.
3746 *
3747 * Note that if this function fails, you must call psa_generator_abort()
3748 * to potentially free embedded data structures and wipe confidential data.
3749 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003750static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003751 const uint8_t *secret,
3752 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003753 psa_algorithm_t hash_alg,
3754 const uint8_t *salt,
3755 size_t salt_length,
3756 const uint8_t *label,
3757 size_t label_length )
3758{
3759 psa_status_t status;
3760 status = psa_hmac_setup_internal( &hkdf->hmac,
3761 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003762 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003763 if( status != PSA_SUCCESS )
3764 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003765 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003766 if( status != PSA_SUCCESS )
3767 return( status );
3768 status = psa_hmac_finish_internal( &hkdf->hmac,
3769 hkdf->prk,
3770 sizeof( hkdf->prk ) );
3771 if( status != PSA_SUCCESS )
3772 return( status );
3773 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3774 hkdf->block_number = 0;
3775 hkdf->info_length = label_length;
3776 if( label_length != 0 )
3777 {
3778 hkdf->info = mbedtls_calloc( 1, label_length );
3779 if( hkdf->info == NULL )
3780 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3781 memcpy( hkdf->info, label, label_length );
3782 }
3783 return( PSA_SUCCESS );
3784}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003785#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003786
Gilles Peskinea05219c2018-11-16 16:02:56 +01003787#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003788/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3789 *
3790 * Note that if this function fails, you must call psa_generator_abort()
3791 * to potentially free embedded data structures and wipe confidential data.
3792 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003793static psa_status_t psa_generator_tls12_prf_setup(
3794 psa_tls12_prf_generator_t *tls12_prf,
3795 const unsigned char *key,
3796 size_t key_len,
3797 psa_algorithm_t hash_alg,
3798 const uint8_t *salt,
3799 size_t salt_length,
3800 const uint8_t *label,
3801 size_t label_length )
3802{
3803 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003804 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3805 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003806
3807 tls12_prf->key = mbedtls_calloc( 1, key_len );
3808 if( tls12_prf->key == NULL )
3809 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3810 tls12_prf->key_len = key_len;
3811 memcpy( tls12_prf->key, key, key_len );
3812
Hanno Becker580fba12018-11-13 20:50:45 +00003813 overflow = ( salt_length + label_length < salt_length ) ||
3814 ( salt_length + label_length + hash_length < hash_length );
3815 if( overflow )
3816 return( PSA_ERROR_INVALID_ARGUMENT );
3817
3818 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3819 if( tls12_prf->Ai_with_seed == NULL )
3820 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3821 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3822
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003823 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3824 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003825 if( label_length != 0 )
3826 {
3827 memcpy( tls12_prf->Ai_with_seed + hash_length,
3828 label, label_length );
3829 }
3830
3831 if( salt_length != 0 )
3832 {
3833 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3834 salt, salt_length );
3835 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003836
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003837 /* The first block gets generated when
3838 * psa_generator_read() is called. */
3839 tls12_prf->block_number = 0;
3840 tls12_prf->offset_in_block = hash_length;
3841
3842 return( PSA_SUCCESS );
3843}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003844
3845/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3846static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3847 psa_tls12_prf_generator_t *tls12_prf,
3848 const unsigned char *psk,
3849 size_t psk_len,
3850 psa_algorithm_t hash_alg,
3851 const uint8_t *salt,
3852 size_t salt_length,
3853 const uint8_t *label,
3854 size_t label_length )
3855{
3856 psa_status_t status;
3857 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3858
3859 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3860 return( PSA_ERROR_INVALID_ARGUMENT );
3861
3862 /* Quoting RFC 4279, Section 2:
3863 *
3864 * The premaster secret is formed as follows: if the PSK is N octets
3865 * long, concatenate a uint16 with the value N, N zero octets, a second
3866 * uint16 with the value N, and the PSK itself.
3867 */
3868
3869 pms[0] = ( psk_len >> 8 ) & 0xff;
3870 pms[1] = ( psk_len >> 0 ) & 0xff;
3871 memset( pms + 2, 0, psk_len );
3872 pms[2 + psk_len + 0] = pms[0];
3873 pms[2 + psk_len + 1] = pms[1];
3874 memcpy( pms + 4 + psk_len, psk, psk_len );
3875
3876 status = psa_generator_tls12_prf_setup( tls12_prf,
3877 pms, 4 + 2 * psk_len,
3878 hash_alg,
3879 salt, salt_length,
3880 label, label_length );
3881
Gilles Peskine3f108122018-12-07 18:14:53 +01003882 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00003883 return( status );
3884}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003885#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003886
Gilles Peskine346797d2018-11-16 16:05:06 +01003887/* Note that if this function fails, you must call psa_generator_abort()
3888 * to potentially free embedded data structures and wipe confidential data.
3889 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003890static psa_status_t psa_key_derivation_internal(
3891 psa_crypto_generator_t *generator,
3892 const uint8_t *secret, size_t secret_length,
3893 psa_algorithm_t alg,
3894 const uint8_t *salt, size_t salt_length,
3895 const uint8_t *label, size_t label_length,
3896 size_t capacity )
3897{
3898 psa_status_t status;
3899 size_t max_capacity;
3900
3901 /* Set generator->alg even on failure so that abort knows what to do. */
3902 generator->alg = alg;
3903
Gilles Peskine751d9652018-09-18 12:05:44 +02003904 if( alg == PSA_ALG_SELECT_RAW )
3905 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003906 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003907 if( salt_length != 0 )
3908 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003909 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003910 if( label_length != 0 )
3911 return( PSA_ERROR_INVALID_ARGUMENT );
3912 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3913 if( generator->ctx.buffer.data == NULL )
3914 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3915 memcpy( generator->ctx.buffer.data, secret, secret_length );
3916 generator->ctx.buffer.size = secret_length;
3917 max_capacity = secret_length;
3918 status = PSA_SUCCESS;
3919 }
3920 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003921#if defined(MBEDTLS_MD_C)
3922 if( PSA_ALG_IS_HKDF( alg ) )
3923 {
3924 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3925 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3926 if( hash_size == 0 )
3927 return( PSA_ERROR_NOT_SUPPORTED );
3928 max_capacity = 255 * hash_size;
3929 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3930 secret, secret_length,
3931 hash_alg,
3932 salt, salt_length,
3933 label, label_length );
3934 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003935 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
3936 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
3937 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003938 {
3939 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3940 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3941
3942 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
3943 if( hash_alg != PSA_ALG_SHA_256 &&
3944 hash_alg != PSA_ALG_SHA_384 )
3945 {
3946 return( PSA_ERROR_NOT_SUPPORTED );
3947 }
3948
3949 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00003950
3951 if( PSA_ALG_IS_TLS12_PRF( alg ) )
3952 {
3953 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
3954 secret, secret_length,
3955 hash_alg, salt, salt_length,
3956 label, label_length );
3957 }
3958 else
3959 {
3960 status = psa_generator_tls12_psk_to_ms_setup(
3961 &generator->ctx.tls12_prf,
3962 secret, secret_length,
3963 hash_alg, salt, salt_length,
3964 label, label_length );
3965 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003966 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003967 else
3968#endif
3969 {
3970 return( PSA_ERROR_NOT_SUPPORTED );
3971 }
3972
3973 if( status != PSA_SUCCESS )
3974 return( status );
3975
3976 if( capacity <= max_capacity )
3977 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003978 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
3979 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003980 else
3981 return( PSA_ERROR_INVALID_ARGUMENT );
3982
3983 return( PSA_SUCCESS );
3984}
3985
Gilles Peskineea0fb492018-07-12 17:17:20 +02003986psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003987 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003988 psa_algorithm_t alg,
3989 const uint8_t *salt,
3990 size_t salt_length,
3991 const uint8_t *label,
3992 size_t label_length,
3993 size_t capacity )
3994{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003995 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003996 psa_status_t status;
3997
3998 if( generator->alg != 0 )
3999 return( PSA_ERROR_BAD_STATE );
4000
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004001 /* Make sure that alg is a key derivation algorithm. This prevents
4002 * key selection algorithms, which psa_key_derivation_internal
4003 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004004 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4005 return( PSA_ERROR_INVALID_ARGUMENT );
4006
Gilles Peskinec5487a82018-12-03 18:08:14 +01004007 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004008 if( status != PSA_SUCCESS )
4009 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004010
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004011 if( slot->type != PSA_KEY_TYPE_DERIVE )
4012 return( PSA_ERROR_INVALID_ARGUMENT );
4013
4014 status = psa_key_derivation_internal( generator,
4015 slot->data.raw.data,
4016 slot->data.raw.bytes,
4017 alg,
4018 salt, salt_length,
4019 label, label_length,
4020 capacity );
4021 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004022 psa_generator_abort( generator );
4023 return( status );
4024}
4025
4026
4027
4028/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004029/* Key agreement */
4030/****************************************************************/
4031
Gilles Peskinea05219c2018-11-16 16:02:56 +01004032#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004033static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4034 size_t peer_key_length,
4035 const mbedtls_ecp_keypair *our_key,
4036 uint8_t *shared_secret,
4037 size_t shared_secret_size,
4038 size_t *shared_secret_length )
4039{
4040 mbedtls_pk_context pk;
4041 mbedtls_ecp_keypair *their_key = NULL;
4042 mbedtls_ecdh_context ecdh;
4043 int ret;
4044 mbedtls_ecdh_init( &ecdh );
4045 mbedtls_pk_init( &pk );
4046
4047 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4048 if( ret != 0 )
4049 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004050 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004051 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004052 case MBEDTLS_PK_ECKEY:
4053 case MBEDTLS_PK_ECKEY_DH:
4054 break;
4055 default:
4056 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4057 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004058 }
4059 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004060 if( their_key->grp.id != our_key->grp.id )
4061 {
4062 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4063 goto exit;
4064 }
4065
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004066 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4067 if( ret != 0 )
4068 goto exit;
4069 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4070 if( ret != 0 )
4071 goto exit;
4072
4073 ret = mbedtls_ecdh_calc_secret( &ecdh,
4074 shared_secret_length,
4075 shared_secret, shared_secret_size,
4076 mbedtls_ctr_drbg_random,
4077 &global_data.ctr_drbg );
4078
4079exit:
4080 mbedtls_pk_free( &pk );
4081 mbedtls_ecdh_free( &ecdh );
4082 return( mbedtls_to_psa_error( ret ) );
4083}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004084#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004085
Gilles Peskine01d718c2018-09-18 12:01:02 +02004086#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4087
Gilles Peskine346797d2018-11-16 16:05:06 +01004088/* Note that if this function fails, you must call psa_generator_abort()
4089 * to potentially free embedded data structures and wipe confidential data.
4090 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004091static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004092 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004093 const uint8_t *peer_key,
4094 size_t peer_key_length,
4095 psa_algorithm_t alg )
4096{
4097 psa_status_t status;
4098 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4099 size_t shared_secret_length = 0;
4100
4101 /* Step 1: run the secret agreement algorithm to generate the shared
4102 * secret. */
4103 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4104 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004105#if defined(MBEDTLS_ECDH_C)
4106 case PSA_ALG_ECDH_BASE:
4107 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4108 return( PSA_ERROR_INVALID_ARGUMENT );
4109 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4110 private_key->data.ecp,
4111 shared_secret,
4112 sizeof( shared_secret ),
4113 &shared_secret_length );
4114 break;
4115#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004116 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004117 (void) private_key;
4118 (void) peer_key;
4119 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004120 return( PSA_ERROR_NOT_SUPPORTED );
4121 }
4122 if( status != PSA_SUCCESS )
4123 goto exit;
4124
4125 /* Step 2: set up the key derivation to generate key material from
4126 * the shared secret. */
4127 status = psa_key_derivation_internal( generator,
4128 shared_secret, shared_secret_length,
4129 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4130 NULL, 0, NULL, 0,
4131 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4132exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004133 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004134 return( status );
4135}
4136
4137psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004138 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004139 const uint8_t *peer_key,
4140 size_t peer_key_length,
4141 psa_algorithm_t alg )
4142{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004143 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004144 psa_status_t status;
4145 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4146 return( PSA_ERROR_INVALID_ARGUMENT );
4147 status = psa_get_key_from_slot( private_key, &slot,
4148 PSA_KEY_USAGE_DERIVE, alg );
4149 if( status != PSA_SUCCESS )
4150 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004151 status = psa_key_agreement_internal( generator,
4152 slot,
4153 peer_key, peer_key_length,
4154 alg );
4155 if( status != PSA_SUCCESS )
4156 psa_generator_abort( generator );
4157 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004158}
4159
4160
4161
4162/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004163/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004164/****************************************************************/
4165
4166psa_status_t psa_generate_random( uint8_t *output,
4167 size_t output_size )
4168{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004169 int ret;
4170 GUARD_MODULE_INITIALIZED;
4171
4172 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004173 return( mbedtls_to_psa_error( ret ) );
4174}
4175
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004176#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004177
4178/* Support function for error conversion between psa_its error codes to psa crypto */
4179static psa_status_t its_to_psa_error( psa_its_status_t ret )
4180{
4181 switch( ret )
4182 {
4183 case PSA_ITS_SUCCESS:
4184 return( PSA_SUCCESS );
4185
4186 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4187 return( PSA_ERROR_EMPTY_SLOT );
4188
4189 case PSA_ITS_ERROR_STORAGE_FAILURE:
4190 return( PSA_ERROR_STORAGE_FAILURE );
4191
4192 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4193 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4194
4195 case PSA_ITS_ERROR_INVALID_KEY:
Jaeden Amero58600552018-11-30 12:04:38 +00004196 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004197 case PSA_ITS_ERROR_INCORRECT_SIZE:
4198 case PSA_ITS_ERROR_BAD_POINTER:
4199 return( PSA_ERROR_INVALID_ARGUMENT );
4200
4201 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4202 return( PSA_ERROR_NOT_SUPPORTED );
4203
4204 case PSA_ITS_ERROR_WRITE_ONCE:
4205 return( PSA_ERROR_OCCUPIED_SLOT );
4206
4207 default:
4208 return( PSA_ERROR_UNKNOWN_ERROR );
4209 }
4210}
4211
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004212psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4213 size_t seed_size )
4214{
4215 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004216 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004217 struct psa_its_info_t p_info;
4218 if( global_data.initialized )
4219 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004220
4221 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4222 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4223 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4224 return( PSA_ERROR_INVALID_ARGUMENT );
4225
avolinski0d2c2662018-11-21 17:31:07 +02004226 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004227 status = its_to_psa_error( its_status );
4228
4229 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004230 {
avolinski0d2c2662018-11-21 17:31:07 +02004231 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004232 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004233 }
avolinski13beb102018-11-20 16:51:49 +02004234 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004235 {
4236 /* You should not be here. Seed needs to be injected only once */
4237 status = PSA_ERROR_NOT_PERMITTED;
4238 }
4239 return( status );
4240}
4241#endif
4242
Gilles Peskinec5487a82018-12-03 18:08:14 +01004243psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004244 psa_key_type_t type,
4245 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004246 const void *extra,
4247 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004248{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004249 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004250 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004251
Gilles Peskine53d991e2018-07-12 01:14:59 +02004252 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004253 return( PSA_ERROR_INVALID_ARGUMENT );
4254
Gilles Peskinec5487a82018-12-03 18:08:14 +01004255 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004256 if( status != PSA_SUCCESS )
4257 return( status );
4258
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004259 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004260 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004261 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004262 if( status != PSA_SUCCESS )
4263 return( status );
4264 status = psa_generate_random( slot->data.raw.data,
4265 slot->data.raw.bytes );
4266 if( status != PSA_SUCCESS )
4267 {
4268 mbedtls_free( slot->data.raw.data );
4269 return( status );
4270 }
4271#if defined(MBEDTLS_DES_C)
4272 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004273 psa_des_set_key_parity( slot->data.raw.data,
4274 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004275#endif /* MBEDTLS_DES_C */
4276 }
4277 else
4278
4279#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4280 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4281 {
4282 mbedtls_rsa_context *rsa;
4283 int ret;
4284 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004285 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4286 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004287 /* Accept only byte-aligned keys, for the same reasons as
4288 * in psa_import_rsa_key(). */
4289 if( bits % 8 != 0 )
4290 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004291 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004292 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004293 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004294 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004295 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004296#if INT_MAX < 0xffffffff
4297 /* Check that the uint32_t value passed by the caller fits
4298 * in the range supported by this implementation. */
4299 if( p->e > INT_MAX )
4300 return( PSA_ERROR_NOT_SUPPORTED );
4301#endif
4302 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004303 }
4304 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4305 if( rsa == NULL )
4306 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4307 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4308 ret = mbedtls_rsa_gen_key( rsa,
4309 mbedtls_ctr_drbg_random,
4310 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004311 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004312 exponent );
4313 if( ret != 0 )
4314 {
4315 mbedtls_rsa_free( rsa );
4316 mbedtls_free( rsa );
4317 return( mbedtls_to_psa_error( ret ) );
4318 }
4319 slot->data.rsa = rsa;
4320 }
4321 else
4322#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4323
4324#if defined(MBEDTLS_ECP_C)
4325 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4326 {
4327 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4328 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4329 const mbedtls_ecp_curve_info *curve_info =
4330 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4331 mbedtls_ecp_keypair *ecp;
4332 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004333 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004334 return( PSA_ERROR_NOT_SUPPORTED );
4335 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4336 return( PSA_ERROR_NOT_SUPPORTED );
4337 if( curve_info->bit_size != bits )
4338 return( PSA_ERROR_INVALID_ARGUMENT );
4339 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4340 if( ecp == NULL )
4341 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4342 mbedtls_ecp_keypair_init( ecp );
4343 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4344 mbedtls_ctr_drbg_random,
4345 &global_data.ctr_drbg );
4346 if( ret != 0 )
4347 {
4348 mbedtls_ecp_keypair_free( ecp );
4349 mbedtls_free( ecp );
4350 return( mbedtls_to_psa_error( ret ) );
4351 }
4352 slot->data.ecp = ecp;
4353 }
4354 else
4355#endif /* MBEDTLS_ECP_C */
4356
4357 return( PSA_ERROR_NOT_SUPPORTED );
4358
4359 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004360
4361#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4362 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4363 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004364 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004365 }
4366#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4367
4368 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004369}
4370
4371
4372/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004373/* Module setup */
4374/****************************************************************/
4375
Gilles Peskine5e769522018-11-20 21:59:56 +01004376psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4377 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4378 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4379{
4380 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4381 return( PSA_ERROR_BAD_STATE );
4382 global_data.entropy_init = entropy_init;
4383 global_data.entropy_free = entropy_free;
4384 return( PSA_SUCCESS );
4385}
4386
Gilles Peskinee59236f2018-01-27 23:32:46 +01004387void mbedtls_psa_crypto_free( void )
4388{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004389 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004390 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4391 {
4392 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004393 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004394 }
4395 /* Wipe all remaining data, including configuration.
4396 * In particular, this sets all state indicator to the value
4397 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004398 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004399}
4400
4401psa_status_t psa_crypto_init( void )
4402{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004403 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004404 const unsigned char drbg_seed[] = "PSA";
4405
Gilles Peskinec6b69072018-11-20 21:42:52 +01004406 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004407 if( global_data.initialized != 0 )
4408 return( PSA_SUCCESS );
4409
Gilles Peskine5e769522018-11-20 21:59:56 +01004410 /* Set default configuration if
4411 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4412 if( global_data.entropy_init == NULL )
4413 global_data.entropy_init = mbedtls_entropy_init;
4414 if( global_data.entropy_free == NULL )
4415 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004416
Gilles Peskinec6b69072018-11-20 21:42:52 +01004417 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004418 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004419 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004420 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004421 status = mbedtls_to_psa_error(
4422 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4423 mbedtls_entropy_func,
4424 &global_data.entropy,
4425 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4426 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004427 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004428 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004429
Gilles Peskine66fb1262018-12-10 16:29:04 +01004430 status = psa_initialize_key_slots( );
4431 if( status != PSA_SUCCESS )
4432 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004433
4434 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004435 global_data.initialized = 1;
4436
Gilles Peskinee59236f2018-01-27 23:32:46 +01004437exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004438 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004439 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004440 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004441}
4442
4443#endif /* MBEDTLS_PSA_CRYPTO_C */