blob: 8c7dc1e2b5fc9ce2d2cedf851a7bcaedf496e8ef [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)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
42#if defined(MBEDTLS_PLATFORM_C)
43#include "mbedtls/platform.h"
44#else
45#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020050#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000051#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020052#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/blowfish.h"
54#include "mbedtls/camellia.h"
55#include "mbedtls/cipher.h"
56#include "mbedtls/ccm.h"
57#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010058#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020060#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010061#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020063#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/error.h"
65#include "mbedtls/gcm.h"
66#include "mbedtls/md2.h"
67#include "mbedtls/md4.h"
68#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010069#include "mbedtls/md.h"
70#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010071#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010073#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/sha1.h"
77#include "mbedtls/sha256.h"
78#include "mbedtls/sha512.h"
79#include "mbedtls/xtea.h"
80
Netanel Gonen2bcd3122018-11-19 11:53:02 +020081#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
Oren Cohen23a67842019-01-24 14:32:11 +020082#include "psa/internal_trusted_storage.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020083#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010084
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000161 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100162 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
163 return( PSA_ERROR_NOT_SUPPORTED );
164 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
165 return( PSA_ERROR_HARDWARE_FAILURE );
166
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000167 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100168 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
169 return( PSA_ERROR_NOT_SUPPORTED );
170 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
171 return( PSA_ERROR_HARDWARE_FAILURE );
172
173 case MBEDTLS_ERR_CCM_BAD_INPUT:
174 return( PSA_ERROR_INVALID_ARGUMENT );
175 case MBEDTLS_ERR_CCM_AUTH_FAILED:
176 return( PSA_ERROR_INVALID_SIGNATURE );
177 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
181 return( PSA_ERROR_NOT_SUPPORTED );
182 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
183 return( PSA_ERROR_INVALID_ARGUMENT );
184 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
185 return( PSA_ERROR_INSUFFICIENT_MEMORY );
186 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
187 return( PSA_ERROR_INVALID_PADDING );
188 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
189 return( PSA_ERROR_BAD_STATE );
190 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
193 return( PSA_ERROR_TAMPERING_DETECTED );
194 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
195 return( PSA_ERROR_HARDWARE_FAILURE );
196
197 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
198 return( PSA_ERROR_HARDWARE_FAILURE );
199
200 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
202 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
203 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
204 return( PSA_ERROR_NOT_SUPPORTED );
205 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
206 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
207
208 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
209 return( PSA_ERROR_NOT_SUPPORTED );
210 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
Gilles Peskinee59236f2018-01-27 23:32:46 +0100213 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
214 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
215 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
216 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100217
218 case MBEDTLS_ERR_GCM_AUTH_FAILED:
219 return( PSA_ERROR_INVALID_SIGNATURE );
220 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200221 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100222 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
223 return( PSA_ERROR_HARDWARE_FAILURE );
224
225 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
226 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
227 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
228 return( PSA_ERROR_HARDWARE_FAILURE );
229
230 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
231 return( PSA_ERROR_NOT_SUPPORTED );
232 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
233 return( PSA_ERROR_INVALID_ARGUMENT );
234 case MBEDTLS_ERR_MD_ALLOC_FAILED:
235 return( PSA_ERROR_INSUFFICIENT_MEMORY );
236 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
237 return( PSA_ERROR_STORAGE_FAILURE );
238 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
Gilles Peskinef76aa772018-10-29 19:24:33 +0100241 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
242 return( PSA_ERROR_STORAGE_FAILURE );
243 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
244 return( PSA_ERROR_INVALID_ARGUMENT );
245 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
248 return( PSA_ERROR_BUFFER_TOO_SMALL );
249 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
250 return( PSA_ERROR_INVALID_ARGUMENT );
251 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
252 return( PSA_ERROR_INVALID_ARGUMENT );
253 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
254 return( PSA_ERROR_INVALID_ARGUMENT );
255 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
256 return( PSA_ERROR_INSUFFICIENT_MEMORY );
257
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100258 case MBEDTLS_ERR_PK_ALLOC_FAILED:
259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
260 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
261 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100264 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
266 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
269 return( PSA_ERROR_NOT_SUPPORTED );
270 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
271 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
272 return( PSA_ERROR_NOT_PERMITTED );
273 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_PK_INVALID_ALG:
276 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
277 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
278 return( PSA_ERROR_NOT_SUPPORTED );
279 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
280 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
282 return( PSA_ERROR_HARDWARE_FAILURE );
283
284 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
285 return( PSA_ERROR_HARDWARE_FAILURE );
286
287 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_RSA_INVALID_PADDING:
290 return( PSA_ERROR_INVALID_PADDING );
291 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
292 return( PSA_ERROR_HARDWARE_FAILURE );
293 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
294 return( PSA_ERROR_INVALID_ARGUMENT );
295 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
296 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
297 return( PSA_ERROR_TAMPERING_DETECTED );
298 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
299 return( PSA_ERROR_INVALID_SIGNATURE );
300 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
301 return( PSA_ERROR_BUFFER_TOO_SMALL );
302 case MBEDTLS_ERR_RSA_RNG_FAILED:
303 return( PSA_ERROR_INSUFFICIENT_MEMORY );
304 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
305 return( PSA_ERROR_NOT_SUPPORTED );
306 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
307 return( PSA_ERROR_HARDWARE_FAILURE );
308
309 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
310 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
311 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313
314 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
317 return( PSA_ERROR_HARDWARE_FAILURE );
318
itayzafrir5c753392018-05-08 11:18:38 +0300319 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300320 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300321 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300322 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
323 return( PSA_ERROR_BUFFER_TOO_SMALL );
324 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
325 return( PSA_ERROR_NOT_SUPPORTED );
326 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
327 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
328 return( PSA_ERROR_INVALID_SIGNATURE );
329 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
330 return( PSA_ERROR_INSUFFICIENT_MEMORY );
331 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
332 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300333
Gilles Peskinee59236f2018-01-27 23:32:46 +0100334 default:
David Saadab4ecc272019-02-14 13:48:10 +0200335 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100336 }
337}
338
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200339
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200340
341
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100342/****************************************************************/
343/* Key management */
344/****************************************************************/
345
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100346#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200347static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
348{
349 switch( grpid )
350 {
351 case MBEDTLS_ECP_DP_SECP192R1:
352 return( PSA_ECC_CURVE_SECP192R1 );
353 case MBEDTLS_ECP_DP_SECP224R1:
354 return( PSA_ECC_CURVE_SECP224R1 );
355 case MBEDTLS_ECP_DP_SECP256R1:
356 return( PSA_ECC_CURVE_SECP256R1 );
357 case MBEDTLS_ECP_DP_SECP384R1:
358 return( PSA_ECC_CURVE_SECP384R1 );
359 case MBEDTLS_ECP_DP_SECP521R1:
360 return( PSA_ECC_CURVE_SECP521R1 );
361 case MBEDTLS_ECP_DP_BP256R1:
362 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
363 case MBEDTLS_ECP_DP_BP384R1:
364 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
365 case MBEDTLS_ECP_DP_BP512R1:
366 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
367 case MBEDTLS_ECP_DP_CURVE25519:
368 return( PSA_ECC_CURVE_CURVE25519 );
369 case MBEDTLS_ECP_DP_SECP192K1:
370 return( PSA_ECC_CURVE_SECP192K1 );
371 case MBEDTLS_ECP_DP_SECP224K1:
372 return( PSA_ECC_CURVE_SECP224K1 );
373 case MBEDTLS_ECP_DP_SECP256K1:
374 return( PSA_ECC_CURVE_SECP256K1 );
375 case MBEDTLS_ECP_DP_CURVE448:
376 return( PSA_ECC_CURVE_CURVE448 );
377 default:
378 return( 0 );
379 }
380}
381
Gilles Peskine12313cd2018-06-20 00:20:32 +0200382static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
383{
384 switch( curve )
385 {
386 case PSA_ECC_CURVE_SECP192R1:
387 return( MBEDTLS_ECP_DP_SECP192R1 );
388 case PSA_ECC_CURVE_SECP224R1:
389 return( MBEDTLS_ECP_DP_SECP224R1 );
390 case PSA_ECC_CURVE_SECP256R1:
391 return( MBEDTLS_ECP_DP_SECP256R1 );
392 case PSA_ECC_CURVE_SECP384R1:
393 return( MBEDTLS_ECP_DP_SECP384R1 );
394 case PSA_ECC_CURVE_SECP521R1:
395 return( MBEDTLS_ECP_DP_SECP521R1 );
396 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
397 return( MBEDTLS_ECP_DP_BP256R1 );
398 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
399 return( MBEDTLS_ECP_DP_BP384R1 );
400 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
401 return( MBEDTLS_ECP_DP_BP512R1 );
402 case PSA_ECC_CURVE_CURVE25519:
403 return( MBEDTLS_ECP_DP_CURVE25519 );
404 case PSA_ECC_CURVE_SECP192K1:
405 return( MBEDTLS_ECP_DP_SECP192K1 );
406 case PSA_ECC_CURVE_SECP224K1:
407 return( MBEDTLS_ECP_DP_SECP224K1 );
408 case PSA_ECC_CURVE_SECP256K1:
409 return( MBEDTLS_ECP_DP_SECP256K1 );
410 case PSA_ECC_CURVE_CURVE448:
411 return( MBEDTLS_ECP_DP_CURVE448 );
412 default:
413 return( MBEDTLS_ECP_DP_NONE );
414 }
415}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100416#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200417
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200418static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
419 size_t bits,
420 struct raw_data *raw )
421{
422 /* Check that the bit size is acceptable for the key type */
423 switch( type )
424 {
425 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200426 if( bits == 0 )
427 {
428 raw->bytes = 0;
429 raw->data = NULL;
430 return( PSA_SUCCESS );
431 }
432 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200433#if defined(MBEDTLS_MD_C)
434 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200435#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200436 case PSA_KEY_TYPE_DERIVE:
437 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200438#if defined(MBEDTLS_AES_C)
439 case PSA_KEY_TYPE_AES:
440 if( bits != 128 && bits != 192 && bits != 256 )
441 return( PSA_ERROR_INVALID_ARGUMENT );
442 break;
443#endif
444#if defined(MBEDTLS_CAMELLIA_C)
445 case PSA_KEY_TYPE_CAMELLIA:
446 if( bits != 128 && bits != 192 && bits != 256 )
447 return( PSA_ERROR_INVALID_ARGUMENT );
448 break;
449#endif
450#if defined(MBEDTLS_DES_C)
451 case PSA_KEY_TYPE_DES:
452 if( bits != 64 && bits != 128 && bits != 192 )
453 return( PSA_ERROR_INVALID_ARGUMENT );
454 break;
455#endif
456#if defined(MBEDTLS_ARC4_C)
457 case PSA_KEY_TYPE_ARC4:
458 if( bits < 8 || bits > 2048 )
459 return( PSA_ERROR_INVALID_ARGUMENT );
460 break;
461#endif
462 default:
463 return( PSA_ERROR_NOT_SUPPORTED );
464 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200465 if( bits % 8 != 0 )
466 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200467
468 /* Allocate memory for the key */
469 raw->bytes = PSA_BITS_TO_BYTES( bits );
470 raw->data = mbedtls_calloc( 1, raw->bytes );
471 if( raw->data == NULL )
472 {
473 raw->bytes = 0;
474 return( PSA_ERROR_INSUFFICIENT_MEMORY );
475 }
476 return( PSA_SUCCESS );
477}
478
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200479#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100480/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
481 * that are not a multiple of 8) well. For example, there is only
482 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
483 * way to return the exact bit size of a key.
484 * To keep things simple, reject non-byte-aligned key sizes. */
485static psa_status_t psa_check_rsa_key_byte_aligned(
486 const mbedtls_rsa_context *rsa )
487{
488 mbedtls_mpi n;
489 psa_status_t status;
490 mbedtls_mpi_init( &n );
491 status = mbedtls_to_psa_error(
492 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
493 if( status == PSA_SUCCESS )
494 {
495 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
496 status = PSA_ERROR_NOT_SUPPORTED;
497 }
498 mbedtls_mpi_free( &n );
499 return( status );
500}
501
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000502static psa_status_t psa_import_rsa_key( psa_key_type_t type,
503 const uint8_t *data,
504 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200505 mbedtls_rsa_context **p_rsa )
506{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000507 psa_status_t status;
508 mbedtls_pk_context pk;
509 mbedtls_rsa_context *rsa;
510 size_t bits;
511
512 mbedtls_pk_init( &pk );
513
514 /* Parse the data. */
515 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
516 status = mbedtls_to_psa_error(
517 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200518 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000519 status = mbedtls_to_psa_error(
520 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
521 if( status != PSA_SUCCESS )
522 goto exit;
523
524 /* We have something that the pkparse module recognizes. If it is a
525 * valid RSA key, store it. */
526 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200527 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000528 status = PSA_ERROR_INVALID_ARGUMENT;
529 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200530 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000531
532 rsa = mbedtls_pk_rsa( pk );
533 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
534 * supports non-byte-aligned key sizes, but not well. For example,
535 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
536 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
537 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
538 {
539 status = PSA_ERROR_NOT_SUPPORTED;
540 goto exit;
541 }
542 status = psa_check_rsa_key_byte_aligned( rsa );
543
544exit:
545 /* Free the content of the pk object only on error. */
546 if( status != PSA_SUCCESS )
547 {
548 mbedtls_pk_free( &pk );
549 return( status );
550 }
551
552 /* On success, store the content of the object in the RSA context. */
553 *p_rsa = rsa;
554
555 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200556}
557#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
558
Jaeden Ameroccdce902019-01-10 11:42:27 +0000559#if defined(MBEDTLS_ECP_C)
560
561/* Import a public key given as the uncompressed representation defined by SEC1
562 * 2.3.3 as the content of an ECPoint. */
563static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
564 const uint8_t *data,
565 size_t data_length,
566 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200567{
Jaeden Ameroccdce902019-01-10 11:42:27 +0000568 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
569 mbedtls_ecp_keypair *ecp = NULL;
570 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
571
572 *p_ecp = NULL;
573 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
574 if( ecp == NULL )
575 return( PSA_ERROR_INSUFFICIENT_MEMORY );
576 mbedtls_ecp_keypair_init( ecp );
577
578 /* Load the group. */
579 status = mbedtls_to_psa_error(
580 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
581 if( status != PSA_SUCCESS )
582 goto exit;
583 /* Load the public value. */
584 status = mbedtls_to_psa_error(
585 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
586 data, data_length ) );
587 if( status != PSA_SUCCESS )
588 goto exit;
589
590 /* Check that the point is on the curve. */
591 status = mbedtls_to_psa_error(
592 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
593 if( status != PSA_SUCCESS )
594 goto exit;
595
596 *p_ecp = ecp;
597 return( PSA_SUCCESS );
598
599exit:
600 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200601 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000602 mbedtls_ecp_keypair_free( ecp );
603 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200604 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000605 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200606}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000607#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200608
Gilles Peskinef76aa772018-10-29 19:24:33 +0100609#if defined(MBEDTLS_ECP_C)
610/* Import a private key given as a byte string which is the private value
611 * in big-endian order. */
612static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
613 const uint8_t *data,
614 size_t data_length,
615 mbedtls_ecp_keypair **p_ecp )
616{
617 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
618 mbedtls_ecp_keypair *ecp = NULL;
619 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
620
621 *p_ecp = NULL;
622 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
623 if( ecp == NULL )
624 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000625 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100626
627 /* Load the group. */
628 status = mbedtls_to_psa_error(
629 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
630 if( status != PSA_SUCCESS )
631 goto exit;
632 /* Load the secret value. */
633 status = mbedtls_to_psa_error(
634 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
635 if( status != PSA_SUCCESS )
636 goto exit;
637 /* Validate the private key. */
638 status = mbedtls_to_psa_error(
639 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
640 if( status != PSA_SUCCESS )
641 goto exit;
642 /* Calculate the public key from the private key. */
643 status = mbedtls_to_psa_error(
644 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
645 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
646 if( status != PSA_SUCCESS )
647 goto exit;
648
649 *p_ecp = ecp;
650 return( PSA_SUCCESS );
651
652exit:
653 if( ecp != NULL )
654 {
655 mbedtls_ecp_keypair_free( ecp );
656 mbedtls_free( ecp );
657 }
658 return( status );
659}
660#endif /* defined(MBEDTLS_ECP_C) */
661
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100662/** Import key data into a slot. `slot->type` must have been set
663 * previously. This function assumes that the slot does not contain
664 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100665psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
666 const uint8_t *data,
667 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200669 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100670
Darryl Green940d72c2018-07-13 13:18:51 +0100671 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100672 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100673 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674 if( data_length > SIZE_MAX / 8 )
675 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100676 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200677 PSA_BYTES_TO_BITS( data_length ),
678 &slot->data.raw );
679 if( status != PSA_SUCCESS )
680 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200681 if( data_length != 0 )
682 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100683 }
684 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100685#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100686 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100687 {
Darryl Green940d72c2018-07-13 13:18:51 +0100688 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100689 data, data_length,
690 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100691 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000692 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000694 status = psa_import_ec_public_key(
695 PSA_KEY_TYPE_GET_CURVE( slot->type ),
696 data, data_length,
697 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698 }
699 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100700#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000701#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
702 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000704 status = psa_import_rsa_key( slot->type,
705 data, data_length,
706 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 }
708 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000709#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 {
711 return( PSA_ERROR_NOT_SUPPORTED );
712 }
Jaeden Ameroc67200d2019-01-14 13:12:39 +0000713 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100714}
715
Darryl Green06fd18d2018-07-16 11:21:11 +0100716/* Retrieve an empty key slot (slot with no key data, but possibly
717 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100718static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100719 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100720{
721 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100722 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100723
724 *p_slot = NULL;
725
Gilles Peskinec5487a82018-12-03 18:08:14 +0100726 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100727 if( status != PSA_SUCCESS )
728 return( status );
729
730 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200731 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100732
733 *p_slot = slot;
734 return( status );
735}
736
Gilles Peskinef603c712019-01-19 13:40:11 +0100737/** Calculate the intersection of two algorithm usage policies.
738 *
739 * Return 0 (which allows no operation) on incompatibility.
740 */
741static psa_algorithm_t psa_key_policy_algorithm_intersection(
742 psa_algorithm_t alg1,
743 psa_algorithm_t alg2 )
744{
745 /* Common case: the policy only allows alg. */
746 if( alg1 == alg2 )
747 return( alg1 );
748 /* If the policies are from the same hash-and-sign family, check
749 * if one is a wildcard. If so the other has the specific algorithm. */
750 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
751 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
752 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
753 {
754 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
755 return( alg2 );
756 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
757 return( alg1 );
758 }
759 /* If the policies are incompatible, allow nothing. */
760 return( 0 );
761}
762
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100763/** Test whether a policy permits an algorithm.
764 *
765 * The caller must test usage flags separately.
766 */
767static int psa_key_policy_permits( const psa_key_policy_t *policy,
768 psa_algorithm_t alg )
769{
770 /* Common case: the policy only allows alg. */
771 if( alg == policy->alg )
772 return( 1 );
773 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
774 * and alg is the same hash-and-sign family with any hash,
775 * then alg is compliant with policy->alg. */
776 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
777 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
778 {
779 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
780 ( alg & ~PSA_ALG_HASH_MASK ) );
781 }
782 /* If it isn't permitted, it's forbidden. */
783 return( 0 );
784}
785
Gilles Peskinef603c712019-01-19 13:40:11 +0100786/** Restrict a key policy based on a constraint.
787 *
788 * \param[in,out] policy The policy to restrict.
789 * \param[in] constraint The policy constraint to apply.
790 *
791 * \retval #PSA_SUCCESS
792 * \c *policy contains the intersection of the original value of
793 * \c *policy and \c *constraint.
794 * \retval #PSA_ERROR_INVALID_ARGUMENT
795 * \c *policy and \c *constraint are incompatible.
796 * \c *policy is unchanged.
797 */
798static psa_status_t psa_restrict_key_policy(
799 psa_key_policy_t *policy,
800 const psa_key_policy_t *constraint )
801{
802 psa_algorithm_t intersection_alg =
803 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
804 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
805 return( PSA_ERROR_INVALID_ARGUMENT );
806 policy->usage &= constraint->usage;
807 policy->alg = intersection_alg;
808 return( PSA_SUCCESS );
809}
810
Darryl Green06fd18d2018-07-16 11:21:11 +0100811/** Retrieve a slot which must contain a key. The key must have allow all the
812 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
813 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100814static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100815 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100816 psa_key_usage_t usage,
817 psa_algorithm_t alg )
818{
819 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100820 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100821
822 *p_slot = NULL;
823
Gilles Peskinec5487a82018-12-03 18:08:14 +0100824 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100825 if( status != PSA_SUCCESS )
826 return( status );
827 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200828 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100829
830 /* Enforce that usage policy for the key slot contains all the flags
831 * required by the usage parameter. There is one exception: public
832 * keys can always be exported, so we treat public key objects as
833 * if they had the export flag. */
834 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
835 usage &= ~PSA_KEY_USAGE_EXPORT;
836 if( ( slot->policy.usage & usage ) != usage )
837 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100838
839 /* Enforce that the usage policy permits the requested algortihm. */
840 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100841 return( PSA_ERROR_NOT_PERMITTED );
842
843 *p_slot = slot;
844 return( PSA_SUCCESS );
845}
Darryl Green940d72c2018-07-13 13:18:51 +0100846
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100847/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100848static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000849{
850 if( slot->type == PSA_KEY_TYPE_NONE )
851 {
852 /* No key material to clean. */
853 }
854 else if( key_type_is_raw_bytes( slot->type ) )
855 {
856 mbedtls_free( slot->data.raw.data );
857 }
858 else
859#if defined(MBEDTLS_RSA_C)
860 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
861 {
862 mbedtls_rsa_free( slot->data.rsa );
863 mbedtls_free( slot->data.rsa );
864 }
865 else
866#endif /* defined(MBEDTLS_RSA_C) */
867#if defined(MBEDTLS_ECP_C)
868 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
869 {
870 mbedtls_ecp_keypair_free( slot->data.ecp );
871 mbedtls_free( slot->data.ecp );
872 }
873 else
874#endif /* defined(MBEDTLS_ECP_C) */
875 {
876 /* Shouldn't happen: the key type is not any type that we
877 * put in. */
878 return( PSA_ERROR_TAMPERING_DETECTED );
879 }
880
881 return( PSA_SUCCESS );
882}
883
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100884/** Completely wipe a slot in memory, including its policy.
885 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100886psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100887{
888 psa_status_t status = psa_remove_key_data_from_memory( slot );
889 /* At this point, key material and other type-specific content has
890 * been wiped. Clear remaining metadata. We can call memset and not
891 * zeroize because the metadata is not particularly sensitive. */
892 memset( slot, 0, sizeof( *slot ) );
893 return( status );
894}
895
Gilles Peskinec5487a82018-12-03 18:08:14 +0100896psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100897 psa_key_type_t type,
898 const uint8_t *data,
899 size_t data_length )
900{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100901 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100902 psa_status_t status;
903
Gilles Peskinec5487a82018-12-03 18:08:14 +0100904 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100905 if( status != PSA_SUCCESS )
906 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907
908 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100909
910 status = psa_import_key_into_slot( slot, data, data_length );
911 if( status != PSA_SUCCESS )
912 {
913 slot->type = PSA_KEY_TYPE_NONE;
914 return( status );
915 }
916
Darryl Greend49a4992018-06-18 17:27:26 +0100917#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
918 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
919 {
920 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100921 status = psa_save_persistent_key( slot->persistent_storage_id,
922 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100923 data_length );
924 if( status != PSA_SUCCESS )
925 {
926 (void) psa_remove_key_data_from_memory( slot );
927 slot->type = PSA_KEY_TYPE_NONE;
928 }
929 }
930#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
931
932 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100933}
934
Gilles Peskinec5487a82018-12-03 18:08:14 +0100935psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100936{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100937 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100938 psa_status_t status = PSA_SUCCESS;
939 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100940
Gilles Peskinec5487a82018-12-03 18:08:14 +0100941 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100942 if( status != PSA_SUCCESS )
943 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100944#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
945 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
946 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100947 storage_status =
948 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100949 }
950#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100951 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100952 if( status != PSA_SUCCESS )
953 return( status );
954 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100955}
956
Gilles Peskineb870b182018-07-06 16:02:09 +0200957/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100958static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200959{
960 if( key_type_is_raw_bytes( slot->type ) )
961 return( slot->data.raw.bytes * 8 );
962#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200963 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100964 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200965#endif /* defined(MBEDTLS_RSA_C) */
966#if defined(MBEDTLS_ECP_C)
967 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
968 return( slot->data.ecp->grp.pbits );
969#endif /* defined(MBEDTLS_ECP_C) */
970 /* Shouldn't happen except on an empty slot. */
971 return( 0 );
972}
973
Gilles Peskinec5487a82018-12-03 18:08:14 +0100974psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200975 psa_key_type_t *type,
976 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100978 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200979 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200982 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983 if( bits != NULL )
984 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100985 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200986 if( status != PSA_SUCCESS )
987 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200988
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200990 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200991 if( type != NULL )
992 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200993 if( bits != NULL )
994 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 return( PSA_SUCCESS );
996}
997
Jaeden Ameroccdce902019-01-10 11:42:27 +0000998#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +0000999static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1000 unsigned char *buf, size_t size )
1001{
1002 int ret;
1003 unsigned char *c;
1004 size_t len = 0;
1005
1006 c = buf + size;
1007
1008 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1009
1010 return( (int) len );
1011}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001012#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001013
Gilles Peskinef603c712019-01-19 13:40:11 +01001014static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1015 uint8_t *data,
1016 size_t data_size,
1017 size_t *data_length,
1018 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001020 *data_length = 0;
1021
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001022 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001023 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001024
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001025 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001026 {
1027 if( slot->data.raw.bytes > data_size )
1028 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001029 if( data_size != 0 )
1030 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001031 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001032 memset( data + slot->data.raw.bytes, 0,
1033 data_size - slot->data.raw.bytes );
1034 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001035 *data_length = slot->data.raw.bytes;
1036 return( PSA_SUCCESS );
1037 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001038#if defined(MBEDTLS_ECP_C)
1039 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1040 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001041 psa_status_t status;
1042
Gilles Peskine188c71e2018-10-29 19:26:02 +01001043 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1044 if( bytes > data_size )
1045 return( PSA_ERROR_BUFFER_TOO_SMALL );
1046 status = mbedtls_to_psa_error(
1047 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1048 if( status != PSA_SUCCESS )
1049 return( status );
1050 memset( data + bytes, 0, data_size - bytes );
1051 *data_length = bytes;
1052 return( PSA_SUCCESS );
1053 }
1054#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001055 else
Moran Peker17e36e12018-05-02 12:55:20 +03001056 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001057#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001058 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001059 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001060 {
Moran Pekera998bc62018-04-16 18:16:20 +03001061 mbedtls_pk_context pk;
1062 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001063 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001064 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001065#if defined(MBEDTLS_RSA_C)
1066 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001067 pk.pk_info = &mbedtls_rsa_info;
1068 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001069#else
1070 return( PSA_ERROR_NOT_SUPPORTED );
1071#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001072 }
1073 else
1074 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001075#if defined(MBEDTLS_ECP_C)
1076 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001077 pk.pk_info = &mbedtls_eckey_info;
1078 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001079#else
1080 return( PSA_ERROR_NOT_SUPPORTED );
1081#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001082 }
Moran Pekerd7326592018-05-29 16:56:39 +03001083 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001084 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001085 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001086 }
Moran Peker17e36e12018-05-02 12:55:20 +03001087 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001088 {
Moran Peker17e36e12018-05-02 12:55:20 +03001089 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001090 }
Moran Peker60364322018-04-29 11:34:58 +03001091 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001092 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001093 /* If data_size is 0 then data may be NULL and then the
1094 * call to memset would have undefined behavior. */
1095 if( data_size != 0 )
1096 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001097 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001098 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001099 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1100 * Move the data to the beginning and erase remaining data
1101 * at the original location. */
1102 if( 2 * (size_t) ret <= data_size )
1103 {
1104 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001105 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001106 }
1107 else if( (size_t) ret < data_size )
1108 {
1109 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001110 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001111 }
Moran Pekera998bc62018-04-16 18:16:20 +03001112 *data_length = ret;
1113 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001114 }
1115 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001116#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001117 {
1118 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001119 it is valid for a special-purpose implementation to omit
1120 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001121 return( PSA_ERROR_NOT_SUPPORTED );
1122 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001123 }
1124}
1125
Gilles Peskinec5487a82018-12-03 18:08:14 +01001126psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001127 uint8_t *data,
1128 size_t data_size,
1129 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001130{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001131 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001132 psa_status_t status;
1133
1134 /* Set the key to empty now, so that even when there are errors, we always
1135 * set data_length to a value between 0 and data_size. On error, setting
1136 * the key to empty is a good choice because an empty key representation is
1137 * unlikely to be accepted anywhere. */
1138 *data_length = 0;
1139
1140 /* Export requires the EXPORT flag. There is an exception for public keys,
1141 * which don't require any flag, but psa_get_key_from_slot takes
1142 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001143 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001144 if( status != PSA_SUCCESS )
1145 return( status );
1146 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001147 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001148}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001149
Gilles Peskinec5487a82018-12-03 18:08:14 +01001150psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001151 uint8_t *data,
1152 size_t data_size,
1153 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001154{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001155 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001156 psa_status_t status;
1157
1158 /* Set the key to empty now, so that even when there are errors, we always
1159 * set data_length to a value between 0 and data_size. On error, setting
1160 * the key to empty is a good choice because an empty key representation is
1161 * unlikely to be accepted anywhere. */
1162 *data_length = 0;
1163
1164 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001165 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001166 if( status != PSA_SUCCESS )
1167 return( status );
1168 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001169 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001170}
1171
Darryl Green0c6575a2018-11-07 16:05:30 +00001172#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001173static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001174 size_t bits )
1175{
1176 psa_status_t status;
1177 uint8_t *data;
1178 size_t key_length;
1179 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1180 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001181 if( data == NULL )
1182 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001183 /* Get key data in export format */
1184 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1185 if( status != PSA_SUCCESS )
1186 {
1187 slot->type = PSA_KEY_TYPE_NONE;
1188 goto exit;
1189 }
1190 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001191 status = psa_save_persistent_key( slot->persistent_storage_id,
1192 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001193 data, key_length );
1194 if( status != PSA_SUCCESS )
1195 {
1196 slot->type = PSA_KEY_TYPE_NONE;
1197 }
1198exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001199 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001200 mbedtls_free( data );
1201 return( status );
1202}
1203#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1204
Gilles Peskinef603c712019-01-19 13:40:11 +01001205static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1206 psa_key_handle_t target )
1207{
1208 psa_status_t status;
1209 uint8_t *buffer = NULL;
1210 size_t buffer_size = 0;
1211 size_t length;
1212
1213 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1214 psa_get_key_bits( source ) );
1215 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001216 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001217 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001218 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1219 if( status != PSA_SUCCESS )
1220 goto exit;
1221 status = psa_import_key( target, source->type, buffer, length );
1222
1223exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001224 if( buffer_size != 0 )
1225 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001226 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001227 return( status );
1228}
1229
1230psa_status_t psa_copy_key(psa_key_handle_t source_handle,
1231 psa_key_handle_t target_handle,
1232 const psa_key_policy_t *constraint)
1233{
1234 psa_key_slot_t *source_slot = NULL;
1235 psa_key_slot_t *target_slot = NULL;
1236 psa_key_policy_t new_policy;
1237 psa_status_t status;
1238 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1239 if( status != PSA_SUCCESS )
1240 return( status );
1241 status = psa_get_empty_key_slot( target_handle, &target_slot );
1242 if( status != PSA_SUCCESS )
1243 return( status );
1244
1245 new_policy = target_slot->policy;
1246 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1247 if( status != PSA_SUCCESS )
1248 return( status );
1249 if( constraint != NULL )
1250 {
1251 status = psa_restrict_key_policy( &new_policy, constraint );
1252 if( status != PSA_SUCCESS )
1253 return( status );
1254 }
1255
1256 status = psa_copy_key_material( source_slot, target_handle );
1257 if( status != PSA_SUCCESS )
1258 return( status );
1259
1260 target_slot->policy = new_policy;
1261 return( PSA_SUCCESS );
1262}
1263
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001264
1265
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001266/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001267/* Message digests */
1268/****************************************************************/
1269
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001270static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001271{
1272 switch( alg )
1273 {
1274#if defined(MBEDTLS_MD2_C)
1275 case PSA_ALG_MD2:
1276 return( &mbedtls_md2_info );
1277#endif
1278#if defined(MBEDTLS_MD4_C)
1279 case PSA_ALG_MD4:
1280 return( &mbedtls_md4_info );
1281#endif
1282#if defined(MBEDTLS_MD5_C)
1283 case PSA_ALG_MD5:
1284 return( &mbedtls_md5_info );
1285#endif
1286#if defined(MBEDTLS_RIPEMD160_C)
1287 case PSA_ALG_RIPEMD160:
1288 return( &mbedtls_ripemd160_info );
1289#endif
1290#if defined(MBEDTLS_SHA1_C)
1291 case PSA_ALG_SHA_1:
1292 return( &mbedtls_sha1_info );
1293#endif
1294#if defined(MBEDTLS_SHA256_C)
1295 case PSA_ALG_SHA_224:
1296 return( &mbedtls_sha224_info );
1297 case PSA_ALG_SHA_256:
1298 return( &mbedtls_sha256_info );
1299#endif
1300#if defined(MBEDTLS_SHA512_C)
1301 case PSA_ALG_SHA_384:
1302 return( &mbedtls_sha384_info );
1303 case PSA_ALG_SHA_512:
1304 return( &mbedtls_sha512_info );
1305#endif
1306 default:
1307 return( NULL );
1308 }
1309}
1310
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001311psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1312{
1313 switch( operation->alg )
1314 {
Gilles Peskine81736312018-06-26 15:04:31 +02001315 case 0:
1316 /* The object has (apparently) been initialized but it is not
1317 * in use. It's ok to call abort on such an object, and there's
1318 * nothing to do. */
1319 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001320#if defined(MBEDTLS_MD2_C)
1321 case PSA_ALG_MD2:
1322 mbedtls_md2_free( &operation->ctx.md2 );
1323 break;
1324#endif
1325#if defined(MBEDTLS_MD4_C)
1326 case PSA_ALG_MD4:
1327 mbedtls_md4_free( &operation->ctx.md4 );
1328 break;
1329#endif
1330#if defined(MBEDTLS_MD5_C)
1331 case PSA_ALG_MD5:
1332 mbedtls_md5_free( &operation->ctx.md5 );
1333 break;
1334#endif
1335#if defined(MBEDTLS_RIPEMD160_C)
1336 case PSA_ALG_RIPEMD160:
1337 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1338 break;
1339#endif
1340#if defined(MBEDTLS_SHA1_C)
1341 case PSA_ALG_SHA_1:
1342 mbedtls_sha1_free( &operation->ctx.sha1 );
1343 break;
1344#endif
1345#if defined(MBEDTLS_SHA256_C)
1346 case PSA_ALG_SHA_224:
1347 case PSA_ALG_SHA_256:
1348 mbedtls_sha256_free( &operation->ctx.sha256 );
1349 break;
1350#endif
1351#if defined(MBEDTLS_SHA512_C)
1352 case PSA_ALG_SHA_384:
1353 case PSA_ALG_SHA_512:
1354 mbedtls_sha512_free( &operation->ctx.sha512 );
1355 break;
1356#endif
1357 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001358 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001359 }
1360 operation->alg = 0;
1361 return( PSA_SUCCESS );
1362}
1363
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001364psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001365 psa_algorithm_t alg )
1366{
1367 int ret;
1368 operation->alg = 0;
1369 switch( alg )
1370 {
1371#if defined(MBEDTLS_MD2_C)
1372 case PSA_ALG_MD2:
1373 mbedtls_md2_init( &operation->ctx.md2 );
1374 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1375 break;
1376#endif
1377#if defined(MBEDTLS_MD4_C)
1378 case PSA_ALG_MD4:
1379 mbedtls_md4_init( &operation->ctx.md4 );
1380 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1381 break;
1382#endif
1383#if defined(MBEDTLS_MD5_C)
1384 case PSA_ALG_MD5:
1385 mbedtls_md5_init( &operation->ctx.md5 );
1386 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1387 break;
1388#endif
1389#if defined(MBEDTLS_RIPEMD160_C)
1390 case PSA_ALG_RIPEMD160:
1391 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1392 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1393 break;
1394#endif
1395#if defined(MBEDTLS_SHA1_C)
1396 case PSA_ALG_SHA_1:
1397 mbedtls_sha1_init( &operation->ctx.sha1 );
1398 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1399 break;
1400#endif
1401#if defined(MBEDTLS_SHA256_C)
1402 case PSA_ALG_SHA_224:
1403 mbedtls_sha256_init( &operation->ctx.sha256 );
1404 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1405 break;
1406 case PSA_ALG_SHA_256:
1407 mbedtls_sha256_init( &operation->ctx.sha256 );
1408 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1409 break;
1410#endif
1411#if defined(MBEDTLS_SHA512_C)
1412 case PSA_ALG_SHA_384:
1413 mbedtls_sha512_init( &operation->ctx.sha512 );
1414 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1415 break;
1416 case PSA_ALG_SHA_512:
1417 mbedtls_sha512_init( &operation->ctx.sha512 );
1418 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1419 break;
1420#endif
1421 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001422 return( PSA_ALG_IS_HASH( alg ) ?
1423 PSA_ERROR_NOT_SUPPORTED :
1424 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001425 }
1426 if( ret == 0 )
1427 operation->alg = alg;
1428 else
1429 psa_hash_abort( operation );
1430 return( mbedtls_to_psa_error( ret ) );
1431}
1432
1433psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1434 const uint8_t *input,
1435 size_t input_length )
1436{
1437 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001438
1439 /* Don't require hash implementations to behave correctly on a
1440 * zero-length input, which may have an invalid pointer. */
1441 if( input_length == 0 )
1442 return( PSA_SUCCESS );
1443
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001444 switch( operation->alg )
1445 {
1446#if defined(MBEDTLS_MD2_C)
1447 case PSA_ALG_MD2:
1448 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1449 input, input_length );
1450 break;
1451#endif
1452#if defined(MBEDTLS_MD4_C)
1453 case PSA_ALG_MD4:
1454 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1455 input, input_length );
1456 break;
1457#endif
1458#if defined(MBEDTLS_MD5_C)
1459 case PSA_ALG_MD5:
1460 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1461 input, input_length );
1462 break;
1463#endif
1464#if defined(MBEDTLS_RIPEMD160_C)
1465 case PSA_ALG_RIPEMD160:
1466 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1467 input, input_length );
1468 break;
1469#endif
1470#if defined(MBEDTLS_SHA1_C)
1471 case PSA_ALG_SHA_1:
1472 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1473 input, input_length );
1474 break;
1475#endif
1476#if defined(MBEDTLS_SHA256_C)
1477 case PSA_ALG_SHA_224:
1478 case PSA_ALG_SHA_256:
1479 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1480 input, input_length );
1481 break;
1482#endif
1483#if defined(MBEDTLS_SHA512_C)
1484 case PSA_ALG_SHA_384:
1485 case PSA_ALG_SHA_512:
1486 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1487 input, input_length );
1488 break;
1489#endif
1490 default:
1491 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1492 break;
1493 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001494
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001495 if( ret != 0 )
1496 psa_hash_abort( operation );
1497 return( mbedtls_to_psa_error( ret ) );
1498}
1499
1500psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1501 uint8_t *hash,
1502 size_t hash_size,
1503 size_t *hash_length )
1504{
itayzafrir40835d42018-08-02 13:14:17 +03001505 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001506 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001507 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001508
1509 /* Fill the output buffer with something that isn't a valid hash
1510 * (barring an attack on the hash and deliberately-crafted input),
1511 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001512 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001513 /* If hash_size is 0 then hash may be NULL and then the
1514 * call to memset would have undefined behavior. */
1515 if( hash_size != 0 )
1516 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001517
1518 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001519 {
1520 status = PSA_ERROR_BUFFER_TOO_SMALL;
1521 goto exit;
1522 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001523
1524 switch( operation->alg )
1525 {
1526#if defined(MBEDTLS_MD2_C)
1527 case PSA_ALG_MD2:
1528 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1529 break;
1530#endif
1531#if defined(MBEDTLS_MD4_C)
1532 case PSA_ALG_MD4:
1533 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1534 break;
1535#endif
1536#if defined(MBEDTLS_MD5_C)
1537 case PSA_ALG_MD5:
1538 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1539 break;
1540#endif
1541#if defined(MBEDTLS_RIPEMD160_C)
1542 case PSA_ALG_RIPEMD160:
1543 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1544 break;
1545#endif
1546#if defined(MBEDTLS_SHA1_C)
1547 case PSA_ALG_SHA_1:
1548 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1549 break;
1550#endif
1551#if defined(MBEDTLS_SHA256_C)
1552 case PSA_ALG_SHA_224:
1553 case PSA_ALG_SHA_256:
1554 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1555 break;
1556#endif
1557#if defined(MBEDTLS_SHA512_C)
1558 case PSA_ALG_SHA_384:
1559 case PSA_ALG_SHA_512:
1560 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1561 break;
1562#endif
1563 default:
1564 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1565 break;
1566 }
itayzafrir40835d42018-08-02 13:14:17 +03001567 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001568
itayzafrir40835d42018-08-02 13:14:17 +03001569exit:
1570 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001571 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001572 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001573 return( psa_hash_abort( operation ) );
1574 }
1575 else
1576 {
1577 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001578 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001579 }
1580}
1581
Gilles Peskine2d277862018-06-18 15:41:12 +02001582psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1583 const uint8_t *hash,
1584 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001585{
1586 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1587 size_t actual_hash_length;
1588 psa_status_t status = psa_hash_finish( operation,
1589 actual_hash, sizeof( actual_hash ),
1590 &actual_hash_length );
1591 if( status != PSA_SUCCESS )
1592 return( status );
1593 if( actual_hash_length != hash_length )
1594 return( PSA_ERROR_INVALID_SIGNATURE );
1595 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1596 return( PSA_ERROR_INVALID_SIGNATURE );
1597 return( PSA_SUCCESS );
1598}
1599
Gilles Peskineeb35d782019-01-22 17:56:16 +01001600psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1601 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001602{
1603 if( target_operation->alg != 0 )
1604 return( PSA_ERROR_BAD_STATE );
1605
1606 switch( source_operation->alg )
1607 {
1608 case 0:
1609 return( PSA_ERROR_BAD_STATE );
1610#if defined(MBEDTLS_MD2_C)
1611 case PSA_ALG_MD2:
1612 mbedtls_md2_clone( &target_operation->ctx.md2,
1613 &source_operation->ctx.md2 );
1614 break;
1615#endif
1616#if defined(MBEDTLS_MD4_C)
1617 case PSA_ALG_MD4:
1618 mbedtls_md4_clone( &target_operation->ctx.md4,
1619 &source_operation->ctx.md4 );
1620 break;
1621#endif
1622#if defined(MBEDTLS_MD5_C)
1623 case PSA_ALG_MD5:
1624 mbedtls_md5_clone( &target_operation->ctx.md5,
1625 &source_operation->ctx.md5 );
1626 break;
1627#endif
1628#if defined(MBEDTLS_RIPEMD160_C)
1629 case PSA_ALG_RIPEMD160:
1630 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1631 &source_operation->ctx.ripemd160 );
1632 break;
1633#endif
1634#if defined(MBEDTLS_SHA1_C)
1635 case PSA_ALG_SHA_1:
1636 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1637 &source_operation->ctx.sha1 );
1638 break;
1639#endif
1640#if defined(MBEDTLS_SHA256_C)
1641 case PSA_ALG_SHA_224:
1642 case PSA_ALG_SHA_256:
1643 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1644 &source_operation->ctx.sha256 );
1645 break;
1646#endif
1647#if defined(MBEDTLS_SHA512_C)
1648 case PSA_ALG_SHA_384:
1649 case PSA_ALG_SHA_512:
1650 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1651 &source_operation->ctx.sha512 );
1652 break;
1653#endif
1654 default:
1655 return( PSA_ERROR_NOT_SUPPORTED );
1656 }
1657
1658 target_operation->alg = source_operation->alg;
1659 return( PSA_SUCCESS );
1660}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001661
1662
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001663/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001664/* MAC */
1665/****************************************************************/
1666
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001667static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001668 psa_algorithm_t alg,
1669 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001670 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001671 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001672{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001673 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001674 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001675
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001676 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001677 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001678
Gilles Peskine8c9def32018-02-08 10:02:12 +01001679 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1680 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001681 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001682 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001683 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001684 mode = MBEDTLS_MODE_STREAM;
1685 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686 case PSA_ALG_CTR:
1687 mode = MBEDTLS_MODE_CTR;
1688 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001689 case PSA_ALG_CFB:
1690 mode = MBEDTLS_MODE_CFB;
1691 break;
1692 case PSA_ALG_OFB:
1693 mode = MBEDTLS_MODE_OFB;
1694 break;
1695 case PSA_ALG_CBC_NO_PADDING:
1696 mode = MBEDTLS_MODE_CBC;
1697 break;
1698 case PSA_ALG_CBC_PKCS7:
1699 mode = MBEDTLS_MODE_CBC;
1700 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001701 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001702 mode = MBEDTLS_MODE_CCM;
1703 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001704 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001705 mode = MBEDTLS_MODE_GCM;
1706 break;
1707 default:
1708 return( NULL );
1709 }
1710 }
1711 else if( alg == PSA_ALG_CMAC )
1712 mode = MBEDTLS_MODE_ECB;
1713 else if( alg == PSA_ALG_GMAC )
1714 mode = MBEDTLS_MODE_GCM;
1715 else
1716 return( NULL );
1717
1718 switch( key_type )
1719 {
1720 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001721 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001722 break;
1723 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001724 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1725 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001726 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001727 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001728 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001729 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001730 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1731 * but two-key Triple-DES is functionally three-key Triple-DES
1732 * with K1=K3, so that's how we present it to mbedtls. */
1733 if( key_bits == 128 )
1734 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001735 break;
1736 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001737 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001738 break;
1739 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001740 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001741 break;
1742 default:
1743 return( NULL );
1744 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001745 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001746 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001747
Jaeden Amero23bbb752018-06-26 14:16:54 +01001748 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1749 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001750}
1751
Gilles Peskinea05219c2018-11-16 16:02:56 +01001752#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001753static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001754{
Gilles Peskine2d277862018-06-18 15:41:12 +02001755 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001756 {
1757 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001758 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001759 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001760 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001761 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001762 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001763 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001764 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001765 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001766 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001767 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001768 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001769 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001770 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001771 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001772 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001773 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001774 return( 128 );
1775 default:
1776 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001777 }
1778}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001779#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001780
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001781/* Initialize the MAC operation structure. Once this function has been
1782 * called, psa_mac_abort can run and will do the right thing. */
1783static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1784 psa_algorithm_t alg )
1785{
1786 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1787
1788 operation->alg = alg;
1789 operation->key_set = 0;
1790 operation->iv_set = 0;
1791 operation->iv_required = 0;
1792 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001793 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001794
1795#if defined(MBEDTLS_CMAC_C)
1796 if( alg == PSA_ALG_CMAC )
1797 {
1798 operation->iv_required = 0;
1799 mbedtls_cipher_init( &operation->ctx.cmac );
1800 status = PSA_SUCCESS;
1801 }
1802 else
1803#endif /* MBEDTLS_CMAC_C */
1804#if defined(MBEDTLS_MD_C)
1805 if( PSA_ALG_IS_HMAC( operation->alg ) )
1806 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001807 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1808 operation->ctx.hmac.hash_ctx.alg = 0;
1809 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001810 }
1811 else
1812#endif /* MBEDTLS_MD_C */
1813 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001814 if( ! PSA_ALG_IS_MAC( alg ) )
1815 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001816 }
1817
1818 if( status != PSA_SUCCESS )
1819 memset( operation, 0, sizeof( *operation ) );
1820 return( status );
1821}
1822
Gilles Peskine01126fa2018-07-12 17:04:55 +02001823#if defined(MBEDTLS_MD_C)
1824static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1825{
Gilles Peskine3f108122018-12-07 18:14:53 +01001826 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001827 return( psa_hash_abort( &hmac->hash_ctx ) );
1828}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001829
1830static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1831{
1832 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1833 memset( hmac, 0, sizeof( *hmac ) );
1834}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001835#endif /* MBEDTLS_MD_C */
1836
Gilles Peskine8c9def32018-02-08 10:02:12 +01001837psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1838{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001839 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001840 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001841 /* The object has (apparently) been initialized but it is not
1842 * in use. It's ok to call abort on such an object, and there's
1843 * nothing to do. */
1844 return( PSA_SUCCESS );
1845 }
1846 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001847#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001848 if( operation->alg == PSA_ALG_CMAC )
1849 {
1850 mbedtls_cipher_free( &operation->ctx.cmac );
1851 }
1852 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001853#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001854#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001855 if( PSA_ALG_IS_HMAC( operation->alg ) )
1856 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001857 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001858 }
1859 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001860#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001861 {
1862 /* Sanity check (shouldn't happen: operation->alg should
1863 * always have been initialized to a valid value). */
1864 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001865 }
Moran Peker41deec42018-04-04 15:43:05 +03001866
Gilles Peskine8c9def32018-02-08 10:02:12 +01001867 operation->alg = 0;
1868 operation->key_set = 0;
1869 operation->iv_set = 0;
1870 operation->iv_required = 0;
1871 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001872 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001873
Gilles Peskine8c9def32018-02-08 10:02:12 +01001874 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001875
1876bad_state:
1877 /* If abort is called on an uninitialized object, we can't trust
1878 * anything. Wipe the object in case it contains confidential data.
1879 * This may result in a memory leak if a pointer gets overwritten,
1880 * but it's too late to do anything about this. */
1881 memset( operation, 0, sizeof( *operation ) );
1882 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001883}
1884
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001885#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001886static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001887 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001888 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001889 const mbedtls_cipher_info_t *cipher_info )
1890{
1891 int ret;
1892
1893 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001894
1895 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1896 if( ret != 0 )
1897 return( ret );
1898
1899 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1900 slot->data.raw.data,
1901 key_bits );
1902 return( ret );
1903}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001904#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001905
Gilles Peskine248051a2018-06-20 16:09:38 +02001906#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001907static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1908 const uint8_t *key,
1909 size_t key_length,
1910 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001911{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001912 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001913 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001914 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001915 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001916 psa_status_t status;
1917
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001918 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1919 * overflow below. This should never trigger if the hash algorithm
1920 * is implemented correctly. */
1921 /* The size checks against the ipad and opad buffers cannot be written
1922 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1923 * because that triggers -Wlogical-op on GCC 7.3. */
1924 if( block_size > sizeof( ipad ) )
1925 return( PSA_ERROR_NOT_SUPPORTED );
1926 if( block_size > sizeof( hmac->opad ) )
1927 return( PSA_ERROR_NOT_SUPPORTED );
1928 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001929 return( PSA_ERROR_NOT_SUPPORTED );
1930
Gilles Peskined223b522018-06-11 18:12:58 +02001931 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001932 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001933 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001934 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001935 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001936 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001937 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001938 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001939 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001940 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001941 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001942 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001943 }
Gilles Peskine96889972018-07-12 17:07:03 +02001944 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1945 * but it is permitted. It is common when HMAC is used in HKDF, for
1946 * example. Don't call `memcpy` in the 0-length because `key` could be
1947 * an invalid pointer which would make the behavior undefined. */
1948 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001949 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001950
Gilles Peskined223b522018-06-11 18:12:58 +02001951 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1952 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001953 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001954 ipad[i] ^= 0x36;
1955 memset( ipad + key_length, 0x36, block_size - key_length );
1956
1957 /* Copy the key material from ipad to opad, flipping the requisite bits,
1958 * and filling the rest of opad with the requisite constant. */
1959 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001960 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1961 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001962
Gilles Peskine01126fa2018-07-12 17:04:55 +02001963 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001964 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001965 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001966
Gilles Peskine01126fa2018-07-12 17:04:55 +02001967 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001968
1969cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001970 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001971
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001972 return( status );
1973}
Gilles Peskine248051a2018-06-20 16:09:38 +02001974#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001975
Gilles Peskine89167cb2018-07-08 20:12:23 +02001976static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001977 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001978 psa_algorithm_t alg,
1979 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001980{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001981 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001982 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001983 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001984 psa_key_usage_t usage =
1985 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001986 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001987 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001988
Gilles Peskined911eb72018-08-14 15:18:45 +02001989 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001990 if( status != PSA_SUCCESS )
1991 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001992 if( is_sign )
1993 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001994
Gilles Peskinec5487a82018-12-03 18:08:14 +01001995 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001996 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001997 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001998 key_bits = psa_get_key_bits( slot );
1999
Gilles Peskine8c9def32018-02-08 10:02:12 +01002000#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002001 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002002 {
2003 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002004 mbedtls_cipher_info_from_psa( full_length_alg,
2005 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002006 int ret;
2007 if( cipher_info == NULL )
2008 {
2009 status = PSA_ERROR_NOT_SUPPORTED;
2010 goto exit;
2011 }
2012 operation->mac_size = cipher_info->block_size;
2013 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2014 status = mbedtls_to_psa_error( ret );
2015 }
2016 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002017#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002019 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002020 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002021 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002022 if( hash_alg == 0 )
2023 {
2024 status = PSA_ERROR_NOT_SUPPORTED;
2025 goto exit;
2026 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002027
2028 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2029 /* Sanity check. This shouldn't fail on a valid configuration. */
2030 if( operation->mac_size == 0 ||
2031 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2032 {
2033 status = PSA_ERROR_NOT_SUPPORTED;
2034 goto exit;
2035 }
2036
Gilles Peskine01126fa2018-07-12 17:04:55 +02002037 if( slot->type != PSA_KEY_TYPE_HMAC )
2038 {
2039 status = PSA_ERROR_INVALID_ARGUMENT;
2040 goto exit;
2041 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002042
Gilles Peskine01126fa2018-07-12 17:04:55 +02002043 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2044 slot->data.raw.data,
2045 slot->data.raw.bytes,
2046 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002047 }
2048 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002049#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002050 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002051 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002052 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002053 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002054
Gilles Peskined911eb72018-08-14 15:18:45 +02002055 if( truncated == 0 )
2056 {
2057 /* The "normal" case: untruncated algorithm. Nothing to do. */
2058 }
2059 else if( truncated < 4 )
2060 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002061 /* A very short MAC is too short for security since it can be
2062 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2063 * so we make this our minimum, even though 32 bits is still
2064 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002065 status = PSA_ERROR_NOT_SUPPORTED;
2066 }
2067 else if( truncated > operation->mac_size )
2068 {
2069 /* It's impossible to "truncate" to a larger length. */
2070 status = PSA_ERROR_INVALID_ARGUMENT;
2071 }
2072 else
2073 operation->mac_size = truncated;
2074
Gilles Peskinefbfac682018-07-08 20:51:54 +02002075exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002076 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002077 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002078 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002079 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002080 else
2081 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002082 operation->key_set = 1;
2083 }
2084 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002085}
2086
Gilles Peskine89167cb2018-07-08 20:12:23 +02002087psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002088 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002089 psa_algorithm_t alg )
2090{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002091 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002092}
2093
2094psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002095 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002096 psa_algorithm_t alg )
2097{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002098 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002099}
2100
Gilles Peskine8c9def32018-02-08 10:02:12 +01002101psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2102 const uint8_t *input,
2103 size_t input_length )
2104{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002105 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002106 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002107 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002108 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002109 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002110 operation->has_input = 1;
2111
Gilles Peskine8c9def32018-02-08 10:02:12 +01002112#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002113 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002114 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002115 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2116 input, input_length );
2117 status = mbedtls_to_psa_error( ret );
2118 }
2119 else
2120#endif /* MBEDTLS_CMAC_C */
2121#if defined(MBEDTLS_MD_C)
2122 if( PSA_ALG_IS_HMAC( operation->alg ) )
2123 {
2124 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2125 input_length );
2126 }
2127 else
2128#endif /* MBEDTLS_MD_C */
2129 {
2130 /* This shouldn't happen if `operation` was initialized by
2131 * a setup function. */
2132 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002133 }
2134
Gilles Peskinefbfac682018-07-08 20:51:54 +02002135cleanup:
2136 if( status != PSA_SUCCESS )
2137 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002138 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002139}
2140
Gilles Peskine01126fa2018-07-12 17:04:55 +02002141#if defined(MBEDTLS_MD_C)
2142static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2143 uint8_t *mac,
2144 size_t mac_size )
2145{
2146 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2147 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2148 size_t hash_size = 0;
2149 size_t block_size = psa_get_hash_block_size( hash_alg );
2150 psa_status_t status;
2151
Gilles Peskine01126fa2018-07-12 17:04:55 +02002152 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2153 if( status != PSA_SUCCESS )
2154 return( status );
2155 /* From here on, tmp needs to be wiped. */
2156
2157 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2158 if( status != PSA_SUCCESS )
2159 goto exit;
2160
2161 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2162 if( status != PSA_SUCCESS )
2163 goto exit;
2164
2165 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2166 if( status != PSA_SUCCESS )
2167 goto exit;
2168
Gilles Peskined911eb72018-08-14 15:18:45 +02002169 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2170 if( status != PSA_SUCCESS )
2171 goto exit;
2172
2173 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002174
2175exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002176 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002177 return( status );
2178}
2179#endif /* MBEDTLS_MD_C */
2180
mohammad16036df908f2018-04-02 08:34:15 -07002181static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002182 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002183 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002184{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002185 if( ! operation->key_set )
2186 return( PSA_ERROR_BAD_STATE );
2187 if( operation->iv_required && ! operation->iv_set )
2188 return( PSA_ERROR_BAD_STATE );
2189
Gilles Peskine8c9def32018-02-08 10:02:12 +01002190 if( mac_size < operation->mac_size )
2191 return( PSA_ERROR_BUFFER_TOO_SMALL );
2192
Gilles Peskine8c9def32018-02-08 10:02:12 +01002193#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002194 if( operation->alg == PSA_ALG_CMAC )
2195 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002196 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2197 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2198 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002199 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002200 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002201 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002202 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002203 else
2204#endif /* MBEDTLS_CMAC_C */
2205#if defined(MBEDTLS_MD_C)
2206 if( PSA_ALG_IS_HMAC( operation->alg ) )
2207 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002208 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002209 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002210 }
2211 else
2212#endif /* MBEDTLS_MD_C */
2213 {
2214 /* This shouldn't happen if `operation` was initialized by
2215 * a setup function. */
2216 return( PSA_ERROR_BAD_STATE );
2217 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002218}
2219
Gilles Peskineacd4be32018-07-08 19:56:25 +02002220psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2221 uint8_t *mac,
2222 size_t mac_size,
2223 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002224{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002225 psa_status_t status;
2226
2227 /* Fill the output buffer with something that isn't a valid mac
2228 * (barring an attack on the mac and deliberately-crafted input),
2229 * in case the caller doesn't check the return status properly. */
2230 *mac_length = mac_size;
2231 /* If mac_size is 0 then mac may be NULL and then the
2232 * call to memset would have undefined behavior. */
2233 if( mac_size != 0 )
2234 memset( mac, '!', mac_size );
2235
Gilles Peskine89167cb2018-07-08 20:12:23 +02002236 if( ! operation->is_sign )
2237 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002238 status = PSA_ERROR_BAD_STATE;
2239 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002240 }
mohammad16036df908f2018-04-02 08:34:15 -07002241
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002242 status = psa_mac_finish_internal( operation, mac, mac_size );
2243
2244cleanup:
2245 if( status == PSA_SUCCESS )
2246 {
2247 status = psa_mac_abort( operation );
2248 if( status == PSA_SUCCESS )
2249 *mac_length = operation->mac_size;
2250 else
2251 memset( mac, '!', mac_size );
2252 }
2253 else
2254 psa_mac_abort( operation );
2255 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002256}
2257
Gilles Peskineacd4be32018-07-08 19:56:25 +02002258psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2259 const uint8_t *mac,
2260 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002261{
Gilles Peskine828ed142018-06-18 23:25:51 +02002262 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002263 psa_status_t status;
2264
Gilles Peskine89167cb2018-07-08 20:12:23 +02002265 if( operation->is_sign )
2266 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002267 status = PSA_ERROR_BAD_STATE;
2268 goto cleanup;
2269 }
2270 if( operation->mac_size != mac_length )
2271 {
2272 status = PSA_ERROR_INVALID_SIGNATURE;
2273 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002274 }
mohammad16036df908f2018-04-02 08:34:15 -07002275
2276 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002277 actual_mac, sizeof( actual_mac ) );
2278
2279 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2280 status = PSA_ERROR_INVALID_SIGNATURE;
2281
2282cleanup:
2283 if( status == PSA_SUCCESS )
2284 status = psa_mac_abort( operation );
2285 else
2286 psa_mac_abort( operation );
2287
Gilles Peskine3f108122018-12-07 18:14:53 +01002288 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002289
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002290 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002291}
2292
2293
Gilles Peskine20035e32018-02-03 22:44:14 +01002294
Gilles Peskine20035e32018-02-03 22:44:14 +01002295/****************************************************************/
2296/* Asymmetric cryptography */
2297/****************************************************************/
2298
Gilles Peskine2b450e32018-06-27 15:42:46 +02002299#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002300/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002301 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002302static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2303 size_t hash_length,
2304 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002305{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002306 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002307 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002308 *md_alg = mbedtls_md_get_type( md_info );
2309
2310 /* The Mbed TLS RSA module uses an unsigned int for hash length
2311 * parameters. Validate that it fits so that we don't risk an
2312 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002313#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002314 if( hash_length > UINT_MAX )
2315 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002316#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002317
2318#if defined(MBEDTLS_PKCS1_V15)
2319 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2320 * must be correct. */
2321 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2322 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002323 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002324 if( md_info == NULL )
2325 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002326 if( mbedtls_md_get_size( md_info ) != hash_length )
2327 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002328 }
2329#endif /* MBEDTLS_PKCS1_V15 */
2330
2331#if defined(MBEDTLS_PKCS1_V21)
2332 /* PSS requires a hash internally. */
2333 if( PSA_ALG_IS_RSA_PSS( alg ) )
2334 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002335 if( md_info == NULL )
2336 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002337 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002338#endif /* MBEDTLS_PKCS1_V21 */
2339
Gilles Peskine61b91d42018-06-08 16:09:36 +02002340 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002341}
2342
Gilles Peskine2b450e32018-06-27 15:42:46 +02002343static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2344 psa_algorithm_t alg,
2345 const uint8_t *hash,
2346 size_t hash_length,
2347 uint8_t *signature,
2348 size_t signature_size,
2349 size_t *signature_length )
2350{
2351 psa_status_t status;
2352 int ret;
2353 mbedtls_md_type_t md_alg;
2354
2355 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2356 if( status != PSA_SUCCESS )
2357 return( status );
2358
Gilles Peskine630a18a2018-06-29 17:49:35 +02002359 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002360 return( PSA_ERROR_BUFFER_TOO_SMALL );
2361
2362#if defined(MBEDTLS_PKCS1_V15)
2363 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2364 {
2365 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2366 MBEDTLS_MD_NONE );
2367 ret = mbedtls_rsa_pkcs1_sign( rsa,
2368 mbedtls_ctr_drbg_random,
2369 &global_data.ctr_drbg,
2370 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002371 md_alg,
2372 (unsigned int) hash_length,
2373 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002374 signature );
2375 }
2376 else
2377#endif /* MBEDTLS_PKCS1_V15 */
2378#if defined(MBEDTLS_PKCS1_V21)
2379 if( PSA_ALG_IS_RSA_PSS( alg ) )
2380 {
2381 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2382 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2383 mbedtls_ctr_drbg_random,
2384 &global_data.ctr_drbg,
2385 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002386 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002387 (unsigned int) hash_length,
2388 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002389 signature );
2390 }
2391 else
2392#endif /* MBEDTLS_PKCS1_V21 */
2393 {
2394 return( PSA_ERROR_INVALID_ARGUMENT );
2395 }
2396
2397 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002398 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002399 return( mbedtls_to_psa_error( ret ) );
2400}
2401
2402static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2403 psa_algorithm_t alg,
2404 const uint8_t *hash,
2405 size_t hash_length,
2406 const uint8_t *signature,
2407 size_t signature_length )
2408{
2409 psa_status_t status;
2410 int ret;
2411 mbedtls_md_type_t md_alg;
2412
2413 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2414 if( status != PSA_SUCCESS )
2415 return( status );
2416
Gilles Peskine630a18a2018-06-29 17:49:35 +02002417 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002418 return( PSA_ERROR_BUFFER_TOO_SMALL );
2419
2420#if defined(MBEDTLS_PKCS1_V15)
2421 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2422 {
2423 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2424 MBEDTLS_MD_NONE );
2425 ret = mbedtls_rsa_pkcs1_verify( rsa,
2426 mbedtls_ctr_drbg_random,
2427 &global_data.ctr_drbg,
2428 MBEDTLS_RSA_PUBLIC,
2429 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002430 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002431 hash,
2432 signature );
2433 }
2434 else
2435#endif /* MBEDTLS_PKCS1_V15 */
2436#if defined(MBEDTLS_PKCS1_V21)
2437 if( PSA_ALG_IS_RSA_PSS( alg ) )
2438 {
2439 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2440 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2441 mbedtls_ctr_drbg_random,
2442 &global_data.ctr_drbg,
2443 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002444 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002445 (unsigned int) hash_length,
2446 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002447 signature );
2448 }
2449 else
2450#endif /* MBEDTLS_PKCS1_V21 */
2451 {
2452 return( PSA_ERROR_INVALID_ARGUMENT );
2453 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002454
2455 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2456 * the rest of the signature is invalid". This has little use in
2457 * practice and PSA doesn't report this distinction. */
2458 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2459 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002460 return( mbedtls_to_psa_error( ret ) );
2461}
2462#endif /* MBEDTLS_RSA_C */
2463
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002464#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002465/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2466 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2467 * (even though these functions don't modify it). */
2468static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2469 psa_algorithm_t alg,
2470 const uint8_t *hash,
2471 size_t hash_length,
2472 uint8_t *signature,
2473 size_t signature_size,
2474 size_t *signature_length )
2475{
2476 int ret;
2477 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002478 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002479 mbedtls_mpi_init( &r );
2480 mbedtls_mpi_init( &s );
2481
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002482 if( signature_size < 2 * curve_bytes )
2483 {
2484 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2485 goto cleanup;
2486 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002487
Gilles Peskinea05219c2018-11-16 16:02:56 +01002488#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002489 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2490 {
2491 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2492 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2493 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2494 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2495 hash, hash_length,
2496 md_alg ) );
2497 }
2498 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002499#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002500 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002501 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002502 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2503 hash, hash_length,
2504 mbedtls_ctr_drbg_random,
2505 &global_data.ctr_drbg ) );
2506 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002507
2508 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2509 signature,
2510 curve_bytes ) );
2511 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2512 signature + curve_bytes,
2513 curve_bytes ) );
2514
2515cleanup:
2516 mbedtls_mpi_free( &r );
2517 mbedtls_mpi_free( &s );
2518 if( ret == 0 )
2519 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002520 return( mbedtls_to_psa_error( ret ) );
2521}
2522
2523static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2524 const uint8_t *hash,
2525 size_t hash_length,
2526 const uint8_t *signature,
2527 size_t signature_length )
2528{
2529 int ret;
2530 mbedtls_mpi r, s;
2531 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2532 mbedtls_mpi_init( &r );
2533 mbedtls_mpi_init( &s );
2534
2535 if( signature_length != 2 * curve_bytes )
2536 return( PSA_ERROR_INVALID_SIGNATURE );
2537
2538 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2539 signature,
2540 curve_bytes ) );
2541 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2542 signature + curve_bytes,
2543 curve_bytes ) );
2544
2545 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2546 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002547
2548cleanup:
2549 mbedtls_mpi_free( &r );
2550 mbedtls_mpi_free( &s );
2551 return( mbedtls_to_psa_error( ret ) );
2552}
2553#endif /* MBEDTLS_ECDSA_C */
2554
Gilles Peskinec5487a82018-12-03 18:08:14 +01002555psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002556 psa_algorithm_t alg,
2557 const uint8_t *hash,
2558 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002559 uint8_t *signature,
2560 size_t signature_size,
2561 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002562{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002563 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002564 psa_status_t status;
2565
2566 *signature_length = signature_size;
2567
Gilles Peskinec5487a82018-12-03 18:08:14 +01002568 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002569 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002570 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002571 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002572 {
2573 status = PSA_ERROR_INVALID_ARGUMENT;
2574 goto exit;
2575 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002576
Gilles Peskine20035e32018-02-03 22:44:14 +01002577#if defined(MBEDTLS_RSA_C)
2578 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2579 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002580 status = psa_rsa_sign( slot->data.rsa,
2581 alg,
2582 hash, hash_length,
2583 signature, signature_size,
2584 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002585 }
2586 else
2587#endif /* defined(MBEDTLS_RSA_C) */
2588#if defined(MBEDTLS_ECP_C)
2589 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2590 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002591#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002592 if(
2593#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2594 PSA_ALG_IS_ECDSA( alg )
2595#else
2596 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2597#endif
2598 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002599 status = psa_ecdsa_sign( slot->data.ecp,
2600 alg,
2601 hash, hash_length,
2602 signature, signature_size,
2603 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002604 else
2605#endif /* defined(MBEDTLS_ECDSA_C) */
2606 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002607 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002608 }
itayzafrir5c753392018-05-08 11:18:38 +03002609 }
2610 else
2611#endif /* defined(MBEDTLS_ECP_C) */
2612 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002613 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002614 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002615
2616exit:
2617 /* Fill the unused part of the output buffer (the whole buffer on error,
2618 * the trailing part on success) with something that isn't a valid mac
2619 * (barring an attack on the mac and deliberately-crafted input),
2620 * in case the caller doesn't check the return status properly. */
2621 if( status == PSA_SUCCESS )
2622 memset( signature + *signature_length, '!',
2623 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002624 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002625 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002626 /* If signature_size is 0 then we have nothing to do. We must not call
2627 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002628 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002629}
2630
Gilles Peskinec5487a82018-12-03 18:08:14 +01002631psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002632 psa_algorithm_t alg,
2633 const uint8_t *hash,
2634 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002635 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002636 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002637{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002638 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002639 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002640
Gilles Peskinec5487a82018-12-03 18:08:14 +01002641 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002642 if( status != PSA_SUCCESS )
2643 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002644
Gilles Peskine61b91d42018-06-08 16:09:36 +02002645#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002646 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002647 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002648 return( psa_rsa_verify( slot->data.rsa,
2649 alg,
2650 hash, hash_length,
2651 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002652 }
2653 else
2654#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002655#if defined(MBEDTLS_ECP_C)
2656 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2657 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002658#if defined(MBEDTLS_ECDSA_C)
2659 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002660 return( psa_ecdsa_verify( slot->data.ecp,
2661 hash, hash_length,
2662 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002663 else
2664#endif /* defined(MBEDTLS_ECDSA_C) */
2665 {
2666 return( PSA_ERROR_INVALID_ARGUMENT );
2667 }
itayzafrir5c753392018-05-08 11:18:38 +03002668 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002669 else
2670#endif /* defined(MBEDTLS_ECP_C) */
2671 {
2672 return( PSA_ERROR_NOT_SUPPORTED );
2673 }
2674}
2675
Gilles Peskine072ac562018-06-30 00:21:29 +02002676#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2677static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2678 mbedtls_rsa_context *rsa )
2679{
2680 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2681 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2682 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2683 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2684}
2685#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2686
Gilles Peskinec5487a82018-12-03 18:08:14 +01002687psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002688 psa_algorithm_t alg,
2689 const uint8_t *input,
2690 size_t input_length,
2691 const uint8_t *salt,
2692 size_t salt_length,
2693 uint8_t *output,
2694 size_t output_size,
2695 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002696{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002697 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002698 psa_status_t status;
2699
Darryl Green5cc689a2018-07-24 15:34:10 +01002700 (void) input;
2701 (void) input_length;
2702 (void) salt;
2703 (void) output;
2704 (void) output_size;
2705
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002706 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002707
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002708 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2709 return( PSA_ERROR_INVALID_ARGUMENT );
2710
Gilles Peskinec5487a82018-12-03 18:08:14 +01002711 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002712 if( status != PSA_SUCCESS )
2713 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002714 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2715 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002716 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002717
2718#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002719 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002720 {
2721 mbedtls_rsa_context *rsa = slot->data.rsa;
2722 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002723 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002724 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002725#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002726 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002727 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002728 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2729 mbedtls_ctr_drbg_random,
2730 &global_data.ctr_drbg,
2731 MBEDTLS_RSA_PUBLIC,
2732 input_length,
2733 input,
2734 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002735 }
2736 else
2737#endif /* MBEDTLS_PKCS1_V15 */
2738#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002739 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002740 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002741 psa_rsa_oaep_set_padding_mode( alg, rsa );
2742 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2743 mbedtls_ctr_drbg_random,
2744 &global_data.ctr_drbg,
2745 MBEDTLS_RSA_PUBLIC,
2746 salt, salt_length,
2747 input_length,
2748 input,
2749 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002750 }
2751 else
2752#endif /* MBEDTLS_PKCS1_V21 */
2753 {
2754 return( PSA_ERROR_INVALID_ARGUMENT );
2755 }
2756 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002757 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002758 return( mbedtls_to_psa_error( ret ) );
2759 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002760 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002761#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002762 {
2763 return( PSA_ERROR_NOT_SUPPORTED );
2764 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002765}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002766
Gilles Peskinec5487a82018-12-03 18:08:14 +01002767psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002768 psa_algorithm_t alg,
2769 const uint8_t *input,
2770 size_t input_length,
2771 const uint8_t *salt,
2772 size_t salt_length,
2773 uint8_t *output,
2774 size_t output_size,
2775 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002776{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002777 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002778 psa_status_t status;
2779
Darryl Green5cc689a2018-07-24 15:34:10 +01002780 (void) input;
2781 (void) input_length;
2782 (void) salt;
2783 (void) output;
2784 (void) output_size;
2785
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002786 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002787
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002788 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2789 return( PSA_ERROR_INVALID_ARGUMENT );
2790
Gilles Peskinec5487a82018-12-03 18:08:14 +01002791 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002792 if( status != PSA_SUCCESS )
2793 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002794 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2795 return( PSA_ERROR_INVALID_ARGUMENT );
2796
2797#if defined(MBEDTLS_RSA_C)
2798 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2799 {
2800 mbedtls_rsa_context *rsa = slot->data.rsa;
2801 int ret;
2802
Gilles Peskine630a18a2018-06-29 17:49:35 +02002803 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002804 return( PSA_ERROR_INVALID_ARGUMENT );
2805
2806#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002807 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002808 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002809 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2810 mbedtls_ctr_drbg_random,
2811 &global_data.ctr_drbg,
2812 MBEDTLS_RSA_PRIVATE,
2813 output_length,
2814 input,
2815 output,
2816 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002817 }
2818 else
2819#endif /* MBEDTLS_PKCS1_V15 */
2820#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002821 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002822 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002823 psa_rsa_oaep_set_padding_mode( alg, rsa );
2824 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2825 mbedtls_ctr_drbg_random,
2826 &global_data.ctr_drbg,
2827 MBEDTLS_RSA_PRIVATE,
2828 salt, salt_length,
2829 output_length,
2830 input,
2831 output,
2832 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002833 }
2834 else
2835#endif /* MBEDTLS_PKCS1_V21 */
2836 {
2837 return( PSA_ERROR_INVALID_ARGUMENT );
2838 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002839
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002840 return( mbedtls_to_psa_error( ret ) );
2841 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002842 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002843#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002844 {
2845 return( PSA_ERROR_NOT_SUPPORTED );
2846 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002847}
Gilles Peskine20035e32018-02-03 22:44:14 +01002848
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002849
2850
mohammad1603503973b2018-03-12 15:59:30 +02002851/****************************************************************/
2852/* Symmetric cryptography */
2853/****************************************************************/
2854
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002855/* Initialize the cipher operation structure. Once this function has been
2856 * called, psa_cipher_abort can run and will do the right thing. */
2857static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2858 psa_algorithm_t alg )
2859{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002860 if( ! PSA_ALG_IS_CIPHER( alg ) )
2861 {
2862 memset( operation, 0, sizeof( *operation ) );
2863 return( PSA_ERROR_INVALID_ARGUMENT );
2864 }
2865
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002866 operation->alg = alg;
2867 operation->key_set = 0;
2868 operation->iv_set = 0;
2869 operation->iv_required = 1;
2870 operation->iv_size = 0;
2871 operation->block_size = 0;
2872 mbedtls_cipher_init( &operation->ctx.cipher );
2873 return( PSA_SUCCESS );
2874}
2875
Gilles Peskinee553c652018-06-04 16:22:46 +02002876static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002877 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002878 psa_algorithm_t alg,
2879 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002880{
2881 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2882 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002883 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002884 size_t key_bits;
2885 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002886 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2887 PSA_KEY_USAGE_ENCRYPT :
2888 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002889
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002890 status = psa_cipher_init( operation, alg );
2891 if( status != PSA_SUCCESS )
2892 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002893
Gilles Peskinec5487a82018-12-03 18:08:14 +01002894 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002895 if( status != PSA_SUCCESS )
2896 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002897 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002898
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002899 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002900 if( cipher_info == NULL )
2901 return( PSA_ERROR_NOT_SUPPORTED );
2902
mohammad1603503973b2018-03-12 15:59:30 +02002903 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002904 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002905 {
2906 psa_cipher_abort( operation );
2907 return( mbedtls_to_psa_error( ret ) );
2908 }
2909
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002910#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002911 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002912 {
2913 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2914 unsigned char keys[24];
2915 memcpy( keys, slot->data.raw.data, 16 );
2916 memcpy( keys + 16, slot->data.raw.data, 8 );
2917 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2918 keys,
2919 192, cipher_operation );
2920 }
2921 else
2922#endif
2923 {
2924 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2925 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002926 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002927 }
Moran Peker41deec42018-04-04 15:43:05 +03002928 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002929 {
2930 psa_cipher_abort( operation );
2931 return( mbedtls_to_psa_error( ret ) );
2932 }
2933
mohammad16038481e742018-03-18 13:57:31 +02002934#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002935 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002936 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002937 case PSA_ALG_CBC_NO_PADDING:
2938 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2939 MBEDTLS_PADDING_NONE );
2940 break;
2941 case PSA_ALG_CBC_PKCS7:
2942 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2943 MBEDTLS_PADDING_PKCS7 );
2944 break;
2945 default:
2946 /* The algorithm doesn't involve padding. */
2947 ret = 0;
2948 break;
2949 }
2950 if( ret != 0 )
2951 {
2952 psa_cipher_abort( operation );
2953 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002954 }
2955#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2956
mohammad1603503973b2018-03-12 15:59:30 +02002957 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002958 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2959 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2960 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002961 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002962 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002963 }
mohammad1603503973b2018-03-12 15:59:30 +02002964
Moran Peker395db872018-05-31 14:07:14 +03002965 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002966}
2967
Gilles Peskinefe119512018-07-08 21:39:34 +02002968psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002969 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002970 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002971{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002972 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002973}
2974
Gilles Peskinefe119512018-07-08 21:39:34 +02002975psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002976 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002977 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002978{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002979 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002980}
2981
Gilles Peskinefe119512018-07-08 21:39:34 +02002982psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2983 unsigned char *iv,
2984 size_t iv_size,
2985 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002986{
itayzafrir534bd7c2018-08-02 13:56:32 +03002987 psa_status_t status;
2988 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002989 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002990 {
2991 status = PSA_ERROR_BAD_STATE;
2992 goto exit;
2993 }
Moran Peker41deec42018-04-04 15:43:05 +03002994 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002995 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002996 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002997 goto exit;
2998 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002999 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3000 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003001 if( ret != 0 )
3002 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003003 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003004 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003005 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003006
mohammad16038481e742018-03-18 13:57:31 +02003007 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003008 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003009
Moran Peker395db872018-05-31 14:07:14 +03003010exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003011 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003012 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003013 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003014}
3015
Gilles Peskinefe119512018-07-08 21:39:34 +02003016psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3017 const unsigned char *iv,
3018 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003019{
itayzafrir534bd7c2018-08-02 13:56:32 +03003020 psa_status_t status;
3021 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003022 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003023 {
3024 status = PSA_ERROR_BAD_STATE;
3025 goto exit;
3026 }
Moran Pekera28258c2018-05-29 16:25:04 +03003027 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003028 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003029 status = PSA_ERROR_INVALID_ARGUMENT;
3030 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003031 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003032 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3033 status = mbedtls_to_psa_error( ret );
3034exit:
3035 if( status == PSA_SUCCESS )
3036 operation->iv_set = 1;
3037 else
mohammad1603503973b2018-03-12 15:59:30 +02003038 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003039 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003040}
3041
Gilles Peskinee553c652018-06-04 16:22:46 +02003042psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3043 const uint8_t *input,
3044 size_t input_length,
3045 unsigned char *output,
3046 size_t output_size,
3047 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003048{
itayzafrir534bd7c2018-08-02 13:56:32 +03003049 psa_status_t status;
3050 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003051 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003052 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003053 {
3054 /* Take the unprocessed partial block left over from previous
3055 * update calls, if any, plus the input to this call. Remove
3056 * the last partial block, if any. You get the data that will be
3057 * output in this call. */
3058 expected_output_size =
3059 ( operation->ctx.cipher.unprocessed_len + input_length )
3060 / operation->block_size * operation->block_size;
3061 }
3062 else
3063 {
3064 expected_output_size = input_length;
3065 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003066
Gilles Peskine89d789c2018-06-04 17:17:16 +02003067 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003068 {
3069 status = PSA_ERROR_BUFFER_TOO_SMALL;
3070 goto exit;
3071 }
mohammad160382759612018-03-12 18:16:40 +02003072
mohammad1603503973b2018-03-12 15:59:30 +02003073 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003074 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003075 status = mbedtls_to_psa_error( ret );
3076exit:
3077 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003078 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003079 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003080}
3081
Gilles Peskinee553c652018-06-04 16:22:46 +02003082psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3083 uint8_t *output,
3084 size_t output_size,
3085 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003086{
David Saadab4ecc272019-02-14 13:48:10 +02003087 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003088 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003089 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003090
mohammad1603503973b2018-03-12 15:59:30 +02003091 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003092 {
Janos Follath315b51c2018-07-09 16:04:51 +01003093 status = PSA_ERROR_BAD_STATE;
3094 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003095 }
3096 if( operation->iv_required && ! operation->iv_set )
3097 {
Janos Follath315b51c2018-07-09 16:04:51 +01003098 status = PSA_ERROR_BAD_STATE;
3099 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003100 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003101
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003102 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003103 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3104 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003105 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003106 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003107 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003108 }
3109
Janos Follath315b51c2018-07-09 16:04:51 +01003110 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3111 temp_output_buffer,
3112 output_length );
3113 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003114 {
Janos Follath315b51c2018-07-09 16:04:51 +01003115 status = mbedtls_to_psa_error( cipher_ret );
3116 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003117 }
Janos Follath315b51c2018-07-09 16:04:51 +01003118
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003119 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003120 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003121 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003122 memcpy( output, temp_output_buffer, *output_length );
3123 else
3124 {
Janos Follath315b51c2018-07-09 16:04:51 +01003125 status = PSA_ERROR_BUFFER_TOO_SMALL;
3126 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003127 }
mohammad1603503973b2018-03-12 15:59:30 +02003128
Gilles Peskine3f108122018-12-07 18:14:53 +01003129 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003130 status = psa_cipher_abort( operation );
3131
3132 return( status );
3133
3134error:
3135
3136 *output_length = 0;
3137
Gilles Peskine3f108122018-12-07 18:14:53 +01003138 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003139 (void) psa_cipher_abort( operation );
3140
3141 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003142}
3143
Gilles Peskinee553c652018-06-04 16:22:46 +02003144psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3145{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003146 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003147 {
3148 /* The object has (apparently) been initialized but it is not
3149 * in use. It's ok to call abort on such an object, and there's
3150 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003151 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003152 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003153
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003154 /* Sanity check (shouldn't happen: operation->alg should
3155 * always have been initialized to a valid value). */
3156 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3157 return( PSA_ERROR_BAD_STATE );
3158
mohammad1603503973b2018-03-12 15:59:30 +02003159 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003160
Moran Peker41deec42018-04-04 15:43:05 +03003161 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003162 operation->key_set = 0;
3163 operation->iv_set = 0;
3164 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003165 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003166 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003167
Moran Peker395db872018-05-31 14:07:14 +03003168 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003169}
3170
Gilles Peskinea0655c32018-04-30 17:06:50 +02003171
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003172
mohammad16038cc1cee2018-03-28 01:21:33 +03003173/****************************************************************/
3174/* Key Policy */
3175/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003176
mohammad160327010052018-07-03 13:16:15 +03003177#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003178void psa_key_policy_set_usage( psa_key_policy_t *policy,
3179 psa_key_usage_t usage,
3180 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003181{
mohammad16034eed7572018-03-28 05:14:59 -07003182 policy->usage = usage;
3183 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003184}
3185
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003186psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003187{
mohammad16036df908f2018-04-02 08:34:15 -07003188 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003189}
3190
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003191psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003192{
mohammad16036df908f2018-04-02 08:34:15 -07003193 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003194}
mohammad160327010052018-07-03 13:16:15 +03003195#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003196
Gilles Peskinec5487a82018-12-03 18:08:14 +01003197psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003198 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003199{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003200 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003201 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003202
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003203 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003204 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003205
Gilles Peskinec5487a82018-12-03 18:08:14 +01003206 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003207 if( status != PSA_SUCCESS )
3208 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003209
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003210 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3211 PSA_KEY_USAGE_ENCRYPT |
3212 PSA_KEY_USAGE_DECRYPT |
3213 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003214 PSA_KEY_USAGE_VERIFY |
3215 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003216 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003217
mohammad16036df908f2018-04-02 08:34:15 -07003218 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003219
3220 return( PSA_SUCCESS );
3221}
3222
Gilles Peskinec5487a82018-12-03 18:08:14 +01003223psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003224 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003225{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003226 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003227 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003228
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003229 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003230 return( PSA_ERROR_INVALID_ARGUMENT );
3231
Gilles Peskinec5487a82018-12-03 18:08:14 +01003232 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003233 if( status != PSA_SUCCESS )
3234 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003235
mohammad16036df908f2018-04-02 08:34:15 -07003236 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003237
3238 return( PSA_SUCCESS );
3239}
Gilles Peskine20035e32018-02-03 22:44:14 +01003240
Gilles Peskinea0655c32018-04-30 17:06:50 +02003241
3242
mohammad1603804cd712018-03-20 22:44:08 +02003243/****************************************************************/
3244/* Key Lifetime */
3245/****************************************************************/
3246
Gilles Peskinec5487a82018-12-03 18:08:14 +01003247psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003248 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003249{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003250 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003251 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003252
Gilles Peskinec5487a82018-12-03 18:08:14 +01003253 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003254 if( status != PSA_SUCCESS )
3255 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003256
mohammad1603804cd712018-03-20 22:44:08 +02003257 *lifetime = slot->lifetime;
3258
3259 return( PSA_SUCCESS );
3260}
3261
Gilles Peskine20035e32018-02-03 22:44:14 +01003262
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003263
mohammad16035955c982018-04-26 00:53:03 +03003264/****************************************************************/
3265/* AEAD */
3266/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003267
Gilles Peskineedf9a652018-08-17 18:11:56 +02003268typedef struct
3269{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003270 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003271 const mbedtls_cipher_info_t *cipher_info;
3272 union
3273 {
3274#if defined(MBEDTLS_CCM_C)
3275 mbedtls_ccm_context ccm;
3276#endif /* MBEDTLS_CCM_C */
3277#if defined(MBEDTLS_GCM_C)
3278 mbedtls_gcm_context gcm;
3279#endif /* MBEDTLS_GCM_C */
3280 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003281 psa_algorithm_t core_alg;
3282 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003283 uint8_t tag_length;
3284} aead_operation_t;
3285
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003286static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003287{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003288 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003289 {
3290#if defined(MBEDTLS_CCM_C)
3291 case PSA_ALG_CCM:
3292 mbedtls_ccm_free( &operation->ctx.ccm );
3293 break;
3294#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003295#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003296 case PSA_ALG_GCM:
3297 mbedtls_gcm_free( &operation->ctx.gcm );
3298 break;
3299#endif /* MBEDTLS_GCM_C */
3300 }
3301}
3302
3303static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003304 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003305 psa_key_usage_t usage,
3306 psa_algorithm_t alg )
3307{
3308 psa_status_t status;
3309 size_t key_bits;
3310 mbedtls_cipher_id_t cipher_id;
3311
Gilles Peskinec5487a82018-12-03 18:08:14 +01003312 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003313 if( status != PSA_SUCCESS )
3314 return( status );
3315
3316 key_bits = psa_get_key_bits( operation->slot );
3317
3318 operation->cipher_info =
3319 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3320 &cipher_id );
3321 if( operation->cipher_info == NULL )
3322 return( PSA_ERROR_NOT_SUPPORTED );
3323
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003324 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003325 {
3326#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003327 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3328 operation->core_alg = PSA_ALG_CCM;
3329 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003330 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3331 return( PSA_ERROR_INVALID_ARGUMENT );
3332 mbedtls_ccm_init( &operation->ctx.ccm );
3333 status = mbedtls_to_psa_error(
3334 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3335 operation->slot->data.raw.data,
3336 (unsigned int) key_bits ) );
3337 if( status != 0 )
3338 goto cleanup;
3339 break;
3340#endif /* MBEDTLS_CCM_C */
3341
3342#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003343 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3344 operation->core_alg = PSA_ALG_GCM;
3345 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003346 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3347 return( PSA_ERROR_INVALID_ARGUMENT );
3348 mbedtls_gcm_init( &operation->ctx.gcm );
3349 status = mbedtls_to_psa_error(
3350 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3351 operation->slot->data.raw.data,
3352 (unsigned int) key_bits ) );
3353 break;
3354#endif /* MBEDTLS_GCM_C */
3355
3356 default:
3357 return( PSA_ERROR_NOT_SUPPORTED );
3358 }
3359
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003360 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3361 {
3362 status = PSA_ERROR_INVALID_ARGUMENT;
3363 goto cleanup;
3364 }
3365 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3366 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3367 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3368 * In both cases, mbedtls_xxx will validate the tag length below. */
3369
Gilles Peskineedf9a652018-08-17 18:11:56 +02003370 return( PSA_SUCCESS );
3371
3372cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003373 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003374 return( status );
3375}
3376
Gilles Peskinec5487a82018-12-03 18:08:14 +01003377psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003378 psa_algorithm_t alg,
3379 const uint8_t *nonce,
3380 size_t nonce_length,
3381 const uint8_t *additional_data,
3382 size_t additional_data_length,
3383 const uint8_t *plaintext,
3384 size_t plaintext_length,
3385 uint8_t *ciphertext,
3386 size_t ciphertext_size,
3387 size_t *ciphertext_length )
3388{
mohammad16035955c982018-04-26 00:53:03 +03003389 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003390 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003391 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003392
mohammad1603f08a5502018-06-03 15:05:47 +03003393 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003394
Gilles Peskinec5487a82018-12-03 18:08:14 +01003395 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003396 if( status != PSA_SUCCESS )
3397 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003398
Gilles Peskineedf9a652018-08-17 18:11:56 +02003399 /* For all currently supported modes, the tag is at the end of the
3400 * ciphertext. */
3401 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3402 {
3403 status = PSA_ERROR_BUFFER_TOO_SMALL;
3404 goto exit;
3405 }
3406 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003407
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003408#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003409 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003410 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003411 status = mbedtls_to_psa_error(
3412 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3413 MBEDTLS_GCM_ENCRYPT,
3414 plaintext_length,
3415 nonce, nonce_length,
3416 additional_data, additional_data_length,
3417 plaintext, ciphertext,
3418 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003419 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003420 else
3421#endif /* MBEDTLS_GCM_C */
3422#if defined(MBEDTLS_CCM_C)
3423 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003424 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003425 status = mbedtls_to_psa_error(
3426 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3427 plaintext_length,
3428 nonce, nonce_length,
3429 additional_data,
3430 additional_data_length,
3431 plaintext, ciphertext,
3432 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003433 }
mohammad16035c8845f2018-05-09 05:40:09 -07003434 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003435#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003436 {
mohammad1603554faad2018-06-03 15:07:38 +03003437 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003438 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003439
Gilles Peskineedf9a652018-08-17 18:11:56 +02003440 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3441 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003442
Gilles Peskineedf9a652018-08-17 18:11:56 +02003443exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003444 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003445 if( status == PSA_SUCCESS )
3446 *ciphertext_length = plaintext_length + operation.tag_length;
3447 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003448}
3449
Gilles Peskineee652a32018-06-01 19:23:52 +02003450/* Locate the tag in a ciphertext buffer containing the encrypted data
3451 * followed by the tag. Return the length of the part preceding the tag in
3452 * *plaintext_length. This is the size of the plaintext in modes where
3453 * the encrypted data has the same size as the plaintext, such as
3454 * CCM and GCM. */
3455static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3456 const uint8_t *ciphertext,
3457 size_t ciphertext_length,
3458 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003459 const uint8_t **p_tag )
3460{
3461 size_t payload_length;
3462 if( tag_length > ciphertext_length )
3463 return( PSA_ERROR_INVALID_ARGUMENT );
3464 payload_length = ciphertext_length - tag_length;
3465 if( payload_length > plaintext_size )
3466 return( PSA_ERROR_BUFFER_TOO_SMALL );
3467 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003468 return( PSA_SUCCESS );
3469}
3470
Gilles Peskinec5487a82018-12-03 18:08:14 +01003471psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003472 psa_algorithm_t alg,
3473 const uint8_t *nonce,
3474 size_t nonce_length,
3475 const uint8_t *additional_data,
3476 size_t additional_data_length,
3477 const uint8_t *ciphertext,
3478 size_t ciphertext_length,
3479 uint8_t *plaintext,
3480 size_t plaintext_size,
3481 size_t *plaintext_length )
3482{
mohammad16035955c982018-04-26 00:53:03 +03003483 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003484 aead_operation_t operation;
3485 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003486
Gilles Peskineee652a32018-06-01 19:23:52 +02003487 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003488
Gilles Peskinec5487a82018-12-03 18:08:14 +01003489 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003490 if( status != PSA_SUCCESS )
3491 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003492
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003493#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003494 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003495 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003496 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003497 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003498 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003499 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003500 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003501
Gilles Peskineedf9a652018-08-17 18:11:56 +02003502 status = mbedtls_to_psa_error(
3503 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3504 ciphertext_length - operation.tag_length,
3505 nonce, nonce_length,
3506 additional_data,
3507 additional_data_length,
3508 tag, operation.tag_length,
3509 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003510 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003511 else
3512#endif /* MBEDTLS_GCM_C */
3513#if defined(MBEDTLS_CCM_C)
3514 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003515 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003516 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003517 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003518 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003519 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003520 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003521
Gilles Peskineedf9a652018-08-17 18:11:56 +02003522 status = mbedtls_to_psa_error(
3523 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3524 ciphertext_length - operation.tag_length,
3525 nonce, nonce_length,
3526 additional_data,
3527 additional_data_length,
3528 ciphertext, plaintext,
3529 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003530 }
mohammad160339574652018-06-01 04:39:53 -07003531 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003532#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003533 {
mohammad1603554faad2018-06-03 15:07:38 +03003534 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003535 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003536
Gilles Peskineedf9a652018-08-17 18:11:56 +02003537 if( status != PSA_SUCCESS && plaintext_size != 0 )
3538 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003539
Gilles Peskineedf9a652018-08-17 18:11:56 +02003540exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003541 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003542 if( status == PSA_SUCCESS )
3543 *plaintext_length = ciphertext_length - operation.tag_length;
3544 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003545}
3546
Gilles Peskinea0655c32018-04-30 17:06:50 +02003547
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003548
Gilles Peskine20035e32018-02-03 22:44:14 +01003549/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003550/* Generators */
3551/****************************************************************/
3552
3553psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3554{
3555 psa_status_t status = PSA_SUCCESS;
3556 if( generator->alg == 0 )
3557 {
3558 /* The object has (apparently) been initialized but it is not
3559 * in use. It's ok to call abort on such an object, and there's
3560 * nothing to do. */
3561 }
3562 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003563 if( generator->alg == PSA_ALG_SELECT_RAW )
3564 {
3565 if( generator->ctx.buffer.data != NULL )
3566 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003567 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003568 generator->ctx.buffer.size );
3569 mbedtls_free( generator->ctx.buffer.data );
3570 }
3571 }
3572 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003573#if defined(MBEDTLS_MD_C)
3574 if( PSA_ALG_IS_HKDF( generator->alg ) )
3575 {
3576 mbedtls_free( generator->ctx.hkdf.info );
3577 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3578 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003579 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3580 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3581 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003582 {
3583 if( generator->ctx.tls12_prf.key != NULL )
3584 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003585 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003586 generator->ctx.tls12_prf.key_len );
3587 mbedtls_free( generator->ctx.tls12_prf.key );
3588 }
Hanno Becker580fba12018-11-13 20:50:45 +00003589
3590 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3591 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003592 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003593 generator->ctx.tls12_prf.Ai_with_seed_len );
3594 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3595 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003596 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003597 else
3598#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003599 {
3600 status = PSA_ERROR_BAD_STATE;
3601 }
3602 memset( generator, 0, sizeof( *generator ) );
3603 return( status );
3604}
3605
3606
3607psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3608 size_t *capacity)
3609{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003610 if( generator->alg == 0 )
3611 {
3612 /* This is a blank generator. */
3613 return PSA_ERROR_BAD_STATE;
3614 }
3615
Gilles Peskineeab56e42018-07-12 17:12:33 +02003616 *capacity = generator->capacity;
3617 return( PSA_SUCCESS );
3618}
3619
Gilles Peskinebef7f142018-07-12 17:22:21 +02003620#if defined(MBEDTLS_MD_C)
3621/* Read some bytes from an HKDF-based generator. This performs a chunk
3622 * of the expand phase of the HKDF algorithm. */
3623static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3624 psa_algorithm_t hash_alg,
3625 uint8_t *output,
3626 size_t output_length )
3627{
3628 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3629 psa_status_t status;
3630
3631 while( output_length != 0 )
3632 {
3633 /* Copy what remains of the current block */
3634 uint8_t n = hash_length - hkdf->offset_in_block;
3635 if( n > output_length )
3636 n = (uint8_t) output_length;
3637 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3638 output += n;
3639 output_length -= n;
3640 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003641 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003642 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003643 /* We can't be wanting more output after block 0xff, otherwise
3644 * the capacity check in psa_generator_read() would have
3645 * prevented this call. It could happen only if the generator
3646 * object was corrupted or if this function is called directly
3647 * inside the library. */
3648 if( hkdf->block_number == 0xff )
3649 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003650
3651 /* We need a new block */
3652 ++hkdf->block_number;
3653 hkdf->offset_in_block = 0;
3654 status = psa_hmac_setup_internal( &hkdf->hmac,
3655 hkdf->prk, hash_length,
3656 hash_alg );
3657 if( status != PSA_SUCCESS )
3658 return( status );
3659 if( hkdf->block_number != 1 )
3660 {
3661 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3662 hkdf->output_block,
3663 hash_length );
3664 if( status != PSA_SUCCESS )
3665 return( status );
3666 }
3667 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3668 hkdf->info,
3669 hkdf->info_length );
3670 if( status != PSA_SUCCESS )
3671 return( status );
3672 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3673 &hkdf->block_number, 1 );
3674 if( status != PSA_SUCCESS )
3675 return( status );
3676 status = psa_hmac_finish_internal( &hkdf->hmac,
3677 hkdf->output_block,
3678 sizeof( hkdf->output_block ) );
3679 if( status != PSA_SUCCESS )
3680 return( status );
3681 }
3682
3683 return( PSA_SUCCESS );
3684}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003685
3686static psa_status_t psa_generator_tls12_prf_generate_next_block(
3687 psa_tls12_prf_generator_t *tls12_prf,
3688 psa_algorithm_t alg )
3689{
3690 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3691 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3692 psa_hmac_internal_data hmac;
3693 psa_status_t status, cleanup_status;
3694
Hanno Becker3b339e22018-11-13 20:56:14 +00003695 unsigned char *Ai;
3696 size_t Ai_len;
3697
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003698 /* We can't be wanting more output after block 0xff, otherwise
3699 * the capacity check in psa_generator_read() would have
3700 * prevented this call. It could happen only if the generator
3701 * object was corrupted or if this function is called directly
3702 * inside the library. */
3703 if( tls12_prf->block_number == 0xff )
3704 return( PSA_ERROR_BAD_STATE );
3705
3706 /* We need a new block */
3707 ++tls12_prf->block_number;
3708 tls12_prf->offset_in_block = 0;
3709
3710 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3711 *
3712 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3713 *
3714 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3715 * HMAC_hash(secret, A(2) + seed) +
3716 * HMAC_hash(secret, A(3) + seed) + ...
3717 *
3718 * A(0) = seed
3719 * A(i) = HMAC_hash( secret, A(i-1) )
3720 *
3721 * The `psa_tls12_prf_generator` structures saves the block
3722 * `HMAC_hash(secret, A(i) + seed)` from which the output
3723 * is currently extracted as `output_block`, while
3724 * `A(i) + seed` is stored in `Ai_with_seed`.
3725 *
3726 * Generating a new block means recalculating `Ai_with_seed`
3727 * from the A(i)-part of it, and afterwards recalculating
3728 * `output_block`.
3729 *
3730 * A(0) is computed at setup time.
3731 *
3732 */
3733
3734 psa_hmac_init_internal( &hmac );
3735
3736 /* We must distinguish the calculation of A(1) from those
3737 * of A(2) and higher, because A(0)=seed has a different
3738 * length than the other A(i). */
3739 if( tls12_prf->block_number == 1 )
3740 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003741 Ai = tls12_prf->Ai_with_seed + hash_length;
3742 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003743 }
3744 else
3745 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003746 Ai = tls12_prf->Ai_with_seed;
3747 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003748 }
3749
Hanno Becker3b339e22018-11-13 20:56:14 +00003750 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3751 status = psa_hmac_setup_internal( &hmac,
3752 tls12_prf->key,
3753 tls12_prf->key_len,
3754 hash_alg );
3755 if( status != PSA_SUCCESS )
3756 goto cleanup;
3757
3758 status = psa_hash_update( &hmac.hash_ctx,
3759 Ai, Ai_len );
3760 if( status != PSA_SUCCESS )
3761 goto cleanup;
3762
3763 status = psa_hmac_finish_internal( &hmac,
3764 tls12_prf->Ai_with_seed,
3765 hash_length );
3766 if( status != PSA_SUCCESS )
3767 goto cleanup;
3768
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003769 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3770 status = psa_hmac_setup_internal( &hmac,
3771 tls12_prf->key,
3772 tls12_prf->key_len,
3773 hash_alg );
3774 if( status != PSA_SUCCESS )
3775 goto cleanup;
3776
3777 status = psa_hash_update( &hmac.hash_ctx,
3778 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003779 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003780 if( status != PSA_SUCCESS )
3781 goto cleanup;
3782
3783 status = psa_hmac_finish_internal( &hmac,
3784 tls12_prf->output_block,
3785 hash_length );
3786 if( status != PSA_SUCCESS )
3787 goto cleanup;
3788
3789cleanup:
3790
3791 cleanup_status = psa_hmac_abort_internal( &hmac );
3792 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3793 status = cleanup_status;
3794
3795 return( status );
3796}
3797
3798/* Read some bytes from an TLS-1.2-PRF-based generator.
3799 * See Section 5 of RFC 5246. */
3800static psa_status_t psa_generator_tls12_prf_read(
3801 psa_tls12_prf_generator_t *tls12_prf,
3802 psa_algorithm_t alg,
3803 uint8_t *output,
3804 size_t output_length )
3805{
3806 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3807 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3808 psa_status_t status;
3809
3810 while( output_length != 0 )
3811 {
3812 /* Copy what remains of the current block */
3813 uint8_t n = hash_length - tls12_prf->offset_in_block;
3814
3815 /* Check if we have fully processed the current block. */
3816 if( n == 0 )
3817 {
3818 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3819 alg );
3820 if( status != PSA_SUCCESS )
3821 return( status );
3822
3823 continue;
3824 }
3825
3826 if( n > output_length )
3827 n = (uint8_t) output_length;
3828 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3829 n );
3830 output += n;
3831 output_length -= n;
3832 tls12_prf->offset_in_block += n;
3833 }
3834
3835 return( PSA_SUCCESS );
3836}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003837#endif /* MBEDTLS_MD_C */
3838
Gilles Peskineeab56e42018-07-12 17:12:33 +02003839psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3840 uint8_t *output,
3841 size_t output_length )
3842{
3843 psa_status_t status;
3844
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003845 if( generator->alg == 0 )
3846 {
3847 /* This is a blank generator. */
3848 return PSA_ERROR_BAD_STATE;
3849 }
3850
Gilles Peskineeab56e42018-07-12 17:12:33 +02003851 if( output_length > generator->capacity )
3852 {
3853 generator->capacity = 0;
3854 /* Go through the error path to wipe all confidential data now
3855 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02003856 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003857 goto exit;
3858 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003859 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003860 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003861 /* Edge case: this is a finished generator, and 0 bytes
3862 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02003863 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3864 * INSUFFICIENT_CAPACITY, which is right for a finished
3865 * generator, for consistency with the case when
3866 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02003867 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003868 }
3869 generator->capacity -= output_length;
3870
Gilles Peskine751d9652018-09-18 12:05:44 +02003871 if( generator->alg == PSA_ALG_SELECT_RAW )
3872 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003873 /* Initially, the capacity of a selection generator is always
3874 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3875 * abbreviated in this comment as `size`. When the remaining
3876 * capacity is `c`, the next bytes to serve start `c` bytes
3877 * from the end of the buffer, i.e. `size - c` from the
3878 * beginning of the buffer. Since `generator->capacity` was just
3879 * decremented above, we need to serve the bytes from
3880 * `size - generator->capacity - output_length` to
3881 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003882 size_t offset =
3883 generator->ctx.buffer.size - generator->capacity - output_length;
3884 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3885 status = PSA_SUCCESS;
3886 }
3887 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003888#if defined(MBEDTLS_MD_C)
3889 if( PSA_ALG_IS_HKDF( generator->alg ) )
3890 {
3891 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3892 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3893 output, output_length );
3894 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003895 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3896 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003897 {
3898 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3899 generator->alg, output,
3900 output_length );
3901 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003902 else
3903#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003904 {
3905 return( PSA_ERROR_BAD_STATE );
3906 }
3907
3908exit:
3909 if( status != PSA_SUCCESS )
3910 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003911 /* Preserve the algorithm upon errors, but clear all sensitive state.
3912 * This allows us to differentiate between exhausted generators and
3913 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
3914 * generators. */
3915 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003916 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003917 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003918 memset( output, '!', output_length );
3919 }
3920 return( status );
3921}
3922
Gilles Peskine08542d82018-07-19 17:05:42 +02003923#if defined(MBEDTLS_DES_C)
3924static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3925{
3926 if( data_size >= 8 )
3927 mbedtls_des_key_set_parity( data );
3928 if( data_size >= 16 )
3929 mbedtls_des_key_set_parity( data + 8 );
3930 if( data_size >= 24 )
3931 mbedtls_des_key_set_parity( data + 16 );
3932}
3933#endif /* MBEDTLS_DES_C */
3934
Gilles Peskinec5487a82018-12-03 18:08:14 +01003935psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003936 psa_key_type_t type,
3937 size_t bits,
3938 psa_crypto_generator_t *generator )
3939{
3940 uint8_t *data = NULL;
3941 size_t bytes = PSA_BITS_TO_BYTES( bits );
3942 psa_status_t status;
3943
3944 if( ! key_type_is_raw_bytes( type ) )
3945 return( PSA_ERROR_INVALID_ARGUMENT );
3946 if( bits % 8 != 0 )
3947 return( PSA_ERROR_INVALID_ARGUMENT );
3948 data = mbedtls_calloc( 1, bytes );
3949 if( data == NULL )
3950 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3951
3952 status = psa_generator_read( generator, data, bytes );
3953 if( status != PSA_SUCCESS )
3954 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003955#if defined(MBEDTLS_DES_C)
3956 if( type == PSA_KEY_TYPE_DES )
3957 psa_des_set_key_parity( data, bytes );
3958#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003959 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003960
3961exit:
3962 mbedtls_free( data );
3963 return( status );
3964}
3965
3966
3967
3968/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003969/* Key derivation */
3970/****************************************************************/
3971
Gilles Peskinea05219c2018-11-16 16:02:56 +01003972#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003973/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003974 * of the HKDF algorithm.
3975 *
3976 * Note that if this function fails, you must call psa_generator_abort()
3977 * to potentially free embedded data structures and wipe confidential data.
3978 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003979static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003980 const uint8_t *secret,
3981 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003982 psa_algorithm_t hash_alg,
3983 const uint8_t *salt,
3984 size_t salt_length,
3985 const uint8_t *label,
3986 size_t label_length )
3987{
3988 psa_status_t status;
3989 status = psa_hmac_setup_internal( &hkdf->hmac,
3990 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003991 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003992 if( status != PSA_SUCCESS )
3993 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003994 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003995 if( status != PSA_SUCCESS )
3996 return( status );
3997 status = psa_hmac_finish_internal( &hkdf->hmac,
3998 hkdf->prk,
3999 sizeof( hkdf->prk ) );
4000 if( status != PSA_SUCCESS )
4001 return( status );
4002 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4003 hkdf->block_number = 0;
4004 hkdf->info_length = label_length;
4005 if( label_length != 0 )
4006 {
4007 hkdf->info = mbedtls_calloc( 1, label_length );
4008 if( hkdf->info == NULL )
4009 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4010 memcpy( hkdf->info, label, label_length );
4011 }
4012 return( PSA_SUCCESS );
4013}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004014#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004015
Gilles Peskinea05219c2018-11-16 16:02:56 +01004016#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004017/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4018 *
4019 * Note that if this function fails, you must call psa_generator_abort()
4020 * to potentially free embedded data structures and wipe confidential data.
4021 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004022static psa_status_t psa_generator_tls12_prf_setup(
4023 psa_tls12_prf_generator_t *tls12_prf,
4024 const unsigned char *key,
4025 size_t key_len,
4026 psa_algorithm_t hash_alg,
4027 const uint8_t *salt,
4028 size_t salt_length,
4029 const uint8_t *label,
4030 size_t label_length )
4031{
4032 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004033 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4034 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004035
4036 tls12_prf->key = mbedtls_calloc( 1, key_len );
4037 if( tls12_prf->key == NULL )
4038 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4039 tls12_prf->key_len = key_len;
4040 memcpy( tls12_prf->key, key, key_len );
4041
Hanno Becker580fba12018-11-13 20:50:45 +00004042 overflow = ( salt_length + label_length < salt_length ) ||
4043 ( salt_length + label_length + hash_length < hash_length );
4044 if( overflow )
4045 return( PSA_ERROR_INVALID_ARGUMENT );
4046
4047 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4048 if( tls12_prf->Ai_with_seed == NULL )
4049 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4050 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4051
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004052 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4053 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004054 if( label_length != 0 )
4055 {
4056 memcpy( tls12_prf->Ai_with_seed + hash_length,
4057 label, label_length );
4058 }
4059
4060 if( salt_length != 0 )
4061 {
4062 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4063 salt, salt_length );
4064 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004065
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004066 /* The first block gets generated when
4067 * psa_generator_read() is called. */
4068 tls12_prf->block_number = 0;
4069 tls12_prf->offset_in_block = hash_length;
4070
4071 return( PSA_SUCCESS );
4072}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004073
4074/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4075static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4076 psa_tls12_prf_generator_t *tls12_prf,
4077 const unsigned char *psk,
4078 size_t psk_len,
4079 psa_algorithm_t hash_alg,
4080 const uint8_t *salt,
4081 size_t salt_length,
4082 const uint8_t *label,
4083 size_t label_length )
4084{
4085 psa_status_t status;
4086 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4087
4088 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4089 return( PSA_ERROR_INVALID_ARGUMENT );
4090
4091 /* Quoting RFC 4279, Section 2:
4092 *
4093 * The premaster secret is formed as follows: if the PSK is N octets
4094 * long, concatenate a uint16 with the value N, N zero octets, a second
4095 * uint16 with the value N, and the PSK itself.
4096 */
4097
4098 pms[0] = ( psk_len >> 8 ) & 0xff;
4099 pms[1] = ( psk_len >> 0 ) & 0xff;
4100 memset( pms + 2, 0, psk_len );
4101 pms[2 + psk_len + 0] = pms[0];
4102 pms[2 + psk_len + 1] = pms[1];
4103 memcpy( pms + 4 + psk_len, psk, psk_len );
4104
4105 status = psa_generator_tls12_prf_setup( tls12_prf,
4106 pms, 4 + 2 * psk_len,
4107 hash_alg,
4108 salt, salt_length,
4109 label, label_length );
4110
Gilles Peskine3f108122018-12-07 18:14:53 +01004111 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004112 return( status );
4113}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004114#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004115
Gilles Peskine346797d2018-11-16 16:05:06 +01004116/* Note that if this function fails, you must call psa_generator_abort()
4117 * to potentially free embedded data structures and wipe confidential data.
4118 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004119static psa_status_t psa_key_derivation_internal(
4120 psa_crypto_generator_t *generator,
4121 const uint8_t *secret, size_t secret_length,
4122 psa_algorithm_t alg,
4123 const uint8_t *salt, size_t salt_length,
4124 const uint8_t *label, size_t label_length,
4125 size_t capacity )
4126{
4127 psa_status_t status;
4128 size_t max_capacity;
4129
4130 /* Set generator->alg even on failure so that abort knows what to do. */
4131 generator->alg = alg;
4132
Gilles Peskine751d9652018-09-18 12:05:44 +02004133 if( alg == PSA_ALG_SELECT_RAW )
4134 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004135 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004136 if( salt_length != 0 )
4137 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004138 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004139 if( label_length != 0 )
4140 return( PSA_ERROR_INVALID_ARGUMENT );
4141 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4142 if( generator->ctx.buffer.data == NULL )
4143 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4144 memcpy( generator->ctx.buffer.data, secret, secret_length );
4145 generator->ctx.buffer.size = secret_length;
4146 max_capacity = secret_length;
4147 status = PSA_SUCCESS;
4148 }
4149 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004150#if defined(MBEDTLS_MD_C)
4151 if( PSA_ALG_IS_HKDF( alg ) )
4152 {
4153 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4154 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4155 if( hash_size == 0 )
4156 return( PSA_ERROR_NOT_SUPPORTED );
4157 max_capacity = 255 * hash_size;
4158 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4159 secret, secret_length,
4160 hash_alg,
4161 salt, salt_length,
4162 label, label_length );
4163 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004164 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4165 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4166 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004167 {
4168 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4169 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4170
4171 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4172 if( hash_alg != PSA_ALG_SHA_256 &&
4173 hash_alg != PSA_ALG_SHA_384 )
4174 {
4175 return( PSA_ERROR_NOT_SUPPORTED );
4176 }
4177
4178 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004179
4180 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4181 {
4182 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4183 secret, secret_length,
4184 hash_alg, salt, salt_length,
4185 label, label_length );
4186 }
4187 else
4188 {
4189 status = psa_generator_tls12_psk_to_ms_setup(
4190 &generator->ctx.tls12_prf,
4191 secret, secret_length,
4192 hash_alg, salt, salt_length,
4193 label, label_length );
4194 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004195 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004196 else
4197#endif
4198 {
4199 return( PSA_ERROR_NOT_SUPPORTED );
4200 }
4201
4202 if( status != PSA_SUCCESS )
4203 return( status );
4204
4205 if( capacity <= max_capacity )
4206 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004207 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4208 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004209 else
4210 return( PSA_ERROR_INVALID_ARGUMENT );
4211
4212 return( PSA_SUCCESS );
4213}
4214
Gilles Peskineea0fb492018-07-12 17:17:20 +02004215psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004216 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004217 psa_algorithm_t alg,
4218 const uint8_t *salt,
4219 size_t salt_length,
4220 const uint8_t *label,
4221 size_t label_length,
4222 size_t capacity )
4223{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004224 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004225 psa_status_t status;
4226
4227 if( generator->alg != 0 )
4228 return( PSA_ERROR_BAD_STATE );
4229
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004230 /* Make sure that alg is a key derivation algorithm. This prevents
4231 * key selection algorithms, which psa_key_derivation_internal
4232 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004233 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4234 return( PSA_ERROR_INVALID_ARGUMENT );
4235
Gilles Peskinec5487a82018-12-03 18:08:14 +01004236 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004237 if( status != PSA_SUCCESS )
4238 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004239
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004240 if( slot->type != PSA_KEY_TYPE_DERIVE )
4241 return( PSA_ERROR_INVALID_ARGUMENT );
4242
4243 status = psa_key_derivation_internal( generator,
4244 slot->data.raw.data,
4245 slot->data.raw.bytes,
4246 alg,
4247 salt, salt_length,
4248 label, label_length,
4249 capacity );
4250 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004251 psa_generator_abort( generator );
4252 return( status );
4253}
4254
4255
4256
4257/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004258/* Key agreement */
4259/****************************************************************/
4260
Gilles Peskinea05219c2018-11-16 16:02:56 +01004261#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004262static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4263 size_t peer_key_length,
4264 const mbedtls_ecp_keypair *our_key,
4265 uint8_t *shared_secret,
4266 size_t shared_secret_size,
4267 size_t *shared_secret_length )
4268{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004269 mbedtls_ecp_keypair *their_key = NULL;
4270 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004271 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004272 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004273
Jaeden Ameroccdce902019-01-10 11:42:27 +00004274 status = psa_import_ec_public_key(
4275 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4276 peer_key, peer_key_length,
4277 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004278 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004279 goto exit;
4280
Jaeden Amero97271b32019-01-10 19:38:51 +00004281 status = mbedtls_to_psa_error(
4282 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4283 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004284 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004285 status = mbedtls_to_psa_error(
4286 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4287 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004288 goto exit;
4289
Jaeden Amero97271b32019-01-10 19:38:51 +00004290 status = mbedtls_to_psa_error(
4291 mbedtls_ecdh_calc_secret( &ecdh,
4292 shared_secret_length,
4293 shared_secret, shared_secret_size,
4294 mbedtls_ctr_drbg_random,
4295 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004296
4297exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004298 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004299 mbedtls_ecp_keypair_free( their_key );
4300 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004301 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004302}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004303#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004304
Gilles Peskine01d718c2018-09-18 12:01:02 +02004305#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4306
Gilles Peskine346797d2018-11-16 16:05:06 +01004307/* Note that if this function fails, you must call psa_generator_abort()
4308 * to potentially free embedded data structures and wipe confidential data.
4309 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004310static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004311 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004312 const uint8_t *peer_key,
4313 size_t peer_key_length,
4314 psa_algorithm_t alg )
4315{
4316 psa_status_t status;
4317 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4318 size_t shared_secret_length = 0;
4319
4320 /* Step 1: run the secret agreement algorithm to generate the shared
4321 * secret. */
4322 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4323 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004324#if defined(MBEDTLS_ECDH_C)
4325 case PSA_ALG_ECDH_BASE:
4326 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4327 return( PSA_ERROR_INVALID_ARGUMENT );
4328 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4329 private_key->data.ecp,
4330 shared_secret,
4331 sizeof( shared_secret ),
4332 &shared_secret_length );
4333 break;
4334#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004335 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004336 (void) private_key;
4337 (void) peer_key;
4338 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004339 return( PSA_ERROR_NOT_SUPPORTED );
4340 }
4341 if( status != PSA_SUCCESS )
4342 goto exit;
4343
4344 /* Step 2: set up the key derivation to generate key material from
4345 * the shared secret. */
4346 status = psa_key_derivation_internal( generator,
4347 shared_secret, shared_secret_length,
4348 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4349 NULL, 0, NULL, 0,
4350 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4351exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004352 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004353 return( status );
4354}
4355
4356psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004357 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004358 const uint8_t *peer_key,
4359 size_t peer_key_length,
4360 psa_algorithm_t alg )
4361{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004362 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004363 psa_status_t status;
4364 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4365 return( PSA_ERROR_INVALID_ARGUMENT );
4366 status = psa_get_key_from_slot( private_key, &slot,
4367 PSA_KEY_USAGE_DERIVE, alg );
4368 if( status != PSA_SUCCESS )
4369 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004370 status = psa_key_agreement_internal( generator,
4371 slot,
4372 peer_key, peer_key_length,
4373 alg );
4374 if( status != PSA_SUCCESS )
4375 psa_generator_abort( generator );
4376 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004377}
4378
4379
4380
4381/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004382/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004383/****************************************************************/
4384
4385psa_status_t psa_generate_random( uint8_t *output,
4386 size_t output_size )
4387{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004388 int ret;
4389 GUARD_MODULE_INITIALIZED;
4390
4391 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004392 return( mbedtls_to_psa_error( ret ) );
4393}
4394
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004395#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004396
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004397psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4398 size_t seed_size )
4399{
4400 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +02004401 struct psa_storage_info_t p_info;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004402 if( global_data.initialized )
4403 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004404
4405 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4406 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4407 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4408 return( PSA_ERROR_INVALID_ARGUMENT );
4409
David Saadaa2523b22019-02-18 13:56:26 +02004410 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004411
David Saadaa2523b22019-02-18 13:56:26 +02004412 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004413 {
David Saadaa2523b22019-02-18 13:56:26 +02004414 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004415 }
David Saadaa2523b22019-02-18 13:56:26 +02004416 else if( PSA_SUCCESS == status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004417 {
4418 /* You should not be here. Seed needs to be injected only once */
4419 status = PSA_ERROR_NOT_PERMITTED;
4420 }
4421 return( status );
4422}
4423#endif
4424
Gilles Peskinec5487a82018-12-03 18:08:14 +01004425psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004426 psa_key_type_t type,
4427 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004428 const void *extra,
4429 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004430{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004431 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004432 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004433
Gilles Peskine53d991e2018-07-12 01:14:59 +02004434 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004435 return( PSA_ERROR_INVALID_ARGUMENT );
4436
Gilles Peskinec5487a82018-12-03 18:08:14 +01004437 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004438 if( status != PSA_SUCCESS )
4439 return( status );
4440
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004441 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004442 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004443 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004444 if( status != PSA_SUCCESS )
4445 return( status );
4446 status = psa_generate_random( slot->data.raw.data,
4447 slot->data.raw.bytes );
4448 if( status != PSA_SUCCESS )
4449 {
4450 mbedtls_free( slot->data.raw.data );
4451 return( status );
4452 }
4453#if defined(MBEDTLS_DES_C)
4454 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004455 psa_des_set_key_parity( slot->data.raw.data,
4456 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004457#endif /* MBEDTLS_DES_C */
4458 }
4459 else
4460
4461#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4462 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4463 {
4464 mbedtls_rsa_context *rsa;
4465 int ret;
4466 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004467 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4468 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004469 /* Accept only byte-aligned keys, for the same reasons as
4470 * in psa_import_rsa_key(). */
4471 if( bits % 8 != 0 )
4472 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004473 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004474 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004475 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004476 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004477 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004478#if INT_MAX < 0xffffffff
4479 /* Check that the uint32_t value passed by the caller fits
4480 * in the range supported by this implementation. */
4481 if( p->e > INT_MAX )
4482 return( PSA_ERROR_NOT_SUPPORTED );
4483#endif
4484 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004485 }
4486 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4487 if( rsa == NULL )
4488 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4489 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4490 ret = mbedtls_rsa_gen_key( rsa,
4491 mbedtls_ctr_drbg_random,
4492 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004493 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004494 exponent );
4495 if( ret != 0 )
4496 {
4497 mbedtls_rsa_free( rsa );
4498 mbedtls_free( rsa );
4499 return( mbedtls_to_psa_error( ret ) );
4500 }
4501 slot->data.rsa = rsa;
4502 }
4503 else
4504#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4505
4506#if defined(MBEDTLS_ECP_C)
4507 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4508 {
4509 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4510 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4511 const mbedtls_ecp_curve_info *curve_info =
4512 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4513 mbedtls_ecp_keypair *ecp;
4514 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004515 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004516 return( PSA_ERROR_NOT_SUPPORTED );
4517 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4518 return( PSA_ERROR_NOT_SUPPORTED );
4519 if( curve_info->bit_size != bits )
4520 return( PSA_ERROR_INVALID_ARGUMENT );
4521 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4522 if( ecp == NULL )
4523 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4524 mbedtls_ecp_keypair_init( ecp );
4525 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4526 mbedtls_ctr_drbg_random,
4527 &global_data.ctr_drbg );
4528 if( ret != 0 )
4529 {
4530 mbedtls_ecp_keypair_free( ecp );
4531 mbedtls_free( ecp );
4532 return( mbedtls_to_psa_error( ret ) );
4533 }
4534 slot->data.ecp = ecp;
4535 }
4536 else
4537#endif /* MBEDTLS_ECP_C */
4538
4539 return( PSA_ERROR_NOT_SUPPORTED );
4540
4541 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004542
4543#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4544 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4545 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004546 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004547 }
4548#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4549
4550 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004551}
4552
4553
4554/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004555/* Module setup */
4556/****************************************************************/
4557
Gilles Peskine5e769522018-11-20 21:59:56 +01004558psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4559 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4560 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4561{
4562 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4563 return( PSA_ERROR_BAD_STATE );
4564 global_data.entropy_init = entropy_init;
4565 global_data.entropy_free = entropy_free;
4566 return( PSA_SUCCESS );
4567}
4568
Gilles Peskinee59236f2018-01-27 23:32:46 +01004569void mbedtls_psa_crypto_free( void )
4570{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004571 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004572 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4573 {
4574 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004575 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004576 }
4577 /* Wipe all remaining data, including configuration.
4578 * In particular, this sets all state indicator to the value
4579 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004580 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004581}
4582
4583psa_status_t psa_crypto_init( void )
4584{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004585 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004586 const unsigned char drbg_seed[] = "PSA";
4587
Gilles Peskinec6b69072018-11-20 21:42:52 +01004588 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004589 if( global_data.initialized != 0 )
4590 return( PSA_SUCCESS );
4591
Gilles Peskine5e769522018-11-20 21:59:56 +01004592 /* Set default configuration if
4593 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4594 if( global_data.entropy_init == NULL )
4595 global_data.entropy_init = mbedtls_entropy_init;
4596 if( global_data.entropy_free == NULL )
4597 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004598
Gilles Peskinec6b69072018-11-20 21:42:52 +01004599 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004600 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004601 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004602 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004603 status = mbedtls_to_psa_error(
4604 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4605 mbedtls_entropy_func,
4606 &global_data.entropy,
4607 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4608 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004609 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004610 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004611
Gilles Peskine66fb1262018-12-10 16:29:04 +01004612 status = psa_initialize_key_slots( );
4613 if( status != PSA_SUCCESS )
4614 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004615
4616 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004617 global_data.initialized = 1;
4618
Gilles Peskinee59236f2018-01-27 23:32:46 +01004619exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004620 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004621 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004622 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004623}
4624
4625#endif /* MBEDTLS_PSA_CRYPTO_C */