blob: 2fab91cc2f58cec4f2a9dfa123e112fbd52b7b3e [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 Amero6b196002019-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"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/error.h"
64#include "mbedtls/gcm.h"
65#include "mbedtls/md2.h"
66#include "mbedtls/md4.h"
67#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010068#include "mbedtls/md.h"
69#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010070#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010072#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010074#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/sha1.h"
76#include "mbedtls/sha256.h"
77#include "mbedtls/sha512.h"
78#include "mbedtls/xtea.h"
79
Gilles Peskine996deb12018-08-01 15:45:45 +020080#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
81
Gilles Peskine9ef733f2018-02-07 21:05:37 +010082/* constant-time buffer comparison */
83static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
84{
85 size_t i;
86 unsigned char diff = 0;
87
88 for( i = 0; i < n; i++ )
89 diff |= a[i] ^ b[i];
90
91 return( diff );
92}
93
94
95
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010096/****************************************************************/
97/* Global data, support functions and library management */
98/****************************************************************/
99
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100static int key_type_is_raw_bytes( psa_key_type_t type )
101{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200102 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200103}
104
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100105/* Values for psa_global_data_t::rng_state */
106#define RNG_NOT_INITIALIZED 0
107#define RNG_INITIALIZED 1
108#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100109
Gilles Peskine2d277862018-06-18 15:41:12 +0200110typedef struct
111{
Gilles Peskine5e769522018-11-20 21:59:56 +0100112 void (* entropy_init )( mbedtls_entropy_context *ctx );
113 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100114 mbedtls_entropy_context entropy;
115 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100116 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100117 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
itayzafrir0adf0fc2018-09-06 16:24:41 +0300122#define GUARD_MODULE_INITIALIZED \
123 if( global_data.initialized == 0 ) \
124 return( PSA_ERROR_BAD_STATE );
125
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126static psa_status_t mbedtls_to_psa_error( int ret )
127{
Gilles Peskinea5905292018-02-07 20:59:33 +0100128 /* If there's both a high-level code and low-level code, dispatch on
129 * the high-level code. */
130 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131 {
132 case 0:
133 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100134
135 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
136 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
137 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
138 return( PSA_ERROR_NOT_SUPPORTED );
139 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
140 return( PSA_ERROR_HARDWARE_FAILURE );
141
142 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
Gilles Peskine9a944802018-06-21 09:35:35 +0200145 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
146 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
147 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
148 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
149 case MBEDTLS_ERR_ASN1_INVALID_DATA:
150 return( PSA_ERROR_INVALID_ARGUMENT );
151 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
152 return( PSA_ERROR_INSUFFICIENT_MEMORY );
153 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
154 return( PSA_ERROR_BUFFER_TOO_SMALL );
155
Jaeden Amero93e21112019-02-20 13:57:28 +0000156#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000157 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100159 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100161 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
162 return( PSA_ERROR_NOT_SUPPORTED );
163 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
164 return( PSA_ERROR_HARDWARE_FAILURE );
165
Jaeden Amero93e21112019-02-20 13:57:28 +0000166#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000167 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
172 return( PSA_ERROR_NOT_SUPPORTED );
173 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
174 return( PSA_ERROR_HARDWARE_FAILURE );
175
176 case MBEDTLS_ERR_CCM_BAD_INPUT:
177 return( PSA_ERROR_INVALID_ARGUMENT );
178 case MBEDTLS_ERR_CCM_AUTH_FAILED:
179 return( PSA_ERROR_INVALID_SIGNATURE );
180 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
181 return( PSA_ERROR_HARDWARE_FAILURE );
182
183 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
188 return( PSA_ERROR_INSUFFICIENT_MEMORY );
189 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
190 return( PSA_ERROR_INVALID_PADDING );
191 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
196 return( PSA_ERROR_TAMPERING_DETECTED );
197 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
198 return( PSA_ERROR_HARDWARE_FAILURE );
199
200 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
201 return( PSA_ERROR_HARDWARE_FAILURE );
202
203 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
204 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
205 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
206 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
209 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
210
211 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
212 return( PSA_ERROR_NOT_SUPPORTED );
213 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
Gilles Peskinee59236f2018-01-27 23:32:46 +0100216 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
217 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
218 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
221 case MBEDTLS_ERR_GCM_AUTH_FAILED:
222 return( PSA_ERROR_INVALID_SIGNATURE );
223 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200224 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
228 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
229 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
230 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
231 return( PSA_ERROR_HARDWARE_FAILURE );
232
233 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
234 return( PSA_ERROR_NOT_SUPPORTED );
235 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
236 return( PSA_ERROR_INVALID_ARGUMENT );
237 case MBEDTLS_ERR_MD_ALLOC_FAILED:
238 return( PSA_ERROR_INSUFFICIENT_MEMORY );
239 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
240 return( PSA_ERROR_STORAGE_FAILURE );
241 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskinef76aa772018-10-29 19:24:33 +0100244 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
245 return( PSA_ERROR_STORAGE_FAILURE );
246 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
251 return( PSA_ERROR_BUFFER_TOO_SMALL );
252 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
253 return( PSA_ERROR_INVALID_ARGUMENT );
254 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
255 return( PSA_ERROR_INVALID_ARGUMENT );
256 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
260
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_ALLOC_FAILED:
262 return( PSA_ERROR_INSUFFICIENT_MEMORY );
263 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
264 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100268 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
269 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
274 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
275 return( PSA_ERROR_NOT_PERMITTED );
276 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_INVALID_ALG:
279 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
280 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
283 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
285 return( PSA_ERROR_HARDWARE_FAILURE );
286
287 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
288 return( PSA_ERROR_HARDWARE_FAILURE );
289
290 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_RSA_INVALID_PADDING:
293 return( PSA_ERROR_INVALID_PADDING );
294 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
299 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
300 return( PSA_ERROR_TAMPERING_DETECTED );
301 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
302 return( PSA_ERROR_INVALID_SIGNATURE );
303 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
304 return( PSA_ERROR_BUFFER_TOO_SMALL );
305 case MBEDTLS_ERR_RSA_RNG_FAILED:
306 return( PSA_ERROR_INSUFFICIENT_MEMORY );
307 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
308 return( PSA_ERROR_NOT_SUPPORTED );
309 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311
312 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
313 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
314 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316
317 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
itayzafrir5c753392018-05-08 11:18:38 +0300322 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300324 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
326 return( PSA_ERROR_BUFFER_TOO_SMALL );
327 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
328 return( PSA_ERROR_NOT_SUPPORTED );
329 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
330 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
331 return( PSA_ERROR_INVALID_SIGNATURE );
332 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
333 return( PSA_ERROR_INSUFFICIENT_MEMORY );
334 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300336
Gilles Peskinee59236f2018-01-27 23:32:46 +0100337 default:
David Saadab4ecc272019-02-14 13:48:10 +0200338 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100339 }
340}
341
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200342
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200343
344
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100345/****************************************************************/
346/* Key management */
347/****************************************************************/
348
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100349#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200350static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
351{
352 switch( grpid )
353 {
354 case MBEDTLS_ECP_DP_SECP192R1:
355 return( PSA_ECC_CURVE_SECP192R1 );
356 case MBEDTLS_ECP_DP_SECP224R1:
357 return( PSA_ECC_CURVE_SECP224R1 );
358 case MBEDTLS_ECP_DP_SECP256R1:
359 return( PSA_ECC_CURVE_SECP256R1 );
360 case MBEDTLS_ECP_DP_SECP384R1:
361 return( PSA_ECC_CURVE_SECP384R1 );
362 case MBEDTLS_ECP_DP_SECP521R1:
363 return( PSA_ECC_CURVE_SECP521R1 );
364 case MBEDTLS_ECP_DP_BP256R1:
365 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
366 case MBEDTLS_ECP_DP_BP384R1:
367 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
368 case MBEDTLS_ECP_DP_BP512R1:
369 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
370 case MBEDTLS_ECP_DP_CURVE25519:
371 return( PSA_ECC_CURVE_CURVE25519 );
372 case MBEDTLS_ECP_DP_SECP192K1:
373 return( PSA_ECC_CURVE_SECP192K1 );
374 case MBEDTLS_ECP_DP_SECP224K1:
375 return( PSA_ECC_CURVE_SECP224K1 );
376 case MBEDTLS_ECP_DP_SECP256K1:
377 return( PSA_ECC_CURVE_SECP256K1 );
378 case MBEDTLS_ECP_DP_CURVE448:
379 return( PSA_ECC_CURVE_CURVE448 );
380 default:
381 return( 0 );
382 }
383}
384
Gilles Peskine12313cd2018-06-20 00:20:32 +0200385static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
386{
387 switch( curve )
388 {
389 case PSA_ECC_CURVE_SECP192R1:
390 return( MBEDTLS_ECP_DP_SECP192R1 );
391 case PSA_ECC_CURVE_SECP224R1:
392 return( MBEDTLS_ECP_DP_SECP224R1 );
393 case PSA_ECC_CURVE_SECP256R1:
394 return( MBEDTLS_ECP_DP_SECP256R1 );
395 case PSA_ECC_CURVE_SECP384R1:
396 return( MBEDTLS_ECP_DP_SECP384R1 );
397 case PSA_ECC_CURVE_SECP521R1:
398 return( MBEDTLS_ECP_DP_SECP521R1 );
399 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
400 return( MBEDTLS_ECP_DP_BP256R1 );
401 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
402 return( MBEDTLS_ECP_DP_BP384R1 );
403 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
404 return( MBEDTLS_ECP_DP_BP512R1 );
405 case PSA_ECC_CURVE_CURVE25519:
406 return( MBEDTLS_ECP_DP_CURVE25519 );
407 case PSA_ECC_CURVE_SECP192K1:
408 return( MBEDTLS_ECP_DP_SECP192K1 );
409 case PSA_ECC_CURVE_SECP224K1:
410 return( MBEDTLS_ECP_DP_SECP224K1 );
411 case PSA_ECC_CURVE_SECP256K1:
412 return( MBEDTLS_ECP_DP_SECP256K1 );
413 case PSA_ECC_CURVE_CURVE448:
414 return( MBEDTLS_ECP_DP_CURVE448 );
415 default:
416 return( MBEDTLS_ECP_DP_NONE );
417 }
418}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100419#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200420
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200421static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
422 size_t bits,
423 struct raw_data *raw )
424{
425 /* Check that the bit size is acceptable for the key type */
426 switch( type )
427 {
428 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200429 if( bits == 0 )
430 {
431 raw->bytes = 0;
432 raw->data = NULL;
433 return( PSA_SUCCESS );
434 }
435 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436#if defined(MBEDTLS_MD_C)
437 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200438#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200439 case PSA_KEY_TYPE_DERIVE:
440 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200441#if defined(MBEDTLS_AES_C)
442 case PSA_KEY_TYPE_AES:
443 if( bits != 128 && bits != 192 && bits != 256 )
444 return( PSA_ERROR_INVALID_ARGUMENT );
445 break;
446#endif
447#if defined(MBEDTLS_CAMELLIA_C)
448 case PSA_KEY_TYPE_CAMELLIA:
449 if( bits != 128 && bits != 192 && bits != 256 )
450 return( PSA_ERROR_INVALID_ARGUMENT );
451 break;
452#endif
453#if defined(MBEDTLS_DES_C)
454 case PSA_KEY_TYPE_DES:
455 if( bits != 64 && bits != 128 && bits != 192 )
456 return( PSA_ERROR_INVALID_ARGUMENT );
457 break;
458#endif
459#if defined(MBEDTLS_ARC4_C)
460 case PSA_KEY_TYPE_ARC4:
461 if( bits < 8 || bits > 2048 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465 default:
466 return( PSA_ERROR_NOT_SUPPORTED );
467 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200468 if( bits % 8 != 0 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200470
471 /* Allocate memory for the key */
472 raw->bytes = PSA_BITS_TO_BYTES( bits );
473 raw->data = mbedtls_calloc( 1, raw->bytes );
474 if( raw->data == NULL )
475 {
476 raw->bytes = 0;
477 return( PSA_ERROR_INSUFFICIENT_MEMORY );
478 }
479 return( PSA_SUCCESS );
480}
481
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200482#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100483/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
484 * that are not a multiple of 8) well. For example, there is only
485 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
486 * way to return the exact bit size of a key.
487 * To keep things simple, reject non-byte-aligned key sizes. */
488static psa_status_t psa_check_rsa_key_byte_aligned(
489 const mbedtls_rsa_context *rsa )
490{
491 mbedtls_mpi n;
492 psa_status_t status;
493 mbedtls_mpi_init( &n );
494 status = mbedtls_to_psa_error(
495 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
496 if( status == PSA_SUCCESS )
497 {
498 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
499 status = PSA_ERROR_NOT_SUPPORTED;
500 }
501 mbedtls_mpi_free( &n );
502 return( status );
503}
504
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000505static psa_status_t psa_import_rsa_key( psa_key_type_t type,
506 const uint8_t *data,
507 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200508 mbedtls_rsa_context **p_rsa )
509{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000510 psa_status_t status;
511 mbedtls_pk_context pk;
512 mbedtls_rsa_context *rsa;
513 size_t bits;
514
515 mbedtls_pk_init( &pk );
516
517 /* Parse the data. */
518 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
519 status = mbedtls_to_psa_error(
520 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200521 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000522 status = mbedtls_to_psa_error(
523 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
524 if( status != PSA_SUCCESS )
525 goto exit;
526
527 /* We have something that the pkparse module recognizes. If it is a
528 * valid RSA key, store it. */
529 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200530 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000531 status = PSA_ERROR_INVALID_ARGUMENT;
532 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000534
535 rsa = mbedtls_pk_rsa( pk );
536 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
537 * supports non-byte-aligned key sizes, but not well. For example,
538 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
539 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
540 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
541 {
542 status = PSA_ERROR_NOT_SUPPORTED;
543 goto exit;
544 }
545 status = psa_check_rsa_key_byte_aligned( rsa );
546
547exit:
548 /* Free the content of the pk object only on error. */
549 if( status != PSA_SUCCESS )
550 {
551 mbedtls_pk_free( &pk );
552 return( status );
553 }
554
555 /* On success, store the content of the object in the RSA context. */
556 *p_rsa = rsa;
557
558 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200559}
560#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
561
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000562#if defined(MBEDTLS_ECP_C)
563
564/* Import a public key given as the uncompressed representation defined by SEC1
565 * 2.3.3 as the content of an ECPoint. */
566static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
567 const uint8_t *data,
568 size_t data_length,
569 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570{
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000571 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
572 mbedtls_ecp_keypair *ecp = NULL;
573 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
574
575 *p_ecp = NULL;
576 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
577 if( ecp == NULL )
578 return( PSA_ERROR_INSUFFICIENT_MEMORY );
579 mbedtls_ecp_keypair_init( ecp );
580
581 /* Load the group. */
582 status = mbedtls_to_psa_error(
583 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
584 if( status != PSA_SUCCESS )
585 goto exit;
586 /* Load the public value. */
587 status = mbedtls_to_psa_error(
588 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
589 data, data_length ) );
590 if( status != PSA_SUCCESS )
591 goto exit;
592
593 /* Check that the point is on the curve. */
594 status = mbedtls_to_psa_error(
595 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
596 if( status != PSA_SUCCESS )
597 goto exit;
598
599 *p_ecp = ecp;
600 return( PSA_SUCCESS );
601
602exit:
603 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200604 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000605 mbedtls_ecp_keypair_free( ecp );
606 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200607 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000608 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200609}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000610#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200611
Gilles Peskinef76aa772018-10-29 19:24:33 +0100612#if defined(MBEDTLS_ECP_C)
613/* Import a private key given as a byte string which is the private value
614 * in big-endian order. */
615static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
616 const uint8_t *data,
617 size_t data_length,
618 mbedtls_ecp_keypair **p_ecp )
619{
620 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
621 mbedtls_ecp_keypair *ecp = NULL;
622 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
623
624 *p_ecp = NULL;
625 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
626 if( ecp == NULL )
627 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000628 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100629
630 /* Load the group. */
631 status = mbedtls_to_psa_error(
632 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
633 if( status != PSA_SUCCESS )
634 goto exit;
635 /* Load the secret value. */
636 status = mbedtls_to_psa_error(
637 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640 /* Validate the private key. */
641 status = mbedtls_to_psa_error(
642 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
643 if( status != PSA_SUCCESS )
644 goto exit;
645 /* Calculate the public key from the private key. */
646 status = mbedtls_to_psa_error(
647 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
648 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
649 if( status != PSA_SUCCESS )
650 goto exit;
651
652 *p_ecp = ecp;
653 return( PSA_SUCCESS );
654
655exit:
656 if( ecp != NULL )
657 {
658 mbedtls_ecp_keypair_free( ecp );
659 mbedtls_free( ecp );
660 }
661 return( status );
662}
663#endif /* defined(MBEDTLS_ECP_C) */
664
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100665/** Import key data into a slot. `slot->type` must have been set
666 * previously. This function assumes that the slot does not contain
667 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100668psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
669 const uint8_t *data,
670 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100671{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200672 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Darryl Green940d72c2018-07-13 13:18:51 +0100674 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100676 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 if( data_length > SIZE_MAX / 8 )
678 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100679 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200680 PSA_BYTES_TO_BITS( data_length ),
681 &slot->data.raw );
682 if( status != PSA_SUCCESS )
683 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200684 if( data_length != 0 )
685 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 }
687 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100688#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100689 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100690 {
Darryl Green940d72c2018-07-13 13:18:51 +0100691 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100692 data, data_length,
693 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100694 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000695 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
696 {
697 status = psa_import_ec_public_key(
698 PSA_KEY_TYPE_GET_CURVE( slot->type ),
699 data, data_length,
700 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000701 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100702 else
703#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000704#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
705 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000707 status = psa_import_rsa_key( slot->type,
708 data, data_length,
709 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 }
711 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000712#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713 {
714 return( PSA_ERROR_NOT_SUPPORTED );
715 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000716 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100717}
718
Darryl Green06fd18d2018-07-16 11:21:11 +0100719/* Retrieve an empty key slot (slot with no key data, but possibly
Jaeden Amero283dfd12019-01-11 12:06:22 +0000720 * with some metadata such as a policy or domain parameters). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100721static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100722 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100723{
724 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100725 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100726
727 *p_slot = NULL;
728
Gilles Peskinec5487a82018-12-03 18:08:14 +0100729 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100730 if( status != PSA_SUCCESS )
731 return( status );
732
733 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200734 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100735
736 *p_slot = slot;
737 return( status );
738}
739
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100740/** Calculate the intersection of two algorithm usage policies.
741 *
742 * Return 0 (which allows no operation) on incompatibility.
743 */
744static psa_algorithm_t psa_key_policy_algorithm_intersection(
745 psa_algorithm_t alg1,
746 psa_algorithm_t alg2 )
747{
748 /* Common case: the policy only allows alg. */
749 if( alg1 == alg2 )
750 return( alg1 );
751 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100752 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100753 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
754 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
755 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
756 {
757 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
758 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100759 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100760 return( alg1 );
761 }
762 /* If the policies are incompatible, allow nothing. */
763 return( 0 );
764}
765
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100766/** Test whether a policy permits an algorithm.
767 *
768 * The caller must test usage flags separately.
769 */
770static int psa_key_policy_permits( const psa_key_policy_t *policy,
771 psa_algorithm_t alg )
772{
773 /* Common case: the policy only allows alg. */
774 if( alg == policy->alg )
775 return( 1 );
776 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
777 * and alg is the same hash-and-sign family with any hash,
778 * then alg is compliant with policy->alg. */
779 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
780 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
781 {
782 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
783 ( alg & ~PSA_ALG_HASH_MASK ) );
784 }
785 /* If it isn't permitted, it's forbidden. */
786 return( 0 );
787}
788
Gilles Peskinef603c712019-01-19 13:40:11 +0100789/** Restrict a key policy based on a constraint.
790 *
791 * \param[in,out] policy The policy to restrict.
792 * \param[in] constraint The policy constraint to apply.
793 *
794 * \retval #PSA_SUCCESS
795 * \c *policy contains the intersection of the original value of
796 * \c *policy and \c *constraint.
797 * \retval #PSA_ERROR_INVALID_ARGUMENT
798 * \c *policy and \c *constraint are incompatible.
799 * \c *policy is unchanged.
800 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100801static psa_status_t psa_restrict_key_policy(
802 psa_key_policy_t *policy,
803 const psa_key_policy_t *constraint )
804{
805 psa_algorithm_t intersection_alg =
806 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
807 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
808 return( PSA_ERROR_INVALID_ARGUMENT );
809 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100810 policy->alg = intersection_alg;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100811 return( PSA_SUCCESS );
812}
813
Darryl Green06fd18d2018-07-16 11:21:11 +0100814/** Retrieve a slot which must contain a key. The key must have allow all the
815 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
816 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100817static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100818 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100819 psa_key_usage_t usage,
820 psa_algorithm_t alg )
821{
822 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100823 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100824
825 *p_slot = NULL;
826
Gilles Peskinec5487a82018-12-03 18:08:14 +0100827 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100828 if( status != PSA_SUCCESS )
829 return( status );
830 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200831 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100832
833 /* Enforce that usage policy for the key slot contains all the flags
834 * required by the usage parameter. There is one exception: public
835 * keys can always be exported, so we treat public key objects as
836 * if they had the export flag. */
837 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
838 usage &= ~PSA_KEY_USAGE_EXPORT;
839 if( ( slot->policy.usage & usage ) != usage )
840 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841
842 /* Enforce that the usage policy permits the requested algortihm. */
843 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100844 return( PSA_ERROR_NOT_PERMITTED );
845
846 *p_slot = slot;
847 return( PSA_SUCCESS );
848}
Darryl Green940d72c2018-07-13 13:18:51 +0100849
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100850/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100851static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000852{
853 if( slot->type == PSA_KEY_TYPE_NONE )
854 {
855 /* No key material to clean. */
856 }
857 else if( key_type_is_raw_bytes( slot->type ) )
858 {
859 mbedtls_free( slot->data.raw.data );
860 }
861 else
862#if defined(MBEDTLS_RSA_C)
863 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
864 {
865 mbedtls_rsa_free( slot->data.rsa );
866 mbedtls_free( slot->data.rsa );
867 }
868 else
869#endif /* defined(MBEDTLS_RSA_C) */
870#if defined(MBEDTLS_ECP_C)
871 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
872 {
873 mbedtls_ecp_keypair_free( slot->data.ecp );
874 mbedtls_free( slot->data.ecp );
875 }
876 else
877#endif /* defined(MBEDTLS_ECP_C) */
878 {
879 /* Shouldn't happen: the key type is not any type that we
880 * put in. */
881 return( PSA_ERROR_TAMPERING_DETECTED );
882 }
883
884 return( PSA_SUCCESS );
885}
886
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100887static void psa_abort_operations_using_key( psa_key_slot_t *slot )
888{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200889 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100890 (void) slot;
891}
892
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100893/** Completely wipe a slot in memory, including its policy.
894 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100895psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100896{
897 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100898 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100899 /* At this point, key material and other type-specific content has
900 * been wiped. Clear remaining metadata. We can call memset and not
901 * zeroize because the metadata is not particularly sensitive. */
902 memset( slot, 0, sizeof( *slot ) );
903 return( status );
904}
905
Gilles Peskine87a5e562019-04-17 12:28:25 +0200906psa_status_t psa_import_key_to_handle( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100907 psa_key_type_t type,
908 const uint8_t *data,
909 size_t data_length )
910{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100911 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100912 psa_status_t status;
913
Gilles Peskinec5487a82018-12-03 18:08:14 +0100914 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100915 if( status != PSA_SUCCESS )
916 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100917
918 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100919
920 status = psa_import_key_into_slot( slot, data, data_length );
921 if( status != PSA_SUCCESS )
922 {
923 slot->type = PSA_KEY_TYPE_NONE;
924 return( status );
925 }
926
Darryl Greend49a4992018-06-18 17:27:26 +0100927#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
928 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
929 {
930 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100931 status = psa_save_persistent_key( slot->persistent_storage_id,
932 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100933 data_length );
934 if( status != PSA_SUCCESS )
935 {
936 (void) psa_remove_key_data_from_memory( slot );
937 slot->type = PSA_KEY_TYPE_NONE;
938 }
939 }
940#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
941
942 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943}
944
Gilles Peskinec5487a82018-12-03 18:08:14 +0100945psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100948 psa_status_t status = PSA_SUCCESS;
949 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950
Gilles Peskinec5487a82018-12-03 18:08:14 +0100951 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200952 if( status != PSA_SUCCESS )
953 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100954#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
955 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
956 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100957 storage_status =
958 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100959 }
960#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100961 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100962 if( status != PSA_SUCCESS )
963 return( status );
964 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100965}
966
Gilles Peskineb870b182018-07-06 16:02:09 +0200967/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100968static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200969{
970 if( key_type_is_raw_bytes( slot->type ) )
971 return( slot->data.raw.bytes * 8 );
972#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100974 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200975#endif /* defined(MBEDTLS_RSA_C) */
976#if defined(MBEDTLS_ECP_C)
977 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
978 return( slot->data.ecp->grp.pbits );
979#endif /* defined(MBEDTLS_ECP_C) */
980 /* Shouldn't happen except on an empty slot. */
981 return( 0 );
982}
983
Gilles Peskinec5487a82018-12-03 18:08:14 +0100984psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200985 psa_key_type_t *type,
986 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100987{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100988 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200989 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200992 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993 if( bits != NULL )
994 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100995 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200996 if( status != PSA_SUCCESS )
997 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200998
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001000 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001001 if( type != NULL )
1002 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001003 if( bits != NULL )
1004 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005 return( PSA_SUCCESS );
1006}
1007
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001008#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001009static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1010 unsigned char *buf, size_t size )
1011{
1012 int ret;
1013 unsigned char *c;
1014 size_t len = 0;
1015
1016 c = buf + size;
1017
1018 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1019
1020 return( (int) len );
1021}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001022#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001023
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001024static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1025 uint8_t *data,
1026 size_t data_size,
1027 size_t *data_length,
1028 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001029{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001030 *data_length = 0;
1031
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001032 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001033 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001034
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001035 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036 {
1037 if( slot->data.raw.bytes > data_size )
1038 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001039 if( data_size != 0 )
1040 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001041 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001042 memset( data + slot->data.raw.bytes, 0,
1043 data_size - slot->data.raw.bytes );
1044 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001045 *data_length = slot->data.raw.bytes;
1046 return( PSA_SUCCESS );
1047 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001048#if defined(MBEDTLS_ECP_C)
1049 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1050 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001051 psa_status_t status;
1052
Gilles Peskine188c71e2018-10-29 19:26:02 +01001053 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1054 if( bytes > data_size )
1055 return( PSA_ERROR_BUFFER_TOO_SMALL );
1056 status = mbedtls_to_psa_error(
1057 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1058 if( status != PSA_SUCCESS )
1059 return( status );
1060 memset( data + bytes, 0, data_size - bytes );
1061 *data_length = bytes;
1062 return( PSA_SUCCESS );
1063 }
1064#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001065 else
Moran Peker17e36e12018-05-02 12:55:20 +03001066 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001067#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001068 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001069 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001070 {
Moran Pekera998bc62018-04-16 18:16:20 +03001071 mbedtls_pk_context pk;
1072 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001073 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001074 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001075#if defined(MBEDTLS_RSA_C)
1076 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001077 pk.pk_info = &mbedtls_rsa_info;
1078 pk.pk_ctx = slot->data.rsa;
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 }
1083 else
1084 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001085#if defined(MBEDTLS_ECP_C)
1086 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001087 pk.pk_info = &mbedtls_eckey_info;
1088 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001089#else
1090 return( PSA_ERROR_NOT_SUPPORTED );
1091#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001092 }
Moran Pekerd7326592018-05-29 16:56:39 +03001093 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001094 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001095 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001096 }
Moran Peker17e36e12018-05-02 12:55:20 +03001097 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001098 {
Moran Peker17e36e12018-05-02 12:55:20 +03001099 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001100 }
Moran Peker60364322018-04-29 11:34:58 +03001101 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001102 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001103 /* If data_size is 0 then data may be NULL and then the
1104 * call to memset would have undefined behavior. */
1105 if( data_size != 0 )
1106 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001107 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001108 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001109 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1110 * Move the data to the beginning and erase remaining data
1111 * at the original location. */
1112 if( 2 * (size_t) ret <= data_size )
1113 {
1114 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001115 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001116 }
1117 else if( (size_t) ret < data_size )
1118 {
1119 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001120 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001121 }
Moran Pekera998bc62018-04-16 18:16:20 +03001122 *data_length = ret;
1123 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001124 }
1125 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001126#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001127 {
1128 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001129 it is valid for a special-purpose implementation to omit
1130 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001131 return( PSA_ERROR_NOT_SUPPORTED );
1132 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001133 }
1134}
1135
Gilles Peskinec5487a82018-12-03 18:08:14 +01001136psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001137 uint8_t *data,
1138 size_t data_size,
1139 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001140{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001141 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001142 psa_status_t status;
1143
1144 /* Set the key to empty now, so that even when there are errors, we always
1145 * set data_length to a value between 0 and data_size. On error, setting
1146 * the key to empty is a good choice because an empty key representation is
1147 * unlikely to be accepted anywhere. */
1148 *data_length = 0;
1149
1150 /* Export requires the EXPORT flag. There is an exception for public keys,
1151 * which don't require any flag, but psa_get_key_from_slot takes
1152 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001153 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001154 if( status != PSA_SUCCESS )
1155 return( status );
1156 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001157 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001158}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001159
Gilles Peskinec5487a82018-12-03 18:08:14 +01001160psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001161 uint8_t *data,
1162 size_t data_size,
1163 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001164{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001165 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001166 psa_status_t status;
1167
1168 /* Set the key to empty now, so that even when there are errors, we always
1169 * set data_length to a value between 0 and data_size. On error, setting
1170 * the key to empty is a good choice because an empty key representation is
1171 * unlikely to be accepted anywhere. */
1172 *data_length = 0;
1173
1174 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001175 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001176 if( status != PSA_SUCCESS )
1177 return( status );
1178 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001179 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001180}
1181
Darryl Green0c6575a2018-11-07 16:05:30 +00001182#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001183static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001184 size_t bits )
1185{
1186 psa_status_t status;
1187 uint8_t *data;
1188 size_t key_length;
1189 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1190 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001191 if( data == NULL )
1192 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001193 /* Get key data in export format */
1194 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1195 if( status != PSA_SUCCESS )
1196 {
1197 slot->type = PSA_KEY_TYPE_NONE;
1198 goto exit;
1199 }
1200 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001201 status = psa_save_persistent_key( slot->persistent_storage_id,
1202 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001203 data, key_length );
1204 if( status != PSA_SUCCESS )
1205 {
1206 slot->type = PSA_KEY_TYPE_NONE;
1207 }
1208exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001209 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001210 mbedtls_free( data );
1211 return( status );
1212}
1213#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1214
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001215static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1216 psa_key_handle_t target )
1217{
1218 psa_status_t status;
1219 uint8_t *buffer = NULL;
1220 size_t buffer_size = 0;
1221 size_t length;
1222
1223 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1224 psa_get_key_bits( source ) );
1225 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001226 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001227 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001228 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1229 if( status != PSA_SUCCESS )
1230 goto exit;
Gilles Peskine87a5e562019-04-17 12:28:25 +02001231 status = psa_import_key_to_handle( target, source->type, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001232
1233exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001234 if( buffer_size != 0 )
1235 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001236 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001237 return( status );
1238}
1239
Gilles Peskine87a5e562019-04-17 12:28:25 +02001240psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001241 psa_key_handle_t target_handle,
1242 const psa_key_policy_t *constraint)
1243{
1244 psa_key_slot_t *source_slot = NULL;
1245 psa_key_slot_t *target_slot = NULL;
1246 psa_key_policy_t new_policy;
1247 psa_status_t status;
1248 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1249 if( status != PSA_SUCCESS )
1250 return( status );
1251 status = psa_get_empty_key_slot( target_handle, &target_slot );
1252 if( status != PSA_SUCCESS )
1253 return( status );
1254
1255 new_policy = target_slot->policy;
1256 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1257 if( status != PSA_SUCCESS )
1258 return( status );
1259 if( constraint != NULL )
1260 {
1261 status = psa_restrict_key_policy( &new_policy, constraint );
1262 if( status != PSA_SUCCESS )
1263 return( status );
1264 }
1265
1266 status = psa_copy_key_material( source_slot, target_handle );
1267 if( status != PSA_SUCCESS )
1268 return( status );
1269
1270 target_slot->policy = new_policy;
1271 return( PSA_SUCCESS );
1272}
1273
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001274
1275
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001276/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001277/* Message digests */
1278/****************************************************************/
1279
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001280static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001281{
1282 switch( alg )
1283 {
1284#if defined(MBEDTLS_MD2_C)
1285 case PSA_ALG_MD2:
1286 return( &mbedtls_md2_info );
1287#endif
1288#if defined(MBEDTLS_MD4_C)
1289 case PSA_ALG_MD4:
1290 return( &mbedtls_md4_info );
1291#endif
1292#if defined(MBEDTLS_MD5_C)
1293 case PSA_ALG_MD5:
1294 return( &mbedtls_md5_info );
1295#endif
1296#if defined(MBEDTLS_RIPEMD160_C)
1297 case PSA_ALG_RIPEMD160:
1298 return( &mbedtls_ripemd160_info );
1299#endif
1300#if defined(MBEDTLS_SHA1_C)
1301 case PSA_ALG_SHA_1:
1302 return( &mbedtls_sha1_info );
1303#endif
1304#if defined(MBEDTLS_SHA256_C)
1305 case PSA_ALG_SHA_224:
1306 return( &mbedtls_sha224_info );
1307 case PSA_ALG_SHA_256:
1308 return( &mbedtls_sha256_info );
1309#endif
1310#if defined(MBEDTLS_SHA512_C)
1311 case PSA_ALG_SHA_384:
1312 return( &mbedtls_sha384_info );
1313 case PSA_ALG_SHA_512:
1314 return( &mbedtls_sha512_info );
1315#endif
1316 default:
1317 return( NULL );
1318 }
1319}
1320
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001321psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1322{
1323 switch( operation->alg )
1324 {
Gilles Peskine81736312018-06-26 15:04:31 +02001325 case 0:
1326 /* The object has (apparently) been initialized but it is not
1327 * in use. It's ok to call abort on such an object, and there's
1328 * nothing to do. */
1329 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001330#if defined(MBEDTLS_MD2_C)
1331 case PSA_ALG_MD2:
1332 mbedtls_md2_free( &operation->ctx.md2 );
1333 break;
1334#endif
1335#if defined(MBEDTLS_MD4_C)
1336 case PSA_ALG_MD4:
1337 mbedtls_md4_free( &operation->ctx.md4 );
1338 break;
1339#endif
1340#if defined(MBEDTLS_MD5_C)
1341 case PSA_ALG_MD5:
1342 mbedtls_md5_free( &operation->ctx.md5 );
1343 break;
1344#endif
1345#if defined(MBEDTLS_RIPEMD160_C)
1346 case PSA_ALG_RIPEMD160:
1347 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1348 break;
1349#endif
1350#if defined(MBEDTLS_SHA1_C)
1351 case PSA_ALG_SHA_1:
1352 mbedtls_sha1_free( &operation->ctx.sha1 );
1353 break;
1354#endif
1355#if defined(MBEDTLS_SHA256_C)
1356 case PSA_ALG_SHA_224:
1357 case PSA_ALG_SHA_256:
1358 mbedtls_sha256_free( &operation->ctx.sha256 );
1359 break;
1360#endif
1361#if defined(MBEDTLS_SHA512_C)
1362 case PSA_ALG_SHA_384:
1363 case PSA_ALG_SHA_512:
1364 mbedtls_sha512_free( &operation->ctx.sha512 );
1365 break;
1366#endif
1367 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001368 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001369 }
1370 operation->alg = 0;
1371 return( PSA_SUCCESS );
1372}
1373
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001374psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001375 psa_algorithm_t alg )
1376{
1377 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001378
1379 /* A context must be freshly initialized before it can be set up. */
1380 if( operation->alg != 0 )
1381 {
1382 return( PSA_ERROR_BAD_STATE );
1383 }
1384
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001385 switch( alg )
1386 {
1387#if defined(MBEDTLS_MD2_C)
1388 case PSA_ALG_MD2:
1389 mbedtls_md2_init( &operation->ctx.md2 );
1390 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1391 break;
1392#endif
1393#if defined(MBEDTLS_MD4_C)
1394 case PSA_ALG_MD4:
1395 mbedtls_md4_init( &operation->ctx.md4 );
1396 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1397 break;
1398#endif
1399#if defined(MBEDTLS_MD5_C)
1400 case PSA_ALG_MD5:
1401 mbedtls_md5_init( &operation->ctx.md5 );
1402 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1403 break;
1404#endif
1405#if defined(MBEDTLS_RIPEMD160_C)
1406 case PSA_ALG_RIPEMD160:
1407 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1408 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1409 break;
1410#endif
1411#if defined(MBEDTLS_SHA1_C)
1412 case PSA_ALG_SHA_1:
1413 mbedtls_sha1_init( &operation->ctx.sha1 );
1414 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1415 break;
1416#endif
1417#if defined(MBEDTLS_SHA256_C)
1418 case PSA_ALG_SHA_224:
1419 mbedtls_sha256_init( &operation->ctx.sha256 );
1420 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1421 break;
1422 case PSA_ALG_SHA_256:
1423 mbedtls_sha256_init( &operation->ctx.sha256 );
1424 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1425 break;
1426#endif
1427#if defined(MBEDTLS_SHA512_C)
1428 case PSA_ALG_SHA_384:
1429 mbedtls_sha512_init( &operation->ctx.sha512 );
1430 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1431 break;
1432 case PSA_ALG_SHA_512:
1433 mbedtls_sha512_init( &operation->ctx.sha512 );
1434 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1435 break;
1436#endif
1437 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001438 return( PSA_ALG_IS_HASH( alg ) ?
1439 PSA_ERROR_NOT_SUPPORTED :
1440 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001441 }
1442 if( ret == 0 )
1443 operation->alg = alg;
1444 else
1445 psa_hash_abort( operation );
1446 return( mbedtls_to_psa_error( ret ) );
1447}
1448
1449psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1450 const uint8_t *input,
1451 size_t input_length )
1452{
1453 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001454
1455 /* Don't require hash implementations to behave correctly on a
1456 * zero-length input, which may have an invalid pointer. */
1457 if( input_length == 0 )
1458 return( PSA_SUCCESS );
1459
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001460 switch( operation->alg )
1461 {
1462#if defined(MBEDTLS_MD2_C)
1463 case PSA_ALG_MD2:
1464 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1465 input, input_length );
1466 break;
1467#endif
1468#if defined(MBEDTLS_MD4_C)
1469 case PSA_ALG_MD4:
1470 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1471 input, input_length );
1472 break;
1473#endif
1474#if defined(MBEDTLS_MD5_C)
1475 case PSA_ALG_MD5:
1476 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1477 input, input_length );
1478 break;
1479#endif
1480#if defined(MBEDTLS_RIPEMD160_C)
1481 case PSA_ALG_RIPEMD160:
1482 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1483 input, input_length );
1484 break;
1485#endif
1486#if defined(MBEDTLS_SHA1_C)
1487 case PSA_ALG_SHA_1:
1488 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1489 input, input_length );
1490 break;
1491#endif
1492#if defined(MBEDTLS_SHA256_C)
1493 case PSA_ALG_SHA_224:
1494 case PSA_ALG_SHA_256:
1495 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1496 input, input_length );
1497 break;
1498#endif
1499#if defined(MBEDTLS_SHA512_C)
1500 case PSA_ALG_SHA_384:
1501 case PSA_ALG_SHA_512:
1502 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1503 input, input_length );
1504 break;
1505#endif
1506 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001507 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001508 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001509
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001510 if( ret != 0 )
1511 psa_hash_abort( operation );
1512 return( mbedtls_to_psa_error( ret ) );
1513}
1514
1515psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1516 uint8_t *hash,
1517 size_t hash_size,
1518 size_t *hash_length )
1519{
itayzafrir40835d42018-08-02 13:14:17 +03001520 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001521 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001522 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001523
1524 /* Fill the output buffer with something that isn't a valid hash
1525 * (barring an attack on the hash and deliberately-crafted input),
1526 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001527 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001528 /* If hash_size is 0 then hash may be NULL and then the
1529 * call to memset would have undefined behavior. */
1530 if( hash_size != 0 )
1531 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001532
1533 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001534 {
1535 status = PSA_ERROR_BUFFER_TOO_SMALL;
1536 goto exit;
1537 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001538
1539 switch( operation->alg )
1540 {
1541#if defined(MBEDTLS_MD2_C)
1542 case PSA_ALG_MD2:
1543 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1544 break;
1545#endif
1546#if defined(MBEDTLS_MD4_C)
1547 case PSA_ALG_MD4:
1548 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1549 break;
1550#endif
1551#if defined(MBEDTLS_MD5_C)
1552 case PSA_ALG_MD5:
1553 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1554 break;
1555#endif
1556#if defined(MBEDTLS_RIPEMD160_C)
1557 case PSA_ALG_RIPEMD160:
1558 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1559 break;
1560#endif
1561#if defined(MBEDTLS_SHA1_C)
1562 case PSA_ALG_SHA_1:
1563 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1564 break;
1565#endif
1566#if defined(MBEDTLS_SHA256_C)
1567 case PSA_ALG_SHA_224:
1568 case PSA_ALG_SHA_256:
1569 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1570 break;
1571#endif
1572#if defined(MBEDTLS_SHA512_C)
1573 case PSA_ALG_SHA_384:
1574 case PSA_ALG_SHA_512:
1575 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1576 break;
1577#endif
1578 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001579 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001580 }
itayzafrir40835d42018-08-02 13:14:17 +03001581 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001582
itayzafrir40835d42018-08-02 13:14:17 +03001583exit:
1584 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001585 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001586 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001587 return( psa_hash_abort( operation ) );
1588 }
1589 else
1590 {
1591 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001592 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001593 }
1594}
1595
Gilles Peskine2d277862018-06-18 15:41:12 +02001596psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1597 const uint8_t *hash,
1598 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001599{
1600 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1601 size_t actual_hash_length;
1602 psa_status_t status = psa_hash_finish( operation,
1603 actual_hash, sizeof( actual_hash ),
1604 &actual_hash_length );
1605 if( status != PSA_SUCCESS )
1606 return( status );
1607 if( actual_hash_length != hash_length )
1608 return( PSA_ERROR_INVALID_SIGNATURE );
1609 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1610 return( PSA_ERROR_INVALID_SIGNATURE );
1611 return( PSA_SUCCESS );
1612}
1613
Gilles Peskineeb35d782019-01-22 17:56:16 +01001614psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1615 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001616{
1617 if( target_operation->alg != 0 )
1618 return( PSA_ERROR_BAD_STATE );
1619
1620 switch( source_operation->alg )
1621 {
1622 case 0:
1623 return( PSA_ERROR_BAD_STATE );
1624#if defined(MBEDTLS_MD2_C)
1625 case PSA_ALG_MD2:
1626 mbedtls_md2_clone( &target_operation->ctx.md2,
1627 &source_operation->ctx.md2 );
1628 break;
1629#endif
1630#if defined(MBEDTLS_MD4_C)
1631 case PSA_ALG_MD4:
1632 mbedtls_md4_clone( &target_operation->ctx.md4,
1633 &source_operation->ctx.md4 );
1634 break;
1635#endif
1636#if defined(MBEDTLS_MD5_C)
1637 case PSA_ALG_MD5:
1638 mbedtls_md5_clone( &target_operation->ctx.md5,
1639 &source_operation->ctx.md5 );
1640 break;
1641#endif
1642#if defined(MBEDTLS_RIPEMD160_C)
1643 case PSA_ALG_RIPEMD160:
1644 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1645 &source_operation->ctx.ripemd160 );
1646 break;
1647#endif
1648#if defined(MBEDTLS_SHA1_C)
1649 case PSA_ALG_SHA_1:
1650 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1651 &source_operation->ctx.sha1 );
1652 break;
1653#endif
1654#if defined(MBEDTLS_SHA256_C)
1655 case PSA_ALG_SHA_224:
1656 case PSA_ALG_SHA_256:
1657 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1658 &source_operation->ctx.sha256 );
1659 break;
1660#endif
1661#if defined(MBEDTLS_SHA512_C)
1662 case PSA_ALG_SHA_384:
1663 case PSA_ALG_SHA_512:
1664 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1665 &source_operation->ctx.sha512 );
1666 break;
1667#endif
1668 default:
1669 return( PSA_ERROR_NOT_SUPPORTED );
1670 }
1671
1672 target_operation->alg = source_operation->alg;
1673 return( PSA_SUCCESS );
1674}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001675
1676
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001677/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678/* MAC */
1679/****************************************************************/
1680
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001681static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001682 psa_algorithm_t alg,
1683 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001684 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001685 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001687 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001688 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001689
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001690 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001691 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001692
Gilles Peskine8c9def32018-02-08 10:02:12 +01001693 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1694 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001695 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001696 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001697 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001698 mode = MBEDTLS_MODE_STREAM;
1699 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001700 case PSA_ALG_CTR:
1701 mode = MBEDTLS_MODE_CTR;
1702 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001703 case PSA_ALG_CFB:
1704 mode = MBEDTLS_MODE_CFB;
1705 break;
1706 case PSA_ALG_OFB:
1707 mode = MBEDTLS_MODE_OFB;
1708 break;
1709 case PSA_ALG_CBC_NO_PADDING:
1710 mode = MBEDTLS_MODE_CBC;
1711 break;
1712 case PSA_ALG_CBC_PKCS7:
1713 mode = MBEDTLS_MODE_CBC;
1714 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001715 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001716 mode = MBEDTLS_MODE_CCM;
1717 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001718 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001719 mode = MBEDTLS_MODE_GCM;
1720 break;
1721 default:
1722 return( NULL );
1723 }
1724 }
1725 else if( alg == PSA_ALG_CMAC )
1726 mode = MBEDTLS_MODE_ECB;
1727 else if( alg == PSA_ALG_GMAC )
1728 mode = MBEDTLS_MODE_GCM;
1729 else
1730 return( NULL );
1731
1732 switch( key_type )
1733 {
1734 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001735 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001736 break;
1737 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001738 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1739 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001740 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001741 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001742 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001743 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001744 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1745 * but two-key Triple-DES is functionally three-key Triple-DES
1746 * with K1=K3, so that's how we present it to mbedtls. */
1747 if( key_bits == 128 )
1748 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001749 break;
1750 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001751 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001752 break;
1753 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001754 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001755 break;
1756 default:
1757 return( NULL );
1758 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001759 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001760 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001761
Jaeden Amero23bbb752018-06-26 14:16:54 +01001762 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1763 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001764}
1765
Gilles Peskinea05219c2018-11-16 16:02:56 +01001766#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001767static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001768{
Gilles Peskine2d277862018-06-18 15:41:12 +02001769 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001770 {
1771 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001772 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001773 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001774 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001775 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001776 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001777 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001778 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001779 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001780 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001781 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001782 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001783 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001784 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001785 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001786 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001787 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001788 return( 128 );
1789 default:
1790 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001791 }
1792}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001793#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001794
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001795/* Initialize the MAC operation structure. Once this function has been
1796 * called, psa_mac_abort can run and will do the right thing. */
1797static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1798 psa_algorithm_t alg )
1799{
1800 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1801
1802 operation->alg = alg;
1803 operation->key_set = 0;
1804 operation->iv_set = 0;
1805 operation->iv_required = 0;
1806 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001807 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001808
1809#if defined(MBEDTLS_CMAC_C)
1810 if( alg == PSA_ALG_CMAC )
1811 {
1812 operation->iv_required = 0;
1813 mbedtls_cipher_init( &operation->ctx.cmac );
1814 status = PSA_SUCCESS;
1815 }
1816 else
1817#endif /* MBEDTLS_CMAC_C */
1818#if defined(MBEDTLS_MD_C)
1819 if( PSA_ALG_IS_HMAC( operation->alg ) )
1820 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001821 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1822 operation->ctx.hmac.hash_ctx.alg = 0;
1823 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001824 }
1825 else
1826#endif /* MBEDTLS_MD_C */
1827 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001828 if( ! PSA_ALG_IS_MAC( alg ) )
1829 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001830 }
1831
1832 if( status != PSA_SUCCESS )
1833 memset( operation, 0, sizeof( *operation ) );
1834 return( status );
1835}
1836
Gilles Peskine01126fa2018-07-12 17:04:55 +02001837#if defined(MBEDTLS_MD_C)
1838static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1839{
Gilles Peskine3f108122018-12-07 18:14:53 +01001840 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001841 return( psa_hash_abort( &hmac->hash_ctx ) );
1842}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001843
1844static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1845{
1846 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1847 memset( hmac, 0, sizeof( *hmac ) );
1848}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001849#endif /* MBEDTLS_MD_C */
1850
Gilles Peskine8c9def32018-02-08 10:02:12 +01001851psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1852{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001853 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001854 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001855 /* The object has (apparently) been initialized but it is not
1856 * in use. It's ok to call abort on such an object, and there's
1857 * nothing to do. */
1858 return( PSA_SUCCESS );
1859 }
1860 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001861#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001862 if( operation->alg == PSA_ALG_CMAC )
1863 {
1864 mbedtls_cipher_free( &operation->ctx.cmac );
1865 }
1866 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001867#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001868#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001869 if( PSA_ALG_IS_HMAC( operation->alg ) )
1870 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001871 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001872 }
1873 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001874#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001875 {
1876 /* Sanity check (shouldn't happen: operation->alg should
1877 * always have been initialized to a valid value). */
1878 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001879 }
Moran Peker41deec42018-04-04 15:43:05 +03001880
Gilles Peskine8c9def32018-02-08 10:02:12 +01001881 operation->alg = 0;
1882 operation->key_set = 0;
1883 operation->iv_set = 0;
1884 operation->iv_required = 0;
1885 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001886 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001887
Gilles Peskine8c9def32018-02-08 10:02:12 +01001888 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001889
1890bad_state:
1891 /* If abort is called on an uninitialized object, we can't trust
1892 * anything. Wipe the object in case it contains confidential data.
1893 * This may result in a memory leak if a pointer gets overwritten,
1894 * but it's too late to do anything about this. */
1895 memset( operation, 0, sizeof( *operation ) );
1896 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001897}
1898
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001899#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001900static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001901 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001902 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001903 const mbedtls_cipher_info_t *cipher_info )
1904{
1905 int ret;
1906
1907 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001908
1909 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1910 if( ret != 0 )
1911 return( ret );
1912
1913 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1914 slot->data.raw.data,
1915 key_bits );
1916 return( ret );
1917}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001918#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001919
Gilles Peskine248051a2018-06-20 16:09:38 +02001920#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001921static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1922 const uint8_t *key,
1923 size_t key_length,
1924 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001925{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001926 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001927 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001928 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001929 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001930 psa_status_t status;
1931
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001932 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1933 * overflow below. This should never trigger if the hash algorithm
1934 * is implemented correctly. */
1935 /* The size checks against the ipad and opad buffers cannot be written
1936 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1937 * because that triggers -Wlogical-op on GCC 7.3. */
1938 if( block_size > sizeof( ipad ) )
1939 return( PSA_ERROR_NOT_SUPPORTED );
1940 if( block_size > sizeof( hmac->opad ) )
1941 return( PSA_ERROR_NOT_SUPPORTED );
1942 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001943 return( PSA_ERROR_NOT_SUPPORTED );
1944
Gilles Peskined223b522018-06-11 18:12:58 +02001945 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001946 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001947 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001948 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001949 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001950 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001951 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001952 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001953 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001954 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001955 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001956 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001957 }
Gilles Peskine96889972018-07-12 17:07:03 +02001958 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1959 * but it is permitted. It is common when HMAC is used in HKDF, for
1960 * example. Don't call `memcpy` in the 0-length because `key` could be
1961 * an invalid pointer which would make the behavior undefined. */
1962 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001963 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001964
Gilles Peskined223b522018-06-11 18:12:58 +02001965 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1966 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001967 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001968 ipad[i] ^= 0x36;
1969 memset( ipad + key_length, 0x36, block_size - key_length );
1970
1971 /* Copy the key material from ipad to opad, flipping the requisite bits,
1972 * and filling the rest of opad with the requisite constant. */
1973 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001974 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1975 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001976
Gilles Peskine01126fa2018-07-12 17:04:55 +02001977 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001978 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001979 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001980
Gilles Peskine01126fa2018-07-12 17:04:55 +02001981 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001982
1983cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001984 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001985
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001986 return( status );
1987}
Gilles Peskine248051a2018-06-20 16:09:38 +02001988#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001989
Gilles Peskine89167cb2018-07-08 20:12:23 +02001990static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001991 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001992 psa_algorithm_t alg,
1993 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001994{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001995 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001996 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001997 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001998 psa_key_usage_t usage =
1999 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002000 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002001 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002002
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002003 /* A context must be freshly initialized before it can be set up. */
2004 if( operation->alg != 0 )
2005 {
2006 return( PSA_ERROR_BAD_STATE );
2007 }
2008
Gilles Peskined911eb72018-08-14 15:18:45 +02002009 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002010 if( status != PSA_SUCCESS )
2011 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002012 if( is_sign )
2013 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002014
Gilles Peskinec5487a82018-12-03 18:08:14 +01002015 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002016 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002017 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002018 key_bits = psa_get_key_bits( slot );
2019
Gilles Peskine8c9def32018-02-08 10:02:12 +01002020#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002021 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002022 {
2023 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002024 mbedtls_cipher_info_from_psa( full_length_alg,
2025 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002026 int ret;
2027 if( cipher_info == NULL )
2028 {
2029 status = PSA_ERROR_NOT_SUPPORTED;
2030 goto exit;
2031 }
2032 operation->mac_size = cipher_info->block_size;
2033 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2034 status = mbedtls_to_psa_error( ret );
2035 }
2036 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002037#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002038#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002039 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002040 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002041 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002042 if( hash_alg == 0 )
2043 {
2044 status = PSA_ERROR_NOT_SUPPORTED;
2045 goto exit;
2046 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002047
2048 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2049 /* Sanity check. This shouldn't fail on a valid configuration. */
2050 if( operation->mac_size == 0 ||
2051 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2052 {
2053 status = PSA_ERROR_NOT_SUPPORTED;
2054 goto exit;
2055 }
2056
Gilles Peskine01126fa2018-07-12 17:04:55 +02002057 if( slot->type != PSA_KEY_TYPE_HMAC )
2058 {
2059 status = PSA_ERROR_INVALID_ARGUMENT;
2060 goto exit;
2061 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002062
Gilles Peskine01126fa2018-07-12 17:04:55 +02002063 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2064 slot->data.raw.data,
2065 slot->data.raw.bytes,
2066 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002067 }
2068 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002069#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002070 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002071 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002072 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002073 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002074
Gilles Peskined911eb72018-08-14 15:18:45 +02002075 if( truncated == 0 )
2076 {
2077 /* The "normal" case: untruncated algorithm. Nothing to do. */
2078 }
2079 else if( truncated < 4 )
2080 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002081 /* A very short MAC is too short for security since it can be
2082 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2083 * so we make this our minimum, even though 32 bits is still
2084 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002085 status = PSA_ERROR_NOT_SUPPORTED;
2086 }
2087 else if( truncated > operation->mac_size )
2088 {
2089 /* It's impossible to "truncate" to a larger length. */
2090 status = PSA_ERROR_INVALID_ARGUMENT;
2091 }
2092 else
2093 operation->mac_size = truncated;
2094
Gilles Peskinefbfac682018-07-08 20:51:54 +02002095exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002096 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002097 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002098 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002099 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002100 else
2101 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002102 operation->key_set = 1;
2103 }
2104 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002105}
2106
Gilles Peskine89167cb2018-07-08 20:12:23 +02002107psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002108 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002109 psa_algorithm_t alg )
2110{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002111 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002112}
2113
2114psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002115 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002116 psa_algorithm_t alg )
2117{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002118 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002119}
2120
Gilles Peskine8c9def32018-02-08 10:02:12 +01002121psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2122 const uint8_t *input,
2123 size_t input_length )
2124{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002125 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002126 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002127 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002129 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002130 operation->has_input = 1;
2131
Gilles Peskine8c9def32018-02-08 10:02:12 +01002132#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002133 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002134 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002135 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2136 input, input_length );
2137 status = mbedtls_to_psa_error( ret );
2138 }
2139 else
2140#endif /* MBEDTLS_CMAC_C */
2141#if defined(MBEDTLS_MD_C)
2142 if( PSA_ALG_IS_HMAC( operation->alg ) )
2143 {
2144 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2145 input_length );
2146 }
2147 else
2148#endif /* MBEDTLS_MD_C */
2149 {
2150 /* This shouldn't happen if `operation` was initialized by
2151 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002152 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002153 }
2154
Gilles Peskinefbfac682018-07-08 20:51:54 +02002155 if( status != PSA_SUCCESS )
2156 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002157 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002158}
2159
Gilles Peskine01126fa2018-07-12 17:04:55 +02002160#if defined(MBEDTLS_MD_C)
2161static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2162 uint8_t *mac,
2163 size_t mac_size )
2164{
2165 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2166 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2167 size_t hash_size = 0;
2168 size_t block_size = psa_get_hash_block_size( hash_alg );
2169 psa_status_t status;
2170
Gilles Peskine01126fa2018-07-12 17:04:55 +02002171 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2172 if( status != PSA_SUCCESS )
2173 return( status );
2174 /* From here on, tmp needs to be wiped. */
2175
2176 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2177 if( status != PSA_SUCCESS )
2178 goto exit;
2179
2180 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2181 if( status != PSA_SUCCESS )
2182 goto exit;
2183
2184 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2185 if( status != PSA_SUCCESS )
2186 goto exit;
2187
Gilles Peskined911eb72018-08-14 15:18:45 +02002188 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2189 if( status != PSA_SUCCESS )
2190 goto exit;
2191
2192 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002193
2194exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002195 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002196 return( status );
2197}
2198#endif /* MBEDTLS_MD_C */
2199
mohammad16036df908f2018-04-02 08:34:15 -07002200static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002201 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002202 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002203{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002204 if( ! operation->key_set )
2205 return( PSA_ERROR_BAD_STATE );
2206 if( operation->iv_required && ! operation->iv_set )
2207 return( PSA_ERROR_BAD_STATE );
2208
Gilles Peskine8c9def32018-02-08 10:02:12 +01002209 if( mac_size < operation->mac_size )
2210 return( PSA_ERROR_BUFFER_TOO_SMALL );
2211
Gilles Peskine8c9def32018-02-08 10:02:12 +01002212#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002213 if( operation->alg == PSA_ALG_CMAC )
2214 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002215 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2216 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2217 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002218 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002219 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002220 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002221 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002222 else
2223#endif /* MBEDTLS_CMAC_C */
2224#if defined(MBEDTLS_MD_C)
2225 if( PSA_ALG_IS_HMAC( operation->alg ) )
2226 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002227 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002228 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002229 }
2230 else
2231#endif /* MBEDTLS_MD_C */
2232 {
2233 /* This shouldn't happen if `operation` was initialized by
2234 * a setup function. */
2235 return( PSA_ERROR_BAD_STATE );
2236 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002237}
2238
Gilles Peskineacd4be32018-07-08 19:56:25 +02002239psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2240 uint8_t *mac,
2241 size_t mac_size,
2242 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002243{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002244 psa_status_t status;
2245
Jaeden Amero252ef282019-02-15 14:05:35 +00002246 if( operation->alg == 0 )
2247 {
2248 return( PSA_ERROR_BAD_STATE );
2249 }
2250
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002251 /* Fill the output buffer with something that isn't a valid mac
2252 * (barring an attack on the mac and deliberately-crafted input),
2253 * in case the caller doesn't check the return status properly. */
2254 *mac_length = mac_size;
2255 /* If mac_size is 0 then mac may be NULL and then the
2256 * call to memset would have undefined behavior. */
2257 if( mac_size != 0 )
2258 memset( mac, '!', mac_size );
2259
Gilles Peskine89167cb2018-07-08 20:12:23 +02002260 if( ! operation->is_sign )
2261 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002262 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002263 }
mohammad16036df908f2018-04-02 08:34:15 -07002264
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002265 status = psa_mac_finish_internal( operation, mac, mac_size );
2266
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002267 if( status == PSA_SUCCESS )
2268 {
2269 status = psa_mac_abort( operation );
2270 if( status == PSA_SUCCESS )
2271 *mac_length = operation->mac_size;
2272 else
2273 memset( mac, '!', mac_size );
2274 }
2275 else
2276 psa_mac_abort( operation );
2277 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002278}
2279
Gilles Peskineacd4be32018-07-08 19:56:25 +02002280psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2281 const uint8_t *mac,
2282 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002283{
Gilles Peskine828ed142018-06-18 23:25:51 +02002284 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002285 psa_status_t status;
2286
Jaeden Amero252ef282019-02-15 14:05:35 +00002287 if( operation->alg == 0 )
2288 {
2289 return( PSA_ERROR_BAD_STATE );
2290 }
2291
Gilles Peskine89167cb2018-07-08 20:12:23 +02002292 if( operation->is_sign )
2293 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002294 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002295 }
2296 if( operation->mac_size != mac_length )
2297 {
2298 status = PSA_ERROR_INVALID_SIGNATURE;
2299 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002300 }
mohammad16036df908f2018-04-02 08:34:15 -07002301
2302 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002303 actual_mac, sizeof( actual_mac ) );
2304
2305 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2306 status = PSA_ERROR_INVALID_SIGNATURE;
2307
2308cleanup:
2309 if( status == PSA_SUCCESS )
2310 status = psa_mac_abort( operation );
2311 else
2312 psa_mac_abort( operation );
2313
Gilles Peskine3f108122018-12-07 18:14:53 +01002314 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002315
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002316 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002317}
2318
2319
Gilles Peskine20035e32018-02-03 22:44:14 +01002320
Gilles Peskine20035e32018-02-03 22:44:14 +01002321/****************************************************************/
2322/* Asymmetric cryptography */
2323/****************************************************************/
2324
Gilles Peskine2b450e32018-06-27 15:42:46 +02002325#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002326/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002327 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002328static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2329 size_t hash_length,
2330 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002331{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002332 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002333 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002334 *md_alg = mbedtls_md_get_type( md_info );
2335
2336 /* The Mbed TLS RSA module uses an unsigned int for hash length
2337 * parameters. Validate that it fits so that we don't risk an
2338 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002339#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002340 if( hash_length > UINT_MAX )
2341 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002342#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002343
2344#if defined(MBEDTLS_PKCS1_V15)
2345 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2346 * must be correct. */
2347 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2348 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002349 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002350 if( md_info == NULL )
2351 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002352 if( mbedtls_md_get_size( md_info ) != hash_length )
2353 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002354 }
2355#endif /* MBEDTLS_PKCS1_V15 */
2356
2357#if defined(MBEDTLS_PKCS1_V21)
2358 /* PSS requires a hash internally. */
2359 if( PSA_ALG_IS_RSA_PSS( alg ) )
2360 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002361 if( md_info == NULL )
2362 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002363 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002364#endif /* MBEDTLS_PKCS1_V21 */
2365
Gilles Peskine61b91d42018-06-08 16:09:36 +02002366 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002367}
2368
Gilles Peskine2b450e32018-06-27 15:42:46 +02002369static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2370 psa_algorithm_t alg,
2371 const uint8_t *hash,
2372 size_t hash_length,
2373 uint8_t *signature,
2374 size_t signature_size,
2375 size_t *signature_length )
2376{
2377 psa_status_t status;
2378 int ret;
2379 mbedtls_md_type_t md_alg;
2380
2381 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2382 if( status != PSA_SUCCESS )
2383 return( status );
2384
Gilles Peskine630a18a2018-06-29 17:49:35 +02002385 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002386 return( PSA_ERROR_BUFFER_TOO_SMALL );
2387
2388#if defined(MBEDTLS_PKCS1_V15)
2389 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2390 {
2391 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2392 MBEDTLS_MD_NONE );
2393 ret = mbedtls_rsa_pkcs1_sign( rsa,
2394 mbedtls_ctr_drbg_random,
2395 &global_data.ctr_drbg,
2396 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002397 md_alg,
2398 (unsigned int) hash_length,
2399 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002400 signature );
2401 }
2402 else
2403#endif /* MBEDTLS_PKCS1_V15 */
2404#if defined(MBEDTLS_PKCS1_V21)
2405 if( PSA_ALG_IS_RSA_PSS( alg ) )
2406 {
2407 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2408 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2409 mbedtls_ctr_drbg_random,
2410 &global_data.ctr_drbg,
2411 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002412 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002413 (unsigned int) hash_length,
2414 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002415 signature );
2416 }
2417 else
2418#endif /* MBEDTLS_PKCS1_V21 */
2419 {
2420 return( PSA_ERROR_INVALID_ARGUMENT );
2421 }
2422
2423 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002424 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002425 return( mbedtls_to_psa_error( ret ) );
2426}
2427
2428static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2429 psa_algorithm_t alg,
2430 const uint8_t *hash,
2431 size_t hash_length,
2432 const uint8_t *signature,
2433 size_t signature_length )
2434{
2435 psa_status_t status;
2436 int ret;
2437 mbedtls_md_type_t md_alg;
2438
2439 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2440 if( status != PSA_SUCCESS )
2441 return( status );
2442
Gilles Peskine630a18a2018-06-29 17:49:35 +02002443 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002444 return( PSA_ERROR_BUFFER_TOO_SMALL );
2445
2446#if defined(MBEDTLS_PKCS1_V15)
2447 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2448 {
2449 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2450 MBEDTLS_MD_NONE );
2451 ret = mbedtls_rsa_pkcs1_verify( rsa,
2452 mbedtls_ctr_drbg_random,
2453 &global_data.ctr_drbg,
2454 MBEDTLS_RSA_PUBLIC,
2455 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002456 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002457 hash,
2458 signature );
2459 }
2460 else
2461#endif /* MBEDTLS_PKCS1_V15 */
2462#if defined(MBEDTLS_PKCS1_V21)
2463 if( PSA_ALG_IS_RSA_PSS( alg ) )
2464 {
2465 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2466 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2467 mbedtls_ctr_drbg_random,
2468 &global_data.ctr_drbg,
2469 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002470 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002471 (unsigned int) hash_length,
2472 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002473 signature );
2474 }
2475 else
2476#endif /* MBEDTLS_PKCS1_V21 */
2477 {
2478 return( PSA_ERROR_INVALID_ARGUMENT );
2479 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002480
2481 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2482 * the rest of the signature is invalid". This has little use in
2483 * practice and PSA doesn't report this distinction. */
2484 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2485 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002486 return( mbedtls_to_psa_error( ret ) );
2487}
2488#endif /* MBEDTLS_RSA_C */
2489
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002490#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002491/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2492 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2493 * (even though these functions don't modify it). */
2494static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2495 psa_algorithm_t alg,
2496 const uint8_t *hash,
2497 size_t hash_length,
2498 uint8_t *signature,
2499 size_t signature_size,
2500 size_t *signature_length )
2501{
2502 int ret;
2503 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002504 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002505 mbedtls_mpi_init( &r );
2506 mbedtls_mpi_init( &s );
2507
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002508 if( signature_size < 2 * curve_bytes )
2509 {
2510 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2511 goto cleanup;
2512 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002513
Gilles Peskinea05219c2018-11-16 16:02:56 +01002514#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002515 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2516 {
2517 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2518 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2519 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2520 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2521 hash, hash_length,
2522 md_alg ) );
2523 }
2524 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002525#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002526 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002527 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002528 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2529 hash, hash_length,
2530 mbedtls_ctr_drbg_random,
2531 &global_data.ctr_drbg ) );
2532 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002533
2534 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2535 signature,
2536 curve_bytes ) );
2537 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2538 signature + curve_bytes,
2539 curve_bytes ) );
2540
2541cleanup:
2542 mbedtls_mpi_free( &r );
2543 mbedtls_mpi_free( &s );
2544 if( ret == 0 )
2545 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002546 return( mbedtls_to_psa_error( ret ) );
2547}
2548
2549static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2550 const uint8_t *hash,
2551 size_t hash_length,
2552 const uint8_t *signature,
2553 size_t signature_length )
2554{
2555 int ret;
2556 mbedtls_mpi r, s;
2557 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2558 mbedtls_mpi_init( &r );
2559 mbedtls_mpi_init( &s );
2560
2561 if( signature_length != 2 * curve_bytes )
2562 return( PSA_ERROR_INVALID_SIGNATURE );
2563
2564 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2565 signature,
2566 curve_bytes ) );
2567 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2568 signature + curve_bytes,
2569 curve_bytes ) );
2570
2571 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2572 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002573
2574cleanup:
2575 mbedtls_mpi_free( &r );
2576 mbedtls_mpi_free( &s );
2577 return( mbedtls_to_psa_error( ret ) );
2578}
2579#endif /* MBEDTLS_ECDSA_C */
2580
Gilles Peskinec5487a82018-12-03 18:08:14 +01002581psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002582 psa_algorithm_t alg,
2583 const uint8_t *hash,
2584 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002585 uint8_t *signature,
2586 size_t signature_size,
2587 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002588{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002589 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002590 psa_status_t status;
2591
2592 *signature_length = signature_size;
2593
Gilles Peskinec5487a82018-12-03 18:08:14 +01002594 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002595 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002596 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002597 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002598 {
2599 status = PSA_ERROR_INVALID_ARGUMENT;
2600 goto exit;
2601 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002602
Gilles Peskine20035e32018-02-03 22:44:14 +01002603#if defined(MBEDTLS_RSA_C)
2604 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2605 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002606 status = psa_rsa_sign( slot->data.rsa,
2607 alg,
2608 hash, hash_length,
2609 signature, signature_size,
2610 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002611 }
2612 else
2613#endif /* defined(MBEDTLS_RSA_C) */
2614#if defined(MBEDTLS_ECP_C)
2615 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2616 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002617#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002618 if(
2619#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2620 PSA_ALG_IS_ECDSA( alg )
2621#else
2622 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2623#endif
2624 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002625 status = psa_ecdsa_sign( slot->data.ecp,
2626 alg,
2627 hash, hash_length,
2628 signature, signature_size,
2629 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002630 else
2631#endif /* defined(MBEDTLS_ECDSA_C) */
2632 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002633 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002634 }
itayzafrir5c753392018-05-08 11:18:38 +03002635 }
2636 else
2637#endif /* defined(MBEDTLS_ECP_C) */
2638 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002639 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002640 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002641
2642exit:
2643 /* Fill the unused part of the output buffer (the whole buffer on error,
2644 * the trailing part on success) with something that isn't a valid mac
2645 * (barring an attack on the mac and deliberately-crafted input),
2646 * in case the caller doesn't check the return status properly. */
2647 if( status == PSA_SUCCESS )
2648 memset( signature + *signature_length, '!',
2649 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002650 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002651 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002652 /* If signature_size is 0 then we have nothing to do. We must not call
2653 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002654 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002655}
2656
Gilles Peskinec5487a82018-12-03 18:08:14 +01002657psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002658 psa_algorithm_t alg,
2659 const uint8_t *hash,
2660 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002661 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002662 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002663{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002664 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002665 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002666
Gilles Peskinec5487a82018-12-03 18:08:14 +01002667 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002668 if( status != PSA_SUCCESS )
2669 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002670
Gilles Peskine61b91d42018-06-08 16:09:36 +02002671#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002672 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002673 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002674 return( psa_rsa_verify( slot->data.rsa,
2675 alg,
2676 hash, hash_length,
2677 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002678 }
2679 else
2680#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002681#if defined(MBEDTLS_ECP_C)
2682 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2683 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002684#if defined(MBEDTLS_ECDSA_C)
2685 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002686 return( psa_ecdsa_verify( slot->data.ecp,
2687 hash, hash_length,
2688 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002689 else
2690#endif /* defined(MBEDTLS_ECDSA_C) */
2691 {
2692 return( PSA_ERROR_INVALID_ARGUMENT );
2693 }
itayzafrir5c753392018-05-08 11:18:38 +03002694 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002695 else
2696#endif /* defined(MBEDTLS_ECP_C) */
2697 {
2698 return( PSA_ERROR_NOT_SUPPORTED );
2699 }
2700}
2701
Gilles Peskine072ac562018-06-30 00:21:29 +02002702#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2703static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2704 mbedtls_rsa_context *rsa )
2705{
2706 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2707 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2708 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2709 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2710}
2711#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2712
Gilles Peskinec5487a82018-12-03 18:08:14 +01002713psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002714 psa_algorithm_t alg,
2715 const uint8_t *input,
2716 size_t input_length,
2717 const uint8_t *salt,
2718 size_t salt_length,
2719 uint8_t *output,
2720 size_t output_size,
2721 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002722{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002723 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002724 psa_status_t status;
2725
Darryl Green5cc689a2018-07-24 15:34:10 +01002726 (void) input;
2727 (void) input_length;
2728 (void) salt;
2729 (void) output;
2730 (void) output_size;
2731
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002732 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002733
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002734 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2735 return( PSA_ERROR_INVALID_ARGUMENT );
2736
Gilles Peskinec5487a82018-12-03 18:08:14 +01002737 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002738 if( status != PSA_SUCCESS )
2739 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002740 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2741 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002742 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002743
2744#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002745 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002746 {
2747 mbedtls_rsa_context *rsa = slot->data.rsa;
2748 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002749 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002750 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002751#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002752 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002753 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002754 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2755 mbedtls_ctr_drbg_random,
2756 &global_data.ctr_drbg,
2757 MBEDTLS_RSA_PUBLIC,
2758 input_length,
2759 input,
2760 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002761 }
2762 else
2763#endif /* MBEDTLS_PKCS1_V15 */
2764#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002765 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002766 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002767 psa_rsa_oaep_set_padding_mode( alg, rsa );
2768 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2769 mbedtls_ctr_drbg_random,
2770 &global_data.ctr_drbg,
2771 MBEDTLS_RSA_PUBLIC,
2772 salt, salt_length,
2773 input_length,
2774 input,
2775 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002776 }
2777 else
2778#endif /* MBEDTLS_PKCS1_V21 */
2779 {
2780 return( PSA_ERROR_INVALID_ARGUMENT );
2781 }
2782 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002783 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002784 return( mbedtls_to_psa_error( ret ) );
2785 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002786 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002787#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002788 {
2789 return( PSA_ERROR_NOT_SUPPORTED );
2790 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002791}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002792
Gilles Peskinec5487a82018-12-03 18:08:14 +01002793psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002794 psa_algorithm_t alg,
2795 const uint8_t *input,
2796 size_t input_length,
2797 const uint8_t *salt,
2798 size_t salt_length,
2799 uint8_t *output,
2800 size_t output_size,
2801 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002802{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002803 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002804 psa_status_t status;
2805
Darryl Green5cc689a2018-07-24 15:34:10 +01002806 (void) input;
2807 (void) input_length;
2808 (void) salt;
2809 (void) output;
2810 (void) output_size;
2811
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002812 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002813
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002814 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2815 return( PSA_ERROR_INVALID_ARGUMENT );
2816
Gilles Peskinec5487a82018-12-03 18:08:14 +01002817 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002818 if( status != PSA_SUCCESS )
2819 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002820 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2821 return( PSA_ERROR_INVALID_ARGUMENT );
2822
2823#if defined(MBEDTLS_RSA_C)
2824 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2825 {
2826 mbedtls_rsa_context *rsa = slot->data.rsa;
2827 int ret;
2828
Gilles Peskine630a18a2018-06-29 17:49:35 +02002829 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002830 return( PSA_ERROR_INVALID_ARGUMENT );
2831
2832#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002833 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002834 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002835 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2836 mbedtls_ctr_drbg_random,
2837 &global_data.ctr_drbg,
2838 MBEDTLS_RSA_PRIVATE,
2839 output_length,
2840 input,
2841 output,
2842 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002843 }
2844 else
2845#endif /* MBEDTLS_PKCS1_V15 */
2846#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002847 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002848 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002849 psa_rsa_oaep_set_padding_mode( alg, rsa );
2850 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2851 mbedtls_ctr_drbg_random,
2852 &global_data.ctr_drbg,
2853 MBEDTLS_RSA_PRIVATE,
2854 salt, salt_length,
2855 output_length,
2856 input,
2857 output,
2858 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002859 }
2860 else
2861#endif /* MBEDTLS_PKCS1_V21 */
2862 {
2863 return( PSA_ERROR_INVALID_ARGUMENT );
2864 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002865
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002866 return( mbedtls_to_psa_error( ret ) );
2867 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002868 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002869#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002870 {
2871 return( PSA_ERROR_NOT_SUPPORTED );
2872 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002873}
Gilles Peskine20035e32018-02-03 22:44:14 +01002874
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002875
2876
mohammad1603503973b2018-03-12 15:59:30 +02002877/****************************************************************/
2878/* Symmetric cryptography */
2879/****************************************************************/
2880
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002881/* Initialize the cipher operation structure. Once this function has been
2882 * called, psa_cipher_abort can run and will do the right thing. */
2883static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2884 psa_algorithm_t alg )
2885{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002886 if( ! PSA_ALG_IS_CIPHER( alg ) )
2887 {
2888 memset( operation, 0, sizeof( *operation ) );
2889 return( PSA_ERROR_INVALID_ARGUMENT );
2890 }
2891
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002892 operation->alg = alg;
2893 operation->key_set = 0;
2894 operation->iv_set = 0;
2895 operation->iv_required = 1;
2896 operation->iv_size = 0;
2897 operation->block_size = 0;
2898 mbedtls_cipher_init( &operation->ctx.cipher );
2899 return( PSA_SUCCESS );
2900}
2901
Gilles Peskinee553c652018-06-04 16:22:46 +02002902static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002903 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002904 psa_algorithm_t alg,
2905 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002906{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002907 int ret = 0;
2908 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002909 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002910 size_t key_bits;
2911 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002912 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2913 PSA_KEY_USAGE_ENCRYPT :
2914 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002915
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002916 /* A context must be freshly initialized before it can be set up. */
2917 if( operation->alg != 0 )
2918 {
2919 return( PSA_ERROR_BAD_STATE );
2920 }
2921
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002922 status = psa_cipher_init( operation, alg );
2923 if( status != PSA_SUCCESS )
2924 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002925
Gilles Peskinec5487a82018-12-03 18:08:14 +01002926 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002927 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002928 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002929 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002930
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002931 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002932 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002933 {
2934 status = PSA_ERROR_NOT_SUPPORTED;
2935 goto exit;
2936 }
mohammad1603503973b2018-03-12 15:59:30 +02002937
mohammad1603503973b2018-03-12 15:59:30 +02002938 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002939 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002940 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002941
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002942#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002943 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002944 {
2945 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2946 unsigned char keys[24];
2947 memcpy( keys, slot->data.raw.data, 16 );
2948 memcpy( keys + 16, slot->data.raw.data, 8 );
2949 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2950 keys,
2951 192, cipher_operation );
2952 }
2953 else
2954#endif
2955 {
2956 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2957 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002958 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002959 }
Moran Peker41deec42018-04-04 15:43:05 +03002960 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002961 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002962
mohammad16038481e742018-03-18 13:57:31 +02002963#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002964 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002965 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002966 case PSA_ALG_CBC_NO_PADDING:
2967 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2968 MBEDTLS_PADDING_NONE );
2969 break;
2970 case PSA_ALG_CBC_PKCS7:
2971 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2972 MBEDTLS_PADDING_PKCS7 );
2973 break;
2974 default:
2975 /* The algorithm doesn't involve padding. */
2976 ret = 0;
2977 break;
2978 }
2979 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002980 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02002981#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2982
mohammad1603503973b2018-03-12 15:59:30 +02002983 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002984 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2985 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2986 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002987 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002988 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002989 }
mohammad1603503973b2018-03-12 15:59:30 +02002990
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002991exit:
2992 if( status == 0 )
2993 status = mbedtls_to_psa_error( ret );
2994 if( status != 0 )
2995 psa_cipher_abort( operation );
2996 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002997}
2998
Gilles Peskinefe119512018-07-08 21:39:34 +02002999psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003000 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003001 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003002{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003003 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003004}
3005
Gilles Peskinefe119512018-07-08 21:39:34 +02003006psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003007 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003008 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003009{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003010 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003011}
3012
Gilles Peskinefe119512018-07-08 21:39:34 +02003013psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3014 unsigned char *iv,
3015 size_t iv_size,
3016 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003017{
itayzafrir534bd7c2018-08-02 13:56:32 +03003018 psa_status_t status;
3019 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003020 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003021 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003022 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003023 }
Moran Peker41deec42018-04-04 15:43:05 +03003024 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003025 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003026 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003027 goto exit;
3028 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003029 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3030 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003031 if( ret != 0 )
3032 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003033 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003034 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003035 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003036
mohammad16038481e742018-03-18 13:57:31 +02003037 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003038 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003039
Moran Peker395db872018-05-31 14:07:14 +03003040exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003041 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003042 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003043 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003044}
3045
Gilles Peskinefe119512018-07-08 21:39:34 +02003046psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3047 const unsigned char *iv,
3048 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003049{
itayzafrir534bd7c2018-08-02 13:56:32 +03003050 psa_status_t status;
3051 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003052 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003053 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003054 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003055 }
Moran Pekera28258c2018-05-29 16:25:04 +03003056 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003057 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003058 status = PSA_ERROR_INVALID_ARGUMENT;
3059 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003060 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003061 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3062 status = mbedtls_to_psa_error( ret );
3063exit:
3064 if( status == PSA_SUCCESS )
3065 operation->iv_set = 1;
3066 else
mohammad1603503973b2018-03-12 15:59:30 +02003067 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003068 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003069}
3070
Gilles Peskinee553c652018-06-04 16:22:46 +02003071psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3072 const uint8_t *input,
3073 size_t input_length,
3074 unsigned char *output,
3075 size_t output_size,
3076 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003077{
itayzafrir534bd7c2018-08-02 13:56:32 +03003078 psa_status_t status;
3079 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003080 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003081
3082 if( operation->alg == 0 )
3083 {
3084 return( PSA_ERROR_BAD_STATE );
3085 }
3086
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003087 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003088 {
3089 /* Take the unprocessed partial block left over from previous
3090 * update calls, if any, plus the input to this call. Remove
3091 * the last partial block, if any. You get the data that will be
3092 * output in this call. */
3093 expected_output_size =
3094 ( operation->ctx.cipher.unprocessed_len + input_length )
3095 / operation->block_size * operation->block_size;
3096 }
3097 else
3098 {
3099 expected_output_size = input_length;
3100 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003101
Gilles Peskine89d789c2018-06-04 17:17:16 +02003102 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003103 {
3104 status = PSA_ERROR_BUFFER_TOO_SMALL;
3105 goto exit;
3106 }
mohammad160382759612018-03-12 18:16:40 +02003107
mohammad1603503973b2018-03-12 15:59:30 +02003108 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003109 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003110 status = mbedtls_to_psa_error( ret );
3111exit:
3112 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003113 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003114 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003115}
3116
Gilles Peskinee553c652018-06-04 16:22:46 +02003117psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3118 uint8_t *output,
3119 size_t output_size,
3120 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003121{
David Saadab4ecc272019-02-14 13:48:10 +02003122 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003123 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003124 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003125
mohammad1603503973b2018-03-12 15:59:30 +02003126 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003127 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003128 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003129 }
3130 if( operation->iv_required && ! operation->iv_set )
3131 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003132 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003133 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003134
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003135 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003136 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3137 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003138 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003139 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003140 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003141 }
3142
Janos Follath315b51c2018-07-09 16:04:51 +01003143 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3144 temp_output_buffer,
3145 output_length );
3146 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003147 {
Janos Follath315b51c2018-07-09 16:04:51 +01003148 status = mbedtls_to_psa_error( cipher_ret );
3149 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003150 }
Janos Follath315b51c2018-07-09 16:04:51 +01003151
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003152 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003153 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003154 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003155 memcpy( output, temp_output_buffer, *output_length );
3156 else
3157 {
Janos Follath315b51c2018-07-09 16:04:51 +01003158 status = PSA_ERROR_BUFFER_TOO_SMALL;
3159 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003160 }
mohammad1603503973b2018-03-12 15:59:30 +02003161
Gilles Peskine3f108122018-12-07 18:14:53 +01003162 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003163 status = psa_cipher_abort( operation );
3164
3165 return( status );
3166
3167error:
3168
3169 *output_length = 0;
3170
Gilles Peskine3f108122018-12-07 18:14:53 +01003171 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003172 (void) psa_cipher_abort( operation );
3173
3174 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003175}
3176
Gilles Peskinee553c652018-06-04 16:22:46 +02003177psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3178{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003179 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003180 {
3181 /* The object has (apparently) been initialized but it is not
3182 * in use. It's ok to call abort on such an object, and there's
3183 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003184 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003185 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003186
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003187 /* Sanity check (shouldn't happen: operation->alg should
3188 * always have been initialized to a valid value). */
3189 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3190 return( PSA_ERROR_BAD_STATE );
3191
mohammad1603503973b2018-03-12 15:59:30 +02003192 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003193
Moran Peker41deec42018-04-04 15:43:05 +03003194 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003195 operation->key_set = 0;
3196 operation->iv_set = 0;
3197 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003198 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003199 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003200
Moran Peker395db872018-05-31 14:07:14 +03003201 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003202}
3203
Gilles Peskinea0655c32018-04-30 17:06:50 +02003204
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003205
mohammad16038cc1cee2018-03-28 01:21:33 +03003206/****************************************************************/
3207/* Key Policy */
3208/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003209
mohammad160327010052018-07-03 13:16:15 +03003210#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003211void psa_key_policy_set_usage( psa_key_policy_t *policy,
3212 psa_key_usage_t usage,
3213 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003214{
mohammad16034eed7572018-03-28 05:14:59 -07003215 policy->usage = usage;
3216 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003217}
3218
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003219psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003220{
mohammad16036df908f2018-04-02 08:34:15 -07003221 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003222}
3223
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003224psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003225{
mohammad16036df908f2018-04-02 08:34:15 -07003226 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003227}
mohammad160327010052018-07-03 13:16:15 +03003228#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003229
Gilles Peskinec5487a82018-12-03 18:08:14 +01003230psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003231 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003232{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003233 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003234 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003235
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003236 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003238
Gilles Peskinec5487a82018-12-03 18:08:14 +01003239 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003240 if( status != PSA_SUCCESS )
3241 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003242
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003243 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3244 PSA_KEY_USAGE_ENCRYPT |
3245 PSA_KEY_USAGE_DECRYPT |
3246 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003247 PSA_KEY_USAGE_VERIFY |
3248 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003249 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003250
mohammad16036df908f2018-04-02 08:34:15 -07003251 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003252
3253 return( PSA_SUCCESS );
3254}
3255
Gilles Peskinec5487a82018-12-03 18:08:14 +01003256psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003257 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003258{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003259 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003260 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003261
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003262 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003263 return( PSA_ERROR_INVALID_ARGUMENT );
3264
Gilles Peskinec5487a82018-12-03 18:08:14 +01003265 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003266 if( status != PSA_SUCCESS )
3267 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003268
mohammad16036df908f2018-04-02 08:34:15 -07003269 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003270
3271 return( PSA_SUCCESS );
3272}
Gilles Peskine20035e32018-02-03 22:44:14 +01003273
Gilles Peskinea0655c32018-04-30 17:06:50 +02003274
3275
mohammad1603804cd712018-03-20 22:44:08 +02003276/****************************************************************/
3277/* Key Lifetime */
3278/****************************************************************/
3279
Gilles Peskine87a5e562019-04-17 12:28:25 +02003280psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003281 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003282{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003283 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003284 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003285
Gilles Peskinec5487a82018-12-03 18:08:14 +01003286 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003287 if( status != PSA_SUCCESS )
3288 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003289
mohammad1603804cd712018-03-20 22:44:08 +02003290 *lifetime = slot->lifetime;
3291
3292 return( PSA_SUCCESS );
3293}
3294
Gilles Peskine20035e32018-02-03 22:44:14 +01003295
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003296
mohammad16035955c982018-04-26 00:53:03 +03003297/****************************************************************/
3298/* AEAD */
3299/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003300
Gilles Peskineedf9a652018-08-17 18:11:56 +02003301typedef struct
3302{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003303 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003304 const mbedtls_cipher_info_t *cipher_info;
3305 union
3306 {
3307#if defined(MBEDTLS_CCM_C)
3308 mbedtls_ccm_context ccm;
3309#endif /* MBEDTLS_CCM_C */
3310#if defined(MBEDTLS_GCM_C)
3311 mbedtls_gcm_context gcm;
3312#endif /* MBEDTLS_GCM_C */
3313 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003314 psa_algorithm_t core_alg;
3315 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003316 uint8_t tag_length;
3317} aead_operation_t;
3318
Gilles Peskine30a9e412019-01-14 18:36:12 +01003319static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003320{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003321 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003322 {
3323#if defined(MBEDTLS_CCM_C)
3324 case PSA_ALG_CCM:
3325 mbedtls_ccm_free( &operation->ctx.ccm );
3326 break;
3327#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003328#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003329 case PSA_ALG_GCM:
3330 mbedtls_gcm_free( &operation->ctx.gcm );
3331 break;
3332#endif /* MBEDTLS_GCM_C */
3333 }
3334}
3335
3336static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003337 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003338 psa_key_usage_t usage,
3339 psa_algorithm_t alg )
3340{
3341 psa_status_t status;
3342 size_t key_bits;
3343 mbedtls_cipher_id_t cipher_id;
3344
Gilles Peskinec5487a82018-12-03 18:08:14 +01003345 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003346 if( status != PSA_SUCCESS )
3347 return( status );
3348
3349 key_bits = psa_get_key_bits( operation->slot );
3350
3351 operation->cipher_info =
3352 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3353 &cipher_id );
3354 if( operation->cipher_info == NULL )
3355 return( PSA_ERROR_NOT_SUPPORTED );
3356
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003357 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003358 {
3359#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003360 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3361 operation->core_alg = PSA_ALG_CCM;
3362 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003363 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3364 return( PSA_ERROR_INVALID_ARGUMENT );
3365 mbedtls_ccm_init( &operation->ctx.ccm );
3366 status = mbedtls_to_psa_error(
3367 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3368 operation->slot->data.raw.data,
3369 (unsigned int) key_bits ) );
3370 if( status != 0 )
3371 goto cleanup;
3372 break;
3373#endif /* MBEDTLS_CCM_C */
3374
3375#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003376 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3377 operation->core_alg = PSA_ALG_GCM;
3378 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003379 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3380 return( PSA_ERROR_INVALID_ARGUMENT );
3381 mbedtls_gcm_init( &operation->ctx.gcm );
3382 status = mbedtls_to_psa_error(
3383 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3384 operation->slot->data.raw.data,
3385 (unsigned int) key_bits ) );
3386 break;
3387#endif /* MBEDTLS_GCM_C */
3388
3389 default:
3390 return( PSA_ERROR_NOT_SUPPORTED );
3391 }
3392
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003393 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3394 {
3395 status = PSA_ERROR_INVALID_ARGUMENT;
3396 goto cleanup;
3397 }
3398 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3399 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3400 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3401 * In both cases, mbedtls_xxx will validate the tag length below. */
3402
Gilles Peskineedf9a652018-08-17 18:11:56 +02003403 return( PSA_SUCCESS );
3404
3405cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003406 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003407 return( status );
3408}
3409
Gilles Peskinec5487a82018-12-03 18:08:14 +01003410psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003411 psa_algorithm_t alg,
3412 const uint8_t *nonce,
3413 size_t nonce_length,
3414 const uint8_t *additional_data,
3415 size_t additional_data_length,
3416 const uint8_t *plaintext,
3417 size_t plaintext_length,
3418 uint8_t *ciphertext,
3419 size_t ciphertext_size,
3420 size_t *ciphertext_length )
3421{
mohammad16035955c982018-04-26 00:53:03 +03003422 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003423 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003424 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003425
mohammad1603f08a5502018-06-03 15:05:47 +03003426 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003427
Gilles Peskinec5487a82018-12-03 18:08:14 +01003428 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003429 if( status != PSA_SUCCESS )
3430 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003431
Gilles Peskineedf9a652018-08-17 18:11:56 +02003432 /* For all currently supported modes, the tag is at the end of the
3433 * ciphertext. */
3434 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3435 {
3436 status = PSA_ERROR_BUFFER_TOO_SMALL;
3437 goto exit;
3438 }
3439 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003440
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003441#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003442 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003443 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003444 status = mbedtls_to_psa_error(
3445 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3446 MBEDTLS_GCM_ENCRYPT,
3447 plaintext_length,
3448 nonce, nonce_length,
3449 additional_data, additional_data_length,
3450 plaintext, ciphertext,
3451 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003452 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003453 else
3454#endif /* MBEDTLS_GCM_C */
3455#if defined(MBEDTLS_CCM_C)
3456 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003457 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003458 status = mbedtls_to_psa_error(
3459 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3460 plaintext_length,
3461 nonce, nonce_length,
3462 additional_data,
3463 additional_data_length,
3464 plaintext, ciphertext,
3465 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003466 }
mohammad16035c8845f2018-05-09 05:40:09 -07003467 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003468#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003469 {
mohammad1603554faad2018-06-03 15:07:38 +03003470 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003471 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003472
Gilles Peskineedf9a652018-08-17 18:11:56 +02003473 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3474 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003475
Gilles Peskineedf9a652018-08-17 18:11:56 +02003476exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003477 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003478 if( status == PSA_SUCCESS )
3479 *ciphertext_length = plaintext_length + operation.tag_length;
3480 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003481}
3482
Gilles Peskineee652a32018-06-01 19:23:52 +02003483/* Locate the tag in a ciphertext buffer containing the encrypted data
3484 * followed by the tag. Return the length of the part preceding the tag in
3485 * *plaintext_length. This is the size of the plaintext in modes where
3486 * the encrypted data has the same size as the plaintext, such as
3487 * CCM and GCM. */
3488static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3489 const uint8_t *ciphertext,
3490 size_t ciphertext_length,
3491 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003492 const uint8_t **p_tag )
3493{
3494 size_t payload_length;
3495 if( tag_length > ciphertext_length )
3496 return( PSA_ERROR_INVALID_ARGUMENT );
3497 payload_length = ciphertext_length - tag_length;
3498 if( payload_length > plaintext_size )
3499 return( PSA_ERROR_BUFFER_TOO_SMALL );
3500 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003501 return( PSA_SUCCESS );
3502}
3503
Gilles Peskinec5487a82018-12-03 18:08:14 +01003504psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003505 psa_algorithm_t alg,
3506 const uint8_t *nonce,
3507 size_t nonce_length,
3508 const uint8_t *additional_data,
3509 size_t additional_data_length,
3510 const uint8_t *ciphertext,
3511 size_t ciphertext_length,
3512 uint8_t *plaintext,
3513 size_t plaintext_size,
3514 size_t *plaintext_length )
3515{
mohammad16035955c982018-04-26 00:53:03 +03003516 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003517 aead_operation_t operation;
3518 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003519
Gilles Peskineee652a32018-06-01 19:23:52 +02003520 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003521
Gilles Peskinec5487a82018-12-03 18:08:14 +01003522 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003523 if( status != PSA_SUCCESS )
3524 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003525
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003526#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003527 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003528 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003529 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003530 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003531 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003532 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003533 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003534
Gilles Peskineedf9a652018-08-17 18:11:56 +02003535 status = mbedtls_to_psa_error(
3536 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3537 ciphertext_length - operation.tag_length,
3538 nonce, nonce_length,
3539 additional_data,
3540 additional_data_length,
3541 tag, operation.tag_length,
3542 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003543 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003544 else
3545#endif /* MBEDTLS_GCM_C */
3546#if defined(MBEDTLS_CCM_C)
3547 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003548 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003549 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003550 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003551 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003552 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003553 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003554
Gilles Peskineedf9a652018-08-17 18:11:56 +02003555 status = mbedtls_to_psa_error(
3556 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3557 ciphertext_length - operation.tag_length,
3558 nonce, nonce_length,
3559 additional_data,
3560 additional_data_length,
3561 ciphertext, plaintext,
3562 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003563 }
mohammad160339574652018-06-01 04:39:53 -07003564 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003565#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003566 {
mohammad1603554faad2018-06-03 15:07:38 +03003567 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003568 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003569
Gilles Peskineedf9a652018-08-17 18:11:56 +02003570 if( status != PSA_SUCCESS && plaintext_size != 0 )
3571 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003572
Gilles Peskineedf9a652018-08-17 18:11:56 +02003573exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003574 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003575 if( status == PSA_SUCCESS )
3576 *plaintext_length = ciphertext_length - operation.tag_length;
3577 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003578}
3579
Gilles Peskinea0655c32018-04-30 17:06:50 +02003580
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003581
Gilles Peskine20035e32018-02-03 22:44:14 +01003582/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003583/* Generators */
3584/****************************************************************/
3585
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003586#define HKDF_STATE_INIT 0 /* no input yet */
3587#define HKDF_STATE_STARTED 1 /* got salt */
3588#define HKDF_STATE_KEYED 2 /* got key */
3589#define HKDF_STATE_OUTPUT 3 /* output started */
3590
Gilles Peskine969c5d62019-01-16 15:53:06 +01003591static psa_algorithm_t psa_generator_get_kdf_alg(
3592 const psa_crypto_generator_t *generator )
3593{
3594 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3595 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3596 else
3597 return( generator->alg );
3598}
3599
3600
Gilles Peskineeab56e42018-07-12 17:12:33 +02003601psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3602{
3603 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003604 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3605 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003606 {
3607 /* The object has (apparently) been initialized but it is not
3608 * in use. It's ok to call abort on such an object, and there's
3609 * nothing to do. */
3610 }
3611 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003612 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003613 {
3614 if( generator->ctx.buffer.data != NULL )
3615 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003616 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003617 generator->ctx.buffer.size );
3618 mbedtls_free( generator->ctx.buffer.data );
3619 }
3620 }
3621 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003622#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003623 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003624 {
3625 mbedtls_free( generator->ctx.hkdf.info );
3626 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3627 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003628 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00003629 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003630 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003631 {
3632 if( generator->ctx.tls12_prf.key != NULL )
3633 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003634 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003635 generator->ctx.tls12_prf.key_len );
3636 mbedtls_free( generator->ctx.tls12_prf.key );
3637 }
Hanno Becker580fba12018-11-13 20:50:45 +00003638
3639 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3640 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003641 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003642 generator->ctx.tls12_prf.Ai_with_seed_len );
3643 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3644 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003645 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003646 else
3647#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003648 {
3649 status = PSA_ERROR_BAD_STATE;
3650 }
3651 memset( generator, 0, sizeof( *generator ) );
3652 return( status );
3653}
3654
Gilles Peskineeab56e42018-07-12 17:12:33 +02003655psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3656 size_t *capacity)
3657{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003658 if( generator->alg == 0 )
3659 {
3660 /* This is a blank generator. */
3661 return PSA_ERROR_BAD_STATE;
3662 }
3663
Gilles Peskineeab56e42018-07-12 17:12:33 +02003664 *capacity = generator->capacity;
3665 return( PSA_SUCCESS );
3666}
3667
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003668psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
3669 size_t capacity )
3670{
3671 if( generator->alg == 0 )
3672 return( PSA_ERROR_BAD_STATE );
3673 if( capacity > generator->capacity )
3674 return( PSA_ERROR_INVALID_ARGUMENT );
3675 generator->capacity = capacity;
3676 return( PSA_SUCCESS );
3677}
3678
Gilles Peskinebef7f142018-07-12 17:22:21 +02003679#if defined(MBEDTLS_MD_C)
3680/* Read some bytes from an HKDF-based generator. This performs a chunk
3681 * of the expand phase of the HKDF algorithm. */
3682static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3683 psa_algorithm_t hash_alg,
3684 uint8_t *output,
3685 size_t output_length )
3686{
3687 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3688 psa_status_t status;
3689
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003690 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3691 return( PSA_ERROR_BAD_STATE );
3692 hkdf->state = HKDF_STATE_OUTPUT;
3693
Gilles Peskinebef7f142018-07-12 17:22:21 +02003694 while( output_length != 0 )
3695 {
3696 /* Copy what remains of the current block */
3697 uint8_t n = hash_length - hkdf->offset_in_block;
3698 if( n > output_length )
3699 n = (uint8_t) output_length;
3700 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3701 output += n;
3702 output_length -= n;
3703 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003704 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003705 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003706 /* We can't be wanting more output after block 0xff, otherwise
3707 * the capacity check in psa_generator_read() would have
3708 * prevented this call. It could happen only if the generator
3709 * object was corrupted or if this function is called directly
3710 * inside the library. */
3711 if( hkdf->block_number == 0xff )
3712 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003713
3714 /* We need a new block */
3715 ++hkdf->block_number;
3716 hkdf->offset_in_block = 0;
3717 status = psa_hmac_setup_internal( &hkdf->hmac,
3718 hkdf->prk, hash_length,
3719 hash_alg );
3720 if( status != PSA_SUCCESS )
3721 return( status );
3722 if( hkdf->block_number != 1 )
3723 {
3724 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3725 hkdf->output_block,
3726 hash_length );
3727 if( status != PSA_SUCCESS )
3728 return( status );
3729 }
3730 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3731 hkdf->info,
3732 hkdf->info_length );
3733 if( status != PSA_SUCCESS )
3734 return( status );
3735 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3736 &hkdf->block_number, 1 );
3737 if( status != PSA_SUCCESS )
3738 return( status );
3739 status = psa_hmac_finish_internal( &hkdf->hmac,
3740 hkdf->output_block,
3741 sizeof( hkdf->output_block ) );
3742 if( status != PSA_SUCCESS )
3743 return( status );
3744 }
3745
3746 return( PSA_SUCCESS );
3747}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003748
3749static psa_status_t psa_generator_tls12_prf_generate_next_block(
3750 psa_tls12_prf_generator_t *tls12_prf,
3751 psa_algorithm_t alg )
3752{
3753 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3754 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3755 psa_hmac_internal_data hmac;
3756 psa_status_t status, cleanup_status;
3757
Hanno Becker3b339e22018-11-13 20:56:14 +00003758 unsigned char *Ai;
3759 size_t Ai_len;
3760
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003761 /* We can't be wanting more output after block 0xff, otherwise
3762 * the capacity check in psa_generator_read() would have
3763 * prevented this call. It could happen only if the generator
3764 * object was corrupted or if this function is called directly
3765 * inside the library. */
3766 if( tls12_prf->block_number == 0xff )
3767 return( PSA_ERROR_BAD_STATE );
3768
3769 /* We need a new block */
3770 ++tls12_prf->block_number;
3771 tls12_prf->offset_in_block = 0;
3772
3773 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3774 *
3775 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3776 *
3777 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3778 * HMAC_hash(secret, A(2) + seed) +
3779 * HMAC_hash(secret, A(3) + seed) + ...
3780 *
3781 * A(0) = seed
3782 * A(i) = HMAC_hash( secret, A(i-1) )
3783 *
3784 * The `psa_tls12_prf_generator` structures saves the block
3785 * `HMAC_hash(secret, A(i) + seed)` from which the output
3786 * is currently extracted as `output_block`, while
3787 * `A(i) + seed` is stored in `Ai_with_seed`.
3788 *
3789 * Generating a new block means recalculating `Ai_with_seed`
3790 * from the A(i)-part of it, and afterwards recalculating
3791 * `output_block`.
3792 *
3793 * A(0) is computed at setup time.
3794 *
3795 */
3796
3797 psa_hmac_init_internal( &hmac );
3798
3799 /* We must distinguish the calculation of A(1) from those
3800 * of A(2) and higher, because A(0)=seed has a different
3801 * length than the other A(i). */
3802 if( tls12_prf->block_number == 1 )
3803 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003804 Ai = tls12_prf->Ai_with_seed + hash_length;
3805 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003806 }
3807 else
3808 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003809 Ai = tls12_prf->Ai_with_seed;
3810 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003811 }
3812
Hanno Becker3b339e22018-11-13 20:56:14 +00003813 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3814 status = psa_hmac_setup_internal( &hmac,
3815 tls12_prf->key,
3816 tls12_prf->key_len,
3817 hash_alg );
3818 if( status != PSA_SUCCESS )
3819 goto cleanup;
3820
3821 status = psa_hash_update( &hmac.hash_ctx,
3822 Ai, Ai_len );
3823 if( status != PSA_SUCCESS )
3824 goto cleanup;
3825
3826 status = psa_hmac_finish_internal( &hmac,
3827 tls12_prf->Ai_with_seed,
3828 hash_length );
3829 if( status != PSA_SUCCESS )
3830 goto cleanup;
3831
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003832 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3833 status = psa_hmac_setup_internal( &hmac,
3834 tls12_prf->key,
3835 tls12_prf->key_len,
3836 hash_alg );
3837 if( status != PSA_SUCCESS )
3838 goto cleanup;
3839
3840 status = psa_hash_update( &hmac.hash_ctx,
3841 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003842 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003843 if( status != PSA_SUCCESS )
3844 goto cleanup;
3845
3846 status = psa_hmac_finish_internal( &hmac,
3847 tls12_prf->output_block,
3848 hash_length );
3849 if( status != PSA_SUCCESS )
3850 goto cleanup;
3851
3852cleanup:
3853
3854 cleanup_status = psa_hmac_abort_internal( &hmac );
3855 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3856 status = cleanup_status;
3857
3858 return( status );
3859}
3860
3861/* Read some bytes from an TLS-1.2-PRF-based generator.
3862 * See Section 5 of RFC 5246. */
3863static psa_status_t psa_generator_tls12_prf_read(
3864 psa_tls12_prf_generator_t *tls12_prf,
3865 psa_algorithm_t alg,
3866 uint8_t *output,
3867 size_t output_length )
3868{
3869 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3870 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3871 psa_status_t status;
3872
3873 while( output_length != 0 )
3874 {
3875 /* Copy what remains of the current block */
3876 uint8_t n = hash_length - tls12_prf->offset_in_block;
3877
3878 /* Check if we have fully processed the current block. */
3879 if( n == 0 )
3880 {
3881 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3882 alg );
3883 if( status != PSA_SUCCESS )
3884 return( status );
3885
3886 continue;
3887 }
3888
3889 if( n > output_length )
3890 n = (uint8_t) output_length;
3891 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3892 n );
3893 output += n;
3894 output_length -= n;
3895 tls12_prf->offset_in_block += n;
3896 }
3897
3898 return( PSA_SUCCESS );
3899}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003900#endif /* MBEDTLS_MD_C */
3901
Gilles Peskineeab56e42018-07-12 17:12:33 +02003902psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3903 uint8_t *output,
3904 size_t output_length )
3905{
3906 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003907 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003908
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003909 if( generator->alg == 0 )
3910 {
3911 /* This is a blank generator. */
3912 return PSA_ERROR_BAD_STATE;
3913 }
3914
Gilles Peskineeab56e42018-07-12 17:12:33 +02003915 if( output_length > generator->capacity )
3916 {
3917 generator->capacity = 0;
3918 /* Go through the error path to wipe all confidential data now
3919 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02003920 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003921 goto exit;
3922 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003923 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003924 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003925 /* Edge case: this is a finished generator, and 0 bytes
3926 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02003927 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3928 * INSUFFICIENT_CAPACITY, which is right for a finished
3929 * generator, for consistency with the case when
3930 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02003931 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003932 }
3933 generator->capacity -= output_length;
3934
Gilles Peskine969c5d62019-01-16 15:53:06 +01003935 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003936 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003937 /* Initially, the capacity of a selection generator is always
3938 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3939 * abbreviated in this comment as `size`. When the remaining
3940 * capacity is `c`, the next bytes to serve start `c` bytes
3941 * from the end of the buffer, i.e. `size - c` from the
3942 * beginning of the buffer. Since `generator->capacity` was just
3943 * decremented above, we need to serve the bytes from
3944 * `size - generator->capacity - output_length` to
3945 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003946 size_t offset =
3947 generator->ctx.buffer.size - generator->capacity - output_length;
3948 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3949 status = PSA_SUCCESS;
3950 }
3951 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003952#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003953 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003954 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01003955 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003956 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3957 output, output_length );
3958 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003959 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
3960 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003961 {
3962 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003963 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003964 output_length );
3965 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003966 else
3967#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003968 {
3969 return( PSA_ERROR_BAD_STATE );
3970 }
3971
3972exit:
3973 if( status != PSA_SUCCESS )
3974 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003975 /* Preserve the algorithm upon errors, but clear all sensitive state.
3976 * This allows us to differentiate between exhausted generators and
3977 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
3978 * generators. */
3979 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003980 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003981 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003982 memset( output, '!', output_length );
3983 }
3984 return( status );
3985}
3986
Gilles Peskine08542d82018-07-19 17:05:42 +02003987#if defined(MBEDTLS_DES_C)
3988static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3989{
3990 if( data_size >= 8 )
3991 mbedtls_des_key_set_parity( data );
3992 if( data_size >= 16 )
3993 mbedtls_des_key_set_parity( data + 8 );
3994 if( data_size >= 24 )
3995 mbedtls_des_key_set_parity( data + 16 );
3996}
3997#endif /* MBEDTLS_DES_C */
3998
Gilles Peskine87a5e562019-04-17 12:28:25 +02003999psa_status_t psa_generator_import_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004000 psa_key_type_t type,
4001 size_t bits,
4002 psa_crypto_generator_t *generator )
4003{
4004 uint8_t *data = NULL;
4005 size_t bytes = PSA_BITS_TO_BYTES( bits );
4006 psa_status_t status;
4007
4008 if( ! key_type_is_raw_bytes( type ) )
4009 return( PSA_ERROR_INVALID_ARGUMENT );
4010 if( bits % 8 != 0 )
4011 return( PSA_ERROR_INVALID_ARGUMENT );
4012 data = mbedtls_calloc( 1, bytes );
4013 if( data == NULL )
4014 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4015
4016 status = psa_generator_read( generator, data, bytes );
4017 if( status != PSA_SUCCESS )
4018 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004019#if defined(MBEDTLS_DES_C)
4020 if( type == PSA_KEY_TYPE_DES )
4021 psa_des_set_key_parity( data, bytes );
4022#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004023 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004024
4025exit:
4026 mbedtls_free( data );
4027 return( status );
4028}
4029
4030
4031
4032/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004033/* Key derivation */
4034/****************************************************************/
4035
Gilles Peskinea05219c2018-11-16 16:02:56 +01004036#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004037/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004038 * of the HKDF algorithm.
4039 *
4040 * Note that if this function fails, you must call psa_generator_abort()
4041 * to potentially free embedded data structures and wipe confidential data.
4042 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004043static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004044 const uint8_t *secret,
4045 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004046 psa_algorithm_t hash_alg,
4047 const uint8_t *salt,
4048 size_t salt_length,
4049 const uint8_t *label,
4050 size_t label_length )
4051{
4052 psa_status_t status;
4053 status = psa_hmac_setup_internal( &hkdf->hmac,
4054 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004055 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004056 if( status != PSA_SUCCESS )
4057 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004058 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004059 if( status != PSA_SUCCESS )
4060 return( status );
4061 status = psa_hmac_finish_internal( &hkdf->hmac,
4062 hkdf->prk,
4063 sizeof( hkdf->prk ) );
4064 if( status != PSA_SUCCESS )
4065 return( status );
4066 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4067 hkdf->block_number = 0;
4068 hkdf->info_length = label_length;
4069 if( label_length != 0 )
4070 {
4071 hkdf->info = mbedtls_calloc( 1, label_length );
4072 if( hkdf->info == NULL )
4073 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4074 memcpy( hkdf->info, label, label_length );
4075 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004076 hkdf->state = HKDF_STATE_KEYED;
4077 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004078 return( PSA_SUCCESS );
4079}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004080#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004081
Gilles Peskinea05219c2018-11-16 16:02:56 +01004082#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004083/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4084 *
4085 * Note that if this function fails, you must call psa_generator_abort()
4086 * to potentially free embedded data structures and wipe confidential data.
4087 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004088static psa_status_t psa_generator_tls12_prf_setup(
4089 psa_tls12_prf_generator_t *tls12_prf,
4090 const unsigned char *key,
4091 size_t key_len,
4092 psa_algorithm_t hash_alg,
4093 const uint8_t *salt,
4094 size_t salt_length,
4095 const uint8_t *label,
4096 size_t label_length )
4097{
4098 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004099 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4100 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004101
4102 tls12_prf->key = mbedtls_calloc( 1, key_len );
4103 if( tls12_prf->key == NULL )
4104 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4105 tls12_prf->key_len = key_len;
4106 memcpy( tls12_prf->key, key, key_len );
4107
Hanno Becker580fba12018-11-13 20:50:45 +00004108 overflow = ( salt_length + label_length < salt_length ) ||
4109 ( salt_length + label_length + hash_length < hash_length );
4110 if( overflow )
4111 return( PSA_ERROR_INVALID_ARGUMENT );
4112
4113 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4114 if( tls12_prf->Ai_with_seed == NULL )
4115 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4116 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4117
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004118 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4119 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004120 if( label_length != 0 )
4121 {
4122 memcpy( tls12_prf->Ai_with_seed + hash_length,
4123 label, label_length );
4124 }
4125
4126 if( salt_length != 0 )
4127 {
4128 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4129 salt, salt_length );
4130 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004131
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004132 /* The first block gets generated when
4133 * psa_generator_read() is called. */
4134 tls12_prf->block_number = 0;
4135 tls12_prf->offset_in_block = hash_length;
4136
4137 return( PSA_SUCCESS );
4138}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004139
4140/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4141static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4142 psa_tls12_prf_generator_t *tls12_prf,
4143 const unsigned char *psk,
4144 size_t psk_len,
4145 psa_algorithm_t hash_alg,
4146 const uint8_t *salt,
4147 size_t salt_length,
4148 const uint8_t *label,
4149 size_t label_length )
4150{
4151 psa_status_t status;
4152 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4153
4154 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4155 return( PSA_ERROR_INVALID_ARGUMENT );
4156
4157 /* Quoting RFC 4279, Section 2:
4158 *
4159 * The premaster secret is formed as follows: if the PSK is N octets
4160 * long, concatenate a uint16 with the value N, N zero octets, a second
4161 * uint16 with the value N, and the PSK itself.
4162 */
4163
4164 pms[0] = ( psk_len >> 8 ) & 0xff;
4165 pms[1] = ( psk_len >> 0 ) & 0xff;
4166 memset( pms + 2, 0, psk_len );
4167 pms[2 + psk_len + 0] = pms[0];
4168 pms[2 + psk_len + 1] = pms[1];
4169 memcpy( pms + 4 + psk_len, psk, psk_len );
4170
4171 status = psa_generator_tls12_prf_setup( tls12_prf,
4172 pms, 4 + 2 * psk_len,
4173 hash_alg,
4174 salt, salt_length,
4175 label, label_length );
4176
Gilles Peskine3f108122018-12-07 18:14:53 +01004177 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004178 return( status );
4179}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004180#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004181
Gilles Peskine346797d2018-11-16 16:05:06 +01004182/* Note that if this function fails, you must call psa_generator_abort()
4183 * to potentially free embedded data structures and wipe confidential data.
4184 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004185static psa_status_t psa_key_derivation_internal(
4186 psa_crypto_generator_t *generator,
4187 const uint8_t *secret, size_t secret_length,
4188 psa_algorithm_t alg,
4189 const uint8_t *salt, size_t salt_length,
4190 const uint8_t *label, size_t label_length,
4191 size_t capacity )
4192{
4193 psa_status_t status;
4194 size_t max_capacity;
4195
4196 /* Set generator->alg even on failure so that abort knows what to do. */
4197 generator->alg = alg;
4198
Gilles Peskine751d9652018-09-18 12:05:44 +02004199 if( alg == PSA_ALG_SELECT_RAW )
4200 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004201 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004202 if( salt_length != 0 )
4203 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004204 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004205 if( label_length != 0 )
4206 return( PSA_ERROR_INVALID_ARGUMENT );
4207 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4208 if( generator->ctx.buffer.data == NULL )
4209 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4210 memcpy( generator->ctx.buffer.data, secret, secret_length );
4211 generator->ctx.buffer.size = secret_length;
4212 max_capacity = secret_length;
4213 status = PSA_SUCCESS;
4214 }
4215 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004216#if defined(MBEDTLS_MD_C)
4217 if( PSA_ALG_IS_HKDF( alg ) )
4218 {
4219 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4220 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4221 if( hash_size == 0 )
4222 return( PSA_ERROR_NOT_SUPPORTED );
4223 max_capacity = 255 * hash_size;
4224 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4225 secret, secret_length,
4226 hash_alg,
4227 salt, salt_length,
4228 label, label_length );
4229 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004230 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4231 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4232 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004233 {
4234 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4235 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4236
4237 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4238 if( hash_alg != PSA_ALG_SHA_256 &&
4239 hash_alg != PSA_ALG_SHA_384 )
4240 {
4241 return( PSA_ERROR_NOT_SUPPORTED );
4242 }
4243
4244 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004245
4246 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4247 {
4248 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4249 secret, secret_length,
4250 hash_alg, salt, salt_length,
4251 label, label_length );
4252 }
4253 else
4254 {
4255 status = psa_generator_tls12_psk_to_ms_setup(
4256 &generator->ctx.tls12_prf,
4257 secret, secret_length,
4258 hash_alg, salt, salt_length,
4259 label, label_length );
4260 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004261 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004262 else
4263#endif
4264 {
4265 return( PSA_ERROR_NOT_SUPPORTED );
4266 }
4267
4268 if( status != PSA_SUCCESS )
4269 return( status );
4270
4271 if( capacity <= max_capacity )
4272 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004273 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4274 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004275 else
4276 return( PSA_ERROR_INVALID_ARGUMENT );
4277
4278 return( PSA_SUCCESS );
4279}
4280
Gilles Peskineea0fb492018-07-12 17:17:20 +02004281psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004282 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004283 psa_algorithm_t alg,
4284 const uint8_t *salt,
4285 size_t salt_length,
4286 const uint8_t *label,
4287 size_t label_length,
4288 size_t capacity )
4289{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004290 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004291 psa_status_t status;
4292
4293 if( generator->alg != 0 )
4294 return( PSA_ERROR_BAD_STATE );
4295
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004296 /* Make sure that alg is a key derivation algorithm. This prevents
4297 * key selection algorithms, which psa_key_derivation_internal
4298 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004299 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4300 return( PSA_ERROR_INVALID_ARGUMENT );
4301
Gilles Peskinec5487a82018-12-03 18:08:14 +01004302 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004303 if( status != PSA_SUCCESS )
4304 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004305
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004306 if( slot->type != PSA_KEY_TYPE_DERIVE )
4307 return( PSA_ERROR_INVALID_ARGUMENT );
4308
4309 status = psa_key_derivation_internal( generator,
4310 slot->data.raw.data,
4311 slot->data.raw.bytes,
4312 alg,
4313 salt, salt_length,
4314 label, label_length,
4315 capacity );
4316 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004317 psa_generator_abort( generator );
4318 return( status );
4319}
4320
Gilles Peskine969c5d62019-01-16 15:53:06 +01004321static psa_status_t psa_key_derivation_setup_kdf(
4322 psa_crypto_generator_t *generator,
4323 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004324{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004325 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004326#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004327 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4328 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4329 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004330 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004331 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004332 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4333 if( hash_size == 0 )
4334 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004335 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4336 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004337 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004338 {
4339 return( PSA_ERROR_NOT_SUPPORTED );
4340 }
4341 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004342 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004343 }
4344#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004345 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004346 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004347}
4348
4349psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4350 psa_algorithm_t alg )
4351{
4352 psa_status_t status;
4353
4354 if( generator->alg != 0 )
4355 return( PSA_ERROR_BAD_STATE );
4356
Gilles Peskine6843c292019-01-18 16:44:49 +01004357 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4358 return( PSA_ERROR_INVALID_ARGUMENT );
4359 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004360 {
4361 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004362 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004363 }
4364 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4365 {
4366 status = psa_key_derivation_setup_kdf( generator, alg );
4367 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004368 else
4369 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004370
4371 if( status == PSA_SUCCESS )
4372 generator->alg = alg;
4373 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004374}
4375
4376#if defined(MBEDTLS_MD_C)
4377static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4378 psa_algorithm_t hash_alg,
4379 psa_key_derivation_step_t step,
4380 const uint8_t *data,
4381 size_t data_length )
4382{
4383 psa_status_t status;
4384 switch( step )
4385 {
4386 case PSA_KDF_STEP_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004387 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004388 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004389 status = psa_hmac_setup_internal( &hkdf->hmac,
4390 data, data_length,
4391 hash_alg );
4392 if( status != PSA_SUCCESS )
4393 return( status );
4394 hkdf->state = HKDF_STATE_STARTED;
4395 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004396 case PSA_KDF_STEP_SECRET:
4397 /* If no salt was provided, use an empty salt. */
4398 if( hkdf->state == HKDF_STATE_INIT )
4399 {
4400 status = psa_hmac_setup_internal( &hkdf->hmac,
4401 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004402 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004403 if( status != PSA_SUCCESS )
4404 return( status );
4405 hkdf->state = HKDF_STATE_STARTED;
4406 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004407 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004408 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004409 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4410 data, data_length );
4411 if( status != PSA_SUCCESS )
4412 return( status );
4413 status = psa_hmac_finish_internal( &hkdf->hmac,
4414 hkdf->prk,
4415 sizeof( hkdf->prk ) );
4416 if( status != PSA_SUCCESS )
4417 return( status );
4418 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4419 hkdf->block_number = 0;
4420 hkdf->state = HKDF_STATE_KEYED;
4421 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004422 case PSA_KDF_STEP_INFO:
4423 if( hkdf->state == HKDF_STATE_OUTPUT )
4424 return( PSA_ERROR_BAD_STATE );
4425 if( hkdf->info_set )
4426 return( PSA_ERROR_BAD_STATE );
4427 hkdf->info_length = data_length;
4428 if( data_length != 0 )
4429 {
4430 hkdf->info = mbedtls_calloc( 1, data_length );
4431 if( hkdf->info == NULL )
4432 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4433 memcpy( hkdf->info, data, data_length );
4434 }
4435 hkdf->info_set = 1;
4436 return( PSA_SUCCESS );
4437 default:
4438 return( PSA_ERROR_INVALID_ARGUMENT );
4439 }
4440}
4441#endif /* MBEDTLS_MD_C */
4442
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004443static psa_status_t psa_key_derivation_input_raw(
4444 psa_crypto_generator_t *generator,
4445 psa_key_derivation_step_t step,
4446 const uint8_t *data,
4447 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004448{
4449 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004450 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004451
Gilles Peskine969c5d62019-01-16 15:53:06 +01004452 if( kdf_alg == PSA_ALG_SELECT_RAW )
4453 {
4454 if( generator->capacity != 0 )
4455 return( PSA_ERROR_INVALID_ARGUMENT );
4456 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4457 if( generator->ctx.buffer.data == NULL )
4458 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4459 memcpy( generator->ctx.buffer.data, data, data_length );
4460 generator->ctx.buffer.size = data_length;
4461 generator->capacity = data_length;
4462 status = PSA_SUCCESS;
4463 }
4464 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004465#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004466 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004467 {
4468 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004469 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004470 step, data, data_length );
4471 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004472 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004473#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004474#if defined(MBEDTLS_MD_C)
4475 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004476 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4477 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004478 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004479 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004480 status = PSA_ERROR_NOT_SUPPORTED;
4481 }
4482 else
4483#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004484 {
4485 /* This can't happen unless the generator object was not initialized */
4486 return( PSA_ERROR_BAD_STATE );
4487 }
4488
4489 if( status != PSA_SUCCESS )
4490 psa_generator_abort( generator );
4491 return( status );
4492}
4493
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004494psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4495 psa_key_derivation_step_t step,
4496 const uint8_t *data,
4497 size_t data_length )
4498{
4499 switch( step )
4500 {
4501 case PSA_KDF_STEP_LABEL:
4502 case PSA_KDF_STEP_SALT:
4503 case PSA_KDF_STEP_INFO:
4504 return( psa_key_derivation_input_raw( generator, step,
4505 data, data_length ) );
4506 default:
4507 return( PSA_ERROR_INVALID_ARGUMENT );
4508 }
4509}
4510
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004511psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4512 psa_key_derivation_step_t step,
4513 psa_key_handle_t handle )
4514{
4515 psa_key_slot_t *slot;
4516 psa_status_t status;
4517 status = psa_get_key_from_slot( handle, &slot,
4518 PSA_KEY_USAGE_DERIVE,
4519 generator->alg );
4520 if( status != PSA_SUCCESS )
4521 return( status );
4522 if( slot->type != PSA_KEY_TYPE_DERIVE )
4523 return( PSA_ERROR_INVALID_ARGUMENT );
4524 /* Don't allow a key to be used as an input that is usually public.
4525 * This is debatable. It's ok from a cryptographic perspective to
4526 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004527 * the material should be dedicated to a particular input step,
4528 * otherwise this may allow the key to be used in an unintended way
4529 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004530 if( step != PSA_KDF_STEP_SECRET )
4531 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004532 return( psa_key_derivation_input_raw( generator,
4533 step,
4534 slot->data.raw.data,
4535 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004536}
4537
Gilles Peskineea0fb492018-07-12 17:17:20 +02004538
4539
4540/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004541/* Key agreement */
4542/****************************************************************/
4543
Gilles Peskinea05219c2018-11-16 16:02:56 +01004544#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004545static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4546 size_t peer_key_length,
4547 const mbedtls_ecp_keypair *our_key,
4548 uint8_t *shared_secret,
4549 size_t shared_secret_size,
4550 size_t *shared_secret_length )
4551{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004552 mbedtls_ecp_keypair *their_key = NULL;
4553 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004554 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004555 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004556
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004557 status = psa_import_ec_public_key(
4558 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4559 peer_key, peer_key_length,
4560 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004561 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004562 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004563
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004564 status = mbedtls_to_psa_error(
4565 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4566 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004567 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004568 status = mbedtls_to_psa_error(
4569 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4570 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004571 goto exit;
4572
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004573 status = mbedtls_to_psa_error(
4574 mbedtls_ecdh_calc_secret( &ecdh,
4575 shared_secret_length,
4576 shared_secret, shared_secret_size,
4577 mbedtls_ctr_drbg_random,
4578 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004579
4580exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004581 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004582 mbedtls_ecp_keypair_free( their_key );
4583 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004584 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004585}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004586#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004587
Gilles Peskine01d718c2018-09-18 12:01:02 +02004588#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4589
Gilles Peskine0216fe12019-04-11 21:23:21 +02004590static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4591 psa_key_slot_t *private_key,
4592 const uint8_t *peer_key,
4593 size_t peer_key_length,
4594 uint8_t *shared_secret,
4595 size_t shared_secret_size,
4596 size_t *shared_secret_length )
4597{
4598 switch( alg )
4599 {
4600#if defined(MBEDTLS_ECDH_C)
4601 case PSA_ALG_ECDH:
4602 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4603 return( PSA_ERROR_INVALID_ARGUMENT );
4604 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
4605 private_key->data.ecp,
4606 shared_secret, shared_secret_size,
4607 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004608#endif /* MBEDTLS_ECDH_C */
4609 default:
4610 (void) private_key;
4611 (void) peer_key;
4612 (void) peer_key_length;
4613 (void) shared_secret;
4614 (void) shared_secret_size;
4615 (void) shared_secret_length;
4616 return( PSA_ERROR_NOT_SUPPORTED );
4617 }
4618}
4619
Gilles Peskine346797d2018-11-16 16:05:06 +01004620/* Note that if this function fails, you must call psa_generator_abort()
4621 * to potentially free embedded data structures and wipe confidential data.
4622 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004623static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004624 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004625 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004626 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004627 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004628{
4629 psa_status_t status;
4630 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4631 size_t shared_secret_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004632 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004633
4634 /* Step 1: run the secret agreement algorithm to generate the shared
4635 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004636 status = psa_key_agreement_raw_internal( ka_alg,
4637 private_key,
4638 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004639 shared_secret,
4640 sizeof( shared_secret ),
4641 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004642 if( status != PSA_SUCCESS )
4643 goto exit;
4644
4645 /* Step 2: set up the key derivation to generate key material from
4646 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004647 status = psa_key_derivation_input_raw( generator, step,
4648 shared_secret, shared_secret_length );
4649
Gilles Peskine01d718c2018-09-18 12:01:02 +02004650exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004651 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004652 return( status );
4653}
4654
4655psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004656 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004657 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004658 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004659 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004660{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004661 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004662 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004663 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004664 return( PSA_ERROR_INVALID_ARGUMENT );
4665 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004666 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004667 if( status != PSA_SUCCESS )
4668 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004669 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004670 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004671 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004672 if( status != PSA_SUCCESS )
4673 psa_generator_abort( generator );
4674 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004675}
4676
Gilles Peskine0216fe12019-04-11 21:23:21 +02004677psa_status_t psa_key_agreement_raw_shared_secret( psa_algorithm_t alg,
4678 psa_key_handle_t private_key,
4679 const uint8_t *peer_key,
4680 size_t peer_key_length,
4681 uint8_t *output,
4682 size_t output_size,
4683 size_t *output_length )
4684{
4685 psa_key_slot_t *slot;
4686 psa_status_t status;
4687
4688 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4689 {
4690 status = PSA_ERROR_INVALID_ARGUMENT;
4691 goto exit;
4692 }
4693 status = psa_get_key_from_slot( private_key, &slot,
4694 PSA_KEY_USAGE_DERIVE, alg );
4695 if( status != PSA_SUCCESS )
4696 goto exit;
4697
4698 status = psa_key_agreement_raw_internal( alg, slot,
4699 peer_key, peer_key_length,
4700 output, output_size,
4701 output_length );
4702
4703exit:
4704 if( status != PSA_SUCCESS )
4705 {
4706 /* If an error happens and is not handled properly, the output
4707 * may be used as a key to protect sensitive data. Arrange for such
4708 * a key to be random, which is likely to result in decryption or
4709 * verification errors. This is better than filling the buffer with
4710 * some constant data such as zeros, which would result in the data
4711 * being protected with a reproducible, easily knowable key.
4712 */
4713 psa_generate_random( output, output_size );
4714 *output_length = output_size;
4715 }
4716 return( status );
4717}
Gilles Peskine01d718c2018-09-18 12:01:02 +02004718
4719
4720/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004721/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004722/****************************************************************/
4723
4724psa_status_t psa_generate_random( uint8_t *output,
4725 size_t output_size )
4726{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004727 int ret;
4728 GUARD_MODULE_INITIALIZED;
4729
4730 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004731 return( mbedtls_to_psa_error( ret ) );
4732}
4733
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004734#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
4735#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02004736
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004737psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4738 size_t seed_size )
4739{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004740 if( global_data.initialized )
4741 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004742
4743 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4744 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4745 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4746 return( PSA_ERROR_INVALID_ARGUMENT );
4747
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004748 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004749}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004750#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004751
Gilles Peskine87a5e562019-04-17 12:28:25 +02004752psa_status_t psa_generate_key_to_handle( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004753 psa_key_type_t type,
4754 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004755 const void *extra,
4756 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004757{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004758 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004759 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004760
Gilles Peskine53d991e2018-07-12 01:14:59 +02004761 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004762 return( PSA_ERROR_INVALID_ARGUMENT );
4763
Gilles Peskinec5487a82018-12-03 18:08:14 +01004764 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004765 if( status != PSA_SUCCESS )
4766 return( status );
4767
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004768 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004769 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004770 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004771 if( status != PSA_SUCCESS )
4772 return( status );
4773 status = psa_generate_random( slot->data.raw.data,
4774 slot->data.raw.bytes );
4775 if( status != PSA_SUCCESS )
4776 {
4777 mbedtls_free( slot->data.raw.data );
4778 return( status );
4779 }
4780#if defined(MBEDTLS_DES_C)
4781 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004782 psa_des_set_key_parity( slot->data.raw.data,
4783 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004784#endif /* MBEDTLS_DES_C */
4785 }
4786 else
4787
4788#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4789 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4790 {
4791 mbedtls_rsa_context *rsa;
4792 int ret;
4793 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004794 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4795 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004796 /* Accept only byte-aligned keys, for the same reasons as
4797 * in psa_import_rsa_key(). */
4798 if( bits % 8 != 0 )
4799 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004800 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004801 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004802 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004803 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004804 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004805#if INT_MAX < 0xffffffff
4806 /* Check that the uint32_t value passed by the caller fits
4807 * in the range supported by this implementation. */
4808 if( p->e > INT_MAX )
4809 return( PSA_ERROR_NOT_SUPPORTED );
4810#endif
4811 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004812 }
4813 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4814 if( rsa == NULL )
4815 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4816 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4817 ret = mbedtls_rsa_gen_key( rsa,
4818 mbedtls_ctr_drbg_random,
4819 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004820 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004821 exponent );
4822 if( ret != 0 )
4823 {
4824 mbedtls_rsa_free( rsa );
4825 mbedtls_free( rsa );
4826 return( mbedtls_to_psa_error( ret ) );
4827 }
4828 slot->data.rsa = rsa;
4829 }
4830 else
4831#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4832
4833#if defined(MBEDTLS_ECP_C)
4834 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4835 {
4836 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4837 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4838 const mbedtls_ecp_curve_info *curve_info =
4839 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4840 mbedtls_ecp_keypair *ecp;
4841 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004842 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004843 return( PSA_ERROR_NOT_SUPPORTED );
4844 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4845 return( PSA_ERROR_NOT_SUPPORTED );
4846 if( curve_info->bit_size != bits )
4847 return( PSA_ERROR_INVALID_ARGUMENT );
4848 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4849 if( ecp == NULL )
4850 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4851 mbedtls_ecp_keypair_init( ecp );
4852 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4853 mbedtls_ctr_drbg_random,
4854 &global_data.ctr_drbg );
4855 if( ret != 0 )
4856 {
4857 mbedtls_ecp_keypair_free( ecp );
4858 mbedtls_free( ecp );
4859 return( mbedtls_to_psa_error( ret ) );
4860 }
4861 slot->data.ecp = ecp;
4862 }
4863 else
4864#endif /* MBEDTLS_ECP_C */
4865
4866 return( PSA_ERROR_NOT_SUPPORTED );
4867
4868 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004869
4870#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4871 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4872 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004873 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004874 }
4875#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4876
4877 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004878}
4879
4880
4881/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004882/* Module setup */
4883/****************************************************************/
4884
Gilles Peskine5e769522018-11-20 21:59:56 +01004885psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4886 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4887 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4888{
4889 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4890 return( PSA_ERROR_BAD_STATE );
4891 global_data.entropy_init = entropy_init;
4892 global_data.entropy_free = entropy_free;
4893 return( PSA_SUCCESS );
4894}
4895
Gilles Peskinee59236f2018-01-27 23:32:46 +01004896void mbedtls_psa_crypto_free( void )
4897{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004898 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004899 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4900 {
4901 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004902 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004903 }
4904 /* Wipe all remaining data, including configuration.
4905 * In particular, this sets all state indicator to the value
4906 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004907 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004908}
4909
4910psa_status_t psa_crypto_init( void )
4911{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004912 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004913 const unsigned char drbg_seed[] = "PSA";
4914
Gilles Peskinec6b69072018-11-20 21:42:52 +01004915 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004916 if( global_data.initialized != 0 )
4917 return( PSA_SUCCESS );
4918
Gilles Peskine5e769522018-11-20 21:59:56 +01004919 /* Set default configuration if
4920 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4921 if( global_data.entropy_init == NULL )
4922 global_data.entropy_init = mbedtls_entropy_init;
4923 if( global_data.entropy_free == NULL )
4924 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004925
Gilles Peskinec6b69072018-11-20 21:42:52 +01004926 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004927 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004928 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004929 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004930 status = mbedtls_to_psa_error(
4931 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4932 mbedtls_entropy_func,
4933 &global_data.entropy,
4934 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4935 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004936 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004937 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004938
Gilles Peskine66fb1262018-12-10 16:29:04 +01004939 status = psa_initialize_key_slots( );
4940 if( status != PSA_SUCCESS )
4941 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004942
4943 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004944 global_data.initialized = 1;
4945
Gilles Peskinee59236f2018-01-27 23:32:46 +01004946exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004947 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004948 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004949 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004950}
4951
4952#endif /* MBEDTLS_PSA_CRYPTO_C */