blob: 38977cf0650622e383b90401f417d0cf808b7ad6 [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
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200624 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
625 return( PSA_ERROR_INVALID_ARGUMENT );
626
Gilles Peskinef76aa772018-10-29 19:24:33 +0100627 *p_ecp = NULL;
628 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
629 if( ecp == NULL )
630 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000631 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100632
633 /* Load the group. */
634 status = mbedtls_to_psa_error(
635 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
636 if( status != PSA_SUCCESS )
637 goto exit;
638 /* Load the secret value. */
639 status = mbedtls_to_psa_error(
640 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
641 if( status != PSA_SUCCESS )
642 goto exit;
643 /* Validate the private key. */
644 status = mbedtls_to_psa_error(
645 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
646 if( status != PSA_SUCCESS )
647 goto exit;
648 /* Calculate the public key from the private key. */
649 status = mbedtls_to_psa_error(
650 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
651 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
652 if( status != PSA_SUCCESS )
653 goto exit;
654
655 *p_ecp = ecp;
656 return( PSA_SUCCESS );
657
658exit:
659 if( ecp != NULL )
660 {
661 mbedtls_ecp_keypair_free( ecp );
662 mbedtls_free( ecp );
663 }
664 return( status );
665}
666#endif /* defined(MBEDTLS_ECP_C) */
667
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100668/** Import key data into a slot. `slot->type` must have been set
669 * previously. This function assumes that the slot does not contain
670 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100671psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
672 const uint8_t *data,
673 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200675 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676
Darryl Green940d72c2018-07-13 13:18:51 +0100677 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100679 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680 if( data_length > SIZE_MAX / 8 )
681 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100682 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200683 PSA_BYTES_TO_BITS( data_length ),
684 &slot->data.raw );
685 if( status != PSA_SUCCESS )
686 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200687 if( data_length != 0 )
688 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100689 }
690 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100691#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100692 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100693 {
Darryl Green940d72c2018-07-13 13:18:51 +0100694 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100695 data, data_length,
696 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100697 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000698 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
699 {
700 status = psa_import_ec_public_key(
701 PSA_KEY_TYPE_GET_CURVE( slot->type ),
702 data, data_length,
703 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000704 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100705 else
706#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000707#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
708 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100709 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000710 status = psa_import_rsa_key( slot->type,
711 data, data_length,
712 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713 }
714 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000715#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100716 {
717 return( PSA_ERROR_NOT_SUPPORTED );
718 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000719 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100720}
721
Darryl Green06fd18d2018-07-16 11:21:11 +0100722/* Retrieve an empty key slot (slot with no key data, but possibly
Jaeden Amero283dfd12019-01-11 12:06:22 +0000723 * with some metadata such as a policy or domain parameters). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100724static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100725 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100726{
727 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100728 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100729
730 *p_slot = NULL;
731
Gilles Peskinec5487a82018-12-03 18:08:14 +0100732 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100733 if( status != PSA_SUCCESS )
734 return( status );
735
736 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200737 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100738
739 *p_slot = slot;
740 return( status );
741}
742
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100743/** Calculate the intersection of two algorithm usage policies.
744 *
745 * Return 0 (which allows no operation) on incompatibility.
746 */
747static psa_algorithm_t psa_key_policy_algorithm_intersection(
748 psa_algorithm_t alg1,
749 psa_algorithm_t alg2 )
750{
751 /* Common case: the policy only allows alg. */
752 if( alg1 == alg2 )
753 return( alg1 );
754 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100755 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100756 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
757 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
758 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
759 {
760 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
761 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100762 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100763 return( alg1 );
764 }
765 /* If the policies are incompatible, allow nothing. */
766 return( 0 );
767}
768
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100769/** Test whether a policy permits an algorithm.
770 *
771 * The caller must test usage flags separately.
772 */
773static int psa_key_policy_permits( const psa_key_policy_t *policy,
774 psa_algorithm_t alg )
775{
776 /* Common case: the policy only allows alg. */
777 if( alg == policy->alg )
778 return( 1 );
779 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
780 * and alg is the same hash-and-sign family with any hash,
781 * then alg is compliant with policy->alg. */
782 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
783 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
784 {
785 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
786 ( alg & ~PSA_ALG_HASH_MASK ) );
787 }
788 /* If it isn't permitted, it's forbidden. */
789 return( 0 );
790}
791
Gilles Peskinef603c712019-01-19 13:40:11 +0100792/** Restrict a key policy based on a constraint.
793 *
794 * \param[in,out] policy The policy to restrict.
795 * \param[in] constraint The policy constraint to apply.
796 *
797 * \retval #PSA_SUCCESS
798 * \c *policy contains the intersection of the original value of
799 * \c *policy and \c *constraint.
800 * \retval #PSA_ERROR_INVALID_ARGUMENT
801 * \c *policy and \c *constraint are incompatible.
802 * \c *policy is unchanged.
803 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100804static psa_status_t psa_restrict_key_policy(
805 psa_key_policy_t *policy,
806 const psa_key_policy_t *constraint )
807{
808 psa_algorithm_t intersection_alg =
809 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
810 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
811 return( PSA_ERROR_INVALID_ARGUMENT );
812 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100813 policy->alg = intersection_alg;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100814 return( PSA_SUCCESS );
815}
816
Darryl Green06fd18d2018-07-16 11:21:11 +0100817/** Retrieve a slot which must contain a key. The key must have allow all the
818 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
819 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100820static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100821 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100822 psa_key_usage_t usage,
823 psa_algorithm_t alg )
824{
825 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100826 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100827
828 *p_slot = NULL;
829
Gilles Peskinec5487a82018-12-03 18:08:14 +0100830 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100831 if( status != PSA_SUCCESS )
832 return( status );
833 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200834 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100835
836 /* Enforce that usage policy for the key slot contains all the flags
837 * required by the usage parameter. There is one exception: public
838 * keys can always be exported, so we treat public key objects as
839 * if they had the export flag. */
840 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
841 usage &= ~PSA_KEY_USAGE_EXPORT;
842 if( ( slot->policy.usage & usage ) != usage )
843 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100844
845 /* Enforce that the usage policy permits the requested algortihm. */
846 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100847 return( PSA_ERROR_NOT_PERMITTED );
848
849 *p_slot = slot;
850 return( PSA_SUCCESS );
851}
Darryl Green940d72c2018-07-13 13:18:51 +0100852
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100853/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100854static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000855{
856 if( slot->type == PSA_KEY_TYPE_NONE )
857 {
858 /* No key material to clean. */
859 }
860 else if( key_type_is_raw_bytes( slot->type ) )
861 {
862 mbedtls_free( slot->data.raw.data );
863 }
864 else
865#if defined(MBEDTLS_RSA_C)
866 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
867 {
868 mbedtls_rsa_free( slot->data.rsa );
869 mbedtls_free( slot->data.rsa );
870 }
871 else
872#endif /* defined(MBEDTLS_RSA_C) */
873#if defined(MBEDTLS_ECP_C)
874 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
875 {
876 mbedtls_ecp_keypair_free( slot->data.ecp );
877 mbedtls_free( slot->data.ecp );
878 }
879 else
880#endif /* defined(MBEDTLS_ECP_C) */
881 {
882 /* Shouldn't happen: the key type is not any type that we
883 * put in. */
884 return( PSA_ERROR_TAMPERING_DETECTED );
885 }
886
887 return( PSA_SUCCESS );
888}
889
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100890static void psa_abort_operations_using_key( psa_key_slot_t *slot )
891{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200892 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100893 (void) slot;
894}
895
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100896/** Completely wipe a slot in memory, including its policy.
897 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100898psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100899{
900 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100901 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100902 /* At this point, key material and other type-specific content has
903 * been wiped. Clear remaining metadata. We can call memset and not
904 * zeroize because the metadata is not particularly sensitive. */
905 memset( slot, 0, sizeof( *slot ) );
906 return( status );
907}
908
Gilles Peskine87a5e562019-04-17 12:28:25 +0200909psa_status_t psa_import_key_to_handle( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100910 psa_key_type_t type,
911 const uint8_t *data,
912 size_t data_length )
913{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100914 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100915 psa_status_t status;
916
Gilles Peskinec5487a82018-12-03 18:08:14 +0100917 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100918 if( status != PSA_SUCCESS )
919 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100920
921 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100922
923 status = psa_import_key_into_slot( slot, data, data_length );
924 if( status != PSA_SUCCESS )
925 {
926 slot->type = PSA_KEY_TYPE_NONE;
927 return( status );
928 }
929
Darryl Greend49a4992018-06-18 17:27:26 +0100930#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
931 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
932 {
933 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100934 status = psa_save_persistent_key( slot->persistent_storage_id,
935 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100936 data_length );
937 if( status != PSA_SUCCESS )
938 {
939 (void) psa_remove_key_data_from_memory( slot );
940 slot->type = PSA_KEY_TYPE_NONE;
941 }
942 }
943#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
944
945 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946}
947
Gilles Peskinec5487a82018-12-03 18:08:14 +0100948psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100949{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100950 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100951 psa_status_t status = PSA_SUCCESS;
952 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953
Gilles Peskinec5487a82018-12-03 18:08:14 +0100954 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200955 if( status != PSA_SUCCESS )
956 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100957#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
958 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
959 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100960 storage_status =
961 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100962 }
963#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100964 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100965 if( status != PSA_SUCCESS )
966 return( status );
967 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968}
969
Gilles Peskineb870b182018-07-06 16:02:09 +0200970/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200971static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200972{
973 if( key_type_is_raw_bytes( slot->type ) )
974 return( slot->data.raw.bytes * 8 );
975#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200976 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100977 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200978#endif /* defined(MBEDTLS_RSA_C) */
979#if defined(MBEDTLS_ECP_C)
980 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
981 return( slot->data.ecp->grp.pbits );
982#endif /* defined(MBEDTLS_ECP_C) */
983 /* Shouldn't happen except on an empty slot. */
984 return( 0 );
985}
986
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200987void psa_reset_key_attributes( psa_key_attributes_t *attributes )
988{
Gilles Peskineb699f072019-04-26 16:06:02 +0200989 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200990 memset( attributes, 0, sizeof( *attributes ) );
991}
992
Gilles Peskineb699f072019-04-26 16:06:02 +0200993psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
994 psa_key_type_t type,
995 const uint8_t *data,
996 size_t data_length )
997{
998 uint8_t *copy = NULL;
999
1000 if( data_length != 0 )
1001 {
1002 copy = mbedtls_calloc( 1, data_length );
1003 if( copy == NULL )
1004 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1005 memcpy( copy, data, data_length );
1006 }
1007 /* After this point, this function is guaranteed to succeed, so it
1008 * can start modifying `*attributes`. */
1009
1010 if( attributes->domain_parameters != NULL )
1011 {
1012 mbedtls_free( attributes->domain_parameters );
1013 attributes->domain_parameters = NULL;
1014 attributes->domain_parameters_size = 0;
1015 }
1016
1017 attributes->domain_parameters = copy;
1018 attributes->domain_parameters_size = data_length;
1019 attributes->type = type;
1020 return( PSA_SUCCESS );
1021}
1022
1023psa_status_t psa_get_key_domain_parameters(
1024 const psa_key_attributes_t *attributes,
1025 uint8_t *data, size_t data_size, size_t *data_length )
1026{
1027 if( attributes->domain_parameters_size > data_size )
1028 return( PSA_ERROR_BUFFER_TOO_SMALL );
1029 *data_length = attributes->domain_parameters_size;
1030 if( attributes->domain_parameters_size != 0 )
1031 memcpy( data, attributes->domain_parameters,
1032 attributes->domain_parameters_size );
1033 return( PSA_SUCCESS );
1034}
1035
1036#if defined(MBEDTLS_RSA_C)
1037static psa_status_t psa_get_rsa_public_exponent(
1038 const mbedtls_rsa_context *rsa,
1039 psa_key_attributes_t *attributes )
1040{
1041 mbedtls_mpi mpi;
1042 int ret;
1043 uint8_t *buffer = NULL;
1044 size_t buflen;
1045 mbedtls_mpi_init( &mpi );
1046
1047 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1048 if( ret != 0 )
1049 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001050 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1051 {
1052 /* It's the default value, which is reported as an empty string,
1053 * so there's nothing to do. */
1054 goto exit;
1055 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001056
1057 buflen = mbedtls_mpi_size( &mpi );
1058 buffer = mbedtls_calloc( 1, buflen );
1059 if( buffer == NULL )
1060 {
1061 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1062 goto exit;
1063 }
1064 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1065 if( ret != 0 )
1066 goto exit;
1067 attributes->domain_parameters = buffer;
1068 attributes->domain_parameters_size = buflen;
1069
1070exit:
1071 mbedtls_mpi_free( &mpi );
1072 if( ret != 0 )
1073 mbedtls_free( buffer );
1074 return( mbedtls_to_psa_error( ret ) );
1075}
1076#endif /* MBEDTLS_RSA_C */
1077
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001078psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1079 psa_key_attributes_t *attributes )
1080{
1081 psa_key_slot_t *slot;
1082 psa_status_t status;
1083
1084 psa_reset_key_attributes( attributes );
1085
1086 status = psa_get_key_slot( handle, &slot );
1087 if( status != PSA_SUCCESS )
1088 return( status );
1089
1090 attributes->id = slot->persistent_storage_id;
1091 attributes->lifetime = slot->lifetime;
1092 attributes->policy = slot->policy;
1093 attributes->type = slot->type;
1094 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001095
1096 switch( slot->type )
1097 {
1098#if defined(MBEDTLS_RSA_C)
1099 case PSA_KEY_TYPE_RSA_KEYPAIR:
1100 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1101 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1102 break;
1103#endif
1104 default:
1105 /* Nothing else to do. */
1106 break;
1107 }
1108
1109 if( status != PSA_SUCCESS )
1110 psa_reset_key_attributes( attributes );
1111 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001112}
1113
Gilles Peskinec5487a82018-12-03 18:08:14 +01001114psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001115 psa_key_type_t *type,
1116 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001117{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001118 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001119 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001120
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001121 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001122 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001123 if( bits != NULL )
1124 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001125 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001126 if( status != PSA_SUCCESS )
1127 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001128
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001129 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001130 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001131 if( type != NULL )
1132 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001133 if( bits != NULL )
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001134 *bits = psa_get_key_slot_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001135 return( PSA_SUCCESS );
1136}
1137
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001138#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001139static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1140 unsigned char *buf, size_t size )
1141{
1142 int ret;
1143 unsigned char *c;
1144 size_t len = 0;
1145
1146 c = buf + size;
1147
1148 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1149
1150 return( (int) len );
1151}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001152#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001153
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001154static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1155 uint8_t *data,
1156 size_t data_size,
1157 size_t *data_length,
1158 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001159{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001160 *data_length = 0;
1161
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001162 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001163 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001164
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001165 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001166 {
1167 if( slot->data.raw.bytes > data_size )
1168 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001169 if( data_size != 0 )
1170 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001171 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001172 memset( data + slot->data.raw.bytes, 0,
1173 data_size - slot->data.raw.bytes );
1174 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001175 *data_length = slot->data.raw.bytes;
1176 return( PSA_SUCCESS );
1177 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001178#if defined(MBEDTLS_ECP_C)
1179 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1180 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001181 psa_status_t status;
1182
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001183 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001184 if( bytes > data_size )
1185 return( PSA_ERROR_BUFFER_TOO_SMALL );
1186 status = mbedtls_to_psa_error(
1187 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1188 if( status != PSA_SUCCESS )
1189 return( status );
1190 memset( data + bytes, 0, data_size - bytes );
1191 *data_length = bytes;
1192 return( PSA_SUCCESS );
1193 }
1194#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001195 else
Moran Peker17e36e12018-05-02 12:55:20 +03001196 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001197#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001198 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001199 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001200 {
Moran Pekera998bc62018-04-16 18:16:20 +03001201 mbedtls_pk_context pk;
1202 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001203 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001204 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001205#if defined(MBEDTLS_RSA_C)
1206 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001207 pk.pk_info = &mbedtls_rsa_info;
1208 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001209#else
1210 return( PSA_ERROR_NOT_SUPPORTED );
1211#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001212 }
1213 else
1214 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001215#if defined(MBEDTLS_ECP_C)
1216 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001217 pk.pk_info = &mbedtls_eckey_info;
1218 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001219#else
1220 return( PSA_ERROR_NOT_SUPPORTED );
1221#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001222 }
Moran Pekerd7326592018-05-29 16:56:39 +03001223 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001224 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001225 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001226 }
Moran Peker17e36e12018-05-02 12:55:20 +03001227 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001228 {
Moran Peker17e36e12018-05-02 12:55:20 +03001229 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001230 }
Moran Peker60364322018-04-29 11:34:58 +03001231 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001232 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001233 /* If data_size is 0 then data may be NULL and then the
1234 * call to memset would have undefined behavior. */
1235 if( data_size != 0 )
1236 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001237 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001238 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001239 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1240 * Move the data to the beginning and erase remaining data
1241 * at the original location. */
1242 if( 2 * (size_t) ret <= data_size )
1243 {
1244 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001245 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001246 }
1247 else if( (size_t) ret < data_size )
1248 {
1249 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001250 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001251 }
Moran Pekera998bc62018-04-16 18:16:20 +03001252 *data_length = ret;
1253 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001254 }
1255 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001256#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001257 {
1258 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001259 it is valid for a special-purpose implementation to omit
1260 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001261 return( PSA_ERROR_NOT_SUPPORTED );
1262 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001263 }
1264}
1265
Gilles Peskinec5487a82018-12-03 18:08:14 +01001266psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001267 uint8_t *data,
1268 size_t data_size,
1269 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001270{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001271 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001272 psa_status_t status;
1273
1274 /* Set the key to empty now, so that even when there are errors, we always
1275 * set data_length to a value between 0 and data_size. On error, setting
1276 * the key to empty is a good choice because an empty key representation is
1277 * unlikely to be accepted anywhere. */
1278 *data_length = 0;
1279
1280 /* Export requires the EXPORT flag. There is an exception for public keys,
1281 * which don't require any flag, but psa_get_key_from_slot takes
1282 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001283 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001284 if( status != PSA_SUCCESS )
1285 return( status );
1286 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001287 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001288}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001289
Gilles Peskinec5487a82018-12-03 18:08:14 +01001290psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001291 uint8_t *data,
1292 size_t data_size,
1293 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001294{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001295 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001296 psa_status_t status;
1297
1298 /* Set the key to empty now, so that even when there are errors, we always
1299 * set data_length to a value between 0 and data_size. On error, setting
1300 * the key to empty is a good choice because an empty key representation is
1301 * unlikely to be accepted anywhere. */
1302 *data_length = 0;
1303
1304 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001305 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001306 if( status != PSA_SUCCESS )
1307 return( status );
1308 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001309 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001310}
1311
Darryl Green0c6575a2018-11-07 16:05:30 +00001312#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001313static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001314 size_t bits )
1315{
1316 psa_status_t status;
1317 uint8_t *data;
1318 size_t key_length;
1319 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1320 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001321 if( data == NULL )
1322 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001323 /* Get key data in export format */
1324 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1325 if( status != PSA_SUCCESS )
1326 {
1327 slot->type = PSA_KEY_TYPE_NONE;
1328 goto exit;
1329 }
1330 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001331 status = psa_save_persistent_key( slot->persistent_storage_id,
1332 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001333 data, key_length );
1334 if( status != PSA_SUCCESS )
1335 {
1336 slot->type = PSA_KEY_TYPE_NONE;
1337 }
1338exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001339 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001340 mbedtls_free( data );
1341 return( status );
1342}
1343#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1344
Gilles Peskine4747d192019-04-17 15:05:45 +02001345static psa_status_t psa_set_key_policy_internal(
1346 psa_key_slot_t *slot,
1347 const psa_key_policy_t *policy )
1348{
1349 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
1350 PSA_KEY_USAGE_ENCRYPT |
1351 PSA_KEY_USAGE_DECRYPT |
1352 PSA_KEY_USAGE_SIGN |
1353 PSA_KEY_USAGE_VERIFY |
1354 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1355 return( PSA_ERROR_INVALID_ARGUMENT );
1356
1357 slot->policy = *policy;
1358 return( PSA_SUCCESS );
1359}
1360
1361/** Prepare a key slot to receive key material.
1362 *
1363 * This function allocates a key slot and sets its metadata.
1364 *
1365 * If this function fails, call psa_fail_key_creation().
1366 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001367 * This function is intended to be used as follows:
1368 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1369 * it with the specified attributes, and assign it a handle.
1370 * -# Populate the slot with the key material.
1371 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1372 * In case of failure at any step, stop the sequence and call
1373 * psa_fail_key_creation().
1374 *
Gilles Peskine4747d192019-04-17 15:05:45 +02001375 * \param attributes Key attributes for the new key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001376 * \param handle On success, a handle for the allocated slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001377 * \param p_slot On success, a pointer to the prepared slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001378 *
1379 * \retval #PSA_SUCCESS
1380 * The key slot is ready to receive key material.
1381 * \return If this function fails, the key slot is an invalid state.
1382 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001383 */
1384static psa_status_t psa_start_key_creation(
1385 const psa_key_attributes_t *attributes,
1386 psa_key_handle_t *handle,
1387 psa_key_slot_t **p_slot )
1388{
1389 psa_status_t status;
1390 psa_key_slot_t *slot;
1391
1392 status = psa_allocate_key( handle );
1393 if( status != PSA_SUCCESS )
1394 return( status );
1395 status = psa_get_key_slot( *handle, p_slot );
1396 if( status != PSA_SUCCESS )
1397 return( status );
1398 slot = *p_slot;
1399
1400 status = psa_set_key_policy_internal( slot, &attributes->policy );
1401 if( status != PSA_SUCCESS )
1402 return( status );
1403 slot->lifetime = attributes->lifetime;
1404 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001405 {
1406 status = psa_validate_persistent_key_parameters( attributes->lifetime,
1407 attributes->id );
1408 if( status != PSA_SUCCESS )
1409 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001410 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001411 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001412 slot->type = attributes->type;
1413
1414 return( status );
1415}
1416
1417/** Finalize the creation of a key once its key material has been set.
1418 *
1419 * This entails writing the key to persistent storage.
1420 *
1421 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001422 * See the documentation of psa_start_key_creation() for the intended use
1423 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001424 *
1425 * \param slot Pointer to the slot with key material.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001426 *
1427 * \retval #PSA_SUCCESS
1428 * The key was successfully created. The handle is now valid.
1429 * \return If this function fails, the key slot is an invalid state.
1430 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001431 */
1432static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1433{
1434 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001435 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001436
1437#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1438 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1439 {
1440 uint8_t *buffer = NULL;
1441 size_t buffer_size = 0;
1442 size_t length;
1443
1444 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001445 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001446 buffer = mbedtls_calloc( 1, buffer_size );
1447 if( buffer == NULL && buffer_size != 0 )
1448 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1449 status = psa_internal_export_key( slot,
1450 buffer, buffer_size, &length,
1451 0 );
1452
1453 if( status == PSA_SUCCESS )
1454 {
1455 status = psa_save_persistent_key( slot->persistent_storage_id,
1456 slot->type, &slot->policy,
1457 buffer, length );
1458 }
1459
1460 if( buffer_size != 0 )
1461 mbedtls_platform_zeroize( buffer, buffer_size );
1462 mbedtls_free( buffer );
1463 }
1464#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1465
1466 return( status );
1467}
1468
1469/** Abort the creation of a key.
1470 *
1471 * You may call this function after calling psa_start_key_creation(),
1472 * or after psa_finish_key_creation() fails. In other circumstances, this
1473 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001474 * See the documentation of psa_start_key_creation() for the intended use
1475 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001476 *
1477 * \param slot Pointer to the slot with key material.
1478 */
1479static void psa_fail_key_creation( psa_key_slot_t *slot )
1480{
1481 if( slot == NULL )
1482 return;
1483 psa_wipe_key_slot( slot );
1484}
1485
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001486static psa_status_t psa_check_key_slot_attributes(
1487 const psa_key_slot_t *slot,
1488 const psa_key_attributes_t *attributes )
1489{
1490 if( attributes->type != 0 )
1491 {
1492 if( attributes->type != slot->type )
1493 return( PSA_ERROR_INVALID_ARGUMENT );
1494 }
1495
1496 if( attributes->domain_parameters_size != 0 )
1497 {
1498#if defined(MBEDTLS_RSA_C)
1499 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1500 {
1501 mbedtls_mpi actual, required;
1502 int ret;
1503 mbedtls_mpi_init( &actual );
1504 mbedtls_mpi_init( &required );
1505 ret = mbedtls_rsa_export( slot->data.rsa,
1506 NULL, NULL, NULL, NULL, &actual );
1507 if( ret != 0 )
1508 goto rsa_exit;
1509 ret = mbedtls_mpi_read_binary( &required,
1510 attributes->domain_parameters,
1511 attributes->domain_parameters_size );
1512 if( ret != 0 )
1513 goto rsa_exit;
1514 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1515 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1516 rsa_exit:
1517 mbedtls_mpi_free( &actual );
1518 mbedtls_mpi_free( &required );
1519 if( ret != 0)
1520 return( mbedtls_to_psa_error( ret ) );
1521 }
1522 else
1523#endif
1524 {
1525 return( PSA_ERROR_INVALID_ARGUMENT );
1526 }
1527 }
1528
1529 if( attributes->bits != 0 )
1530 {
1531 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1532 return( PSA_ERROR_INVALID_ARGUMENT );
1533 }
1534
1535 return( PSA_SUCCESS );
1536}
1537
Gilles Peskine4747d192019-04-17 15:05:45 +02001538psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
1539 psa_key_handle_t *handle,
1540 const uint8_t *data,
1541 size_t data_length )
1542{
1543 psa_status_t status;
1544 psa_key_slot_t *slot = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001545
Gilles Peskine4747d192019-04-17 15:05:45 +02001546 status = psa_start_key_creation( attributes, handle, &slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001547 if( status != PSA_SUCCESS )
1548 goto exit;
1549
1550 status = psa_import_key_into_slot( slot, data, data_length );
1551 if( status != PSA_SUCCESS )
1552 goto exit;
1553 status = psa_check_key_slot_attributes( slot, attributes );
1554 if( status != PSA_SUCCESS )
1555 goto exit;
1556
1557 status = psa_finish_key_creation( slot );
1558exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001559 if( status != PSA_SUCCESS )
1560 {
1561 psa_fail_key_creation( slot );
1562 *handle = 0;
1563 }
1564 return( status );
1565}
1566
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001567static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001568 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001569{
1570 psa_status_t status;
1571 uint8_t *buffer = NULL;
1572 size_t buffer_size = 0;
1573 size_t length;
1574
1575 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001576 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001577 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001578 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001579 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001580 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1581 if( status != PSA_SUCCESS )
1582 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001583 target->type = source->type;
1584 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001585
1586exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001587 if( buffer_size != 0 )
1588 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001589 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001590 return( status );
1591}
1592
Gilles Peskine87a5e562019-04-17 12:28:25 +02001593psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001594 psa_key_handle_t target_handle,
1595 const psa_key_policy_t *constraint)
1596{
1597 psa_key_slot_t *source_slot = NULL;
1598 psa_key_slot_t *target_slot = NULL;
1599 psa_key_policy_t new_policy;
1600 psa_status_t status;
1601 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1602 if( status != PSA_SUCCESS )
1603 return( status );
1604 status = psa_get_empty_key_slot( target_handle, &target_slot );
1605 if( status != PSA_SUCCESS )
1606 return( status );
1607
1608 new_policy = target_slot->policy;
1609 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1610 if( status != PSA_SUCCESS )
1611 return( status );
1612 if( constraint != NULL )
1613 {
1614 status = psa_restrict_key_policy( &new_policy, constraint );
1615 if( status != PSA_SUCCESS )
1616 return( status );
1617 }
1618
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001619 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001620 if( status != PSA_SUCCESS )
1621 return( status );
1622
1623 target_slot->policy = new_policy;
1624 return( PSA_SUCCESS );
1625}
1626
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001627psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1628 const psa_key_attributes_t *specified_attributes,
1629 psa_key_handle_t *target_handle )
1630{
1631 psa_status_t status;
1632 psa_key_slot_t *source_slot = NULL;
1633 psa_key_slot_t *target_slot = NULL;
1634 psa_key_attributes_t actual_attributes = *specified_attributes;
1635
1636 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1637 if( status != PSA_SUCCESS )
1638 goto exit;
1639
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001640 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1641 if( status != PSA_SUCCESS )
1642 goto exit;
1643
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001644 status = psa_restrict_key_policy( &actual_attributes.policy,
1645 &source_slot->policy );
1646 if( status != PSA_SUCCESS )
1647 goto exit;
1648
1649 status = psa_start_key_creation( &actual_attributes,
1650 target_handle, &target_slot );
1651 if( status != PSA_SUCCESS )
1652 goto exit;
1653
1654 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001655 if( status != PSA_SUCCESS )
1656 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001657
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001658 status = psa_finish_key_creation( target_slot );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001659exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001660 if( status != PSA_SUCCESS )
1661 {
1662 psa_fail_key_creation( target_slot );
1663 *target_handle = 0;
1664 }
1665 return( status );
1666}
1667
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001668
1669
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001670/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001671/* Message digests */
1672/****************************************************************/
1673
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001674static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001675{
1676 switch( alg )
1677 {
1678#if defined(MBEDTLS_MD2_C)
1679 case PSA_ALG_MD2:
1680 return( &mbedtls_md2_info );
1681#endif
1682#if defined(MBEDTLS_MD4_C)
1683 case PSA_ALG_MD4:
1684 return( &mbedtls_md4_info );
1685#endif
1686#if defined(MBEDTLS_MD5_C)
1687 case PSA_ALG_MD5:
1688 return( &mbedtls_md5_info );
1689#endif
1690#if defined(MBEDTLS_RIPEMD160_C)
1691 case PSA_ALG_RIPEMD160:
1692 return( &mbedtls_ripemd160_info );
1693#endif
1694#if defined(MBEDTLS_SHA1_C)
1695 case PSA_ALG_SHA_1:
1696 return( &mbedtls_sha1_info );
1697#endif
1698#if defined(MBEDTLS_SHA256_C)
1699 case PSA_ALG_SHA_224:
1700 return( &mbedtls_sha224_info );
1701 case PSA_ALG_SHA_256:
1702 return( &mbedtls_sha256_info );
1703#endif
1704#if defined(MBEDTLS_SHA512_C)
1705 case PSA_ALG_SHA_384:
1706 return( &mbedtls_sha384_info );
1707 case PSA_ALG_SHA_512:
1708 return( &mbedtls_sha512_info );
1709#endif
1710 default:
1711 return( NULL );
1712 }
1713}
1714
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001715psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1716{
1717 switch( operation->alg )
1718 {
Gilles Peskine81736312018-06-26 15:04:31 +02001719 case 0:
1720 /* The object has (apparently) been initialized but it is not
1721 * in use. It's ok to call abort on such an object, and there's
1722 * nothing to do. */
1723 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001724#if defined(MBEDTLS_MD2_C)
1725 case PSA_ALG_MD2:
1726 mbedtls_md2_free( &operation->ctx.md2 );
1727 break;
1728#endif
1729#if defined(MBEDTLS_MD4_C)
1730 case PSA_ALG_MD4:
1731 mbedtls_md4_free( &operation->ctx.md4 );
1732 break;
1733#endif
1734#if defined(MBEDTLS_MD5_C)
1735 case PSA_ALG_MD5:
1736 mbedtls_md5_free( &operation->ctx.md5 );
1737 break;
1738#endif
1739#if defined(MBEDTLS_RIPEMD160_C)
1740 case PSA_ALG_RIPEMD160:
1741 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1742 break;
1743#endif
1744#if defined(MBEDTLS_SHA1_C)
1745 case PSA_ALG_SHA_1:
1746 mbedtls_sha1_free( &operation->ctx.sha1 );
1747 break;
1748#endif
1749#if defined(MBEDTLS_SHA256_C)
1750 case PSA_ALG_SHA_224:
1751 case PSA_ALG_SHA_256:
1752 mbedtls_sha256_free( &operation->ctx.sha256 );
1753 break;
1754#endif
1755#if defined(MBEDTLS_SHA512_C)
1756 case PSA_ALG_SHA_384:
1757 case PSA_ALG_SHA_512:
1758 mbedtls_sha512_free( &operation->ctx.sha512 );
1759 break;
1760#endif
1761 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001762 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001763 }
1764 operation->alg = 0;
1765 return( PSA_SUCCESS );
1766}
1767
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001768psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001769 psa_algorithm_t alg )
1770{
1771 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001772
1773 /* A context must be freshly initialized before it can be set up. */
1774 if( operation->alg != 0 )
1775 {
1776 return( PSA_ERROR_BAD_STATE );
1777 }
1778
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001779 switch( alg )
1780 {
1781#if defined(MBEDTLS_MD2_C)
1782 case PSA_ALG_MD2:
1783 mbedtls_md2_init( &operation->ctx.md2 );
1784 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1785 break;
1786#endif
1787#if defined(MBEDTLS_MD4_C)
1788 case PSA_ALG_MD4:
1789 mbedtls_md4_init( &operation->ctx.md4 );
1790 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1791 break;
1792#endif
1793#if defined(MBEDTLS_MD5_C)
1794 case PSA_ALG_MD5:
1795 mbedtls_md5_init( &operation->ctx.md5 );
1796 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1797 break;
1798#endif
1799#if defined(MBEDTLS_RIPEMD160_C)
1800 case PSA_ALG_RIPEMD160:
1801 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1802 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1803 break;
1804#endif
1805#if defined(MBEDTLS_SHA1_C)
1806 case PSA_ALG_SHA_1:
1807 mbedtls_sha1_init( &operation->ctx.sha1 );
1808 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1809 break;
1810#endif
1811#if defined(MBEDTLS_SHA256_C)
1812 case PSA_ALG_SHA_224:
1813 mbedtls_sha256_init( &operation->ctx.sha256 );
1814 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1815 break;
1816 case PSA_ALG_SHA_256:
1817 mbedtls_sha256_init( &operation->ctx.sha256 );
1818 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1819 break;
1820#endif
1821#if defined(MBEDTLS_SHA512_C)
1822 case PSA_ALG_SHA_384:
1823 mbedtls_sha512_init( &operation->ctx.sha512 );
1824 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1825 break;
1826 case PSA_ALG_SHA_512:
1827 mbedtls_sha512_init( &operation->ctx.sha512 );
1828 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1829 break;
1830#endif
1831 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001832 return( PSA_ALG_IS_HASH( alg ) ?
1833 PSA_ERROR_NOT_SUPPORTED :
1834 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001835 }
1836 if( ret == 0 )
1837 operation->alg = alg;
1838 else
1839 psa_hash_abort( operation );
1840 return( mbedtls_to_psa_error( ret ) );
1841}
1842
1843psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1844 const uint8_t *input,
1845 size_t input_length )
1846{
1847 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001848
1849 /* Don't require hash implementations to behave correctly on a
1850 * zero-length input, which may have an invalid pointer. */
1851 if( input_length == 0 )
1852 return( PSA_SUCCESS );
1853
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001854 switch( operation->alg )
1855 {
1856#if defined(MBEDTLS_MD2_C)
1857 case PSA_ALG_MD2:
1858 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1859 input, input_length );
1860 break;
1861#endif
1862#if defined(MBEDTLS_MD4_C)
1863 case PSA_ALG_MD4:
1864 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1865 input, input_length );
1866 break;
1867#endif
1868#if defined(MBEDTLS_MD5_C)
1869 case PSA_ALG_MD5:
1870 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1871 input, input_length );
1872 break;
1873#endif
1874#if defined(MBEDTLS_RIPEMD160_C)
1875 case PSA_ALG_RIPEMD160:
1876 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1877 input, input_length );
1878 break;
1879#endif
1880#if defined(MBEDTLS_SHA1_C)
1881 case PSA_ALG_SHA_1:
1882 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1883 input, input_length );
1884 break;
1885#endif
1886#if defined(MBEDTLS_SHA256_C)
1887 case PSA_ALG_SHA_224:
1888 case PSA_ALG_SHA_256:
1889 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1890 input, input_length );
1891 break;
1892#endif
1893#if defined(MBEDTLS_SHA512_C)
1894 case PSA_ALG_SHA_384:
1895 case PSA_ALG_SHA_512:
1896 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1897 input, input_length );
1898 break;
1899#endif
1900 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001901 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001902 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001903
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001904 if( ret != 0 )
1905 psa_hash_abort( operation );
1906 return( mbedtls_to_psa_error( ret ) );
1907}
1908
1909psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1910 uint8_t *hash,
1911 size_t hash_size,
1912 size_t *hash_length )
1913{
itayzafrir40835d42018-08-02 13:14:17 +03001914 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001915 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001916 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001917
1918 /* Fill the output buffer with something that isn't a valid hash
1919 * (barring an attack on the hash and deliberately-crafted input),
1920 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001921 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001922 /* If hash_size is 0 then hash may be NULL and then the
1923 * call to memset would have undefined behavior. */
1924 if( hash_size != 0 )
1925 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001926
1927 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001928 {
1929 status = PSA_ERROR_BUFFER_TOO_SMALL;
1930 goto exit;
1931 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001932
1933 switch( operation->alg )
1934 {
1935#if defined(MBEDTLS_MD2_C)
1936 case PSA_ALG_MD2:
1937 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1938 break;
1939#endif
1940#if defined(MBEDTLS_MD4_C)
1941 case PSA_ALG_MD4:
1942 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1943 break;
1944#endif
1945#if defined(MBEDTLS_MD5_C)
1946 case PSA_ALG_MD5:
1947 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1948 break;
1949#endif
1950#if defined(MBEDTLS_RIPEMD160_C)
1951 case PSA_ALG_RIPEMD160:
1952 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1953 break;
1954#endif
1955#if defined(MBEDTLS_SHA1_C)
1956 case PSA_ALG_SHA_1:
1957 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1958 break;
1959#endif
1960#if defined(MBEDTLS_SHA256_C)
1961 case PSA_ALG_SHA_224:
1962 case PSA_ALG_SHA_256:
1963 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1964 break;
1965#endif
1966#if defined(MBEDTLS_SHA512_C)
1967 case PSA_ALG_SHA_384:
1968 case PSA_ALG_SHA_512:
1969 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1970 break;
1971#endif
1972 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001973 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001974 }
itayzafrir40835d42018-08-02 13:14:17 +03001975 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001976
itayzafrir40835d42018-08-02 13:14:17 +03001977exit:
1978 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001979 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001980 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001981 return( psa_hash_abort( operation ) );
1982 }
1983 else
1984 {
1985 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001986 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001987 }
1988}
1989
Gilles Peskine2d277862018-06-18 15:41:12 +02001990psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1991 const uint8_t *hash,
1992 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001993{
1994 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1995 size_t actual_hash_length;
1996 psa_status_t status = psa_hash_finish( operation,
1997 actual_hash, sizeof( actual_hash ),
1998 &actual_hash_length );
1999 if( status != PSA_SUCCESS )
2000 return( status );
2001 if( actual_hash_length != hash_length )
2002 return( PSA_ERROR_INVALID_SIGNATURE );
2003 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2004 return( PSA_ERROR_INVALID_SIGNATURE );
2005 return( PSA_SUCCESS );
2006}
2007
Gilles Peskineeb35d782019-01-22 17:56:16 +01002008psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2009 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002010{
2011 if( target_operation->alg != 0 )
2012 return( PSA_ERROR_BAD_STATE );
2013
2014 switch( source_operation->alg )
2015 {
2016 case 0:
2017 return( PSA_ERROR_BAD_STATE );
2018#if defined(MBEDTLS_MD2_C)
2019 case PSA_ALG_MD2:
2020 mbedtls_md2_clone( &target_operation->ctx.md2,
2021 &source_operation->ctx.md2 );
2022 break;
2023#endif
2024#if defined(MBEDTLS_MD4_C)
2025 case PSA_ALG_MD4:
2026 mbedtls_md4_clone( &target_operation->ctx.md4,
2027 &source_operation->ctx.md4 );
2028 break;
2029#endif
2030#if defined(MBEDTLS_MD5_C)
2031 case PSA_ALG_MD5:
2032 mbedtls_md5_clone( &target_operation->ctx.md5,
2033 &source_operation->ctx.md5 );
2034 break;
2035#endif
2036#if defined(MBEDTLS_RIPEMD160_C)
2037 case PSA_ALG_RIPEMD160:
2038 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2039 &source_operation->ctx.ripemd160 );
2040 break;
2041#endif
2042#if defined(MBEDTLS_SHA1_C)
2043 case PSA_ALG_SHA_1:
2044 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2045 &source_operation->ctx.sha1 );
2046 break;
2047#endif
2048#if defined(MBEDTLS_SHA256_C)
2049 case PSA_ALG_SHA_224:
2050 case PSA_ALG_SHA_256:
2051 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2052 &source_operation->ctx.sha256 );
2053 break;
2054#endif
2055#if defined(MBEDTLS_SHA512_C)
2056 case PSA_ALG_SHA_384:
2057 case PSA_ALG_SHA_512:
2058 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2059 &source_operation->ctx.sha512 );
2060 break;
2061#endif
2062 default:
2063 return( PSA_ERROR_NOT_SUPPORTED );
2064 }
2065
2066 target_operation->alg = source_operation->alg;
2067 return( PSA_SUCCESS );
2068}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002069
2070
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002071/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002072/* MAC */
2073/****************************************************************/
2074
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002075static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002076 psa_algorithm_t alg,
2077 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002078 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002079 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002080{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002081 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002082 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002083
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002084 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002085 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002086
Gilles Peskine8c9def32018-02-08 10:02:12 +01002087 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2088 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002089 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002090 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002091 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002092 mode = MBEDTLS_MODE_STREAM;
2093 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002094 case PSA_ALG_CTR:
2095 mode = MBEDTLS_MODE_CTR;
2096 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002097 case PSA_ALG_CFB:
2098 mode = MBEDTLS_MODE_CFB;
2099 break;
2100 case PSA_ALG_OFB:
2101 mode = MBEDTLS_MODE_OFB;
2102 break;
2103 case PSA_ALG_CBC_NO_PADDING:
2104 mode = MBEDTLS_MODE_CBC;
2105 break;
2106 case PSA_ALG_CBC_PKCS7:
2107 mode = MBEDTLS_MODE_CBC;
2108 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002109 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002110 mode = MBEDTLS_MODE_CCM;
2111 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002112 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002113 mode = MBEDTLS_MODE_GCM;
2114 break;
2115 default:
2116 return( NULL );
2117 }
2118 }
2119 else if( alg == PSA_ALG_CMAC )
2120 mode = MBEDTLS_MODE_ECB;
2121 else if( alg == PSA_ALG_GMAC )
2122 mode = MBEDTLS_MODE_GCM;
2123 else
2124 return( NULL );
2125
2126 switch( key_type )
2127 {
2128 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002129 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002130 break;
2131 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002132 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2133 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002134 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002135 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002136 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002137 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002138 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2139 * but two-key Triple-DES is functionally three-key Triple-DES
2140 * with K1=K3, so that's how we present it to mbedtls. */
2141 if( key_bits == 128 )
2142 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002143 break;
2144 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002145 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002146 break;
2147 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002148 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002149 break;
2150 default:
2151 return( NULL );
2152 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002153 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002154 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002155
Jaeden Amero23bbb752018-06-26 14:16:54 +01002156 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2157 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002158}
2159
Gilles Peskinea05219c2018-11-16 16:02:56 +01002160#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002161static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002162{
Gilles Peskine2d277862018-06-18 15:41:12 +02002163 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002164 {
2165 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002166 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002167 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002168 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002169 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002170 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002171 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002172 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002173 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002174 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002175 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002176 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002177 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002178 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002179 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002180 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002181 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002182 return( 128 );
2183 default:
2184 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002185 }
2186}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002187#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002188
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002189/* Initialize the MAC operation structure. Once this function has been
2190 * called, psa_mac_abort can run and will do the right thing. */
2191static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2192 psa_algorithm_t alg )
2193{
2194 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2195
2196 operation->alg = alg;
2197 operation->key_set = 0;
2198 operation->iv_set = 0;
2199 operation->iv_required = 0;
2200 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002201 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002202
2203#if defined(MBEDTLS_CMAC_C)
2204 if( alg == PSA_ALG_CMAC )
2205 {
2206 operation->iv_required = 0;
2207 mbedtls_cipher_init( &operation->ctx.cmac );
2208 status = PSA_SUCCESS;
2209 }
2210 else
2211#endif /* MBEDTLS_CMAC_C */
2212#if defined(MBEDTLS_MD_C)
2213 if( PSA_ALG_IS_HMAC( operation->alg ) )
2214 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002215 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2216 operation->ctx.hmac.hash_ctx.alg = 0;
2217 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002218 }
2219 else
2220#endif /* MBEDTLS_MD_C */
2221 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002222 if( ! PSA_ALG_IS_MAC( alg ) )
2223 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002224 }
2225
2226 if( status != PSA_SUCCESS )
2227 memset( operation, 0, sizeof( *operation ) );
2228 return( status );
2229}
2230
Gilles Peskine01126fa2018-07-12 17:04:55 +02002231#if defined(MBEDTLS_MD_C)
2232static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2233{
Gilles Peskine3f108122018-12-07 18:14:53 +01002234 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002235 return( psa_hash_abort( &hmac->hash_ctx ) );
2236}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002237
2238static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2239{
2240 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2241 memset( hmac, 0, sizeof( *hmac ) );
2242}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002243#endif /* MBEDTLS_MD_C */
2244
Gilles Peskine8c9def32018-02-08 10:02:12 +01002245psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2246{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002247 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002248 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002249 /* The object has (apparently) been initialized but it is not
2250 * in use. It's ok to call abort on such an object, and there's
2251 * nothing to do. */
2252 return( PSA_SUCCESS );
2253 }
2254 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002255#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002256 if( operation->alg == PSA_ALG_CMAC )
2257 {
2258 mbedtls_cipher_free( &operation->ctx.cmac );
2259 }
2260 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002261#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002262#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002263 if( PSA_ALG_IS_HMAC( operation->alg ) )
2264 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002265 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002266 }
2267 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002268#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002269 {
2270 /* Sanity check (shouldn't happen: operation->alg should
2271 * always have been initialized to a valid value). */
2272 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002273 }
Moran Peker41deec42018-04-04 15:43:05 +03002274
Gilles Peskine8c9def32018-02-08 10:02:12 +01002275 operation->alg = 0;
2276 operation->key_set = 0;
2277 operation->iv_set = 0;
2278 operation->iv_required = 0;
2279 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002280 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002281
Gilles Peskine8c9def32018-02-08 10:02:12 +01002282 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002283
2284bad_state:
2285 /* If abort is called on an uninitialized object, we can't trust
2286 * anything. Wipe the object in case it contains confidential data.
2287 * This may result in a memory leak if a pointer gets overwritten,
2288 * but it's too late to do anything about this. */
2289 memset( operation, 0, sizeof( *operation ) );
2290 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002291}
2292
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002293#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002294static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002295 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002296 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002297 const mbedtls_cipher_info_t *cipher_info )
2298{
2299 int ret;
2300
2301 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002302
2303 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2304 if( ret != 0 )
2305 return( ret );
2306
2307 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2308 slot->data.raw.data,
2309 key_bits );
2310 return( ret );
2311}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002312#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002313
Gilles Peskine248051a2018-06-20 16:09:38 +02002314#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002315static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2316 const uint8_t *key,
2317 size_t key_length,
2318 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002319{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002320 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002321 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002322 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002323 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002324 psa_status_t status;
2325
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002326 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2327 * overflow below. This should never trigger if the hash algorithm
2328 * is implemented correctly. */
2329 /* The size checks against the ipad and opad buffers cannot be written
2330 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2331 * because that triggers -Wlogical-op on GCC 7.3. */
2332 if( block_size > sizeof( ipad ) )
2333 return( PSA_ERROR_NOT_SUPPORTED );
2334 if( block_size > sizeof( hmac->opad ) )
2335 return( PSA_ERROR_NOT_SUPPORTED );
2336 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002337 return( PSA_ERROR_NOT_SUPPORTED );
2338
Gilles Peskined223b522018-06-11 18:12:58 +02002339 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002340 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002341 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002342 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002343 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002344 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002345 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002346 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002347 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002348 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002349 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002350 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002351 }
Gilles Peskine96889972018-07-12 17:07:03 +02002352 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2353 * but it is permitted. It is common when HMAC is used in HKDF, for
2354 * example. Don't call `memcpy` in the 0-length because `key` could be
2355 * an invalid pointer which would make the behavior undefined. */
2356 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002357 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002358
Gilles Peskined223b522018-06-11 18:12:58 +02002359 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2360 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002361 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002362 ipad[i] ^= 0x36;
2363 memset( ipad + key_length, 0x36, block_size - key_length );
2364
2365 /* Copy the key material from ipad to opad, flipping the requisite bits,
2366 * and filling the rest of opad with the requisite constant. */
2367 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002368 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2369 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002370
Gilles Peskine01126fa2018-07-12 17:04:55 +02002371 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002372 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002373 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002374
Gilles Peskine01126fa2018-07-12 17:04:55 +02002375 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002376
2377cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002378 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002379
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002380 return( status );
2381}
Gilles Peskine248051a2018-06-20 16:09:38 +02002382#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002383
Gilles Peskine89167cb2018-07-08 20:12:23 +02002384static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002385 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002386 psa_algorithm_t alg,
2387 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002388{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002389 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002390 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002391 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002392 psa_key_usage_t usage =
2393 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002394 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002395 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002396
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002397 /* A context must be freshly initialized before it can be set up. */
2398 if( operation->alg != 0 )
2399 {
2400 return( PSA_ERROR_BAD_STATE );
2401 }
2402
Gilles Peskined911eb72018-08-14 15:18:45 +02002403 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002404 if( status != PSA_SUCCESS )
2405 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002406 if( is_sign )
2407 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002408
Gilles Peskinec5487a82018-12-03 18:08:14 +01002409 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002410 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002411 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002412 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002413
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002415 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002416 {
2417 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002418 mbedtls_cipher_info_from_psa( full_length_alg,
2419 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002420 int ret;
2421 if( cipher_info == NULL )
2422 {
2423 status = PSA_ERROR_NOT_SUPPORTED;
2424 goto exit;
2425 }
2426 operation->mac_size = cipher_info->block_size;
2427 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2428 status = mbedtls_to_psa_error( ret );
2429 }
2430 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002433 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002434 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002435 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002436 if( hash_alg == 0 )
2437 {
2438 status = PSA_ERROR_NOT_SUPPORTED;
2439 goto exit;
2440 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002441
2442 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2443 /* Sanity check. This shouldn't fail on a valid configuration. */
2444 if( operation->mac_size == 0 ||
2445 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2446 {
2447 status = PSA_ERROR_NOT_SUPPORTED;
2448 goto exit;
2449 }
2450
Gilles Peskine01126fa2018-07-12 17:04:55 +02002451 if( slot->type != PSA_KEY_TYPE_HMAC )
2452 {
2453 status = PSA_ERROR_INVALID_ARGUMENT;
2454 goto exit;
2455 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002456
Gilles Peskine01126fa2018-07-12 17:04:55 +02002457 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2458 slot->data.raw.data,
2459 slot->data.raw.bytes,
2460 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002461 }
2462 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002463#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002464 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002465 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002466 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002467 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002468
Gilles Peskined911eb72018-08-14 15:18:45 +02002469 if( truncated == 0 )
2470 {
2471 /* The "normal" case: untruncated algorithm. Nothing to do. */
2472 }
2473 else if( truncated < 4 )
2474 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002475 /* A very short MAC is too short for security since it can be
2476 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2477 * so we make this our minimum, even though 32 bits is still
2478 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002479 status = PSA_ERROR_NOT_SUPPORTED;
2480 }
2481 else if( truncated > operation->mac_size )
2482 {
2483 /* It's impossible to "truncate" to a larger length. */
2484 status = PSA_ERROR_INVALID_ARGUMENT;
2485 }
2486 else
2487 operation->mac_size = truncated;
2488
Gilles Peskinefbfac682018-07-08 20:51:54 +02002489exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002490 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002492 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002494 else
2495 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002496 operation->key_set = 1;
2497 }
2498 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499}
2500
Gilles Peskine89167cb2018-07-08 20:12:23 +02002501psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002502 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002503 psa_algorithm_t alg )
2504{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002505 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002506}
2507
2508psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002509 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002510 psa_algorithm_t alg )
2511{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002512 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002513}
2514
Gilles Peskine8c9def32018-02-08 10:02:12 +01002515psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2516 const uint8_t *input,
2517 size_t input_length )
2518{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002519 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002521 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002522 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002523 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524 operation->has_input = 1;
2525
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002527 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002528 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002529 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2530 input, input_length );
2531 status = mbedtls_to_psa_error( ret );
2532 }
2533 else
2534#endif /* MBEDTLS_CMAC_C */
2535#if defined(MBEDTLS_MD_C)
2536 if( PSA_ALG_IS_HMAC( operation->alg ) )
2537 {
2538 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2539 input_length );
2540 }
2541 else
2542#endif /* MBEDTLS_MD_C */
2543 {
2544 /* This shouldn't happen if `operation` was initialized by
2545 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002546 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002547 }
2548
Gilles Peskinefbfac682018-07-08 20:51:54 +02002549 if( status != PSA_SUCCESS )
2550 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002551 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552}
2553
Gilles Peskine01126fa2018-07-12 17:04:55 +02002554#if defined(MBEDTLS_MD_C)
2555static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2556 uint8_t *mac,
2557 size_t mac_size )
2558{
2559 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2560 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2561 size_t hash_size = 0;
2562 size_t block_size = psa_get_hash_block_size( hash_alg );
2563 psa_status_t status;
2564
Gilles Peskine01126fa2018-07-12 17:04:55 +02002565 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2566 if( status != PSA_SUCCESS )
2567 return( status );
2568 /* From here on, tmp needs to be wiped. */
2569
2570 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2571 if( status != PSA_SUCCESS )
2572 goto exit;
2573
2574 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2575 if( status != PSA_SUCCESS )
2576 goto exit;
2577
2578 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2579 if( status != PSA_SUCCESS )
2580 goto exit;
2581
Gilles Peskined911eb72018-08-14 15:18:45 +02002582 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2583 if( status != PSA_SUCCESS )
2584 goto exit;
2585
2586 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002587
2588exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002589 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002590 return( status );
2591}
2592#endif /* MBEDTLS_MD_C */
2593
mohammad16036df908f2018-04-02 08:34:15 -07002594static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002595 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002596 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002598 if( ! operation->key_set )
2599 return( PSA_ERROR_BAD_STATE );
2600 if( operation->iv_required && ! operation->iv_set )
2601 return( PSA_ERROR_BAD_STATE );
2602
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603 if( mac_size < operation->mac_size )
2604 return( PSA_ERROR_BUFFER_TOO_SMALL );
2605
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002607 if( operation->alg == PSA_ALG_CMAC )
2608 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002609 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2610 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2611 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002612 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002613 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002614 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002615 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002616 else
2617#endif /* MBEDTLS_CMAC_C */
2618#if defined(MBEDTLS_MD_C)
2619 if( PSA_ALG_IS_HMAC( operation->alg ) )
2620 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002621 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002622 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002623 }
2624 else
2625#endif /* MBEDTLS_MD_C */
2626 {
2627 /* This shouldn't happen if `operation` was initialized by
2628 * a setup function. */
2629 return( PSA_ERROR_BAD_STATE );
2630 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631}
2632
Gilles Peskineacd4be32018-07-08 19:56:25 +02002633psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2634 uint8_t *mac,
2635 size_t mac_size,
2636 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002637{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002638 psa_status_t status;
2639
Jaeden Amero252ef282019-02-15 14:05:35 +00002640 if( operation->alg == 0 )
2641 {
2642 return( PSA_ERROR_BAD_STATE );
2643 }
2644
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002645 /* Fill the output buffer with something that isn't a valid mac
2646 * (barring an attack on the mac and deliberately-crafted input),
2647 * in case the caller doesn't check the return status properly. */
2648 *mac_length = mac_size;
2649 /* If mac_size is 0 then mac may be NULL and then the
2650 * call to memset would have undefined behavior. */
2651 if( mac_size != 0 )
2652 memset( mac, '!', mac_size );
2653
Gilles Peskine89167cb2018-07-08 20:12:23 +02002654 if( ! operation->is_sign )
2655 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002656 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002657 }
mohammad16036df908f2018-04-02 08:34:15 -07002658
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002659 status = psa_mac_finish_internal( operation, mac, mac_size );
2660
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002661 if( status == PSA_SUCCESS )
2662 {
2663 status = psa_mac_abort( operation );
2664 if( status == PSA_SUCCESS )
2665 *mac_length = operation->mac_size;
2666 else
2667 memset( mac, '!', mac_size );
2668 }
2669 else
2670 psa_mac_abort( operation );
2671 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002672}
2673
Gilles Peskineacd4be32018-07-08 19:56:25 +02002674psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2675 const uint8_t *mac,
2676 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002677{
Gilles Peskine828ed142018-06-18 23:25:51 +02002678 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002679 psa_status_t status;
2680
Jaeden Amero252ef282019-02-15 14:05:35 +00002681 if( operation->alg == 0 )
2682 {
2683 return( PSA_ERROR_BAD_STATE );
2684 }
2685
Gilles Peskine89167cb2018-07-08 20:12:23 +02002686 if( operation->is_sign )
2687 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002688 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002689 }
2690 if( operation->mac_size != mac_length )
2691 {
2692 status = PSA_ERROR_INVALID_SIGNATURE;
2693 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002694 }
mohammad16036df908f2018-04-02 08:34:15 -07002695
2696 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002697 actual_mac, sizeof( actual_mac ) );
2698
2699 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2700 status = PSA_ERROR_INVALID_SIGNATURE;
2701
2702cleanup:
2703 if( status == PSA_SUCCESS )
2704 status = psa_mac_abort( operation );
2705 else
2706 psa_mac_abort( operation );
2707
Gilles Peskine3f108122018-12-07 18:14:53 +01002708 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002709
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002710 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002711}
2712
2713
Gilles Peskine20035e32018-02-03 22:44:14 +01002714
Gilles Peskine20035e32018-02-03 22:44:14 +01002715/****************************************************************/
2716/* Asymmetric cryptography */
2717/****************************************************************/
2718
Gilles Peskine2b450e32018-06-27 15:42:46 +02002719#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002720/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002721 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002722static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2723 size_t hash_length,
2724 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002725{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002726 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002727 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002728 *md_alg = mbedtls_md_get_type( md_info );
2729
2730 /* The Mbed TLS RSA module uses an unsigned int for hash length
2731 * parameters. Validate that it fits so that we don't risk an
2732 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002733#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002734 if( hash_length > UINT_MAX )
2735 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002736#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002737
2738#if defined(MBEDTLS_PKCS1_V15)
2739 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2740 * must be correct. */
2741 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2742 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002743 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002744 if( md_info == NULL )
2745 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002746 if( mbedtls_md_get_size( md_info ) != hash_length )
2747 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002748 }
2749#endif /* MBEDTLS_PKCS1_V15 */
2750
2751#if defined(MBEDTLS_PKCS1_V21)
2752 /* PSS requires a hash internally. */
2753 if( PSA_ALG_IS_RSA_PSS( alg ) )
2754 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002755 if( md_info == NULL )
2756 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002757 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002758#endif /* MBEDTLS_PKCS1_V21 */
2759
Gilles Peskine61b91d42018-06-08 16:09:36 +02002760 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002761}
2762
Gilles Peskine2b450e32018-06-27 15:42:46 +02002763static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2764 psa_algorithm_t alg,
2765 const uint8_t *hash,
2766 size_t hash_length,
2767 uint8_t *signature,
2768 size_t signature_size,
2769 size_t *signature_length )
2770{
2771 psa_status_t status;
2772 int ret;
2773 mbedtls_md_type_t md_alg;
2774
2775 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2776 if( status != PSA_SUCCESS )
2777 return( status );
2778
Gilles Peskine630a18a2018-06-29 17:49:35 +02002779 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002780 return( PSA_ERROR_BUFFER_TOO_SMALL );
2781
2782#if defined(MBEDTLS_PKCS1_V15)
2783 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2784 {
2785 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2786 MBEDTLS_MD_NONE );
2787 ret = mbedtls_rsa_pkcs1_sign( rsa,
2788 mbedtls_ctr_drbg_random,
2789 &global_data.ctr_drbg,
2790 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002791 md_alg,
2792 (unsigned int) hash_length,
2793 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002794 signature );
2795 }
2796 else
2797#endif /* MBEDTLS_PKCS1_V15 */
2798#if defined(MBEDTLS_PKCS1_V21)
2799 if( PSA_ALG_IS_RSA_PSS( alg ) )
2800 {
2801 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2802 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2803 mbedtls_ctr_drbg_random,
2804 &global_data.ctr_drbg,
2805 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002806 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002807 (unsigned int) hash_length,
2808 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002809 signature );
2810 }
2811 else
2812#endif /* MBEDTLS_PKCS1_V21 */
2813 {
2814 return( PSA_ERROR_INVALID_ARGUMENT );
2815 }
2816
2817 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002818 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002819 return( mbedtls_to_psa_error( ret ) );
2820}
2821
2822static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2823 psa_algorithm_t alg,
2824 const uint8_t *hash,
2825 size_t hash_length,
2826 const uint8_t *signature,
2827 size_t signature_length )
2828{
2829 psa_status_t status;
2830 int ret;
2831 mbedtls_md_type_t md_alg;
2832
2833 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2834 if( status != PSA_SUCCESS )
2835 return( status );
2836
Gilles Peskine630a18a2018-06-29 17:49:35 +02002837 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002838 return( PSA_ERROR_BUFFER_TOO_SMALL );
2839
2840#if defined(MBEDTLS_PKCS1_V15)
2841 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2842 {
2843 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2844 MBEDTLS_MD_NONE );
2845 ret = mbedtls_rsa_pkcs1_verify( rsa,
2846 mbedtls_ctr_drbg_random,
2847 &global_data.ctr_drbg,
2848 MBEDTLS_RSA_PUBLIC,
2849 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002850 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002851 hash,
2852 signature );
2853 }
2854 else
2855#endif /* MBEDTLS_PKCS1_V15 */
2856#if defined(MBEDTLS_PKCS1_V21)
2857 if( PSA_ALG_IS_RSA_PSS( alg ) )
2858 {
2859 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2860 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2861 mbedtls_ctr_drbg_random,
2862 &global_data.ctr_drbg,
2863 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002864 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002865 (unsigned int) hash_length,
2866 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002867 signature );
2868 }
2869 else
2870#endif /* MBEDTLS_PKCS1_V21 */
2871 {
2872 return( PSA_ERROR_INVALID_ARGUMENT );
2873 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002874
2875 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2876 * the rest of the signature is invalid". This has little use in
2877 * practice and PSA doesn't report this distinction. */
2878 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2879 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002880 return( mbedtls_to_psa_error( ret ) );
2881}
2882#endif /* MBEDTLS_RSA_C */
2883
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002884#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002885/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2886 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2887 * (even though these functions don't modify it). */
2888static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2889 psa_algorithm_t alg,
2890 const uint8_t *hash,
2891 size_t hash_length,
2892 uint8_t *signature,
2893 size_t signature_size,
2894 size_t *signature_length )
2895{
2896 int ret;
2897 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002898 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002899 mbedtls_mpi_init( &r );
2900 mbedtls_mpi_init( &s );
2901
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002902 if( signature_size < 2 * curve_bytes )
2903 {
2904 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2905 goto cleanup;
2906 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002907
Gilles Peskinea05219c2018-11-16 16:02:56 +01002908#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002909 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2910 {
2911 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2912 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2913 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2914 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2915 hash, hash_length,
2916 md_alg ) );
2917 }
2918 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002919#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002920 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002921 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002922 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2923 hash, hash_length,
2924 mbedtls_ctr_drbg_random,
2925 &global_data.ctr_drbg ) );
2926 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002927
2928 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2929 signature,
2930 curve_bytes ) );
2931 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2932 signature + curve_bytes,
2933 curve_bytes ) );
2934
2935cleanup:
2936 mbedtls_mpi_free( &r );
2937 mbedtls_mpi_free( &s );
2938 if( ret == 0 )
2939 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002940 return( mbedtls_to_psa_error( ret ) );
2941}
2942
2943static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2944 const uint8_t *hash,
2945 size_t hash_length,
2946 const uint8_t *signature,
2947 size_t signature_length )
2948{
2949 int ret;
2950 mbedtls_mpi r, s;
2951 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2952 mbedtls_mpi_init( &r );
2953 mbedtls_mpi_init( &s );
2954
2955 if( signature_length != 2 * curve_bytes )
2956 return( PSA_ERROR_INVALID_SIGNATURE );
2957
2958 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2959 signature,
2960 curve_bytes ) );
2961 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2962 signature + curve_bytes,
2963 curve_bytes ) );
2964
2965 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2966 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002967
2968cleanup:
2969 mbedtls_mpi_free( &r );
2970 mbedtls_mpi_free( &s );
2971 return( mbedtls_to_psa_error( ret ) );
2972}
2973#endif /* MBEDTLS_ECDSA_C */
2974
Gilles Peskinec5487a82018-12-03 18:08:14 +01002975psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002976 psa_algorithm_t alg,
2977 const uint8_t *hash,
2978 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002979 uint8_t *signature,
2980 size_t signature_size,
2981 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002982{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002983 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002984 psa_status_t status;
2985
2986 *signature_length = signature_size;
2987
Gilles Peskinec5487a82018-12-03 18:08:14 +01002988 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002989 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002990 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002991 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002992 {
2993 status = PSA_ERROR_INVALID_ARGUMENT;
2994 goto exit;
2995 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002996
Gilles Peskine20035e32018-02-03 22:44:14 +01002997#if defined(MBEDTLS_RSA_C)
2998 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2999 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003000 status = psa_rsa_sign( slot->data.rsa,
3001 alg,
3002 hash, hash_length,
3003 signature, signature_size,
3004 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003005 }
3006 else
3007#endif /* defined(MBEDTLS_RSA_C) */
3008#if defined(MBEDTLS_ECP_C)
3009 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3010 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003011#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003012 if(
3013#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3014 PSA_ALG_IS_ECDSA( alg )
3015#else
3016 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3017#endif
3018 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003019 status = psa_ecdsa_sign( slot->data.ecp,
3020 alg,
3021 hash, hash_length,
3022 signature, signature_size,
3023 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003024 else
3025#endif /* defined(MBEDTLS_ECDSA_C) */
3026 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003027 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003028 }
itayzafrir5c753392018-05-08 11:18:38 +03003029 }
3030 else
3031#endif /* defined(MBEDTLS_ECP_C) */
3032 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003033 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003034 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003035
3036exit:
3037 /* Fill the unused part of the output buffer (the whole buffer on error,
3038 * the trailing part on success) with something that isn't a valid mac
3039 * (barring an attack on the mac and deliberately-crafted input),
3040 * in case the caller doesn't check the return status properly. */
3041 if( status == PSA_SUCCESS )
3042 memset( signature + *signature_length, '!',
3043 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003044 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003045 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003046 /* If signature_size is 0 then we have nothing to do. We must not call
3047 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003048 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003049}
3050
Gilles Peskinec5487a82018-12-03 18:08:14 +01003051psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003052 psa_algorithm_t alg,
3053 const uint8_t *hash,
3054 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003055 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003056 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003057{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003058 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003059 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003060
Gilles Peskinec5487a82018-12-03 18:08:14 +01003061 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003062 if( status != PSA_SUCCESS )
3063 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003064
Gilles Peskine61b91d42018-06-08 16:09:36 +02003065#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003066 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003067 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003068 return( psa_rsa_verify( slot->data.rsa,
3069 alg,
3070 hash, hash_length,
3071 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003072 }
3073 else
3074#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003075#if defined(MBEDTLS_ECP_C)
3076 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3077 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003078#if defined(MBEDTLS_ECDSA_C)
3079 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003080 return( psa_ecdsa_verify( slot->data.ecp,
3081 hash, hash_length,
3082 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003083 else
3084#endif /* defined(MBEDTLS_ECDSA_C) */
3085 {
3086 return( PSA_ERROR_INVALID_ARGUMENT );
3087 }
itayzafrir5c753392018-05-08 11:18:38 +03003088 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003089 else
3090#endif /* defined(MBEDTLS_ECP_C) */
3091 {
3092 return( PSA_ERROR_NOT_SUPPORTED );
3093 }
3094}
3095
Gilles Peskine072ac562018-06-30 00:21:29 +02003096#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3097static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3098 mbedtls_rsa_context *rsa )
3099{
3100 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3101 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3102 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3103 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3104}
3105#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3106
Gilles Peskinec5487a82018-12-03 18:08:14 +01003107psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003108 psa_algorithm_t alg,
3109 const uint8_t *input,
3110 size_t input_length,
3111 const uint8_t *salt,
3112 size_t salt_length,
3113 uint8_t *output,
3114 size_t output_size,
3115 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003116{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003117 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003118 psa_status_t status;
3119
Darryl Green5cc689a2018-07-24 15:34:10 +01003120 (void) input;
3121 (void) input_length;
3122 (void) salt;
3123 (void) output;
3124 (void) output_size;
3125
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003126 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003127
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003128 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3129 return( PSA_ERROR_INVALID_ARGUMENT );
3130
Gilles Peskinec5487a82018-12-03 18:08:14 +01003131 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003132 if( status != PSA_SUCCESS )
3133 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003134 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
3135 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003136 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003137
3138#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003139 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003140 {
3141 mbedtls_rsa_context *rsa = slot->data.rsa;
3142 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003143 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003144 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003145#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003146 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003148 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3149 mbedtls_ctr_drbg_random,
3150 &global_data.ctr_drbg,
3151 MBEDTLS_RSA_PUBLIC,
3152 input_length,
3153 input,
3154 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003155 }
3156 else
3157#endif /* MBEDTLS_PKCS1_V15 */
3158#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003159 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003160 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003161 psa_rsa_oaep_set_padding_mode( alg, rsa );
3162 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3163 mbedtls_ctr_drbg_random,
3164 &global_data.ctr_drbg,
3165 MBEDTLS_RSA_PUBLIC,
3166 salt, salt_length,
3167 input_length,
3168 input,
3169 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003170 }
3171 else
3172#endif /* MBEDTLS_PKCS1_V21 */
3173 {
3174 return( PSA_ERROR_INVALID_ARGUMENT );
3175 }
3176 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003177 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178 return( mbedtls_to_psa_error( ret ) );
3179 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003180 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003181#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003182 {
3183 return( PSA_ERROR_NOT_SUPPORTED );
3184 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003185}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003186
Gilles Peskinec5487a82018-12-03 18:08:14 +01003187psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003188 psa_algorithm_t alg,
3189 const uint8_t *input,
3190 size_t input_length,
3191 const uint8_t *salt,
3192 size_t salt_length,
3193 uint8_t *output,
3194 size_t output_size,
3195 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003197 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003198 psa_status_t status;
3199
Darryl Green5cc689a2018-07-24 15:34:10 +01003200 (void) input;
3201 (void) input_length;
3202 (void) salt;
3203 (void) output;
3204 (void) output_size;
3205
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003206 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003207
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003208 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3209 return( PSA_ERROR_INVALID_ARGUMENT );
3210
Gilles Peskinec5487a82018-12-03 18:08:14 +01003211 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003212 if( status != PSA_SUCCESS )
3213 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003214 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
3215 return( PSA_ERROR_INVALID_ARGUMENT );
3216
3217#if defined(MBEDTLS_RSA_C)
3218 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
3219 {
3220 mbedtls_rsa_context *rsa = slot->data.rsa;
3221 int ret;
3222
Gilles Peskine630a18a2018-06-29 17:49:35 +02003223 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003224 return( PSA_ERROR_INVALID_ARGUMENT );
3225
3226#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003227 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003228 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003229 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3230 mbedtls_ctr_drbg_random,
3231 &global_data.ctr_drbg,
3232 MBEDTLS_RSA_PRIVATE,
3233 output_length,
3234 input,
3235 output,
3236 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237 }
3238 else
3239#endif /* MBEDTLS_PKCS1_V15 */
3240#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003241 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003243 psa_rsa_oaep_set_padding_mode( alg, rsa );
3244 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3245 mbedtls_ctr_drbg_random,
3246 &global_data.ctr_drbg,
3247 MBEDTLS_RSA_PRIVATE,
3248 salt, salt_length,
3249 output_length,
3250 input,
3251 output,
3252 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253 }
3254 else
3255#endif /* MBEDTLS_PKCS1_V21 */
3256 {
3257 return( PSA_ERROR_INVALID_ARGUMENT );
3258 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003259
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003260 return( mbedtls_to_psa_error( ret ) );
3261 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003263#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003264 {
3265 return( PSA_ERROR_NOT_SUPPORTED );
3266 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003267}
Gilles Peskine20035e32018-02-03 22:44:14 +01003268
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003269
3270
mohammad1603503973b2018-03-12 15:59:30 +02003271/****************************************************************/
3272/* Symmetric cryptography */
3273/****************************************************************/
3274
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003275/* Initialize the cipher operation structure. Once this function has been
3276 * called, psa_cipher_abort can run and will do the right thing. */
3277static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3278 psa_algorithm_t alg )
3279{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003280 if( ! PSA_ALG_IS_CIPHER( alg ) )
3281 {
3282 memset( operation, 0, sizeof( *operation ) );
3283 return( PSA_ERROR_INVALID_ARGUMENT );
3284 }
3285
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003286 operation->alg = alg;
3287 operation->key_set = 0;
3288 operation->iv_set = 0;
3289 operation->iv_required = 1;
3290 operation->iv_size = 0;
3291 operation->block_size = 0;
3292 mbedtls_cipher_init( &operation->ctx.cipher );
3293 return( PSA_SUCCESS );
3294}
3295
Gilles Peskinee553c652018-06-04 16:22:46 +02003296static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003297 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003298 psa_algorithm_t alg,
3299 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003300{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003301 int ret = 0;
3302 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003303 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003304 size_t key_bits;
3305 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003306 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3307 PSA_KEY_USAGE_ENCRYPT :
3308 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003309
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003310 /* A context must be freshly initialized before it can be set up. */
3311 if( operation->alg != 0 )
3312 {
3313 return( PSA_ERROR_BAD_STATE );
3314 }
3315
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003316 status = psa_cipher_init( operation, alg );
3317 if( status != PSA_SUCCESS )
3318 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003319
Gilles Peskinec5487a82018-12-03 18:08:14 +01003320 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003321 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003322 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003323 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003324
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003325 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003326 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003327 {
3328 status = PSA_ERROR_NOT_SUPPORTED;
3329 goto exit;
3330 }
mohammad1603503973b2018-03-12 15:59:30 +02003331
mohammad1603503973b2018-03-12 15:59:30 +02003332 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003333 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003334 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003335
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003336#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003337 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003338 {
3339 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3340 unsigned char keys[24];
3341 memcpy( keys, slot->data.raw.data, 16 );
3342 memcpy( keys + 16, slot->data.raw.data, 8 );
3343 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3344 keys,
3345 192, cipher_operation );
3346 }
3347 else
3348#endif
3349 {
3350 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3351 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003352 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003353 }
Moran Peker41deec42018-04-04 15:43:05 +03003354 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003355 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003356
mohammad16038481e742018-03-18 13:57:31 +02003357#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003358 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003359 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003360 case PSA_ALG_CBC_NO_PADDING:
3361 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3362 MBEDTLS_PADDING_NONE );
3363 break;
3364 case PSA_ALG_CBC_PKCS7:
3365 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3366 MBEDTLS_PADDING_PKCS7 );
3367 break;
3368 default:
3369 /* The algorithm doesn't involve padding. */
3370 ret = 0;
3371 break;
3372 }
3373 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003374 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003375#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3376
mohammad1603503973b2018-03-12 15:59:30 +02003377 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003378 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3379 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3380 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003381 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003382 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003383 }
mohammad1603503973b2018-03-12 15:59:30 +02003384
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003385exit:
3386 if( status == 0 )
3387 status = mbedtls_to_psa_error( ret );
3388 if( status != 0 )
3389 psa_cipher_abort( operation );
3390 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003391}
3392
Gilles Peskinefe119512018-07-08 21:39:34 +02003393psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003394 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003395 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003396{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003397 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003398}
3399
Gilles Peskinefe119512018-07-08 21:39:34 +02003400psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003401 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003402 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003403{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003404 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003405}
3406
Gilles Peskinefe119512018-07-08 21:39:34 +02003407psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3408 unsigned char *iv,
3409 size_t iv_size,
3410 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003411{
itayzafrir534bd7c2018-08-02 13:56:32 +03003412 psa_status_t status;
3413 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003414 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003415 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003416 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003417 }
Moran Peker41deec42018-04-04 15:43:05 +03003418 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003419 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003420 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003421 goto exit;
3422 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003423 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3424 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003425 if( ret != 0 )
3426 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003427 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003428 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003429 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003430
mohammad16038481e742018-03-18 13:57:31 +02003431 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003432 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003433
Moran Peker395db872018-05-31 14:07:14 +03003434exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003435 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003436 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003437 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003438}
3439
Gilles Peskinefe119512018-07-08 21:39:34 +02003440psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3441 const unsigned char *iv,
3442 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003443{
itayzafrir534bd7c2018-08-02 13:56:32 +03003444 psa_status_t status;
3445 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003446 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003447 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003448 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003449 }
Moran Pekera28258c2018-05-29 16:25:04 +03003450 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003451 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003452 status = PSA_ERROR_INVALID_ARGUMENT;
3453 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003454 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003455 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3456 status = mbedtls_to_psa_error( ret );
3457exit:
3458 if( status == PSA_SUCCESS )
3459 operation->iv_set = 1;
3460 else
mohammad1603503973b2018-03-12 15:59:30 +02003461 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003462 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003463}
3464
Gilles Peskinee553c652018-06-04 16:22:46 +02003465psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3466 const uint8_t *input,
3467 size_t input_length,
3468 unsigned char *output,
3469 size_t output_size,
3470 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003471{
itayzafrir534bd7c2018-08-02 13:56:32 +03003472 psa_status_t status;
3473 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003474 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003475
3476 if( operation->alg == 0 )
3477 {
3478 return( PSA_ERROR_BAD_STATE );
3479 }
3480
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003481 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003482 {
3483 /* Take the unprocessed partial block left over from previous
3484 * update calls, if any, plus the input to this call. Remove
3485 * the last partial block, if any. You get the data that will be
3486 * output in this call. */
3487 expected_output_size =
3488 ( operation->ctx.cipher.unprocessed_len + input_length )
3489 / operation->block_size * operation->block_size;
3490 }
3491 else
3492 {
3493 expected_output_size = input_length;
3494 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003495
Gilles Peskine89d789c2018-06-04 17:17:16 +02003496 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003497 {
3498 status = PSA_ERROR_BUFFER_TOO_SMALL;
3499 goto exit;
3500 }
mohammad160382759612018-03-12 18:16:40 +02003501
mohammad1603503973b2018-03-12 15:59:30 +02003502 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003503 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003504 status = mbedtls_to_psa_error( ret );
3505exit:
3506 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003507 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003508 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003509}
3510
Gilles Peskinee553c652018-06-04 16:22:46 +02003511psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3512 uint8_t *output,
3513 size_t output_size,
3514 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003515{
David Saadab4ecc272019-02-14 13:48:10 +02003516 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003517 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003518 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003519
mohammad1603503973b2018-03-12 15:59:30 +02003520 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003521 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003522 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003523 }
3524 if( operation->iv_required && ! operation->iv_set )
3525 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003526 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003527 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003528
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003529 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003530 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3531 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003532 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003533 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003534 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003535 }
3536
Janos Follath315b51c2018-07-09 16:04:51 +01003537 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3538 temp_output_buffer,
3539 output_length );
3540 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003541 {
Janos Follath315b51c2018-07-09 16:04:51 +01003542 status = mbedtls_to_psa_error( cipher_ret );
3543 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003544 }
Janos Follath315b51c2018-07-09 16:04:51 +01003545
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003546 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003547 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003548 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003549 memcpy( output, temp_output_buffer, *output_length );
3550 else
3551 {
Janos Follath315b51c2018-07-09 16:04:51 +01003552 status = PSA_ERROR_BUFFER_TOO_SMALL;
3553 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003554 }
mohammad1603503973b2018-03-12 15:59:30 +02003555
Gilles Peskine3f108122018-12-07 18:14:53 +01003556 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003557 status = psa_cipher_abort( operation );
3558
3559 return( status );
3560
3561error:
3562
3563 *output_length = 0;
3564
Gilles Peskine3f108122018-12-07 18:14:53 +01003565 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003566 (void) psa_cipher_abort( operation );
3567
3568 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003569}
3570
Gilles Peskinee553c652018-06-04 16:22:46 +02003571psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3572{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003573 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003574 {
3575 /* The object has (apparently) been initialized but it is not
3576 * in use. It's ok to call abort on such an object, and there's
3577 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003578 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003579 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003580
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003581 /* Sanity check (shouldn't happen: operation->alg should
3582 * always have been initialized to a valid value). */
3583 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3584 return( PSA_ERROR_BAD_STATE );
3585
mohammad1603503973b2018-03-12 15:59:30 +02003586 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003587
Moran Peker41deec42018-04-04 15:43:05 +03003588 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003589 operation->key_set = 0;
3590 operation->iv_set = 0;
3591 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003592 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003593 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003594
Moran Peker395db872018-05-31 14:07:14 +03003595 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003596}
3597
Gilles Peskinea0655c32018-04-30 17:06:50 +02003598
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003599
mohammad16038cc1cee2018-03-28 01:21:33 +03003600/****************************************************************/
3601/* Key Policy */
3602/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003603
mohammad160327010052018-07-03 13:16:15 +03003604#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003605void psa_key_policy_set_usage( psa_key_policy_t *policy,
3606 psa_key_usage_t usage,
3607 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003608{
mohammad16034eed7572018-03-28 05:14:59 -07003609 policy->usage = usage;
3610 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003611}
3612
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003613psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003614{
mohammad16036df908f2018-04-02 08:34:15 -07003615 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003616}
3617
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003618psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003619{
mohammad16036df908f2018-04-02 08:34:15 -07003620 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003621}
mohammad160327010052018-07-03 13:16:15 +03003622#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003623
Gilles Peskinec5487a82018-12-03 18:08:14 +01003624psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003625 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003626{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003627 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003628 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003629
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003630 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003631 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003632
Gilles Peskinec5487a82018-12-03 18:08:14 +01003633 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003634 if( status != PSA_SUCCESS )
3635 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003636
Gilles Peskine4747d192019-04-17 15:05:45 +02003637 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003638}
3639
Gilles Peskinec5487a82018-12-03 18:08:14 +01003640psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003641 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003642{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003643 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003644 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003645
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003646 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003647 return( PSA_ERROR_INVALID_ARGUMENT );
3648
Gilles Peskinec5487a82018-12-03 18:08:14 +01003649 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003650 if( status != PSA_SUCCESS )
3651 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003652
mohammad16036df908f2018-04-02 08:34:15 -07003653 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003654
3655 return( PSA_SUCCESS );
3656}
Gilles Peskine20035e32018-02-03 22:44:14 +01003657
Gilles Peskinea0655c32018-04-30 17:06:50 +02003658
3659
mohammad1603804cd712018-03-20 22:44:08 +02003660/****************************************************************/
3661/* Key Lifetime */
3662/****************************************************************/
3663
Gilles Peskine87a5e562019-04-17 12:28:25 +02003664psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003665 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003666{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003667 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003668 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003669
Gilles Peskinec5487a82018-12-03 18:08:14 +01003670 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003671 if( status != PSA_SUCCESS )
3672 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003673
mohammad1603804cd712018-03-20 22:44:08 +02003674 *lifetime = slot->lifetime;
3675
3676 return( PSA_SUCCESS );
3677}
3678
Gilles Peskine20035e32018-02-03 22:44:14 +01003679
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003680
mohammad16035955c982018-04-26 00:53:03 +03003681/****************************************************************/
3682/* AEAD */
3683/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003684
Gilles Peskineedf9a652018-08-17 18:11:56 +02003685typedef struct
3686{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003687 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003688 const mbedtls_cipher_info_t *cipher_info;
3689 union
3690 {
3691#if defined(MBEDTLS_CCM_C)
3692 mbedtls_ccm_context ccm;
3693#endif /* MBEDTLS_CCM_C */
3694#if defined(MBEDTLS_GCM_C)
3695 mbedtls_gcm_context gcm;
3696#endif /* MBEDTLS_GCM_C */
3697 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003698 psa_algorithm_t core_alg;
3699 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003700 uint8_t tag_length;
3701} aead_operation_t;
3702
Gilles Peskine30a9e412019-01-14 18:36:12 +01003703static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003704{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003705 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003706 {
3707#if defined(MBEDTLS_CCM_C)
3708 case PSA_ALG_CCM:
3709 mbedtls_ccm_free( &operation->ctx.ccm );
3710 break;
3711#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003712#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003713 case PSA_ALG_GCM:
3714 mbedtls_gcm_free( &operation->ctx.gcm );
3715 break;
3716#endif /* MBEDTLS_GCM_C */
3717 }
3718}
3719
3720static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003721 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003722 psa_key_usage_t usage,
3723 psa_algorithm_t alg )
3724{
3725 psa_status_t status;
3726 size_t key_bits;
3727 mbedtls_cipher_id_t cipher_id;
3728
Gilles Peskinec5487a82018-12-03 18:08:14 +01003729 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003730 if( status != PSA_SUCCESS )
3731 return( status );
3732
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003733 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003734
3735 operation->cipher_info =
3736 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3737 &cipher_id );
3738 if( operation->cipher_info == NULL )
3739 return( PSA_ERROR_NOT_SUPPORTED );
3740
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003741 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003742 {
3743#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003744 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3745 operation->core_alg = PSA_ALG_CCM;
3746 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003747 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3748 return( PSA_ERROR_INVALID_ARGUMENT );
3749 mbedtls_ccm_init( &operation->ctx.ccm );
3750 status = mbedtls_to_psa_error(
3751 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3752 operation->slot->data.raw.data,
3753 (unsigned int) key_bits ) );
3754 if( status != 0 )
3755 goto cleanup;
3756 break;
3757#endif /* MBEDTLS_CCM_C */
3758
3759#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003760 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3761 operation->core_alg = PSA_ALG_GCM;
3762 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003763 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3764 return( PSA_ERROR_INVALID_ARGUMENT );
3765 mbedtls_gcm_init( &operation->ctx.gcm );
3766 status = mbedtls_to_psa_error(
3767 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3768 operation->slot->data.raw.data,
3769 (unsigned int) key_bits ) );
3770 break;
3771#endif /* MBEDTLS_GCM_C */
3772
3773 default:
3774 return( PSA_ERROR_NOT_SUPPORTED );
3775 }
3776
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003777 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3778 {
3779 status = PSA_ERROR_INVALID_ARGUMENT;
3780 goto cleanup;
3781 }
3782 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3783 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3784 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3785 * In both cases, mbedtls_xxx will validate the tag length below. */
3786
Gilles Peskineedf9a652018-08-17 18:11:56 +02003787 return( PSA_SUCCESS );
3788
3789cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003790 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003791 return( status );
3792}
3793
Gilles Peskinec5487a82018-12-03 18:08:14 +01003794psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003795 psa_algorithm_t alg,
3796 const uint8_t *nonce,
3797 size_t nonce_length,
3798 const uint8_t *additional_data,
3799 size_t additional_data_length,
3800 const uint8_t *plaintext,
3801 size_t plaintext_length,
3802 uint8_t *ciphertext,
3803 size_t ciphertext_size,
3804 size_t *ciphertext_length )
3805{
mohammad16035955c982018-04-26 00:53:03 +03003806 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003807 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003808 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003809
mohammad1603f08a5502018-06-03 15:05:47 +03003810 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003811
Gilles Peskinec5487a82018-12-03 18:08:14 +01003812 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003813 if( status != PSA_SUCCESS )
3814 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003815
Gilles Peskineedf9a652018-08-17 18:11:56 +02003816 /* For all currently supported modes, the tag is at the end of the
3817 * ciphertext. */
3818 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3819 {
3820 status = PSA_ERROR_BUFFER_TOO_SMALL;
3821 goto exit;
3822 }
3823 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003824
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003825#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003826 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003827 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003828 status = mbedtls_to_psa_error(
3829 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3830 MBEDTLS_GCM_ENCRYPT,
3831 plaintext_length,
3832 nonce, nonce_length,
3833 additional_data, additional_data_length,
3834 plaintext, ciphertext,
3835 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003836 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003837 else
3838#endif /* MBEDTLS_GCM_C */
3839#if defined(MBEDTLS_CCM_C)
3840 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003841 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003842 status = mbedtls_to_psa_error(
3843 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3844 plaintext_length,
3845 nonce, nonce_length,
3846 additional_data,
3847 additional_data_length,
3848 plaintext, ciphertext,
3849 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003850 }
mohammad16035c8845f2018-05-09 05:40:09 -07003851 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003852#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003853 {
mohammad1603554faad2018-06-03 15:07:38 +03003854 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003855 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003856
Gilles Peskineedf9a652018-08-17 18:11:56 +02003857 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3858 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003859
Gilles Peskineedf9a652018-08-17 18:11:56 +02003860exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003861 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003862 if( status == PSA_SUCCESS )
3863 *ciphertext_length = plaintext_length + operation.tag_length;
3864 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003865}
3866
Gilles Peskineee652a32018-06-01 19:23:52 +02003867/* Locate the tag in a ciphertext buffer containing the encrypted data
3868 * followed by the tag. Return the length of the part preceding the tag in
3869 * *plaintext_length. This is the size of the plaintext in modes where
3870 * the encrypted data has the same size as the plaintext, such as
3871 * CCM and GCM. */
3872static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3873 const uint8_t *ciphertext,
3874 size_t ciphertext_length,
3875 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003876 const uint8_t **p_tag )
3877{
3878 size_t payload_length;
3879 if( tag_length > ciphertext_length )
3880 return( PSA_ERROR_INVALID_ARGUMENT );
3881 payload_length = ciphertext_length - tag_length;
3882 if( payload_length > plaintext_size )
3883 return( PSA_ERROR_BUFFER_TOO_SMALL );
3884 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003885 return( PSA_SUCCESS );
3886}
3887
Gilles Peskinec5487a82018-12-03 18:08:14 +01003888psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003889 psa_algorithm_t alg,
3890 const uint8_t *nonce,
3891 size_t nonce_length,
3892 const uint8_t *additional_data,
3893 size_t additional_data_length,
3894 const uint8_t *ciphertext,
3895 size_t ciphertext_length,
3896 uint8_t *plaintext,
3897 size_t plaintext_size,
3898 size_t *plaintext_length )
3899{
mohammad16035955c982018-04-26 00:53:03 +03003900 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003901 aead_operation_t operation;
3902 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003903
Gilles Peskineee652a32018-06-01 19:23:52 +02003904 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003905
Gilles Peskinec5487a82018-12-03 18:08:14 +01003906 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003907 if( status != PSA_SUCCESS )
3908 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003909
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003910#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003911 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003912 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003913 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003914 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003915 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003916 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003917 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003918
Gilles Peskineedf9a652018-08-17 18:11:56 +02003919 status = mbedtls_to_psa_error(
3920 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3921 ciphertext_length - operation.tag_length,
3922 nonce, nonce_length,
3923 additional_data,
3924 additional_data_length,
3925 tag, operation.tag_length,
3926 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003927 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003928 else
3929#endif /* MBEDTLS_GCM_C */
3930#if defined(MBEDTLS_CCM_C)
3931 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003932 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003933 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003934 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003935 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003936 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003937 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003938
Gilles Peskineedf9a652018-08-17 18:11:56 +02003939 status = mbedtls_to_psa_error(
3940 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3941 ciphertext_length - operation.tag_length,
3942 nonce, nonce_length,
3943 additional_data,
3944 additional_data_length,
3945 ciphertext, plaintext,
3946 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003947 }
mohammad160339574652018-06-01 04:39:53 -07003948 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003949#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003950 {
mohammad1603554faad2018-06-03 15:07:38 +03003951 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003952 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003953
Gilles Peskineedf9a652018-08-17 18:11:56 +02003954 if( status != PSA_SUCCESS && plaintext_size != 0 )
3955 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003956
Gilles Peskineedf9a652018-08-17 18:11:56 +02003957exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003958 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003959 if( status == PSA_SUCCESS )
3960 *plaintext_length = ciphertext_length - operation.tag_length;
3961 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003962}
3963
Gilles Peskinea0655c32018-04-30 17:06:50 +02003964
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003965
Gilles Peskine20035e32018-02-03 22:44:14 +01003966/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003967/* Generators */
3968/****************************************************************/
3969
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003970#define HKDF_STATE_INIT 0 /* no input yet */
3971#define HKDF_STATE_STARTED 1 /* got salt */
3972#define HKDF_STATE_KEYED 2 /* got key */
3973#define HKDF_STATE_OUTPUT 3 /* output started */
3974
Gilles Peskine969c5d62019-01-16 15:53:06 +01003975static psa_algorithm_t psa_generator_get_kdf_alg(
3976 const psa_crypto_generator_t *generator )
3977{
3978 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3979 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3980 else
3981 return( generator->alg );
3982}
3983
3984
Gilles Peskineeab56e42018-07-12 17:12:33 +02003985psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3986{
3987 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003988 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3989 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003990 {
3991 /* The object has (apparently) been initialized but it is not
3992 * in use. It's ok to call abort on such an object, and there's
3993 * nothing to do. */
3994 }
3995 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003996 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003997 {
3998 if( generator->ctx.buffer.data != NULL )
3999 {
Gilles Peskine3f108122018-12-07 18:14:53 +01004000 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02004001 generator->ctx.buffer.size );
4002 mbedtls_free( generator->ctx.buffer.data );
4003 }
4004 }
4005 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004006#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004007 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004008 {
4009 mbedtls_free( generator->ctx.hkdf.info );
4010 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
4011 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004012 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00004013 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004014 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004015 {
4016 if( generator->ctx.tls12_prf.key != NULL )
4017 {
Gilles Peskine3f108122018-12-07 18:14:53 +01004018 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004019 generator->ctx.tls12_prf.key_len );
4020 mbedtls_free( generator->ctx.tls12_prf.key );
4021 }
Hanno Becker580fba12018-11-13 20:50:45 +00004022
4023 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
4024 {
Gilles Peskine3f108122018-12-07 18:14:53 +01004025 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004026 generator->ctx.tls12_prf.Ai_with_seed_len );
4027 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
4028 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004029 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004030 else
4031#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004032 {
4033 status = PSA_ERROR_BAD_STATE;
4034 }
4035 memset( generator, 0, sizeof( *generator ) );
4036 return( status );
4037}
4038
Gilles Peskineeab56e42018-07-12 17:12:33 +02004039psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
4040 size_t *capacity)
4041{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004042 if( generator->alg == 0 )
4043 {
4044 /* This is a blank generator. */
4045 return PSA_ERROR_BAD_STATE;
4046 }
4047
Gilles Peskineeab56e42018-07-12 17:12:33 +02004048 *capacity = generator->capacity;
4049 return( PSA_SUCCESS );
4050}
4051
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004052psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
4053 size_t capacity )
4054{
4055 if( generator->alg == 0 )
4056 return( PSA_ERROR_BAD_STATE );
4057 if( capacity > generator->capacity )
4058 return( PSA_ERROR_INVALID_ARGUMENT );
4059 generator->capacity = capacity;
4060 return( PSA_SUCCESS );
4061}
4062
Gilles Peskinebef7f142018-07-12 17:22:21 +02004063#if defined(MBEDTLS_MD_C)
4064/* Read some bytes from an HKDF-based generator. This performs a chunk
4065 * of the expand phase of the HKDF algorithm. */
4066static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
4067 psa_algorithm_t hash_alg,
4068 uint8_t *output,
4069 size_t output_length )
4070{
4071 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4072 psa_status_t status;
4073
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004074 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4075 return( PSA_ERROR_BAD_STATE );
4076 hkdf->state = HKDF_STATE_OUTPUT;
4077
Gilles Peskinebef7f142018-07-12 17:22:21 +02004078 while( output_length != 0 )
4079 {
4080 /* Copy what remains of the current block */
4081 uint8_t n = hash_length - hkdf->offset_in_block;
4082 if( n > output_length )
4083 n = (uint8_t) output_length;
4084 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4085 output += n;
4086 output_length -= n;
4087 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004088 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004089 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004090 /* We can't be wanting more output after block 0xff, otherwise
4091 * the capacity check in psa_generator_read() would have
4092 * prevented this call. It could happen only if the generator
4093 * object was corrupted or if this function is called directly
4094 * inside the library. */
4095 if( hkdf->block_number == 0xff )
4096 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004097
4098 /* We need a new block */
4099 ++hkdf->block_number;
4100 hkdf->offset_in_block = 0;
4101 status = psa_hmac_setup_internal( &hkdf->hmac,
4102 hkdf->prk, hash_length,
4103 hash_alg );
4104 if( status != PSA_SUCCESS )
4105 return( status );
4106 if( hkdf->block_number != 1 )
4107 {
4108 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4109 hkdf->output_block,
4110 hash_length );
4111 if( status != PSA_SUCCESS )
4112 return( status );
4113 }
4114 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4115 hkdf->info,
4116 hkdf->info_length );
4117 if( status != PSA_SUCCESS )
4118 return( status );
4119 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4120 &hkdf->block_number, 1 );
4121 if( status != PSA_SUCCESS )
4122 return( status );
4123 status = psa_hmac_finish_internal( &hkdf->hmac,
4124 hkdf->output_block,
4125 sizeof( hkdf->output_block ) );
4126 if( status != PSA_SUCCESS )
4127 return( status );
4128 }
4129
4130 return( PSA_SUCCESS );
4131}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004132
4133static psa_status_t psa_generator_tls12_prf_generate_next_block(
4134 psa_tls12_prf_generator_t *tls12_prf,
4135 psa_algorithm_t alg )
4136{
4137 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4138 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4139 psa_hmac_internal_data hmac;
4140 psa_status_t status, cleanup_status;
4141
Hanno Becker3b339e22018-11-13 20:56:14 +00004142 unsigned char *Ai;
4143 size_t Ai_len;
4144
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004145 /* We can't be wanting more output after block 0xff, otherwise
4146 * the capacity check in psa_generator_read() would have
4147 * prevented this call. It could happen only if the generator
4148 * object was corrupted or if this function is called directly
4149 * inside the library. */
4150 if( tls12_prf->block_number == 0xff )
4151 return( PSA_ERROR_BAD_STATE );
4152
4153 /* We need a new block */
4154 ++tls12_prf->block_number;
4155 tls12_prf->offset_in_block = 0;
4156
4157 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4158 *
4159 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4160 *
4161 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4162 * HMAC_hash(secret, A(2) + seed) +
4163 * HMAC_hash(secret, A(3) + seed) + ...
4164 *
4165 * A(0) = seed
4166 * A(i) = HMAC_hash( secret, A(i-1) )
4167 *
4168 * The `psa_tls12_prf_generator` structures saves the block
4169 * `HMAC_hash(secret, A(i) + seed)` from which the output
4170 * is currently extracted as `output_block`, while
4171 * `A(i) + seed` is stored in `Ai_with_seed`.
4172 *
4173 * Generating a new block means recalculating `Ai_with_seed`
4174 * from the A(i)-part of it, and afterwards recalculating
4175 * `output_block`.
4176 *
4177 * A(0) is computed at setup time.
4178 *
4179 */
4180
4181 psa_hmac_init_internal( &hmac );
4182
4183 /* We must distinguish the calculation of A(1) from those
4184 * of A(2) and higher, because A(0)=seed has a different
4185 * length than the other A(i). */
4186 if( tls12_prf->block_number == 1 )
4187 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004188 Ai = tls12_prf->Ai_with_seed + hash_length;
4189 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004190 }
4191 else
4192 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004193 Ai = tls12_prf->Ai_with_seed;
4194 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004195 }
4196
Hanno Becker3b339e22018-11-13 20:56:14 +00004197 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4198 status = psa_hmac_setup_internal( &hmac,
4199 tls12_prf->key,
4200 tls12_prf->key_len,
4201 hash_alg );
4202 if( status != PSA_SUCCESS )
4203 goto cleanup;
4204
4205 status = psa_hash_update( &hmac.hash_ctx,
4206 Ai, Ai_len );
4207 if( status != PSA_SUCCESS )
4208 goto cleanup;
4209
4210 status = psa_hmac_finish_internal( &hmac,
4211 tls12_prf->Ai_with_seed,
4212 hash_length );
4213 if( status != PSA_SUCCESS )
4214 goto cleanup;
4215
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004216 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4217 status = psa_hmac_setup_internal( &hmac,
4218 tls12_prf->key,
4219 tls12_prf->key_len,
4220 hash_alg );
4221 if( status != PSA_SUCCESS )
4222 goto cleanup;
4223
4224 status = psa_hash_update( &hmac.hash_ctx,
4225 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004226 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004227 if( status != PSA_SUCCESS )
4228 goto cleanup;
4229
4230 status = psa_hmac_finish_internal( &hmac,
4231 tls12_prf->output_block,
4232 hash_length );
4233 if( status != PSA_SUCCESS )
4234 goto cleanup;
4235
4236cleanup:
4237
4238 cleanup_status = psa_hmac_abort_internal( &hmac );
4239 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4240 status = cleanup_status;
4241
4242 return( status );
4243}
4244
4245/* Read some bytes from an TLS-1.2-PRF-based generator.
4246 * See Section 5 of RFC 5246. */
4247static psa_status_t psa_generator_tls12_prf_read(
4248 psa_tls12_prf_generator_t *tls12_prf,
4249 psa_algorithm_t alg,
4250 uint8_t *output,
4251 size_t output_length )
4252{
4253 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4254 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4255 psa_status_t status;
4256
4257 while( output_length != 0 )
4258 {
4259 /* Copy what remains of the current block */
4260 uint8_t n = hash_length - tls12_prf->offset_in_block;
4261
4262 /* Check if we have fully processed the current block. */
4263 if( n == 0 )
4264 {
4265 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
4266 alg );
4267 if( status != PSA_SUCCESS )
4268 return( status );
4269
4270 continue;
4271 }
4272
4273 if( n > output_length )
4274 n = (uint8_t) output_length;
4275 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4276 n );
4277 output += n;
4278 output_length -= n;
4279 tls12_prf->offset_in_block += n;
4280 }
4281
4282 return( PSA_SUCCESS );
4283}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004284#endif /* MBEDTLS_MD_C */
4285
Gilles Peskineeab56e42018-07-12 17:12:33 +02004286psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
4287 uint8_t *output,
4288 size_t output_length )
4289{
4290 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004291 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004292
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004293 if( generator->alg == 0 )
4294 {
4295 /* This is a blank generator. */
4296 return PSA_ERROR_BAD_STATE;
4297 }
4298
Gilles Peskineeab56e42018-07-12 17:12:33 +02004299 if( output_length > generator->capacity )
4300 {
4301 generator->capacity = 0;
4302 /* Go through the error path to wipe all confidential data now
4303 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004304 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004305 goto exit;
4306 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004307 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004308 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004309 /* Edge case: this is a finished generator, and 0 bytes
4310 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004311 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4312 * INSUFFICIENT_CAPACITY, which is right for a finished
4313 * generator, for consistency with the case when
4314 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004315 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004316 }
4317 generator->capacity -= output_length;
4318
Gilles Peskine969c5d62019-01-16 15:53:06 +01004319 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004320 {
Gilles Peskine211a4362018-10-25 22:22:31 +02004321 /* Initially, the capacity of a selection generator is always
4322 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
4323 * abbreviated in this comment as `size`. When the remaining
4324 * capacity is `c`, the next bytes to serve start `c` bytes
4325 * from the end of the buffer, i.e. `size - c` from the
4326 * beginning of the buffer. Since `generator->capacity` was just
4327 * decremented above, we need to serve the bytes from
4328 * `size - generator->capacity - output_length` to
4329 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004330 size_t offset =
4331 generator->ctx.buffer.size - generator->capacity - output_length;
4332 memcpy( output, generator->ctx.buffer.data + offset, output_length );
4333 status = PSA_SUCCESS;
4334 }
4335 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004336#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004337 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004338 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004339 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004340 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
4341 output, output_length );
4342 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004343 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4344 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004345 {
4346 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004347 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004348 output_length );
4349 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004350 else
4351#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004352 {
4353 return( PSA_ERROR_BAD_STATE );
4354 }
4355
4356exit:
4357 if( status != PSA_SUCCESS )
4358 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004359 /* Preserve the algorithm upon errors, but clear all sensitive state.
4360 * This allows us to differentiate between exhausted generators and
4361 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
4362 * generators. */
4363 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004364 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004365 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004366 memset( output, '!', output_length );
4367 }
4368 return( status );
4369}
4370
Gilles Peskine08542d82018-07-19 17:05:42 +02004371#if defined(MBEDTLS_DES_C)
4372static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4373{
4374 if( data_size >= 8 )
4375 mbedtls_des_key_set_parity( data );
4376 if( data_size >= 16 )
4377 mbedtls_des_key_set_parity( data + 8 );
4378 if( data_size >= 24 )
4379 mbedtls_des_key_set_parity( data + 16 );
4380}
4381#endif /* MBEDTLS_DES_C */
4382
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004383static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004384 psa_key_slot_t *slot,
4385 size_t bits,
4386 psa_crypto_generator_t *generator )
4387{
4388 uint8_t *data = NULL;
4389 size_t bytes = PSA_BITS_TO_BYTES( bits );
4390 psa_status_t status;
4391
4392 if( ! key_type_is_raw_bytes( slot->type ) )
4393 return( PSA_ERROR_INVALID_ARGUMENT );
4394 if( bits % 8 != 0 )
4395 return( PSA_ERROR_INVALID_ARGUMENT );
4396 data = mbedtls_calloc( 1, bytes );
4397 if( data == NULL )
4398 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4399
4400 status = psa_generator_read( generator, data, bytes );
4401 if( status != PSA_SUCCESS )
4402 goto exit;
4403#if defined(MBEDTLS_DES_C)
4404 if( slot->type == PSA_KEY_TYPE_DES )
4405 psa_des_set_key_parity( data, bytes );
4406#endif /* MBEDTLS_DES_C */
4407 status = psa_import_key_into_slot( slot, data, bytes );
4408
4409exit:
4410 mbedtls_free( data );
4411 return( status );
4412}
4413
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004414psa_status_t psa_generate_derived_key( const psa_key_attributes_t *attributes,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004415 psa_key_handle_t *handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004416 psa_crypto_generator_t *generator )
4417{
4418 psa_status_t status;
4419 psa_key_slot_t *slot = NULL;
4420 status = psa_start_key_creation( attributes, handle, &slot );
4421 if( status == PSA_SUCCESS )
4422 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004423 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004424 attributes->bits,
4425 generator );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004426 }
4427 if( status == PSA_SUCCESS )
4428 status = psa_finish_key_creation( slot );
4429 if( status != PSA_SUCCESS )
4430 {
4431 psa_fail_key_creation( slot );
4432 *handle = 0;
4433 }
4434 return( status );
4435}
4436
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004437psa_status_t psa_generate_derived_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004438 psa_key_type_t type,
4439 size_t bits,
4440 psa_crypto_generator_t *generator )
4441{
4442 uint8_t *data = NULL;
4443 size_t bytes = PSA_BITS_TO_BYTES( bits );
4444 psa_status_t status;
4445
4446 if( ! key_type_is_raw_bytes( type ) )
4447 return( PSA_ERROR_INVALID_ARGUMENT );
4448 if( bits % 8 != 0 )
4449 return( PSA_ERROR_INVALID_ARGUMENT );
4450 data = mbedtls_calloc( 1, bytes );
4451 if( data == NULL )
4452 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4453
4454 status = psa_generator_read( generator, data, bytes );
4455 if( status != PSA_SUCCESS )
4456 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004457#if defined(MBEDTLS_DES_C)
4458 if( type == PSA_KEY_TYPE_DES )
4459 psa_des_set_key_parity( data, bytes );
4460#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004461 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004462
4463exit:
4464 mbedtls_free( data );
4465 return( status );
4466}
4467
4468
4469
4470/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004471/* Key derivation */
4472/****************************************************************/
4473
Gilles Peskinea05219c2018-11-16 16:02:56 +01004474#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004475/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004476 * of the HKDF algorithm.
4477 *
4478 * Note that if this function fails, you must call psa_generator_abort()
4479 * to potentially free embedded data structures and wipe confidential data.
4480 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004481static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004482 const uint8_t *secret,
4483 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004484 psa_algorithm_t hash_alg,
4485 const uint8_t *salt,
4486 size_t salt_length,
4487 const uint8_t *label,
4488 size_t label_length )
4489{
4490 psa_status_t status;
4491 status = psa_hmac_setup_internal( &hkdf->hmac,
4492 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004493 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004494 if( status != PSA_SUCCESS )
4495 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004496 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004497 if( status != PSA_SUCCESS )
4498 return( status );
4499 status = psa_hmac_finish_internal( &hkdf->hmac,
4500 hkdf->prk,
4501 sizeof( hkdf->prk ) );
4502 if( status != PSA_SUCCESS )
4503 return( status );
4504 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4505 hkdf->block_number = 0;
4506 hkdf->info_length = label_length;
4507 if( label_length != 0 )
4508 {
4509 hkdf->info = mbedtls_calloc( 1, label_length );
4510 if( hkdf->info == NULL )
4511 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4512 memcpy( hkdf->info, label, label_length );
4513 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004514 hkdf->state = HKDF_STATE_KEYED;
4515 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004516 return( PSA_SUCCESS );
4517}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004518#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004519
Gilles Peskinea05219c2018-11-16 16:02:56 +01004520#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004521/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4522 *
4523 * Note that if this function fails, you must call psa_generator_abort()
4524 * to potentially free embedded data structures and wipe confidential data.
4525 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004526static psa_status_t psa_generator_tls12_prf_setup(
4527 psa_tls12_prf_generator_t *tls12_prf,
4528 const unsigned char *key,
4529 size_t key_len,
4530 psa_algorithm_t hash_alg,
4531 const uint8_t *salt,
4532 size_t salt_length,
4533 const uint8_t *label,
4534 size_t label_length )
4535{
4536 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004537 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4538 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004539
4540 tls12_prf->key = mbedtls_calloc( 1, key_len );
4541 if( tls12_prf->key == NULL )
4542 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4543 tls12_prf->key_len = key_len;
4544 memcpy( tls12_prf->key, key, key_len );
4545
Hanno Becker580fba12018-11-13 20:50:45 +00004546 overflow = ( salt_length + label_length < salt_length ) ||
4547 ( salt_length + label_length + hash_length < hash_length );
4548 if( overflow )
4549 return( PSA_ERROR_INVALID_ARGUMENT );
4550
4551 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4552 if( tls12_prf->Ai_with_seed == NULL )
4553 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4554 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4555
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004556 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4557 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004558 if( label_length != 0 )
4559 {
4560 memcpy( tls12_prf->Ai_with_seed + hash_length,
4561 label, label_length );
4562 }
4563
4564 if( salt_length != 0 )
4565 {
4566 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4567 salt, salt_length );
4568 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004569
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004570 /* The first block gets generated when
4571 * psa_generator_read() is called. */
4572 tls12_prf->block_number = 0;
4573 tls12_prf->offset_in_block = hash_length;
4574
4575 return( PSA_SUCCESS );
4576}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004577
4578/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4579static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4580 psa_tls12_prf_generator_t *tls12_prf,
4581 const unsigned char *psk,
4582 size_t psk_len,
4583 psa_algorithm_t hash_alg,
4584 const uint8_t *salt,
4585 size_t salt_length,
4586 const uint8_t *label,
4587 size_t label_length )
4588{
4589 psa_status_t status;
4590 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4591
4592 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4593 return( PSA_ERROR_INVALID_ARGUMENT );
4594
4595 /* Quoting RFC 4279, Section 2:
4596 *
4597 * The premaster secret is formed as follows: if the PSK is N octets
4598 * long, concatenate a uint16 with the value N, N zero octets, a second
4599 * uint16 with the value N, and the PSK itself.
4600 */
4601
4602 pms[0] = ( psk_len >> 8 ) & 0xff;
4603 pms[1] = ( psk_len >> 0 ) & 0xff;
4604 memset( pms + 2, 0, psk_len );
4605 pms[2 + psk_len + 0] = pms[0];
4606 pms[2 + psk_len + 1] = pms[1];
4607 memcpy( pms + 4 + psk_len, psk, psk_len );
4608
4609 status = psa_generator_tls12_prf_setup( tls12_prf,
4610 pms, 4 + 2 * psk_len,
4611 hash_alg,
4612 salt, salt_length,
4613 label, label_length );
4614
Gilles Peskine3f108122018-12-07 18:14:53 +01004615 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004616 return( status );
4617}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004618#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004619
Gilles Peskine346797d2018-11-16 16:05:06 +01004620/* Note that if this function fails, you must call psa_generator_abort()
4621 * to potentially free embedded data structures and wipe confidential data.
4622 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004623static psa_status_t psa_key_derivation_internal(
4624 psa_crypto_generator_t *generator,
4625 const uint8_t *secret, size_t secret_length,
4626 psa_algorithm_t alg,
4627 const uint8_t *salt, size_t salt_length,
4628 const uint8_t *label, size_t label_length,
4629 size_t capacity )
4630{
4631 psa_status_t status;
4632 size_t max_capacity;
4633
4634 /* Set generator->alg even on failure so that abort knows what to do. */
4635 generator->alg = alg;
4636
Gilles Peskine751d9652018-09-18 12:05:44 +02004637 if( alg == PSA_ALG_SELECT_RAW )
4638 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004639 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004640 if( salt_length != 0 )
4641 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004642 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004643 if( label_length != 0 )
4644 return( PSA_ERROR_INVALID_ARGUMENT );
4645 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4646 if( generator->ctx.buffer.data == NULL )
4647 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4648 memcpy( generator->ctx.buffer.data, secret, secret_length );
4649 generator->ctx.buffer.size = secret_length;
4650 max_capacity = secret_length;
4651 status = PSA_SUCCESS;
4652 }
4653 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004654#if defined(MBEDTLS_MD_C)
4655 if( PSA_ALG_IS_HKDF( alg ) )
4656 {
4657 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4658 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4659 if( hash_size == 0 )
4660 return( PSA_ERROR_NOT_SUPPORTED );
4661 max_capacity = 255 * hash_size;
4662 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4663 secret, secret_length,
4664 hash_alg,
4665 salt, salt_length,
4666 label, label_length );
4667 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004668 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4669 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4670 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004671 {
4672 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4673 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4674
4675 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4676 if( hash_alg != PSA_ALG_SHA_256 &&
4677 hash_alg != PSA_ALG_SHA_384 )
4678 {
4679 return( PSA_ERROR_NOT_SUPPORTED );
4680 }
4681
4682 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004683
4684 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4685 {
4686 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4687 secret, secret_length,
4688 hash_alg, salt, salt_length,
4689 label, label_length );
4690 }
4691 else
4692 {
4693 status = psa_generator_tls12_psk_to_ms_setup(
4694 &generator->ctx.tls12_prf,
4695 secret, secret_length,
4696 hash_alg, salt, salt_length,
4697 label, label_length );
4698 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004699 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004700 else
4701#endif
4702 {
4703 return( PSA_ERROR_NOT_SUPPORTED );
4704 }
4705
4706 if( status != PSA_SUCCESS )
4707 return( status );
4708
4709 if( capacity <= max_capacity )
4710 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004711 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4712 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004713 else
4714 return( PSA_ERROR_INVALID_ARGUMENT );
4715
4716 return( PSA_SUCCESS );
4717}
4718
Gilles Peskineea0fb492018-07-12 17:17:20 +02004719psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004720 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004721 psa_algorithm_t alg,
4722 const uint8_t *salt,
4723 size_t salt_length,
4724 const uint8_t *label,
4725 size_t label_length,
4726 size_t capacity )
4727{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004728 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004729 psa_status_t status;
4730
4731 if( generator->alg != 0 )
4732 return( PSA_ERROR_BAD_STATE );
4733
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004734 /* Make sure that alg is a key derivation algorithm. This prevents
4735 * key selection algorithms, which psa_key_derivation_internal
4736 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004737 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4738 return( PSA_ERROR_INVALID_ARGUMENT );
4739
Gilles Peskinec5487a82018-12-03 18:08:14 +01004740 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004741 if( status != PSA_SUCCESS )
4742 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004743
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004744 if( slot->type != PSA_KEY_TYPE_DERIVE )
4745 return( PSA_ERROR_INVALID_ARGUMENT );
4746
4747 status = psa_key_derivation_internal( generator,
4748 slot->data.raw.data,
4749 slot->data.raw.bytes,
4750 alg,
4751 salt, salt_length,
4752 label, label_length,
4753 capacity );
4754 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004755 psa_generator_abort( generator );
4756 return( status );
4757}
4758
Gilles Peskine969c5d62019-01-16 15:53:06 +01004759static psa_status_t psa_key_derivation_setup_kdf(
4760 psa_crypto_generator_t *generator,
4761 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004762{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004763 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004764#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004765 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4766 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4767 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004768 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004769 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004770 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4771 if( hash_size == 0 )
4772 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004773 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4774 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004775 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004776 {
4777 return( PSA_ERROR_NOT_SUPPORTED );
4778 }
4779 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004780 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004781 }
4782#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004783 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004784 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004785}
4786
4787psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4788 psa_algorithm_t alg )
4789{
4790 psa_status_t status;
4791
4792 if( generator->alg != 0 )
4793 return( PSA_ERROR_BAD_STATE );
4794
Gilles Peskine6843c292019-01-18 16:44:49 +01004795 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4796 return( PSA_ERROR_INVALID_ARGUMENT );
4797 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004798 {
4799 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004800 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004801 }
4802 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4803 {
4804 status = psa_key_derivation_setup_kdf( generator, alg );
4805 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004806 else
4807 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004808
4809 if( status == PSA_SUCCESS )
4810 generator->alg = alg;
4811 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004812}
4813
4814#if defined(MBEDTLS_MD_C)
4815static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4816 psa_algorithm_t hash_alg,
4817 psa_key_derivation_step_t step,
4818 const uint8_t *data,
4819 size_t data_length )
4820{
4821 psa_status_t status;
4822 switch( step )
4823 {
4824 case PSA_KDF_STEP_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004825 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004826 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004827 status = psa_hmac_setup_internal( &hkdf->hmac,
4828 data, data_length,
4829 hash_alg );
4830 if( status != PSA_SUCCESS )
4831 return( status );
4832 hkdf->state = HKDF_STATE_STARTED;
4833 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004834 case PSA_KDF_STEP_SECRET:
4835 /* If no salt was provided, use an empty salt. */
4836 if( hkdf->state == HKDF_STATE_INIT )
4837 {
4838 status = psa_hmac_setup_internal( &hkdf->hmac,
4839 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004840 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004841 if( status != PSA_SUCCESS )
4842 return( status );
4843 hkdf->state = HKDF_STATE_STARTED;
4844 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004845 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004846 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004847 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4848 data, data_length );
4849 if( status != PSA_SUCCESS )
4850 return( status );
4851 status = psa_hmac_finish_internal( &hkdf->hmac,
4852 hkdf->prk,
4853 sizeof( hkdf->prk ) );
4854 if( status != PSA_SUCCESS )
4855 return( status );
4856 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4857 hkdf->block_number = 0;
4858 hkdf->state = HKDF_STATE_KEYED;
4859 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004860 case PSA_KDF_STEP_INFO:
4861 if( hkdf->state == HKDF_STATE_OUTPUT )
4862 return( PSA_ERROR_BAD_STATE );
4863 if( hkdf->info_set )
4864 return( PSA_ERROR_BAD_STATE );
4865 hkdf->info_length = data_length;
4866 if( data_length != 0 )
4867 {
4868 hkdf->info = mbedtls_calloc( 1, data_length );
4869 if( hkdf->info == NULL )
4870 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4871 memcpy( hkdf->info, data, data_length );
4872 }
4873 hkdf->info_set = 1;
4874 return( PSA_SUCCESS );
4875 default:
4876 return( PSA_ERROR_INVALID_ARGUMENT );
4877 }
4878}
4879#endif /* MBEDTLS_MD_C */
4880
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004881static psa_status_t psa_key_derivation_input_raw(
4882 psa_crypto_generator_t *generator,
4883 psa_key_derivation_step_t step,
4884 const uint8_t *data,
4885 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004886{
4887 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004888 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004889
Gilles Peskine969c5d62019-01-16 15:53:06 +01004890 if( kdf_alg == PSA_ALG_SELECT_RAW )
4891 {
4892 if( generator->capacity != 0 )
4893 return( PSA_ERROR_INVALID_ARGUMENT );
4894 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4895 if( generator->ctx.buffer.data == NULL )
4896 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4897 memcpy( generator->ctx.buffer.data, data, data_length );
4898 generator->ctx.buffer.size = data_length;
4899 generator->capacity = data_length;
4900 status = PSA_SUCCESS;
4901 }
4902 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004903#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004904 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004905 {
4906 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004907 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004908 step, data, data_length );
4909 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004910 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004911#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004912#if defined(MBEDTLS_MD_C)
4913 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004914 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4915 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004916 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004917 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004918 status = PSA_ERROR_NOT_SUPPORTED;
4919 }
4920 else
4921#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004922 {
4923 /* This can't happen unless the generator object was not initialized */
4924 return( PSA_ERROR_BAD_STATE );
4925 }
4926
4927 if( status != PSA_SUCCESS )
4928 psa_generator_abort( generator );
4929 return( status );
4930}
4931
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004932psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4933 psa_key_derivation_step_t step,
4934 const uint8_t *data,
4935 size_t data_length )
4936{
4937 switch( step )
4938 {
4939 case PSA_KDF_STEP_LABEL:
4940 case PSA_KDF_STEP_SALT:
4941 case PSA_KDF_STEP_INFO:
4942 return( psa_key_derivation_input_raw( generator, step,
4943 data, data_length ) );
4944 default:
4945 return( PSA_ERROR_INVALID_ARGUMENT );
4946 }
4947}
4948
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004949psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4950 psa_key_derivation_step_t step,
4951 psa_key_handle_t handle )
4952{
4953 psa_key_slot_t *slot;
4954 psa_status_t status;
4955 status = psa_get_key_from_slot( handle, &slot,
4956 PSA_KEY_USAGE_DERIVE,
4957 generator->alg );
4958 if( status != PSA_SUCCESS )
4959 return( status );
4960 if( slot->type != PSA_KEY_TYPE_DERIVE )
4961 return( PSA_ERROR_INVALID_ARGUMENT );
4962 /* Don't allow a key to be used as an input that is usually public.
4963 * This is debatable. It's ok from a cryptographic perspective to
4964 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004965 * the material should be dedicated to a particular input step,
4966 * otherwise this may allow the key to be used in an unintended way
4967 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004968 if( step != PSA_KDF_STEP_SECRET )
4969 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004970 return( psa_key_derivation_input_raw( generator,
4971 step,
4972 slot->data.raw.data,
4973 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004974}
4975
Gilles Peskineea0fb492018-07-12 17:17:20 +02004976
4977
4978/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004979/* Key agreement */
4980/****************************************************************/
4981
Gilles Peskinea05219c2018-11-16 16:02:56 +01004982#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004983static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4984 size_t peer_key_length,
4985 const mbedtls_ecp_keypair *our_key,
4986 uint8_t *shared_secret,
4987 size_t shared_secret_size,
4988 size_t *shared_secret_length )
4989{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004990 mbedtls_ecp_keypair *their_key = NULL;
4991 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004992 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004993 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004994
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004995 status = psa_import_ec_public_key(
4996 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4997 peer_key, peer_key_length,
4998 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004999 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005000 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005001
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005002 status = mbedtls_to_psa_error(
5003 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5004 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005005 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005006 status = mbedtls_to_psa_error(
5007 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5008 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005009 goto exit;
5010
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005011 status = mbedtls_to_psa_error(
5012 mbedtls_ecdh_calc_secret( &ecdh,
5013 shared_secret_length,
5014 shared_secret, shared_secret_size,
5015 mbedtls_ctr_drbg_random,
5016 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005017
5018exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005019 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005020 mbedtls_ecp_keypair_free( their_key );
5021 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005022 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005023}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005024#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005025
Gilles Peskine01d718c2018-09-18 12:01:02 +02005026#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5027
Gilles Peskine0216fe12019-04-11 21:23:21 +02005028static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5029 psa_key_slot_t *private_key,
5030 const uint8_t *peer_key,
5031 size_t peer_key_length,
5032 uint8_t *shared_secret,
5033 size_t shared_secret_size,
5034 size_t *shared_secret_length )
5035{
5036 switch( alg )
5037 {
5038#if defined(MBEDTLS_ECDH_C)
5039 case PSA_ALG_ECDH:
5040 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
5041 return( PSA_ERROR_INVALID_ARGUMENT );
5042 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5043 private_key->data.ecp,
5044 shared_secret, shared_secret_size,
5045 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005046#endif /* MBEDTLS_ECDH_C */
5047 default:
5048 (void) private_key;
5049 (void) peer_key;
5050 (void) peer_key_length;
5051 (void) shared_secret;
5052 (void) shared_secret_size;
5053 (void) shared_secret_length;
5054 return( PSA_ERROR_NOT_SUPPORTED );
5055 }
5056}
5057
Gilles Peskine346797d2018-11-16 16:05:06 +01005058/* Note that if this function fails, you must call psa_generator_abort()
5059 * to potentially free embedded data structures and wipe confidential data.
5060 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005061static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005062 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005063 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005064 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005065 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005066{
5067 psa_status_t status;
5068 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5069 size_t shared_secret_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005070 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005071
5072 /* Step 1: run the secret agreement algorithm to generate the shared
5073 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005074 status = psa_key_agreement_raw_internal( ka_alg,
5075 private_key,
5076 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005077 shared_secret,
5078 sizeof( shared_secret ),
5079 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005080 if( status != PSA_SUCCESS )
5081 goto exit;
5082
5083 /* Step 2: set up the key derivation to generate key material from
5084 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005085 status = psa_key_derivation_input_raw( generator, step,
5086 shared_secret, shared_secret_length );
5087
Gilles Peskine01d718c2018-09-18 12:01:02 +02005088exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005089 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005090 return( status );
5091}
5092
5093psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005094 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005095 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005096 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005097 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005098{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005099 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005100 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005101 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005102 return( PSA_ERROR_INVALID_ARGUMENT );
5103 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005104 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005105 if( status != PSA_SUCCESS )
5106 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005107 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005108 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005109 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005110 if( status != PSA_SUCCESS )
5111 psa_generator_abort( generator );
5112 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005113}
5114
Gilles Peskine0216fe12019-04-11 21:23:21 +02005115psa_status_t psa_key_agreement_raw_shared_secret( psa_algorithm_t alg,
5116 psa_key_handle_t private_key,
5117 const uint8_t *peer_key,
5118 size_t peer_key_length,
5119 uint8_t *output,
5120 size_t output_size,
5121 size_t *output_length )
5122{
5123 psa_key_slot_t *slot;
5124 psa_status_t status;
5125
5126 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5127 {
5128 status = PSA_ERROR_INVALID_ARGUMENT;
5129 goto exit;
5130 }
5131 status = psa_get_key_from_slot( private_key, &slot,
5132 PSA_KEY_USAGE_DERIVE, alg );
5133 if( status != PSA_SUCCESS )
5134 goto exit;
5135
5136 status = psa_key_agreement_raw_internal( alg, slot,
5137 peer_key, peer_key_length,
5138 output, output_size,
5139 output_length );
5140
5141exit:
5142 if( status != PSA_SUCCESS )
5143 {
5144 /* If an error happens and is not handled properly, the output
5145 * may be used as a key to protect sensitive data. Arrange for such
5146 * a key to be random, which is likely to result in decryption or
5147 * verification errors. This is better than filling the buffer with
5148 * some constant data such as zeros, which would result in the data
5149 * being protected with a reproducible, easily knowable key.
5150 */
5151 psa_generate_random( output, output_size );
5152 *output_length = output_size;
5153 }
5154 return( status );
5155}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005156
5157
5158/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005159/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005160/****************************************************************/
5161
5162psa_status_t psa_generate_random( uint8_t *output,
5163 size_t output_size )
5164{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005165 int ret;
5166 GUARD_MODULE_INITIALIZED;
5167
5168 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005169 return( mbedtls_to_psa_error( ret ) );
5170}
5171
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005172#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5173#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005174
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005175psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5176 size_t seed_size )
5177{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005178 if( global_data.initialized )
5179 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005180
5181 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5182 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5183 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5184 return( PSA_ERROR_INVALID_ARGUMENT );
5185
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005186 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005187}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005188#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005189
Gilles Peskinee56e8782019-04-26 17:34:02 +02005190#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5191static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5192 size_t domain_parameters_size,
5193 int *exponent )
5194{
5195 size_t i;
5196 uint32_t acc = 0;
5197
5198 if( domain_parameters_size == 0 )
5199 {
5200 *exponent = 65537;
5201 return( PSA_SUCCESS );
5202 }
5203
5204 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5205 * support values that fit in a 32-bit integer, which is larger than
5206 * int on just about every platform anyway. */
5207 if( domain_parameters_size > sizeof( acc ) )
5208 return( PSA_ERROR_NOT_SUPPORTED );
5209 for( i = 0; i < domain_parameters_size; i++ )
5210 acc = ( acc << 8 ) | domain_parameters[i];
5211 if( acc > INT_MAX )
5212 return( PSA_ERROR_NOT_SUPPORTED );
5213 *exponent = acc;
5214 return( PSA_SUCCESS );
5215}
5216#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5217
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005218static psa_status_t psa_generate_random_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005219 psa_key_slot_t *slot, size_t bits,
5220 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005221{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005222 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005223
Gilles Peskinee56e8782019-04-26 17:34:02 +02005224 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005225 return( PSA_ERROR_INVALID_ARGUMENT );
5226
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005227 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005228 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005229 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005230 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005231 if( status != PSA_SUCCESS )
5232 return( status );
5233 status = psa_generate_random( slot->data.raw.data,
5234 slot->data.raw.bytes );
5235 if( status != PSA_SUCCESS )
5236 {
5237 mbedtls_free( slot->data.raw.data );
5238 return( status );
5239 }
5240#if defined(MBEDTLS_DES_C)
5241 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005242 psa_des_set_key_parity( slot->data.raw.data,
5243 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005244#endif /* MBEDTLS_DES_C */
5245 }
5246 else
5247
5248#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5249 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
5250 {
5251 mbedtls_rsa_context *rsa;
5252 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005253 int exponent;
5254 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005255 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5256 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005257 /* Accept only byte-aligned keys, for the same reasons as
5258 * in psa_import_rsa_key(). */
5259 if( bits % 8 != 0 )
5260 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005261 status = psa_read_rsa_exponent( domain_parameters,
5262 domain_parameters_size,
5263 &exponent );
5264 if( status != PSA_SUCCESS )
5265 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005266 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5267 if( rsa == NULL )
5268 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5269 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5270 ret = mbedtls_rsa_gen_key( rsa,
5271 mbedtls_ctr_drbg_random,
5272 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005273 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005274 exponent );
5275 if( ret != 0 )
5276 {
5277 mbedtls_rsa_free( rsa );
5278 mbedtls_free( rsa );
5279 return( mbedtls_to_psa_error( ret ) );
5280 }
5281 slot->data.rsa = rsa;
5282 }
5283 else
5284#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5285
5286#if defined(MBEDTLS_ECP_C)
5287 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
5288 {
5289 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5290 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5291 const mbedtls_ecp_curve_info *curve_info =
5292 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5293 mbedtls_ecp_keypair *ecp;
5294 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005295 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005296 return( PSA_ERROR_NOT_SUPPORTED );
5297 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5298 return( PSA_ERROR_NOT_SUPPORTED );
5299 if( curve_info->bit_size != bits )
5300 return( PSA_ERROR_INVALID_ARGUMENT );
5301 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5302 if( ecp == NULL )
5303 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5304 mbedtls_ecp_keypair_init( ecp );
5305 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5306 mbedtls_ctr_drbg_random,
5307 &global_data.ctr_drbg );
5308 if( ret != 0 )
5309 {
5310 mbedtls_ecp_keypair_free( ecp );
5311 mbedtls_free( ecp );
5312 return( mbedtls_to_psa_error( ret ) );
5313 }
5314 slot->data.ecp = ecp;
5315 }
5316 else
5317#endif /* MBEDTLS_ECP_C */
5318
5319 return( PSA_ERROR_NOT_SUPPORTED );
5320
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005321 return( PSA_SUCCESS );
5322}
5323
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005324psa_status_t psa_generate_random_key_to_handle( psa_key_handle_t handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005325 psa_key_type_t type,
5326 size_t bits,
5327 const void *extra,
5328 size_t extra_size )
5329{
5330 psa_key_slot_t *slot;
5331 psa_status_t status;
5332
Gilles Peskinee56e8782019-04-26 17:34:02 +02005333#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5334 /* The old public exponent encoding is no longer supported. */
5335 if( extra_size != 0 )
5336 return( PSA_ERROR_NOT_SUPPORTED );
5337#endif
5338
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005339 status = psa_get_empty_key_slot( handle, &slot );
5340 if( status != PSA_SUCCESS )
5341 return( status );
5342
Gilles Peskine12313cd2018-06-20 00:20:32 +02005343 slot->type = type;
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005344 status = psa_generate_random_key_internal( slot, bits, extra, extra_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005345 if( status != PSA_SUCCESS )
5346 slot->type = 0;
Darryl Green0c6575a2018-11-07 16:05:30 +00005347
5348#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
5349 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
5350 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01005351 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005352 }
5353#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5354
5355 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005356}
5357
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005358psa_status_t psa_generate_random_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005359 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005360{
5361 psa_status_t status;
5362 psa_key_slot_t *slot = NULL;
5363 status = psa_start_key_creation( attributes, handle, &slot );
5364 if( status == PSA_SUCCESS )
5365 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005366 status = psa_generate_random_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005367 slot, attributes->bits,
5368 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005369 }
5370 if( status == PSA_SUCCESS )
5371 status = psa_finish_key_creation( slot );
5372 if( status != PSA_SUCCESS )
5373 {
5374 psa_fail_key_creation( slot );
5375 *handle = 0;
5376 }
5377 return( status );
5378}
5379
5380
Gilles Peskine05d69892018-06-19 22:00:52 +02005381
5382/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005383/* Module setup */
5384/****************************************************************/
5385
Gilles Peskine5e769522018-11-20 21:59:56 +01005386psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5387 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5388 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5389{
5390 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5391 return( PSA_ERROR_BAD_STATE );
5392 global_data.entropy_init = entropy_init;
5393 global_data.entropy_free = entropy_free;
5394 return( PSA_SUCCESS );
5395}
5396
Gilles Peskinee59236f2018-01-27 23:32:46 +01005397void mbedtls_psa_crypto_free( void )
5398{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005399 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005400 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5401 {
5402 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005403 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005404 }
5405 /* Wipe all remaining data, including configuration.
5406 * In particular, this sets all state indicator to the value
5407 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005408 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005409}
5410
5411psa_status_t psa_crypto_init( void )
5412{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005413 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005414 const unsigned char drbg_seed[] = "PSA";
5415
Gilles Peskinec6b69072018-11-20 21:42:52 +01005416 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005417 if( global_data.initialized != 0 )
5418 return( PSA_SUCCESS );
5419
Gilles Peskine5e769522018-11-20 21:59:56 +01005420 /* Set default configuration if
5421 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5422 if( global_data.entropy_init == NULL )
5423 global_data.entropy_init = mbedtls_entropy_init;
5424 if( global_data.entropy_free == NULL )
5425 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005426
Gilles Peskinec6b69072018-11-20 21:42:52 +01005427 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005428 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005429 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005430 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005431 status = mbedtls_to_psa_error(
5432 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5433 mbedtls_entropy_func,
5434 &global_data.entropy,
5435 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5436 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005437 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005438 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005439
Gilles Peskine66fb1262018-12-10 16:29:04 +01005440 status = psa_initialize_key_slots( );
5441 if( status != PSA_SUCCESS )
5442 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005443
5444 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005445 global_data.initialized = 1;
5446
Gilles Peskinee59236f2018-01-27 23:32:46 +01005447exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005448 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005449 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005450 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005451}
5452
5453#endif /* MBEDTLS_PSA_CRYPTO_C */