blob: b0b7de10fdc3cfa2e03651b1aa02bc28ab1ac0dc [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
42#if defined(MBEDTLS_PLATFORM_C)
43#include "mbedtls/platform.h"
44#else
45#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020050#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000051#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020052#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/blowfish.h"
54#include "mbedtls/camellia.h"
55#include "mbedtls/cipher.h"
56#include "mbedtls/ccm.h"
57#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010058#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020060#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010061#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/error.h"
64#include "mbedtls/gcm.h"
65#include "mbedtls/md2.h"
66#include "mbedtls/md4.h"
67#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010068#include "mbedtls/md.h"
69#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010070#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010072#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010074#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/sha1.h"
76#include "mbedtls/sha256.h"
77#include "mbedtls/sha512.h"
78#include "mbedtls/xtea.h"
79
Gilles Peskine996deb12018-08-01 15:45:45 +020080#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
81
Gilles Peskine9ef733f2018-02-07 21:05:37 +010082/* constant-time buffer comparison */
83static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
84{
85 size_t i;
86 unsigned char diff = 0;
87
88 for( i = 0; i < n; i++ )
89 diff |= a[i] ^ b[i];
90
91 return( diff );
92}
93
94
95
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010096/****************************************************************/
97/* Global data, support functions and library management */
98/****************************************************************/
99
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100static int key_type_is_raw_bytes( psa_key_type_t type )
101{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200102 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200103}
104
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100105/* Values for psa_global_data_t::rng_state */
106#define RNG_NOT_INITIALIZED 0
107#define RNG_INITIALIZED 1
108#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100109
Gilles Peskine2d277862018-06-18 15:41:12 +0200110typedef struct
111{
Gilles Peskine5e769522018-11-20 21:59:56 +0100112 void (* entropy_init )( mbedtls_entropy_context *ctx );
113 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100114 mbedtls_entropy_context entropy;
115 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100116 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100117 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
itayzafrir0adf0fc2018-09-06 16:24:41 +0300122#define GUARD_MODULE_INITIALIZED \
123 if( global_data.initialized == 0 ) \
124 return( PSA_ERROR_BAD_STATE );
125
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126static psa_status_t mbedtls_to_psa_error( int ret )
127{
Gilles Peskinea5905292018-02-07 20:59:33 +0100128 /* If there's both a high-level code and low-level code, dispatch on
129 * the high-level code. */
130 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131 {
132 case 0:
133 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100134
135 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
136 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
137 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
138 return( PSA_ERROR_NOT_SUPPORTED );
139 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
140 return( PSA_ERROR_HARDWARE_FAILURE );
141
142 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
Gilles Peskine9a944802018-06-21 09:35:35 +0200145 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
146 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
147 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
148 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
149 case MBEDTLS_ERR_ASN1_INVALID_DATA:
150 return( PSA_ERROR_INVALID_ARGUMENT );
151 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
152 return( PSA_ERROR_INSUFFICIENT_MEMORY );
153 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
154 return( PSA_ERROR_BUFFER_TOO_SMALL );
155
Jaeden Amero93e21112019-02-20 13:57:28 +0000156#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000157 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100159 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100161 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
162 return( PSA_ERROR_NOT_SUPPORTED );
163 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
164 return( PSA_ERROR_HARDWARE_FAILURE );
165
Jaeden Amero93e21112019-02-20 13:57:28 +0000166#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000167 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
172 return( PSA_ERROR_NOT_SUPPORTED );
173 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
174 return( PSA_ERROR_HARDWARE_FAILURE );
175
176 case MBEDTLS_ERR_CCM_BAD_INPUT:
177 return( PSA_ERROR_INVALID_ARGUMENT );
178 case MBEDTLS_ERR_CCM_AUTH_FAILED:
179 return( PSA_ERROR_INVALID_SIGNATURE );
180 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
181 return( PSA_ERROR_HARDWARE_FAILURE );
182
183 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
188 return( PSA_ERROR_INSUFFICIENT_MEMORY );
189 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
190 return( PSA_ERROR_INVALID_PADDING );
191 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
196 return( PSA_ERROR_TAMPERING_DETECTED );
197 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
198 return( PSA_ERROR_HARDWARE_FAILURE );
199
200 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
201 return( PSA_ERROR_HARDWARE_FAILURE );
202
203 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
204 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
205 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
206 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
209 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
210
211 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
212 return( PSA_ERROR_NOT_SUPPORTED );
213 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
Gilles Peskinee59236f2018-01-27 23:32:46 +0100216 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
217 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
218 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
221 case MBEDTLS_ERR_GCM_AUTH_FAILED:
222 return( PSA_ERROR_INVALID_SIGNATURE );
223 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200224 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
228 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
229 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
230 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
231 return( PSA_ERROR_HARDWARE_FAILURE );
232
233 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
234 return( PSA_ERROR_NOT_SUPPORTED );
235 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
236 return( PSA_ERROR_INVALID_ARGUMENT );
237 case MBEDTLS_ERR_MD_ALLOC_FAILED:
238 return( PSA_ERROR_INSUFFICIENT_MEMORY );
239 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
240 return( PSA_ERROR_STORAGE_FAILURE );
241 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskinef76aa772018-10-29 19:24:33 +0100244 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
245 return( PSA_ERROR_STORAGE_FAILURE );
246 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
251 return( PSA_ERROR_BUFFER_TOO_SMALL );
252 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
253 return( PSA_ERROR_INVALID_ARGUMENT );
254 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
255 return( PSA_ERROR_INVALID_ARGUMENT );
256 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
260
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_ALLOC_FAILED:
262 return( PSA_ERROR_INSUFFICIENT_MEMORY );
263 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
264 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100268 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
269 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
274 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
275 return( PSA_ERROR_NOT_PERMITTED );
276 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_INVALID_ALG:
279 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
280 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
283 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
285 return( PSA_ERROR_HARDWARE_FAILURE );
286
287 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
288 return( PSA_ERROR_HARDWARE_FAILURE );
289
290 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_RSA_INVALID_PADDING:
293 return( PSA_ERROR_INVALID_PADDING );
294 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
299 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
300 return( PSA_ERROR_TAMPERING_DETECTED );
301 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
302 return( PSA_ERROR_INVALID_SIGNATURE );
303 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
304 return( PSA_ERROR_BUFFER_TOO_SMALL );
305 case MBEDTLS_ERR_RSA_RNG_FAILED:
306 return( PSA_ERROR_INSUFFICIENT_MEMORY );
307 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
308 return( PSA_ERROR_NOT_SUPPORTED );
309 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311
312 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
313 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
314 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316
317 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
itayzafrir5c753392018-05-08 11:18:38 +0300322 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300324 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
326 return( PSA_ERROR_BUFFER_TOO_SMALL );
327 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
328 return( PSA_ERROR_NOT_SUPPORTED );
329 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
330 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
331 return( PSA_ERROR_INVALID_SIGNATURE );
332 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
333 return( PSA_ERROR_INSUFFICIENT_MEMORY );
334 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300336
Gilles Peskinee59236f2018-01-27 23:32:46 +0100337 default:
David Saadab4ecc272019-02-14 13:48:10 +0200338 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100339 }
340}
341
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200342
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200343
344
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100345/****************************************************************/
346/* Key management */
347/****************************************************************/
348
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100349#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200350static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
351{
352 switch( grpid )
353 {
354 case MBEDTLS_ECP_DP_SECP192R1:
355 return( PSA_ECC_CURVE_SECP192R1 );
356 case MBEDTLS_ECP_DP_SECP224R1:
357 return( PSA_ECC_CURVE_SECP224R1 );
358 case MBEDTLS_ECP_DP_SECP256R1:
359 return( PSA_ECC_CURVE_SECP256R1 );
360 case MBEDTLS_ECP_DP_SECP384R1:
361 return( PSA_ECC_CURVE_SECP384R1 );
362 case MBEDTLS_ECP_DP_SECP521R1:
363 return( PSA_ECC_CURVE_SECP521R1 );
364 case MBEDTLS_ECP_DP_BP256R1:
365 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
366 case MBEDTLS_ECP_DP_BP384R1:
367 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
368 case MBEDTLS_ECP_DP_BP512R1:
369 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
370 case MBEDTLS_ECP_DP_CURVE25519:
371 return( PSA_ECC_CURVE_CURVE25519 );
372 case MBEDTLS_ECP_DP_SECP192K1:
373 return( PSA_ECC_CURVE_SECP192K1 );
374 case MBEDTLS_ECP_DP_SECP224K1:
375 return( PSA_ECC_CURVE_SECP224K1 );
376 case MBEDTLS_ECP_DP_SECP256K1:
377 return( PSA_ECC_CURVE_SECP256K1 );
378 case MBEDTLS_ECP_DP_CURVE448:
379 return( PSA_ECC_CURVE_CURVE448 );
380 default:
381 return( 0 );
382 }
383}
384
Gilles Peskine12313cd2018-06-20 00:20:32 +0200385static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
386{
387 switch( curve )
388 {
389 case PSA_ECC_CURVE_SECP192R1:
390 return( MBEDTLS_ECP_DP_SECP192R1 );
391 case PSA_ECC_CURVE_SECP224R1:
392 return( MBEDTLS_ECP_DP_SECP224R1 );
393 case PSA_ECC_CURVE_SECP256R1:
394 return( MBEDTLS_ECP_DP_SECP256R1 );
395 case PSA_ECC_CURVE_SECP384R1:
396 return( MBEDTLS_ECP_DP_SECP384R1 );
397 case PSA_ECC_CURVE_SECP521R1:
398 return( MBEDTLS_ECP_DP_SECP521R1 );
399 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
400 return( MBEDTLS_ECP_DP_BP256R1 );
401 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
402 return( MBEDTLS_ECP_DP_BP384R1 );
403 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
404 return( MBEDTLS_ECP_DP_BP512R1 );
405 case PSA_ECC_CURVE_CURVE25519:
406 return( MBEDTLS_ECP_DP_CURVE25519 );
407 case PSA_ECC_CURVE_SECP192K1:
408 return( MBEDTLS_ECP_DP_SECP192K1 );
409 case PSA_ECC_CURVE_SECP224K1:
410 return( MBEDTLS_ECP_DP_SECP224K1 );
411 case PSA_ECC_CURVE_SECP256K1:
412 return( MBEDTLS_ECP_DP_SECP256K1 );
413 case PSA_ECC_CURVE_CURVE448:
414 return( MBEDTLS_ECP_DP_CURVE448 );
415 default:
416 return( MBEDTLS_ECP_DP_NONE );
417 }
418}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100419#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200420
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200421static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
422 size_t bits,
423 struct raw_data *raw )
424{
425 /* Check that the bit size is acceptable for the key type */
426 switch( type )
427 {
428 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200429 if( bits == 0 )
430 {
431 raw->bytes = 0;
432 raw->data = NULL;
433 return( PSA_SUCCESS );
434 }
435 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436#if defined(MBEDTLS_MD_C)
437 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200438#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200439 case PSA_KEY_TYPE_DERIVE:
440 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200441#if defined(MBEDTLS_AES_C)
442 case PSA_KEY_TYPE_AES:
443 if( bits != 128 && bits != 192 && bits != 256 )
444 return( PSA_ERROR_INVALID_ARGUMENT );
445 break;
446#endif
447#if defined(MBEDTLS_CAMELLIA_C)
448 case PSA_KEY_TYPE_CAMELLIA:
449 if( bits != 128 && bits != 192 && bits != 256 )
450 return( PSA_ERROR_INVALID_ARGUMENT );
451 break;
452#endif
453#if defined(MBEDTLS_DES_C)
454 case PSA_KEY_TYPE_DES:
455 if( bits != 64 && bits != 128 && bits != 192 )
456 return( PSA_ERROR_INVALID_ARGUMENT );
457 break;
458#endif
459#if defined(MBEDTLS_ARC4_C)
460 case PSA_KEY_TYPE_ARC4:
461 if( bits < 8 || bits > 2048 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465 default:
466 return( PSA_ERROR_NOT_SUPPORTED );
467 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200468 if( bits % 8 != 0 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200470
471 /* Allocate memory for the key */
472 raw->bytes = PSA_BITS_TO_BYTES( bits );
473 raw->data = mbedtls_calloc( 1, raw->bytes );
474 if( raw->data == NULL )
475 {
476 raw->bytes = 0;
477 return( PSA_ERROR_INSUFFICIENT_MEMORY );
478 }
479 return( PSA_SUCCESS );
480}
481
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200482#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100483/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
484 * that are not a multiple of 8) well. For example, there is only
485 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
486 * way to return the exact bit size of a key.
487 * To keep things simple, reject non-byte-aligned key sizes. */
488static psa_status_t psa_check_rsa_key_byte_aligned(
489 const mbedtls_rsa_context *rsa )
490{
491 mbedtls_mpi n;
492 psa_status_t status;
493 mbedtls_mpi_init( &n );
494 status = mbedtls_to_psa_error(
495 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
496 if( status == PSA_SUCCESS )
497 {
498 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
499 status = PSA_ERROR_NOT_SUPPORTED;
500 }
501 mbedtls_mpi_free( &n );
502 return( status );
503}
504
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000505static psa_status_t psa_import_rsa_key( psa_key_type_t type,
506 const uint8_t *data,
507 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200508 mbedtls_rsa_context **p_rsa )
509{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000510 psa_status_t status;
511 mbedtls_pk_context pk;
512 mbedtls_rsa_context *rsa;
513 size_t bits;
514
515 mbedtls_pk_init( &pk );
516
517 /* Parse the data. */
518 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
519 status = mbedtls_to_psa_error(
520 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200521 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000522 status = mbedtls_to_psa_error(
523 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
524 if( status != PSA_SUCCESS )
525 goto exit;
526
527 /* We have something that the pkparse module recognizes. If it is a
528 * valid RSA key, store it. */
529 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200530 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000531 status = PSA_ERROR_INVALID_ARGUMENT;
532 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000534
535 rsa = mbedtls_pk_rsa( pk );
536 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
537 * supports non-byte-aligned key sizes, but not well. For example,
538 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
539 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
540 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
541 {
542 status = PSA_ERROR_NOT_SUPPORTED;
543 goto exit;
544 }
545 status = psa_check_rsa_key_byte_aligned( rsa );
546
547exit:
548 /* Free the content of the pk object only on error. */
549 if( status != PSA_SUCCESS )
550 {
551 mbedtls_pk_free( &pk );
552 return( status );
553 }
554
555 /* On success, store the content of the object in the RSA context. */
556 *p_rsa = rsa;
557
558 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200559}
560#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
561
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000562#if defined(MBEDTLS_ECP_C)
563
564/* Import a public key given as the uncompressed representation defined by SEC1
565 * 2.3.3 as the content of an ECPoint. */
566static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
567 const uint8_t *data,
568 size_t data_length,
569 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570{
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000571 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
572 mbedtls_ecp_keypair *ecp = NULL;
573 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
574
575 *p_ecp = NULL;
576 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
577 if( ecp == NULL )
578 return( PSA_ERROR_INSUFFICIENT_MEMORY );
579 mbedtls_ecp_keypair_init( ecp );
580
581 /* Load the group. */
582 status = mbedtls_to_psa_error(
583 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
584 if( status != PSA_SUCCESS )
585 goto exit;
586 /* Load the public value. */
587 status = mbedtls_to_psa_error(
588 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
589 data, data_length ) );
590 if( status != PSA_SUCCESS )
591 goto exit;
592
593 /* Check that the point is on the curve. */
594 status = mbedtls_to_psa_error(
595 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
596 if( status != PSA_SUCCESS )
597 goto exit;
598
599 *p_ecp = ecp;
600 return( PSA_SUCCESS );
601
602exit:
603 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200604 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000605 mbedtls_ecp_keypair_free( ecp );
606 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200607 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000608 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200609}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000610#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200611
Gilles Peskinef76aa772018-10-29 19:24:33 +0100612#if defined(MBEDTLS_ECP_C)
613/* Import a private key given as a byte string which is the private value
614 * in big-endian order. */
615static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
616 const uint8_t *data,
617 size_t data_length,
618 mbedtls_ecp_keypair **p_ecp )
619{
620 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
621 mbedtls_ecp_keypair *ecp = NULL;
622 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
623
624 *p_ecp = NULL;
625 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
626 if( ecp == NULL )
627 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000628 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100629
630 /* Load the group. */
631 status = mbedtls_to_psa_error(
632 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
633 if( status != PSA_SUCCESS )
634 goto exit;
635 /* Load the secret value. */
636 status = mbedtls_to_psa_error(
637 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640 /* Validate the private key. */
641 status = mbedtls_to_psa_error(
642 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
643 if( status != PSA_SUCCESS )
644 goto exit;
645 /* Calculate the public key from the private key. */
646 status = mbedtls_to_psa_error(
647 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
648 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
649 if( status != PSA_SUCCESS )
650 goto exit;
651
652 *p_ecp = ecp;
653 return( PSA_SUCCESS );
654
655exit:
656 if( ecp != NULL )
657 {
658 mbedtls_ecp_keypair_free( ecp );
659 mbedtls_free( ecp );
660 }
661 return( status );
662}
663#endif /* defined(MBEDTLS_ECP_C) */
664
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100665/** Import key data into a slot. `slot->type` must have been set
666 * previously. This function assumes that the slot does not contain
667 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100668psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
669 const uint8_t *data,
670 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100671{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200672 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Darryl Green940d72c2018-07-13 13:18:51 +0100674 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100676 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 if( data_length > SIZE_MAX / 8 )
678 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100679 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200680 PSA_BYTES_TO_BITS( data_length ),
681 &slot->data.raw );
682 if( status != PSA_SUCCESS )
683 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200684 if( data_length != 0 )
685 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 }
687 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100688#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100689 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100690 {
Darryl Green940d72c2018-07-13 13:18:51 +0100691 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100692 data, data_length,
693 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100694 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000695 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
696 {
697 status = psa_import_ec_public_key(
698 PSA_KEY_TYPE_GET_CURVE( slot->type ),
699 data, data_length,
700 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000701 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100702 else
703#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000704#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
705 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000707 status = psa_import_rsa_key( slot->type,
708 data, data_length,
709 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 }
711 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000712#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713 {
714 return( PSA_ERROR_NOT_SUPPORTED );
715 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000716 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100717}
718
Darryl Green06fd18d2018-07-16 11:21:11 +0100719/* Retrieve an empty key slot (slot with no key data, but possibly
Jaeden Amero283dfd12019-01-11 12:06:22 +0000720 * with some metadata such as a policy or domain parameters). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100721static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100722 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100723{
724 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100725 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100726
727 *p_slot = NULL;
728
Gilles Peskinec5487a82018-12-03 18:08:14 +0100729 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100730 if( status != PSA_SUCCESS )
731 return( status );
732
733 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200734 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100735
736 *p_slot = slot;
737 return( status );
738}
739
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100740/** Calculate the intersection of two algorithm usage policies.
741 *
742 * Return 0 (which allows no operation) on incompatibility.
743 */
744static psa_algorithm_t psa_key_policy_algorithm_intersection(
745 psa_algorithm_t alg1,
746 psa_algorithm_t alg2 )
747{
748 /* Common case: the policy only allows alg. */
749 if( alg1 == alg2 )
750 return( alg1 );
751 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100752 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100753 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
754 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
755 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
756 {
757 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
758 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100759 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100760 return( alg1 );
761 }
762 /* If the policies are incompatible, allow nothing. */
763 return( 0 );
764}
765
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100766/** Test whether a policy permits an algorithm.
767 *
768 * The caller must test usage flags separately.
769 */
770static int psa_key_policy_permits( const psa_key_policy_t *policy,
771 psa_algorithm_t alg )
772{
773 /* Common case: the policy only allows alg. */
774 if( alg == policy->alg )
775 return( 1 );
776 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
777 * and alg is the same hash-and-sign family with any hash,
778 * then alg is compliant with policy->alg. */
779 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
780 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
781 {
782 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
783 ( alg & ~PSA_ALG_HASH_MASK ) );
784 }
785 /* If it isn't permitted, it's forbidden. */
786 return( 0 );
787}
788
Gilles Peskinef603c712019-01-19 13:40:11 +0100789/** Restrict a key policy based on a constraint.
790 *
791 * \param[in,out] policy The policy to restrict.
792 * \param[in] constraint The policy constraint to apply.
793 *
794 * \retval #PSA_SUCCESS
795 * \c *policy contains the intersection of the original value of
796 * \c *policy and \c *constraint.
797 * \retval #PSA_ERROR_INVALID_ARGUMENT
798 * \c *policy and \c *constraint are incompatible.
799 * \c *policy is unchanged.
800 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100801static psa_status_t psa_restrict_key_policy(
802 psa_key_policy_t *policy,
803 const psa_key_policy_t *constraint )
804{
805 psa_algorithm_t intersection_alg =
806 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
807 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
808 return( PSA_ERROR_INVALID_ARGUMENT );
809 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100810 policy->alg = intersection_alg;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100811 return( PSA_SUCCESS );
812}
813
Darryl Green06fd18d2018-07-16 11:21:11 +0100814/** Retrieve a slot which must contain a key. The key must have allow all the
815 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
816 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100817static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100818 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100819 psa_key_usage_t usage,
820 psa_algorithm_t alg )
821{
822 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100823 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100824
825 *p_slot = NULL;
826
Gilles Peskinec5487a82018-12-03 18:08:14 +0100827 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100828 if( status != PSA_SUCCESS )
829 return( status );
830 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200831 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100832
833 /* Enforce that usage policy for the key slot contains all the flags
834 * required by the usage parameter. There is one exception: public
835 * keys can always be exported, so we treat public key objects as
836 * if they had the export flag. */
837 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
838 usage &= ~PSA_KEY_USAGE_EXPORT;
839 if( ( slot->policy.usage & usage ) != usage )
840 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841
842 /* Enforce that the usage policy permits the requested algortihm. */
843 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100844 return( PSA_ERROR_NOT_PERMITTED );
845
846 *p_slot = slot;
847 return( PSA_SUCCESS );
848}
Darryl Green940d72c2018-07-13 13:18:51 +0100849
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100850/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100851static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000852{
853 if( slot->type == PSA_KEY_TYPE_NONE )
854 {
855 /* No key material to clean. */
856 }
857 else if( key_type_is_raw_bytes( slot->type ) )
858 {
859 mbedtls_free( slot->data.raw.data );
860 }
861 else
862#if defined(MBEDTLS_RSA_C)
863 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
864 {
865 mbedtls_rsa_free( slot->data.rsa );
866 mbedtls_free( slot->data.rsa );
867 }
868 else
869#endif /* defined(MBEDTLS_RSA_C) */
870#if defined(MBEDTLS_ECP_C)
871 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
872 {
873 mbedtls_ecp_keypair_free( slot->data.ecp );
874 mbedtls_free( slot->data.ecp );
875 }
876 else
877#endif /* defined(MBEDTLS_ECP_C) */
878 {
879 /* Shouldn't happen: the key type is not any type that we
880 * put in. */
881 return( PSA_ERROR_TAMPERING_DETECTED );
882 }
883
884 return( PSA_SUCCESS );
885}
886
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100887static void psa_abort_operations_using_key( psa_key_slot_t *slot )
888{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200889 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100890 (void) slot;
891}
892
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100893/** Completely wipe a slot in memory, including its policy.
894 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100895psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100896{
897 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100898 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100899 /* At this point, key material and other type-specific content has
900 * been wiped. Clear remaining metadata. We can call memset and not
901 * zeroize because the metadata is not particularly sensitive. */
902 memset( slot, 0, sizeof( *slot ) );
903 return( status );
904}
905
Gilles Peskine87a5e562019-04-17 12:28:25 +0200906psa_status_t psa_import_key_to_handle( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100907 psa_key_type_t type,
908 const uint8_t *data,
909 size_t data_length )
910{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100911 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100912 psa_status_t status;
913
Gilles Peskinec5487a82018-12-03 18:08:14 +0100914 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100915 if( status != PSA_SUCCESS )
916 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100917
918 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100919
920 status = psa_import_key_into_slot( slot, data, data_length );
921 if( status != PSA_SUCCESS )
922 {
923 slot->type = PSA_KEY_TYPE_NONE;
924 return( status );
925 }
926
Darryl Greend49a4992018-06-18 17:27:26 +0100927#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
928 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
929 {
930 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100931 status = psa_save_persistent_key( slot->persistent_storage_id,
932 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100933 data_length );
934 if( status != PSA_SUCCESS )
935 {
936 (void) psa_remove_key_data_from_memory( slot );
937 slot->type = PSA_KEY_TYPE_NONE;
938 }
939 }
940#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
941
942 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943}
944
Gilles Peskinec5487a82018-12-03 18:08:14 +0100945psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100948 psa_status_t status = PSA_SUCCESS;
949 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950
Gilles Peskinec5487a82018-12-03 18:08:14 +0100951 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200952 if( status != PSA_SUCCESS )
953 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100954#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
955 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
956 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100957 storage_status =
958 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100959 }
960#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100961 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100962 if( status != PSA_SUCCESS )
963 return( status );
964 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100965}
966
Gilles Peskineb870b182018-07-06 16:02:09 +0200967/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200968static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200969{
970 if( key_type_is_raw_bytes( slot->type ) )
971 return( slot->data.raw.bytes * 8 );
972#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100974 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200975#endif /* defined(MBEDTLS_RSA_C) */
976#if defined(MBEDTLS_ECP_C)
977 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
978 return( slot->data.ecp->grp.pbits );
979#endif /* defined(MBEDTLS_ECP_C) */
980 /* Shouldn't happen except on an empty slot. */
981 return( 0 );
982}
983
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200984void psa_reset_key_attributes( psa_key_attributes_t *attributes )
985{
Gilles Peskineb699f072019-04-26 16:06:02 +0200986 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200987 memset( attributes, 0, sizeof( *attributes ) );
988}
989
Gilles Peskineb699f072019-04-26 16:06:02 +0200990psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
991 psa_key_type_t type,
992 const uint8_t *data,
993 size_t data_length )
994{
995 uint8_t *copy = NULL;
996
997 if( data_length != 0 )
998 {
999 copy = mbedtls_calloc( 1, data_length );
1000 if( copy == NULL )
1001 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1002 memcpy( copy, data, data_length );
1003 }
1004 /* After this point, this function is guaranteed to succeed, so it
1005 * can start modifying `*attributes`. */
1006
1007 if( attributes->domain_parameters != NULL )
1008 {
1009 mbedtls_free( attributes->domain_parameters );
1010 attributes->domain_parameters = NULL;
1011 attributes->domain_parameters_size = 0;
1012 }
1013
1014 attributes->domain_parameters = copy;
1015 attributes->domain_parameters_size = data_length;
1016 attributes->type = type;
1017 return( PSA_SUCCESS );
1018}
1019
1020psa_status_t psa_get_key_domain_parameters(
1021 const psa_key_attributes_t *attributes,
1022 uint8_t *data, size_t data_size, size_t *data_length )
1023{
1024 if( attributes->domain_parameters_size > data_size )
1025 return( PSA_ERROR_BUFFER_TOO_SMALL );
1026 *data_length = attributes->domain_parameters_size;
1027 if( attributes->domain_parameters_size != 0 )
1028 memcpy( data, attributes->domain_parameters,
1029 attributes->domain_parameters_size );
1030 return( PSA_SUCCESS );
1031}
1032
1033#if defined(MBEDTLS_RSA_C)
1034static psa_status_t psa_get_rsa_public_exponent(
1035 const mbedtls_rsa_context *rsa,
1036 psa_key_attributes_t *attributes )
1037{
1038 mbedtls_mpi mpi;
1039 int ret;
1040 uint8_t *buffer = NULL;
1041 size_t buflen;
1042 mbedtls_mpi_init( &mpi );
1043
1044 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1045 if( ret != 0 )
1046 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001047 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1048 {
1049 /* It's the default value, which is reported as an empty string,
1050 * so there's nothing to do. */
1051 goto exit;
1052 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001053
1054 buflen = mbedtls_mpi_size( &mpi );
1055 buffer = mbedtls_calloc( 1, buflen );
1056 if( buffer == NULL )
1057 {
1058 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1059 goto exit;
1060 }
1061 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1062 if( ret != 0 )
1063 goto exit;
1064 attributes->domain_parameters = buffer;
1065 attributes->domain_parameters_size = buflen;
1066
1067exit:
1068 mbedtls_mpi_free( &mpi );
1069 if( ret != 0 )
1070 mbedtls_free( buffer );
1071 return( mbedtls_to_psa_error( ret ) );
1072}
1073#endif /* MBEDTLS_RSA_C */
1074
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001075psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1076 psa_key_attributes_t *attributes )
1077{
1078 psa_key_slot_t *slot;
1079 psa_status_t status;
1080
1081 psa_reset_key_attributes( attributes );
1082
1083 status = psa_get_key_slot( handle, &slot );
1084 if( status != PSA_SUCCESS )
1085 return( status );
1086
1087 attributes->id = slot->persistent_storage_id;
1088 attributes->lifetime = slot->lifetime;
1089 attributes->policy = slot->policy;
1090 attributes->type = slot->type;
1091 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001092
1093 switch( slot->type )
1094 {
1095#if defined(MBEDTLS_RSA_C)
1096 case PSA_KEY_TYPE_RSA_KEYPAIR:
1097 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1098 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1099 break;
1100#endif
1101 default:
1102 /* Nothing else to do. */
1103 break;
1104 }
1105
1106 if( status != PSA_SUCCESS )
1107 psa_reset_key_attributes( attributes );
1108 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001109}
1110
Gilles Peskinec5487a82018-12-03 18:08:14 +01001111psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001112 psa_key_type_t *type,
1113 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001114{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001115 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001116 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001117
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001118 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001119 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001120 if( bits != NULL )
1121 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001122 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001123 if( status != PSA_SUCCESS )
1124 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001125
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001126 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001127 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001128 if( type != NULL )
1129 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001130 if( bits != NULL )
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001131 *bits = psa_get_key_slot_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001132 return( PSA_SUCCESS );
1133}
1134
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001135#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001136static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1137 unsigned char *buf, size_t size )
1138{
1139 int ret;
1140 unsigned char *c;
1141 size_t len = 0;
1142
1143 c = buf + size;
1144
1145 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1146
1147 return( (int) len );
1148}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001149#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001150
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001151static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1152 uint8_t *data,
1153 size_t data_size,
1154 size_t *data_length,
1155 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001156{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001157 *data_length = 0;
1158
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001159 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001160 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001161
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001162 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001163 {
1164 if( slot->data.raw.bytes > data_size )
1165 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001166 if( data_size != 0 )
1167 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001168 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001169 memset( data + slot->data.raw.bytes, 0,
1170 data_size - slot->data.raw.bytes );
1171 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001172 *data_length = slot->data.raw.bytes;
1173 return( PSA_SUCCESS );
1174 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001175#if defined(MBEDTLS_ECP_C)
1176 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1177 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001178 psa_status_t status;
1179
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001180 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001181 if( bytes > data_size )
1182 return( PSA_ERROR_BUFFER_TOO_SMALL );
1183 status = mbedtls_to_psa_error(
1184 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1185 if( status != PSA_SUCCESS )
1186 return( status );
1187 memset( data + bytes, 0, data_size - bytes );
1188 *data_length = bytes;
1189 return( PSA_SUCCESS );
1190 }
1191#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001192 else
Moran Peker17e36e12018-05-02 12:55:20 +03001193 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001194#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001195 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001196 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001197 {
Moran Pekera998bc62018-04-16 18:16:20 +03001198 mbedtls_pk_context pk;
1199 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001200 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001201 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001202#if defined(MBEDTLS_RSA_C)
1203 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001204 pk.pk_info = &mbedtls_rsa_info;
1205 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001206#else
1207 return( PSA_ERROR_NOT_SUPPORTED );
1208#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001209 }
1210 else
1211 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001212#if defined(MBEDTLS_ECP_C)
1213 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001214 pk.pk_info = &mbedtls_eckey_info;
1215 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001216#else
1217 return( PSA_ERROR_NOT_SUPPORTED );
1218#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001219 }
Moran Pekerd7326592018-05-29 16:56:39 +03001220 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001221 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001222 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001223 }
Moran Peker17e36e12018-05-02 12:55:20 +03001224 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001225 {
Moran Peker17e36e12018-05-02 12:55:20 +03001226 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001227 }
Moran Peker60364322018-04-29 11:34:58 +03001228 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001229 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001230 /* If data_size is 0 then data may be NULL and then the
1231 * call to memset would have undefined behavior. */
1232 if( data_size != 0 )
1233 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001234 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001235 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001236 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1237 * Move the data to the beginning and erase remaining data
1238 * at the original location. */
1239 if( 2 * (size_t) ret <= data_size )
1240 {
1241 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001242 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001243 }
1244 else if( (size_t) ret < data_size )
1245 {
1246 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001247 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001248 }
Moran Pekera998bc62018-04-16 18:16:20 +03001249 *data_length = ret;
1250 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001251 }
1252 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001253#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001254 {
1255 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001256 it is valid for a special-purpose implementation to omit
1257 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001258 return( PSA_ERROR_NOT_SUPPORTED );
1259 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001260 }
1261}
1262
Gilles Peskinec5487a82018-12-03 18:08:14 +01001263psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001264 uint8_t *data,
1265 size_t data_size,
1266 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001267{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001268 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001269 psa_status_t status;
1270
1271 /* Set the key to empty now, so that even when there are errors, we always
1272 * set data_length to a value between 0 and data_size. On error, setting
1273 * the key to empty is a good choice because an empty key representation is
1274 * unlikely to be accepted anywhere. */
1275 *data_length = 0;
1276
1277 /* Export requires the EXPORT flag. There is an exception for public keys,
1278 * which don't require any flag, but psa_get_key_from_slot takes
1279 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001280 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001281 if( status != PSA_SUCCESS )
1282 return( status );
1283 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001284 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001286
Gilles Peskinec5487a82018-12-03 18:08:14 +01001287psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001288 uint8_t *data,
1289 size_t data_size,
1290 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001291{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001292 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001293 psa_status_t status;
1294
1295 /* Set the key to empty now, so that even when there are errors, we always
1296 * set data_length to a value between 0 and data_size. On error, setting
1297 * the key to empty is a good choice because an empty key representation is
1298 * unlikely to be accepted anywhere. */
1299 *data_length = 0;
1300
1301 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001302 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001303 if( status != PSA_SUCCESS )
1304 return( status );
1305 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001306 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001307}
1308
Darryl Green0c6575a2018-11-07 16:05:30 +00001309#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001310static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001311 size_t bits )
1312{
1313 psa_status_t status;
1314 uint8_t *data;
1315 size_t key_length;
1316 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1317 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001318 if( data == NULL )
1319 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001320 /* Get key data in export format */
1321 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1322 if( status != PSA_SUCCESS )
1323 {
1324 slot->type = PSA_KEY_TYPE_NONE;
1325 goto exit;
1326 }
1327 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001328 status = psa_save_persistent_key( slot->persistent_storage_id,
1329 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001330 data, key_length );
1331 if( status != PSA_SUCCESS )
1332 {
1333 slot->type = PSA_KEY_TYPE_NONE;
1334 }
1335exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001336 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001337 mbedtls_free( data );
1338 return( status );
1339}
1340#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1341
Gilles Peskine4747d192019-04-17 15:05:45 +02001342static psa_status_t psa_set_key_policy_internal(
1343 psa_key_slot_t *slot,
1344 const psa_key_policy_t *policy )
1345{
1346 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
1347 PSA_KEY_USAGE_ENCRYPT |
1348 PSA_KEY_USAGE_DECRYPT |
1349 PSA_KEY_USAGE_SIGN |
1350 PSA_KEY_USAGE_VERIFY |
1351 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1352 return( PSA_ERROR_INVALID_ARGUMENT );
1353
1354 slot->policy = *policy;
1355 return( PSA_SUCCESS );
1356}
1357
1358/** Prepare a key slot to receive key material.
1359 *
1360 * This function allocates a key slot and sets its metadata.
1361 *
1362 * If this function fails, call psa_fail_key_creation().
1363 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001364 * This function is intended to be used as follows:
1365 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1366 * it with the specified attributes, and assign it a handle.
1367 * -# Populate the slot with the key material.
1368 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1369 * In case of failure at any step, stop the sequence and call
1370 * psa_fail_key_creation().
1371 *
Gilles Peskine4747d192019-04-17 15:05:45 +02001372 * \param attributes Key attributes for the new key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001373 * \param handle On success, a handle for the allocated slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001374 * \param p_slot On success, a pointer to the prepared slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001375 *
1376 * \retval #PSA_SUCCESS
1377 * The key slot is ready to receive key material.
1378 * \return If this function fails, the key slot is an invalid state.
1379 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001380 */
1381static psa_status_t psa_start_key_creation(
1382 const psa_key_attributes_t *attributes,
1383 psa_key_handle_t *handle,
1384 psa_key_slot_t **p_slot )
1385{
1386 psa_status_t status;
1387 psa_key_slot_t *slot;
1388
1389 status = psa_allocate_key( handle );
1390 if( status != PSA_SUCCESS )
1391 return( status );
1392 status = psa_get_key_slot( *handle, p_slot );
1393 if( status != PSA_SUCCESS )
1394 return( status );
1395 slot = *p_slot;
1396
1397 status = psa_set_key_policy_internal( slot, &attributes->policy );
1398 if( status != PSA_SUCCESS )
1399 return( status );
1400 slot->lifetime = attributes->lifetime;
1401 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001402 {
1403 status = psa_validate_persistent_key_parameters( attributes->lifetime,
1404 attributes->id );
1405 if( status != PSA_SUCCESS )
1406 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001407 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001408 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001409 slot->type = attributes->type;
1410
1411 return( status );
1412}
1413
1414/** Finalize the creation of a key once its key material has been set.
1415 *
1416 * This entails writing the key to persistent storage.
1417 *
1418 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001419 * See the documentation of psa_start_key_creation() for the intended use
1420 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001421 *
1422 * \param slot Pointer to the slot with key material.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001423 *
1424 * \retval #PSA_SUCCESS
1425 * The key was successfully created. The handle is now valid.
1426 * \return If this function fails, the key slot is an invalid state.
1427 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001428 */
1429static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1430{
1431 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001432 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001433
1434#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1435 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1436 {
1437 uint8_t *buffer = NULL;
1438 size_t buffer_size = 0;
1439 size_t length;
1440
1441 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001442 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001443 buffer = mbedtls_calloc( 1, buffer_size );
1444 if( buffer == NULL && buffer_size != 0 )
1445 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1446 status = psa_internal_export_key( slot,
1447 buffer, buffer_size, &length,
1448 0 );
1449
1450 if( status == PSA_SUCCESS )
1451 {
1452 status = psa_save_persistent_key( slot->persistent_storage_id,
1453 slot->type, &slot->policy,
1454 buffer, length );
1455 }
1456
1457 if( buffer_size != 0 )
1458 mbedtls_platform_zeroize( buffer, buffer_size );
1459 mbedtls_free( buffer );
1460 }
1461#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1462
1463 return( status );
1464}
1465
1466/** Abort the creation of a key.
1467 *
1468 * You may call this function after calling psa_start_key_creation(),
1469 * or after psa_finish_key_creation() fails. In other circumstances, this
1470 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001471 * See the documentation of psa_start_key_creation() for the intended use
1472 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001473 *
1474 * \param slot Pointer to the slot with key material.
1475 */
1476static void psa_fail_key_creation( psa_key_slot_t *slot )
1477{
1478 if( slot == NULL )
1479 return;
1480 psa_wipe_key_slot( slot );
1481}
1482
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001483static psa_status_t psa_check_key_slot_attributes(
1484 const psa_key_slot_t *slot,
1485 const psa_key_attributes_t *attributes )
1486{
1487 if( attributes->type != 0 )
1488 {
1489 if( attributes->type != slot->type )
1490 return( PSA_ERROR_INVALID_ARGUMENT );
1491 }
1492
1493 if( attributes->domain_parameters_size != 0 )
1494 {
1495#if defined(MBEDTLS_RSA_C)
1496 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1497 {
1498 mbedtls_mpi actual, required;
1499 int ret;
1500 mbedtls_mpi_init( &actual );
1501 mbedtls_mpi_init( &required );
1502 ret = mbedtls_rsa_export( slot->data.rsa,
1503 NULL, NULL, NULL, NULL, &actual );
1504 if( ret != 0 )
1505 goto rsa_exit;
1506 ret = mbedtls_mpi_read_binary( &required,
1507 attributes->domain_parameters,
1508 attributes->domain_parameters_size );
1509 if( ret != 0 )
1510 goto rsa_exit;
1511 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1512 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1513 rsa_exit:
1514 mbedtls_mpi_free( &actual );
1515 mbedtls_mpi_free( &required );
1516 if( ret != 0)
1517 return( mbedtls_to_psa_error( ret ) );
1518 }
1519 else
1520#endif
1521 {
1522 return( PSA_ERROR_INVALID_ARGUMENT );
1523 }
1524 }
1525
1526 if( attributes->bits != 0 )
1527 {
1528 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1529 return( PSA_ERROR_INVALID_ARGUMENT );
1530 }
1531
1532 return( PSA_SUCCESS );
1533}
1534
Gilles Peskine4747d192019-04-17 15:05:45 +02001535psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
1536 psa_key_handle_t *handle,
1537 const uint8_t *data,
1538 size_t data_length )
1539{
1540 psa_status_t status;
1541 psa_key_slot_t *slot = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001542
Gilles Peskine4747d192019-04-17 15:05:45 +02001543 status = psa_start_key_creation( attributes, handle, &slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001544 if( status != PSA_SUCCESS )
1545 goto exit;
1546
1547 status = psa_import_key_into_slot( slot, data, data_length );
1548 if( status != PSA_SUCCESS )
1549 goto exit;
1550 status = psa_check_key_slot_attributes( slot, attributes );
1551 if( status != PSA_SUCCESS )
1552 goto exit;
1553
1554 status = psa_finish_key_creation( slot );
1555exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001556 if( status != PSA_SUCCESS )
1557 {
1558 psa_fail_key_creation( slot );
1559 *handle = 0;
1560 }
1561 return( status );
1562}
1563
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001564static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001565 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001566{
1567 psa_status_t status;
1568 uint8_t *buffer = NULL;
1569 size_t buffer_size = 0;
1570 size_t length;
1571
1572 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001573 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001574 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001575 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001576 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001577 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1578 if( status != PSA_SUCCESS )
1579 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001580 target->type = source->type;
1581 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001582
1583exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001584 if( buffer_size != 0 )
1585 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001586 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001587 return( status );
1588}
1589
Gilles Peskine87a5e562019-04-17 12:28:25 +02001590psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001591 psa_key_handle_t target_handle,
1592 const psa_key_policy_t *constraint)
1593{
1594 psa_key_slot_t *source_slot = NULL;
1595 psa_key_slot_t *target_slot = NULL;
1596 psa_key_policy_t new_policy;
1597 psa_status_t status;
1598 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1599 if( status != PSA_SUCCESS )
1600 return( status );
1601 status = psa_get_empty_key_slot( target_handle, &target_slot );
1602 if( status != PSA_SUCCESS )
1603 return( status );
1604
1605 new_policy = target_slot->policy;
1606 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1607 if( status != PSA_SUCCESS )
1608 return( status );
1609 if( constraint != NULL )
1610 {
1611 status = psa_restrict_key_policy( &new_policy, constraint );
1612 if( status != PSA_SUCCESS )
1613 return( status );
1614 }
1615
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001616 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001617 if( status != PSA_SUCCESS )
1618 return( status );
1619
1620 target_slot->policy = new_policy;
1621 return( PSA_SUCCESS );
1622}
1623
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001624psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1625 const psa_key_attributes_t *specified_attributes,
1626 psa_key_handle_t *target_handle )
1627{
1628 psa_status_t status;
1629 psa_key_slot_t *source_slot = NULL;
1630 psa_key_slot_t *target_slot = NULL;
1631 psa_key_attributes_t actual_attributes = *specified_attributes;
1632
1633 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1634 if( status != PSA_SUCCESS )
1635 goto exit;
1636
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001637 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1638 if( status != PSA_SUCCESS )
1639 goto exit;
1640
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001641 status = psa_restrict_key_policy( &actual_attributes.policy,
1642 &source_slot->policy );
1643 if( status != PSA_SUCCESS )
1644 goto exit;
1645
1646 status = psa_start_key_creation( &actual_attributes,
1647 target_handle, &target_slot );
1648 if( status != PSA_SUCCESS )
1649 goto exit;
1650
1651 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001652 if( status != PSA_SUCCESS )
1653 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001654
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001655 status = psa_finish_key_creation( target_slot );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001656exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001657 if( status != PSA_SUCCESS )
1658 {
1659 psa_fail_key_creation( target_slot );
1660 *target_handle = 0;
1661 }
1662 return( status );
1663}
1664
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001665
1666
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001667/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001668/* Message digests */
1669/****************************************************************/
1670
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001671static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001672{
1673 switch( alg )
1674 {
1675#if defined(MBEDTLS_MD2_C)
1676 case PSA_ALG_MD2:
1677 return( &mbedtls_md2_info );
1678#endif
1679#if defined(MBEDTLS_MD4_C)
1680 case PSA_ALG_MD4:
1681 return( &mbedtls_md4_info );
1682#endif
1683#if defined(MBEDTLS_MD5_C)
1684 case PSA_ALG_MD5:
1685 return( &mbedtls_md5_info );
1686#endif
1687#if defined(MBEDTLS_RIPEMD160_C)
1688 case PSA_ALG_RIPEMD160:
1689 return( &mbedtls_ripemd160_info );
1690#endif
1691#if defined(MBEDTLS_SHA1_C)
1692 case PSA_ALG_SHA_1:
1693 return( &mbedtls_sha1_info );
1694#endif
1695#if defined(MBEDTLS_SHA256_C)
1696 case PSA_ALG_SHA_224:
1697 return( &mbedtls_sha224_info );
1698 case PSA_ALG_SHA_256:
1699 return( &mbedtls_sha256_info );
1700#endif
1701#if defined(MBEDTLS_SHA512_C)
1702 case PSA_ALG_SHA_384:
1703 return( &mbedtls_sha384_info );
1704 case PSA_ALG_SHA_512:
1705 return( &mbedtls_sha512_info );
1706#endif
1707 default:
1708 return( NULL );
1709 }
1710}
1711
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001712psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1713{
1714 switch( operation->alg )
1715 {
Gilles Peskine81736312018-06-26 15:04:31 +02001716 case 0:
1717 /* The object has (apparently) been initialized but it is not
1718 * in use. It's ok to call abort on such an object, and there's
1719 * nothing to do. */
1720 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001721#if defined(MBEDTLS_MD2_C)
1722 case PSA_ALG_MD2:
1723 mbedtls_md2_free( &operation->ctx.md2 );
1724 break;
1725#endif
1726#if defined(MBEDTLS_MD4_C)
1727 case PSA_ALG_MD4:
1728 mbedtls_md4_free( &operation->ctx.md4 );
1729 break;
1730#endif
1731#if defined(MBEDTLS_MD5_C)
1732 case PSA_ALG_MD5:
1733 mbedtls_md5_free( &operation->ctx.md5 );
1734 break;
1735#endif
1736#if defined(MBEDTLS_RIPEMD160_C)
1737 case PSA_ALG_RIPEMD160:
1738 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1739 break;
1740#endif
1741#if defined(MBEDTLS_SHA1_C)
1742 case PSA_ALG_SHA_1:
1743 mbedtls_sha1_free( &operation->ctx.sha1 );
1744 break;
1745#endif
1746#if defined(MBEDTLS_SHA256_C)
1747 case PSA_ALG_SHA_224:
1748 case PSA_ALG_SHA_256:
1749 mbedtls_sha256_free( &operation->ctx.sha256 );
1750 break;
1751#endif
1752#if defined(MBEDTLS_SHA512_C)
1753 case PSA_ALG_SHA_384:
1754 case PSA_ALG_SHA_512:
1755 mbedtls_sha512_free( &operation->ctx.sha512 );
1756 break;
1757#endif
1758 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001759 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001760 }
1761 operation->alg = 0;
1762 return( PSA_SUCCESS );
1763}
1764
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001765psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001766 psa_algorithm_t alg )
1767{
1768 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001769
1770 /* A context must be freshly initialized before it can be set up. */
1771 if( operation->alg != 0 )
1772 {
1773 return( PSA_ERROR_BAD_STATE );
1774 }
1775
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001776 switch( alg )
1777 {
1778#if defined(MBEDTLS_MD2_C)
1779 case PSA_ALG_MD2:
1780 mbedtls_md2_init( &operation->ctx.md2 );
1781 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1782 break;
1783#endif
1784#if defined(MBEDTLS_MD4_C)
1785 case PSA_ALG_MD4:
1786 mbedtls_md4_init( &operation->ctx.md4 );
1787 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1788 break;
1789#endif
1790#if defined(MBEDTLS_MD5_C)
1791 case PSA_ALG_MD5:
1792 mbedtls_md5_init( &operation->ctx.md5 );
1793 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1794 break;
1795#endif
1796#if defined(MBEDTLS_RIPEMD160_C)
1797 case PSA_ALG_RIPEMD160:
1798 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1799 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1800 break;
1801#endif
1802#if defined(MBEDTLS_SHA1_C)
1803 case PSA_ALG_SHA_1:
1804 mbedtls_sha1_init( &operation->ctx.sha1 );
1805 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1806 break;
1807#endif
1808#if defined(MBEDTLS_SHA256_C)
1809 case PSA_ALG_SHA_224:
1810 mbedtls_sha256_init( &operation->ctx.sha256 );
1811 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1812 break;
1813 case PSA_ALG_SHA_256:
1814 mbedtls_sha256_init( &operation->ctx.sha256 );
1815 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1816 break;
1817#endif
1818#if defined(MBEDTLS_SHA512_C)
1819 case PSA_ALG_SHA_384:
1820 mbedtls_sha512_init( &operation->ctx.sha512 );
1821 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1822 break;
1823 case PSA_ALG_SHA_512:
1824 mbedtls_sha512_init( &operation->ctx.sha512 );
1825 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1826 break;
1827#endif
1828 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001829 return( PSA_ALG_IS_HASH( alg ) ?
1830 PSA_ERROR_NOT_SUPPORTED :
1831 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001832 }
1833 if( ret == 0 )
1834 operation->alg = alg;
1835 else
1836 psa_hash_abort( operation );
1837 return( mbedtls_to_psa_error( ret ) );
1838}
1839
1840psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1841 const uint8_t *input,
1842 size_t input_length )
1843{
1844 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001845
1846 /* Don't require hash implementations to behave correctly on a
1847 * zero-length input, which may have an invalid pointer. */
1848 if( input_length == 0 )
1849 return( PSA_SUCCESS );
1850
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001851 switch( operation->alg )
1852 {
1853#if defined(MBEDTLS_MD2_C)
1854 case PSA_ALG_MD2:
1855 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1856 input, input_length );
1857 break;
1858#endif
1859#if defined(MBEDTLS_MD4_C)
1860 case PSA_ALG_MD4:
1861 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1862 input, input_length );
1863 break;
1864#endif
1865#if defined(MBEDTLS_MD5_C)
1866 case PSA_ALG_MD5:
1867 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1868 input, input_length );
1869 break;
1870#endif
1871#if defined(MBEDTLS_RIPEMD160_C)
1872 case PSA_ALG_RIPEMD160:
1873 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1874 input, input_length );
1875 break;
1876#endif
1877#if defined(MBEDTLS_SHA1_C)
1878 case PSA_ALG_SHA_1:
1879 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1880 input, input_length );
1881 break;
1882#endif
1883#if defined(MBEDTLS_SHA256_C)
1884 case PSA_ALG_SHA_224:
1885 case PSA_ALG_SHA_256:
1886 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1887 input, input_length );
1888 break;
1889#endif
1890#if defined(MBEDTLS_SHA512_C)
1891 case PSA_ALG_SHA_384:
1892 case PSA_ALG_SHA_512:
1893 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1894 input, input_length );
1895 break;
1896#endif
1897 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001898 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001899 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001900
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001901 if( ret != 0 )
1902 psa_hash_abort( operation );
1903 return( mbedtls_to_psa_error( ret ) );
1904}
1905
1906psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1907 uint8_t *hash,
1908 size_t hash_size,
1909 size_t *hash_length )
1910{
itayzafrir40835d42018-08-02 13:14:17 +03001911 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001912 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001913 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001914
1915 /* Fill the output buffer with something that isn't a valid hash
1916 * (barring an attack on the hash and deliberately-crafted input),
1917 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001918 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001919 /* If hash_size is 0 then hash may be NULL and then the
1920 * call to memset would have undefined behavior. */
1921 if( hash_size != 0 )
1922 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001923
1924 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001925 {
1926 status = PSA_ERROR_BUFFER_TOO_SMALL;
1927 goto exit;
1928 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001929
1930 switch( operation->alg )
1931 {
1932#if defined(MBEDTLS_MD2_C)
1933 case PSA_ALG_MD2:
1934 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1935 break;
1936#endif
1937#if defined(MBEDTLS_MD4_C)
1938 case PSA_ALG_MD4:
1939 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1940 break;
1941#endif
1942#if defined(MBEDTLS_MD5_C)
1943 case PSA_ALG_MD5:
1944 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1945 break;
1946#endif
1947#if defined(MBEDTLS_RIPEMD160_C)
1948 case PSA_ALG_RIPEMD160:
1949 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1950 break;
1951#endif
1952#if defined(MBEDTLS_SHA1_C)
1953 case PSA_ALG_SHA_1:
1954 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1955 break;
1956#endif
1957#if defined(MBEDTLS_SHA256_C)
1958 case PSA_ALG_SHA_224:
1959 case PSA_ALG_SHA_256:
1960 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1961 break;
1962#endif
1963#if defined(MBEDTLS_SHA512_C)
1964 case PSA_ALG_SHA_384:
1965 case PSA_ALG_SHA_512:
1966 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1967 break;
1968#endif
1969 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001970 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001971 }
itayzafrir40835d42018-08-02 13:14:17 +03001972 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001973
itayzafrir40835d42018-08-02 13:14:17 +03001974exit:
1975 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001976 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001977 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001978 return( psa_hash_abort( operation ) );
1979 }
1980 else
1981 {
1982 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001983 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001984 }
1985}
1986
Gilles Peskine2d277862018-06-18 15:41:12 +02001987psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1988 const uint8_t *hash,
1989 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001990{
1991 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1992 size_t actual_hash_length;
1993 psa_status_t status = psa_hash_finish( operation,
1994 actual_hash, sizeof( actual_hash ),
1995 &actual_hash_length );
1996 if( status != PSA_SUCCESS )
1997 return( status );
1998 if( actual_hash_length != hash_length )
1999 return( PSA_ERROR_INVALID_SIGNATURE );
2000 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2001 return( PSA_ERROR_INVALID_SIGNATURE );
2002 return( PSA_SUCCESS );
2003}
2004
Gilles Peskineeb35d782019-01-22 17:56:16 +01002005psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2006 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002007{
2008 if( target_operation->alg != 0 )
2009 return( PSA_ERROR_BAD_STATE );
2010
2011 switch( source_operation->alg )
2012 {
2013 case 0:
2014 return( PSA_ERROR_BAD_STATE );
2015#if defined(MBEDTLS_MD2_C)
2016 case PSA_ALG_MD2:
2017 mbedtls_md2_clone( &target_operation->ctx.md2,
2018 &source_operation->ctx.md2 );
2019 break;
2020#endif
2021#if defined(MBEDTLS_MD4_C)
2022 case PSA_ALG_MD4:
2023 mbedtls_md4_clone( &target_operation->ctx.md4,
2024 &source_operation->ctx.md4 );
2025 break;
2026#endif
2027#if defined(MBEDTLS_MD5_C)
2028 case PSA_ALG_MD5:
2029 mbedtls_md5_clone( &target_operation->ctx.md5,
2030 &source_operation->ctx.md5 );
2031 break;
2032#endif
2033#if defined(MBEDTLS_RIPEMD160_C)
2034 case PSA_ALG_RIPEMD160:
2035 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2036 &source_operation->ctx.ripemd160 );
2037 break;
2038#endif
2039#if defined(MBEDTLS_SHA1_C)
2040 case PSA_ALG_SHA_1:
2041 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2042 &source_operation->ctx.sha1 );
2043 break;
2044#endif
2045#if defined(MBEDTLS_SHA256_C)
2046 case PSA_ALG_SHA_224:
2047 case PSA_ALG_SHA_256:
2048 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2049 &source_operation->ctx.sha256 );
2050 break;
2051#endif
2052#if defined(MBEDTLS_SHA512_C)
2053 case PSA_ALG_SHA_384:
2054 case PSA_ALG_SHA_512:
2055 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2056 &source_operation->ctx.sha512 );
2057 break;
2058#endif
2059 default:
2060 return( PSA_ERROR_NOT_SUPPORTED );
2061 }
2062
2063 target_operation->alg = source_operation->alg;
2064 return( PSA_SUCCESS );
2065}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002066
2067
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002068/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002069/* MAC */
2070/****************************************************************/
2071
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002072static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002073 psa_algorithm_t alg,
2074 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002075 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002076 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002077{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002078 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002079 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002080
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002081 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002082 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002083
Gilles Peskine8c9def32018-02-08 10:02:12 +01002084 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2085 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002086 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002087 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002088 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002089 mode = MBEDTLS_MODE_STREAM;
2090 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002091 case PSA_ALG_CTR:
2092 mode = MBEDTLS_MODE_CTR;
2093 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002094 case PSA_ALG_CFB:
2095 mode = MBEDTLS_MODE_CFB;
2096 break;
2097 case PSA_ALG_OFB:
2098 mode = MBEDTLS_MODE_OFB;
2099 break;
2100 case PSA_ALG_CBC_NO_PADDING:
2101 mode = MBEDTLS_MODE_CBC;
2102 break;
2103 case PSA_ALG_CBC_PKCS7:
2104 mode = MBEDTLS_MODE_CBC;
2105 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002106 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002107 mode = MBEDTLS_MODE_CCM;
2108 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002109 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002110 mode = MBEDTLS_MODE_GCM;
2111 break;
2112 default:
2113 return( NULL );
2114 }
2115 }
2116 else if( alg == PSA_ALG_CMAC )
2117 mode = MBEDTLS_MODE_ECB;
2118 else if( alg == PSA_ALG_GMAC )
2119 mode = MBEDTLS_MODE_GCM;
2120 else
2121 return( NULL );
2122
2123 switch( key_type )
2124 {
2125 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002126 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002127 break;
2128 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002129 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2130 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002131 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002132 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002133 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002134 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002135 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2136 * but two-key Triple-DES is functionally three-key Triple-DES
2137 * with K1=K3, so that's how we present it to mbedtls. */
2138 if( key_bits == 128 )
2139 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002140 break;
2141 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002142 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002143 break;
2144 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002145 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002146 break;
2147 default:
2148 return( NULL );
2149 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002150 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002151 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002152
Jaeden Amero23bbb752018-06-26 14:16:54 +01002153 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2154 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002155}
2156
Gilles Peskinea05219c2018-11-16 16:02:56 +01002157#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002158static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002159{
Gilles Peskine2d277862018-06-18 15:41:12 +02002160 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002161 {
2162 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002163 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002164 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002165 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002166 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002167 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002168 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002169 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002170 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002171 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002172 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002173 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002174 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002175 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002176 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002177 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002178 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002179 return( 128 );
2180 default:
2181 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002182 }
2183}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002184#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002185
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002186/* Initialize the MAC operation structure. Once this function has been
2187 * called, psa_mac_abort can run and will do the right thing. */
2188static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2189 psa_algorithm_t alg )
2190{
2191 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2192
2193 operation->alg = alg;
2194 operation->key_set = 0;
2195 operation->iv_set = 0;
2196 operation->iv_required = 0;
2197 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002198 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002199
2200#if defined(MBEDTLS_CMAC_C)
2201 if( alg == PSA_ALG_CMAC )
2202 {
2203 operation->iv_required = 0;
2204 mbedtls_cipher_init( &operation->ctx.cmac );
2205 status = PSA_SUCCESS;
2206 }
2207 else
2208#endif /* MBEDTLS_CMAC_C */
2209#if defined(MBEDTLS_MD_C)
2210 if( PSA_ALG_IS_HMAC( operation->alg ) )
2211 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002212 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2213 operation->ctx.hmac.hash_ctx.alg = 0;
2214 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002215 }
2216 else
2217#endif /* MBEDTLS_MD_C */
2218 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002219 if( ! PSA_ALG_IS_MAC( alg ) )
2220 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002221 }
2222
2223 if( status != PSA_SUCCESS )
2224 memset( operation, 0, sizeof( *operation ) );
2225 return( status );
2226}
2227
Gilles Peskine01126fa2018-07-12 17:04:55 +02002228#if defined(MBEDTLS_MD_C)
2229static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2230{
Gilles Peskine3f108122018-12-07 18:14:53 +01002231 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002232 return( psa_hash_abort( &hmac->hash_ctx ) );
2233}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002234
2235static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2236{
2237 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2238 memset( hmac, 0, sizeof( *hmac ) );
2239}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002240#endif /* MBEDTLS_MD_C */
2241
Gilles Peskine8c9def32018-02-08 10:02:12 +01002242psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2243{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002244 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002245 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002246 /* The object has (apparently) been initialized but it is not
2247 * in use. It's ok to call abort on such an object, and there's
2248 * nothing to do. */
2249 return( PSA_SUCCESS );
2250 }
2251 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002252#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002253 if( operation->alg == PSA_ALG_CMAC )
2254 {
2255 mbedtls_cipher_free( &operation->ctx.cmac );
2256 }
2257 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002258#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002259#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002260 if( PSA_ALG_IS_HMAC( operation->alg ) )
2261 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002262 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002263 }
2264 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002265#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002266 {
2267 /* Sanity check (shouldn't happen: operation->alg should
2268 * always have been initialized to a valid value). */
2269 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002270 }
Moran Peker41deec42018-04-04 15:43:05 +03002271
Gilles Peskine8c9def32018-02-08 10:02:12 +01002272 operation->alg = 0;
2273 operation->key_set = 0;
2274 operation->iv_set = 0;
2275 operation->iv_required = 0;
2276 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002277 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002278
Gilles Peskine8c9def32018-02-08 10:02:12 +01002279 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002280
2281bad_state:
2282 /* If abort is called on an uninitialized object, we can't trust
2283 * anything. Wipe the object in case it contains confidential data.
2284 * This may result in a memory leak if a pointer gets overwritten,
2285 * but it's too late to do anything about this. */
2286 memset( operation, 0, sizeof( *operation ) );
2287 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002288}
2289
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002290#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002291static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002292 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002293 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002294 const mbedtls_cipher_info_t *cipher_info )
2295{
2296 int ret;
2297
2298 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002299
2300 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2301 if( ret != 0 )
2302 return( ret );
2303
2304 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2305 slot->data.raw.data,
2306 key_bits );
2307 return( ret );
2308}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002309#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002310
Gilles Peskine248051a2018-06-20 16:09:38 +02002311#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002312static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2313 const uint8_t *key,
2314 size_t key_length,
2315 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002316{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002317 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002318 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002319 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002320 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002321 psa_status_t status;
2322
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002323 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2324 * overflow below. This should never trigger if the hash algorithm
2325 * is implemented correctly. */
2326 /* The size checks against the ipad and opad buffers cannot be written
2327 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2328 * because that triggers -Wlogical-op on GCC 7.3. */
2329 if( block_size > sizeof( ipad ) )
2330 return( PSA_ERROR_NOT_SUPPORTED );
2331 if( block_size > sizeof( hmac->opad ) )
2332 return( PSA_ERROR_NOT_SUPPORTED );
2333 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002334 return( PSA_ERROR_NOT_SUPPORTED );
2335
Gilles Peskined223b522018-06-11 18:12:58 +02002336 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002337 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002338 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002339 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002340 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002341 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002342 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002343 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002344 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002345 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002346 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002347 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002348 }
Gilles Peskine96889972018-07-12 17:07:03 +02002349 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2350 * but it is permitted. It is common when HMAC is used in HKDF, for
2351 * example. Don't call `memcpy` in the 0-length because `key` could be
2352 * an invalid pointer which would make the behavior undefined. */
2353 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002354 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002355
Gilles Peskined223b522018-06-11 18:12:58 +02002356 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2357 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002358 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002359 ipad[i] ^= 0x36;
2360 memset( ipad + key_length, 0x36, block_size - key_length );
2361
2362 /* Copy the key material from ipad to opad, flipping the requisite bits,
2363 * and filling the rest of opad with the requisite constant. */
2364 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002365 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2366 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002367
Gilles Peskine01126fa2018-07-12 17:04:55 +02002368 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002369 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002370 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002371
Gilles Peskine01126fa2018-07-12 17:04:55 +02002372 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002373
2374cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002375 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002376
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002377 return( status );
2378}
Gilles Peskine248051a2018-06-20 16:09:38 +02002379#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002380
Gilles Peskine89167cb2018-07-08 20:12:23 +02002381static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002382 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002383 psa_algorithm_t alg,
2384 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002385{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002386 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002387 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002388 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002389 psa_key_usage_t usage =
2390 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002391 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002392 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002393
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002394 /* A context must be freshly initialized before it can be set up. */
2395 if( operation->alg != 0 )
2396 {
2397 return( PSA_ERROR_BAD_STATE );
2398 }
2399
Gilles Peskined911eb72018-08-14 15:18:45 +02002400 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002401 if( status != PSA_SUCCESS )
2402 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002403 if( is_sign )
2404 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002405
Gilles Peskinec5487a82018-12-03 18:08:14 +01002406 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002407 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002408 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002409 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002410
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002412 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002413 {
2414 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002415 mbedtls_cipher_info_from_psa( full_length_alg,
2416 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002417 int ret;
2418 if( cipher_info == NULL )
2419 {
2420 status = PSA_ERROR_NOT_SUPPORTED;
2421 goto exit;
2422 }
2423 operation->mac_size = cipher_info->block_size;
2424 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2425 status = mbedtls_to_psa_error( ret );
2426 }
2427 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002428#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002429#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002430 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002431 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002432 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002433 if( hash_alg == 0 )
2434 {
2435 status = PSA_ERROR_NOT_SUPPORTED;
2436 goto exit;
2437 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002438
2439 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2440 /* Sanity check. This shouldn't fail on a valid configuration. */
2441 if( operation->mac_size == 0 ||
2442 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2443 {
2444 status = PSA_ERROR_NOT_SUPPORTED;
2445 goto exit;
2446 }
2447
Gilles Peskine01126fa2018-07-12 17:04:55 +02002448 if( slot->type != PSA_KEY_TYPE_HMAC )
2449 {
2450 status = PSA_ERROR_INVALID_ARGUMENT;
2451 goto exit;
2452 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002453
Gilles Peskine01126fa2018-07-12 17:04:55 +02002454 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2455 slot->data.raw.data,
2456 slot->data.raw.bytes,
2457 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002458 }
2459 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002460#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002461 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002462 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002463 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002464 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002465
Gilles Peskined911eb72018-08-14 15:18:45 +02002466 if( truncated == 0 )
2467 {
2468 /* The "normal" case: untruncated algorithm. Nothing to do. */
2469 }
2470 else if( truncated < 4 )
2471 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002472 /* A very short MAC is too short for security since it can be
2473 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2474 * so we make this our minimum, even though 32 bits is still
2475 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002476 status = PSA_ERROR_NOT_SUPPORTED;
2477 }
2478 else if( truncated > operation->mac_size )
2479 {
2480 /* It's impossible to "truncate" to a larger length. */
2481 status = PSA_ERROR_INVALID_ARGUMENT;
2482 }
2483 else
2484 operation->mac_size = truncated;
2485
Gilles Peskinefbfac682018-07-08 20:51:54 +02002486exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002487 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002489 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002491 else
2492 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002493 operation->key_set = 1;
2494 }
2495 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496}
2497
Gilles Peskine89167cb2018-07-08 20:12:23 +02002498psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002499 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002500 psa_algorithm_t alg )
2501{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002502 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002503}
2504
2505psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002506 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002507 psa_algorithm_t alg )
2508{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002509 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002510}
2511
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2513 const uint8_t *input,
2514 size_t input_length )
2515{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002516 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002517 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002518 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002519 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002520 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002521 operation->has_input = 1;
2522
Gilles Peskine8c9def32018-02-08 10:02:12 +01002523#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002524 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002525 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002526 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2527 input, input_length );
2528 status = mbedtls_to_psa_error( ret );
2529 }
2530 else
2531#endif /* MBEDTLS_CMAC_C */
2532#if defined(MBEDTLS_MD_C)
2533 if( PSA_ALG_IS_HMAC( operation->alg ) )
2534 {
2535 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2536 input_length );
2537 }
2538 else
2539#endif /* MBEDTLS_MD_C */
2540 {
2541 /* This shouldn't happen if `operation` was initialized by
2542 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002543 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002544 }
2545
Gilles Peskinefbfac682018-07-08 20:51:54 +02002546 if( status != PSA_SUCCESS )
2547 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002548 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549}
2550
Gilles Peskine01126fa2018-07-12 17:04:55 +02002551#if defined(MBEDTLS_MD_C)
2552static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2553 uint8_t *mac,
2554 size_t mac_size )
2555{
2556 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2557 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2558 size_t hash_size = 0;
2559 size_t block_size = psa_get_hash_block_size( hash_alg );
2560 psa_status_t status;
2561
Gilles Peskine01126fa2018-07-12 17:04:55 +02002562 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2563 if( status != PSA_SUCCESS )
2564 return( status );
2565 /* From here on, tmp needs to be wiped. */
2566
2567 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2568 if( status != PSA_SUCCESS )
2569 goto exit;
2570
2571 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2572 if( status != PSA_SUCCESS )
2573 goto exit;
2574
2575 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2576 if( status != PSA_SUCCESS )
2577 goto exit;
2578
Gilles Peskined911eb72018-08-14 15:18:45 +02002579 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2580 if( status != PSA_SUCCESS )
2581 goto exit;
2582
2583 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002584
2585exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002586 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002587 return( status );
2588}
2589#endif /* MBEDTLS_MD_C */
2590
mohammad16036df908f2018-04-02 08:34:15 -07002591static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002592 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002593 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002595 if( ! operation->key_set )
2596 return( PSA_ERROR_BAD_STATE );
2597 if( operation->iv_required && ! operation->iv_set )
2598 return( PSA_ERROR_BAD_STATE );
2599
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600 if( mac_size < operation->mac_size )
2601 return( PSA_ERROR_BUFFER_TOO_SMALL );
2602
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002604 if( operation->alg == PSA_ALG_CMAC )
2605 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002606 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2607 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2608 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002609 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002610 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002611 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002613 else
2614#endif /* MBEDTLS_CMAC_C */
2615#if defined(MBEDTLS_MD_C)
2616 if( PSA_ALG_IS_HMAC( operation->alg ) )
2617 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002618 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002619 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002620 }
2621 else
2622#endif /* MBEDTLS_MD_C */
2623 {
2624 /* This shouldn't happen if `operation` was initialized by
2625 * a setup function. */
2626 return( PSA_ERROR_BAD_STATE );
2627 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002628}
2629
Gilles Peskineacd4be32018-07-08 19:56:25 +02002630psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2631 uint8_t *mac,
2632 size_t mac_size,
2633 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002634{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002635 psa_status_t status;
2636
Jaeden Amero252ef282019-02-15 14:05:35 +00002637 if( operation->alg == 0 )
2638 {
2639 return( PSA_ERROR_BAD_STATE );
2640 }
2641
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002642 /* Fill the output buffer with something that isn't a valid mac
2643 * (barring an attack on the mac and deliberately-crafted input),
2644 * in case the caller doesn't check the return status properly. */
2645 *mac_length = mac_size;
2646 /* If mac_size is 0 then mac may be NULL and then the
2647 * call to memset would have undefined behavior. */
2648 if( mac_size != 0 )
2649 memset( mac, '!', mac_size );
2650
Gilles Peskine89167cb2018-07-08 20:12:23 +02002651 if( ! operation->is_sign )
2652 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002653 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002654 }
mohammad16036df908f2018-04-02 08:34:15 -07002655
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002656 status = psa_mac_finish_internal( operation, mac, mac_size );
2657
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002658 if( status == PSA_SUCCESS )
2659 {
2660 status = psa_mac_abort( operation );
2661 if( status == PSA_SUCCESS )
2662 *mac_length = operation->mac_size;
2663 else
2664 memset( mac, '!', mac_size );
2665 }
2666 else
2667 psa_mac_abort( operation );
2668 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002669}
2670
Gilles Peskineacd4be32018-07-08 19:56:25 +02002671psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2672 const uint8_t *mac,
2673 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002674{
Gilles Peskine828ed142018-06-18 23:25:51 +02002675 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002676 psa_status_t status;
2677
Jaeden Amero252ef282019-02-15 14:05:35 +00002678 if( operation->alg == 0 )
2679 {
2680 return( PSA_ERROR_BAD_STATE );
2681 }
2682
Gilles Peskine89167cb2018-07-08 20:12:23 +02002683 if( operation->is_sign )
2684 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002685 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002686 }
2687 if( operation->mac_size != mac_length )
2688 {
2689 status = PSA_ERROR_INVALID_SIGNATURE;
2690 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002691 }
mohammad16036df908f2018-04-02 08:34:15 -07002692
2693 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002694 actual_mac, sizeof( actual_mac ) );
2695
2696 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2697 status = PSA_ERROR_INVALID_SIGNATURE;
2698
2699cleanup:
2700 if( status == PSA_SUCCESS )
2701 status = psa_mac_abort( operation );
2702 else
2703 psa_mac_abort( operation );
2704
Gilles Peskine3f108122018-12-07 18:14:53 +01002705 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002706
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002707 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708}
2709
2710
Gilles Peskine20035e32018-02-03 22:44:14 +01002711
Gilles Peskine20035e32018-02-03 22:44:14 +01002712/****************************************************************/
2713/* Asymmetric cryptography */
2714/****************************************************************/
2715
Gilles Peskine2b450e32018-06-27 15:42:46 +02002716#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002717/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002718 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002719static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2720 size_t hash_length,
2721 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002722{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002723 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002724 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002725 *md_alg = mbedtls_md_get_type( md_info );
2726
2727 /* The Mbed TLS RSA module uses an unsigned int for hash length
2728 * parameters. Validate that it fits so that we don't risk an
2729 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002730#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002731 if( hash_length > UINT_MAX )
2732 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002733#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002734
2735#if defined(MBEDTLS_PKCS1_V15)
2736 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2737 * must be correct. */
2738 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2739 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002740 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002741 if( md_info == NULL )
2742 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002743 if( mbedtls_md_get_size( md_info ) != hash_length )
2744 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002745 }
2746#endif /* MBEDTLS_PKCS1_V15 */
2747
2748#if defined(MBEDTLS_PKCS1_V21)
2749 /* PSS requires a hash internally. */
2750 if( PSA_ALG_IS_RSA_PSS( alg ) )
2751 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002752 if( md_info == NULL )
2753 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002754 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002755#endif /* MBEDTLS_PKCS1_V21 */
2756
Gilles Peskine61b91d42018-06-08 16:09:36 +02002757 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002758}
2759
Gilles Peskine2b450e32018-06-27 15:42:46 +02002760static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2761 psa_algorithm_t alg,
2762 const uint8_t *hash,
2763 size_t hash_length,
2764 uint8_t *signature,
2765 size_t signature_size,
2766 size_t *signature_length )
2767{
2768 psa_status_t status;
2769 int ret;
2770 mbedtls_md_type_t md_alg;
2771
2772 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2773 if( status != PSA_SUCCESS )
2774 return( status );
2775
Gilles Peskine630a18a2018-06-29 17:49:35 +02002776 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002777 return( PSA_ERROR_BUFFER_TOO_SMALL );
2778
2779#if defined(MBEDTLS_PKCS1_V15)
2780 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2781 {
2782 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2783 MBEDTLS_MD_NONE );
2784 ret = mbedtls_rsa_pkcs1_sign( rsa,
2785 mbedtls_ctr_drbg_random,
2786 &global_data.ctr_drbg,
2787 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002788 md_alg,
2789 (unsigned int) hash_length,
2790 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002791 signature );
2792 }
2793 else
2794#endif /* MBEDTLS_PKCS1_V15 */
2795#if defined(MBEDTLS_PKCS1_V21)
2796 if( PSA_ALG_IS_RSA_PSS( alg ) )
2797 {
2798 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2799 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2800 mbedtls_ctr_drbg_random,
2801 &global_data.ctr_drbg,
2802 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002803 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002804 (unsigned int) hash_length,
2805 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002806 signature );
2807 }
2808 else
2809#endif /* MBEDTLS_PKCS1_V21 */
2810 {
2811 return( PSA_ERROR_INVALID_ARGUMENT );
2812 }
2813
2814 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002815 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002816 return( mbedtls_to_psa_error( ret ) );
2817}
2818
2819static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2820 psa_algorithm_t alg,
2821 const uint8_t *hash,
2822 size_t hash_length,
2823 const uint8_t *signature,
2824 size_t signature_length )
2825{
2826 psa_status_t status;
2827 int ret;
2828 mbedtls_md_type_t md_alg;
2829
2830 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2831 if( status != PSA_SUCCESS )
2832 return( status );
2833
Gilles Peskine630a18a2018-06-29 17:49:35 +02002834 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002835 return( PSA_ERROR_BUFFER_TOO_SMALL );
2836
2837#if defined(MBEDTLS_PKCS1_V15)
2838 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2839 {
2840 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2841 MBEDTLS_MD_NONE );
2842 ret = mbedtls_rsa_pkcs1_verify( rsa,
2843 mbedtls_ctr_drbg_random,
2844 &global_data.ctr_drbg,
2845 MBEDTLS_RSA_PUBLIC,
2846 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002847 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002848 hash,
2849 signature );
2850 }
2851 else
2852#endif /* MBEDTLS_PKCS1_V15 */
2853#if defined(MBEDTLS_PKCS1_V21)
2854 if( PSA_ALG_IS_RSA_PSS( alg ) )
2855 {
2856 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2857 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2858 mbedtls_ctr_drbg_random,
2859 &global_data.ctr_drbg,
2860 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002861 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002862 (unsigned int) hash_length,
2863 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002864 signature );
2865 }
2866 else
2867#endif /* MBEDTLS_PKCS1_V21 */
2868 {
2869 return( PSA_ERROR_INVALID_ARGUMENT );
2870 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002871
2872 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2873 * the rest of the signature is invalid". This has little use in
2874 * practice and PSA doesn't report this distinction. */
2875 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2876 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002877 return( mbedtls_to_psa_error( ret ) );
2878}
2879#endif /* MBEDTLS_RSA_C */
2880
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002881#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002882/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2883 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2884 * (even though these functions don't modify it). */
2885static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2886 psa_algorithm_t alg,
2887 const uint8_t *hash,
2888 size_t hash_length,
2889 uint8_t *signature,
2890 size_t signature_size,
2891 size_t *signature_length )
2892{
2893 int ret;
2894 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002895 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002896 mbedtls_mpi_init( &r );
2897 mbedtls_mpi_init( &s );
2898
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002899 if( signature_size < 2 * curve_bytes )
2900 {
2901 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2902 goto cleanup;
2903 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002904
Gilles Peskinea05219c2018-11-16 16:02:56 +01002905#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002906 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2907 {
2908 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2909 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2910 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2911 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2912 hash, hash_length,
2913 md_alg ) );
2914 }
2915 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002916#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002917 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002918 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002919 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2920 hash, hash_length,
2921 mbedtls_ctr_drbg_random,
2922 &global_data.ctr_drbg ) );
2923 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002924
2925 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2926 signature,
2927 curve_bytes ) );
2928 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2929 signature + curve_bytes,
2930 curve_bytes ) );
2931
2932cleanup:
2933 mbedtls_mpi_free( &r );
2934 mbedtls_mpi_free( &s );
2935 if( ret == 0 )
2936 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002937 return( mbedtls_to_psa_error( ret ) );
2938}
2939
2940static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2941 const uint8_t *hash,
2942 size_t hash_length,
2943 const uint8_t *signature,
2944 size_t signature_length )
2945{
2946 int ret;
2947 mbedtls_mpi r, s;
2948 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2949 mbedtls_mpi_init( &r );
2950 mbedtls_mpi_init( &s );
2951
2952 if( signature_length != 2 * curve_bytes )
2953 return( PSA_ERROR_INVALID_SIGNATURE );
2954
2955 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2956 signature,
2957 curve_bytes ) );
2958 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2959 signature + curve_bytes,
2960 curve_bytes ) );
2961
2962 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2963 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002964
2965cleanup:
2966 mbedtls_mpi_free( &r );
2967 mbedtls_mpi_free( &s );
2968 return( mbedtls_to_psa_error( ret ) );
2969}
2970#endif /* MBEDTLS_ECDSA_C */
2971
Gilles Peskinec5487a82018-12-03 18:08:14 +01002972psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002973 psa_algorithm_t alg,
2974 const uint8_t *hash,
2975 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002976 uint8_t *signature,
2977 size_t signature_size,
2978 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002979{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002980 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002981 psa_status_t status;
2982
2983 *signature_length = signature_size;
2984
Gilles Peskinec5487a82018-12-03 18:08:14 +01002985 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002986 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002987 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002988 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002989 {
2990 status = PSA_ERROR_INVALID_ARGUMENT;
2991 goto exit;
2992 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002993
Gilles Peskine20035e32018-02-03 22:44:14 +01002994#if defined(MBEDTLS_RSA_C)
2995 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2996 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002997 status = psa_rsa_sign( slot->data.rsa,
2998 alg,
2999 hash, hash_length,
3000 signature, signature_size,
3001 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003002 }
3003 else
3004#endif /* defined(MBEDTLS_RSA_C) */
3005#if defined(MBEDTLS_ECP_C)
3006 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3007 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003008#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003009 if(
3010#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3011 PSA_ALG_IS_ECDSA( alg )
3012#else
3013 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3014#endif
3015 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003016 status = psa_ecdsa_sign( slot->data.ecp,
3017 alg,
3018 hash, hash_length,
3019 signature, signature_size,
3020 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003021 else
3022#endif /* defined(MBEDTLS_ECDSA_C) */
3023 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003024 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003025 }
itayzafrir5c753392018-05-08 11:18:38 +03003026 }
3027 else
3028#endif /* defined(MBEDTLS_ECP_C) */
3029 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003030 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003031 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003032
3033exit:
3034 /* Fill the unused part of the output buffer (the whole buffer on error,
3035 * the trailing part on success) with something that isn't a valid mac
3036 * (barring an attack on the mac and deliberately-crafted input),
3037 * in case the caller doesn't check the return status properly. */
3038 if( status == PSA_SUCCESS )
3039 memset( signature + *signature_length, '!',
3040 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003041 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003042 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003043 /* If signature_size is 0 then we have nothing to do. We must not call
3044 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003045 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003046}
3047
Gilles Peskinec5487a82018-12-03 18:08:14 +01003048psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003049 psa_algorithm_t alg,
3050 const uint8_t *hash,
3051 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003052 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003053 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003054{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003055 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003056 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003057
Gilles Peskinec5487a82018-12-03 18:08:14 +01003058 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003059 if( status != PSA_SUCCESS )
3060 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003061
Gilles Peskine61b91d42018-06-08 16:09:36 +02003062#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003063 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003064 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003065 return( psa_rsa_verify( slot->data.rsa,
3066 alg,
3067 hash, hash_length,
3068 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003069 }
3070 else
3071#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003072#if defined(MBEDTLS_ECP_C)
3073 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3074 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003075#if defined(MBEDTLS_ECDSA_C)
3076 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003077 return( psa_ecdsa_verify( slot->data.ecp,
3078 hash, hash_length,
3079 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003080 else
3081#endif /* defined(MBEDTLS_ECDSA_C) */
3082 {
3083 return( PSA_ERROR_INVALID_ARGUMENT );
3084 }
itayzafrir5c753392018-05-08 11:18:38 +03003085 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003086 else
3087#endif /* defined(MBEDTLS_ECP_C) */
3088 {
3089 return( PSA_ERROR_NOT_SUPPORTED );
3090 }
3091}
3092
Gilles Peskine072ac562018-06-30 00:21:29 +02003093#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3094static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3095 mbedtls_rsa_context *rsa )
3096{
3097 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3098 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3099 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3100 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3101}
3102#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3103
Gilles Peskinec5487a82018-12-03 18:08:14 +01003104psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003105 psa_algorithm_t alg,
3106 const uint8_t *input,
3107 size_t input_length,
3108 const uint8_t *salt,
3109 size_t salt_length,
3110 uint8_t *output,
3111 size_t output_size,
3112 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003113{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003114 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003115 psa_status_t status;
3116
Darryl Green5cc689a2018-07-24 15:34:10 +01003117 (void) input;
3118 (void) input_length;
3119 (void) salt;
3120 (void) output;
3121 (void) output_size;
3122
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003123 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003124
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003125 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3126 return( PSA_ERROR_INVALID_ARGUMENT );
3127
Gilles Peskinec5487a82018-12-03 18:08:14 +01003128 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003129 if( status != PSA_SUCCESS )
3130 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003131 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
3132 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003133 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003134
3135#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003136 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003137 {
3138 mbedtls_rsa_context *rsa = slot->data.rsa;
3139 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003140 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003141 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003143 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003144 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003145 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3146 mbedtls_ctr_drbg_random,
3147 &global_data.ctr_drbg,
3148 MBEDTLS_RSA_PUBLIC,
3149 input_length,
3150 input,
3151 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003152 }
3153 else
3154#endif /* MBEDTLS_PKCS1_V15 */
3155#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003156 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003157 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003158 psa_rsa_oaep_set_padding_mode( alg, rsa );
3159 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3160 mbedtls_ctr_drbg_random,
3161 &global_data.ctr_drbg,
3162 MBEDTLS_RSA_PUBLIC,
3163 salt, salt_length,
3164 input_length,
3165 input,
3166 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003167 }
3168 else
3169#endif /* MBEDTLS_PKCS1_V21 */
3170 {
3171 return( PSA_ERROR_INVALID_ARGUMENT );
3172 }
3173 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003174 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003175 return( mbedtls_to_psa_error( ret ) );
3176 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003178#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003179 {
3180 return( PSA_ERROR_NOT_SUPPORTED );
3181 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003182}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003183
Gilles Peskinec5487a82018-12-03 18:08:14 +01003184psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003185 psa_algorithm_t alg,
3186 const uint8_t *input,
3187 size_t input_length,
3188 const uint8_t *salt,
3189 size_t salt_length,
3190 uint8_t *output,
3191 size_t output_size,
3192 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003193{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003194 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003195 psa_status_t status;
3196
Darryl Green5cc689a2018-07-24 15:34:10 +01003197 (void) input;
3198 (void) input_length;
3199 (void) salt;
3200 (void) output;
3201 (void) output_size;
3202
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003203 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003204
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003205 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3206 return( PSA_ERROR_INVALID_ARGUMENT );
3207
Gilles Peskinec5487a82018-12-03 18:08:14 +01003208 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003209 if( status != PSA_SUCCESS )
3210 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
3212 return( PSA_ERROR_INVALID_ARGUMENT );
3213
3214#if defined(MBEDTLS_RSA_C)
3215 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
3216 {
3217 mbedtls_rsa_context *rsa = slot->data.rsa;
3218 int ret;
3219
Gilles Peskine630a18a2018-06-29 17:49:35 +02003220 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003221 return( PSA_ERROR_INVALID_ARGUMENT );
3222
3223#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003224 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003225 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003226 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3227 mbedtls_ctr_drbg_random,
3228 &global_data.ctr_drbg,
3229 MBEDTLS_RSA_PRIVATE,
3230 output_length,
3231 input,
3232 output,
3233 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003234 }
3235 else
3236#endif /* MBEDTLS_PKCS1_V15 */
3237#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003238 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003239 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003240 psa_rsa_oaep_set_padding_mode( alg, rsa );
3241 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3242 mbedtls_ctr_drbg_random,
3243 &global_data.ctr_drbg,
3244 MBEDTLS_RSA_PRIVATE,
3245 salt, salt_length,
3246 output_length,
3247 input,
3248 output,
3249 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003250 }
3251 else
3252#endif /* MBEDTLS_PKCS1_V21 */
3253 {
3254 return( PSA_ERROR_INVALID_ARGUMENT );
3255 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003256
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003257 return( mbedtls_to_psa_error( ret ) );
3258 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003259 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003260#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003261 {
3262 return( PSA_ERROR_NOT_SUPPORTED );
3263 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003264}
Gilles Peskine20035e32018-02-03 22:44:14 +01003265
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003266
3267
mohammad1603503973b2018-03-12 15:59:30 +02003268/****************************************************************/
3269/* Symmetric cryptography */
3270/****************************************************************/
3271
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003272/* Initialize the cipher operation structure. Once this function has been
3273 * called, psa_cipher_abort can run and will do the right thing. */
3274static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3275 psa_algorithm_t alg )
3276{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003277 if( ! PSA_ALG_IS_CIPHER( alg ) )
3278 {
3279 memset( operation, 0, sizeof( *operation ) );
3280 return( PSA_ERROR_INVALID_ARGUMENT );
3281 }
3282
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003283 operation->alg = alg;
3284 operation->key_set = 0;
3285 operation->iv_set = 0;
3286 operation->iv_required = 1;
3287 operation->iv_size = 0;
3288 operation->block_size = 0;
3289 mbedtls_cipher_init( &operation->ctx.cipher );
3290 return( PSA_SUCCESS );
3291}
3292
Gilles Peskinee553c652018-06-04 16:22:46 +02003293static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003294 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003295 psa_algorithm_t alg,
3296 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003297{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003298 int ret = 0;
3299 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003300 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003301 size_t key_bits;
3302 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003303 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3304 PSA_KEY_USAGE_ENCRYPT :
3305 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003306
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003307 /* A context must be freshly initialized before it can be set up. */
3308 if( operation->alg != 0 )
3309 {
3310 return( PSA_ERROR_BAD_STATE );
3311 }
3312
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003313 status = psa_cipher_init( operation, alg );
3314 if( status != PSA_SUCCESS )
3315 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003316
Gilles Peskinec5487a82018-12-03 18:08:14 +01003317 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003318 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003319 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003320 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003321
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003322 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003323 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003324 {
3325 status = PSA_ERROR_NOT_SUPPORTED;
3326 goto exit;
3327 }
mohammad1603503973b2018-03-12 15:59:30 +02003328
mohammad1603503973b2018-03-12 15:59:30 +02003329 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003330 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003331 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003332
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003333#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003334 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003335 {
3336 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3337 unsigned char keys[24];
3338 memcpy( keys, slot->data.raw.data, 16 );
3339 memcpy( keys + 16, slot->data.raw.data, 8 );
3340 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3341 keys,
3342 192, cipher_operation );
3343 }
3344 else
3345#endif
3346 {
3347 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3348 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003349 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003350 }
Moran Peker41deec42018-04-04 15:43:05 +03003351 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003352 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003353
mohammad16038481e742018-03-18 13:57:31 +02003354#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003355 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003356 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003357 case PSA_ALG_CBC_NO_PADDING:
3358 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3359 MBEDTLS_PADDING_NONE );
3360 break;
3361 case PSA_ALG_CBC_PKCS7:
3362 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3363 MBEDTLS_PADDING_PKCS7 );
3364 break;
3365 default:
3366 /* The algorithm doesn't involve padding. */
3367 ret = 0;
3368 break;
3369 }
3370 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003371 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003372#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3373
mohammad1603503973b2018-03-12 15:59:30 +02003374 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003375 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3376 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3377 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003378 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003379 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003380 }
mohammad1603503973b2018-03-12 15:59:30 +02003381
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003382exit:
3383 if( status == 0 )
3384 status = mbedtls_to_psa_error( ret );
3385 if( status != 0 )
3386 psa_cipher_abort( operation );
3387 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003388}
3389
Gilles Peskinefe119512018-07-08 21:39:34 +02003390psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003391 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003392 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003393{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003394 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003395}
3396
Gilles Peskinefe119512018-07-08 21:39:34 +02003397psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003398 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003399 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003400{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003401 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003402}
3403
Gilles Peskinefe119512018-07-08 21:39:34 +02003404psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3405 unsigned char *iv,
3406 size_t iv_size,
3407 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003408{
itayzafrir534bd7c2018-08-02 13:56:32 +03003409 psa_status_t status;
3410 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003411 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003412 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003413 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003414 }
Moran Peker41deec42018-04-04 15:43:05 +03003415 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003416 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003417 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003418 goto exit;
3419 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003420 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3421 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003422 if( ret != 0 )
3423 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003424 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003425 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003426 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003427
mohammad16038481e742018-03-18 13:57:31 +02003428 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003429 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003430
Moran Peker395db872018-05-31 14:07:14 +03003431exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003432 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003433 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003434 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003435}
3436
Gilles Peskinefe119512018-07-08 21:39:34 +02003437psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3438 const unsigned char *iv,
3439 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003440{
itayzafrir534bd7c2018-08-02 13:56:32 +03003441 psa_status_t status;
3442 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003443 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003444 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003445 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003446 }
Moran Pekera28258c2018-05-29 16:25:04 +03003447 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003448 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003449 status = PSA_ERROR_INVALID_ARGUMENT;
3450 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003451 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003452 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3453 status = mbedtls_to_psa_error( ret );
3454exit:
3455 if( status == PSA_SUCCESS )
3456 operation->iv_set = 1;
3457 else
mohammad1603503973b2018-03-12 15:59:30 +02003458 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003459 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003460}
3461
Gilles Peskinee553c652018-06-04 16:22:46 +02003462psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3463 const uint8_t *input,
3464 size_t input_length,
3465 unsigned char *output,
3466 size_t output_size,
3467 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003468{
itayzafrir534bd7c2018-08-02 13:56:32 +03003469 psa_status_t status;
3470 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003471 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003472
3473 if( operation->alg == 0 )
3474 {
3475 return( PSA_ERROR_BAD_STATE );
3476 }
3477
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003478 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003479 {
3480 /* Take the unprocessed partial block left over from previous
3481 * update calls, if any, plus the input to this call. Remove
3482 * the last partial block, if any. You get the data that will be
3483 * output in this call. */
3484 expected_output_size =
3485 ( operation->ctx.cipher.unprocessed_len + input_length )
3486 / operation->block_size * operation->block_size;
3487 }
3488 else
3489 {
3490 expected_output_size = input_length;
3491 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003492
Gilles Peskine89d789c2018-06-04 17:17:16 +02003493 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003494 {
3495 status = PSA_ERROR_BUFFER_TOO_SMALL;
3496 goto exit;
3497 }
mohammad160382759612018-03-12 18:16:40 +02003498
mohammad1603503973b2018-03-12 15:59:30 +02003499 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003500 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003501 status = mbedtls_to_psa_error( ret );
3502exit:
3503 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003504 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003505 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003506}
3507
Gilles Peskinee553c652018-06-04 16:22:46 +02003508psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3509 uint8_t *output,
3510 size_t output_size,
3511 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003512{
David Saadab4ecc272019-02-14 13:48:10 +02003513 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003514 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003515 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003516
mohammad1603503973b2018-03-12 15:59:30 +02003517 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003518 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003519 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003520 }
3521 if( operation->iv_required && ! operation->iv_set )
3522 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003523 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003524 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003525
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003526 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003527 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3528 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003529 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003530 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003531 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003532 }
3533
Janos Follath315b51c2018-07-09 16:04:51 +01003534 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3535 temp_output_buffer,
3536 output_length );
3537 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003538 {
Janos Follath315b51c2018-07-09 16:04:51 +01003539 status = mbedtls_to_psa_error( cipher_ret );
3540 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003541 }
Janos Follath315b51c2018-07-09 16:04:51 +01003542
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003543 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003544 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003545 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003546 memcpy( output, temp_output_buffer, *output_length );
3547 else
3548 {
Janos Follath315b51c2018-07-09 16:04:51 +01003549 status = PSA_ERROR_BUFFER_TOO_SMALL;
3550 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003551 }
mohammad1603503973b2018-03-12 15:59:30 +02003552
Gilles Peskine3f108122018-12-07 18:14:53 +01003553 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003554 status = psa_cipher_abort( operation );
3555
3556 return( status );
3557
3558error:
3559
3560 *output_length = 0;
3561
Gilles Peskine3f108122018-12-07 18:14:53 +01003562 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003563 (void) psa_cipher_abort( operation );
3564
3565 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003566}
3567
Gilles Peskinee553c652018-06-04 16:22:46 +02003568psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3569{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003570 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003571 {
3572 /* The object has (apparently) been initialized but it is not
3573 * in use. It's ok to call abort on such an object, and there's
3574 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003575 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003576 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003577
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003578 /* Sanity check (shouldn't happen: operation->alg should
3579 * always have been initialized to a valid value). */
3580 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3581 return( PSA_ERROR_BAD_STATE );
3582
mohammad1603503973b2018-03-12 15:59:30 +02003583 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003584
Moran Peker41deec42018-04-04 15:43:05 +03003585 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003586 operation->key_set = 0;
3587 operation->iv_set = 0;
3588 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003589 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003590 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003591
Moran Peker395db872018-05-31 14:07:14 +03003592 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003593}
3594
Gilles Peskinea0655c32018-04-30 17:06:50 +02003595
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003596
mohammad16038cc1cee2018-03-28 01:21:33 +03003597/****************************************************************/
3598/* Key Policy */
3599/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003600
mohammad160327010052018-07-03 13:16:15 +03003601#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003602void psa_key_policy_set_usage( psa_key_policy_t *policy,
3603 psa_key_usage_t usage,
3604 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003605{
mohammad16034eed7572018-03-28 05:14:59 -07003606 policy->usage = usage;
3607 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003608}
3609
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003610psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003611{
mohammad16036df908f2018-04-02 08:34:15 -07003612 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003613}
3614
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003615psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003616{
mohammad16036df908f2018-04-02 08:34:15 -07003617 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003618}
mohammad160327010052018-07-03 13:16:15 +03003619#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003620
Gilles Peskinec5487a82018-12-03 18:08:14 +01003621psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003622 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003623{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003624 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003625 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003626
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003627 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003628 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003629
Gilles Peskinec5487a82018-12-03 18:08:14 +01003630 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003631 if( status != PSA_SUCCESS )
3632 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003633
Gilles Peskine4747d192019-04-17 15:05:45 +02003634 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003635}
3636
Gilles Peskinec5487a82018-12-03 18:08:14 +01003637psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003638 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003639{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003640 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003641 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003642
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003643 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003644 return( PSA_ERROR_INVALID_ARGUMENT );
3645
Gilles Peskinec5487a82018-12-03 18:08:14 +01003646 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003647 if( status != PSA_SUCCESS )
3648 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003649
mohammad16036df908f2018-04-02 08:34:15 -07003650 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003651
3652 return( PSA_SUCCESS );
3653}
Gilles Peskine20035e32018-02-03 22:44:14 +01003654
Gilles Peskinea0655c32018-04-30 17:06:50 +02003655
3656
mohammad1603804cd712018-03-20 22:44:08 +02003657/****************************************************************/
3658/* Key Lifetime */
3659/****************************************************************/
3660
Gilles Peskine87a5e562019-04-17 12:28:25 +02003661psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003662 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003663{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003664 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003665 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003666
Gilles Peskinec5487a82018-12-03 18:08:14 +01003667 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003668 if( status != PSA_SUCCESS )
3669 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003670
mohammad1603804cd712018-03-20 22:44:08 +02003671 *lifetime = slot->lifetime;
3672
3673 return( PSA_SUCCESS );
3674}
3675
Gilles Peskine20035e32018-02-03 22:44:14 +01003676
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003677
mohammad16035955c982018-04-26 00:53:03 +03003678/****************************************************************/
3679/* AEAD */
3680/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003681
Gilles Peskineedf9a652018-08-17 18:11:56 +02003682typedef struct
3683{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003684 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003685 const mbedtls_cipher_info_t *cipher_info;
3686 union
3687 {
3688#if defined(MBEDTLS_CCM_C)
3689 mbedtls_ccm_context ccm;
3690#endif /* MBEDTLS_CCM_C */
3691#if defined(MBEDTLS_GCM_C)
3692 mbedtls_gcm_context gcm;
3693#endif /* MBEDTLS_GCM_C */
3694 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003695 psa_algorithm_t core_alg;
3696 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003697 uint8_t tag_length;
3698} aead_operation_t;
3699
Gilles Peskine30a9e412019-01-14 18:36:12 +01003700static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003701{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003702 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003703 {
3704#if defined(MBEDTLS_CCM_C)
3705 case PSA_ALG_CCM:
3706 mbedtls_ccm_free( &operation->ctx.ccm );
3707 break;
3708#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003709#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003710 case PSA_ALG_GCM:
3711 mbedtls_gcm_free( &operation->ctx.gcm );
3712 break;
3713#endif /* MBEDTLS_GCM_C */
3714 }
3715}
3716
3717static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003718 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003719 psa_key_usage_t usage,
3720 psa_algorithm_t alg )
3721{
3722 psa_status_t status;
3723 size_t key_bits;
3724 mbedtls_cipher_id_t cipher_id;
3725
Gilles Peskinec5487a82018-12-03 18:08:14 +01003726 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003727 if( status != PSA_SUCCESS )
3728 return( status );
3729
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003730 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003731
3732 operation->cipher_info =
3733 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3734 &cipher_id );
3735 if( operation->cipher_info == NULL )
3736 return( PSA_ERROR_NOT_SUPPORTED );
3737
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003738 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003739 {
3740#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003741 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3742 operation->core_alg = PSA_ALG_CCM;
3743 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003744 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3745 return( PSA_ERROR_INVALID_ARGUMENT );
3746 mbedtls_ccm_init( &operation->ctx.ccm );
3747 status = mbedtls_to_psa_error(
3748 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3749 operation->slot->data.raw.data,
3750 (unsigned int) key_bits ) );
3751 if( status != 0 )
3752 goto cleanup;
3753 break;
3754#endif /* MBEDTLS_CCM_C */
3755
3756#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003757 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3758 operation->core_alg = PSA_ALG_GCM;
3759 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003760 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3761 return( PSA_ERROR_INVALID_ARGUMENT );
3762 mbedtls_gcm_init( &operation->ctx.gcm );
3763 status = mbedtls_to_psa_error(
3764 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3765 operation->slot->data.raw.data,
3766 (unsigned int) key_bits ) );
3767 break;
3768#endif /* MBEDTLS_GCM_C */
3769
3770 default:
3771 return( PSA_ERROR_NOT_SUPPORTED );
3772 }
3773
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003774 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3775 {
3776 status = PSA_ERROR_INVALID_ARGUMENT;
3777 goto cleanup;
3778 }
3779 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3780 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3781 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3782 * In both cases, mbedtls_xxx will validate the tag length below. */
3783
Gilles Peskineedf9a652018-08-17 18:11:56 +02003784 return( PSA_SUCCESS );
3785
3786cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003787 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003788 return( status );
3789}
3790
Gilles Peskinec5487a82018-12-03 18:08:14 +01003791psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003792 psa_algorithm_t alg,
3793 const uint8_t *nonce,
3794 size_t nonce_length,
3795 const uint8_t *additional_data,
3796 size_t additional_data_length,
3797 const uint8_t *plaintext,
3798 size_t plaintext_length,
3799 uint8_t *ciphertext,
3800 size_t ciphertext_size,
3801 size_t *ciphertext_length )
3802{
mohammad16035955c982018-04-26 00:53:03 +03003803 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003804 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003805 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003806
mohammad1603f08a5502018-06-03 15:05:47 +03003807 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003808
Gilles Peskinec5487a82018-12-03 18:08:14 +01003809 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003810 if( status != PSA_SUCCESS )
3811 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003812
Gilles Peskineedf9a652018-08-17 18:11:56 +02003813 /* For all currently supported modes, the tag is at the end of the
3814 * ciphertext. */
3815 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3816 {
3817 status = PSA_ERROR_BUFFER_TOO_SMALL;
3818 goto exit;
3819 }
3820 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003821
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003822#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003823 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003824 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003825 status = mbedtls_to_psa_error(
3826 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3827 MBEDTLS_GCM_ENCRYPT,
3828 plaintext_length,
3829 nonce, nonce_length,
3830 additional_data, additional_data_length,
3831 plaintext, ciphertext,
3832 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003833 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003834 else
3835#endif /* MBEDTLS_GCM_C */
3836#if defined(MBEDTLS_CCM_C)
3837 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003838 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003839 status = mbedtls_to_psa_error(
3840 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3841 plaintext_length,
3842 nonce, nonce_length,
3843 additional_data,
3844 additional_data_length,
3845 plaintext, ciphertext,
3846 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003847 }
mohammad16035c8845f2018-05-09 05:40:09 -07003848 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003849#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003850 {
mohammad1603554faad2018-06-03 15:07:38 +03003851 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003852 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003853
Gilles Peskineedf9a652018-08-17 18:11:56 +02003854 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3855 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003856
Gilles Peskineedf9a652018-08-17 18:11:56 +02003857exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003858 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003859 if( status == PSA_SUCCESS )
3860 *ciphertext_length = plaintext_length + operation.tag_length;
3861 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003862}
3863
Gilles Peskineee652a32018-06-01 19:23:52 +02003864/* Locate the tag in a ciphertext buffer containing the encrypted data
3865 * followed by the tag. Return the length of the part preceding the tag in
3866 * *plaintext_length. This is the size of the plaintext in modes where
3867 * the encrypted data has the same size as the plaintext, such as
3868 * CCM and GCM. */
3869static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3870 const uint8_t *ciphertext,
3871 size_t ciphertext_length,
3872 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003873 const uint8_t **p_tag )
3874{
3875 size_t payload_length;
3876 if( tag_length > ciphertext_length )
3877 return( PSA_ERROR_INVALID_ARGUMENT );
3878 payload_length = ciphertext_length - tag_length;
3879 if( payload_length > plaintext_size )
3880 return( PSA_ERROR_BUFFER_TOO_SMALL );
3881 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003882 return( PSA_SUCCESS );
3883}
3884
Gilles Peskinec5487a82018-12-03 18:08:14 +01003885psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003886 psa_algorithm_t alg,
3887 const uint8_t *nonce,
3888 size_t nonce_length,
3889 const uint8_t *additional_data,
3890 size_t additional_data_length,
3891 const uint8_t *ciphertext,
3892 size_t ciphertext_length,
3893 uint8_t *plaintext,
3894 size_t plaintext_size,
3895 size_t *plaintext_length )
3896{
mohammad16035955c982018-04-26 00:53:03 +03003897 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003898 aead_operation_t operation;
3899 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003900
Gilles Peskineee652a32018-06-01 19:23:52 +02003901 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003902
Gilles Peskinec5487a82018-12-03 18:08:14 +01003903 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003904 if( status != PSA_SUCCESS )
3905 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003906
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003907#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003908 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003909 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003910 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003911 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003912 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003913 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003914 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003915
Gilles Peskineedf9a652018-08-17 18:11:56 +02003916 status = mbedtls_to_psa_error(
3917 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3918 ciphertext_length - operation.tag_length,
3919 nonce, nonce_length,
3920 additional_data,
3921 additional_data_length,
3922 tag, operation.tag_length,
3923 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003924 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003925 else
3926#endif /* MBEDTLS_GCM_C */
3927#if defined(MBEDTLS_CCM_C)
3928 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003929 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003930 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003931 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003932 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003933 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003934 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003935
Gilles Peskineedf9a652018-08-17 18:11:56 +02003936 status = mbedtls_to_psa_error(
3937 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3938 ciphertext_length - operation.tag_length,
3939 nonce, nonce_length,
3940 additional_data,
3941 additional_data_length,
3942 ciphertext, plaintext,
3943 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003944 }
mohammad160339574652018-06-01 04:39:53 -07003945 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003946#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003947 {
mohammad1603554faad2018-06-03 15:07:38 +03003948 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003949 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003950
Gilles Peskineedf9a652018-08-17 18:11:56 +02003951 if( status != PSA_SUCCESS && plaintext_size != 0 )
3952 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003953
Gilles Peskineedf9a652018-08-17 18:11:56 +02003954exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003955 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003956 if( status == PSA_SUCCESS )
3957 *plaintext_length = ciphertext_length - operation.tag_length;
3958 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003959}
3960
Gilles Peskinea0655c32018-04-30 17:06:50 +02003961
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003962
Gilles Peskine20035e32018-02-03 22:44:14 +01003963/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003964/* Generators */
3965/****************************************************************/
3966
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003967#define HKDF_STATE_INIT 0 /* no input yet */
3968#define HKDF_STATE_STARTED 1 /* got salt */
3969#define HKDF_STATE_KEYED 2 /* got key */
3970#define HKDF_STATE_OUTPUT 3 /* output started */
3971
Gilles Peskine969c5d62019-01-16 15:53:06 +01003972static psa_algorithm_t psa_generator_get_kdf_alg(
3973 const psa_crypto_generator_t *generator )
3974{
3975 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3976 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3977 else
3978 return( generator->alg );
3979}
3980
3981
Gilles Peskineeab56e42018-07-12 17:12:33 +02003982psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3983{
3984 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003985 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3986 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003987 {
3988 /* The object has (apparently) been initialized but it is not
3989 * in use. It's ok to call abort on such an object, and there's
3990 * nothing to do. */
3991 }
3992 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003993 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003994 {
3995 if( generator->ctx.buffer.data != NULL )
3996 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003997 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003998 generator->ctx.buffer.size );
3999 mbedtls_free( generator->ctx.buffer.data );
4000 }
4001 }
4002 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004003#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004004 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004005 {
4006 mbedtls_free( generator->ctx.hkdf.info );
4007 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
4008 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004009 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00004010 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004011 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004012 {
4013 if( generator->ctx.tls12_prf.key != NULL )
4014 {
Gilles Peskine3f108122018-12-07 18:14:53 +01004015 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004016 generator->ctx.tls12_prf.key_len );
4017 mbedtls_free( generator->ctx.tls12_prf.key );
4018 }
Hanno Becker580fba12018-11-13 20:50:45 +00004019
4020 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
4021 {
Gilles Peskine3f108122018-12-07 18:14:53 +01004022 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004023 generator->ctx.tls12_prf.Ai_with_seed_len );
4024 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
4025 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004026 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004027 else
4028#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004029 {
4030 status = PSA_ERROR_BAD_STATE;
4031 }
4032 memset( generator, 0, sizeof( *generator ) );
4033 return( status );
4034}
4035
Gilles Peskineeab56e42018-07-12 17:12:33 +02004036psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
4037 size_t *capacity)
4038{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004039 if( generator->alg == 0 )
4040 {
4041 /* This is a blank generator. */
4042 return PSA_ERROR_BAD_STATE;
4043 }
4044
Gilles Peskineeab56e42018-07-12 17:12:33 +02004045 *capacity = generator->capacity;
4046 return( PSA_SUCCESS );
4047}
4048
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004049psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
4050 size_t capacity )
4051{
4052 if( generator->alg == 0 )
4053 return( PSA_ERROR_BAD_STATE );
4054 if( capacity > generator->capacity )
4055 return( PSA_ERROR_INVALID_ARGUMENT );
4056 generator->capacity = capacity;
4057 return( PSA_SUCCESS );
4058}
4059
Gilles Peskinebef7f142018-07-12 17:22:21 +02004060#if defined(MBEDTLS_MD_C)
4061/* Read some bytes from an HKDF-based generator. This performs a chunk
4062 * of the expand phase of the HKDF algorithm. */
4063static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
4064 psa_algorithm_t hash_alg,
4065 uint8_t *output,
4066 size_t output_length )
4067{
4068 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4069 psa_status_t status;
4070
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004071 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4072 return( PSA_ERROR_BAD_STATE );
4073 hkdf->state = HKDF_STATE_OUTPUT;
4074
Gilles Peskinebef7f142018-07-12 17:22:21 +02004075 while( output_length != 0 )
4076 {
4077 /* Copy what remains of the current block */
4078 uint8_t n = hash_length - hkdf->offset_in_block;
4079 if( n > output_length )
4080 n = (uint8_t) output_length;
4081 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4082 output += n;
4083 output_length -= n;
4084 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004085 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004086 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004087 /* We can't be wanting more output after block 0xff, otherwise
4088 * the capacity check in psa_generator_read() would have
4089 * prevented this call. It could happen only if the generator
4090 * object was corrupted or if this function is called directly
4091 * inside the library. */
4092 if( hkdf->block_number == 0xff )
4093 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004094
4095 /* We need a new block */
4096 ++hkdf->block_number;
4097 hkdf->offset_in_block = 0;
4098 status = psa_hmac_setup_internal( &hkdf->hmac,
4099 hkdf->prk, hash_length,
4100 hash_alg );
4101 if( status != PSA_SUCCESS )
4102 return( status );
4103 if( hkdf->block_number != 1 )
4104 {
4105 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4106 hkdf->output_block,
4107 hash_length );
4108 if( status != PSA_SUCCESS )
4109 return( status );
4110 }
4111 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4112 hkdf->info,
4113 hkdf->info_length );
4114 if( status != PSA_SUCCESS )
4115 return( status );
4116 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4117 &hkdf->block_number, 1 );
4118 if( status != PSA_SUCCESS )
4119 return( status );
4120 status = psa_hmac_finish_internal( &hkdf->hmac,
4121 hkdf->output_block,
4122 sizeof( hkdf->output_block ) );
4123 if( status != PSA_SUCCESS )
4124 return( status );
4125 }
4126
4127 return( PSA_SUCCESS );
4128}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004129
4130static psa_status_t psa_generator_tls12_prf_generate_next_block(
4131 psa_tls12_prf_generator_t *tls12_prf,
4132 psa_algorithm_t alg )
4133{
4134 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4135 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4136 psa_hmac_internal_data hmac;
4137 psa_status_t status, cleanup_status;
4138
Hanno Becker3b339e22018-11-13 20:56:14 +00004139 unsigned char *Ai;
4140 size_t Ai_len;
4141
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004142 /* We can't be wanting more output after block 0xff, otherwise
4143 * the capacity check in psa_generator_read() would have
4144 * prevented this call. It could happen only if the generator
4145 * object was corrupted or if this function is called directly
4146 * inside the library. */
4147 if( tls12_prf->block_number == 0xff )
4148 return( PSA_ERROR_BAD_STATE );
4149
4150 /* We need a new block */
4151 ++tls12_prf->block_number;
4152 tls12_prf->offset_in_block = 0;
4153
4154 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4155 *
4156 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4157 *
4158 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4159 * HMAC_hash(secret, A(2) + seed) +
4160 * HMAC_hash(secret, A(3) + seed) + ...
4161 *
4162 * A(0) = seed
4163 * A(i) = HMAC_hash( secret, A(i-1) )
4164 *
4165 * The `psa_tls12_prf_generator` structures saves the block
4166 * `HMAC_hash(secret, A(i) + seed)` from which the output
4167 * is currently extracted as `output_block`, while
4168 * `A(i) + seed` is stored in `Ai_with_seed`.
4169 *
4170 * Generating a new block means recalculating `Ai_with_seed`
4171 * from the A(i)-part of it, and afterwards recalculating
4172 * `output_block`.
4173 *
4174 * A(0) is computed at setup time.
4175 *
4176 */
4177
4178 psa_hmac_init_internal( &hmac );
4179
4180 /* We must distinguish the calculation of A(1) from those
4181 * of A(2) and higher, because A(0)=seed has a different
4182 * length than the other A(i). */
4183 if( tls12_prf->block_number == 1 )
4184 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004185 Ai = tls12_prf->Ai_with_seed + hash_length;
4186 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004187 }
4188 else
4189 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004190 Ai = tls12_prf->Ai_with_seed;
4191 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004192 }
4193
Hanno Becker3b339e22018-11-13 20:56:14 +00004194 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4195 status = psa_hmac_setup_internal( &hmac,
4196 tls12_prf->key,
4197 tls12_prf->key_len,
4198 hash_alg );
4199 if( status != PSA_SUCCESS )
4200 goto cleanup;
4201
4202 status = psa_hash_update( &hmac.hash_ctx,
4203 Ai, Ai_len );
4204 if( status != PSA_SUCCESS )
4205 goto cleanup;
4206
4207 status = psa_hmac_finish_internal( &hmac,
4208 tls12_prf->Ai_with_seed,
4209 hash_length );
4210 if( status != PSA_SUCCESS )
4211 goto cleanup;
4212
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004213 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4214 status = psa_hmac_setup_internal( &hmac,
4215 tls12_prf->key,
4216 tls12_prf->key_len,
4217 hash_alg );
4218 if( status != PSA_SUCCESS )
4219 goto cleanup;
4220
4221 status = psa_hash_update( &hmac.hash_ctx,
4222 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004223 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004224 if( status != PSA_SUCCESS )
4225 goto cleanup;
4226
4227 status = psa_hmac_finish_internal( &hmac,
4228 tls12_prf->output_block,
4229 hash_length );
4230 if( status != PSA_SUCCESS )
4231 goto cleanup;
4232
4233cleanup:
4234
4235 cleanup_status = psa_hmac_abort_internal( &hmac );
4236 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4237 status = cleanup_status;
4238
4239 return( status );
4240}
4241
4242/* Read some bytes from an TLS-1.2-PRF-based generator.
4243 * See Section 5 of RFC 5246. */
4244static psa_status_t psa_generator_tls12_prf_read(
4245 psa_tls12_prf_generator_t *tls12_prf,
4246 psa_algorithm_t alg,
4247 uint8_t *output,
4248 size_t output_length )
4249{
4250 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4251 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4252 psa_status_t status;
4253
4254 while( output_length != 0 )
4255 {
4256 /* Copy what remains of the current block */
4257 uint8_t n = hash_length - tls12_prf->offset_in_block;
4258
4259 /* Check if we have fully processed the current block. */
4260 if( n == 0 )
4261 {
4262 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
4263 alg );
4264 if( status != PSA_SUCCESS )
4265 return( status );
4266
4267 continue;
4268 }
4269
4270 if( n > output_length )
4271 n = (uint8_t) output_length;
4272 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4273 n );
4274 output += n;
4275 output_length -= n;
4276 tls12_prf->offset_in_block += n;
4277 }
4278
4279 return( PSA_SUCCESS );
4280}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004281#endif /* MBEDTLS_MD_C */
4282
Gilles Peskineeab56e42018-07-12 17:12:33 +02004283psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
4284 uint8_t *output,
4285 size_t output_length )
4286{
4287 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004288 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004289
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004290 if( generator->alg == 0 )
4291 {
4292 /* This is a blank generator. */
4293 return PSA_ERROR_BAD_STATE;
4294 }
4295
Gilles Peskineeab56e42018-07-12 17:12:33 +02004296 if( output_length > generator->capacity )
4297 {
4298 generator->capacity = 0;
4299 /* Go through the error path to wipe all confidential data now
4300 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004301 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004302 goto exit;
4303 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004304 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004305 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004306 /* Edge case: this is a finished generator, and 0 bytes
4307 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004308 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4309 * INSUFFICIENT_CAPACITY, which is right for a finished
4310 * generator, for consistency with the case when
4311 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004312 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004313 }
4314 generator->capacity -= output_length;
4315
Gilles Peskine969c5d62019-01-16 15:53:06 +01004316 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004317 {
Gilles Peskine211a4362018-10-25 22:22:31 +02004318 /* Initially, the capacity of a selection generator is always
4319 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
4320 * abbreviated in this comment as `size`. When the remaining
4321 * capacity is `c`, the next bytes to serve start `c` bytes
4322 * from the end of the buffer, i.e. `size - c` from the
4323 * beginning of the buffer. Since `generator->capacity` was just
4324 * decremented above, we need to serve the bytes from
4325 * `size - generator->capacity - output_length` to
4326 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004327 size_t offset =
4328 generator->ctx.buffer.size - generator->capacity - output_length;
4329 memcpy( output, generator->ctx.buffer.data + offset, output_length );
4330 status = PSA_SUCCESS;
4331 }
4332 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004333#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004334 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004335 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004336 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004337 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
4338 output, output_length );
4339 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004340 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4341 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004342 {
4343 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004344 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004345 output_length );
4346 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004347 else
4348#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004349 {
4350 return( PSA_ERROR_BAD_STATE );
4351 }
4352
4353exit:
4354 if( status != PSA_SUCCESS )
4355 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004356 /* Preserve the algorithm upon errors, but clear all sensitive state.
4357 * This allows us to differentiate between exhausted generators and
4358 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
4359 * generators. */
4360 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004361 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004362 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004363 memset( output, '!', output_length );
4364 }
4365 return( status );
4366}
4367
Gilles Peskine08542d82018-07-19 17:05:42 +02004368#if defined(MBEDTLS_DES_C)
4369static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4370{
4371 if( data_size >= 8 )
4372 mbedtls_des_key_set_parity( data );
4373 if( data_size >= 16 )
4374 mbedtls_des_key_set_parity( data + 8 );
4375 if( data_size >= 24 )
4376 mbedtls_des_key_set_parity( data + 16 );
4377}
4378#endif /* MBEDTLS_DES_C */
4379
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004380static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004381 psa_key_slot_t *slot,
4382 size_t bits,
4383 psa_crypto_generator_t *generator )
4384{
4385 uint8_t *data = NULL;
4386 size_t bytes = PSA_BITS_TO_BYTES( bits );
4387 psa_status_t status;
4388
4389 if( ! key_type_is_raw_bytes( slot->type ) )
4390 return( PSA_ERROR_INVALID_ARGUMENT );
4391 if( bits % 8 != 0 )
4392 return( PSA_ERROR_INVALID_ARGUMENT );
4393 data = mbedtls_calloc( 1, bytes );
4394 if( data == NULL )
4395 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4396
4397 status = psa_generator_read( generator, data, bytes );
4398 if( status != PSA_SUCCESS )
4399 goto exit;
4400#if defined(MBEDTLS_DES_C)
4401 if( slot->type == PSA_KEY_TYPE_DES )
4402 psa_des_set_key_parity( data, bytes );
4403#endif /* MBEDTLS_DES_C */
4404 status = psa_import_key_into_slot( slot, data, bytes );
4405
4406exit:
4407 mbedtls_free( data );
4408 return( status );
4409}
4410
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004411psa_status_t psa_generate_derived_key( const psa_key_attributes_t *attributes,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004412 psa_crypto_generator_t *generator,
4413 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004414{
4415 psa_status_t status;
4416 psa_key_slot_t *slot = NULL;
4417 status = psa_start_key_creation( attributes, handle, &slot );
4418 if( status == PSA_SUCCESS )
4419 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004420 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004421 attributes->bits,
4422 generator );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004423 }
4424 if( status == PSA_SUCCESS )
4425 status = psa_finish_key_creation( slot );
4426 if( status != PSA_SUCCESS )
4427 {
4428 psa_fail_key_creation( slot );
4429 *handle = 0;
4430 }
4431 return( status );
4432}
4433
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004434psa_status_t psa_generate_derived_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004435 psa_key_type_t type,
4436 size_t bits,
4437 psa_crypto_generator_t *generator )
4438{
4439 uint8_t *data = NULL;
4440 size_t bytes = PSA_BITS_TO_BYTES( bits );
4441 psa_status_t status;
4442
4443 if( ! key_type_is_raw_bytes( type ) )
4444 return( PSA_ERROR_INVALID_ARGUMENT );
4445 if( bits % 8 != 0 )
4446 return( PSA_ERROR_INVALID_ARGUMENT );
4447 data = mbedtls_calloc( 1, bytes );
4448 if( data == NULL )
4449 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4450
4451 status = psa_generator_read( generator, data, bytes );
4452 if( status != PSA_SUCCESS )
4453 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004454#if defined(MBEDTLS_DES_C)
4455 if( type == PSA_KEY_TYPE_DES )
4456 psa_des_set_key_parity( data, bytes );
4457#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004458 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004459
4460exit:
4461 mbedtls_free( data );
4462 return( status );
4463}
4464
4465
4466
4467/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004468/* Key derivation */
4469/****************************************************************/
4470
Gilles Peskinea05219c2018-11-16 16:02:56 +01004471#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004472/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004473 * of the HKDF algorithm.
4474 *
4475 * Note that if this function fails, you must call psa_generator_abort()
4476 * to potentially free embedded data structures and wipe confidential data.
4477 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004478static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004479 const uint8_t *secret,
4480 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004481 psa_algorithm_t hash_alg,
4482 const uint8_t *salt,
4483 size_t salt_length,
4484 const uint8_t *label,
4485 size_t label_length )
4486{
4487 psa_status_t status;
4488 status = psa_hmac_setup_internal( &hkdf->hmac,
4489 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004490 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004491 if( status != PSA_SUCCESS )
4492 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004493 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004494 if( status != PSA_SUCCESS )
4495 return( status );
4496 status = psa_hmac_finish_internal( &hkdf->hmac,
4497 hkdf->prk,
4498 sizeof( hkdf->prk ) );
4499 if( status != PSA_SUCCESS )
4500 return( status );
4501 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4502 hkdf->block_number = 0;
4503 hkdf->info_length = label_length;
4504 if( label_length != 0 )
4505 {
4506 hkdf->info = mbedtls_calloc( 1, label_length );
4507 if( hkdf->info == NULL )
4508 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4509 memcpy( hkdf->info, label, label_length );
4510 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004511 hkdf->state = HKDF_STATE_KEYED;
4512 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004513 return( PSA_SUCCESS );
4514}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004515#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004516
Gilles Peskinea05219c2018-11-16 16:02:56 +01004517#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004518/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4519 *
4520 * Note that if this function fails, you must call psa_generator_abort()
4521 * to potentially free embedded data structures and wipe confidential data.
4522 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004523static psa_status_t psa_generator_tls12_prf_setup(
4524 psa_tls12_prf_generator_t *tls12_prf,
4525 const unsigned char *key,
4526 size_t key_len,
4527 psa_algorithm_t hash_alg,
4528 const uint8_t *salt,
4529 size_t salt_length,
4530 const uint8_t *label,
4531 size_t label_length )
4532{
4533 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004534 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4535 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004536
4537 tls12_prf->key = mbedtls_calloc( 1, key_len );
4538 if( tls12_prf->key == NULL )
4539 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4540 tls12_prf->key_len = key_len;
4541 memcpy( tls12_prf->key, key, key_len );
4542
Hanno Becker580fba12018-11-13 20:50:45 +00004543 overflow = ( salt_length + label_length < salt_length ) ||
4544 ( salt_length + label_length + hash_length < hash_length );
4545 if( overflow )
4546 return( PSA_ERROR_INVALID_ARGUMENT );
4547
4548 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4549 if( tls12_prf->Ai_with_seed == NULL )
4550 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4551 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4552
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004553 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4554 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004555 if( label_length != 0 )
4556 {
4557 memcpy( tls12_prf->Ai_with_seed + hash_length,
4558 label, label_length );
4559 }
4560
4561 if( salt_length != 0 )
4562 {
4563 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4564 salt, salt_length );
4565 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004566
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004567 /* The first block gets generated when
4568 * psa_generator_read() is called. */
4569 tls12_prf->block_number = 0;
4570 tls12_prf->offset_in_block = hash_length;
4571
4572 return( PSA_SUCCESS );
4573}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004574
4575/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4576static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4577 psa_tls12_prf_generator_t *tls12_prf,
4578 const unsigned char *psk,
4579 size_t psk_len,
4580 psa_algorithm_t hash_alg,
4581 const uint8_t *salt,
4582 size_t salt_length,
4583 const uint8_t *label,
4584 size_t label_length )
4585{
4586 psa_status_t status;
4587 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4588
4589 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4590 return( PSA_ERROR_INVALID_ARGUMENT );
4591
4592 /* Quoting RFC 4279, Section 2:
4593 *
4594 * The premaster secret is formed as follows: if the PSK is N octets
4595 * long, concatenate a uint16 with the value N, N zero octets, a second
4596 * uint16 with the value N, and the PSK itself.
4597 */
4598
4599 pms[0] = ( psk_len >> 8 ) & 0xff;
4600 pms[1] = ( psk_len >> 0 ) & 0xff;
4601 memset( pms + 2, 0, psk_len );
4602 pms[2 + psk_len + 0] = pms[0];
4603 pms[2 + psk_len + 1] = pms[1];
4604 memcpy( pms + 4 + psk_len, psk, psk_len );
4605
4606 status = psa_generator_tls12_prf_setup( tls12_prf,
4607 pms, 4 + 2 * psk_len,
4608 hash_alg,
4609 salt, salt_length,
4610 label, label_length );
4611
Gilles Peskine3f108122018-12-07 18:14:53 +01004612 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004613 return( status );
4614}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004615#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004616
Gilles Peskine346797d2018-11-16 16:05:06 +01004617/* Note that if this function fails, you must call psa_generator_abort()
4618 * to potentially free embedded data structures and wipe confidential data.
4619 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004620static psa_status_t psa_key_derivation_internal(
4621 psa_crypto_generator_t *generator,
4622 const uint8_t *secret, size_t secret_length,
4623 psa_algorithm_t alg,
4624 const uint8_t *salt, size_t salt_length,
4625 const uint8_t *label, size_t label_length,
4626 size_t capacity )
4627{
4628 psa_status_t status;
4629 size_t max_capacity;
4630
4631 /* Set generator->alg even on failure so that abort knows what to do. */
4632 generator->alg = alg;
4633
Gilles Peskine751d9652018-09-18 12:05:44 +02004634 if( alg == PSA_ALG_SELECT_RAW )
4635 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004636 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004637 if( salt_length != 0 )
4638 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004639 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004640 if( label_length != 0 )
4641 return( PSA_ERROR_INVALID_ARGUMENT );
4642 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4643 if( generator->ctx.buffer.data == NULL )
4644 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4645 memcpy( generator->ctx.buffer.data, secret, secret_length );
4646 generator->ctx.buffer.size = secret_length;
4647 max_capacity = secret_length;
4648 status = PSA_SUCCESS;
4649 }
4650 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004651#if defined(MBEDTLS_MD_C)
4652 if( PSA_ALG_IS_HKDF( alg ) )
4653 {
4654 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4655 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4656 if( hash_size == 0 )
4657 return( PSA_ERROR_NOT_SUPPORTED );
4658 max_capacity = 255 * hash_size;
4659 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4660 secret, secret_length,
4661 hash_alg,
4662 salt, salt_length,
4663 label, label_length );
4664 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004665 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4666 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4667 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004668 {
4669 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4670 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4671
4672 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4673 if( hash_alg != PSA_ALG_SHA_256 &&
4674 hash_alg != PSA_ALG_SHA_384 )
4675 {
4676 return( PSA_ERROR_NOT_SUPPORTED );
4677 }
4678
4679 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004680
4681 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4682 {
4683 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4684 secret, secret_length,
4685 hash_alg, salt, salt_length,
4686 label, label_length );
4687 }
4688 else
4689 {
4690 status = psa_generator_tls12_psk_to_ms_setup(
4691 &generator->ctx.tls12_prf,
4692 secret, secret_length,
4693 hash_alg, salt, salt_length,
4694 label, label_length );
4695 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004696 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004697 else
4698#endif
4699 {
4700 return( PSA_ERROR_NOT_SUPPORTED );
4701 }
4702
4703 if( status != PSA_SUCCESS )
4704 return( status );
4705
4706 if( capacity <= max_capacity )
4707 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004708 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4709 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004710 else
4711 return( PSA_ERROR_INVALID_ARGUMENT );
4712
4713 return( PSA_SUCCESS );
4714}
4715
Gilles Peskineea0fb492018-07-12 17:17:20 +02004716psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004717 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004718 psa_algorithm_t alg,
4719 const uint8_t *salt,
4720 size_t salt_length,
4721 const uint8_t *label,
4722 size_t label_length,
4723 size_t capacity )
4724{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004725 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004726 psa_status_t status;
4727
4728 if( generator->alg != 0 )
4729 return( PSA_ERROR_BAD_STATE );
4730
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004731 /* Make sure that alg is a key derivation algorithm. This prevents
4732 * key selection algorithms, which psa_key_derivation_internal
4733 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004734 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4735 return( PSA_ERROR_INVALID_ARGUMENT );
4736
Gilles Peskinec5487a82018-12-03 18:08:14 +01004737 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004738 if( status != PSA_SUCCESS )
4739 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004740
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004741 if( slot->type != PSA_KEY_TYPE_DERIVE )
4742 return( PSA_ERROR_INVALID_ARGUMENT );
4743
4744 status = psa_key_derivation_internal( generator,
4745 slot->data.raw.data,
4746 slot->data.raw.bytes,
4747 alg,
4748 salt, salt_length,
4749 label, label_length,
4750 capacity );
4751 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004752 psa_generator_abort( generator );
4753 return( status );
4754}
4755
Gilles Peskine969c5d62019-01-16 15:53:06 +01004756static psa_status_t psa_key_derivation_setup_kdf(
4757 psa_crypto_generator_t *generator,
4758 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004759{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004760 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004761#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004762 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4763 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4764 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004765 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004766 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004767 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4768 if( hash_size == 0 )
4769 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004770 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4771 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004772 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004773 {
4774 return( PSA_ERROR_NOT_SUPPORTED );
4775 }
4776 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004777 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004778 }
4779#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004780 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004781 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004782}
4783
4784psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4785 psa_algorithm_t alg )
4786{
4787 psa_status_t status;
4788
4789 if( generator->alg != 0 )
4790 return( PSA_ERROR_BAD_STATE );
4791
Gilles Peskine6843c292019-01-18 16:44:49 +01004792 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4793 return( PSA_ERROR_INVALID_ARGUMENT );
4794 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004795 {
4796 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004797 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004798 }
4799 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4800 {
4801 status = psa_key_derivation_setup_kdf( generator, alg );
4802 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004803 else
4804 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004805
4806 if( status == PSA_SUCCESS )
4807 generator->alg = alg;
4808 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004809}
4810
4811#if defined(MBEDTLS_MD_C)
4812static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4813 psa_algorithm_t hash_alg,
4814 psa_key_derivation_step_t step,
4815 const uint8_t *data,
4816 size_t data_length )
4817{
4818 psa_status_t status;
4819 switch( step )
4820 {
4821 case PSA_KDF_STEP_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004822 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004823 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004824 status = psa_hmac_setup_internal( &hkdf->hmac,
4825 data, data_length,
4826 hash_alg );
4827 if( status != PSA_SUCCESS )
4828 return( status );
4829 hkdf->state = HKDF_STATE_STARTED;
4830 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004831 case PSA_KDF_STEP_SECRET:
4832 /* If no salt was provided, use an empty salt. */
4833 if( hkdf->state == HKDF_STATE_INIT )
4834 {
4835 status = psa_hmac_setup_internal( &hkdf->hmac,
4836 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004837 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004838 if( status != PSA_SUCCESS )
4839 return( status );
4840 hkdf->state = HKDF_STATE_STARTED;
4841 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004842 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004843 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004844 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4845 data, data_length );
4846 if( status != PSA_SUCCESS )
4847 return( status );
4848 status = psa_hmac_finish_internal( &hkdf->hmac,
4849 hkdf->prk,
4850 sizeof( hkdf->prk ) );
4851 if( status != PSA_SUCCESS )
4852 return( status );
4853 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4854 hkdf->block_number = 0;
4855 hkdf->state = HKDF_STATE_KEYED;
4856 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004857 case PSA_KDF_STEP_INFO:
4858 if( hkdf->state == HKDF_STATE_OUTPUT )
4859 return( PSA_ERROR_BAD_STATE );
4860 if( hkdf->info_set )
4861 return( PSA_ERROR_BAD_STATE );
4862 hkdf->info_length = data_length;
4863 if( data_length != 0 )
4864 {
4865 hkdf->info = mbedtls_calloc( 1, data_length );
4866 if( hkdf->info == NULL )
4867 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4868 memcpy( hkdf->info, data, data_length );
4869 }
4870 hkdf->info_set = 1;
4871 return( PSA_SUCCESS );
4872 default:
4873 return( PSA_ERROR_INVALID_ARGUMENT );
4874 }
4875}
4876#endif /* MBEDTLS_MD_C */
4877
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004878static psa_status_t psa_key_derivation_input_raw(
4879 psa_crypto_generator_t *generator,
4880 psa_key_derivation_step_t step,
4881 const uint8_t *data,
4882 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004883{
4884 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004885 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004886
Gilles Peskine969c5d62019-01-16 15:53:06 +01004887 if( kdf_alg == PSA_ALG_SELECT_RAW )
4888 {
4889 if( generator->capacity != 0 )
4890 return( PSA_ERROR_INVALID_ARGUMENT );
4891 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4892 if( generator->ctx.buffer.data == NULL )
4893 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4894 memcpy( generator->ctx.buffer.data, data, data_length );
4895 generator->ctx.buffer.size = data_length;
4896 generator->capacity = data_length;
4897 status = PSA_SUCCESS;
4898 }
4899 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004900#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004901 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004902 {
4903 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004904 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004905 step, data, data_length );
4906 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004907 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004908#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004909#if defined(MBEDTLS_MD_C)
4910 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004911 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4912 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004913 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004914 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004915 status = PSA_ERROR_NOT_SUPPORTED;
4916 }
4917 else
4918#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004919 {
4920 /* This can't happen unless the generator object was not initialized */
4921 return( PSA_ERROR_BAD_STATE );
4922 }
4923
4924 if( status != PSA_SUCCESS )
4925 psa_generator_abort( generator );
4926 return( status );
4927}
4928
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004929psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4930 psa_key_derivation_step_t step,
4931 const uint8_t *data,
4932 size_t data_length )
4933{
4934 switch( step )
4935 {
4936 case PSA_KDF_STEP_LABEL:
4937 case PSA_KDF_STEP_SALT:
4938 case PSA_KDF_STEP_INFO:
4939 return( psa_key_derivation_input_raw( generator, step,
4940 data, data_length ) );
4941 default:
4942 return( PSA_ERROR_INVALID_ARGUMENT );
4943 }
4944}
4945
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004946psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4947 psa_key_derivation_step_t step,
4948 psa_key_handle_t handle )
4949{
4950 psa_key_slot_t *slot;
4951 psa_status_t status;
4952 status = psa_get_key_from_slot( handle, &slot,
4953 PSA_KEY_USAGE_DERIVE,
4954 generator->alg );
4955 if( status != PSA_SUCCESS )
4956 return( status );
4957 if( slot->type != PSA_KEY_TYPE_DERIVE )
4958 return( PSA_ERROR_INVALID_ARGUMENT );
4959 /* Don't allow a key to be used as an input that is usually public.
4960 * This is debatable. It's ok from a cryptographic perspective to
4961 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004962 * the material should be dedicated to a particular input step,
4963 * otherwise this may allow the key to be used in an unintended way
4964 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004965 if( step != PSA_KDF_STEP_SECRET )
4966 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004967 return( psa_key_derivation_input_raw( generator,
4968 step,
4969 slot->data.raw.data,
4970 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004971}
4972
Gilles Peskineea0fb492018-07-12 17:17:20 +02004973
4974
4975/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004976/* Key agreement */
4977/****************************************************************/
4978
Gilles Peskinea05219c2018-11-16 16:02:56 +01004979#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004980static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4981 size_t peer_key_length,
4982 const mbedtls_ecp_keypair *our_key,
4983 uint8_t *shared_secret,
4984 size_t shared_secret_size,
4985 size_t *shared_secret_length )
4986{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004987 mbedtls_ecp_keypair *their_key = NULL;
4988 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004989 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004990 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004991
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004992 status = psa_import_ec_public_key(
4993 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4994 peer_key, peer_key_length,
4995 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004996 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004997 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004998
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004999 status = mbedtls_to_psa_error(
5000 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5001 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005002 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005003 status = mbedtls_to_psa_error(
5004 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5005 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005006 goto exit;
5007
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005008 status = mbedtls_to_psa_error(
5009 mbedtls_ecdh_calc_secret( &ecdh,
5010 shared_secret_length,
5011 shared_secret, shared_secret_size,
5012 mbedtls_ctr_drbg_random,
5013 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005014
5015exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005016 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005017 mbedtls_ecp_keypair_free( their_key );
5018 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005019 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005020}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005021#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005022
Gilles Peskine01d718c2018-09-18 12:01:02 +02005023#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5024
Gilles Peskine0216fe12019-04-11 21:23:21 +02005025static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5026 psa_key_slot_t *private_key,
5027 const uint8_t *peer_key,
5028 size_t peer_key_length,
5029 uint8_t *shared_secret,
5030 size_t shared_secret_size,
5031 size_t *shared_secret_length )
5032{
5033 switch( alg )
5034 {
5035#if defined(MBEDTLS_ECDH_C)
5036 case PSA_ALG_ECDH:
5037 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
5038 return( PSA_ERROR_INVALID_ARGUMENT );
5039 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5040 private_key->data.ecp,
5041 shared_secret, shared_secret_size,
5042 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005043#endif /* MBEDTLS_ECDH_C */
5044 default:
5045 (void) private_key;
5046 (void) peer_key;
5047 (void) peer_key_length;
5048 (void) shared_secret;
5049 (void) shared_secret_size;
5050 (void) shared_secret_length;
5051 return( PSA_ERROR_NOT_SUPPORTED );
5052 }
5053}
5054
Gilles Peskine346797d2018-11-16 16:05:06 +01005055/* Note that if this function fails, you must call psa_generator_abort()
5056 * to potentially free embedded data structures and wipe confidential data.
5057 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005058static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005059 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005060 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005061 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005062 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005063{
5064 psa_status_t status;
5065 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5066 size_t shared_secret_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005067 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005068
5069 /* Step 1: run the secret agreement algorithm to generate the shared
5070 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005071 status = psa_key_agreement_raw_internal( ka_alg,
5072 private_key,
5073 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005074 shared_secret,
5075 sizeof( shared_secret ),
5076 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005077 if( status != PSA_SUCCESS )
5078 goto exit;
5079
5080 /* Step 2: set up the key derivation to generate key material from
5081 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005082 status = psa_key_derivation_input_raw( generator, step,
5083 shared_secret, shared_secret_length );
5084
Gilles Peskine01d718c2018-09-18 12:01:02 +02005085exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005086 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005087 return( status );
5088}
5089
5090psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005091 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005092 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005093 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005094 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005095{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005096 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005097 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005098 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005099 return( PSA_ERROR_INVALID_ARGUMENT );
5100 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005101 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005102 if( status != PSA_SUCCESS )
5103 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005104 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005105 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005106 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005107 if( status != PSA_SUCCESS )
5108 psa_generator_abort( generator );
5109 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005110}
5111
Gilles Peskine0216fe12019-04-11 21:23:21 +02005112psa_status_t psa_key_agreement_raw_shared_secret( psa_algorithm_t alg,
5113 psa_key_handle_t private_key,
5114 const uint8_t *peer_key,
5115 size_t peer_key_length,
5116 uint8_t *output,
5117 size_t output_size,
5118 size_t *output_length )
5119{
5120 psa_key_slot_t *slot;
5121 psa_status_t status;
5122
5123 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5124 {
5125 status = PSA_ERROR_INVALID_ARGUMENT;
5126 goto exit;
5127 }
5128 status = psa_get_key_from_slot( private_key, &slot,
5129 PSA_KEY_USAGE_DERIVE, alg );
5130 if( status != PSA_SUCCESS )
5131 goto exit;
5132
5133 status = psa_key_agreement_raw_internal( alg, slot,
5134 peer_key, peer_key_length,
5135 output, output_size,
5136 output_length );
5137
5138exit:
5139 if( status != PSA_SUCCESS )
5140 {
5141 /* If an error happens and is not handled properly, the output
5142 * may be used as a key to protect sensitive data. Arrange for such
5143 * a key to be random, which is likely to result in decryption or
5144 * verification errors. This is better than filling the buffer with
5145 * some constant data such as zeros, which would result in the data
5146 * being protected with a reproducible, easily knowable key.
5147 */
5148 psa_generate_random( output, output_size );
5149 *output_length = output_size;
5150 }
5151 return( status );
5152}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005153
5154
5155/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005156/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005157/****************************************************************/
5158
5159psa_status_t psa_generate_random( uint8_t *output,
5160 size_t output_size )
5161{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005162 int ret;
5163 GUARD_MODULE_INITIALIZED;
5164
5165 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005166 return( mbedtls_to_psa_error( ret ) );
5167}
5168
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005169#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5170#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005171
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005172psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5173 size_t seed_size )
5174{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005175 if( global_data.initialized )
5176 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005177
5178 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5179 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5180 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5181 return( PSA_ERROR_INVALID_ARGUMENT );
5182
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005183 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005184}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005185#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005186
Gilles Peskinee56e8782019-04-26 17:34:02 +02005187#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5188static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5189 size_t domain_parameters_size,
5190 int *exponent )
5191{
5192 size_t i;
5193 uint32_t acc = 0;
5194
5195 if( domain_parameters_size == 0 )
5196 {
5197 *exponent = 65537;
5198 return( PSA_SUCCESS );
5199 }
5200
5201 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5202 * support values that fit in a 32-bit integer, which is larger than
5203 * int on just about every platform anyway. */
5204 if( domain_parameters_size > sizeof( acc ) )
5205 return( PSA_ERROR_NOT_SUPPORTED );
5206 for( i = 0; i < domain_parameters_size; i++ )
5207 acc = ( acc << 8 ) | domain_parameters[i];
5208 if( acc > INT_MAX )
5209 return( PSA_ERROR_NOT_SUPPORTED );
5210 *exponent = acc;
5211 return( PSA_SUCCESS );
5212}
5213#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5214
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005215static psa_status_t psa_generate_random_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005216 psa_key_slot_t *slot, size_t bits,
5217 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005218{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005219 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005220
Gilles Peskinee56e8782019-04-26 17:34:02 +02005221 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005222 return( PSA_ERROR_INVALID_ARGUMENT );
5223
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005224 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005225 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005226 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005227 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005228 if( status != PSA_SUCCESS )
5229 return( status );
5230 status = psa_generate_random( slot->data.raw.data,
5231 slot->data.raw.bytes );
5232 if( status != PSA_SUCCESS )
5233 {
5234 mbedtls_free( slot->data.raw.data );
5235 return( status );
5236 }
5237#if defined(MBEDTLS_DES_C)
5238 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005239 psa_des_set_key_parity( slot->data.raw.data,
5240 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005241#endif /* MBEDTLS_DES_C */
5242 }
5243 else
5244
5245#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5246 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
5247 {
5248 mbedtls_rsa_context *rsa;
5249 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005250 int exponent;
5251 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005252 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5253 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005254 /* Accept only byte-aligned keys, for the same reasons as
5255 * in psa_import_rsa_key(). */
5256 if( bits % 8 != 0 )
5257 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005258 status = psa_read_rsa_exponent( domain_parameters,
5259 domain_parameters_size,
5260 &exponent );
5261 if( status != PSA_SUCCESS )
5262 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005263 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5264 if( rsa == NULL )
5265 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5266 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5267 ret = mbedtls_rsa_gen_key( rsa,
5268 mbedtls_ctr_drbg_random,
5269 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005270 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005271 exponent );
5272 if( ret != 0 )
5273 {
5274 mbedtls_rsa_free( rsa );
5275 mbedtls_free( rsa );
5276 return( mbedtls_to_psa_error( ret ) );
5277 }
5278 slot->data.rsa = rsa;
5279 }
5280 else
5281#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5282
5283#if defined(MBEDTLS_ECP_C)
5284 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
5285 {
5286 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5287 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5288 const mbedtls_ecp_curve_info *curve_info =
5289 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5290 mbedtls_ecp_keypair *ecp;
5291 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005292 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005293 return( PSA_ERROR_NOT_SUPPORTED );
5294 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5295 return( PSA_ERROR_NOT_SUPPORTED );
5296 if( curve_info->bit_size != bits )
5297 return( PSA_ERROR_INVALID_ARGUMENT );
5298 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5299 if( ecp == NULL )
5300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5301 mbedtls_ecp_keypair_init( ecp );
5302 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5303 mbedtls_ctr_drbg_random,
5304 &global_data.ctr_drbg );
5305 if( ret != 0 )
5306 {
5307 mbedtls_ecp_keypair_free( ecp );
5308 mbedtls_free( ecp );
5309 return( mbedtls_to_psa_error( ret ) );
5310 }
5311 slot->data.ecp = ecp;
5312 }
5313 else
5314#endif /* MBEDTLS_ECP_C */
5315
5316 return( PSA_ERROR_NOT_SUPPORTED );
5317
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005318 return( PSA_SUCCESS );
5319}
5320
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005321psa_status_t psa_generate_random_key_to_handle( psa_key_handle_t handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005322 psa_key_type_t type,
5323 size_t bits,
5324 const void *extra,
5325 size_t extra_size )
5326{
5327 psa_key_slot_t *slot;
5328 psa_status_t status;
5329
Gilles Peskinee56e8782019-04-26 17:34:02 +02005330#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5331 /* The old public exponent encoding is no longer supported. */
5332 if( extra_size != 0 )
5333 return( PSA_ERROR_NOT_SUPPORTED );
5334#endif
5335
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005336 status = psa_get_empty_key_slot( handle, &slot );
5337 if( status != PSA_SUCCESS )
5338 return( status );
5339
Gilles Peskine12313cd2018-06-20 00:20:32 +02005340 slot->type = type;
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005341 status = psa_generate_random_key_internal( slot, bits, extra, extra_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005342 if( status != PSA_SUCCESS )
5343 slot->type = 0;
Darryl Green0c6575a2018-11-07 16:05:30 +00005344
5345#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
5346 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
5347 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01005348 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005349 }
5350#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5351
5352 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005353}
5354
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005355psa_status_t psa_generate_random_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005356 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005357{
5358 psa_status_t status;
5359 psa_key_slot_t *slot = NULL;
5360 status = psa_start_key_creation( attributes, handle, &slot );
5361 if( status == PSA_SUCCESS )
5362 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005363 status = psa_generate_random_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005364 slot, attributes->bits,
5365 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005366 }
5367 if( status == PSA_SUCCESS )
5368 status = psa_finish_key_creation( slot );
5369 if( status != PSA_SUCCESS )
5370 {
5371 psa_fail_key_creation( slot );
5372 *handle = 0;
5373 }
5374 return( status );
5375}
5376
5377
Gilles Peskine05d69892018-06-19 22:00:52 +02005378
5379/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005380/* Module setup */
5381/****************************************************************/
5382
Gilles Peskine5e769522018-11-20 21:59:56 +01005383psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5384 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5385 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5386{
5387 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5388 return( PSA_ERROR_BAD_STATE );
5389 global_data.entropy_init = entropy_init;
5390 global_data.entropy_free = entropy_free;
5391 return( PSA_SUCCESS );
5392}
5393
Gilles Peskinee59236f2018-01-27 23:32:46 +01005394void mbedtls_psa_crypto_free( void )
5395{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005396 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005397 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5398 {
5399 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005400 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005401 }
5402 /* Wipe all remaining data, including configuration.
5403 * In particular, this sets all state indicator to the value
5404 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005405 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005406}
5407
5408psa_status_t psa_crypto_init( void )
5409{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005410 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005411 const unsigned char drbg_seed[] = "PSA";
5412
Gilles Peskinec6b69072018-11-20 21:42:52 +01005413 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005414 if( global_data.initialized != 0 )
5415 return( PSA_SUCCESS );
5416
Gilles Peskine5e769522018-11-20 21:59:56 +01005417 /* Set default configuration if
5418 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5419 if( global_data.entropy_init == NULL )
5420 global_data.entropy_init = mbedtls_entropy_init;
5421 if( global_data.entropy_free == NULL )
5422 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005423
Gilles Peskinec6b69072018-11-20 21:42:52 +01005424 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005425 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005426 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005427 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005428 status = mbedtls_to_psa_error(
5429 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5430 mbedtls_entropy_func,
5431 &global_data.entropy,
5432 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5433 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005434 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005435 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005436
Gilles Peskine66fb1262018-12-10 16:29:04 +01005437 status = psa_initialize_key_slots( );
5438 if( status != PSA_SUCCESS )
5439 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005440
5441 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005442 global_data.initialized = 1;
5443
Gilles Peskinee59236f2018-01-27 23:32:46 +01005444exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005445 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005446 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005447 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005448}
5449
5450#endif /* MBEDTLS_PSA_CRYPTO_C */