blob: eb4d43347a7c7955ca63659c20af4800dac9ba00 [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 Peskine4cb9dde2019-01-19 13:40:11 +0100716/** Calculate the intersection of two algorithm usage policies.
717 *
718 * Return 0 (which allows no operation) on incompatibility.
719 */
720static psa_algorithm_t psa_key_policy_algorithm_intersection(
721 psa_algorithm_t alg1,
722 psa_algorithm_t alg2 )
723{
724 /* Common case: the policy only allows alg. */
725 if( alg1 == alg2 )
726 return( alg1 );
727 /* If the policies are from the same hash-and-sign family, check
728 * if one is a wildcard. */
729 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
730 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
731 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
732 {
733 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
734 return( alg2 );
735 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
736 return( alg1 );
737 }
738 /* If the policies are incompatible, allow nothing. */
739 return( 0 );
740}
741
Gilles Peskine0ce26e32019-01-14 16:06:39 +0100742/** Test whether a policy permits an algorithm.
743 *
744 * The caller must test usage flags separately.
745 */
746static int psa_key_policy_permits( const psa_key_policy_t *policy,
747 psa_algorithm_t alg )
748{
749 /* Common case: the policy only allows alg. */
750 if( alg == policy->alg )
751 return( 1 );
752 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
753 * and alg is the same hash-and-sign family with any hash,
754 * then alg is compliant with policy->alg. */
755 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
756 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
757 {
758 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
759 ( alg & ~PSA_ALG_HASH_MASK ) );
760 }
761 /* If it isn't permitted, it's forbidden. */
762 return( 0 );
763}
764
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100765static psa_status_t psa_restrict_key_policy(
766 psa_key_policy_t *policy,
767 const psa_key_policy_t *constraint )
768{
769 psa_algorithm_t intersection_alg =
770 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
771 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
772 return( PSA_ERROR_INVALID_ARGUMENT );
773 policy->usage &= constraint->usage;
774 return( PSA_SUCCESS );
775}
776
Darryl Green06fd18d2018-07-16 11:21:11 +0100777/** Retrieve a slot which must contain a key. The key must have allow all the
778 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
779 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100780static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100781 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100782 psa_key_usage_t usage,
783 psa_algorithm_t alg )
784{
785 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100786 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100787
788 *p_slot = NULL;
789
Gilles Peskinec5487a82018-12-03 18:08:14 +0100790 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100791 if( status != PSA_SUCCESS )
792 return( status );
793 if( slot->type == PSA_KEY_TYPE_NONE )
794 return( PSA_ERROR_EMPTY_SLOT );
795
796 /* Enforce that usage policy for the key slot contains all the flags
797 * required by the usage parameter. There is one exception: public
798 * keys can always be exported, so we treat public key objects as
799 * if they had the export flag. */
800 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
801 usage &= ~PSA_KEY_USAGE_EXPORT;
802 if( ( slot->policy.usage & usage ) != usage )
803 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine0ce26e32019-01-14 16:06:39 +0100804
805 /* Enforce that the usage policy permits the requested algortihm. */
806 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100807 return( PSA_ERROR_NOT_PERMITTED );
808
809 *p_slot = slot;
810 return( PSA_SUCCESS );
811}
Darryl Green940d72c2018-07-13 13:18:51 +0100812
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100813/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100814static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000815{
816 if( slot->type == PSA_KEY_TYPE_NONE )
817 {
818 /* No key material to clean. */
819 }
820 else if( key_type_is_raw_bytes( slot->type ) )
821 {
822 mbedtls_free( slot->data.raw.data );
823 }
824 else
825#if defined(MBEDTLS_RSA_C)
826 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
827 {
828 mbedtls_rsa_free( slot->data.rsa );
829 mbedtls_free( slot->data.rsa );
830 }
831 else
832#endif /* defined(MBEDTLS_RSA_C) */
833#if defined(MBEDTLS_ECP_C)
834 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
835 {
836 mbedtls_ecp_keypair_free( slot->data.ecp );
837 mbedtls_free( slot->data.ecp );
838 }
839 else
840#endif /* defined(MBEDTLS_ECP_C) */
841 {
842 /* Shouldn't happen: the key type is not any type that we
843 * put in. */
844 return( PSA_ERROR_TAMPERING_DETECTED );
845 }
846
847 return( PSA_SUCCESS );
848}
849
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100850/** Completely wipe a slot in memory, including its policy.
851 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100852psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100853{
854 psa_status_t status = psa_remove_key_data_from_memory( slot );
855 /* At this point, key material and other type-specific content has
856 * been wiped. Clear remaining metadata. We can call memset and not
857 * zeroize because the metadata is not particularly sensitive. */
858 memset( slot, 0, sizeof( *slot ) );
859 return( status );
860}
861
Gilles Peskinec5487a82018-12-03 18:08:14 +0100862psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100863 psa_key_type_t type,
864 const uint8_t *data,
865 size_t data_length )
866{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100867 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100868 psa_status_t status;
869
Gilles Peskinec5487a82018-12-03 18:08:14 +0100870 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100871 if( status != PSA_SUCCESS )
872 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873
874 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100875
876 status = psa_import_key_into_slot( slot, data, data_length );
877 if( status != PSA_SUCCESS )
878 {
879 slot->type = PSA_KEY_TYPE_NONE;
880 return( status );
881 }
882
Darryl Greend49a4992018-06-18 17:27:26 +0100883#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
884 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
885 {
886 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100887 status = psa_save_persistent_key( slot->persistent_storage_id,
888 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100889 data_length );
890 if( status != PSA_SUCCESS )
891 {
892 (void) psa_remove_key_data_from_memory( slot );
893 slot->type = PSA_KEY_TYPE_NONE;
894 }
895 }
896#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
897
898 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100899}
900
Gilles Peskinec5487a82018-12-03 18:08:14 +0100901psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100902{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100903 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100904 psa_status_t status = PSA_SUCCESS;
905 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100906
Gilles Peskinec5487a82018-12-03 18:08:14 +0100907 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200908 if( status != PSA_SUCCESS )
909 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100910#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
911 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
912 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100913 storage_status =
914 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100915 }
916#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100917 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100918 if( status != PSA_SUCCESS )
919 return( status );
920 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100921}
922
Gilles Peskineb870b182018-07-06 16:02:09 +0200923/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100924static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200925{
926 if( key_type_is_raw_bytes( slot->type ) )
927 return( slot->data.raw.bytes * 8 );
928#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200929 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100930 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200931#endif /* defined(MBEDTLS_RSA_C) */
932#if defined(MBEDTLS_ECP_C)
933 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
934 return( slot->data.ecp->grp.pbits );
935#endif /* defined(MBEDTLS_ECP_C) */
936 /* Shouldn't happen except on an empty slot. */
937 return( 0 );
938}
939
Gilles Peskinec5487a82018-12-03 18:08:14 +0100940psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200941 psa_key_type_t *type,
942 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100944 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200945 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200948 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100949 if( bits != NULL )
950 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100951 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200952 if( status != PSA_SUCCESS )
953 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200954
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100955 if( slot->type == PSA_KEY_TYPE_NONE )
956 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200957 if( type != NULL )
958 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200959 if( bits != NULL )
960 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961 return( PSA_SUCCESS );
962}
963
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100964static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
965 uint8_t *data,
966 size_t data_size,
967 size_t *data_length,
968 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100970 *data_length = 0;
971
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200972 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300973 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300974
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200975 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100976 {
977 if( slot->data.raw.bytes > data_size )
978 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100979 if( data_size != 0 )
980 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200981 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100982 memset( data + slot->data.raw.bytes, 0,
983 data_size - slot->data.raw.bytes );
984 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985 *data_length = slot->data.raw.bytes;
986 return( PSA_SUCCESS );
987 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100988#if defined(MBEDTLS_ECP_C)
989 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
990 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000991 psa_status_t status;
992
Gilles Peskine188c71e2018-10-29 19:26:02 +0100993 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
994 if( bytes > data_size )
995 return( PSA_ERROR_BUFFER_TOO_SMALL );
996 status = mbedtls_to_psa_error(
997 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
998 if( status != PSA_SUCCESS )
999 return( status );
1000 memset( data + bytes, 0, data_size - bytes );
1001 *data_length = bytes;
1002 return( PSA_SUCCESS );
1003 }
1004#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005 else
Moran Peker17e36e12018-05-02 12:55:20 +03001006 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001007#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001008 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001009 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001010 {
Moran Pekera998bc62018-04-16 18:16:20 +03001011 mbedtls_pk_context pk;
1012 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001013 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001014 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001015#if defined(MBEDTLS_RSA_C)
1016 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001017 pk.pk_info = &mbedtls_rsa_info;
1018 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001019#else
1020 return( PSA_ERROR_NOT_SUPPORTED );
1021#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001022 }
1023 else
1024 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001025#if defined(MBEDTLS_ECP_C)
1026 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001027 pk.pk_info = &mbedtls_eckey_info;
1028 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001029#else
1030 return( PSA_ERROR_NOT_SUPPORTED );
1031#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001032 }
Moran Pekerd7326592018-05-29 16:56:39 +03001033 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001034 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001035 else
1036 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001037 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001038 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001039 /* If data_size is 0 then data may be NULL and then the
1040 * call to memset would have undefined behavior. */
1041 if( data_size != 0 )
1042 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001043 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001044 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001045 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1046 * Move the data to the beginning and erase remaining data
1047 * at the original location. */
1048 if( 2 * (size_t) ret <= data_size )
1049 {
1050 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001051 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001052 }
1053 else if( (size_t) ret < data_size )
1054 {
1055 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001056 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001057 }
Moran Pekera998bc62018-04-16 18:16:20 +03001058 *data_length = ret;
1059 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001060 }
1061 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001062#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001063 {
1064 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001065 it is valid for a special-purpose implementation to omit
1066 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001067 return( PSA_ERROR_NOT_SUPPORTED );
1068 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001069 }
1070}
1071
Gilles Peskinec5487a82018-12-03 18:08:14 +01001072psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001073 uint8_t *data,
1074 size_t data_size,
1075 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001076{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001077 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001078 psa_status_t status;
1079
1080 /* Set the key to empty now, so that even when there are errors, we always
1081 * set data_length to a value between 0 and data_size. On error, setting
1082 * the key to empty is a good choice because an empty key representation is
1083 * unlikely to be accepted anywhere. */
1084 *data_length = 0;
1085
1086 /* Export requires the EXPORT flag. There is an exception for public keys,
1087 * which don't require any flag, but psa_get_key_from_slot takes
1088 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001089 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001090 if( status != PSA_SUCCESS )
1091 return( status );
1092 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001093 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001094}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001095
Gilles Peskinec5487a82018-12-03 18:08:14 +01001096psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001097 uint8_t *data,
1098 size_t data_size,
1099 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001100{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001101 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001102 psa_status_t status;
1103
1104 /* Set the key to empty now, so that even when there are errors, we always
1105 * set data_length to a value between 0 and data_size. On error, setting
1106 * the key to empty is a good choice because an empty key representation is
1107 * unlikely to be accepted anywhere. */
1108 *data_length = 0;
1109
1110 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001111 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001112 if( status != PSA_SUCCESS )
1113 return( status );
1114 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001115 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001116}
1117
Darryl Green0c6575a2018-11-07 16:05:30 +00001118#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001119static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001120 size_t bits )
1121{
1122 psa_status_t status;
1123 uint8_t *data;
1124 size_t key_length;
1125 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1126 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001127 if( data == NULL )
1128 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001129 /* Get key data in export format */
1130 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1131 if( status != PSA_SUCCESS )
1132 {
1133 slot->type = PSA_KEY_TYPE_NONE;
1134 goto exit;
1135 }
1136 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001137 status = psa_save_persistent_key( slot->persistent_storage_id,
1138 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001139 data, key_length );
1140 if( status != PSA_SUCCESS )
1141 {
1142 slot->type = PSA_KEY_TYPE_NONE;
1143 }
1144exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001145 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001146 mbedtls_free( data );
1147 return( status );
1148}
1149#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1150
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001151static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1152 psa_key_handle_t target )
1153{
1154 psa_status_t status;
1155 uint8_t *buffer = NULL;
1156 size_t buffer_size = 0;
1157 size_t length;
1158
1159 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1160 psa_get_key_bits( source ) );
1161 buffer = mbedtls_calloc( 1, buffer_size );
1162 if( buffer == NULL )
1163 {
1164 status = PSA_ERROR_INSUFFICIENT_MEMORY;
1165 goto exit;
1166 }
1167 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1168 if( status != PSA_SUCCESS )
1169 goto exit;
1170 status = psa_import_key( target, source->type, buffer, length );
1171
1172exit:
1173 return( status );
1174}
1175
1176psa_status_t psa_copy_key(psa_key_handle_t source_handle,
1177 psa_key_handle_t target_handle,
1178 const psa_key_policy_t *constraint)
1179{
1180 psa_key_slot_t *source_slot = NULL;
1181 psa_key_slot_t *target_slot = NULL;
1182 psa_key_policy_t new_policy;
1183 psa_status_t status;
1184 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1185 if( status != PSA_SUCCESS )
1186 return( status );
1187 status = psa_get_empty_key_slot( target_handle, &target_slot );
1188 if( status != PSA_SUCCESS )
1189 return( status );
1190
1191 new_policy = target_slot->policy;
1192 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1193 if( status != PSA_SUCCESS )
1194 return( status );
1195 if( constraint != NULL )
1196 {
1197 status = psa_restrict_key_policy( &new_policy, constraint );
1198 if( status != PSA_SUCCESS )
1199 return( status );
1200 }
1201
1202 status = psa_copy_key_material( source_slot, target_handle );
1203 if( status != PSA_SUCCESS )
1204 return( status );
1205
1206 target_slot->policy = new_policy;
1207 return( PSA_SUCCESS );
1208}
1209
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001210
1211
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001212/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001213/* Message digests */
1214/****************************************************************/
1215
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001216static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001217{
1218 switch( alg )
1219 {
1220#if defined(MBEDTLS_MD2_C)
1221 case PSA_ALG_MD2:
1222 return( &mbedtls_md2_info );
1223#endif
1224#if defined(MBEDTLS_MD4_C)
1225 case PSA_ALG_MD4:
1226 return( &mbedtls_md4_info );
1227#endif
1228#if defined(MBEDTLS_MD5_C)
1229 case PSA_ALG_MD5:
1230 return( &mbedtls_md5_info );
1231#endif
1232#if defined(MBEDTLS_RIPEMD160_C)
1233 case PSA_ALG_RIPEMD160:
1234 return( &mbedtls_ripemd160_info );
1235#endif
1236#if defined(MBEDTLS_SHA1_C)
1237 case PSA_ALG_SHA_1:
1238 return( &mbedtls_sha1_info );
1239#endif
1240#if defined(MBEDTLS_SHA256_C)
1241 case PSA_ALG_SHA_224:
1242 return( &mbedtls_sha224_info );
1243 case PSA_ALG_SHA_256:
1244 return( &mbedtls_sha256_info );
1245#endif
1246#if defined(MBEDTLS_SHA512_C)
1247 case PSA_ALG_SHA_384:
1248 return( &mbedtls_sha384_info );
1249 case PSA_ALG_SHA_512:
1250 return( &mbedtls_sha512_info );
1251#endif
1252 default:
1253 return( NULL );
1254 }
1255}
1256
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001257psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1258{
1259 switch( operation->alg )
1260 {
Gilles Peskine81736312018-06-26 15:04:31 +02001261 case 0:
1262 /* The object has (apparently) been initialized but it is not
1263 * in use. It's ok to call abort on such an object, and there's
1264 * nothing to do. */
1265 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001266#if defined(MBEDTLS_MD2_C)
1267 case PSA_ALG_MD2:
1268 mbedtls_md2_free( &operation->ctx.md2 );
1269 break;
1270#endif
1271#if defined(MBEDTLS_MD4_C)
1272 case PSA_ALG_MD4:
1273 mbedtls_md4_free( &operation->ctx.md4 );
1274 break;
1275#endif
1276#if defined(MBEDTLS_MD5_C)
1277 case PSA_ALG_MD5:
1278 mbedtls_md5_free( &operation->ctx.md5 );
1279 break;
1280#endif
1281#if defined(MBEDTLS_RIPEMD160_C)
1282 case PSA_ALG_RIPEMD160:
1283 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1284 break;
1285#endif
1286#if defined(MBEDTLS_SHA1_C)
1287 case PSA_ALG_SHA_1:
1288 mbedtls_sha1_free( &operation->ctx.sha1 );
1289 break;
1290#endif
1291#if defined(MBEDTLS_SHA256_C)
1292 case PSA_ALG_SHA_224:
1293 case PSA_ALG_SHA_256:
1294 mbedtls_sha256_free( &operation->ctx.sha256 );
1295 break;
1296#endif
1297#if defined(MBEDTLS_SHA512_C)
1298 case PSA_ALG_SHA_384:
1299 case PSA_ALG_SHA_512:
1300 mbedtls_sha512_free( &operation->ctx.sha512 );
1301 break;
1302#endif
1303 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001304 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001305 }
1306 operation->alg = 0;
1307 return( PSA_SUCCESS );
1308}
1309
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001310psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001311 psa_algorithm_t alg )
1312{
1313 int ret;
1314 operation->alg = 0;
1315 switch( alg )
1316 {
1317#if defined(MBEDTLS_MD2_C)
1318 case PSA_ALG_MD2:
1319 mbedtls_md2_init( &operation->ctx.md2 );
1320 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1321 break;
1322#endif
1323#if defined(MBEDTLS_MD4_C)
1324 case PSA_ALG_MD4:
1325 mbedtls_md4_init( &operation->ctx.md4 );
1326 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1327 break;
1328#endif
1329#if defined(MBEDTLS_MD5_C)
1330 case PSA_ALG_MD5:
1331 mbedtls_md5_init( &operation->ctx.md5 );
1332 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1333 break;
1334#endif
1335#if defined(MBEDTLS_RIPEMD160_C)
1336 case PSA_ALG_RIPEMD160:
1337 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1338 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1339 break;
1340#endif
1341#if defined(MBEDTLS_SHA1_C)
1342 case PSA_ALG_SHA_1:
1343 mbedtls_sha1_init( &operation->ctx.sha1 );
1344 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1345 break;
1346#endif
1347#if defined(MBEDTLS_SHA256_C)
1348 case PSA_ALG_SHA_224:
1349 mbedtls_sha256_init( &operation->ctx.sha256 );
1350 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1351 break;
1352 case PSA_ALG_SHA_256:
1353 mbedtls_sha256_init( &operation->ctx.sha256 );
1354 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1355 break;
1356#endif
1357#if defined(MBEDTLS_SHA512_C)
1358 case PSA_ALG_SHA_384:
1359 mbedtls_sha512_init( &operation->ctx.sha512 );
1360 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1361 break;
1362 case PSA_ALG_SHA_512:
1363 mbedtls_sha512_init( &operation->ctx.sha512 );
1364 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1365 break;
1366#endif
1367 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001368 return( PSA_ALG_IS_HASH( alg ) ?
1369 PSA_ERROR_NOT_SUPPORTED :
1370 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001371 }
1372 if( ret == 0 )
1373 operation->alg = alg;
1374 else
1375 psa_hash_abort( operation );
1376 return( mbedtls_to_psa_error( ret ) );
1377}
1378
1379psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1380 const uint8_t *input,
1381 size_t input_length )
1382{
1383 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001384
1385 /* Don't require hash implementations to behave correctly on a
1386 * zero-length input, which may have an invalid pointer. */
1387 if( input_length == 0 )
1388 return( PSA_SUCCESS );
1389
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001390 switch( operation->alg )
1391 {
1392#if defined(MBEDTLS_MD2_C)
1393 case PSA_ALG_MD2:
1394 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1395 input, input_length );
1396 break;
1397#endif
1398#if defined(MBEDTLS_MD4_C)
1399 case PSA_ALG_MD4:
1400 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1401 input, input_length );
1402 break;
1403#endif
1404#if defined(MBEDTLS_MD5_C)
1405 case PSA_ALG_MD5:
1406 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1407 input, input_length );
1408 break;
1409#endif
1410#if defined(MBEDTLS_RIPEMD160_C)
1411 case PSA_ALG_RIPEMD160:
1412 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1413 input, input_length );
1414 break;
1415#endif
1416#if defined(MBEDTLS_SHA1_C)
1417 case PSA_ALG_SHA_1:
1418 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1419 input, input_length );
1420 break;
1421#endif
1422#if defined(MBEDTLS_SHA256_C)
1423 case PSA_ALG_SHA_224:
1424 case PSA_ALG_SHA_256:
1425 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1426 input, input_length );
1427 break;
1428#endif
1429#if defined(MBEDTLS_SHA512_C)
1430 case PSA_ALG_SHA_384:
1431 case PSA_ALG_SHA_512:
1432 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1433 input, input_length );
1434 break;
1435#endif
1436 default:
1437 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1438 break;
1439 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001440
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001441 if( ret != 0 )
1442 psa_hash_abort( operation );
1443 return( mbedtls_to_psa_error( ret ) );
1444}
1445
1446psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1447 uint8_t *hash,
1448 size_t hash_size,
1449 size_t *hash_length )
1450{
itayzafrir40835d42018-08-02 13:14:17 +03001451 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001452 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001453 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001454
1455 /* Fill the output buffer with something that isn't a valid hash
1456 * (barring an attack on the hash and deliberately-crafted input),
1457 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001458 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001459 /* If hash_size is 0 then hash may be NULL and then the
1460 * call to memset would have undefined behavior. */
1461 if( hash_size != 0 )
1462 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001463
1464 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001465 {
1466 status = PSA_ERROR_BUFFER_TOO_SMALL;
1467 goto exit;
1468 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001469
1470 switch( operation->alg )
1471 {
1472#if defined(MBEDTLS_MD2_C)
1473 case PSA_ALG_MD2:
1474 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1475 break;
1476#endif
1477#if defined(MBEDTLS_MD4_C)
1478 case PSA_ALG_MD4:
1479 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1480 break;
1481#endif
1482#if defined(MBEDTLS_MD5_C)
1483 case PSA_ALG_MD5:
1484 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1485 break;
1486#endif
1487#if defined(MBEDTLS_RIPEMD160_C)
1488 case PSA_ALG_RIPEMD160:
1489 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1490 break;
1491#endif
1492#if defined(MBEDTLS_SHA1_C)
1493 case PSA_ALG_SHA_1:
1494 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1495 break;
1496#endif
1497#if defined(MBEDTLS_SHA256_C)
1498 case PSA_ALG_SHA_224:
1499 case PSA_ALG_SHA_256:
1500 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1501 break;
1502#endif
1503#if defined(MBEDTLS_SHA512_C)
1504 case PSA_ALG_SHA_384:
1505 case PSA_ALG_SHA_512:
1506 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1507 break;
1508#endif
1509 default:
1510 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1511 break;
1512 }
itayzafrir40835d42018-08-02 13:14:17 +03001513 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001514
itayzafrir40835d42018-08-02 13:14:17 +03001515exit:
1516 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001517 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001518 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001519 return( psa_hash_abort( operation ) );
1520 }
1521 else
1522 {
1523 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001524 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001525 }
1526}
1527
Gilles Peskine2d277862018-06-18 15:41:12 +02001528psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1529 const uint8_t *hash,
1530 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001531{
1532 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1533 size_t actual_hash_length;
1534 psa_status_t status = psa_hash_finish( operation,
1535 actual_hash, sizeof( actual_hash ),
1536 &actual_hash_length );
1537 if( status != PSA_SUCCESS )
1538 return( status );
1539 if( actual_hash_length != hash_length )
1540 return( PSA_ERROR_INVALID_SIGNATURE );
1541 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1542 return( PSA_ERROR_INVALID_SIGNATURE );
1543 return( PSA_SUCCESS );
1544}
1545
1546
1547
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001548/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001549/* MAC */
1550/****************************************************************/
1551
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001552static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001553 psa_algorithm_t alg,
1554 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001555 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001556 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001557{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001558 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001559 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001560
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001561 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001562 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001563
Gilles Peskine8c9def32018-02-08 10:02:12 +01001564 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1565 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001566 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001567 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001568 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001569 mode = MBEDTLS_MODE_STREAM;
1570 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001571 case PSA_ALG_CTR:
1572 mode = MBEDTLS_MODE_CTR;
1573 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001574 case PSA_ALG_CFB:
1575 mode = MBEDTLS_MODE_CFB;
1576 break;
1577 case PSA_ALG_OFB:
1578 mode = MBEDTLS_MODE_OFB;
1579 break;
1580 case PSA_ALG_CBC_NO_PADDING:
1581 mode = MBEDTLS_MODE_CBC;
1582 break;
1583 case PSA_ALG_CBC_PKCS7:
1584 mode = MBEDTLS_MODE_CBC;
1585 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001586 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001587 mode = MBEDTLS_MODE_CCM;
1588 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001589 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001590 mode = MBEDTLS_MODE_GCM;
1591 break;
1592 default:
1593 return( NULL );
1594 }
1595 }
1596 else if( alg == PSA_ALG_CMAC )
1597 mode = MBEDTLS_MODE_ECB;
1598 else if( alg == PSA_ALG_GMAC )
1599 mode = MBEDTLS_MODE_GCM;
1600 else
1601 return( NULL );
1602
1603 switch( key_type )
1604 {
1605 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001606 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001607 break;
1608 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001609 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1610 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001611 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001612 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001613 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001614 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001615 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1616 * but two-key Triple-DES is functionally three-key Triple-DES
1617 * with K1=K3, so that's how we present it to mbedtls. */
1618 if( key_bits == 128 )
1619 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001620 break;
1621 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001622 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001623 break;
1624 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001625 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001626 break;
1627 default:
1628 return( NULL );
1629 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001630 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001631 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001632
Jaeden Amero23bbb752018-06-26 14:16:54 +01001633 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1634 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001635}
1636
Gilles Peskinea05219c2018-11-16 16:02:56 +01001637#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001638static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001639{
Gilles Peskine2d277862018-06-18 15:41:12 +02001640 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001641 {
1642 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001643 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001644 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001645 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001646 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001647 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001648 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001649 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001650 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001651 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001652 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001653 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001654 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001655 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001656 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001657 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001658 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001659 return( 128 );
1660 default:
1661 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001662 }
1663}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001664#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001665
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001666/* Initialize the MAC operation structure. Once this function has been
1667 * called, psa_mac_abort can run and will do the right thing. */
1668static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1669 psa_algorithm_t alg )
1670{
1671 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1672
1673 operation->alg = alg;
1674 operation->key_set = 0;
1675 operation->iv_set = 0;
1676 operation->iv_required = 0;
1677 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001678 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001679
1680#if defined(MBEDTLS_CMAC_C)
1681 if( alg == PSA_ALG_CMAC )
1682 {
1683 operation->iv_required = 0;
1684 mbedtls_cipher_init( &operation->ctx.cmac );
1685 status = PSA_SUCCESS;
1686 }
1687 else
1688#endif /* MBEDTLS_CMAC_C */
1689#if defined(MBEDTLS_MD_C)
1690 if( PSA_ALG_IS_HMAC( operation->alg ) )
1691 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001692 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1693 operation->ctx.hmac.hash_ctx.alg = 0;
1694 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001695 }
1696 else
1697#endif /* MBEDTLS_MD_C */
1698 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001699 if( ! PSA_ALG_IS_MAC( alg ) )
1700 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001701 }
1702
1703 if( status != PSA_SUCCESS )
1704 memset( operation, 0, sizeof( *operation ) );
1705 return( status );
1706}
1707
Gilles Peskine01126fa2018-07-12 17:04:55 +02001708#if defined(MBEDTLS_MD_C)
1709static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1710{
Gilles Peskine3f108122018-12-07 18:14:53 +01001711 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001712 return( psa_hash_abort( &hmac->hash_ctx ) );
1713}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001714
1715static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1716{
1717 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1718 memset( hmac, 0, sizeof( *hmac ) );
1719}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001720#endif /* MBEDTLS_MD_C */
1721
Gilles Peskine8c9def32018-02-08 10:02:12 +01001722psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1723{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001724 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001725 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001726 /* The object has (apparently) been initialized but it is not
1727 * in use. It's ok to call abort on such an object, and there's
1728 * nothing to do. */
1729 return( PSA_SUCCESS );
1730 }
1731 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001732#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001733 if( operation->alg == PSA_ALG_CMAC )
1734 {
1735 mbedtls_cipher_free( &operation->ctx.cmac );
1736 }
1737 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001738#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001739#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001740 if( PSA_ALG_IS_HMAC( operation->alg ) )
1741 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001742 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001743 }
1744 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001745#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001746 {
1747 /* Sanity check (shouldn't happen: operation->alg should
1748 * always have been initialized to a valid value). */
1749 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001750 }
Moran Peker41deec42018-04-04 15:43:05 +03001751
Gilles Peskine8c9def32018-02-08 10:02:12 +01001752 operation->alg = 0;
1753 operation->key_set = 0;
1754 operation->iv_set = 0;
1755 operation->iv_required = 0;
1756 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001757 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001758
Gilles Peskine8c9def32018-02-08 10:02:12 +01001759 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001760
1761bad_state:
1762 /* If abort is called on an uninitialized object, we can't trust
1763 * anything. Wipe the object in case it contains confidential data.
1764 * This may result in a memory leak if a pointer gets overwritten,
1765 * but it's too late to do anything about this. */
1766 memset( operation, 0, sizeof( *operation ) );
1767 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001768}
1769
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001770#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001771static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001772 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001773 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001774 const mbedtls_cipher_info_t *cipher_info )
1775{
1776 int ret;
1777
1778 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001779
1780 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1781 if( ret != 0 )
1782 return( ret );
1783
1784 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1785 slot->data.raw.data,
1786 key_bits );
1787 return( ret );
1788}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001789#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001790
Gilles Peskine248051a2018-06-20 16:09:38 +02001791#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001792static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1793 const uint8_t *key,
1794 size_t key_length,
1795 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001796{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001797 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001798 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001799 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001800 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001801 psa_status_t status;
1802
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001803 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1804 * overflow below. This should never trigger if the hash algorithm
1805 * is implemented correctly. */
1806 /* The size checks against the ipad and opad buffers cannot be written
1807 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1808 * because that triggers -Wlogical-op on GCC 7.3. */
1809 if( block_size > sizeof( ipad ) )
1810 return( PSA_ERROR_NOT_SUPPORTED );
1811 if( block_size > sizeof( hmac->opad ) )
1812 return( PSA_ERROR_NOT_SUPPORTED );
1813 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001814 return( PSA_ERROR_NOT_SUPPORTED );
1815
Gilles Peskined223b522018-06-11 18:12:58 +02001816 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001817 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001818 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001819 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001820 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001821 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001822 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001823 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001824 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001825 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001826 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001827 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001828 }
Gilles Peskine96889972018-07-12 17:07:03 +02001829 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1830 * but it is permitted. It is common when HMAC is used in HKDF, for
1831 * example. Don't call `memcpy` in the 0-length because `key` could be
1832 * an invalid pointer which would make the behavior undefined. */
1833 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001834 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001835
Gilles Peskined223b522018-06-11 18:12:58 +02001836 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1837 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001838 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001839 ipad[i] ^= 0x36;
1840 memset( ipad + key_length, 0x36, block_size - key_length );
1841
1842 /* Copy the key material from ipad to opad, flipping the requisite bits,
1843 * and filling the rest of opad with the requisite constant. */
1844 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001845 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1846 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001847
Gilles Peskine01126fa2018-07-12 17:04:55 +02001848 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001849 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001850 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001851
Gilles Peskine01126fa2018-07-12 17:04:55 +02001852 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001853
1854cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001855 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001856
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001857 return( status );
1858}
Gilles Peskine248051a2018-06-20 16:09:38 +02001859#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001860
Gilles Peskine89167cb2018-07-08 20:12:23 +02001861static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001862 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001863 psa_algorithm_t alg,
1864 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001865{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001866 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001867 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001868 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001869 psa_key_usage_t usage =
1870 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001871 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001872 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001873
Gilles Peskined911eb72018-08-14 15:18:45 +02001874 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001875 if( status != PSA_SUCCESS )
1876 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001877 if( is_sign )
1878 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001879
Gilles Peskinec5487a82018-12-03 18:08:14 +01001880 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001881 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001882 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001883 key_bits = psa_get_key_bits( slot );
1884
Gilles Peskine8c9def32018-02-08 10:02:12 +01001885#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001886 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001887 {
1888 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001889 mbedtls_cipher_info_from_psa( full_length_alg,
1890 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001891 int ret;
1892 if( cipher_info == NULL )
1893 {
1894 status = PSA_ERROR_NOT_SUPPORTED;
1895 goto exit;
1896 }
1897 operation->mac_size = cipher_info->block_size;
1898 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1899 status = mbedtls_to_psa_error( ret );
1900 }
1901 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001902#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001903#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001904 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001905 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001906 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001907 if( hash_alg == 0 )
1908 {
1909 status = PSA_ERROR_NOT_SUPPORTED;
1910 goto exit;
1911 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001912
1913 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1914 /* Sanity check. This shouldn't fail on a valid configuration. */
1915 if( operation->mac_size == 0 ||
1916 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1917 {
1918 status = PSA_ERROR_NOT_SUPPORTED;
1919 goto exit;
1920 }
1921
Gilles Peskine01126fa2018-07-12 17:04:55 +02001922 if( slot->type != PSA_KEY_TYPE_HMAC )
1923 {
1924 status = PSA_ERROR_INVALID_ARGUMENT;
1925 goto exit;
1926 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001927
Gilles Peskine01126fa2018-07-12 17:04:55 +02001928 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1929 slot->data.raw.data,
1930 slot->data.raw.bytes,
1931 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001932 }
1933 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001934#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001935 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001936 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001937 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001938 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001939
Gilles Peskined911eb72018-08-14 15:18:45 +02001940 if( truncated == 0 )
1941 {
1942 /* The "normal" case: untruncated algorithm. Nothing to do. */
1943 }
1944 else if( truncated < 4 )
1945 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001946 /* A very short MAC is too short for security since it can be
1947 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1948 * so we make this our minimum, even though 32 bits is still
1949 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001950 status = PSA_ERROR_NOT_SUPPORTED;
1951 }
1952 else if( truncated > operation->mac_size )
1953 {
1954 /* It's impossible to "truncate" to a larger length. */
1955 status = PSA_ERROR_INVALID_ARGUMENT;
1956 }
1957 else
1958 operation->mac_size = truncated;
1959
Gilles Peskinefbfac682018-07-08 20:51:54 +02001960exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001961 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001962 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001963 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001964 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001965 else
1966 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001967 operation->key_set = 1;
1968 }
1969 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001970}
1971
Gilles Peskine89167cb2018-07-08 20:12:23 +02001972psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001973 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001974 psa_algorithm_t alg )
1975{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001976 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001977}
1978
1979psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001980 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001981 psa_algorithm_t alg )
1982{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001983 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001984}
1985
Gilles Peskine8c9def32018-02-08 10:02:12 +01001986psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1987 const uint8_t *input,
1988 size_t input_length )
1989{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001990 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001991 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001992 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001993 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001994 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001995 operation->has_input = 1;
1996
Gilles Peskine8c9def32018-02-08 10:02:12 +01001997#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001998 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001999 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002000 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2001 input, input_length );
2002 status = mbedtls_to_psa_error( ret );
2003 }
2004 else
2005#endif /* MBEDTLS_CMAC_C */
2006#if defined(MBEDTLS_MD_C)
2007 if( PSA_ALG_IS_HMAC( operation->alg ) )
2008 {
2009 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2010 input_length );
2011 }
2012 else
2013#endif /* MBEDTLS_MD_C */
2014 {
2015 /* This shouldn't happen if `operation` was initialized by
2016 * a setup function. */
2017 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002018 }
2019
Gilles Peskinefbfac682018-07-08 20:51:54 +02002020cleanup:
2021 if( status != PSA_SUCCESS )
2022 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002023 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002024}
2025
Gilles Peskine01126fa2018-07-12 17:04:55 +02002026#if defined(MBEDTLS_MD_C)
2027static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2028 uint8_t *mac,
2029 size_t mac_size )
2030{
2031 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2032 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2033 size_t hash_size = 0;
2034 size_t block_size = psa_get_hash_block_size( hash_alg );
2035 psa_status_t status;
2036
Gilles Peskine01126fa2018-07-12 17:04:55 +02002037 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2038 if( status != PSA_SUCCESS )
2039 return( status );
2040 /* From here on, tmp needs to be wiped. */
2041
2042 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2043 if( status != PSA_SUCCESS )
2044 goto exit;
2045
2046 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2047 if( status != PSA_SUCCESS )
2048 goto exit;
2049
2050 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2051 if( status != PSA_SUCCESS )
2052 goto exit;
2053
Gilles Peskined911eb72018-08-14 15:18:45 +02002054 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2055 if( status != PSA_SUCCESS )
2056 goto exit;
2057
2058 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002059
2060exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002061 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002062 return( status );
2063}
2064#endif /* MBEDTLS_MD_C */
2065
mohammad16036df908f2018-04-02 08:34:15 -07002066static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002067 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002068 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002069{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002070 if( ! operation->key_set )
2071 return( PSA_ERROR_BAD_STATE );
2072 if( operation->iv_required && ! operation->iv_set )
2073 return( PSA_ERROR_BAD_STATE );
2074
Gilles Peskine8c9def32018-02-08 10:02:12 +01002075 if( mac_size < operation->mac_size )
2076 return( PSA_ERROR_BUFFER_TOO_SMALL );
2077
Gilles Peskine8c9def32018-02-08 10:02:12 +01002078#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002079 if( operation->alg == PSA_ALG_CMAC )
2080 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002081 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2082 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2083 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002084 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002085 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002086 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002087 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002088 else
2089#endif /* MBEDTLS_CMAC_C */
2090#if defined(MBEDTLS_MD_C)
2091 if( PSA_ALG_IS_HMAC( operation->alg ) )
2092 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002093 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002094 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002095 }
2096 else
2097#endif /* MBEDTLS_MD_C */
2098 {
2099 /* This shouldn't happen if `operation` was initialized by
2100 * a setup function. */
2101 return( PSA_ERROR_BAD_STATE );
2102 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002103}
2104
Gilles Peskineacd4be32018-07-08 19:56:25 +02002105psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2106 uint8_t *mac,
2107 size_t mac_size,
2108 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002109{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002110 psa_status_t status;
2111
2112 /* Fill the output buffer with something that isn't a valid mac
2113 * (barring an attack on the mac and deliberately-crafted input),
2114 * in case the caller doesn't check the return status properly. */
2115 *mac_length = mac_size;
2116 /* If mac_size is 0 then mac may be NULL and then the
2117 * call to memset would have undefined behavior. */
2118 if( mac_size != 0 )
2119 memset( mac, '!', mac_size );
2120
Gilles Peskine89167cb2018-07-08 20:12:23 +02002121 if( ! operation->is_sign )
2122 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002123 status = PSA_ERROR_BAD_STATE;
2124 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002125 }
mohammad16036df908f2018-04-02 08:34:15 -07002126
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002127 status = psa_mac_finish_internal( operation, mac, mac_size );
2128
2129cleanup:
2130 if( status == PSA_SUCCESS )
2131 {
2132 status = psa_mac_abort( operation );
2133 if( status == PSA_SUCCESS )
2134 *mac_length = operation->mac_size;
2135 else
2136 memset( mac, '!', mac_size );
2137 }
2138 else
2139 psa_mac_abort( operation );
2140 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002141}
2142
Gilles Peskineacd4be32018-07-08 19:56:25 +02002143psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2144 const uint8_t *mac,
2145 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002146{
Gilles Peskine828ed142018-06-18 23:25:51 +02002147 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002148 psa_status_t status;
2149
Gilles Peskine89167cb2018-07-08 20:12:23 +02002150 if( operation->is_sign )
2151 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002152 status = PSA_ERROR_BAD_STATE;
2153 goto cleanup;
2154 }
2155 if( operation->mac_size != mac_length )
2156 {
2157 status = PSA_ERROR_INVALID_SIGNATURE;
2158 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002159 }
mohammad16036df908f2018-04-02 08:34:15 -07002160
2161 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002162 actual_mac, sizeof( actual_mac ) );
2163
2164 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2165 status = PSA_ERROR_INVALID_SIGNATURE;
2166
2167cleanup:
2168 if( status == PSA_SUCCESS )
2169 status = psa_mac_abort( operation );
2170 else
2171 psa_mac_abort( operation );
2172
Gilles Peskine3f108122018-12-07 18:14:53 +01002173 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002174
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002175 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002176}
2177
2178
Gilles Peskine20035e32018-02-03 22:44:14 +01002179
Gilles Peskine20035e32018-02-03 22:44:14 +01002180/****************************************************************/
2181/* Asymmetric cryptography */
2182/****************************************************************/
2183
Gilles Peskine2b450e32018-06-27 15:42:46 +02002184#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002185/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002186 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002187static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2188 size_t hash_length,
2189 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002190{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002191 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002192 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002193 *md_alg = mbedtls_md_get_type( md_info );
2194
2195 /* The Mbed TLS RSA module uses an unsigned int for hash length
2196 * parameters. Validate that it fits so that we don't risk an
2197 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002198#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002199 if( hash_length > UINT_MAX )
2200 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002201#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002202
2203#if defined(MBEDTLS_PKCS1_V15)
2204 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2205 * must be correct. */
2206 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2207 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002208 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002209 if( md_info == NULL )
2210 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002211 if( mbedtls_md_get_size( md_info ) != hash_length )
2212 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002213 }
2214#endif /* MBEDTLS_PKCS1_V15 */
2215
2216#if defined(MBEDTLS_PKCS1_V21)
2217 /* PSS requires a hash internally. */
2218 if( PSA_ALG_IS_RSA_PSS( alg ) )
2219 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002220 if( md_info == NULL )
2221 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002222 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002223#endif /* MBEDTLS_PKCS1_V21 */
2224
Gilles Peskine61b91d42018-06-08 16:09:36 +02002225 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002226}
2227
Gilles Peskine2b450e32018-06-27 15:42:46 +02002228static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2229 psa_algorithm_t alg,
2230 const uint8_t *hash,
2231 size_t hash_length,
2232 uint8_t *signature,
2233 size_t signature_size,
2234 size_t *signature_length )
2235{
2236 psa_status_t status;
2237 int ret;
2238 mbedtls_md_type_t md_alg;
2239
2240 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2241 if( status != PSA_SUCCESS )
2242 return( status );
2243
Gilles Peskine630a18a2018-06-29 17:49:35 +02002244 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002245 return( PSA_ERROR_BUFFER_TOO_SMALL );
2246
2247#if defined(MBEDTLS_PKCS1_V15)
2248 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2249 {
2250 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2251 MBEDTLS_MD_NONE );
2252 ret = mbedtls_rsa_pkcs1_sign( rsa,
2253 mbedtls_ctr_drbg_random,
2254 &global_data.ctr_drbg,
2255 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002256 md_alg,
2257 (unsigned int) hash_length,
2258 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002259 signature );
2260 }
2261 else
2262#endif /* MBEDTLS_PKCS1_V15 */
2263#if defined(MBEDTLS_PKCS1_V21)
2264 if( PSA_ALG_IS_RSA_PSS( alg ) )
2265 {
2266 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2267 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2268 mbedtls_ctr_drbg_random,
2269 &global_data.ctr_drbg,
2270 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002271 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002272 (unsigned int) hash_length,
2273 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002274 signature );
2275 }
2276 else
2277#endif /* MBEDTLS_PKCS1_V21 */
2278 {
2279 return( PSA_ERROR_INVALID_ARGUMENT );
2280 }
2281
2282 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002283 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002284 return( mbedtls_to_psa_error( ret ) );
2285}
2286
2287static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2288 psa_algorithm_t alg,
2289 const uint8_t *hash,
2290 size_t hash_length,
2291 const uint8_t *signature,
2292 size_t signature_length )
2293{
2294 psa_status_t status;
2295 int ret;
2296 mbedtls_md_type_t md_alg;
2297
2298 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2299 if( status != PSA_SUCCESS )
2300 return( status );
2301
Gilles Peskine630a18a2018-06-29 17:49:35 +02002302 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002303 return( PSA_ERROR_BUFFER_TOO_SMALL );
2304
2305#if defined(MBEDTLS_PKCS1_V15)
2306 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2307 {
2308 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2309 MBEDTLS_MD_NONE );
2310 ret = mbedtls_rsa_pkcs1_verify( rsa,
2311 mbedtls_ctr_drbg_random,
2312 &global_data.ctr_drbg,
2313 MBEDTLS_RSA_PUBLIC,
2314 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002315 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002316 hash,
2317 signature );
2318 }
2319 else
2320#endif /* MBEDTLS_PKCS1_V15 */
2321#if defined(MBEDTLS_PKCS1_V21)
2322 if( PSA_ALG_IS_RSA_PSS( alg ) )
2323 {
2324 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2325 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2326 mbedtls_ctr_drbg_random,
2327 &global_data.ctr_drbg,
2328 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002329 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002330 (unsigned int) hash_length,
2331 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002332 signature );
2333 }
2334 else
2335#endif /* MBEDTLS_PKCS1_V21 */
2336 {
2337 return( PSA_ERROR_INVALID_ARGUMENT );
2338 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002339
2340 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2341 * the rest of the signature is invalid". This has little use in
2342 * practice and PSA doesn't report this distinction. */
2343 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2344 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002345 return( mbedtls_to_psa_error( ret ) );
2346}
2347#endif /* MBEDTLS_RSA_C */
2348
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002349#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002350/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2351 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2352 * (even though these functions don't modify it). */
2353static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2354 psa_algorithm_t alg,
2355 const uint8_t *hash,
2356 size_t hash_length,
2357 uint8_t *signature,
2358 size_t signature_size,
2359 size_t *signature_length )
2360{
2361 int ret;
2362 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002363 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002364 mbedtls_mpi_init( &r );
2365 mbedtls_mpi_init( &s );
2366
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002367 if( signature_size < 2 * curve_bytes )
2368 {
2369 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2370 goto cleanup;
2371 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002372
Gilles Peskinea05219c2018-11-16 16:02:56 +01002373#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002374 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2375 {
2376 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2377 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2378 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2379 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2380 hash, hash_length,
2381 md_alg ) );
2382 }
2383 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002384#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002385 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002386 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002387 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2388 hash, hash_length,
2389 mbedtls_ctr_drbg_random,
2390 &global_data.ctr_drbg ) );
2391 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002392
2393 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2394 signature,
2395 curve_bytes ) );
2396 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2397 signature + curve_bytes,
2398 curve_bytes ) );
2399
2400cleanup:
2401 mbedtls_mpi_free( &r );
2402 mbedtls_mpi_free( &s );
2403 if( ret == 0 )
2404 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002405 return( mbedtls_to_psa_error( ret ) );
2406}
2407
2408static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2409 const uint8_t *hash,
2410 size_t hash_length,
2411 const uint8_t *signature,
2412 size_t signature_length )
2413{
2414 int ret;
2415 mbedtls_mpi r, s;
2416 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2417 mbedtls_mpi_init( &r );
2418 mbedtls_mpi_init( &s );
2419
2420 if( signature_length != 2 * curve_bytes )
2421 return( PSA_ERROR_INVALID_SIGNATURE );
2422
2423 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2424 signature,
2425 curve_bytes ) );
2426 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2427 signature + curve_bytes,
2428 curve_bytes ) );
2429
2430 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2431 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002432
2433cleanup:
2434 mbedtls_mpi_free( &r );
2435 mbedtls_mpi_free( &s );
2436 return( mbedtls_to_psa_error( ret ) );
2437}
2438#endif /* MBEDTLS_ECDSA_C */
2439
Gilles Peskinec5487a82018-12-03 18:08:14 +01002440psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002441 psa_algorithm_t alg,
2442 const uint8_t *hash,
2443 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002444 uint8_t *signature,
2445 size_t signature_size,
2446 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002447{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002448 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002449 psa_status_t status;
2450
2451 *signature_length = signature_size;
2452
Gilles Peskinec5487a82018-12-03 18:08:14 +01002453 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002454 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002455 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002456 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002457 {
2458 status = PSA_ERROR_INVALID_ARGUMENT;
2459 goto exit;
2460 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002461
Gilles Peskine20035e32018-02-03 22:44:14 +01002462#if defined(MBEDTLS_RSA_C)
2463 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2464 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002465 status = psa_rsa_sign( slot->data.rsa,
2466 alg,
2467 hash, hash_length,
2468 signature, signature_size,
2469 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002470 }
2471 else
2472#endif /* defined(MBEDTLS_RSA_C) */
2473#if defined(MBEDTLS_ECP_C)
2474 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2475 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002476#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002477 if(
2478#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2479 PSA_ALG_IS_ECDSA( alg )
2480#else
2481 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2482#endif
2483 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002484 status = psa_ecdsa_sign( slot->data.ecp,
2485 alg,
2486 hash, hash_length,
2487 signature, signature_size,
2488 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002489 else
2490#endif /* defined(MBEDTLS_ECDSA_C) */
2491 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002492 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002493 }
itayzafrir5c753392018-05-08 11:18:38 +03002494 }
2495 else
2496#endif /* defined(MBEDTLS_ECP_C) */
2497 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002498 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002499 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002500
2501exit:
2502 /* Fill the unused part of the output buffer (the whole buffer on error,
2503 * the trailing part on success) with something that isn't a valid mac
2504 * (barring an attack on the mac and deliberately-crafted input),
2505 * in case the caller doesn't check the return status properly. */
2506 if( status == PSA_SUCCESS )
2507 memset( signature + *signature_length, '!',
2508 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002509 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002510 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002511 /* If signature_size is 0 then we have nothing to do. We must not call
2512 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002513 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002514}
2515
Gilles Peskinec5487a82018-12-03 18:08:14 +01002516psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002517 psa_algorithm_t alg,
2518 const uint8_t *hash,
2519 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002520 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002521 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002522{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002523 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002524 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002525
Gilles Peskinec5487a82018-12-03 18:08:14 +01002526 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002527 if( status != PSA_SUCCESS )
2528 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002529
Gilles Peskine61b91d42018-06-08 16:09:36 +02002530#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002531 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002532 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002533 return( psa_rsa_verify( slot->data.rsa,
2534 alg,
2535 hash, hash_length,
2536 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002537 }
2538 else
2539#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002540#if defined(MBEDTLS_ECP_C)
2541 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2542 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002543#if defined(MBEDTLS_ECDSA_C)
2544 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002545 return( psa_ecdsa_verify( slot->data.ecp,
2546 hash, hash_length,
2547 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002548 else
2549#endif /* defined(MBEDTLS_ECDSA_C) */
2550 {
2551 return( PSA_ERROR_INVALID_ARGUMENT );
2552 }
itayzafrir5c753392018-05-08 11:18:38 +03002553 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002554 else
2555#endif /* defined(MBEDTLS_ECP_C) */
2556 {
2557 return( PSA_ERROR_NOT_SUPPORTED );
2558 }
2559}
2560
Gilles Peskine072ac562018-06-30 00:21:29 +02002561#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2562static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2563 mbedtls_rsa_context *rsa )
2564{
2565 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2566 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2567 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2568 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2569}
2570#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2571
Gilles Peskinec5487a82018-12-03 18:08:14 +01002572psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002573 psa_algorithm_t alg,
2574 const uint8_t *input,
2575 size_t input_length,
2576 const uint8_t *salt,
2577 size_t salt_length,
2578 uint8_t *output,
2579 size_t output_size,
2580 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002581{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002582 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002583 psa_status_t status;
2584
Darryl Green5cc689a2018-07-24 15:34:10 +01002585 (void) input;
2586 (void) input_length;
2587 (void) salt;
2588 (void) output;
2589 (void) output_size;
2590
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002591 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002592
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002593 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2594 return( PSA_ERROR_INVALID_ARGUMENT );
2595
Gilles Peskinec5487a82018-12-03 18:08:14 +01002596 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002597 if( status != PSA_SUCCESS )
2598 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002599 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2600 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002601 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002602
2603#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002604 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002605 {
2606 mbedtls_rsa_context *rsa = slot->data.rsa;
2607 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002608 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002609 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002610#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002611 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002612 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002613 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2614 mbedtls_ctr_drbg_random,
2615 &global_data.ctr_drbg,
2616 MBEDTLS_RSA_PUBLIC,
2617 input_length,
2618 input,
2619 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002620 }
2621 else
2622#endif /* MBEDTLS_PKCS1_V15 */
2623#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002624 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002625 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002626 psa_rsa_oaep_set_padding_mode( alg, rsa );
2627 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2628 mbedtls_ctr_drbg_random,
2629 &global_data.ctr_drbg,
2630 MBEDTLS_RSA_PUBLIC,
2631 salt, salt_length,
2632 input_length,
2633 input,
2634 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002635 }
2636 else
2637#endif /* MBEDTLS_PKCS1_V21 */
2638 {
2639 return( PSA_ERROR_INVALID_ARGUMENT );
2640 }
2641 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002642 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002643 return( mbedtls_to_psa_error( ret ) );
2644 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002645 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002646#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002647 {
2648 return( PSA_ERROR_NOT_SUPPORTED );
2649 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002650}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002651
Gilles Peskinec5487a82018-12-03 18:08:14 +01002652psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002653 psa_algorithm_t alg,
2654 const uint8_t *input,
2655 size_t input_length,
2656 const uint8_t *salt,
2657 size_t salt_length,
2658 uint8_t *output,
2659 size_t output_size,
2660 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002661{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002662 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002663 psa_status_t status;
2664
Darryl Green5cc689a2018-07-24 15:34:10 +01002665 (void) input;
2666 (void) input_length;
2667 (void) salt;
2668 (void) output;
2669 (void) output_size;
2670
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002671 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002672
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002673 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2674 return( PSA_ERROR_INVALID_ARGUMENT );
2675
Gilles Peskinec5487a82018-12-03 18:08:14 +01002676 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002677 if( status != PSA_SUCCESS )
2678 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002679 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2680 return( PSA_ERROR_INVALID_ARGUMENT );
2681
2682#if defined(MBEDTLS_RSA_C)
2683 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2684 {
2685 mbedtls_rsa_context *rsa = slot->data.rsa;
2686 int ret;
2687
Gilles Peskine630a18a2018-06-29 17:49:35 +02002688 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002689 return( PSA_ERROR_INVALID_ARGUMENT );
2690
2691#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002692 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002693 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002694 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2695 mbedtls_ctr_drbg_random,
2696 &global_data.ctr_drbg,
2697 MBEDTLS_RSA_PRIVATE,
2698 output_length,
2699 input,
2700 output,
2701 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002702 }
2703 else
2704#endif /* MBEDTLS_PKCS1_V15 */
2705#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002706 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002707 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002708 psa_rsa_oaep_set_padding_mode( alg, rsa );
2709 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2710 mbedtls_ctr_drbg_random,
2711 &global_data.ctr_drbg,
2712 MBEDTLS_RSA_PRIVATE,
2713 salt, salt_length,
2714 output_length,
2715 input,
2716 output,
2717 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002718 }
2719 else
2720#endif /* MBEDTLS_PKCS1_V21 */
2721 {
2722 return( PSA_ERROR_INVALID_ARGUMENT );
2723 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002724
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002725 return( mbedtls_to_psa_error( ret ) );
2726 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002727 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002728#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002729 {
2730 return( PSA_ERROR_NOT_SUPPORTED );
2731 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002732}
Gilles Peskine20035e32018-02-03 22:44:14 +01002733
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002734
2735
mohammad1603503973b2018-03-12 15:59:30 +02002736/****************************************************************/
2737/* Symmetric cryptography */
2738/****************************************************************/
2739
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002740/* Initialize the cipher operation structure. Once this function has been
2741 * called, psa_cipher_abort can run and will do the right thing. */
2742static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2743 psa_algorithm_t alg )
2744{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002745 if( ! PSA_ALG_IS_CIPHER( alg ) )
2746 {
2747 memset( operation, 0, sizeof( *operation ) );
2748 return( PSA_ERROR_INVALID_ARGUMENT );
2749 }
2750
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002751 operation->alg = alg;
2752 operation->key_set = 0;
2753 operation->iv_set = 0;
2754 operation->iv_required = 1;
2755 operation->iv_size = 0;
2756 operation->block_size = 0;
2757 mbedtls_cipher_init( &operation->ctx.cipher );
2758 return( PSA_SUCCESS );
2759}
2760
Gilles Peskinee553c652018-06-04 16:22:46 +02002761static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002762 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002763 psa_algorithm_t alg,
2764 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002765{
2766 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2767 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002768 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002769 size_t key_bits;
2770 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002771 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2772 PSA_KEY_USAGE_ENCRYPT :
2773 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002774
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002775 status = psa_cipher_init( operation, alg );
2776 if( status != PSA_SUCCESS )
2777 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002778
Gilles Peskinec5487a82018-12-03 18:08:14 +01002779 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002780 if( status != PSA_SUCCESS )
2781 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002782 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002783
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002784 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002785 if( cipher_info == NULL )
2786 return( PSA_ERROR_NOT_SUPPORTED );
2787
mohammad1603503973b2018-03-12 15:59:30 +02002788 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002789 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002790 {
2791 psa_cipher_abort( operation );
2792 return( mbedtls_to_psa_error( ret ) );
2793 }
2794
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002795#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002796 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002797 {
2798 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2799 unsigned char keys[24];
2800 memcpy( keys, slot->data.raw.data, 16 );
2801 memcpy( keys + 16, slot->data.raw.data, 8 );
2802 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2803 keys,
2804 192, cipher_operation );
2805 }
2806 else
2807#endif
2808 {
2809 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2810 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002811 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002812 }
Moran Peker41deec42018-04-04 15:43:05 +03002813 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002814 {
2815 psa_cipher_abort( operation );
2816 return( mbedtls_to_psa_error( ret ) );
2817 }
2818
mohammad16038481e742018-03-18 13:57:31 +02002819#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002820 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002821 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002822 case PSA_ALG_CBC_NO_PADDING:
2823 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2824 MBEDTLS_PADDING_NONE );
2825 break;
2826 case PSA_ALG_CBC_PKCS7:
2827 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2828 MBEDTLS_PADDING_PKCS7 );
2829 break;
2830 default:
2831 /* The algorithm doesn't involve padding. */
2832 ret = 0;
2833 break;
2834 }
2835 if( ret != 0 )
2836 {
2837 psa_cipher_abort( operation );
2838 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002839 }
2840#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2841
mohammad1603503973b2018-03-12 15:59:30 +02002842 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002843 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2844 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2845 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002846 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002847 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002848 }
mohammad1603503973b2018-03-12 15:59:30 +02002849
Moran Peker395db872018-05-31 14:07:14 +03002850 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002851}
2852
Gilles Peskinefe119512018-07-08 21:39:34 +02002853psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002854 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002855 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002856{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002857 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002858}
2859
Gilles Peskinefe119512018-07-08 21:39:34 +02002860psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002861 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002862 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002863{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002864 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002865}
2866
Gilles Peskinefe119512018-07-08 21:39:34 +02002867psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2868 unsigned char *iv,
2869 size_t iv_size,
2870 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002871{
itayzafrir534bd7c2018-08-02 13:56:32 +03002872 psa_status_t status;
2873 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002874 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002875 {
2876 status = PSA_ERROR_BAD_STATE;
2877 goto exit;
2878 }
Moran Peker41deec42018-04-04 15:43:05 +03002879 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002880 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002881 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002882 goto exit;
2883 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002884 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2885 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002886 if( ret != 0 )
2887 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002888 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002889 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002890 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002891
mohammad16038481e742018-03-18 13:57:31 +02002892 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002893 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002894
Moran Peker395db872018-05-31 14:07:14 +03002895exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002896 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002897 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002898 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002899}
2900
Gilles Peskinefe119512018-07-08 21:39:34 +02002901psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2902 const unsigned char *iv,
2903 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002904{
itayzafrir534bd7c2018-08-02 13:56:32 +03002905 psa_status_t status;
2906 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002907 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002908 {
2909 status = PSA_ERROR_BAD_STATE;
2910 goto exit;
2911 }
Moran Pekera28258c2018-05-29 16:25:04 +03002912 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002913 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002914 status = PSA_ERROR_INVALID_ARGUMENT;
2915 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002916 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002917 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2918 status = mbedtls_to_psa_error( ret );
2919exit:
2920 if( status == PSA_SUCCESS )
2921 operation->iv_set = 1;
2922 else
mohammad1603503973b2018-03-12 15:59:30 +02002923 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002924 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002925}
2926
Gilles Peskinee553c652018-06-04 16:22:46 +02002927psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2928 const uint8_t *input,
2929 size_t input_length,
2930 unsigned char *output,
2931 size_t output_size,
2932 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002933{
itayzafrir534bd7c2018-08-02 13:56:32 +03002934 psa_status_t status;
2935 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002936 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002937 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002938 {
2939 /* Take the unprocessed partial block left over from previous
2940 * update calls, if any, plus the input to this call. Remove
2941 * the last partial block, if any. You get the data that will be
2942 * output in this call. */
2943 expected_output_size =
2944 ( operation->ctx.cipher.unprocessed_len + input_length )
2945 / operation->block_size * operation->block_size;
2946 }
2947 else
2948 {
2949 expected_output_size = input_length;
2950 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002951
Gilles Peskine89d789c2018-06-04 17:17:16 +02002952 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002953 {
2954 status = PSA_ERROR_BUFFER_TOO_SMALL;
2955 goto exit;
2956 }
mohammad160382759612018-03-12 18:16:40 +02002957
mohammad1603503973b2018-03-12 15:59:30 +02002958 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002959 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002960 status = mbedtls_to_psa_error( ret );
2961exit:
2962 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002963 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002964 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002965}
2966
Gilles Peskinee553c652018-06-04 16:22:46 +02002967psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2968 uint8_t *output,
2969 size_t output_size,
2970 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002971{
Janos Follath315b51c2018-07-09 16:04:51 +01002972 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2973 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002974 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002975
mohammad1603503973b2018-03-12 15:59:30 +02002976 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002977 {
Janos Follath315b51c2018-07-09 16:04:51 +01002978 status = PSA_ERROR_BAD_STATE;
2979 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002980 }
2981 if( operation->iv_required && ! operation->iv_set )
2982 {
Janos Follath315b51c2018-07-09 16:04:51 +01002983 status = PSA_ERROR_BAD_STATE;
2984 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002985 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002986
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002987 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002988 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2989 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002990 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002991 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002992 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002993 }
2994
Janos Follath315b51c2018-07-09 16:04:51 +01002995 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2996 temp_output_buffer,
2997 output_length );
2998 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002999 {
Janos Follath315b51c2018-07-09 16:04:51 +01003000 status = mbedtls_to_psa_error( cipher_ret );
3001 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003002 }
Janos Follath315b51c2018-07-09 16:04:51 +01003003
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003004 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003005 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003006 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003007 memcpy( output, temp_output_buffer, *output_length );
3008 else
3009 {
Janos Follath315b51c2018-07-09 16:04:51 +01003010 status = PSA_ERROR_BUFFER_TOO_SMALL;
3011 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003012 }
mohammad1603503973b2018-03-12 15:59:30 +02003013
Gilles Peskine3f108122018-12-07 18:14:53 +01003014 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003015 status = psa_cipher_abort( operation );
3016
3017 return( status );
3018
3019error:
3020
3021 *output_length = 0;
3022
Gilles Peskine3f108122018-12-07 18:14:53 +01003023 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003024 (void) psa_cipher_abort( operation );
3025
3026 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003027}
3028
Gilles Peskinee553c652018-06-04 16:22:46 +02003029psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3030{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003031 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003032 {
3033 /* The object has (apparently) been initialized but it is not
3034 * in use. It's ok to call abort on such an object, and there's
3035 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003036 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003037 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003038
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003039 /* Sanity check (shouldn't happen: operation->alg should
3040 * always have been initialized to a valid value). */
3041 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3042 return( PSA_ERROR_BAD_STATE );
3043
mohammad1603503973b2018-03-12 15:59:30 +02003044 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003045
Moran Peker41deec42018-04-04 15:43:05 +03003046 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003047 operation->key_set = 0;
3048 operation->iv_set = 0;
3049 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003050 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003051 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003052
Moran Peker395db872018-05-31 14:07:14 +03003053 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003054}
3055
Gilles Peskinea0655c32018-04-30 17:06:50 +02003056
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003057
mohammad16038cc1cee2018-03-28 01:21:33 +03003058/****************************************************************/
3059/* Key Policy */
3060/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003061
mohammad160327010052018-07-03 13:16:15 +03003062#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003063void psa_key_policy_set_usage( psa_key_policy_t *policy,
3064 psa_key_usage_t usage,
3065 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003066{
mohammad16034eed7572018-03-28 05:14:59 -07003067 policy->usage = usage;
3068 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003069}
3070
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003071psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003072{
mohammad16036df908f2018-04-02 08:34:15 -07003073 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003074}
3075
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003076psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003077{
mohammad16036df908f2018-04-02 08:34:15 -07003078 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003079}
mohammad160327010052018-07-03 13:16:15 +03003080#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003081
Gilles Peskinec5487a82018-12-03 18:08:14 +01003082psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003083 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003084{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003085 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003086 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003087
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003088 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003089 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003090
Gilles Peskinec5487a82018-12-03 18:08:14 +01003091 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003092 if( status != PSA_SUCCESS )
3093 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003094
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003095 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3096 PSA_KEY_USAGE_ENCRYPT |
3097 PSA_KEY_USAGE_DECRYPT |
3098 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003099 PSA_KEY_USAGE_VERIFY |
3100 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003101 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003102
mohammad16036df908f2018-04-02 08:34:15 -07003103 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003104
3105 return( PSA_SUCCESS );
3106}
3107
Gilles Peskinec5487a82018-12-03 18:08:14 +01003108psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003109 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003110{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003111 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003112 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003113
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003114 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003115 return( PSA_ERROR_INVALID_ARGUMENT );
3116
Gilles Peskinec5487a82018-12-03 18:08:14 +01003117 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003118 if( status != PSA_SUCCESS )
3119 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003120
mohammad16036df908f2018-04-02 08:34:15 -07003121 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003122
3123 return( PSA_SUCCESS );
3124}
Gilles Peskine20035e32018-02-03 22:44:14 +01003125
Gilles Peskinea0655c32018-04-30 17:06:50 +02003126
3127
mohammad1603804cd712018-03-20 22:44:08 +02003128/****************************************************************/
3129/* Key Lifetime */
3130/****************************************************************/
3131
Gilles Peskinec5487a82018-12-03 18:08:14 +01003132psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003133 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003134{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003135 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003136 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003137
Gilles Peskinec5487a82018-12-03 18:08:14 +01003138 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003139 if( status != PSA_SUCCESS )
3140 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003141
mohammad1603804cd712018-03-20 22:44:08 +02003142 *lifetime = slot->lifetime;
3143
3144 return( PSA_SUCCESS );
3145}
3146
Gilles Peskine20035e32018-02-03 22:44:14 +01003147
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003148
mohammad16035955c982018-04-26 00:53:03 +03003149/****************************************************************/
3150/* AEAD */
3151/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003152
Gilles Peskineedf9a652018-08-17 18:11:56 +02003153typedef struct
3154{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003155 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003156 const mbedtls_cipher_info_t *cipher_info;
3157 union
3158 {
3159#if defined(MBEDTLS_CCM_C)
3160 mbedtls_ccm_context ccm;
3161#endif /* MBEDTLS_CCM_C */
3162#if defined(MBEDTLS_GCM_C)
3163 mbedtls_gcm_context gcm;
3164#endif /* MBEDTLS_GCM_C */
3165 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003166 psa_algorithm_t core_alg;
3167 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003168 uint8_t tag_length;
3169} aead_operation_t;
3170
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003171static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003172{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003173 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003174 {
3175#if defined(MBEDTLS_CCM_C)
3176 case PSA_ALG_CCM:
3177 mbedtls_ccm_free( &operation->ctx.ccm );
3178 break;
3179#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003180#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003181 case PSA_ALG_GCM:
3182 mbedtls_gcm_free( &operation->ctx.gcm );
3183 break;
3184#endif /* MBEDTLS_GCM_C */
3185 }
3186}
3187
3188static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003189 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003190 psa_key_usage_t usage,
3191 psa_algorithm_t alg )
3192{
3193 psa_status_t status;
3194 size_t key_bits;
3195 mbedtls_cipher_id_t cipher_id;
3196
Gilles Peskinec5487a82018-12-03 18:08:14 +01003197 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003198 if( status != PSA_SUCCESS )
3199 return( status );
3200
3201 key_bits = psa_get_key_bits( operation->slot );
3202
3203 operation->cipher_info =
3204 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3205 &cipher_id );
3206 if( operation->cipher_info == NULL )
3207 return( PSA_ERROR_NOT_SUPPORTED );
3208
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003209 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003210 {
3211#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003212 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3213 operation->core_alg = PSA_ALG_CCM;
3214 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003215 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3216 return( PSA_ERROR_INVALID_ARGUMENT );
3217 mbedtls_ccm_init( &operation->ctx.ccm );
3218 status = mbedtls_to_psa_error(
3219 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3220 operation->slot->data.raw.data,
3221 (unsigned int) key_bits ) );
3222 if( status != 0 )
3223 goto cleanup;
3224 break;
3225#endif /* MBEDTLS_CCM_C */
3226
3227#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003228 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3229 operation->core_alg = PSA_ALG_GCM;
3230 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003231 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3232 return( PSA_ERROR_INVALID_ARGUMENT );
3233 mbedtls_gcm_init( &operation->ctx.gcm );
3234 status = mbedtls_to_psa_error(
3235 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3236 operation->slot->data.raw.data,
3237 (unsigned int) key_bits ) );
3238 break;
3239#endif /* MBEDTLS_GCM_C */
3240
3241 default:
3242 return( PSA_ERROR_NOT_SUPPORTED );
3243 }
3244
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003245 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3246 {
3247 status = PSA_ERROR_INVALID_ARGUMENT;
3248 goto cleanup;
3249 }
3250 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3251 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3252 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3253 * In both cases, mbedtls_xxx will validate the tag length below. */
3254
Gilles Peskineedf9a652018-08-17 18:11:56 +02003255 return( PSA_SUCCESS );
3256
3257cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003258 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003259 return( status );
3260}
3261
Gilles Peskinec5487a82018-12-03 18:08:14 +01003262psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003263 psa_algorithm_t alg,
3264 const uint8_t *nonce,
3265 size_t nonce_length,
3266 const uint8_t *additional_data,
3267 size_t additional_data_length,
3268 const uint8_t *plaintext,
3269 size_t plaintext_length,
3270 uint8_t *ciphertext,
3271 size_t ciphertext_size,
3272 size_t *ciphertext_length )
3273{
mohammad16035955c982018-04-26 00:53:03 +03003274 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003275 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003276 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003277
mohammad1603f08a5502018-06-03 15:05:47 +03003278 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003279
Gilles Peskinec5487a82018-12-03 18:08:14 +01003280 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003281 if( status != PSA_SUCCESS )
3282 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003283
Gilles Peskineedf9a652018-08-17 18:11:56 +02003284 /* For all currently supported modes, the tag is at the end of the
3285 * ciphertext. */
3286 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3287 {
3288 status = PSA_ERROR_BUFFER_TOO_SMALL;
3289 goto exit;
3290 }
3291 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003292
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003293#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003294 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003295 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003296 status = mbedtls_to_psa_error(
3297 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3298 MBEDTLS_GCM_ENCRYPT,
3299 plaintext_length,
3300 nonce, nonce_length,
3301 additional_data, additional_data_length,
3302 plaintext, ciphertext,
3303 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003304 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003305 else
3306#endif /* MBEDTLS_GCM_C */
3307#if defined(MBEDTLS_CCM_C)
3308 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003309 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003310 status = mbedtls_to_psa_error(
3311 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3312 plaintext_length,
3313 nonce, nonce_length,
3314 additional_data,
3315 additional_data_length,
3316 plaintext, ciphertext,
3317 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003318 }
mohammad16035c8845f2018-05-09 05:40:09 -07003319 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003320#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003321 {
mohammad1603554faad2018-06-03 15:07:38 +03003322 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003323 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003324
Gilles Peskineedf9a652018-08-17 18:11:56 +02003325 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3326 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003327
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 *ciphertext_length = plaintext_length + operation.tag_length;
3332 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003333}
3334
Gilles Peskineee652a32018-06-01 19:23:52 +02003335/* Locate the tag in a ciphertext buffer containing the encrypted data
3336 * followed by the tag. Return the length of the part preceding the tag in
3337 * *plaintext_length. This is the size of the plaintext in modes where
3338 * the encrypted data has the same size as the plaintext, such as
3339 * CCM and GCM. */
3340static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3341 const uint8_t *ciphertext,
3342 size_t ciphertext_length,
3343 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003344 const uint8_t **p_tag )
3345{
3346 size_t payload_length;
3347 if( tag_length > ciphertext_length )
3348 return( PSA_ERROR_INVALID_ARGUMENT );
3349 payload_length = ciphertext_length - tag_length;
3350 if( payload_length > plaintext_size )
3351 return( PSA_ERROR_BUFFER_TOO_SMALL );
3352 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003353 return( PSA_SUCCESS );
3354}
3355
Gilles Peskinec5487a82018-12-03 18:08:14 +01003356psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003357 psa_algorithm_t alg,
3358 const uint8_t *nonce,
3359 size_t nonce_length,
3360 const uint8_t *additional_data,
3361 size_t additional_data_length,
3362 const uint8_t *ciphertext,
3363 size_t ciphertext_length,
3364 uint8_t *plaintext,
3365 size_t plaintext_size,
3366 size_t *plaintext_length )
3367{
mohammad16035955c982018-04-26 00:53:03 +03003368 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003369 aead_operation_t operation;
3370 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003371
Gilles Peskineee652a32018-06-01 19:23:52 +02003372 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003373
Gilles Peskinec5487a82018-12-03 18:08:14 +01003374 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003375 if( status != PSA_SUCCESS )
3376 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003377
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003378#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003379 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003380 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003381 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003382 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003383 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003384 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003385 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003386
Gilles Peskineedf9a652018-08-17 18:11:56 +02003387 status = mbedtls_to_psa_error(
3388 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3389 ciphertext_length - operation.tag_length,
3390 nonce, nonce_length,
3391 additional_data,
3392 additional_data_length,
3393 tag, operation.tag_length,
3394 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003395 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003396 else
3397#endif /* MBEDTLS_GCM_C */
3398#if defined(MBEDTLS_CCM_C)
3399 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003400 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003401 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003402 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003403 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003404 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003405 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003406
Gilles Peskineedf9a652018-08-17 18:11:56 +02003407 status = mbedtls_to_psa_error(
3408 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3409 ciphertext_length - operation.tag_length,
3410 nonce, nonce_length,
3411 additional_data,
3412 additional_data_length,
3413 ciphertext, plaintext,
3414 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003415 }
mohammad160339574652018-06-01 04:39:53 -07003416 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003417#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003418 {
mohammad1603554faad2018-06-03 15:07:38 +03003419 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003420 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003421
Gilles Peskineedf9a652018-08-17 18:11:56 +02003422 if( status != PSA_SUCCESS && plaintext_size != 0 )
3423 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003424
Gilles Peskineedf9a652018-08-17 18:11:56 +02003425exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003426 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003427 if( status == PSA_SUCCESS )
3428 *plaintext_length = ciphertext_length - operation.tag_length;
3429 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003430}
3431
Gilles Peskinea0655c32018-04-30 17:06:50 +02003432
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003433
Gilles Peskine20035e32018-02-03 22:44:14 +01003434/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003435/* Generators */
3436/****************************************************************/
3437
3438psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3439{
3440 psa_status_t status = PSA_SUCCESS;
3441 if( generator->alg == 0 )
3442 {
3443 /* The object has (apparently) been initialized but it is not
3444 * in use. It's ok to call abort on such an object, and there's
3445 * nothing to do. */
3446 }
3447 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003448 if( generator->alg == PSA_ALG_SELECT_RAW )
3449 {
3450 if( generator->ctx.buffer.data != NULL )
3451 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003452 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003453 generator->ctx.buffer.size );
3454 mbedtls_free( generator->ctx.buffer.data );
3455 }
3456 }
3457 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003458#if defined(MBEDTLS_MD_C)
3459 if( PSA_ALG_IS_HKDF( generator->alg ) )
3460 {
3461 mbedtls_free( generator->ctx.hkdf.info );
3462 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3463 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003464 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3465 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3466 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003467 {
3468 if( generator->ctx.tls12_prf.key != NULL )
3469 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003470 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003471 generator->ctx.tls12_prf.key_len );
3472 mbedtls_free( generator->ctx.tls12_prf.key );
3473 }
Hanno Becker580fba12018-11-13 20:50:45 +00003474
3475 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3476 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003477 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003478 generator->ctx.tls12_prf.Ai_with_seed_len );
3479 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3480 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003481 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003482 else
3483#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003484 {
3485 status = PSA_ERROR_BAD_STATE;
3486 }
3487 memset( generator, 0, sizeof( *generator ) );
3488 return( status );
3489}
3490
3491
3492psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3493 size_t *capacity)
3494{
3495 *capacity = generator->capacity;
3496 return( PSA_SUCCESS );
3497}
3498
Gilles Peskinebef7f142018-07-12 17:22:21 +02003499#if defined(MBEDTLS_MD_C)
3500/* Read some bytes from an HKDF-based generator. This performs a chunk
3501 * of the expand phase of the HKDF algorithm. */
3502static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3503 psa_algorithm_t hash_alg,
3504 uint8_t *output,
3505 size_t output_length )
3506{
3507 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3508 psa_status_t status;
3509
3510 while( output_length != 0 )
3511 {
3512 /* Copy what remains of the current block */
3513 uint8_t n = hash_length - hkdf->offset_in_block;
3514 if( n > output_length )
3515 n = (uint8_t) output_length;
3516 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3517 output += n;
3518 output_length -= n;
3519 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003520 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003521 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003522 /* We can't be wanting more output after block 0xff, otherwise
3523 * the capacity check in psa_generator_read() would have
3524 * prevented this call. It could happen only if the generator
3525 * object was corrupted or if this function is called directly
3526 * inside the library. */
3527 if( hkdf->block_number == 0xff )
3528 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003529
3530 /* We need a new block */
3531 ++hkdf->block_number;
3532 hkdf->offset_in_block = 0;
3533 status = psa_hmac_setup_internal( &hkdf->hmac,
3534 hkdf->prk, hash_length,
3535 hash_alg );
3536 if( status != PSA_SUCCESS )
3537 return( status );
3538 if( hkdf->block_number != 1 )
3539 {
3540 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3541 hkdf->output_block,
3542 hash_length );
3543 if( status != PSA_SUCCESS )
3544 return( status );
3545 }
3546 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3547 hkdf->info,
3548 hkdf->info_length );
3549 if( status != PSA_SUCCESS )
3550 return( status );
3551 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3552 &hkdf->block_number, 1 );
3553 if( status != PSA_SUCCESS )
3554 return( status );
3555 status = psa_hmac_finish_internal( &hkdf->hmac,
3556 hkdf->output_block,
3557 sizeof( hkdf->output_block ) );
3558 if( status != PSA_SUCCESS )
3559 return( status );
3560 }
3561
3562 return( PSA_SUCCESS );
3563}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003564
3565static psa_status_t psa_generator_tls12_prf_generate_next_block(
3566 psa_tls12_prf_generator_t *tls12_prf,
3567 psa_algorithm_t alg )
3568{
3569 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3570 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3571 psa_hmac_internal_data hmac;
3572 psa_status_t status, cleanup_status;
3573
Hanno Becker3b339e22018-11-13 20:56:14 +00003574 unsigned char *Ai;
3575 size_t Ai_len;
3576
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003577 /* We can't be wanting more output after block 0xff, otherwise
3578 * the capacity check in psa_generator_read() would have
3579 * prevented this call. It could happen only if the generator
3580 * object was corrupted or if this function is called directly
3581 * inside the library. */
3582 if( tls12_prf->block_number == 0xff )
3583 return( PSA_ERROR_BAD_STATE );
3584
3585 /* We need a new block */
3586 ++tls12_prf->block_number;
3587 tls12_prf->offset_in_block = 0;
3588
3589 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3590 *
3591 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3592 *
3593 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3594 * HMAC_hash(secret, A(2) + seed) +
3595 * HMAC_hash(secret, A(3) + seed) + ...
3596 *
3597 * A(0) = seed
3598 * A(i) = HMAC_hash( secret, A(i-1) )
3599 *
3600 * The `psa_tls12_prf_generator` structures saves the block
3601 * `HMAC_hash(secret, A(i) + seed)` from which the output
3602 * is currently extracted as `output_block`, while
3603 * `A(i) + seed` is stored in `Ai_with_seed`.
3604 *
3605 * Generating a new block means recalculating `Ai_with_seed`
3606 * from the A(i)-part of it, and afterwards recalculating
3607 * `output_block`.
3608 *
3609 * A(0) is computed at setup time.
3610 *
3611 */
3612
3613 psa_hmac_init_internal( &hmac );
3614
3615 /* We must distinguish the calculation of A(1) from those
3616 * of A(2) and higher, because A(0)=seed has a different
3617 * length than the other A(i). */
3618 if( tls12_prf->block_number == 1 )
3619 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003620 Ai = tls12_prf->Ai_with_seed + hash_length;
3621 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003622 }
3623 else
3624 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003625 Ai = tls12_prf->Ai_with_seed;
3626 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003627 }
3628
Hanno Becker3b339e22018-11-13 20:56:14 +00003629 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3630 status = psa_hmac_setup_internal( &hmac,
3631 tls12_prf->key,
3632 tls12_prf->key_len,
3633 hash_alg );
3634 if( status != PSA_SUCCESS )
3635 goto cleanup;
3636
3637 status = psa_hash_update( &hmac.hash_ctx,
3638 Ai, Ai_len );
3639 if( status != PSA_SUCCESS )
3640 goto cleanup;
3641
3642 status = psa_hmac_finish_internal( &hmac,
3643 tls12_prf->Ai_with_seed,
3644 hash_length );
3645 if( status != PSA_SUCCESS )
3646 goto cleanup;
3647
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003648 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3649 status = psa_hmac_setup_internal( &hmac,
3650 tls12_prf->key,
3651 tls12_prf->key_len,
3652 hash_alg );
3653 if( status != PSA_SUCCESS )
3654 goto cleanup;
3655
3656 status = psa_hash_update( &hmac.hash_ctx,
3657 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003658 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003659 if( status != PSA_SUCCESS )
3660 goto cleanup;
3661
3662 status = psa_hmac_finish_internal( &hmac,
3663 tls12_prf->output_block,
3664 hash_length );
3665 if( status != PSA_SUCCESS )
3666 goto cleanup;
3667
3668cleanup:
3669
3670 cleanup_status = psa_hmac_abort_internal( &hmac );
3671 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3672 status = cleanup_status;
3673
3674 return( status );
3675}
3676
3677/* Read some bytes from an TLS-1.2-PRF-based generator.
3678 * See Section 5 of RFC 5246. */
3679static psa_status_t psa_generator_tls12_prf_read(
3680 psa_tls12_prf_generator_t *tls12_prf,
3681 psa_algorithm_t alg,
3682 uint8_t *output,
3683 size_t output_length )
3684{
3685 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3686 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3687 psa_status_t status;
3688
3689 while( output_length != 0 )
3690 {
3691 /* Copy what remains of the current block */
3692 uint8_t n = hash_length - tls12_prf->offset_in_block;
3693
3694 /* Check if we have fully processed the current block. */
3695 if( n == 0 )
3696 {
3697 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3698 alg );
3699 if( status != PSA_SUCCESS )
3700 return( status );
3701
3702 continue;
3703 }
3704
3705 if( n > output_length )
3706 n = (uint8_t) output_length;
3707 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3708 n );
3709 output += n;
3710 output_length -= n;
3711 tls12_prf->offset_in_block += n;
3712 }
3713
3714 return( PSA_SUCCESS );
3715}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003716#endif /* MBEDTLS_MD_C */
3717
Gilles Peskineeab56e42018-07-12 17:12:33 +02003718psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3719 uint8_t *output,
3720 size_t output_length )
3721{
3722 psa_status_t status;
3723
3724 if( output_length > generator->capacity )
3725 {
3726 generator->capacity = 0;
3727 /* Go through the error path to wipe all confidential data now
3728 * that the generator object is useless. */
3729 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3730 goto exit;
3731 }
3732 if( output_length == 0 &&
3733 generator->capacity == 0 && generator->alg == 0 )
3734 {
3735 /* Edge case: this is a blank or finished generator, and 0
3736 * bytes were requested. The right error in this case could
3737 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3738 * INSUFFICIENT_CAPACITY, which is right for a finished
3739 * generator, for consistency with the case when
3740 * output_length > 0. */
3741 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3742 }
3743 generator->capacity -= output_length;
3744
Gilles Peskine751d9652018-09-18 12:05:44 +02003745 if( generator->alg == PSA_ALG_SELECT_RAW )
3746 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003747 /* Initially, the capacity of a selection generator is always
3748 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3749 * abbreviated in this comment as `size`. When the remaining
3750 * capacity is `c`, the next bytes to serve start `c` bytes
3751 * from the end of the buffer, i.e. `size - c` from the
3752 * beginning of the buffer. Since `generator->capacity` was just
3753 * decremented above, we need to serve the bytes from
3754 * `size - generator->capacity - output_length` to
3755 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003756 size_t offset =
3757 generator->ctx.buffer.size - generator->capacity - output_length;
3758 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3759 status = PSA_SUCCESS;
3760 }
3761 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003762#if defined(MBEDTLS_MD_C)
3763 if( PSA_ALG_IS_HKDF( generator->alg ) )
3764 {
3765 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3766 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3767 output, output_length );
3768 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003769 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3770 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003771 {
3772 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3773 generator->alg, output,
3774 output_length );
3775 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003776 else
3777#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003778 {
3779 return( PSA_ERROR_BAD_STATE );
3780 }
3781
3782exit:
3783 if( status != PSA_SUCCESS )
3784 {
3785 psa_generator_abort( generator );
3786 memset( output, '!', output_length );
3787 }
3788 return( status );
3789}
3790
Gilles Peskine08542d82018-07-19 17:05:42 +02003791#if defined(MBEDTLS_DES_C)
3792static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3793{
3794 if( data_size >= 8 )
3795 mbedtls_des_key_set_parity( data );
3796 if( data_size >= 16 )
3797 mbedtls_des_key_set_parity( data + 8 );
3798 if( data_size >= 24 )
3799 mbedtls_des_key_set_parity( data + 16 );
3800}
3801#endif /* MBEDTLS_DES_C */
3802
Gilles Peskinec5487a82018-12-03 18:08:14 +01003803psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003804 psa_key_type_t type,
3805 size_t bits,
3806 psa_crypto_generator_t *generator )
3807{
3808 uint8_t *data = NULL;
3809 size_t bytes = PSA_BITS_TO_BYTES( bits );
3810 psa_status_t status;
3811
3812 if( ! key_type_is_raw_bytes( type ) )
3813 return( PSA_ERROR_INVALID_ARGUMENT );
3814 if( bits % 8 != 0 )
3815 return( PSA_ERROR_INVALID_ARGUMENT );
3816 data = mbedtls_calloc( 1, bytes );
3817 if( data == NULL )
3818 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3819
3820 status = psa_generator_read( generator, data, bytes );
3821 if( status != PSA_SUCCESS )
3822 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003823#if defined(MBEDTLS_DES_C)
3824 if( type == PSA_KEY_TYPE_DES )
3825 psa_des_set_key_parity( data, bytes );
3826#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003827 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003828
3829exit:
3830 mbedtls_free( data );
3831 return( status );
3832}
3833
3834
3835
3836/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003837/* Key derivation */
3838/****************************************************************/
3839
Gilles Peskinea05219c2018-11-16 16:02:56 +01003840#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003841/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003842 * of the HKDF algorithm.
3843 *
3844 * Note that if this function fails, you must call psa_generator_abort()
3845 * to potentially free embedded data structures and wipe confidential data.
3846 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003847static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003848 const uint8_t *secret,
3849 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003850 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 status = psa_hmac_setup_internal( &hkdf->hmac,
3858 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003859 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003860 if( status != PSA_SUCCESS )
3861 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003862 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003863 if( status != PSA_SUCCESS )
3864 return( status );
3865 status = psa_hmac_finish_internal( &hkdf->hmac,
3866 hkdf->prk,
3867 sizeof( hkdf->prk ) );
3868 if( status != PSA_SUCCESS )
3869 return( status );
3870 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3871 hkdf->block_number = 0;
3872 hkdf->info_length = label_length;
3873 if( label_length != 0 )
3874 {
3875 hkdf->info = mbedtls_calloc( 1, label_length );
3876 if( hkdf->info == NULL )
3877 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3878 memcpy( hkdf->info, label, label_length );
3879 }
3880 return( PSA_SUCCESS );
3881}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003882#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003883
Gilles Peskinea05219c2018-11-16 16:02:56 +01003884#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003885/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3886 *
3887 * Note that if this function fails, you must call psa_generator_abort()
3888 * to potentially free embedded data structures and wipe confidential data.
3889 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003890static psa_status_t psa_generator_tls12_prf_setup(
3891 psa_tls12_prf_generator_t *tls12_prf,
3892 const unsigned char *key,
3893 size_t key_len,
3894 psa_algorithm_t hash_alg,
3895 const uint8_t *salt,
3896 size_t salt_length,
3897 const uint8_t *label,
3898 size_t label_length )
3899{
3900 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003901 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3902 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003903
3904 tls12_prf->key = mbedtls_calloc( 1, key_len );
3905 if( tls12_prf->key == NULL )
3906 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3907 tls12_prf->key_len = key_len;
3908 memcpy( tls12_prf->key, key, key_len );
3909
Hanno Becker580fba12018-11-13 20:50:45 +00003910 overflow = ( salt_length + label_length < salt_length ) ||
3911 ( salt_length + label_length + hash_length < hash_length );
3912 if( overflow )
3913 return( PSA_ERROR_INVALID_ARGUMENT );
3914
3915 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3916 if( tls12_prf->Ai_with_seed == NULL )
3917 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3918 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3919
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003920 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3921 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003922 if( label_length != 0 )
3923 {
3924 memcpy( tls12_prf->Ai_with_seed + hash_length,
3925 label, label_length );
3926 }
3927
3928 if( salt_length != 0 )
3929 {
3930 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3931 salt, salt_length );
3932 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003933
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003934 /* The first block gets generated when
3935 * psa_generator_read() is called. */
3936 tls12_prf->block_number = 0;
3937 tls12_prf->offset_in_block = hash_length;
3938
3939 return( PSA_SUCCESS );
3940}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003941
3942/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3943static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3944 psa_tls12_prf_generator_t *tls12_prf,
3945 const unsigned char *psk,
3946 size_t psk_len,
3947 psa_algorithm_t hash_alg,
3948 const uint8_t *salt,
3949 size_t salt_length,
3950 const uint8_t *label,
3951 size_t label_length )
3952{
3953 psa_status_t status;
3954 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3955
3956 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3957 return( PSA_ERROR_INVALID_ARGUMENT );
3958
3959 /* Quoting RFC 4279, Section 2:
3960 *
3961 * The premaster secret is formed as follows: if the PSK is N octets
3962 * long, concatenate a uint16 with the value N, N zero octets, a second
3963 * uint16 with the value N, and the PSK itself.
3964 */
3965
3966 pms[0] = ( psk_len >> 8 ) & 0xff;
3967 pms[1] = ( psk_len >> 0 ) & 0xff;
3968 memset( pms + 2, 0, psk_len );
3969 pms[2 + psk_len + 0] = pms[0];
3970 pms[2 + psk_len + 1] = pms[1];
3971 memcpy( pms + 4 + psk_len, psk, psk_len );
3972
3973 status = psa_generator_tls12_prf_setup( tls12_prf,
3974 pms, 4 + 2 * psk_len,
3975 hash_alg,
3976 salt, salt_length,
3977 label, label_length );
3978
Gilles Peskine3f108122018-12-07 18:14:53 +01003979 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00003980 return( status );
3981}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003982#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003983
Gilles Peskine346797d2018-11-16 16:05:06 +01003984/* Note that if this function fails, you must call psa_generator_abort()
3985 * to potentially free embedded data structures and wipe confidential data.
3986 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003987static psa_status_t psa_key_derivation_internal(
3988 psa_crypto_generator_t *generator,
3989 const uint8_t *secret, size_t secret_length,
3990 psa_algorithm_t alg,
3991 const uint8_t *salt, size_t salt_length,
3992 const uint8_t *label, size_t label_length,
3993 size_t capacity )
3994{
3995 psa_status_t status;
3996 size_t max_capacity;
3997
3998 /* Set generator->alg even on failure so that abort knows what to do. */
3999 generator->alg = alg;
4000
Gilles Peskine751d9652018-09-18 12:05:44 +02004001 if( alg == PSA_ALG_SELECT_RAW )
4002 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004003 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004004 if( salt_length != 0 )
4005 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004006 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004007 if( label_length != 0 )
4008 return( PSA_ERROR_INVALID_ARGUMENT );
4009 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4010 if( generator->ctx.buffer.data == NULL )
4011 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4012 memcpy( generator->ctx.buffer.data, secret, secret_length );
4013 generator->ctx.buffer.size = secret_length;
4014 max_capacity = secret_length;
4015 status = PSA_SUCCESS;
4016 }
4017 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004018#if defined(MBEDTLS_MD_C)
4019 if( PSA_ALG_IS_HKDF( alg ) )
4020 {
4021 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4022 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4023 if( hash_size == 0 )
4024 return( PSA_ERROR_NOT_SUPPORTED );
4025 max_capacity = 255 * hash_size;
4026 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4027 secret, secret_length,
4028 hash_alg,
4029 salt, salt_length,
4030 label, label_length );
4031 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004032 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4033 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4034 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004035 {
4036 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4037 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4038
4039 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4040 if( hash_alg != PSA_ALG_SHA_256 &&
4041 hash_alg != PSA_ALG_SHA_384 )
4042 {
4043 return( PSA_ERROR_NOT_SUPPORTED );
4044 }
4045
4046 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004047
4048 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4049 {
4050 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4051 secret, secret_length,
4052 hash_alg, salt, salt_length,
4053 label, label_length );
4054 }
4055 else
4056 {
4057 status = psa_generator_tls12_psk_to_ms_setup(
4058 &generator->ctx.tls12_prf,
4059 secret, secret_length,
4060 hash_alg, salt, salt_length,
4061 label, label_length );
4062 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004063 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004064 else
4065#endif
4066 {
4067 return( PSA_ERROR_NOT_SUPPORTED );
4068 }
4069
4070 if( status != PSA_SUCCESS )
4071 return( status );
4072
4073 if( capacity <= max_capacity )
4074 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004075 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4076 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004077 else
4078 return( PSA_ERROR_INVALID_ARGUMENT );
4079
4080 return( PSA_SUCCESS );
4081}
4082
Gilles Peskineea0fb492018-07-12 17:17:20 +02004083psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004084 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004085 psa_algorithm_t alg,
4086 const uint8_t *salt,
4087 size_t salt_length,
4088 const uint8_t *label,
4089 size_t label_length,
4090 size_t capacity )
4091{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004092 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004093 psa_status_t status;
4094
4095 if( generator->alg != 0 )
4096 return( PSA_ERROR_BAD_STATE );
4097
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004098 /* Make sure that alg is a key derivation algorithm. This prevents
4099 * key selection algorithms, which psa_key_derivation_internal
4100 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004101 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4102 return( PSA_ERROR_INVALID_ARGUMENT );
4103
Gilles Peskinec5487a82018-12-03 18:08:14 +01004104 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004105 if( status != PSA_SUCCESS )
4106 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004107
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004108 if( slot->type != PSA_KEY_TYPE_DERIVE )
4109 return( PSA_ERROR_INVALID_ARGUMENT );
4110
4111 status = psa_key_derivation_internal( generator,
4112 slot->data.raw.data,
4113 slot->data.raw.bytes,
4114 alg,
4115 salt, salt_length,
4116 label, label_length,
4117 capacity );
4118 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004119 psa_generator_abort( generator );
4120 return( status );
4121}
4122
4123
4124
4125/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004126/* Key agreement */
4127/****************************************************************/
4128
Gilles Peskinea05219c2018-11-16 16:02:56 +01004129#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004130static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4131 size_t peer_key_length,
4132 const mbedtls_ecp_keypair *our_key,
4133 uint8_t *shared_secret,
4134 size_t shared_secret_size,
4135 size_t *shared_secret_length )
4136{
4137 mbedtls_pk_context pk;
4138 mbedtls_ecp_keypair *their_key = NULL;
4139 mbedtls_ecdh_context ecdh;
4140 int ret;
4141 mbedtls_ecdh_init( &ecdh );
4142 mbedtls_pk_init( &pk );
4143
4144 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4145 if( ret != 0 )
4146 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004147 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004148 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004149 case MBEDTLS_PK_ECKEY:
4150 case MBEDTLS_PK_ECKEY_DH:
4151 break;
4152 default:
4153 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4154 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004155 }
4156 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004157 if( their_key->grp.id != our_key->grp.id )
4158 {
4159 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4160 goto exit;
4161 }
4162
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004163 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4164 if( ret != 0 )
4165 goto exit;
4166 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4167 if( ret != 0 )
4168 goto exit;
4169
4170 ret = mbedtls_ecdh_calc_secret( &ecdh,
4171 shared_secret_length,
4172 shared_secret, shared_secret_size,
4173 mbedtls_ctr_drbg_random,
4174 &global_data.ctr_drbg );
4175
4176exit:
4177 mbedtls_pk_free( &pk );
4178 mbedtls_ecdh_free( &ecdh );
4179 return( mbedtls_to_psa_error( ret ) );
4180}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004181#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004182
Gilles Peskine01d718c2018-09-18 12:01:02 +02004183#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4184
Gilles Peskine346797d2018-11-16 16:05:06 +01004185/* Note that if this function fails, you must call psa_generator_abort()
4186 * to potentially free embedded data structures and wipe confidential data.
4187 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004188static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004189 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004190 const uint8_t *peer_key,
4191 size_t peer_key_length,
4192 psa_algorithm_t alg )
4193{
4194 psa_status_t status;
4195 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4196 size_t shared_secret_length = 0;
4197
4198 /* Step 1: run the secret agreement algorithm to generate the shared
4199 * secret. */
4200 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4201 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004202#if defined(MBEDTLS_ECDH_C)
4203 case PSA_ALG_ECDH_BASE:
4204 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4205 return( PSA_ERROR_INVALID_ARGUMENT );
4206 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4207 private_key->data.ecp,
4208 shared_secret,
4209 sizeof( shared_secret ),
4210 &shared_secret_length );
4211 break;
4212#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004213 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004214 (void) private_key;
4215 (void) peer_key;
4216 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004217 return( PSA_ERROR_NOT_SUPPORTED );
4218 }
4219 if( status != PSA_SUCCESS )
4220 goto exit;
4221
4222 /* Step 2: set up the key derivation to generate key material from
4223 * the shared secret. */
4224 status = psa_key_derivation_internal( generator,
4225 shared_secret, shared_secret_length,
4226 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4227 NULL, 0, NULL, 0,
4228 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4229exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004230 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004231 return( status );
4232}
4233
4234psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004235 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004236 const uint8_t *peer_key,
4237 size_t peer_key_length,
4238 psa_algorithm_t alg )
4239{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004240 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004241 psa_status_t status;
4242 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4243 return( PSA_ERROR_INVALID_ARGUMENT );
4244 status = psa_get_key_from_slot( private_key, &slot,
4245 PSA_KEY_USAGE_DERIVE, alg );
4246 if( status != PSA_SUCCESS )
4247 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004248 status = psa_key_agreement_internal( generator,
4249 slot,
4250 peer_key, peer_key_length,
4251 alg );
4252 if( status != PSA_SUCCESS )
4253 psa_generator_abort( generator );
4254 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004255}
4256
4257
4258
4259/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004260/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004261/****************************************************************/
4262
4263psa_status_t psa_generate_random( uint8_t *output,
4264 size_t output_size )
4265{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004266 int ret;
4267 GUARD_MODULE_INITIALIZED;
4268
4269 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004270 return( mbedtls_to_psa_error( ret ) );
4271}
4272
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004273#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004274
4275/* Support function for error conversion between psa_its error codes to psa crypto */
4276static psa_status_t its_to_psa_error( psa_its_status_t ret )
4277{
4278 switch( ret )
4279 {
4280 case PSA_ITS_SUCCESS:
4281 return( PSA_SUCCESS );
4282
4283 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4284 return( PSA_ERROR_EMPTY_SLOT );
4285
4286 case PSA_ITS_ERROR_STORAGE_FAILURE:
4287 return( PSA_ERROR_STORAGE_FAILURE );
4288
4289 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4290 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4291
4292 case PSA_ITS_ERROR_INVALID_KEY:
Jaeden Amero58600552018-11-30 12:04:38 +00004293 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004294 case PSA_ITS_ERROR_INCORRECT_SIZE:
4295 case PSA_ITS_ERROR_BAD_POINTER:
4296 return( PSA_ERROR_INVALID_ARGUMENT );
4297
4298 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4299 return( PSA_ERROR_NOT_SUPPORTED );
4300
4301 case PSA_ITS_ERROR_WRITE_ONCE:
4302 return( PSA_ERROR_OCCUPIED_SLOT );
4303
4304 default:
4305 return( PSA_ERROR_UNKNOWN_ERROR );
4306 }
4307}
4308
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004309psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4310 size_t seed_size )
4311{
4312 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004313 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004314 struct psa_its_info_t p_info;
4315 if( global_data.initialized )
4316 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004317
4318 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4319 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4320 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4321 return( PSA_ERROR_INVALID_ARGUMENT );
4322
avolinski0d2c2662018-11-21 17:31:07 +02004323 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004324 status = its_to_psa_error( its_status );
4325
4326 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004327 {
avolinski0d2c2662018-11-21 17:31:07 +02004328 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004329 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004330 }
avolinski13beb102018-11-20 16:51:49 +02004331 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004332 {
4333 /* You should not be here. Seed needs to be injected only once */
4334 status = PSA_ERROR_NOT_PERMITTED;
4335 }
4336 return( status );
4337}
4338#endif
4339
Gilles Peskinec5487a82018-12-03 18:08:14 +01004340psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004341 psa_key_type_t type,
4342 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004343 const void *extra,
4344 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004345{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004346 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004347 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004348
Gilles Peskine53d991e2018-07-12 01:14:59 +02004349 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004350 return( PSA_ERROR_INVALID_ARGUMENT );
4351
Gilles Peskinec5487a82018-12-03 18:08:14 +01004352 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004353 if( status != PSA_SUCCESS )
4354 return( status );
4355
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004356 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004357 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004358 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004359 if( status != PSA_SUCCESS )
4360 return( status );
4361 status = psa_generate_random( slot->data.raw.data,
4362 slot->data.raw.bytes );
4363 if( status != PSA_SUCCESS )
4364 {
4365 mbedtls_free( slot->data.raw.data );
4366 return( status );
4367 }
4368#if defined(MBEDTLS_DES_C)
4369 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004370 psa_des_set_key_parity( slot->data.raw.data,
4371 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004372#endif /* MBEDTLS_DES_C */
4373 }
4374 else
4375
4376#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4377 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4378 {
4379 mbedtls_rsa_context *rsa;
4380 int ret;
4381 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004382 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4383 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004384 /* Accept only byte-aligned keys, for the same reasons as
4385 * in psa_import_rsa_key(). */
4386 if( bits % 8 != 0 )
4387 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004388 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004389 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004390 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004391 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004392 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004393#if INT_MAX < 0xffffffff
4394 /* Check that the uint32_t value passed by the caller fits
4395 * in the range supported by this implementation. */
4396 if( p->e > INT_MAX )
4397 return( PSA_ERROR_NOT_SUPPORTED );
4398#endif
4399 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004400 }
4401 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4402 if( rsa == NULL )
4403 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4404 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4405 ret = mbedtls_rsa_gen_key( rsa,
4406 mbedtls_ctr_drbg_random,
4407 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004408 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004409 exponent );
4410 if( ret != 0 )
4411 {
4412 mbedtls_rsa_free( rsa );
4413 mbedtls_free( rsa );
4414 return( mbedtls_to_psa_error( ret ) );
4415 }
4416 slot->data.rsa = rsa;
4417 }
4418 else
4419#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4420
4421#if defined(MBEDTLS_ECP_C)
4422 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4423 {
4424 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4425 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4426 const mbedtls_ecp_curve_info *curve_info =
4427 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4428 mbedtls_ecp_keypair *ecp;
4429 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004430 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004431 return( PSA_ERROR_NOT_SUPPORTED );
4432 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4433 return( PSA_ERROR_NOT_SUPPORTED );
4434 if( curve_info->bit_size != bits )
4435 return( PSA_ERROR_INVALID_ARGUMENT );
4436 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4437 if( ecp == NULL )
4438 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4439 mbedtls_ecp_keypair_init( ecp );
4440 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4441 mbedtls_ctr_drbg_random,
4442 &global_data.ctr_drbg );
4443 if( ret != 0 )
4444 {
4445 mbedtls_ecp_keypair_free( ecp );
4446 mbedtls_free( ecp );
4447 return( mbedtls_to_psa_error( ret ) );
4448 }
4449 slot->data.ecp = ecp;
4450 }
4451 else
4452#endif /* MBEDTLS_ECP_C */
4453
4454 return( PSA_ERROR_NOT_SUPPORTED );
4455
4456 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004457
4458#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4459 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4460 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004461 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004462 }
4463#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4464
4465 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004466}
4467
4468
4469/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004470/* Module setup */
4471/****************************************************************/
4472
Gilles Peskine5e769522018-11-20 21:59:56 +01004473psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4474 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4475 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4476{
4477 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4478 return( PSA_ERROR_BAD_STATE );
4479 global_data.entropy_init = entropy_init;
4480 global_data.entropy_free = entropy_free;
4481 return( PSA_SUCCESS );
4482}
4483
Gilles Peskinee59236f2018-01-27 23:32:46 +01004484void mbedtls_psa_crypto_free( void )
4485{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004486 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004487 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4488 {
4489 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004490 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004491 }
4492 /* Wipe all remaining data, including configuration.
4493 * In particular, this sets all state indicator to the value
4494 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004495 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004496}
4497
4498psa_status_t psa_crypto_init( void )
4499{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004500 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004501 const unsigned char drbg_seed[] = "PSA";
4502
Gilles Peskinec6b69072018-11-20 21:42:52 +01004503 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004504 if( global_data.initialized != 0 )
4505 return( PSA_SUCCESS );
4506
Gilles Peskine5e769522018-11-20 21:59:56 +01004507 /* Set default configuration if
4508 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4509 if( global_data.entropy_init == NULL )
4510 global_data.entropy_init = mbedtls_entropy_init;
4511 if( global_data.entropy_free == NULL )
4512 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004513
Gilles Peskinec6b69072018-11-20 21:42:52 +01004514 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004515 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004516 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004517 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004518 status = mbedtls_to_psa_error(
4519 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4520 mbedtls_entropy_func,
4521 &global_data.entropy,
4522 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4523 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004524 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004525 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004526
Gilles Peskine66fb1262018-12-10 16:29:04 +01004527 status = psa_initialize_key_slots( );
4528 if( status != PSA_SUCCESS )
4529 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004530
4531 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004532 global_data.initialized = 1;
4533
Gilles Peskinee59236f2018-01-27 23:32:46 +01004534exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004535 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004536 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004537 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004538}
4539
4540#endif /* MBEDTLS_PSA_CRYPTO_C */