blob: b0acc308db260e62c2c1304b64849121c94d0f3f [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 Peskinedb4b3ab2019-04-18 12:53:01 +0200968static size_t psa_get_key_slot_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 Peskine8c8f2ab2019-04-18 21:44:46 +0200984void psa_reset_key_attributes( psa_key_attributes_t *attributes )
985{
Gilles Peskineb699f072019-04-26 16:06:02 +0200986 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200987 memset( attributes, 0, sizeof( *attributes ) );
988}
989
Gilles Peskineb699f072019-04-26 16:06:02 +0200990psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
991 psa_key_type_t type,
992 const uint8_t *data,
993 size_t data_length )
994{
995 uint8_t *copy = NULL;
996
997 if( data_length != 0 )
998 {
999 copy = mbedtls_calloc( 1, data_length );
1000 if( copy == NULL )
1001 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1002 memcpy( copy, data, data_length );
1003 }
1004 /* After this point, this function is guaranteed to succeed, so it
1005 * can start modifying `*attributes`. */
1006
1007 if( attributes->domain_parameters != NULL )
1008 {
1009 mbedtls_free( attributes->domain_parameters );
1010 attributes->domain_parameters = NULL;
1011 attributes->domain_parameters_size = 0;
1012 }
1013
1014 attributes->domain_parameters = copy;
1015 attributes->domain_parameters_size = data_length;
1016 attributes->type = type;
1017 return( PSA_SUCCESS );
1018}
1019
1020psa_status_t psa_get_key_domain_parameters(
1021 const psa_key_attributes_t *attributes,
1022 uint8_t *data, size_t data_size, size_t *data_length )
1023{
1024 if( attributes->domain_parameters_size > data_size )
1025 return( PSA_ERROR_BUFFER_TOO_SMALL );
1026 *data_length = attributes->domain_parameters_size;
1027 if( attributes->domain_parameters_size != 0 )
1028 memcpy( data, attributes->domain_parameters,
1029 attributes->domain_parameters_size );
1030 return( PSA_SUCCESS );
1031}
1032
1033#if defined(MBEDTLS_RSA_C)
1034static psa_status_t psa_get_rsa_public_exponent(
1035 const mbedtls_rsa_context *rsa,
1036 psa_key_attributes_t *attributes )
1037{
1038 mbedtls_mpi mpi;
1039 int ret;
1040 uint8_t *buffer = NULL;
1041 size_t buflen;
1042 mbedtls_mpi_init( &mpi );
1043
1044 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1045 if( ret != 0 )
1046 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001047 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1048 {
1049 /* It's the default value, which is reported as an empty string,
1050 * so there's nothing to do. */
1051 goto exit;
1052 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001053
1054 buflen = mbedtls_mpi_size( &mpi );
1055 buffer = mbedtls_calloc( 1, buflen );
1056 if( buffer == NULL )
1057 {
1058 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1059 goto exit;
1060 }
1061 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1062 if( ret != 0 )
1063 goto exit;
1064 attributes->domain_parameters = buffer;
1065 attributes->domain_parameters_size = buflen;
1066
1067exit:
1068 mbedtls_mpi_free( &mpi );
1069 if( ret != 0 )
1070 mbedtls_free( buffer );
1071 return( mbedtls_to_psa_error( ret ) );
1072}
1073#endif /* MBEDTLS_RSA_C */
1074
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001075psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1076 psa_key_attributes_t *attributes )
1077{
1078 psa_key_slot_t *slot;
1079 psa_status_t status;
1080
1081 psa_reset_key_attributes( attributes );
1082
1083 status = psa_get_key_slot( handle, &slot );
1084 if( status != PSA_SUCCESS )
1085 return( status );
1086
1087 attributes->id = slot->persistent_storage_id;
1088 attributes->lifetime = slot->lifetime;
1089 attributes->policy = slot->policy;
1090 attributes->type = slot->type;
1091 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001092
1093 switch( slot->type )
1094 {
1095#if defined(MBEDTLS_RSA_C)
1096 case PSA_KEY_TYPE_RSA_KEYPAIR:
1097 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1098 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1099 break;
1100#endif
1101 default:
1102 /* Nothing else to do. */
1103 break;
1104 }
1105
1106 if( status != PSA_SUCCESS )
1107 psa_reset_key_attributes( attributes );
1108 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001109}
1110
Gilles Peskinec5487a82018-12-03 18:08:14 +01001111psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001112 psa_key_type_t *type,
1113 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001114{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001115 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001116 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001117
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001118 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001119 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001120 if( bits != NULL )
1121 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001122 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001123 if( status != PSA_SUCCESS )
1124 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001125
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001126 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001127 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001128 if( type != NULL )
1129 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001130 if( bits != NULL )
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001131 *bits = psa_get_key_slot_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001132 return( PSA_SUCCESS );
1133}
1134
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001135#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001136static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1137 unsigned char *buf, size_t size )
1138{
1139 int ret;
1140 unsigned char *c;
1141 size_t len = 0;
1142
1143 c = buf + size;
1144
1145 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1146
1147 return( (int) len );
1148}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001149#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001150
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001151static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1152 uint8_t *data,
1153 size_t data_size,
1154 size_t *data_length,
1155 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001156{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001157 *data_length = 0;
1158
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001159 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001160 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001161
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001162 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001163 {
1164 if( slot->data.raw.bytes > data_size )
1165 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001166 if( data_size != 0 )
1167 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001168 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001169 memset( data + slot->data.raw.bytes, 0,
1170 data_size - slot->data.raw.bytes );
1171 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001172 *data_length = slot->data.raw.bytes;
1173 return( PSA_SUCCESS );
1174 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001175#if defined(MBEDTLS_ECP_C)
1176 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1177 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001178 psa_status_t status;
1179
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001180 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001181 if( bytes > data_size )
1182 return( PSA_ERROR_BUFFER_TOO_SMALL );
1183 status = mbedtls_to_psa_error(
1184 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1185 if( status != PSA_SUCCESS )
1186 return( status );
1187 memset( data + bytes, 0, data_size - bytes );
1188 *data_length = bytes;
1189 return( PSA_SUCCESS );
1190 }
1191#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001192 else
Moran Peker17e36e12018-05-02 12:55:20 +03001193 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001194#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001195 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001196 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001197 {
Moran Pekera998bc62018-04-16 18:16:20 +03001198 mbedtls_pk_context pk;
1199 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001200 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001201 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001202#if defined(MBEDTLS_RSA_C)
1203 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001204 pk.pk_info = &mbedtls_rsa_info;
1205 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001206#else
1207 return( PSA_ERROR_NOT_SUPPORTED );
1208#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001209 }
1210 else
1211 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001212#if defined(MBEDTLS_ECP_C)
1213 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001214 pk.pk_info = &mbedtls_eckey_info;
1215 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001216#else
1217 return( PSA_ERROR_NOT_SUPPORTED );
1218#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001219 }
Moran Pekerd7326592018-05-29 16:56:39 +03001220 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001221 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001222 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001223 }
Moran Peker17e36e12018-05-02 12:55:20 +03001224 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001225 {
Moran Peker17e36e12018-05-02 12:55:20 +03001226 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001227 }
Moran Peker60364322018-04-29 11:34:58 +03001228 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001229 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001230 /* If data_size is 0 then data may be NULL and then the
1231 * call to memset would have undefined behavior. */
1232 if( data_size != 0 )
1233 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001234 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001235 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001236 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1237 * Move the data to the beginning and erase remaining data
1238 * at the original location. */
1239 if( 2 * (size_t) ret <= data_size )
1240 {
1241 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001242 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001243 }
1244 else if( (size_t) ret < data_size )
1245 {
1246 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001247 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001248 }
Moran Pekera998bc62018-04-16 18:16:20 +03001249 *data_length = ret;
1250 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001251 }
1252 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001253#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001254 {
1255 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001256 it is valid for a special-purpose implementation to omit
1257 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001258 return( PSA_ERROR_NOT_SUPPORTED );
1259 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001260 }
1261}
1262
Gilles Peskinec5487a82018-12-03 18:08:14 +01001263psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001264 uint8_t *data,
1265 size_t data_size,
1266 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001267{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001268 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001269 psa_status_t status;
1270
1271 /* Set the key to empty now, so that even when there are errors, we always
1272 * set data_length to a value between 0 and data_size. On error, setting
1273 * the key to empty is a good choice because an empty key representation is
1274 * unlikely to be accepted anywhere. */
1275 *data_length = 0;
1276
1277 /* Export requires the EXPORT flag. There is an exception for public keys,
1278 * which don't require any flag, but psa_get_key_from_slot takes
1279 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001280 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001281 if( status != PSA_SUCCESS )
1282 return( status );
1283 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001284 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001286
Gilles Peskinec5487a82018-12-03 18:08:14 +01001287psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001288 uint8_t *data,
1289 size_t data_size,
1290 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001291{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001292 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001293 psa_status_t status;
1294
1295 /* Set the key to empty now, so that even when there are errors, we always
1296 * set data_length to a value between 0 and data_size. On error, setting
1297 * the key to empty is a good choice because an empty key representation is
1298 * unlikely to be accepted anywhere. */
1299 *data_length = 0;
1300
1301 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001302 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001303 if( status != PSA_SUCCESS )
1304 return( status );
1305 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001306 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001307}
1308
Darryl Green0c6575a2018-11-07 16:05:30 +00001309#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001310static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001311 size_t bits )
1312{
1313 psa_status_t status;
1314 uint8_t *data;
1315 size_t key_length;
1316 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1317 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001318 if( data == NULL )
1319 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001320 /* Get key data in export format */
1321 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1322 if( status != PSA_SUCCESS )
1323 {
1324 slot->type = PSA_KEY_TYPE_NONE;
1325 goto exit;
1326 }
1327 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001328 status = psa_save_persistent_key( slot->persistent_storage_id,
1329 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001330 data, key_length );
1331 if( status != PSA_SUCCESS )
1332 {
1333 slot->type = PSA_KEY_TYPE_NONE;
1334 }
1335exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001336 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001337 mbedtls_free( data );
1338 return( status );
1339}
1340#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1341
Gilles Peskine4747d192019-04-17 15:05:45 +02001342static psa_status_t psa_set_key_policy_internal(
1343 psa_key_slot_t *slot,
1344 const psa_key_policy_t *policy )
1345{
1346 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001347 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001348 PSA_KEY_USAGE_ENCRYPT |
1349 PSA_KEY_USAGE_DECRYPT |
1350 PSA_KEY_USAGE_SIGN |
1351 PSA_KEY_USAGE_VERIFY |
1352 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1353 return( PSA_ERROR_INVALID_ARGUMENT );
1354
1355 slot->policy = *policy;
1356 return( PSA_SUCCESS );
1357}
1358
1359/** Prepare a key slot to receive key material.
1360 *
1361 * This function allocates a key slot and sets its metadata.
1362 *
1363 * If this function fails, call psa_fail_key_creation().
1364 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001365 * This function is intended to be used as follows:
1366 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1367 * it with the specified attributes, and assign it a handle.
1368 * -# Populate the slot with the key material.
1369 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1370 * In case of failure at any step, stop the sequence and call
1371 * psa_fail_key_creation().
1372 *
Gilles Peskine4747d192019-04-17 15:05:45 +02001373 * \param attributes Key attributes for the new key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001374 * \param handle On success, a handle for the allocated slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001375 * \param p_slot On success, a pointer to the prepared slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001376 *
1377 * \retval #PSA_SUCCESS
1378 * The key slot is ready to receive key material.
1379 * \return If this function fails, the key slot is an invalid state.
1380 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001381 */
1382static psa_status_t psa_start_key_creation(
1383 const psa_key_attributes_t *attributes,
1384 psa_key_handle_t *handle,
1385 psa_key_slot_t **p_slot )
1386{
1387 psa_status_t status;
1388 psa_key_slot_t *slot;
1389
1390 status = psa_allocate_key( handle );
1391 if( status != PSA_SUCCESS )
1392 return( status );
1393 status = psa_get_key_slot( *handle, p_slot );
1394 if( status != PSA_SUCCESS )
1395 return( status );
1396 slot = *p_slot;
1397
1398 status = psa_set_key_policy_internal( slot, &attributes->policy );
1399 if( status != PSA_SUCCESS )
1400 return( status );
1401 slot->lifetime = attributes->lifetime;
1402 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001403 {
1404 status = psa_validate_persistent_key_parameters( attributes->lifetime,
1405 attributes->id );
1406 if( status != PSA_SUCCESS )
1407 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001408 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001409 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001410 slot->type = attributes->type;
1411
1412 return( status );
1413}
1414
1415/** Finalize the creation of a key once its key material has been set.
1416 *
1417 * This entails writing the key to persistent storage.
1418 *
1419 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001420 * See the documentation of psa_start_key_creation() for the intended use
1421 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001422 *
1423 * \param slot Pointer to the slot with key material.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001424 *
1425 * \retval #PSA_SUCCESS
1426 * The key was successfully created. The handle is now valid.
1427 * \return If this function fails, the key slot is an invalid state.
1428 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001429 */
1430static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1431{
1432 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001433 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001434
1435#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1436 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1437 {
1438 uint8_t *buffer = NULL;
1439 size_t buffer_size = 0;
1440 size_t length;
1441
1442 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001443 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001444 buffer = mbedtls_calloc( 1, buffer_size );
1445 if( buffer == NULL && buffer_size != 0 )
1446 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1447 status = psa_internal_export_key( slot,
1448 buffer, buffer_size, &length,
1449 0 );
1450
1451 if( status == PSA_SUCCESS )
1452 {
1453 status = psa_save_persistent_key( slot->persistent_storage_id,
1454 slot->type, &slot->policy,
1455 buffer, length );
1456 }
1457
1458 if( buffer_size != 0 )
1459 mbedtls_platform_zeroize( buffer, buffer_size );
1460 mbedtls_free( buffer );
1461 }
1462#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1463
1464 return( status );
1465}
1466
1467/** Abort the creation of a key.
1468 *
1469 * You may call this function after calling psa_start_key_creation(),
1470 * or after psa_finish_key_creation() fails. In other circumstances, this
1471 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001472 * See the documentation of psa_start_key_creation() for the intended use
1473 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001474 *
1475 * \param slot Pointer to the slot with key material.
1476 */
1477static void psa_fail_key_creation( psa_key_slot_t *slot )
1478{
1479 if( slot == NULL )
1480 return;
1481 psa_wipe_key_slot( slot );
1482}
1483
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001484static psa_status_t psa_check_key_slot_attributes(
1485 const psa_key_slot_t *slot,
1486 const psa_key_attributes_t *attributes )
1487{
1488 if( attributes->type != 0 )
1489 {
1490 if( attributes->type != slot->type )
1491 return( PSA_ERROR_INVALID_ARGUMENT );
1492 }
1493
1494 if( attributes->domain_parameters_size != 0 )
1495 {
1496#if defined(MBEDTLS_RSA_C)
1497 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1498 {
1499 mbedtls_mpi actual, required;
1500 int ret;
1501 mbedtls_mpi_init( &actual );
1502 mbedtls_mpi_init( &required );
1503 ret = mbedtls_rsa_export( slot->data.rsa,
1504 NULL, NULL, NULL, NULL, &actual );
1505 if( ret != 0 )
1506 goto rsa_exit;
1507 ret = mbedtls_mpi_read_binary( &required,
1508 attributes->domain_parameters,
1509 attributes->domain_parameters_size );
1510 if( ret != 0 )
1511 goto rsa_exit;
1512 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1513 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1514 rsa_exit:
1515 mbedtls_mpi_free( &actual );
1516 mbedtls_mpi_free( &required );
1517 if( ret != 0)
1518 return( mbedtls_to_psa_error( ret ) );
1519 }
1520 else
1521#endif
1522 {
1523 return( PSA_ERROR_INVALID_ARGUMENT );
1524 }
1525 }
1526
1527 if( attributes->bits != 0 )
1528 {
1529 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1530 return( PSA_ERROR_INVALID_ARGUMENT );
1531 }
1532
1533 return( PSA_SUCCESS );
1534}
1535
Gilles Peskine4747d192019-04-17 15:05:45 +02001536psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
1537 psa_key_handle_t *handle,
1538 const uint8_t *data,
1539 size_t data_length )
1540{
1541 psa_status_t status;
1542 psa_key_slot_t *slot = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001543
Gilles Peskine4747d192019-04-17 15:05:45 +02001544 status = psa_start_key_creation( attributes, handle, &slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001545 if( status != PSA_SUCCESS )
1546 goto exit;
1547
1548 status = psa_import_key_into_slot( slot, data, data_length );
1549 if( status != PSA_SUCCESS )
1550 goto exit;
1551 status = psa_check_key_slot_attributes( slot, attributes );
1552 if( status != PSA_SUCCESS )
1553 goto exit;
1554
1555 status = psa_finish_key_creation( slot );
1556exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001557 if( status != PSA_SUCCESS )
1558 {
1559 psa_fail_key_creation( slot );
1560 *handle = 0;
1561 }
1562 return( status );
1563}
1564
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001565static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001566 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001567{
1568 psa_status_t status;
1569 uint8_t *buffer = NULL;
1570 size_t buffer_size = 0;
1571 size_t length;
1572
1573 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001574 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001575 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001576 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001577 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001578 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1579 if( status != PSA_SUCCESS )
1580 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001581 target->type = source->type;
1582 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001583
1584exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001585 if( buffer_size != 0 )
1586 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001587 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001588 return( status );
1589}
1590
Gilles Peskine87a5e562019-04-17 12:28:25 +02001591psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001592 psa_key_handle_t target_handle,
1593 const psa_key_policy_t *constraint)
1594{
1595 psa_key_slot_t *source_slot = NULL;
1596 psa_key_slot_t *target_slot = NULL;
1597 psa_key_policy_t new_policy;
1598 psa_status_t status;
1599 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1600 if( status != PSA_SUCCESS )
1601 return( status );
1602 status = psa_get_empty_key_slot( target_handle, &target_slot );
1603 if( status != PSA_SUCCESS )
1604 return( status );
1605
1606 new_policy = target_slot->policy;
1607 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1608 if( status != PSA_SUCCESS )
1609 return( status );
1610 if( constraint != NULL )
1611 {
1612 status = psa_restrict_key_policy( &new_policy, constraint );
1613 if( status != PSA_SUCCESS )
1614 return( status );
1615 }
1616
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001617 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001618 if( status != PSA_SUCCESS )
1619 return( status );
1620
1621 target_slot->policy = new_policy;
1622 return( PSA_SUCCESS );
1623}
1624
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001625psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1626 const psa_key_attributes_t *specified_attributes,
1627 psa_key_handle_t *target_handle )
1628{
1629 psa_status_t status;
1630 psa_key_slot_t *source_slot = NULL;
1631 psa_key_slot_t *target_slot = NULL;
1632 psa_key_attributes_t actual_attributes = *specified_attributes;
1633
1634 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1635 if( status != PSA_SUCCESS )
1636 goto exit;
1637
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001638 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1639 if( status != PSA_SUCCESS )
1640 goto exit;
1641
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001642 status = psa_restrict_key_policy( &actual_attributes.policy,
1643 &source_slot->policy );
1644 if( status != PSA_SUCCESS )
1645 goto exit;
1646
1647 status = psa_start_key_creation( &actual_attributes,
1648 target_handle, &target_slot );
1649 if( status != PSA_SUCCESS )
1650 goto exit;
1651
1652 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001653 if( status != PSA_SUCCESS )
1654 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001655
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001656 status = psa_finish_key_creation( target_slot );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001657exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001658 if( status != PSA_SUCCESS )
1659 {
1660 psa_fail_key_creation( target_slot );
1661 *target_handle = 0;
1662 }
1663 return( status );
1664}
1665
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001666
1667
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001668/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001669/* Message digests */
1670/****************************************************************/
1671
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001672static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001673{
1674 switch( alg )
1675 {
1676#if defined(MBEDTLS_MD2_C)
1677 case PSA_ALG_MD2:
1678 return( &mbedtls_md2_info );
1679#endif
1680#if defined(MBEDTLS_MD4_C)
1681 case PSA_ALG_MD4:
1682 return( &mbedtls_md4_info );
1683#endif
1684#if defined(MBEDTLS_MD5_C)
1685 case PSA_ALG_MD5:
1686 return( &mbedtls_md5_info );
1687#endif
1688#if defined(MBEDTLS_RIPEMD160_C)
1689 case PSA_ALG_RIPEMD160:
1690 return( &mbedtls_ripemd160_info );
1691#endif
1692#if defined(MBEDTLS_SHA1_C)
1693 case PSA_ALG_SHA_1:
1694 return( &mbedtls_sha1_info );
1695#endif
1696#if defined(MBEDTLS_SHA256_C)
1697 case PSA_ALG_SHA_224:
1698 return( &mbedtls_sha224_info );
1699 case PSA_ALG_SHA_256:
1700 return( &mbedtls_sha256_info );
1701#endif
1702#if defined(MBEDTLS_SHA512_C)
1703 case PSA_ALG_SHA_384:
1704 return( &mbedtls_sha384_info );
1705 case PSA_ALG_SHA_512:
1706 return( &mbedtls_sha512_info );
1707#endif
1708 default:
1709 return( NULL );
1710 }
1711}
1712
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001713psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1714{
1715 switch( operation->alg )
1716 {
Gilles Peskine81736312018-06-26 15:04:31 +02001717 case 0:
1718 /* The object has (apparently) been initialized but it is not
1719 * in use. It's ok to call abort on such an object, and there's
1720 * nothing to do. */
1721 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001722#if defined(MBEDTLS_MD2_C)
1723 case PSA_ALG_MD2:
1724 mbedtls_md2_free( &operation->ctx.md2 );
1725 break;
1726#endif
1727#if defined(MBEDTLS_MD4_C)
1728 case PSA_ALG_MD4:
1729 mbedtls_md4_free( &operation->ctx.md4 );
1730 break;
1731#endif
1732#if defined(MBEDTLS_MD5_C)
1733 case PSA_ALG_MD5:
1734 mbedtls_md5_free( &operation->ctx.md5 );
1735 break;
1736#endif
1737#if defined(MBEDTLS_RIPEMD160_C)
1738 case PSA_ALG_RIPEMD160:
1739 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1740 break;
1741#endif
1742#if defined(MBEDTLS_SHA1_C)
1743 case PSA_ALG_SHA_1:
1744 mbedtls_sha1_free( &operation->ctx.sha1 );
1745 break;
1746#endif
1747#if defined(MBEDTLS_SHA256_C)
1748 case PSA_ALG_SHA_224:
1749 case PSA_ALG_SHA_256:
1750 mbedtls_sha256_free( &operation->ctx.sha256 );
1751 break;
1752#endif
1753#if defined(MBEDTLS_SHA512_C)
1754 case PSA_ALG_SHA_384:
1755 case PSA_ALG_SHA_512:
1756 mbedtls_sha512_free( &operation->ctx.sha512 );
1757 break;
1758#endif
1759 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001760 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001761 }
1762 operation->alg = 0;
1763 return( PSA_SUCCESS );
1764}
1765
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001766psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001767 psa_algorithm_t alg )
1768{
1769 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001770
1771 /* A context must be freshly initialized before it can be set up. */
1772 if( operation->alg != 0 )
1773 {
1774 return( PSA_ERROR_BAD_STATE );
1775 }
1776
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001777 switch( alg )
1778 {
1779#if defined(MBEDTLS_MD2_C)
1780 case PSA_ALG_MD2:
1781 mbedtls_md2_init( &operation->ctx.md2 );
1782 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1783 break;
1784#endif
1785#if defined(MBEDTLS_MD4_C)
1786 case PSA_ALG_MD4:
1787 mbedtls_md4_init( &operation->ctx.md4 );
1788 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1789 break;
1790#endif
1791#if defined(MBEDTLS_MD5_C)
1792 case PSA_ALG_MD5:
1793 mbedtls_md5_init( &operation->ctx.md5 );
1794 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1795 break;
1796#endif
1797#if defined(MBEDTLS_RIPEMD160_C)
1798 case PSA_ALG_RIPEMD160:
1799 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1800 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1801 break;
1802#endif
1803#if defined(MBEDTLS_SHA1_C)
1804 case PSA_ALG_SHA_1:
1805 mbedtls_sha1_init( &operation->ctx.sha1 );
1806 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1807 break;
1808#endif
1809#if defined(MBEDTLS_SHA256_C)
1810 case PSA_ALG_SHA_224:
1811 mbedtls_sha256_init( &operation->ctx.sha256 );
1812 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1813 break;
1814 case PSA_ALG_SHA_256:
1815 mbedtls_sha256_init( &operation->ctx.sha256 );
1816 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1817 break;
1818#endif
1819#if defined(MBEDTLS_SHA512_C)
1820 case PSA_ALG_SHA_384:
1821 mbedtls_sha512_init( &operation->ctx.sha512 );
1822 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1823 break;
1824 case PSA_ALG_SHA_512:
1825 mbedtls_sha512_init( &operation->ctx.sha512 );
1826 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1827 break;
1828#endif
1829 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001830 return( PSA_ALG_IS_HASH( alg ) ?
1831 PSA_ERROR_NOT_SUPPORTED :
1832 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001833 }
1834 if( ret == 0 )
1835 operation->alg = alg;
1836 else
1837 psa_hash_abort( operation );
1838 return( mbedtls_to_psa_error( ret ) );
1839}
1840
1841psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1842 const uint8_t *input,
1843 size_t input_length )
1844{
1845 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001846
1847 /* Don't require hash implementations to behave correctly on a
1848 * zero-length input, which may have an invalid pointer. */
1849 if( input_length == 0 )
1850 return( PSA_SUCCESS );
1851
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001852 switch( operation->alg )
1853 {
1854#if defined(MBEDTLS_MD2_C)
1855 case PSA_ALG_MD2:
1856 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1857 input, input_length );
1858 break;
1859#endif
1860#if defined(MBEDTLS_MD4_C)
1861 case PSA_ALG_MD4:
1862 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1863 input, input_length );
1864 break;
1865#endif
1866#if defined(MBEDTLS_MD5_C)
1867 case PSA_ALG_MD5:
1868 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1869 input, input_length );
1870 break;
1871#endif
1872#if defined(MBEDTLS_RIPEMD160_C)
1873 case PSA_ALG_RIPEMD160:
1874 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1875 input, input_length );
1876 break;
1877#endif
1878#if defined(MBEDTLS_SHA1_C)
1879 case PSA_ALG_SHA_1:
1880 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1881 input, input_length );
1882 break;
1883#endif
1884#if defined(MBEDTLS_SHA256_C)
1885 case PSA_ALG_SHA_224:
1886 case PSA_ALG_SHA_256:
1887 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1888 input, input_length );
1889 break;
1890#endif
1891#if defined(MBEDTLS_SHA512_C)
1892 case PSA_ALG_SHA_384:
1893 case PSA_ALG_SHA_512:
1894 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1895 input, input_length );
1896 break;
1897#endif
1898 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001899 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001900 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001901
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001902 if( ret != 0 )
1903 psa_hash_abort( operation );
1904 return( mbedtls_to_psa_error( ret ) );
1905}
1906
1907psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1908 uint8_t *hash,
1909 size_t hash_size,
1910 size_t *hash_length )
1911{
itayzafrir40835d42018-08-02 13:14:17 +03001912 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001913 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001914 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001915
1916 /* Fill the output buffer with something that isn't a valid hash
1917 * (barring an attack on the hash and deliberately-crafted input),
1918 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001919 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001920 /* If hash_size is 0 then hash may be NULL and then the
1921 * call to memset would have undefined behavior. */
1922 if( hash_size != 0 )
1923 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001924
1925 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001926 {
1927 status = PSA_ERROR_BUFFER_TOO_SMALL;
1928 goto exit;
1929 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001930
1931 switch( operation->alg )
1932 {
1933#if defined(MBEDTLS_MD2_C)
1934 case PSA_ALG_MD2:
1935 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1936 break;
1937#endif
1938#if defined(MBEDTLS_MD4_C)
1939 case PSA_ALG_MD4:
1940 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1941 break;
1942#endif
1943#if defined(MBEDTLS_MD5_C)
1944 case PSA_ALG_MD5:
1945 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1946 break;
1947#endif
1948#if defined(MBEDTLS_RIPEMD160_C)
1949 case PSA_ALG_RIPEMD160:
1950 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1951 break;
1952#endif
1953#if defined(MBEDTLS_SHA1_C)
1954 case PSA_ALG_SHA_1:
1955 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1956 break;
1957#endif
1958#if defined(MBEDTLS_SHA256_C)
1959 case PSA_ALG_SHA_224:
1960 case PSA_ALG_SHA_256:
1961 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1962 break;
1963#endif
1964#if defined(MBEDTLS_SHA512_C)
1965 case PSA_ALG_SHA_384:
1966 case PSA_ALG_SHA_512:
1967 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1968 break;
1969#endif
1970 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001971 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001972 }
itayzafrir40835d42018-08-02 13:14:17 +03001973 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001974
itayzafrir40835d42018-08-02 13:14:17 +03001975exit:
1976 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001977 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001978 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001979 return( psa_hash_abort( operation ) );
1980 }
1981 else
1982 {
1983 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001984 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001985 }
1986}
1987
Gilles Peskine2d277862018-06-18 15:41:12 +02001988psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1989 const uint8_t *hash,
1990 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001991{
1992 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1993 size_t actual_hash_length;
1994 psa_status_t status = psa_hash_finish( operation,
1995 actual_hash, sizeof( actual_hash ),
1996 &actual_hash_length );
1997 if( status != PSA_SUCCESS )
1998 return( status );
1999 if( actual_hash_length != hash_length )
2000 return( PSA_ERROR_INVALID_SIGNATURE );
2001 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2002 return( PSA_ERROR_INVALID_SIGNATURE );
2003 return( PSA_SUCCESS );
2004}
2005
Gilles Peskineeb35d782019-01-22 17:56:16 +01002006psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2007 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002008{
2009 if( target_operation->alg != 0 )
2010 return( PSA_ERROR_BAD_STATE );
2011
2012 switch( source_operation->alg )
2013 {
2014 case 0:
2015 return( PSA_ERROR_BAD_STATE );
2016#if defined(MBEDTLS_MD2_C)
2017 case PSA_ALG_MD2:
2018 mbedtls_md2_clone( &target_operation->ctx.md2,
2019 &source_operation->ctx.md2 );
2020 break;
2021#endif
2022#if defined(MBEDTLS_MD4_C)
2023 case PSA_ALG_MD4:
2024 mbedtls_md4_clone( &target_operation->ctx.md4,
2025 &source_operation->ctx.md4 );
2026 break;
2027#endif
2028#if defined(MBEDTLS_MD5_C)
2029 case PSA_ALG_MD5:
2030 mbedtls_md5_clone( &target_operation->ctx.md5,
2031 &source_operation->ctx.md5 );
2032 break;
2033#endif
2034#if defined(MBEDTLS_RIPEMD160_C)
2035 case PSA_ALG_RIPEMD160:
2036 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2037 &source_operation->ctx.ripemd160 );
2038 break;
2039#endif
2040#if defined(MBEDTLS_SHA1_C)
2041 case PSA_ALG_SHA_1:
2042 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2043 &source_operation->ctx.sha1 );
2044 break;
2045#endif
2046#if defined(MBEDTLS_SHA256_C)
2047 case PSA_ALG_SHA_224:
2048 case PSA_ALG_SHA_256:
2049 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2050 &source_operation->ctx.sha256 );
2051 break;
2052#endif
2053#if defined(MBEDTLS_SHA512_C)
2054 case PSA_ALG_SHA_384:
2055 case PSA_ALG_SHA_512:
2056 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2057 &source_operation->ctx.sha512 );
2058 break;
2059#endif
2060 default:
2061 return( PSA_ERROR_NOT_SUPPORTED );
2062 }
2063
2064 target_operation->alg = source_operation->alg;
2065 return( PSA_SUCCESS );
2066}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002067
2068
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002069/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002070/* MAC */
2071/****************************************************************/
2072
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002073static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002074 psa_algorithm_t alg,
2075 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002076 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002077 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002078{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002079 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002080 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002081
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002082 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002083 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002084
Gilles Peskine8c9def32018-02-08 10:02:12 +01002085 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2086 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002087 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002088 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002089 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002090 mode = MBEDTLS_MODE_STREAM;
2091 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002092 case PSA_ALG_CTR:
2093 mode = MBEDTLS_MODE_CTR;
2094 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002095 case PSA_ALG_CFB:
2096 mode = MBEDTLS_MODE_CFB;
2097 break;
2098 case PSA_ALG_OFB:
2099 mode = MBEDTLS_MODE_OFB;
2100 break;
2101 case PSA_ALG_CBC_NO_PADDING:
2102 mode = MBEDTLS_MODE_CBC;
2103 break;
2104 case PSA_ALG_CBC_PKCS7:
2105 mode = MBEDTLS_MODE_CBC;
2106 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002107 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002108 mode = MBEDTLS_MODE_CCM;
2109 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002110 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002111 mode = MBEDTLS_MODE_GCM;
2112 break;
2113 default:
2114 return( NULL );
2115 }
2116 }
2117 else if( alg == PSA_ALG_CMAC )
2118 mode = MBEDTLS_MODE_ECB;
2119 else if( alg == PSA_ALG_GMAC )
2120 mode = MBEDTLS_MODE_GCM;
2121 else
2122 return( NULL );
2123
2124 switch( key_type )
2125 {
2126 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002127 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128 break;
2129 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002130 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2131 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002132 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002133 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002134 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002135 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002136 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2137 * but two-key Triple-DES is functionally three-key Triple-DES
2138 * with K1=K3, so that's how we present it to mbedtls. */
2139 if( key_bits == 128 )
2140 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002141 break;
2142 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002143 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002144 break;
2145 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002146 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002147 break;
2148 default:
2149 return( NULL );
2150 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002151 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002152 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002153
Jaeden Amero23bbb752018-06-26 14:16:54 +01002154 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2155 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002156}
2157
Gilles Peskinea05219c2018-11-16 16:02:56 +01002158#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002159static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002160{
Gilles Peskine2d277862018-06-18 15:41:12 +02002161 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002162 {
2163 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002164 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002165 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002166 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002167 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002168 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002169 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002170 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002171 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002172 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002173 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002174 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002175 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002176 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002177 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002178 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002179 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002180 return( 128 );
2181 default:
2182 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002183 }
2184}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002185#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002186
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002187/* Initialize the MAC operation structure. Once this function has been
2188 * called, psa_mac_abort can run and will do the right thing. */
2189static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2190 psa_algorithm_t alg )
2191{
2192 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2193
2194 operation->alg = alg;
2195 operation->key_set = 0;
2196 operation->iv_set = 0;
2197 operation->iv_required = 0;
2198 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002199 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002200
2201#if defined(MBEDTLS_CMAC_C)
2202 if( alg == PSA_ALG_CMAC )
2203 {
2204 operation->iv_required = 0;
2205 mbedtls_cipher_init( &operation->ctx.cmac );
2206 status = PSA_SUCCESS;
2207 }
2208 else
2209#endif /* MBEDTLS_CMAC_C */
2210#if defined(MBEDTLS_MD_C)
2211 if( PSA_ALG_IS_HMAC( operation->alg ) )
2212 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002213 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2214 operation->ctx.hmac.hash_ctx.alg = 0;
2215 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002216 }
2217 else
2218#endif /* MBEDTLS_MD_C */
2219 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002220 if( ! PSA_ALG_IS_MAC( alg ) )
2221 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002222 }
2223
2224 if( status != PSA_SUCCESS )
2225 memset( operation, 0, sizeof( *operation ) );
2226 return( status );
2227}
2228
Gilles Peskine01126fa2018-07-12 17:04:55 +02002229#if defined(MBEDTLS_MD_C)
2230static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2231{
Gilles Peskine3f108122018-12-07 18:14:53 +01002232 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002233 return( psa_hash_abort( &hmac->hash_ctx ) );
2234}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002235
2236static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2237{
2238 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2239 memset( hmac, 0, sizeof( *hmac ) );
2240}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002241#endif /* MBEDTLS_MD_C */
2242
Gilles Peskine8c9def32018-02-08 10:02:12 +01002243psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2244{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002245 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002246 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002247 /* The object has (apparently) been initialized but it is not
2248 * in use. It's ok to call abort on such an object, and there's
2249 * nothing to do. */
2250 return( PSA_SUCCESS );
2251 }
2252 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002253#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002254 if( operation->alg == PSA_ALG_CMAC )
2255 {
2256 mbedtls_cipher_free( &operation->ctx.cmac );
2257 }
2258 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002259#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002260#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002261 if( PSA_ALG_IS_HMAC( operation->alg ) )
2262 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002263 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002264 }
2265 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002266#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002267 {
2268 /* Sanity check (shouldn't happen: operation->alg should
2269 * always have been initialized to a valid value). */
2270 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002271 }
Moran Peker41deec42018-04-04 15:43:05 +03002272
Gilles Peskine8c9def32018-02-08 10:02:12 +01002273 operation->alg = 0;
2274 operation->key_set = 0;
2275 operation->iv_set = 0;
2276 operation->iv_required = 0;
2277 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002278 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002279
Gilles Peskine8c9def32018-02-08 10:02:12 +01002280 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002281
2282bad_state:
2283 /* If abort is called on an uninitialized object, we can't trust
2284 * anything. Wipe the object in case it contains confidential data.
2285 * This may result in a memory leak if a pointer gets overwritten,
2286 * but it's too late to do anything about this. */
2287 memset( operation, 0, sizeof( *operation ) );
2288 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002289}
2290
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002291#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002292static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002293 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002294 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002295 const mbedtls_cipher_info_t *cipher_info )
2296{
2297 int ret;
2298
2299 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002300
2301 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2302 if( ret != 0 )
2303 return( ret );
2304
2305 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2306 slot->data.raw.data,
2307 key_bits );
2308 return( ret );
2309}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002310#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002311
Gilles Peskine248051a2018-06-20 16:09:38 +02002312#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002313static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2314 const uint8_t *key,
2315 size_t key_length,
2316 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002317{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002318 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002319 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002320 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002321 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002322 psa_status_t status;
2323
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002324 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2325 * overflow below. This should never trigger if the hash algorithm
2326 * is implemented correctly. */
2327 /* The size checks against the ipad and opad buffers cannot be written
2328 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2329 * because that triggers -Wlogical-op on GCC 7.3. */
2330 if( block_size > sizeof( ipad ) )
2331 return( PSA_ERROR_NOT_SUPPORTED );
2332 if( block_size > sizeof( hmac->opad ) )
2333 return( PSA_ERROR_NOT_SUPPORTED );
2334 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002335 return( PSA_ERROR_NOT_SUPPORTED );
2336
Gilles Peskined223b522018-06-11 18:12:58 +02002337 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002338 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002339 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002340 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002341 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002342 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002343 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002344 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002345 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002346 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002347 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002348 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002349 }
Gilles Peskine96889972018-07-12 17:07:03 +02002350 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2351 * but it is permitted. It is common when HMAC is used in HKDF, for
2352 * example. Don't call `memcpy` in the 0-length because `key` could be
2353 * an invalid pointer which would make the behavior undefined. */
2354 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002355 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002356
Gilles Peskined223b522018-06-11 18:12:58 +02002357 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2358 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002359 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002360 ipad[i] ^= 0x36;
2361 memset( ipad + key_length, 0x36, block_size - key_length );
2362
2363 /* Copy the key material from ipad to opad, flipping the requisite bits,
2364 * and filling the rest of opad with the requisite constant. */
2365 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002366 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2367 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002368
Gilles Peskine01126fa2018-07-12 17:04:55 +02002369 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002370 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002371 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002372
Gilles Peskine01126fa2018-07-12 17:04:55 +02002373 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002374
2375cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002376 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002377
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002378 return( status );
2379}
Gilles Peskine248051a2018-06-20 16:09:38 +02002380#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002381
Gilles Peskine89167cb2018-07-08 20:12:23 +02002382static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002383 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002384 psa_algorithm_t alg,
2385 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002386{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002387 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002388 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002389 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002390 psa_key_usage_t usage =
2391 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002392 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002393 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002394
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002395 /* A context must be freshly initialized before it can be set up. */
2396 if( operation->alg != 0 )
2397 {
2398 return( PSA_ERROR_BAD_STATE );
2399 }
2400
Gilles Peskined911eb72018-08-14 15:18:45 +02002401 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002402 if( status != PSA_SUCCESS )
2403 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002404 if( is_sign )
2405 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002406
Gilles Peskinec5487a82018-12-03 18:08:14 +01002407 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002408 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002409 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002410 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002411
Gilles Peskine8c9def32018-02-08 10:02:12 +01002412#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002413 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002414 {
2415 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002416 mbedtls_cipher_info_from_psa( full_length_alg,
2417 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002418 int ret;
2419 if( cipher_info == NULL )
2420 {
2421 status = PSA_ERROR_NOT_SUPPORTED;
2422 goto exit;
2423 }
2424 operation->mac_size = cipher_info->block_size;
2425 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2426 status = mbedtls_to_psa_error( ret );
2427 }
2428 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002429#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002431 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002432 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002433 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002434 if( hash_alg == 0 )
2435 {
2436 status = PSA_ERROR_NOT_SUPPORTED;
2437 goto exit;
2438 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002439
2440 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2441 /* Sanity check. This shouldn't fail on a valid configuration. */
2442 if( operation->mac_size == 0 ||
2443 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2444 {
2445 status = PSA_ERROR_NOT_SUPPORTED;
2446 goto exit;
2447 }
2448
Gilles Peskine01126fa2018-07-12 17:04:55 +02002449 if( slot->type != PSA_KEY_TYPE_HMAC )
2450 {
2451 status = PSA_ERROR_INVALID_ARGUMENT;
2452 goto exit;
2453 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002454
Gilles Peskine01126fa2018-07-12 17:04:55 +02002455 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2456 slot->data.raw.data,
2457 slot->data.raw.bytes,
2458 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002459 }
2460 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002461#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002462 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002463 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002464 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002465 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002466
Gilles Peskined911eb72018-08-14 15:18:45 +02002467 if( truncated == 0 )
2468 {
2469 /* The "normal" case: untruncated algorithm. Nothing to do. */
2470 }
2471 else if( truncated < 4 )
2472 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002473 /* A very short MAC is too short for security since it can be
2474 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2475 * so we make this our minimum, even though 32 bits is still
2476 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002477 status = PSA_ERROR_NOT_SUPPORTED;
2478 }
2479 else if( truncated > operation->mac_size )
2480 {
2481 /* It's impossible to "truncate" to a larger length. */
2482 status = PSA_ERROR_INVALID_ARGUMENT;
2483 }
2484 else
2485 operation->mac_size = truncated;
2486
Gilles Peskinefbfac682018-07-08 20:51:54 +02002487exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002488 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002490 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002492 else
2493 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002494 operation->key_set = 1;
2495 }
2496 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497}
2498
Gilles Peskine89167cb2018-07-08 20:12:23 +02002499psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002500 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002501 psa_algorithm_t alg )
2502{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002503 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002504}
2505
2506psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002507 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002508 psa_algorithm_t alg )
2509{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002510 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002511}
2512
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2514 const uint8_t *input,
2515 size_t input_length )
2516{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002517 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002519 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002521 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002522 operation->has_input = 1;
2523
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002525 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002526 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002527 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2528 input, input_length );
2529 status = mbedtls_to_psa_error( ret );
2530 }
2531 else
2532#endif /* MBEDTLS_CMAC_C */
2533#if defined(MBEDTLS_MD_C)
2534 if( PSA_ALG_IS_HMAC( operation->alg ) )
2535 {
2536 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2537 input_length );
2538 }
2539 else
2540#endif /* MBEDTLS_MD_C */
2541 {
2542 /* This shouldn't happen if `operation` was initialized by
2543 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002544 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002545 }
2546
Gilles Peskinefbfac682018-07-08 20:51:54 +02002547 if( status != PSA_SUCCESS )
2548 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002549 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550}
2551
Gilles Peskine01126fa2018-07-12 17:04:55 +02002552#if defined(MBEDTLS_MD_C)
2553static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2554 uint8_t *mac,
2555 size_t mac_size )
2556{
2557 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2558 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2559 size_t hash_size = 0;
2560 size_t block_size = psa_get_hash_block_size( hash_alg );
2561 psa_status_t status;
2562
Gilles Peskine01126fa2018-07-12 17:04:55 +02002563 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2564 if( status != PSA_SUCCESS )
2565 return( status );
2566 /* From here on, tmp needs to be wiped. */
2567
2568 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2569 if( status != PSA_SUCCESS )
2570 goto exit;
2571
2572 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2573 if( status != PSA_SUCCESS )
2574 goto exit;
2575
2576 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2577 if( status != PSA_SUCCESS )
2578 goto exit;
2579
Gilles Peskined911eb72018-08-14 15:18:45 +02002580 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2581 if( status != PSA_SUCCESS )
2582 goto exit;
2583
2584 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002585
2586exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002587 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002588 return( status );
2589}
2590#endif /* MBEDTLS_MD_C */
2591
mohammad16036df908f2018-04-02 08:34:15 -07002592static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002593 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002594 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002596 if( ! operation->key_set )
2597 return( PSA_ERROR_BAD_STATE );
2598 if( operation->iv_required && ! operation->iv_set )
2599 return( PSA_ERROR_BAD_STATE );
2600
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601 if( mac_size < operation->mac_size )
2602 return( PSA_ERROR_BUFFER_TOO_SMALL );
2603
Gilles Peskine8c9def32018-02-08 10:02:12 +01002604#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002605 if( operation->alg == PSA_ALG_CMAC )
2606 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002607 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2608 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2609 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002610 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002611 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002612 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002614 else
2615#endif /* MBEDTLS_CMAC_C */
2616#if defined(MBEDTLS_MD_C)
2617 if( PSA_ALG_IS_HMAC( operation->alg ) )
2618 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002619 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002620 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002621 }
2622 else
2623#endif /* MBEDTLS_MD_C */
2624 {
2625 /* This shouldn't happen if `operation` was initialized by
2626 * a setup function. */
2627 return( PSA_ERROR_BAD_STATE );
2628 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002629}
2630
Gilles Peskineacd4be32018-07-08 19:56:25 +02002631psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2632 uint8_t *mac,
2633 size_t mac_size,
2634 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002635{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002636 psa_status_t status;
2637
Jaeden Amero252ef282019-02-15 14:05:35 +00002638 if( operation->alg == 0 )
2639 {
2640 return( PSA_ERROR_BAD_STATE );
2641 }
2642
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002643 /* Fill the output buffer with something that isn't a valid mac
2644 * (barring an attack on the mac and deliberately-crafted input),
2645 * in case the caller doesn't check the return status properly. */
2646 *mac_length = mac_size;
2647 /* If mac_size is 0 then mac may be NULL and then the
2648 * call to memset would have undefined behavior. */
2649 if( mac_size != 0 )
2650 memset( mac, '!', mac_size );
2651
Gilles Peskine89167cb2018-07-08 20:12:23 +02002652 if( ! operation->is_sign )
2653 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002654 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002655 }
mohammad16036df908f2018-04-02 08:34:15 -07002656
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002657 status = psa_mac_finish_internal( operation, mac, mac_size );
2658
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002659 if( status == PSA_SUCCESS )
2660 {
2661 status = psa_mac_abort( operation );
2662 if( status == PSA_SUCCESS )
2663 *mac_length = operation->mac_size;
2664 else
2665 memset( mac, '!', mac_size );
2666 }
2667 else
2668 psa_mac_abort( operation );
2669 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002670}
2671
Gilles Peskineacd4be32018-07-08 19:56:25 +02002672psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2673 const uint8_t *mac,
2674 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675{
Gilles Peskine828ed142018-06-18 23:25:51 +02002676 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002677 psa_status_t status;
2678
Jaeden Amero252ef282019-02-15 14:05:35 +00002679 if( operation->alg == 0 )
2680 {
2681 return( PSA_ERROR_BAD_STATE );
2682 }
2683
Gilles Peskine89167cb2018-07-08 20:12:23 +02002684 if( operation->is_sign )
2685 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002686 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002687 }
2688 if( operation->mac_size != mac_length )
2689 {
2690 status = PSA_ERROR_INVALID_SIGNATURE;
2691 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002692 }
mohammad16036df908f2018-04-02 08:34:15 -07002693
2694 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002695 actual_mac, sizeof( actual_mac ) );
2696
2697 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2698 status = PSA_ERROR_INVALID_SIGNATURE;
2699
2700cleanup:
2701 if( status == PSA_SUCCESS )
2702 status = psa_mac_abort( operation );
2703 else
2704 psa_mac_abort( operation );
2705
Gilles Peskine3f108122018-12-07 18:14:53 +01002706 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002707
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002708 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002709}
2710
2711
Gilles Peskine20035e32018-02-03 22:44:14 +01002712
Gilles Peskine20035e32018-02-03 22:44:14 +01002713/****************************************************************/
2714/* Asymmetric cryptography */
2715/****************************************************************/
2716
Gilles Peskine2b450e32018-06-27 15:42:46 +02002717#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002718/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002719 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002720static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2721 size_t hash_length,
2722 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002723{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002724 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002725 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002726 *md_alg = mbedtls_md_get_type( md_info );
2727
2728 /* The Mbed TLS RSA module uses an unsigned int for hash length
2729 * parameters. Validate that it fits so that we don't risk an
2730 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002731#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002732 if( hash_length > UINT_MAX )
2733 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002734#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002735
2736#if defined(MBEDTLS_PKCS1_V15)
2737 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2738 * must be correct. */
2739 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2740 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002741 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002742 if( md_info == NULL )
2743 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002744 if( mbedtls_md_get_size( md_info ) != hash_length )
2745 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002746 }
2747#endif /* MBEDTLS_PKCS1_V15 */
2748
2749#if defined(MBEDTLS_PKCS1_V21)
2750 /* PSS requires a hash internally. */
2751 if( PSA_ALG_IS_RSA_PSS( alg ) )
2752 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002753 if( md_info == NULL )
2754 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002755 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002756#endif /* MBEDTLS_PKCS1_V21 */
2757
Gilles Peskine61b91d42018-06-08 16:09:36 +02002758 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002759}
2760
Gilles Peskine2b450e32018-06-27 15:42:46 +02002761static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2762 psa_algorithm_t alg,
2763 const uint8_t *hash,
2764 size_t hash_length,
2765 uint8_t *signature,
2766 size_t signature_size,
2767 size_t *signature_length )
2768{
2769 psa_status_t status;
2770 int ret;
2771 mbedtls_md_type_t md_alg;
2772
2773 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2774 if( status != PSA_SUCCESS )
2775 return( status );
2776
Gilles Peskine630a18a2018-06-29 17:49:35 +02002777 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002778 return( PSA_ERROR_BUFFER_TOO_SMALL );
2779
2780#if defined(MBEDTLS_PKCS1_V15)
2781 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2782 {
2783 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2784 MBEDTLS_MD_NONE );
2785 ret = mbedtls_rsa_pkcs1_sign( rsa,
2786 mbedtls_ctr_drbg_random,
2787 &global_data.ctr_drbg,
2788 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002789 md_alg,
2790 (unsigned int) hash_length,
2791 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002792 signature );
2793 }
2794 else
2795#endif /* MBEDTLS_PKCS1_V15 */
2796#if defined(MBEDTLS_PKCS1_V21)
2797 if( PSA_ALG_IS_RSA_PSS( alg ) )
2798 {
2799 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2800 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2801 mbedtls_ctr_drbg_random,
2802 &global_data.ctr_drbg,
2803 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002804 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002805 (unsigned int) hash_length,
2806 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002807 signature );
2808 }
2809 else
2810#endif /* MBEDTLS_PKCS1_V21 */
2811 {
2812 return( PSA_ERROR_INVALID_ARGUMENT );
2813 }
2814
2815 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002816 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002817 return( mbedtls_to_psa_error( ret ) );
2818}
2819
2820static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2821 psa_algorithm_t alg,
2822 const uint8_t *hash,
2823 size_t hash_length,
2824 const uint8_t *signature,
2825 size_t signature_length )
2826{
2827 psa_status_t status;
2828 int ret;
2829 mbedtls_md_type_t md_alg;
2830
2831 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2832 if( status != PSA_SUCCESS )
2833 return( status );
2834
Gilles Peskine630a18a2018-06-29 17:49:35 +02002835 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002836 return( PSA_ERROR_BUFFER_TOO_SMALL );
2837
2838#if defined(MBEDTLS_PKCS1_V15)
2839 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2840 {
2841 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2842 MBEDTLS_MD_NONE );
2843 ret = mbedtls_rsa_pkcs1_verify( rsa,
2844 mbedtls_ctr_drbg_random,
2845 &global_data.ctr_drbg,
2846 MBEDTLS_RSA_PUBLIC,
2847 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002848 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002849 hash,
2850 signature );
2851 }
2852 else
2853#endif /* MBEDTLS_PKCS1_V15 */
2854#if defined(MBEDTLS_PKCS1_V21)
2855 if( PSA_ALG_IS_RSA_PSS( alg ) )
2856 {
2857 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2858 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2859 mbedtls_ctr_drbg_random,
2860 &global_data.ctr_drbg,
2861 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002862 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002863 (unsigned int) hash_length,
2864 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002865 signature );
2866 }
2867 else
2868#endif /* MBEDTLS_PKCS1_V21 */
2869 {
2870 return( PSA_ERROR_INVALID_ARGUMENT );
2871 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002872
2873 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2874 * the rest of the signature is invalid". This has little use in
2875 * practice and PSA doesn't report this distinction. */
2876 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2877 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002878 return( mbedtls_to_psa_error( ret ) );
2879}
2880#endif /* MBEDTLS_RSA_C */
2881
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002882#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002883/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2884 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2885 * (even though these functions don't modify it). */
2886static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2887 psa_algorithm_t alg,
2888 const uint8_t *hash,
2889 size_t hash_length,
2890 uint8_t *signature,
2891 size_t signature_size,
2892 size_t *signature_length )
2893{
2894 int ret;
2895 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002896 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002897 mbedtls_mpi_init( &r );
2898 mbedtls_mpi_init( &s );
2899
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002900 if( signature_size < 2 * curve_bytes )
2901 {
2902 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2903 goto cleanup;
2904 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002905
Gilles Peskinea05219c2018-11-16 16:02:56 +01002906#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002907 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2908 {
2909 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2910 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2911 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2912 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2913 hash, hash_length,
2914 md_alg ) );
2915 }
2916 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002917#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002918 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002919 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002920 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2921 hash, hash_length,
2922 mbedtls_ctr_drbg_random,
2923 &global_data.ctr_drbg ) );
2924 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002925
2926 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2927 signature,
2928 curve_bytes ) );
2929 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2930 signature + curve_bytes,
2931 curve_bytes ) );
2932
2933cleanup:
2934 mbedtls_mpi_free( &r );
2935 mbedtls_mpi_free( &s );
2936 if( ret == 0 )
2937 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002938 return( mbedtls_to_psa_error( ret ) );
2939}
2940
2941static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2942 const uint8_t *hash,
2943 size_t hash_length,
2944 const uint8_t *signature,
2945 size_t signature_length )
2946{
2947 int ret;
2948 mbedtls_mpi r, s;
2949 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2950 mbedtls_mpi_init( &r );
2951 mbedtls_mpi_init( &s );
2952
2953 if( signature_length != 2 * curve_bytes )
2954 return( PSA_ERROR_INVALID_SIGNATURE );
2955
2956 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2957 signature,
2958 curve_bytes ) );
2959 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2960 signature + curve_bytes,
2961 curve_bytes ) );
2962
2963 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2964 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002965
2966cleanup:
2967 mbedtls_mpi_free( &r );
2968 mbedtls_mpi_free( &s );
2969 return( mbedtls_to_psa_error( ret ) );
2970}
2971#endif /* MBEDTLS_ECDSA_C */
2972
Gilles Peskinec5487a82018-12-03 18:08:14 +01002973psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002974 psa_algorithm_t alg,
2975 const uint8_t *hash,
2976 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002977 uint8_t *signature,
2978 size_t signature_size,
2979 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002980{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002981 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002982 psa_status_t status;
2983
2984 *signature_length = signature_size;
2985
Gilles Peskinec5487a82018-12-03 18:08:14 +01002986 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002987 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002988 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002989 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002990 {
2991 status = PSA_ERROR_INVALID_ARGUMENT;
2992 goto exit;
2993 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002994
Gilles Peskine20035e32018-02-03 22:44:14 +01002995#if defined(MBEDTLS_RSA_C)
2996 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2997 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002998 status = psa_rsa_sign( slot->data.rsa,
2999 alg,
3000 hash, hash_length,
3001 signature, signature_size,
3002 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003003 }
3004 else
3005#endif /* defined(MBEDTLS_RSA_C) */
3006#if defined(MBEDTLS_ECP_C)
3007 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3008 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003009#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003010 if(
3011#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3012 PSA_ALG_IS_ECDSA( alg )
3013#else
3014 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3015#endif
3016 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003017 status = psa_ecdsa_sign( slot->data.ecp,
3018 alg,
3019 hash, hash_length,
3020 signature, signature_size,
3021 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003022 else
3023#endif /* defined(MBEDTLS_ECDSA_C) */
3024 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003025 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003026 }
itayzafrir5c753392018-05-08 11:18:38 +03003027 }
3028 else
3029#endif /* defined(MBEDTLS_ECP_C) */
3030 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003031 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003032 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003033
3034exit:
3035 /* Fill the unused part of the output buffer (the whole buffer on error,
3036 * the trailing part on success) with something that isn't a valid mac
3037 * (barring an attack on the mac and deliberately-crafted input),
3038 * in case the caller doesn't check the return status properly. */
3039 if( status == PSA_SUCCESS )
3040 memset( signature + *signature_length, '!',
3041 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003042 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003043 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003044 /* If signature_size is 0 then we have nothing to do. We must not call
3045 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003046 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003047}
3048
Gilles Peskinec5487a82018-12-03 18:08:14 +01003049psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003050 psa_algorithm_t alg,
3051 const uint8_t *hash,
3052 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003053 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003054 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003055{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003056 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003057 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003058
Gilles Peskinec5487a82018-12-03 18:08:14 +01003059 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003060 if( status != PSA_SUCCESS )
3061 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003062
Gilles Peskine61b91d42018-06-08 16:09:36 +02003063#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003064 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003065 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003066 return( psa_rsa_verify( slot->data.rsa,
3067 alg,
3068 hash, hash_length,
3069 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003070 }
3071 else
3072#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003073#if defined(MBEDTLS_ECP_C)
3074 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3075 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003076#if defined(MBEDTLS_ECDSA_C)
3077 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003078 return( psa_ecdsa_verify( slot->data.ecp,
3079 hash, hash_length,
3080 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003081 else
3082#endif /* defined(MBEDTLS_ECDSA_C) */
3083 {
3084 return( PSA_ERROR_INVALID_ARGUMENT );
3085 }
itayzafrir5c753392018-05-08 11:18:38 +03003086 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003087 else
3088#endif /* defined(MBEDTLS_ECP_C) */
3089 {
3090 return( PSA_ERROR_NOT_SUPPORTED );
3091 }
3092}
3093
Gilles Peskine072ac562018-06-30 00:21:29 +02003094#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3095static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3096 mbedtls_rsa_context *rsa )
3097{
3098 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3099 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3100 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3101 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3102}
3103#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3104
Gilles Peskinec5487a82018-12-03 18:08:14 +01003105psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003106 psa_algorithm_t alg,
3107 const uint8_t *input,
3108 size_t input_length,
3109 const uint8_t *salt,
3110 size_t salt_length,
3111 uint8_t *output,
3112 size_t output_size,
3113 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003114{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003115 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003116 psa_status_t status;
3117
Darryl Green5cc689a2018-07-24 15:34:10 +01003118 (void) input;
3119 (void) input_length;
3120 (void) salt;
3121 (void) output;
3122 (void) output_size;
3123
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003124 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003126 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3127 return( PSA_ERROR_INVALID_ARGUMENT );
3128
Gilles Peskinec5487a82018-12-03 18:08:14 +01003129 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003130 if( status != PSA_SUCCESS )
3131 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003132 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
3133 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003134 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003135
3136#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003137 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003138 {
3139 mbedtls_rsa_context *rsa = slot->data.rsa;
3140 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003141 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003142 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003143#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003144 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003145 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003146 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3147 mbedtls_ctr_drbg_random,
3148 &global_data.ctr_drbg,
3149 MBEDTLS_RSA_PUBLIC,
3150 input_length,
3151 input,
3152 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003153 }
3154 else
3155#endif /* MBEDTLS_PKCS1_V15 */
3156#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003157 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003158 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003159 psa_rsa_oaep_set_padding_mode( alg, rsa );
3160 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3161 mbedtls_ctr_drbg_random,
3162 &global_data.ctr_drbg,
3163 MBEDTLS_RSA_PUBLIC,
3164 salt, salt_length,
3165 input_length,
3166 input,
3167 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003168 }
3169 else
3170#endif /* MBEDTLS_PKCS1_V21 */
3171 {
3172 return( PSA_ERROR_INVALID_ARGUMENT );
3173 }
3174 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003175 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003176 return( mbedtls_to_psa_error( ret ) );
3177 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003179#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003180 {
3181 return( PSA_ERROR_NOT_SUPPORTED );
3182 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003183}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003184
Gilles Peskinec5487a82018-12-03 18:08:14 +01003185psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003186 psa_algorithm_t alg,
3187 const uint8_t *input,
3188 size_t input_length,
3189 const uint8_t *salt,
3190 size_t salt_length,
3191 uint8_t *output,
3192 size_t output_size,
3193 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003195 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003196 psa_status_t status;
3197
Darryl Green5cc689a2018-07-24 15:34:10 +01003198 (void) input;
3199 (void) input_length;
3200 (void) salt;
3201 (void) output;
3202 (void) output_size;
3203
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003204 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003205
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003206 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3207 return( PSA_ERROR_INVALID_ARGUMENT );
3208
Gilles Peskinec5487a82018-12-03 18:08:14 +01003209 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003210 if( status != PSA_SUCCESS )
3211 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
3213 return( PSA_ERROR_INVALID_ARGUMENT );
3214
3215#if defined(MBEDTLS_RSA_C)
3216 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
3217 {
3218 mbedtls_rsa_context *rsa = slot->data.rsa;
3219 int ret;
3220
Gilles Peskine630a18a2018-06-29 17:49:35 +02003221 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003222 return( PSA_ERROR_INVALID_ARGUMENT );
3223
3224#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003225 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003227 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3228 mbedtls_ctr_drbg_random,
3229 &global_data.ctr_drbg,
3230 MBEDTLS_RSA_PRIVATE,
3231 output_length,
3232 input,
3233 output,
3234 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003235 }
3236 else
3237#endif /* MBEDTLS_PKCS1_V15 */
3238#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003239 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003240 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003241 psa_rsa_oaep_set_padding_mode( alg, rsa );
3242 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3243 mbedtls_ctr_drbg_random,
3244 &global_data.ctr_drbg,
3245 MBEDTLS_RSA_PRIVATE,
3246 salt, salt_length,
3247 output_length,
3248 input,
3249 output,
3250 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003251 }
3252 else
3253#endif /* MBEDTLS_PKCS1_V21 */
3254 {
3255 return( PSA_ERROR_INVALID_ARGUMENT );
3256 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003257
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003258 return( mbedtls_to_psa_error( ret ) );
3259 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003260 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003261#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262 {
3263 return( PSA_ERROR_NOT_SUPPORTED );
3264 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003265}
Gilles Peskine20035e32018-02-03 22:44:14 +01003266
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003267
3268
mohammad1603503973b2018-03-12 15:59:30 +02003269/****************************************************************/
3270/* Symmetric cryptography */
3271/****************************************************************/
3272
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003273/* Initialize the cipher operation structure. Once this function has been
3274 * called, psa_cipher_abort can run and will do the right thing. */
3275static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3276 psa_algorithm_t alg )
3277{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003278 if( ! PSA_ALG_IS_CIPHER( alg ) )
3279 {
3280 memset( operation, 0, sizeof( *operation ) );
3281 return( PSA_ERROR_INVALID_ARGUMENT );
3282 }
3283
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003284 operation->alg = alg;
3285 operation->key_set = 0;
3286 operation->iv_set = 0;
3287 operation->iv_required = 1;
3288 operation->iv_size = 0;
3289 operation->block_size = 0;
3290 mbedtls_cipher_init( &operation->ctx.cipher );
3291 return( PSA_SUCCESS );
3292}
3293
Gilles Peskinee553c652018-06-04 16:22:46 +02003294static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003295 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003296 psa_algorithm_t alg,
3297 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003298{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003299 int ret = 0;
3300 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003301 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003302 size_t key_bits;
3303 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003304 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3305 PSA_KEY_USAGE_ENCRYPT :
3306 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003307
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003308 /* A context must be freshly initialized before it can be set up. */
3309 if( operation->alg != 0 )
3310 {
3311 return( PSA_ERROR_BAD_STATE );
3312 }
3313
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003314 status = psa_cipher_init( operation, alg );
3315 if( status != PSA_SUCCESS )
3316 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003317
Gilles Peskinec5487a82018-12-03 18:08:14 +01003318 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003319 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003320 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003321 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003322
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003323 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003324 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003325 {
3326 status = PSA_ERROR_NOT_SUPPORTED;
3327 goto exit;
3328 }
mohammad1603503973b2018-03-12 15:59:30 +02003329
mohammad1603503973b2018-03-12 15:59:30 +02003330 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003331 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003332 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003333
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003334#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003335 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003336 {
3337 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3338 unsigned char keys[24];
3339 memcpy( keys, slot->data.raw.data, 16 );
3340 memcpy( keys + 16, slot->data.raw.data, 8 );
3341 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3342 keys,
3343 192, cipher_operation );
3344 }
3345 else
3346#endif
3347 {
3348 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3349 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003350 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003351 }
Moran Peker41deec42018-04-04 15:43:05 +03003352 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003353 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003354
mohammad16038481e742018-03-18 13:57:31 +02003355#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003356 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003357 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003358 case PSA_ALG_CBC_NO_PADDING:
3359 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3360 MBEDTLS_PADDING_NONE );
3361 break;
3362 case PSA_ALG_CBC_PKCS7:
3363 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3364 MBEDTLS_PADDING_PKCS7 );
3365 break;
3366 default:
3367 /* The algorithm doesn't involve padding. */
3368 ret = 0;
3369 break;
3370 }
3371 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003372 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003373#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3374
mohammad1603503973b2018-03-12 15:59:30 +02003375 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003376 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3377 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3378 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003379 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003380 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003381 }
mohammad1603503973b2018-03-12 15:59:30 +02003382
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003383exit:
3384 if( status == 0 )
3385 status = mbedtls_to_psa_error( ret );
3386 if( status != 0 )
3387 psa_cipher_abort( operation );
3388 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003389}
3390
Gilles Peskinefe119512018-07-08 21:39:34 +02003391psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003392 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003393 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003394{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003395 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003396}
3397
Gilles Peskinefe119512018-07-08 21:39:34 +02003398psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003399 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003400 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003401{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003402 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003403}
3404
Gilles Peskinefe119512018-07-08 21:39:34 +02003405psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3406 unsigned char *iv,
3407 size_t iv_size,
3408 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003409{
itayzafrir534bd7c2018-08-02 13:56:32 +03003410 psa_status_t status;
3411 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003412 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003413 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003414 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003415 }
Moran Peker41deec42018-04-04 15:43:05 +03003416 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003417 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003418 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003419 goto exit;
3420 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003421 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3422 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003423 if( ret != 0 )
3424 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003425 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003426 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003427 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003428
mohammad16038481e742018-03-18 13:57:31 +02003429 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003430 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003431
Moran Peker395db872018-05-31 14:07:14 +03003432exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003433 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003434 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003435 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003436}
3437
Gilles Peskinefe119512018-07-08 21:39:34 +02003438psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3439 const unsigned char *iv,
3440 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003441{
itayzafrir534bd7c2018-08-02 13:56:32 +03003442 psa_status_t status;
3443 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003444 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003445 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003446 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003447 }
Moran Pekera28258c2018-05-29 16:25:04 +03003448 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003449 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003450 status = PSA_ERROR_INVALID_ARGUMENT;
3451 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003452 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003453 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3454 status = mbedtls_to_psa_error( ret );
3455exit:
3456 if( status == PSA_SUCCESS )
3457 operation->iv_set = 1;
3458 else
mohammad1603503973b2018-03-12 15:59:30 +02003459 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003460 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003461}
3462
Gilles Peskinee553c652018-06-04 16:22:46 +02003463psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3464 const uint8_t *input,
3465 size_t input_length,
3466 unsigned char *output,
3467 size_t output_size,
3468 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003469{
itayzafrir534bd7c2018-08-02 13:56:32 +03003470 psa_status_t status;
3471 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003472 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003473
3474 if( operation->alg == 0 )
3475 {
3476 return( PSA_ERROR_BAD_STATE );
3477 }
3478
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003479 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003480 {
3481 /* Take the unprocessed partial block left over from previous
3482 * update calls, if any, plus the input to this call. Remove
3483 * the last partial block, if any. You get the data that will be
3484 * output in this call. */
3485 expected_output_size =
3486 ( operation->ctx.cipher.unprocessed_len + input_length )
3487 / operation->block_size * operation->block_size;
3488 }
3489 else
3490 {
3491 expected_output_size = input_length;
3492 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003493
Gilles Peskine89d789c2018-06-04 17:17:16 +02003494 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003495 {
3496 status = PSA_ERROR_BUFFER_TOO_SMALL;
3497 goto exit;
3498 }
mohammad160382759612018-03-12 18:16:40 +02003499
mohammad1603503973b2018-03-12 15:59:30 +02003500 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003501 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003502 status = mbedtls_to_psa_error( ret );
3503exit:
3504 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003505 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003506 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003507}
3508
Gilles Peskinee553c652018-06-04 16:22:46 +02003509psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3510 uint8_t *output,
3511 size_t output_size,
3512 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003513{
David Saadab4ecc272019-02-14 13:48:10 +02003514 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003515 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003516 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003517
mohammad1603503973b2018-03-12 15:59:30 +02003518 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003519 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003520 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003521 }
3522 if( operation->iv_required && ! operation->iv_set )
3523 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003524 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003525 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003526
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003527 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003528 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3529 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003530 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003531 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003532 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003533 }
3534
Janos Follath315b51c2018-07-09 16:04:51 +01003535 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3536 temp_output_buffer,
3537 output_length );
3538 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003539 {
Janos Follath315b51c2018-07-09 16:04:51 +01003540 status = mbedtls_to_psa_error( cipher_ret );
3541 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003542 }
Janos Follath315b51c2018-07-09 16:04:51 +01003543
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003544 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003545 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003546 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003547 memcpy( output, temp_output_buffer, *output_length );
3548 else
3549 {
Janos Follath315b51c2018-07-09 16:04:51 +01003550 status = PSA_ERROR_BUFFER_TOO_SMALL;
3551 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003552 }
mohammad1603503973b2018-03-12 15:59:30 +02003553
Gilles Peskine3f108122018-12-07 18:14:53 +01003554 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003555 status = psa_cipher_abort( operation );
3556
3557 return( status );
3558
3559error:
3560
3561 *output_length = 0;
3562
Gilles Peskine3f108122018-12-07 18:14:53 +01003563 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003564 (void) psa_cipher_abort( operation );
3565
3566 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003567}
3568
Gilles Peskinee553c652018-06-04 16:22:46 +02003569psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3570{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003571 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003572 {
3573 /* The object has (apparently) been initialized but it is not
3574 * in use. It's ok to call abort on such an object, and there's
3575 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003576 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003577 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003578
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003579 /* Sanity check (shouldn't happen: operation->alg should
3580 * always have been initialized to a valid value). */
3581 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3582 return( PSA_ERROR_BAD_STATE );
3583
mohammad1603503973b2018-03-12 15:59:30 +02003584 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003585
Moran Peker41deec42018-04-04 15:43:05 +03003586 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003587 operation->key_set = 0;
3588 operation->iv_set = 0;
3589 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003590 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003591 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003592
Moran Peker395db872018-05-31 14:07:14 +03003593 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003594}
3595
Gilles Peskinea0655c32018-04-30 17:06:50 +02003596
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003597
mohammad16038cc1cee2018-03-28 01:21:33 +03003598/****************************************************************/
3599/* Key Policy */
3600/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003601
mohammad160327010052018-07-03 13:16:15 +03003602#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003603void psa_key_policy_set_usage( psa_key_policy_t *policy,
3604 psa_key_usage_t usage,
3605 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003606{
mohammad16034eed7572018-03-28 05:14:59 -07003607 policy->usage = usage;
3608 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003609}
3610
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003611psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003612{
mohammad16036df908f2018-04-02 08:34:15 -07003613 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003614}
3615
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003616psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003617{
mohammad16036df908f2018-04-02 08:34:15 -07003618 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003619}
mohammad160327010052018-07-03 13:16:15 +03003620#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003621
Gilles Peskinec5487a82018-12-03 18:08:14 +01003622psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003623 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003624{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003625 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003626 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003627
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003628 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003629 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003630
Gilles Peskinec5487a82018-12-03 18:08:14 +01003631 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003632 if( status != PSA_SUCCESS )
3633 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003634
Gilles Peskine4747d192019-04-17 15:05:45 +02003635 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003636}
3637
Gilles Peskinec5487a82018-12-03 18:08:14 +01003638psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003639 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003640{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003641 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003642 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003643
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003644 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003645 return( PSA_ERROR_INVALID_ARGUMENT );
3646
Gilles Peskinec5487a82018-12-03 18:08:14 +01003647 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003648 if( status != PSA_SUCCESS )
3649 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003650
mohammad16036df908f2018-04-02 08:34:15 -07003651 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003652
3653 return( PSA_SUCCESS );
3654}
Gilles Peskine20035e32018-02-03 22:44:14 +01003655
Gilles Peskinea0655c32018-04-30 17:06:50 +02003656
3657
mohammad1603804cd712018-03-20 22:44:08 +02003658/****************************************************************/
3659/* Key Lifetime */
3660/****************************************************************/
3661
Gilles Peskine87a5e562019-04-17 12:28:25 +02003662psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003663 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003664{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003665 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003666 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003667
Gilles Peskinec5487a82018-12-03 18:08:14 +01003668 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003669 if( status != PSA_SUCCESS )
3670 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003671
mohammad1603804cd712018-03-20 22:44:08 +02003672 *lifetime = slot->lifetime;
3673
3674 return( PSA_SUCCESS );
3675}
3676
Gilles Peskine20035e32018-02-03 22:44:14 +01003677
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003678
mohammad16035955c982018-04-26 00:53:03 +03003679/****************************************************************/
3680/* AEAD */
3681/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003682
Gilles Peskineedf9a652018-08-17 18:11:56 +02003683typedef struct
3684{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003685 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003686 const mbedtls_cipher_info_t *cipher_info;
3687 union
3688 {
3689#if defined(MBEDTLS_CCM_C)
3690 mbedtls_ccm_context ccm;
3691#endif /* MBEDTLS_CCM_C */
3692#if defined(MBEDTLS_GCM_C)
3693 mbedtls_gcm_context gcm;
3694#endif /* MBEDTLS_GCM_C */
3695 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003696 psa_algorithm_t core_alg;
3697 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003698 uint8_t tag_length;
3699} aead_operation_t;
3700
Gilles Peskine30a9e412019-01-14 18:36:12 +01003701static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003702{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003703 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003704 {
3705#if defined(MBEDTLS_CCM_C)
3706 case PSA_ALG_CCM:
3707 mbedtls_ccm_free( &operation->ctx.ccm );
3708 break;
3709#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003710#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003711 case PSA_ALG_GCM:
3712 mbedtls_gcm_free( &operation->ctx.gcm );
3713 break;
3714#endif /* MBEDTLS_GCM_C */
3715 }
3716}
3717
3718static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003719 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003720 psa_key_usage_t usage,
3721 psa_algorithm_t alg )
3722{
3723 psa_status_t status;
3724 size_t key_bits;
3725 mbedtls_cipher_id_t cipher_id;
3726
Gilles Peskinec5487a82018-12-03 18:08:14 +01003727 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003728 if( status != PSA_SUCCESS )
3729 return( status );
3730
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003731 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003732
3733 operation->cipher_info =
3734 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3735 &cipher_id );
3736 if( operation->cipher_info == NULL )
3737 return( PSA_ERROR_NOT_SUPPORTED );
3738
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003739 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003740 {
3741#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003742 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3743 operation->core_alg = PSA_ALG_CCM;
3744 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003745 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3746 return( PSA_ERROR_INVALID_ARGUMENT );
3747 mbedtls_ccm_init( &operation->ctx.ccm );
3748 status = mbedtls_to_psa_error(
3749 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3750 operation->slot->data.raw.data,
3751 (unsigned int) key_bits ) );
3752 if( status != 0 )
3753 goto cleanup;
3754 break;
3755#endif /* MBEDTLS_CCM_C */
3756
3757#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003758 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3759 operation->core_alg = PSA_ALG_GCM;
3760 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003761 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3762 return( PSA_ERROR_INVALID_ARGUMENT );
3763 mbedtls_gcm_init( &operation->ctx.gcm );
3764 status = mbedtls_to_psa_error(
3765 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3766 operation->slot->data.raw.data,
3767 (unsigned int) key_bits ) );
3768 break;
3769#endif /* MBEDTLS_GCM_C */
3770
3771 default:
3772 return( PSA_ERROR_NOT_SUPPORTED );
3773 }
3774
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003775 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3776 {
3777 status = PSA_ERROR_INVALID_ARGUMENT;
3778 goto cleanup;
3779 }
3780 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3781 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3782 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3783 * In both cases, mbedtls_xxx will validate the tag length below. */
3784
Gilles Peskineedf9a652018-08-17 18:11:56 +02003785 return( PSA_SUCCESS );
3786
3787cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003788 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003789 return( status );
3790}
3791
Gilles Peskinec5487a82018-12-03 18:08:14 +01003792psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003793 psa_algorithm_t alg,
3794 const uint8_t *nonce,
3795 size_t nonce_length,
3796 const uint8_t *additional_data,
3797 size_t additional_data_length,
3798 const uint8_t *plaintext,
3799 size_t plaintext_length,
3800 uint8_t *ciphertext,
3801 size_t ciphertext_size,
3802 size_t *ciphertext_length )
3803{
mohammad16035955c982018-04-26 00:53:03 +03003804 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003805 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003806 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003807
mohammad1603f08a5502018-06-03 15:05:47 +03003808 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003809
Gilles Peskinec5487a82018-12-03 18:08:14 +01003810 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003811 if( status != PSA_SUCCESS )
3812 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003813
Gilles Peskineedf9a652018-08-17 18:11:56 +02003814 /* For all currently supported modes, the tag is at the end of the
3815 * ciphertext. */
3816 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3817 {
3818 status = PSA_ERROR_BUFFER_TOO_SMALL;
3819 goto exit;
3820 }
3821 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003822
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003823#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003824 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003825 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003826 status = mbedtls_to_psa_error(
3827 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3828 MBEDTLS_GCM_ENCRYPT,
3829 plaintext_length,
3830 nonce, nonce_length,
3831 additional_data, additional_data_length,
3832 plaintext, ciphertext,
3833 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003834 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003835 else
3836#endif /* MBEDTLS_GCM_C */
3837#if defined(MBEDTLS_CCM_C)
3838 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003839 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003840 status = mbedtls_to_psa_error(
3841 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3842 plaintext_length,
3843 nonce, nonce_length,
3844 additional_data,
3845 additional_data_length,
3846 plaintext, ciphertext,
3847 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003848 }
mohammad16035c8845f2018-05-09 05:40:09 -07003849 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003850#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003851 {
mohammad1603554faad2018-06-03 15:07:38 +03003852 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003853 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003854
Gilles Peskineedf9a652018-08-17 18:11:56 +02003855 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3856 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003857
Gilles Peskineedf9a652018-08-17 18:11:56 +02003858exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003859 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003860 if( status == PSA_SUCCESS )
3861 *ciphertext_length = plaintext_length + operation.tag_length;
3862 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003863}
3864
Gilles Peskineee652a32018-06-01 19:23:52 +02003865/* Locate the tag in a ciphertext buffer containing the encrypted data
3866 * followed by the tag. Return the length of the part preceding the tag in
3867 * *plaintext_length. This is the size of the plaintext in modes where
3868 * the encrypted data has the same size as the plaintext, such as
3869 * CCM and GCM. */
3870static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3871 const uint8_t *ciphertext,
3872 size_t ciphertext_length,
3873 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003874 const uint8_t **p_tag )
3875{
3876 size_t payload_length;
3877 if( tag_length > ciphertext_length )
3878 return( PSA_ERROR_INVALID_ARGUMENT );
3879 payload_length = ciphertext_length - tag_length;
3880 if( payload_length > plaintext_size )
3881 return( PSA_ERROR_BUFFER_TOO_SMALL );
3882 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003883 return( PSA_SUCCESS );
3884}
3885
Gilles Peskinec5487a82018-12-03 18:08:14 +01003886psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003887 psa_algorithm_t alg,
3888 const uint8_t *nonce,
3889 size_t nonce_length,
3890 const uint8_t *additional_data,
3891 size_t additional_data_length,
3892 const uint8_t *ciphertext,
3893 size_t ciphertext_length,
3894 uint8_t *plaintext,
3895 size_t plaintext_size,
3896 size_t *plaintext_length )
3897{
mohammad16035955c982018-04-26 00:53:03 +03003898 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003899 aead_operation_t operation;
3900 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003901
Gilles Peskineee652a32018-06-01 19:23:52 +02003902 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003903
Gilles Peskinec5487a82018-12-03 18:08:14 +01003904 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003905 if( status != PSA_SUCCESS )
3906 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003907
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003908#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003909 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003910 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003911 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003912 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003913 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003914 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003915 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003916
Gilles Peskineedf9a652018-08-17 18:11:56 +02003917 status = mbedtls_to_psa_error(
3918 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3919 ciphertext_length - operation.tag_length,
3920 nonce, nonce_length,
3921 additional_data,
3922 additional_data_length,
3923 tag, operation.tag_length,
3924 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003925 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003926 else
3927#endif /* MBEDTLS_GCM_C */
3928#if defined(MBEDTLS_CCM_C)
3929 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003930 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003931 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003932 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003933 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003934 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003935 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003936
Gilles Peskineedf9a652018-08-17 18:11:56 +02003937 status = mbedtls_to_psa_error(
3938 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3939 ciphertext_length - operation.tag_length,
3940 nonce, nonce_length,
3941 additional_data,
3942 additional_data_length,
3943 ciphertext, plaintext,
3944 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003945 }
mohammad160339574652018-06-01 04:39:53 -07003946 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003947#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003948 {
mohammad1603554faad2018-06-03 15:07:38 +03003949 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003950 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003951
Gilles Peskineedf9a652018-08-17 18:11:56 +02003952 if( status != PSA_SUCCESS && plaintext_size != 0 )
3953 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003954
Gilles Peskineedf9a652018-08-17 18:11:56 +02003955exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003956 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003957 if( status == PSA_SUCCESS )
3958 *plaintext_length = ciphertext_length - operation.tag_length;
3959 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003960}
3961
Gilles Peskinea0655c32018-04-30 17:06:50 +02003962
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003963
Gilles Peskine20035e32018-02-03 22:44:14 +01003964/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003965/* Generators */
3966/****************************************************************/
3967
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003968#define HKDF_STATE_INIT 0 /* no input yet */
3969#define HKDF_STATE_STARTED 1 /* got salt */
3970#define HKDF_STATE_KEYED 2 /* got key */
3971#define HKDF_STATE_OUTPUT 3 /* output started */
3972
Gilles Peskine969c5d62019-01-16 15:53:06 +01003973static psa_algorithm_t psa_generator_get_kdf_alg(
3974 const psa_crypto_generator_t *generator )
3975{
3976 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3977 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3978 else
3979 return( generator->alg );
3980}
3981
3982
Gilles Peskineeab56e42018-07-12 17:12:33 +02003983psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3984{
3985 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003986 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3987 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003988 {
3989 /* The object has (apparently) been initialized but it is not
3990 * in use. It's ok to call abort on such an object, and there's
3991 * nothing to do. */
3992 }
3993 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003994 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003995 {
3996 if( generator->ctx.buffer.data != NULL )
3997 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003998 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003999 generator->ctx.buffer.size );
4000 mbedtls_free( generator->ctx.buffer.data );
4001 }
4002 }
4003 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004004#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004005 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004006 {
4007 mbedtls_free( generator->ctx.hkdf.info );
4008 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
4009 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004010 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00004011 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004012 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004013 {
4014 if( generator->ctx.tls12_prf.key != NULL )
4015 {
Gilles Peskine3f108122018-12-07 18:14:53 +01004016 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004017 generator->ctx.tls12_prf.key_len );
4018 mbedtls_free( generator->ctx.tls12_prf.key );
4019 }
Hanno Becker580fba12018-11-13 20:50:45 +00004020
4021 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
4022 {
Gilles Peskine3f108122018-12-07 18:14:53 +01004023 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004024 generator->ctx.tls12_prf.Ai_with_seed_len );
4025 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
4026 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004027 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004028 else
4029#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004030 {
4031 status = PSA_ERROR_BAD_STATE;
4032 }
4033 memset( generator, 0, sizeof( *generator ) );
4034 return( status );
4035}
4036
Gilles Peskineeab56e42018-07-12 17:12:33 +02004037psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
4038 size_t *capacity)
4039{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004040 if( generator->alg == 0 )
4041 {
4042 /* This is a blank generator. */
4043 return PSA_ERROR_BAD_STATE;
4044 }
4045
Gilles Peskineeab56e42018-07-12 17:12:33 +02004046 *capacity = generator->capacity;
4047 return( PSA_SUCCESS );
4048}
4049
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004050psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
4051 size_t capacity )
4052{
4053 if( generator->alg == 0 )
4054 return( PSA_ERROR_BAD_STATE );
4055 if( capacity > generator->capacity )
4056 return( PSA_ERROR_INVALID_ARGUMENT );
4057 generator->capacity = capacity;
4058 return( PSA_SUCCESS );
4059}
4060
Gilles Peskinebef7f142018-07-12 17:22:21 +02004061#if defined(MBEDTLS_MD_C)
4062/* Read some bytes from an HKDF-based generator. This performs a chunk
4063 * of the expand phase of the HKDF algorithm. */
4064static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
4065 psa_algorithm_t hash_alg,
4066 uint8_t *output,
4067 size_t output_length )
4068{
4069 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4070 psa_status_t status;
4071
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004072 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4073 return( PSA_ERROR_BAD_STATE );
4074 hkdf->state = HKDF_STATE_OUTPUT;
4075
Gilles Peskinebef7f142018-07-12 17:22:21 +02004076 while( output_length != 0 )
4077 {
4078 /* Copy what remains of the current block */
4079 uint8_t n = hash_length - hkdf->offset_in_block;
4080 if( n > output_length )
4081 n = (uint8_t) output_length;
4082 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4083 output += n;
4084 output_length -= n;
4085 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004086 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004087 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004088 /* We can't be wanting more output after block 0xff, otherwise
4089 * the capacity check in psa_generator_read() would have
4090 * prevented this call. It could happen only if the generator
4091 * object was corrupted or if this function is called directly
4092 * inside the library. */
4093 if( hkdf->block_number == 0xff )
4094 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004095
4096 /* We need a new block */
4097 ++hkdf->block_number;
4098 hkdf->offset_in_block = 0;
4099 status = psa_hmac_setup_internal( &hkdf->hmac,
4100 hkdf->prk, hash_length,
4101 hash_alg );
4102 if( status != PSA_SUCCESS )
4103 return( status );
4104 if( hkdf->block_number != 1 )
4105 {
4106 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4107 hkdf->output_block,
4108 hash_length );
4109 if( status != PSA_SUCCESS )
4110 return( status );
4111 }
4112 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4113 hkdf->info,
4114 hkdf->info_length );
4115 if( status != PSA_SUCCESS )
4116 return( status );
4117 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4118 &hkdf->block_number, 1 );
4119 if( status != PSA_SUCCESS )
4120 return( status );
4121 status = psa_hmac_finish_internal( &hkdf->hmac,
4122 hkdf->output_block,
4123 sizeof( hkdf->output_block ) );
4124 if( status != PSA_SUCCESS )
4125 return( status );
4126 }
4127
4128 return( PSA_SUCCESS );
4129}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004130
4131static psa_status_t psa_generator_tls12_prf_generate_next_block(
4132 psa_tls12_prf_generator_t *tls12_prf,
4133 psa_algorithm_t alg )
4134{
4135 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4136 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4137 psa_hmac_internal_data hmac;
4138 psa_status_t status, cleanup_status;
4139
Hanno Becker3b339e22018-11-13 20:56:14 +00004140 unsigned char *Ai;
4141 size_t Ai_len;
4142
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004143 /* We can't be wanting more output after block 0xff, otherwise
4144 * the capacity check in psa_generator_read() would have
4145 * prevented this call. It could happen only if the generator
4146 * object was corrupted or if this function is called directly
4147 * inside the library. */
4148 if( tls12_prf->block_number == 0xff )
4149 return( PSA_ERROR_BAD_STATE );
4150
4151 /* We need a new block */
4152 ++tls12_prf->block_number;
4153 tls12_prf->offset_in_block = 0;
4154
4155 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4156 *
4157 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4158 *
4159 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4160 * HMAC_hash(secret, A(2) + seed) +
4161 * HMAC_hash(secret, A(3) + seed) + ...
4162 *
4163 * A(0) = seed
4164 * A(i) = HMAC_hash( secret, A(i-1) )
4165 *
4166 * The `psa_tls12_prf_generator` structures saves the block
4167 * `HMAC_hash(secret, A(i) + seed)` from which the output
4168 * is currently extracted as `output_block`, while
4169 * `A(i) + seed` is stored in `Ai_with_seed`.
4170 *
4171 * Generating a new block means recalculating `Ai_with_seed`
4172 * from the A(i)-part of it, and afterwards recalculating
4173 * `output_block`.
4174 *
4175 * A(0) is computed at setup time.
4176 *
4177 */
4178
4179 psa_hmac_init_internal( &hmac );
4180
4181 /* We must distinguish the calculation of A(1) from those
4182 * of A(2) and higher, because A(0)=seed has a different
4183 * length than the other A(i). */
4184 if( tls12_prf->block_number == 1 )
4185 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004186 Ai = tls12_prf->Ai_with_seed + hash_length;
4187 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004188 }
4189 else
4190 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004191 Ai = tls12_prf->Ai_with_seed;
4192 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004193 }
4194
Hanno Becker3b339e22018-11-13 20:56:14 +00004195 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4196 status = psa_hmac_setup_internal( &hmac,
4197 tls12_prf->key,
4198 tls12_prf->key_len,
4199 hash_alg );
4200 if( status != PSA_SUCCESS )
4201 goto cleanup;
4202
4203 status = psa_hash_update( &hmac.hash_ctx,
4204 Ai, Ai_len );
4205 if( status != PSA_SUCCESS )
4206 goto cleanup;
4207
4208 status = psa_hmac_finish_internal( &hmac,
4209 tls12_prf->Ai_with_seed,
4210 hash_length );
4211 if( status != PSA_SUCCESS )
4212 goto cleanup;
4213
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004214 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4215 status = psa_hmac_setup_internal( &hmac,
4216 tls12_prf->key,
4217 tls12_prf->key_len,
4218 hash_alg );
4219 if( status != PSA_SUCCESS )
4220 goto cleanup;
4221
4222 status = psa_hash_update( &hmac.hash_ctx,
4223 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004224 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004225 if( status != PSA_SUCCESS )
4226 goto cleanup;
4227
4228 status = psa_hmac_finish_internal( &hmac,
4229 tls12_prf->output_block,
4230 hash_length );
4231 if( status != PSA_SUCCESS )
4232 goto cleanup;
4233
4234cleanup:
4235
4236 cleanup_status = psa_hmac_abort_internal( &hmac );
4237 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4238 status = cleanup_status;
4239
4240 return( status );
4241}
4242
4243/* Read some bytes from an TLS-1.2-PRF-based generator.
4244 * See Section 5 of RFC 5246. */
4245static psa_status_t psa_generator_tls12_prf_read(
4246 psa_tls12_prf_generator_t *tls12_prf,
4247 psa_algorithm_t alg,
4248 uint8_t *output,
4249 size_t output_length )
4250{
4251 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4252 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4253 psa_status_t status;
4254
4255 while( output_length != 0 )
4256 {
4257 /* Copy what remains of the current block */
4258 uint8_t n = hash_length - tls12_prf->offset_in_block;
4259
4260 /* Check if we have fully processed the current block. */
4261 if( n == 0 )
4262 {
4263 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
4264 alg );
4265 if( status != PSA_SUCCESS )
4266 return( status );
4267
4268 continue;
4269 }
4270
4271 if( n > output_length )
4272 n = (uint8_t) output_length;
4273 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4274 n );
4275 output += n;
4276 output_length -= n;
4277 tls12_prf->offset_in_block += n;
4278 }
4279
4280 return( PSA_SUCCESS );
4281}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004282#endif /* MBEDTLS_MD_C */
4283
Gilles Peskineeab56e42018-07-12 17:12:33 +02004284psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
4285 uint8_t *output,
4286 size_t output_length )
4287{
4288 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004289 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004290
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004291 if( generator->alg == 0 )
4292 {
4293 /* This is a blank generator. */
4294 return PSA_ERROR_BAD_STATE;
4295 }
4296
Gilles Peskineeab56e42018-07-12 17:12:33 +02004297 if( output_length > generator->capacity )
4298 {
4299 generator->capacity = 0;
4300 /* Go through the error path to wipe all confidential data now
4301 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004302 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004303 goto exit;
4304 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004305 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004306 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004307 /* Edge case: this is a finished generator, and 0 bytes
4308 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004309 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4310 * INSUFFICIENT_CAPACITY, which is right for a finished
4311 * generator, for consistency with the case when
4312 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004313 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004314 }
4315 generator->capacity -= output_length;
4316
Gilles Peskine969c5d62019-01-16 15:53:06 +01004317 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004318 {
Gilles Peskine211a4362018-10-25 22:22:31 +02004319 /* Initially, the capacity of a selection generator is always
4320 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
4321 * abbreviated in this comment as `size`. When the remaining
4322 * capacity is `c`, the next bytes to serve start `c` bytes
4323 * from the end of the buffer, i.e. `size - c` from the
4324 * beginning of the buffer. Since `generator->capacity` was just
4325 * decremented above, we need to serve the bytes from
4326 * `size - generator->capacity - output_length` to
4327 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004328 size_t offset =
4329 generator->ctx.buffer.size - generator->capacity - output_length;
4330 memcpy( output, generator->ctx.buffer.data + offset, output_length );
4331 status = PSA_SUCCESS;
4332 }
4333 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004334#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004335 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004336 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004337 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004338 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
4339 output, output_length );
4340 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004341 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4342 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004343 {
4344 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004345 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004346 output_length );
4347 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004348 else
4349#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004350 {
4351 return( PSA_ERROR_BAD_STATE );
4352 }
4353
4354exit:
4355 if( status != PSA_SUCCESS )
4356 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004357 /* Preserve the algorithm upon errors, but clear all sensitive state.
4358 * This allows us to differentiate between exhausted generators and
4359 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
4360 * generators. */
4361 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004362 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004363 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004364 memset( output, '!', output_length );
4365 }
4366 return( status );
4367}
4368
Gilles Peskine08542d82018-07-19 17:05:42 +02004369#if defined(MBEDTLS_DES_C)
4370static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4371{
4372 if( data_size >= 8 )
4373 mbedtls_des_key_set_parity( data );
4374 if( data_size >= 16 )
4375 mbedtls_des_key_set_parity( data + 8 );
4376 if( data_size >= 24 )
4377 mbedtls_des_key_set_parity( data + 16 );
4378}
4379#endif /* MBEDTLS_DES_C */
4380
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004381static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004382 psa_key_slot_t *slot,
4383 size_t bits,
4384 psa_crypto_generator_t *generator )
4385{
4386 uint8_t *data = NULL;
4387 size_t bytes = PSA_BITS_TO_BYTES( bits );
4388 psa_status_t status;
4389
4390 if( ! key_type_is_raw_bytes( slot->type ) )
4391 return( PSA_ERROR_INVALID_ARGUMENT );
4392 if( bits % 8 != 0 )
4393 return( PSA_ERROR_INVALID_ARGUMENT );
4394 data = mbedtls_calloc( 1, bytes );
4395 if( data == NULL )
4396 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4397
4398 status = psa_generator_read( generator, data, bytes );
4399 if( status != PSA_SUCCESS )
4400 goto exit;
4401#if defined(MBEDTLS_DES_C)
4402 if( slot->type == PSA_KEY_TYPE_DES )
4403 psa_des_set_key_parity( data, bytes );
4404#endif /* MBEDTLS_DES_C */
4405 status = psa_import_key_into_slot( slot, data, bytes );
4406
4407exit:
4408 mbedtls_free( data );
4409 return( status );
4410}
4411
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004412psa_status_t psa_generate_derived_key( const psa_key_attributes_t *attributes,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004413 psa_key_handle_t *handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004414 psa_crypto_generator_t *generator )
4415{
4416 psa_status_t status;
4417 psa_key_slot_t *slot = NULL;
4418 status = psa_start_key_creation( attributes, handle, &slot );
4419 if( status == PSA_SUCCESS )
4420 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004421 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004422 attributes->bits,
4423 generator );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004424 }
4425 if( status == PSA_SUCCESS )
4426 status = psa_finish_key_creation( slot );
4427 if( status != PSA_SUCCESS )
4428 {
4429 psa_fail_key_creation( slot );
4430 *handle = 0;
4431 }
4432 return( status );
4433}
4434
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004435psa_status_t psa_generate_derived_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004436 psa_key_type_t type,
4437 size_t bits,
4438 psa_crypto_generator_t *generator )
4439{
4440 uint8_t *data = NULL;
4441 size_t bytes = PSA_BITS_TO_BYTES( bits );
4442 psa_status_t status;
4443
4444 if( ! key_type_is_raw_bytes( type ) )
4445 return( PSA_ERROR_INVALID_ARGUMENT );
4446 if( bits % 8 != 0 )
4447 return( PSA_ERROR_INVALID_ARGUMENT );
4448 data = mbedtls_calloc( 1, bytes );
4449 if( data == NULL )
4450 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4451
4452 status = psa_generator_read( generator, data, bytes );
4453 if( status != PSA_SUCCESS )
4454 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004455#if defined(MBEDTLS_DES_C)
4456 if( type == PSA_KEY_TYPE_DES )
4457 psa_des_set_key_parity( data, bytes );
4458#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004459 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004460
4461exit:
4462 mbedtls_free( data );
4463 return( status );
4464}
4465
4466
4467
4468/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004469/* Key derivation */
4470/****************************************************************/
4471
Gilles Peskinea05219c2018-11-16 16:02:56 +01004472#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004473/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004474 * of the HKDF algorithm.
4475 *
4476 * Note that if this function fails, you must call psa_generator_abort()
4477 * to potentially free embedded data structures and wipe confidential data.
4478 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004479static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004480 const uint8_t *secret,
4481 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004482 psa_algorithm_t hash_alg,
4483 const uint8_t *salt,
4484 size_t salt_length,
4485 const uint8_t *label,
4486 size_t label_length )
4487{
4488 psa_status_t status;
4489 status = psa_hmac_setup_internal( &hkdf->hmac,
4490 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004491 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004492 if( status != PSA_SUCCESS )
4493 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004494 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004495 if( status != PSA_SUCCESS )
4496 return( status );
4497 status = psa_hmac_finish_internal( &hkdf->hmac,
4498 hkdf->prk,
4499 sizeof( hkdf->prk ) );
4500 if( status != PSA_SUCCESS )
4501 return( status );
4502 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4503 hkdf->block_number = 0;
4504 hkdf->info_length = label_length;
4505 if( label_length != 0 )
4506 {
4507 hkdf->info = mbedtls_calloc( 1, label_length );
4508 if( hkdf->info == NULL )
4509 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4510 memcpy( hkdf->info, label, label_length );
4511 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004512 hkdf->state = HKDF_STATE_KEYED;
4513 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004514 return( PSA_SUCCESS );
4515}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004516#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004517
Gilles Peskinea05219c2018-11-16 16:02:56 +01004518#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004519/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4520 *
4521 * Note that if this function fails, you must call psa_generator_abort()
4522 * to potentially free embedded data structures and wipe confidential data.
4523 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004524static psa_status_t psa_generator_tls12_prf_setup(
4525 psa_tls12_prf_generator_t *tls12_prf,
4526 const unsigned char *key,
4527 size_t key_len,
4528 psa_algorithm_t hash_alg,
4529 const uint8_t *salt,
4530 size_t salt_length,
4531 const uint8_t *label,
4532 size_t label_length )
4533{
4534 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004535 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4536 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004537
4538 tls12_prf->key = mbedtls_calloc( 1, key_len );
4539 if( tls12_prf->key == NULL )
4540 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4541 tls12_prf->key_len = key_len;
4542 memcpy( tls12_prf->key, key, key_len );
4543
Hanno Becker580fba12018-11-13 20:50:45 +00004544 overflow = ( salt_length + label_length < salt_length ) ||
4545 ( salt_length + label_length + hash_length < hash_length );
4546 if( overflow )
4547 return( PSA_ERROR_INVALID_ARGUMENT );
4548
4549 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4550 if( tls12_prf->Ai_with_seed == NULL )
4551 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4552 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4553
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004554 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4555 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004556 if( label_length != 0 )
4557 {
4558 memcpy( tls12_prf->Ai_with_seed + hash_length,
4559 label, label_length );
4560 }
4561
4562 if( salt_length != 0 )
4563 {
4564 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4565 salt, salt_length );
4566 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004567
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004568 /* The first block gets generated when
4569 * psa_generator_read() is called. */
4570 tls12_prf->block_number = 0;
4571 tls12_prf->offset_in_block = hash_length;
4572
4573 return( PSA_SUCCESS );
4574}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004575
4576/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4577static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4578 psa_tls12_prf_generator_t *tls12_prf,
4579 const unsigned char *psk,
4580 size_t psk_len,
4581 psa_algorithm_t hash_alg,
4582 const uint8_t *salt,
4583 size_t salt_length,
4584 const uint8_t *label,
4585 size_t label_length )
4586{
4587 psa_status_t status;
4588 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4589
4590 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4591 return( PSA_ERROR_INVALID_ARGUMENT );
4592
4593 /* Quoting RFC 4279, Section 2:
4594 *
4595 * The premaster secret is formed as follows: if the PSK is N octets
4596 * long, concatenate a uint16 with the value N, N zero octets, a second
4597 * uint16 with the value N, and the PSK itself.
4598 */
4599
4600 pms[0] = ( psk_len >> 8 ) & 0xff;
4601 pms[1] = ( psk_len >> 0 ) & 0xff;
4602 memset( pms + 2, 0, psk_len );
4603 pms[2 + psk_len + 0] = pms[0];
4604 pms[2 + psk_len + 1] = pms[1];
4605 memcpy( pms + 4 + psk_len, psk, psk_len );
4606
4607 status = psa_generator_tls12_prf_setup( tls12_prf,
4608 pms, 4 + 2 * psk_len,
4609 hash_alg,
4610 salt, salt_length,
4611 label, label_length );
4612
Gilles Peskine3f108122018-12-07 18:14:53 +01004613 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004614 return( status );
4615}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004616#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004617
Gilles Peskine346797d2018-11-16 16:05:06 +01004618/* Note that if this function fails, you must call psa_generator_abort()
4619 * to potentially free embedded data structures and wipe confidential data.
4620 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004621static psa_status_t psa_key_derivation_internal(
4622 psa_crypto_generator_t *generator,
4623 const uint8_t *secret, size_t secret_length,
4624 psa_algorithm_t alg,
4625 const uint8_t *salt, size_t salt_length,
4626 const uint8_t *label, size_t label_length,
4627 size_t capacity )
4628{
4629 psa_status_t status;
4630 size_t max_capacity;
4631
4632 /* Set generator->alg even on failure so that abort knows what to do. */
4633 generator->alg = alg;
4634
Gilles Peskine751d9652018-09-18 12:05:44 +02004635 if( alg == PSA_ALG_SELECT_RAW )
4636 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004637 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004638 if( salt_length != 0 )
4639 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004640 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004641 if( label_length != 0 )
4642 return( PSA_ERROR_INVALID_ARGUMENT );
4643 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4644 if( generator->ctx.buffer.data == NULL )
4645 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4646 memcpy( generator->ctx.buffer.data, secret, secret_length );
4647 generator->ctx.buffer.size = secret_length;
4648 max_capacity = secret_length;
4649 status = PSA_SUCCESS;
4650 }
4651 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004652#if defined(MBEDTLS_MD_C)
4653 if( PSA_ALG_IS_HKDF( alg ) )
4654 {
4655 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4656 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4657 if( hash_size == 0 )
4658 return( PSA_ERROR_NOT_SUPPORTED );
4659 max_capacity = 255 * hash_size;
4660 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4661 secret, secret_length,
4662 hash_alg,
4663 salt, salt_length,
4664 label, label_length );
4665 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004666 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4667 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4668 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004669 {
4670 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4671 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4672
4673 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4674 if( hash_alg != PSA_ALG_SHA_256 &&
4675 hash_alg != PSA_ALG_SHA_384 )
4676 {
4677 return( PSA_ERROR_NOT_SUPPORTED );
4678 }
4679
4680 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004681
4682 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4683 {
4684 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4685 secret, secret_length,
4686 hash_alg, salt, salt_length,
4687 label, label_length );
4688 }
4689 else
4690 {
4691 status = psa_generator_tls12_psk_to_ms_setup(
4692 &generator->ctx.tls12_prf,
4693 secret, secret_length,
4694 hash_alg, salt, salt_length,
4695 label, label_length );
4696 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004697 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004698 else
4699#endif
4700 {
4701 return( PSA_ERROR_NOT_SUPPORTED );
4702 }
4703
4704 if( status != PSA_SUCCESS )
4705 return( status );
4706
4707 if( capacity <= max_capacity )
4708 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004709 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4710 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004711 else
4712 return( PSA_ERROR_INVALID_ARGUMENT );
4713
4714 return( PSA_SUCCESS );
4715}
4716
Gilles Peskineea0fb492018-07-12 17:17:20 +02004717psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004718 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004719 psa_algorithm_t alg,
4720 const uint8_t *salt,
4721 size_t salt_length,
4722 const uint8_t *label,
4723 size_t label_length,
4724 size_t capacity )
4725{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004726 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004727 psa_status_t status;
4728
4729 if( generator->alg != 0 )
4730 return( PSA_ERROR_BAD_STATE );
4731
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004732 /* Make sure that alg is a key derivation algorithm. This prevents
4733 * key selection algorithms, which psa_key_derivation_internal
4734 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004735 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4736 return( PSA_ERROR_INVALID_ARGUMENT );
4737
Gilles Peskinec5487a82018-12-03 18:08:14 +01004738 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004739 if( status != PSA_SUCCESS )
4740 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004741
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004742 if( slot->type != PSA_KEY_TYPE_DERIVE )
4743 return( PSA_ERROR_INVALID_ARGUMENT );
4744
4745 status = psa_key_derivation_internal( generator,
4746 slot->data.raw.data,
4747 slot->data.raw.bytes,
4748 alg,
4749 salt, salt_length,
4750 label, label_length,
4751 capacity );
4752 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004753 psa_generator_abort( generator );
4754 return( status );
4755}
4756
Gilles Peskine969c5d62019-01-16 15:53:06 +01004757static psa_status_t psa_key_derivation_setup_kdf(
4758 psa_crypto_generator_t *generator,
4759 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004760{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004761 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004762#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004763 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4764 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4765 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004766 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004767 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004768 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4769 if( hash_size == 0 )
4770 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004771 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4772 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004773 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004774 {
4775 return( PSA_ERROR_NOT_SUPPORTED );
4776 }
4777 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004778 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004779 }
4780#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004781 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004782 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004783}
4784
4785psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4786 psa_algorithm_t alg )
4787{
4788 psa_status_t status;
4789
4790 if( generator->alg != 0 )
4791 return( PSA_ERROR_BAD_STATE );
4792
Gilles Peskine6843c292019-01-18 16:44:49 +01004793 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4794 return( PSA_ERROR_INVALID_ARGUMENT );
4795 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004796 {
4797 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004798 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004799 }
4800 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4801 {
4802 status = psa_key_derivation_setup_kdf( generator, alg );
4803 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004804 else
4805 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004806
4807 if( status == PSA_SUCCESS )
4808 generator->alg = alg;
4809 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004810}
4811
4812#if defined(MBEDTLS_MD_C)
4813static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4814 psa_algorithm_t hash_alg,
4815 psa_key_derivation_step_t step,
4816 const uint8_t *data,
4817 size_t data_length )
4818{
4819 psa_status_t status;
4820 switch( step )
4821 {
4822 case PSA_KDF_STEP_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004823 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004824 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004825 status = psa_hmac_setup_internal( &hkdf->hmac,
4826 data, data_length,
4827 hash_alg );
4828 if( status != PSA_SUCCESS )
4829 return( status );
4830 hkdf->state = HKDF_STATE_STARTED;
4831 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004832 case PSA_KDF_STEP_SECRET:
4833 /* If no salt was provided, use an empty salt. */
4834 if( hkdf->state == HKDF_STATE_INIT )
4835 {
4836 status = psa_hmac_setup_internal( &hkdf->hmac,
4837 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004838 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004839 if( status != PSA_SUCCESS )
4840 return( status );
4841 hkdf->state = HKDF_STATE_STARTED;
4842 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004843 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004844 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004845 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4846 data, data_length );
4847 if( status != PSA_SUCCESS )
4848 return( status );
4849 status = psa_hmac_finish_internal( &hkdf->hmac,
4850 hkdf->prk,
4851 sizeof( hkdf->prk ) );
4852 if( status != PSA_SUCCESS )
4853 return( status );
4854 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4855 hkdf->block_number = 0;
4856 hkdf->state = HKDF_STATE_KEYED;
4857 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004858 case PSA_KDF_STEP_INFO:
4859 if( hkdf->state == HKDF_STATE_OUTPUT )
4860 return( PSA_ERROR_BAD_STATE );
4861 if( hkdf->info_set )
4862 return( PSA_ERROR_BAD_STATE );
4863 hkdf->info_length = data_length;
4864 if( data_length != 0 )
4865 {
4866 hkdf->info = mbedtls_calloc( 1, data_length );
4867 if( hkdf->info == NULL )
4868 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4869 memcpy( hkdf->info, data, data_length );
4870 }
4871 hkdf->info_set = 1;
4872 return( PSA_SUCCESS );
4873 default:
4874 return( PSA_ERROR_INVALID_ARGUMENT );
4875 }
4876}
4877#endif /* MBEDTLS_MD_C */
4878
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004879static psa_status_t psa_key_derivation_input_raw(
4880 psa_crypto_generator_t *generator,
4881 psa_key_derivation_step_t step,
4882 const uint8_t *data,
4883 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004884{
4885 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004886 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004887
Gilles Peskine969c5d62019-01-16 15:53:06 +01004888 if( kdf_alg == PSA_ALG_SELECT_RAW )
4889 {
4890 if( generator->capacity != 0 )
4891 return( PSA_ERROR_INVALID_ARGUMENT );
4892 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4893 if( generator->ctx.buffer.data == NULL )
4894 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4895 memcpy( generator->ctx.buffer.data, data, data_length );
4896 generator->ctx.buffer.size = data_length;
4897 generator->capacity = data_length;
4898 status = PSA_SUCCESS;
4899 }
4900 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004901#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004902 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004903 {
4904 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004905 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004906 step, data, data_length );
4907 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004908 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004909#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004910#if defined(MBEDTLS_MD_C)
4911 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004912 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4913 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004914 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004915 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004916 status = PSA_ERROR_NOT_SUPPORTED;
4917 }
4918 else
4919#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004920 {
4921 /* This can't happen unless the generator object was not initialized */
4922 return( PSA_ERROR_BAD_STATE );
4923 }
4924
4925 if( status != PSA_SUCCESS )
4926 psa_generator_abort( generator );
4927 return( status );
4928}
4929
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004930psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4931 psa_key_derivation_step_t step,
4932 const uint8_t *data,
4933 size_t data_length )
4934{
4935 switch( step )
4936 {
4937 case PSA_KDF_STEP_LABEL:
4938 case PSA_KDF_STEP_SALT:
4939 case PSA_KDF_STEP_INFO:
4940 return( psa_key_derivation_input_raw( generator, step,
4941 data, data_length ) );
4942 default:
4943 return( PSA_ERROR_INVALID_ARGUMENT );
4944 }
4945}
4946
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004947psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4948 psa_key_derivation_step_t step,
4949 psa_key_handle_t handle )
4950{
4951 psa_key_slot_t *slot;
4952 psa_status_t status;
4953 status = psa_get_key_from_slot( handle, &slot,
4954 PSA_KEY_USAGE_DERIVE,
4955 generator->alg );
4956 if( status != PSA_SUCCESS )
4957 return( status );
4958 if( slot->type != PSA_KEY_TYPE_DERIVE )
4959 return( PSA_ERROR_INVALID_ARGUMENT );
4960 /* Don't allow a key to be used as an input that is usually public.
4961 * This is debatable. It's ok from a cryptographic perspective to
4962 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004963 * the material should be dedicated to a particular input step,
4964 * otherwise this may allow the key to be used in an unintended way
4965 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004966 if( step != PSA_KDF_STEP_SECRET )
4967 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004968 return( psa_key_derivation_input_raw( generator,
4969 step,
4970 slot->data.raw.data,
4971 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004972}
4973
Gilles Peskineea0fb492018-07-12 17:17:20 +02004974
4975
4976/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004977/* Key agreement */
4978/****************************************************************/
4979
Gilles Peskinea05219c2018-11-16 16:02:56 +01004980#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004981static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4982 size_t peer_key_length,
4983 const mbedtls_ecp_keypair *our_key,
4984 uint8_t *shared_secret,
4985 size_t shared_secret_size,
4986 size_t *shared_secret_length )
4987{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004988 mbedtls_ecp_keypair *their_key = NULL;
4989 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004990 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004991 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004992
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004993 status = psa_import_ec_public_key(
4994 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4995 peer_key, peer_key_length,
4996 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004997 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004998 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004999
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005000 status = mbedtls_to_psa_error(
5001 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5002 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005003 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005004 status = mbedtls_to_psa_error(
5005 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5006 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005007 goto exit;
5008
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005009 status = mbedtls_to_psa_error(
5010 mbedtls_ecdh_calc_secret( &ecdh,
5011 shared_secret_length,
5012 shared_secret, shared_secret_size,
5013 mbedtls_ctr_drbg_random,
5014 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005015
5016exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005017 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005018 mbedtls_ecp_keypair_free( their_key );
5019 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005020 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005021}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005022#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005023
Gilles Peskine01d718c2018-09-18 12:01:02 +02005024#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5025
Gilles Peskine0216fe12019-04-11 21:23:21 +02005026static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5027 psa_key_slot_t *private_key,
5028 const uint8_t *peer_key,
5029 size_t peer_key_length,
5030 uint8_t *shared_secret,
5031 size_t shared_secret_size,
5032 size_t *shared_secret_length )
5033{
5034 switch( alg )
5035 {
5036#if defined(MBEDTLS_ECDH_C)
5037 case PSA_ALG_ECDH:
5038 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
5039 return( PSA_ERROR_INVALID_ARGUMENT );
5040 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5041 private_key->data.ecp,
5042 shared_secret, shared_secret_size,
5043 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005044#endif /* MBEDTLS_ECDH_C */
5045 default:
5046 (void) private_key;
5047 (void) peer_key;
5048 (void) peer_key_length;
5049 (void) shared_secret;
5050 (void) shared_secret_size;
5051 (void) shared_secret_length;
5052 return( PSA_ERROR_NOT_SUPPORTED );
5053 }
5054}
5055
Gilles Peskine346797d2018-11-16 16:05:06 +01005056/* Note that if this function fails, you must call psa_generator_abort()
5057 * to potentially free embedded data structures and wipe confidential data.
5058 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005059static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005060 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005061 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005062 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005063 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005064{
5065 psa_status_t status;
5066 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5067 size_t shared_secret_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005068 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005069
5070 /* Step 1: run the secret agreement algorithm to generate the shared
5071 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005072 status = psa_key_agreement_raw_internal( ka_alg,
5073 private_key,
5074 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005075 shared_secret,
5076 sizeof( shared_secret ),
5077 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005078 if( status != PSA_SUCCESS )
5079 goto exit;
5080
5081 /* Step 2: set up the key derivation to generate key material from
5082 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005083 status = psa_key_derivation_input_raw( generator, step,
5084 shared_secret, shared_secret_length );
5085
Gilles Peskine01d718c2018-09-18 12:01:02 +02005086exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005087 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005088 return( status );
5089}
5090
5091psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005092 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005093 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005094 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005095 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005096{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005097 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005098 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005099 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005100 return( PSA_ERROR_INVALID_ARGUMENT );
5101 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005102 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005103 if( status != PSA_SUCCESS )
5104 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005105 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005106 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005107 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005108 if( status != PSA_SUCCESS )
5109 psa_generator_abort( generator );
5110 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005111}
5112
Gilles Peskine0216fe12019-04-11 21:23:21 +02005113psa_status_t psa_key_agreement_raw_shared_secret( psa_algorithm_t alg,
5114 psa_key_handle_t private_key,
5115 const uint8_t *peer_key,
5116 size_t peer_key_length,
5117 uint8_t *output,
5118 size_t output_size,
5119 size_t *output_length )
5120{
5121 psa_key_slot_t *slot;
5122 psa_status_t status;
5123
5124 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5125 {
5126 status = PSA_ERROR_INVALID_ARGUMENT;
5127 goto exit;
5128 }
5129 status = psa_get_key_from_slot( private_key, &slot,
5130 PSA_KEY_USAGE_DERIVE, alg );
5131 if( status != PSA_SUCCESS )
5132 goto exit;
5133
5134 status = psa_key_agreement_raw_internal( alg, slot,
5135 peer_key, peer_key_length,
5136 output, output_size,
5137 output_length );
5138
5139exit:
5140 if( status != PSA_SUCCESS )
5141 {
5142 /* If an error happens and is not handled properly, the output
5143 * may be used as a key to protect sensitive data. Arrange for such
5144 * a key to be random, which is likely to result in decryption or
5145 * verification errors. This is better than filling the buffer with
5146 * some constant data such as zeros, which would result in the data
5147 * being protected with a reproducible, easily knowable key.
5148 */
5149 psa_generate_random( output, output_size );
5150 *output_length = output_size;
5151 }
5152 return( status );
5153}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005154
5155
5156/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005157/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005158/****************************************************************/
5159
5160psa_status_t psa_generate_random( uint8_t *output,
5161 size_t output_size )
5162{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005163 int ret;
5164 GUARD_MODULE_INITIALIZED;
5165
5166 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005167 return( mbedtls_to_psa_error( ret ) );
5168}
5169
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005170#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5171#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005172
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005173psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5174 size_t seed_size )
5175{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005176 if( global_data.initialized )
5177 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005178
5179 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5180 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5181 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5182 return( PSA_ERROR_INVALID_ARGUMENT );
5183
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005184 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005185}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005186#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005187
Gilles Peskinee56e8782019-04-26 17:34:02 +02005188#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5189static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5190 size_t domain_parameters_size,
5191 int *exponent )
5192{
5193 size_t i;
5194 uint32_t acc = 0;
5195
5196 if( domain_parameters_size == 0 )
5197 {
5198 *exponent = 65537;
5199 return( PSA_SUCCESS );
5200 }
5201
5202 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5203 * support values that fit in a 32-bit integer, which is larger than
5204 * int on just about every platform anyway. */
5205 if( domain_parameters_size > sizeof( acc ) )
5206 return( PSA_ERROR_NOT_SUPPORTED );
5207 for( i = 0; i < domain_parameters_size; i++ )
5208 acc = ( acc << 8 ) | domain_parameters[i];
5209 if( acc > INT_MAX )
5210 return( PSA_ERROR_NOT_SUPPORTED );
5211 *exponent = acc;
5212 return( PSA_SUCCESS );
5213}
5214#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5215
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005216static psa_status_t psa_generate_random_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005217 psa_key_slot_t *slot, size_t bits,
5218 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005219{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005220 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005221
Gilles Peskinee56e8782019-04-26 17:34:02 +02005222 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005223 return( PSA_ERROR_INVALID_ARGUMENT );
5224
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005225 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005226 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005227 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005228 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005229 if( status != PSA_SUCCESS )
5230 return( status );
5231 status = psa_generate_random( slot->data.raw.data,
5232 slot->data.raw.bytes );
5233 if( status != PSA_SUCCESS )
5234 {
5235 mbedtls_free( slot->data.raw.data );
5236 return( status );
5237 }
5238#if defined(MBEDTLS_DES_C)
5239 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005240 psa_des_set_key_parity( slot->data.raw.data,
5241 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005242#endif /* MBEDTLS_DES_C */
5243 }
5244 else
5245
5246#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5247 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
5248 {
5249 mbedtls_rsa_context *rsa;
5250 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005251 int exponent;
5252 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005253 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5254 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005255 /* Accept only byte-aligned keys, for the same reasons as
5256 * in psa_import_rsa_key(). */
5257 if( bits % 8 != 0 )
5258 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005259 status = psa_read_rsa_exponent( domain_parameters,
5260 domain_parameters_size,
5261 &exponent );
5262 if( status != PSA_SUCCESS )
5263 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005264 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5265 if( rsa == NULL )
5266 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5267 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5268 ret = mbedtls_rsa_gen_key( rsa,
5269 mbedtls_ctr_drbg_random,
5270 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005271 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005272 exponent );
5273 if( ret != 0 )
5274 {
5275 mbedtls_rsa_free( rsa );
5276 mbedtls_free( rsa );
5277 return( mbedtls_to_psa_error( ret ) );
5278 }
5279 slot->data.rsa = rsa;
5280 }
5281 else
5282#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5283
5284#if defined(MBEDTLS_ECP_C)
5285 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
5286 {
5287 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5288 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5289 const mbedtls_ecp_curve_info *curve_info =
5290 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5291 mbedtls_ecp_keypair *ecp;
5292 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005293 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005294 return( PSA_ERROR_NOT_SUPPORTED );
5295 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5296 return( PSA_ERROR_NOT_SUPPORTED );
5297 if( curve_info->bit_size != bits )
5298 return( PSA_ERROR_INVALID_ARGUMENT );
5299 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5300 if( ecp == NULL )
5301 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5302 mbedtls_ecp_keypair_init( ecp );
5303 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5304 mbedtls_ctr_drbg_random,
5305 &global_data.ctr_drbg );
5306 if( ret != 0 )
5307 {
5308 mbedtls_ecp_keypair_free( ecp );
5309 mbedtls_free( ecp );
5310 return( mbedtls_to_psa_error( ret ) );
5311 }
5312 slot->data.ecp = ecp;
5313 }
5314 else
5315#endif /* MBEDTLS_ECP_C */
5316
5317 return( PSA_ERROR_NOT_SUPPORTED );
5318
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005319 return( PSA_SUCCESS );
5320}
5321
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005322psa_status_t psa_generate_random_key_to_handle( psa_key_handle_t handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005323 psa_key_type_t type,
5324 size_t bits,
5325 const void *extra,
5326 size_t extra_size )
5327{
5328 psa_key_slot_t *slot;
5329 psa_status_t status;
5330
Gilles Peskinee56e8782019-04-26 17:34:02 +02005331#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5332 /* The old public exponent encoding is no longer supported. */
5333 if( extra_size != 0 )
5334 return( PSA_ERROR_NOT_SUPPORTED );
5335#endif
5336
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005337 status = psa_get_empty_key_slot( handle, &slot );
5338 if( status != PSA_SUCCESS )
5339 return( status );
5340
Gilles Peskine12313cd2018-06-20 00:20:32 +02005341 slot->type = type;
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005342 status = psa_generate_random_key_internal( slot, bits, extra, extra_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005343 if( status != PSA_SUCCESS )
5344 slot->type = 0;
Darryl Green0c6575a2018-11-07 16:05:30 +00005345
5346#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
5347 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
5348 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01005349 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005350 }
5351#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5352
5353 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005354}
5355
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005356psa_status_t psa_generate_random_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005357 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005358{
5359 psa_status_t status;
5360 psa_key_slot_t *slot = NULL;
5361 status = psa_start_key_creation( attributes, handle, &slot );
5362 if( status == PSA_SUCCESS )
5363 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005364 status = psa_generate_random_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005365 slot, attributes->bits,
5366 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005367 }
5368 if( status == PSA_SUCCESS )
5369 status = psa_finish_key_creation( slot );
5370 if( status != PSA_SUCCESS )
5371 {
5372 psa_fail_key_creation( slot );
5373 *handle = 0;
5374 }
5375 return( status );
5376}
5377
5378
Gilles Peskine05d69892018-06-19 22:00:52 +02005379
5380/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005381/* Module setup */
5382/****************************************************************/
5383
Gilles Peskine5e769522018-11-20 21:59:56 +01005384psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5385 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5386 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5387{
5388 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5389 return( PSA_ERROR_BAD_STATE );
5390 global_data.entropy_init = entropy_init;
5391 global_data.entropy_free = entropy_free;
5392 return( PSA_SUCCESS );
5393}
5394
Gilles Peskinee59236f2018-01-27 23:32:46 +01005395void mbedtls_psa_crypto_free( void )
5396{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005397 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005398 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5399 {
5400 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005401 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005402 }
5403 /* Wipe all remaining data, including configuration.
5404 * In particular, this sets all state indicator to the value
5405 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005406 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005407}
5408
5409psa_status_t psa_crypto_init( void )
5410{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005411 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005412 const unsigned char drbg_seed[] = "PSA";
5413
Gilles Peskinec6b69072018-11-20 21:42:52 +01005414 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005415 if( global_data.initialized != 0 )
5416 return( PSA_SUCCESS );
5417
Gilles Peskine5e769522018-11-20 21:59:56 +01005418 /* Set default configuration if
5419 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5420 if( global_data.entropy_init == NULL )
5421 global_data.entropy_init = mbedtls_entropy_init;
5422 if( global_data.entropy_free == NULL )
5423 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005424
Gilles Peskinec6b69072018-11-20 21:42:52 +01005425 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005426 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005427 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005428 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005429 status = mbedtls_to_psa_error(
5430 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5431 mbedtls_entropy_func,
5432 &global_data.entropy,
5433 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5434 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005435 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005436 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005437
Gilles Peskine66fb1262018-12-10 16:29:04 +01005438 status = psa_initialize_key_slots( );
5439 if( status != PSA_SUCCESS )
5440 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005441
5442 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005443 global_data.initialized = 1;
5444
Gilles Peskinee59236f2018-01-27 23:32:46 +01005445exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005446 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005447 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005448 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005449}
5450
5451#endif /* MBEDTLS_PSA_CRYPTO_C */