blob: e035eceaa262344ffc38a3caf8d8394398e7e13b [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{
986 memset( attributes, 0, sizeof( *attributes ) );
987}
988
989psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
990 psa_key_attributes_t *attributes )
991{
992 psa_key_slot_t *slot;
993 psa_status_t status;
994
995 psa_reset_key_attributes( attributes );
996
997 status = psa_get_key_slot( handle, &slot );
998 if( status != PSA_SUCCESS )
999 return( status );
1000
1001 attributes->id = slot->persistent_storage_id;
1002 attributes->lifetime = slot->lifetime;
1003 attributes->policy = slot->policy;
1004 attributes->type = slot->type;
1005 attributes->bits = psa_get_key_slot_bits( slot );
1006 return( PSA_SUCCESS );
1007}
1008
Gilles Peskinec5487a82018-12-03 18:08:14 +01001009psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001010 psa_key_type_t *type,
1011 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001013 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001014 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001017 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018 if( bits != NULL )
1019 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001020 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001021 if( status != PSA_SUCCESS )
1022 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001023
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001025 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001026 if( type != NULL )
1027 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001028 if( bits != NULL )
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001029 *bits = psa_get_key_slot_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001030 return( PSA_SUCCESS );
1031}
1032
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001033#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001034static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1035 unsigned char *buf, size_t size )
1036{
1037 int ret;
1038 unsigned char *c;
1039 size_t len = 0;
1040
1041 c = buf + size;
1042
1043 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1044
1045 return( (int) len );
1046}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001047#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001048
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001049static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1050 uint8_t *data,
1051 size_t data_size,
1052 size_t *data_length,
1053 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001054{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001055 *data_length = 0;
1056
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001057 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001058 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001059
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001060 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001061 {
1062 if( slot->data.raw.bytes > data_size )
1063 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001064 if( data_size != 0 )
1065 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001066 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001067 memset( data + slot->data.raw.bytes, 0,
1068 data_size - slot->data.raw.bytes );
1069 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070 *data_length = slot->data.raw.bytes;
1071 return( PSA_SUCCESS );
1072 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001073#if defined(MBEDTLS_ECP_C)
1074 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1075 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001076 psa_status_t status;
1077
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001078 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001079 if( bytes > data_size )
1080 return( PSA_ERROR_BUFFER_TOO_SMALL );
1081 status = mbedtls_to_psa_error(
1082 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1083 if( status != PSA_SUCCESS )
1084 return( status );
1085 memset( data + bytes, 0, data_size - bytes );
1086 *data_length = bytes;
1087 return( PSA_SUCCESS );
1088 }
1089#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001090 else
Moran Peker17e36e12018-05-02 12:55:20 +03001091 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001092#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001093 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001094 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001095 {
Moran Pekera998bc62018-04-16 18:16:20 +03001096 mbedtls_pk_context pk;
1097 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001098 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001099 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001100#if defined(MBEDTLS_RSA_C)
1101 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001102 pk.pk_info = &mbedtls_rsa_info;
1103 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001104#else
1105 return( PSA_ERROR_NOT_SUPPORTED );
1106#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001107 }
1108 else
1109 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001110#if defined(MBEDTLS_ECP_C)
1111 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001112 pk.pk_info = &mbedtls_eckey_info;
1113 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001114#else
1115 return( PSA_ERROR_NOT_SUPPORTED );
1116#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001117 }
Moran Pekerd7326592018-05-29 16:56:39 +03001118 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001119 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001120 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001121 }
Moran Peker17e36e12018-05-02 12:55:20 +03001122 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001123 {
Moran Peker17e36e12018-05-02 12:55:20 +03001124 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001125 }
Moran Peker60364322018-04-29 11:34:58 +03001126 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001127 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001128 /* If data_size is 0 then data may be NULL and then the
1129 * call to memset would have undefined behavior. */
1130 if( data_size != 0 )
1131 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001132 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001133 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001134 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1135 * Move the data to the beginning and erase remaining data
1136 * at the original location. */
1137 if( 2 * (size_t) ret <= data_size )
1138 {
1139 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001140 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001141 }
1142 else if( (size_t) ret < data_size )
1143 {
1144 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001145 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001146 }
Moran Pekera998bc62018-04-16 18:16:20 +03001147 *data_length = ret;
1148 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001149 }
1150 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001151#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001152 {
1153 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001154 it is valid for a special-purpose implementation to omit
1155 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001156 return( PSA_ERROR_NOT_SUPPORTED );
1157 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001158 }
1159}
1160
Gilles Peskinec5487a82018-12-03 18:08:14 +01001161psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001162 uint8_t *data,
1163 size_t data_size,
1164 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001165{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001166 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001167 psa_status_t status;
1168
1169 /* Set the key to empty now, so that even when there are errors, we always
1170 * set data_length to a value between 0 and data_size. On error, setting
1171 * the key to empty is a good choice because an empty key representation is
1172 * unlikely to be accepted anywhere. */
1173 *data_length = 0;
1174
1175 /* Export requires the EXPORT flag. There is an exception for public keys,
1176 * which don't require any flag, but psa_get_key_from_slot takes
1177 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001178 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001179 if( status != PSA_SUCCESS )
1180 return( status );
1181 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001182 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001183}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001184
Gilles Peskinec5487a82018-12-03 18:08:14 +01001185psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001186 uint8_t *data,
1187 size_t data_size,
1188 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001189{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001190 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001191 psa_status_t status;
1192
1193 /* Set the key to empty now, so that even when there are errors, we always
1194 * set data_length to a value between 0 and data_size. On error, setting
1195 * the key to empty is a good choice because an empty key representation is
1196 * unlikely to be accepted anywhere. */
1197 *data_length = 0;
1198
1199 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001200 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001201 if( status != PSA_SUCCESS )
1202 return( status );
1203 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001204 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001205}
1206
Darryl Green0c6575a2018-11-07 16:05:30 +00001207#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001208static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001209 size_t bits )
1210{
1211 psa_status_t status;
1212 uint8_t *data;
1213 size_t key_length;
1214 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1215 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001216 if( data == NULL )
1217 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001218 /* Get key data in export format */
1219 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1220 if( status != PSA_SUCCESS )
1221 {
1222 slot->type = PSA_KEY_TYPE_NONE;
1223 goto exit;
1224 }
1225 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001226 status = psa_save_persistent_key( slot->persistent_storage_id,
1227 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001228 data, key_length );
1229 if( status != PSA_SUCCESS )
1230 {
1231 slot->type = PSA_KEY_TYPE_NONE;
1232 }
1233exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001234 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001235 mbedtls_free( data );
1236 return( status );
1237}
1238#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1239
Gilles Peskine4747d192019-04-17 15:05:45 +02001240static psa_status_t psa_set_key_policy_internal(
1241 psa_key_slot_t *slot,
1242 const psa_key_policy_t *policy )
1243{
1244 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
1245 PSA_KEY_USAGE_ENCRYPT |
1246 PSA_KEY_USAGE_DECRYPT |
1247 PSA_KEY_USAGE_SIGN |
1248 PSA_KEY_USAGE_VERIFY |
1249 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1250 return( PSA_ERROR_INVALID_ARGUMENT );
1251
1252 slot->policy = *policy;
1253 return( PSA_SUCCESS );
1254}
1255
1256/** Prepare a key slot to receive key material.
1257 *
1258 * This function allocates a key slot and sets its metadata.
1259 *
1260 * If this function fails, call psa_fail_key_creation().
1261 *
1262 * \param attributes Key attributes for the new key.
1263 * \param handle On success, the allocated handle.
1264 * \param p_slot On success, a pointer to the prepared slot.
1265 */
1266static psa_status_t psa_start_key_creation(
1267 const psa_key_attributes_t *attributes,
1268 psa_key_handle_t *handle,
1269 psa_key_slot_t **p_slot )
1270{
1271 psa_status_t status;
1272 psa_key_slot_t *slot;
1273
1274 status = psa_allocate_key( handle );
1275 if( status != PSA_SUCCESS )
1276 return( status );
1277 status = psa_get_key_slot( *handle, p_slot );
1278 if( status != PSA_SUCCESS )
1279 return( status );
1280 slot = *p_slot;
1281
1282 status = psa_set_key_policy_internal( slot, &attributes->policy );
1283 if( status != PSA_SUCCESS )
1284 return( status );
1285 slot->lifetime = attributes->lifetime;
1286 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001287 {
1288 status = psa_validate_persistent_key_parameters( attributes->lifetime,
1289 attributes->id );
1290 if( status != PSA_SUCCESS )
1291 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001292 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001293 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001294 slot->type = attributes->type;
1295
1296 return( status );
1297}
1298
1299/** Finalize the creation of a key once its key material has been set.
1300 *
1301 * This entails writing the key to persistent storage.
1302 *
1303 * If this function fails, call psa_fail_key_creation().
1304 *
1305 * \param slot Pointer to the slot with key material.
1306 */
1307static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1308{
1309 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001310 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001311
1312#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1313 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1314 {
1315 uint8_t *buffer = NULL;
1316 size_t buffer_size = 0;
1317 size_t length;
1318
1319 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001320 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001321 buffer = mbedtls_calloc( 1, buffer_size );
1322 if( buffer == NULL && buffer_size != 0 )
1323 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1324 status = psa_internal_export_key( slot,
1325 buffer, buffer_size, &length,
1326 0 );
1327
1328 if( status == PSA_SUCCESS )
1329 {
1330 status = psa_save_persistent_key( slot->persistent_storage_id,
1331 slot->type, &slot->policy,
1332 buffer, length );
1333 }
1334
1335 if( buffer_size != 0 )
1336 mbedtls_platform_zeroize( buffer, buffer_size );
1337 mbedtls_free( buffer );
1338 }
1339#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1340
1341 return( status );
1342}
1343
1344/** Abort the creation of a key.
1345 *
1346 * You may call this function after calling psa_start_key_creation(),
1347 * or after psa_finish_key_creation() fails. In other circumstances, this
1348 * function may not clean up persistent storage.
1349 *
1350 * \param slot Pointer to the slot with key material.
1351 */
1352static void psa_fail_key_creation( psa_key_slot_t *slot )
1353{
1354 if( slot == NULL )
1355 return;
1356 psa_wipe_key_slot( slot );
1357}
1358
1359psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
1360 psa_key_handle_t *handle,
1361 const uint8_t *data,
1362 size_t data_length )
1363{
1364 psa_status_t status;
1365 psa_key_slot_t *slot = NULL;
1366 status = psa_start_key_creation( attributes, handle, &slot );
1367 if( status == PSA_SUCCESS )
1368 {
1369 status = psa_import_key_into_slot( slot, data, data_length );
1370 }
1371 if( status == PSA_SUCCESS )
1372 status = psa_finish_key_creation( slot );
1373 if( status != PSA_SUCCESS )
1374 {
1375 psa_fail_key_creation( slot );
1376 *handle = 0;
1377 }
1378 return( status );
1379}
1380
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001381static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001382 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001383{
1384 psa_status_t status;
1385 uint8_t *buffer = NULL;
1386 size_t buffer_size = 0;
1387 size_t length;
1388
1389 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001390 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001391 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001392 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001393 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001394 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1395 if( status != PSA_SUCCESS )
1396 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001397 target->type = source->type;
1398 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001399
1400exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001401 if( buffer_size != 0 )
1402 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001403 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001404 return( status );
1405}
1406
Gilles Peskine87a5e562019-04-17 12:28:25 +02001407psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001408 psa_key_handle_t target_handle,
1409 const psa_key_policy_t *constraint)
1410{
1411 psa_key_slot_t *source_slot = NULL;
1412 psa_key_slot_t *target_slot = NULL;
1413 psa_key_policy_t new_policy;
1414 psa_status_t status;
1415 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1416 if( status != PSA_SUCCESS )
1417 return( status );
1418 status = psa_get_empty_key_slot( target_handle, &target_slot );
1419 if( status != PSA_SUCCESS )
1420 return( status );
1421
1422 new_policy = target_slot->policy;
1423 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1424 if( status != PSA_SUCCESS )
1425 return( status );
1426 if( constraint != NULL )
1427 {
1428 status = psa_restrict_key_policy( &new_policy, constraint );
1429 if( status != PSA_SUCCESS )
1430 return( status );
1431 }
1432
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001433 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001434 if( status != PSA_SUCCESS )
1435 return( status );
1436
1437 target_slot->policy = new_policy;
1438 return( PSA_SUCCESS );
1439}
1440
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001441psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1442 const psa_key_attributes_t *specified_attributes,
1443 psa_key_handle_t *target_handle )
1444{
1445 psa_status_t status;
1446 psa_key_slot_t *source_slot = NULL;
1447 psa_key_slot_t *target_slot = NULL;
1448 psa_key_attributes_t actual_attributes = *specified_attributes;
1449
1450 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1451 if( status != PSA_SUCCESS )
1452 goto exit;
1453
1454 status = psa_restrict_key_policy( &actual_attributes.policy,
1455 &source_slot->policy );
1456 if( status != PSA_SUCCESS )
1457 goto exit;
1458
1459 status = psa_start_key_creation( &actual_attributes,
1460 target_handle, &target_slot );
1461 if( status != PSA_SUCCESS )
1462 goto exit;
1463
1464 status = psa_copy_key_material( source_slot, target_slot );
1465
1466exit:
1467 if( status == PSA_SUCCESS )
1468 status = psa_finish_key_creation( target_slot );
1469 if( status != PSA_SUCCESS )
1470 {
1471 psa_fail_key_creation( target_slot );
1472 *target_handle = 0;
1473 }
1474 return( status );
1475}
1476
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001477
1478
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001479/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001480/* Message digests */
1481/****************************************************************/
1482
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001483static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001484{
1485 switch( alg )
1486 {
1487#if defined(MBEDTLS_MD2_C)
1488 case PSA_ALG_MD2:
1489 return( &mbedtls_md2_info );
1490#endif
1491#if defined(MBEDTLS_MD4_C)
1492 case PSA_ALG_MD4:
1493 return( &mbedtls_md4_info );
1494#endif
1495#if defined(MBEDTLS_MD5_C)
1496 case PSA_ALG_MD5:
1497 return( &mbedtls_md5_info );
1498#endif
1499#if defined(MBEDTLS_RIPEMD160_C)
1500 case PSA_ALG_RIPEMD160:
1501 return( &mbedtls_ripemd160_info );
1502#endif
1503#if defined(MBEDTLS_SHA1_C)
1504 case PSA_ALG_SHA_1:
1505 return( &mbedtls_sha1_info );
1506#endif
1507#if defined(MBEDTLS_SHA256_C)
1508 case PSA_ALG_SHA_224:
1509 return( &mbedtls_sha224_info );
1510 case PSA_ALG_SHA_256:
1511 return( &mbedtls_sha256_info );
1512#endif
1513#if defined(MBEDTLS_SHA512_C)
1514 case PSA_ALG_SHA_384:
1515 return( &mbedtls_sha384_info );
1516 case PSA_ALG_SHA_512:
1517 return( &mbedtls_sha512_info );
1518#endif
1519 default:
1520 return( NULL );
1521 }
1522}
1523
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001524psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1525{
1526 switch( operation->alg )
1527 {
Gilles Peskine81736312018-06-26 15:04:31 +02001528 case 0:
1529 /* The object has (apparently) been initialized but it is not
1530 * in use. It's ok to call abort on such an object, and there's
1531 * nothing to do. */
1532 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001533#if defined(MBEDTLS_MD2_C)
1534 case PSA_ALG_MD2:
1535 mbedtls_md2_free( &operation->ctx.md2 );
1536 break;
1537#endif
1538#if defined(MBEDTLS_MD4_C)
1539 case PSA_ALG_MD4:
1540 mbedtls_md4_free( &operation->ctx.md4 );
1541 break;
1542#endif
1543#if defined(MBEDTLS_MD5_C)
1544 case PSA_ALG_MD5:
1545 mbedtls_md5_free( &operation->ctx.md5 );
1546 break;
1547#endif
1548#if defined(MBEDTLS_RIPEMD160_C)
1549 case PSA_ALG_RIPEMD160:
1550 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1551 break;
1552#endif
1553#if defined(MBEDTLS_SHA1_C)
1554 case PSA_ALG_SHA_1:
1555 mbedtls_sha1_free( &operation->ctx.sha1 );
1556 break;
1557#endif
1558#if defined(MBEDTLS_SHA256_C)
1559 case PSA_ALG_SHA_224:
1560 case PSA_ALG_SHA_256:
1561 mbedtls_sha256_free( &operation->ctx.sha256 );
1562 break;
1563#endif
1564#if defined(MBEDTLS_SHA512_C)
1565 case PSA_ALG_SHA_384:
1566 case PSA_ALG_SHA_512:
1567 mbedtls_sha512_free( &operation->ctx.sha512 );
1568 break;
1569#endif
1570 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001571 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001572 }
1573 operation->alg = 0;
1574 return( PSA_SUCCESS );
1575}
1576
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001577psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001578 psa_algorithm_t alg )
1579{
1580 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001581
1582 /* A context must be freshly initialized before it can be set up. */
1583 if( operation->alg != 0 )
1584 {
1585 return( PSA_ERROR_BAD_STATE );
1586 }
1587
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001588 switch( alg )
1589 {
1590#if defined(MBEDTLS_MD2_C)
1591 case PSA_ALG_MD2:
1592 mbedtls_md2_init( &operation->ctx.md2 );
1593 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1594 break;
1595#endif
1596#if defined(MBEDTLS_MD4_C)
1597 case PSA_ALG_MD4:
1598 mbedtls_md4_init( &operation->ctx.md4 );
1599 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1600 break;
1601#endif
1602#if defined(MBEDTLS_MD5_C)
1603 case PSA_ALG_MD5:
1604 mbedtls_md5_init( &operation->ctx.md5 );
1605 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1606 break;
1607#endif
1608#if defined(MBEDTLS_RIPEMD160_C)
1609 case PSA_ALG_RIPEMD160:
1610 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1611 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1612 break;
1613#endif
1614#if defined(MBEDTLS_SHA1_C)
1615 case PSA_ALG_SHA_1:
1616 mbedtls_sha1_init( &operation->ctx.sha1 );
1617 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1618 break;
1619#endif
1620#if defined(MBEDTLS_SHA256_C)
1621 case PSA_ALG_SHA_224:
1622 mbedtls_sha256_init( &operation->ctx.sha256 );
1623 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1624 break;
1625 case PSA_ALG_SHA_256:
1626 mbedtls_sha256_init( &operation->ctx.sha256 );
1627 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1628 break;
1629#endif
1630#if defined(MBEDTLS_SHA512_C)
1631 case PSA_ALG_SHA_384:
1632 mbedtls_sha512_init( &operation->ctx.sha512 );
1633 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1634 break;
1635 case PSA_ALG_SHA_512:
1636 mbedtls_sha512_init( &operation->ctx.sha512 );
1637 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1638 break;
1639#endif
1640 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001641 return( PSA_ALG_IS_HASH( alg ) ?
1642 PSA_ERROR_NOT_SUPPORTED :
1643 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001644 }
1645 if( ret == 0 )
1646 operation->alg = alg;
1647 else
1648 psa_hash_abort( operation );
1649 return( mbedtls_to_psa_error( ret ) );
1650}
1651
1652psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1653 const uint8_t *input,
1654 size_t input_length )
1655{
1656 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001657
1658 /* Don't require hash implementations to behave correctly on a
1659 * zero-length input, which may have an invalid pointer. */
1660 if( input_length == 0 )
1661 return( PSA_SUCCESS );
1662
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001663 switch( operation->alg )
1664 {
1665#if defined(MBEDTLS_MD2_C)
1666 case PSA_ALG_MD2:
1667 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1668 input, input_length );
1669 break;
1670#endif
1671#if defined(MBEDTLS_MD4_C)
1672 case PSA_ALG_MD4:
1673 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1674 input, input_length );
1675 break;
1676#endif
1677#if defined(MBEDTLS_MD5_C)
1678 case PSA_ALG_MD5:
1679 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1680 input, input_length );
1681 break;
1682#endif
1683#if defined(MBEDTLS_RIPEMD160_C)
1684 case PSA_ALG_RIPEMD160:
1685 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1686 input, input_length );
1687 break;
1688#endif
1689#if defined(MBEDTLS_SHA1_C)
1690 case PSA_ALG_SHA_1:
1691 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1692 input, input_length );
1693 break;
1694#endif
1695#if defined(MBEDTLS_SHA256_C)
1696 case PSA_ALG_SHA_224:
1697 case PSA_ALG_SHA_256:
1698 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1699 input, input_length );
1700 break;
1701#endif
1702#if defined(MBEDTLS_SHA512_C)
1703 case PSA_ALG_SHA_384:
1704 case PSA_ALG_SHA_512:
1705 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1706 input, input_length );
1707 break;
1708#endif
1709 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001710 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001711 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001712
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001713 if( ret != 0 )
1714 psa_hash_abort( operation );
1715 return( mbedtls_to_psa_error( ret ) );
1716}
1717
1718psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1719 uint8_t *hash,
1720 size_t hash_size,
1721 size_t *hash_length )
1722{
itayzafrir40835d42018-08-02 13:14:17 +03001723 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001724 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001725 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001726
1727 /* Fill the output buffer with something that isn't a valid hash
1728 * (barring an attack on the hash and deliberately-crafted input),
1729 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001730 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001731 /* If hash_size is 0 then hash may be NULL and then the
1732 * call to memset would have undefined behavior. */
1733 if( hash_size != 0 )
1734 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001735
1736 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001737 {
1738 status = PSA_ERROR_BUFFER_TOO_SMALL;
1739 goto exit;
1740 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001741
1742 switch( operation->alg )
1743 {
1744#if defined(MBEDTLS_MD2_C)
1745 case PSA_ALG_MD2:
1746 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1747 break;
1748#endif
1749#if defined(MBEDTLS_MD4_C)
1750 case PSA_ALG_MD4:
1751 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1752 break;
1753#endif
1754#if defined(MBEDTLS_MD5_C)
1755 case PSA_ALG_MD5:
1756 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1757 break;
1758#endif
1759#if defined(MBEDTLS_RIPEMD160_C)
1760 case PSA_ALG_RIPEMD160:
1761 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1762 break;
1763#endif
1764#if defined(MBEDTLS_SHA1_C)
1765 case PSA_ALG_SHA_1:
1766 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1767 break;
1768#endif
1769#if defined(MBEDTLS_SHA256_C)
1770 case PSA_ALG_SHA_224:
1771 case PSA_ALG_SHA_256:
1772 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1773 break;
1774#endif
1775#if defined(MBEDTLS_SHA512_C)
1776 case PSA_ALG_SHA_384:
1777 case PSA_ALG_SHA_512:
1778 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1779 break;
1780#endif
1781 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001782 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001783 }
itayzafrir40835d42018-08-02 13:14:17 +03001784 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001785
itayzafrir40835d42018-08-02 13:14:17 +03001786exit:
1787 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001788 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001789 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001790 return( psa_hash_abort( operation ) );
1791 }
1792 else
1793 {
1794 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001795 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001796 }
1797}
1798
Gilles Peskine2d277862018-06-18 15:41:12 +02001799psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1800 const uint8_t *hash,
1801 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001802{
1803 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1804 size_t actual_hash_length;
1805 psa_status_t status = psa_hash_finish( operation,
1806 actual_hash, sizeof( actual_hash ),
1807 &actual_hash_length );
1808 if( status != PSA_SUCCESS )
1809 return( status );
1810 if( actual_hash_length != hash_length )
1811 return( PSA_ERROR_INVALID_SIGNATURE );
1812 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1813 return( PSA_ERROR_INVALID_SIGNATURE );
1814 return( PSA_SUCCESS );
1815}
1816
Gilles Peskineeb35d782019-01-22 17:56:16 +01001817psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1818 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001819{
1820 if( target_operation->alg != 0 )
1821 return( PSA_ERROR_BAD_STATE );
1822
1823 switch( source_operation->alg )
1824 {
1825 case 0:
1826 return( PSA_ERROR_BAD_STATE );
1827#if defined(MBEDTLS_MD2_C)
1828 case PSA_ALG_MD2:
1829 mbedtls_md2_clone( &target_operation->ctx.md2,
1830 &source_operation->ctx.md2 );
1831 break;
1832#endif
1833#if defined(MBEDTLS_MD4_C)
1834 case PSA_ALG_MD4:
1835 mbedtls_md4_clone( &target_operation->ctx.md4,
1836 &source_operation->ctx.md4 );
1837 break;
1838#endif
1839#if defined(MBEDTLS_MD5_C)
1840 case PSA_ALG_MD5:
1841 mbedtls_md5_clone( &target_operation->ctx.md5,
1842 &source_operation->ctx.md5 );
1843 break;
1844#endif
1845#if defined(MBEDTLS_RIPEMD160_C)
1846 case PSA_ALG_RIPEMD160:
1847 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1848 &source_operation->ctx.ripemd160 );
1849 break;
1850#endif
1851#if defined(MBEDTLS_SHA1_C)
1852 case PSA_ALG_SHA_1:
1853 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1854 &source_operation->ctx.sha1 );
1855 break;
1856#endif
1857#if defined(MBEDTLS_SHA256_C)
1858 case PSA_ALG_SHA_224:
1859 case PSA_ALG_SHA_256:
1860 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1861 &source_operation->ctx.sha256 );
1862 break;
1863#endif
1864#if defined(MBEDTLS_SHA512_C)
1865 case PSA_ALG_SHA_384:
1866 case PSA_ALG_SHA_512:
1867 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1868 &source_operation->ctx.sha512 );
1869 break;
1870#endif
1871 default:
1872 return( PSA_ERROR_NOT_SUPPORTED );
1873 }
1874
1875 target_operation->alg = source_operation->alg;
1876 return( PSA_SUCCESS );
1877}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001878
1879
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001880/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001881/* MAC */
1882/****************************************************************/
1883
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001884static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001885 psa_algorithm_t alg,
1886 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001887 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001888 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001889{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001890 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001891 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001892
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001893 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001894 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001895
Gilles Peskine8c9def32018-02-08 10:02:12 +01001896 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1897 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001898 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001899 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001900 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001901 mode = MBEDTLS_MODE_STREAM;
1902 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001903 case PSA_ALG_CTR:
1904 mode = MBEDTLS_MODE_CTR;
1905 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001906 case PSA_ALG_CFB:
1907 mode = MBEDTLS_MODE_CFB;
1908 break;
1909 case PSA_ALG_OFB:
1910 mode = MBEDTLS_MODE_OFB;
1911 break;
1912 case PSA_ALG_CBC_NO_PADDING:
1913 mode = MBEDTLS_MODE_CBC;
1914 break;
1915 case PSA_ALG_CBC_PKCS7:
1916 mode = MBEDTLS_MODE_CBC;
1917 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001918 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001919 mode = MBEDTLS_MODE_CCM;
1920 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001921 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001922 mode = MBEDTLS_MODE_GCM;
1923 break;
1924 default:
1925 return( NULL );
1926 }
1927 }
1928 else if( alg == PSA_ALG_CMAC )
1929 mode = MBEDTLS_MODE_ECB;
1930 else if( alg == PSA_ALG_GMAC )
1931 mode = MBEDTLS_MODE_GCM;
1932 else
1933 return( NULL );
1934
1935 switch( key_type )
1936 {
1937 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001938 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001939 break;
1940 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001941 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1942 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001943 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001944 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001945 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001946 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001947 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1948 * but two-key Triple-DES is functionally three-key Triple-DES
1949 * with K1=K3, so that's how we present it to mbedtls. */
1950 if( key_bits == 128 )
1951 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001952 break;
1953 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001954 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001955 break;
1956 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001957 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001958 break;
1959 default:
1960 return( NULL );
1961 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001962 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001963 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001964
Jaeden Amero23bbb752018-06-26 14:16:54 +01001965 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1966 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001967}
1968
Gilles Peskinea05219c2018-11-16 16:02:56 +01001969#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001970static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001971{
Gilles Peskine2d277862018-06-18 15:41:12 +02001972 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001973 {
1974 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001975 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001976 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001977 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001978 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001979 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001980 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001981 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001982 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001983 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001984 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001985 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001986 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001987 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001988 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001989 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001990 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001991 return( 128 );
1992 default:
1993 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001994 }
1995}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001996#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001997
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001998/* Initialize the MAC operation structure. Once this function has been
1999 * called, psa_mac_abort can run and will do the right thing. */
2000static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2001 psa_algorithm_t alg )
2002{
2003 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2004
2005 operation->alg = alg;
2006 operation->key_set = 0;
2007 operation->iv_set = 0;
2008 operation->iv_required = 0;
2009 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002010 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002011
2012#if defined(MBEDTLS_CMAC_C)
2013 if( alg == PSA_ALG_CMAC )
2014 {
2015 operation->iv_required = 0;
2016 mbedtls_cipher_init( &operation->ctx.cmac );
2017 status = PSA_SUCCESS;
2018 }
2019 else
2020#endif /* MBEDTLS_CMAC_C */
2021#if defined(MBEDTLS_MD_C)
2022 if( PSA_ALG_IS_HMAC( operation->alg ) )
2023 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002024 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2025 operation->ctx.hmac.hash_ctx.alg = 0;
2026 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002027 }
2028 else
2029#endif /* MBEDTLS_MD_C */
2030 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002031 if( ! PSA_ALG_IS_MAC( alg ) )
2032 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002033 }
2034
2035 if( status != PSA_SUCCESS )
2036 memset( operation, 0, sizeof( *operation ) );
2037 return( status );
2038}
2039
Gilles Peskine01126fa2018-07-12 17:04:55 +02002040#if defined(MBEDTLS_MD_C)
2041static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2042{
Gilles Peskine3f108122018-12-07 18:14:53 +01002043 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002044 return( psa_hash_abort( &hmac->hash_ctx ) );
2045}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002046
2047static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2048{
2049 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2050 memset( hmac, 0, sizeof( *hmac ) );
2051}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002052#endif /* MBEDTLS_MD_C */
2053
Gilles Peskine8c9def32018-02-08 10:02:12 +01002054psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2055{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002056 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002057 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002058 /* The object has (apparently) been initialized but it is not
2059 * in use. It's ok to call abort on such an object, and there's
2060 * nothing to do. */
2061 return( PSA_SUCCESS );
2062 }
2063 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002064#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002065 if( operation->alg == PSA_ALG_CMAC )
2066 {
2067 mbedtls_cipher_free( &operation->ctx.cmac );
2068 }
2069 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002070#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002071#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002072 if( PSA_ALG_IS_HMAC( operation->alg ) )
2073 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002074 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002075 }
2076 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002077#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002078 {
2079 /* Sanity check (shouldn't happen: operation->alg should
2080 * always have been initialized to a valid value). */
2081 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002082 }
Moran Peker41deec42018-04-04 15:43:05 +03002083
Gilles Peskine8c9def32018-02-08 10:02:12 +01002084 operation->alg = 0;
2085 operation->key_set = 0;
2086 operation->iv_set = 0;
2087 operation->iv_required = 0;
2088 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002089 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002090
Gilles Peskine8c9def32018-02-08 10:02:12 +01002091 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002092
2093bad_state:
2094 /* If abort is called on an uninitialized object, we can't trust
2095 * anything. Wipe the object in case it contains confidential data.
2096 * This may result in a memory leak if a pointer gets overwritten,
2097 * but it's too late to do anything about this. */
2098 memset( operation, 0, sizeof( *operation ) );
2099 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002100}
2101
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002102#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002103static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002104 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002105 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002106 const mbedtls_cipher_info_t *cipher_info )
2107{
2108 int ret;
2109
2110 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002111
2112 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2113 if( ret != 0 )
2114 return( ret );
2115
2116 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2117 slot->data.raw.data,
2118 key_bits );
2119 return( ret );
2120}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002121#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002122
Gilles Peskine248051a2018-06-20 16:09:38 +02002123#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002124static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2125 const uint8_t *key,
2126 size_t key_length,
2127 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002128{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002129 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002130 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002131 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002132 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002133 psa_status_t status;
2134
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002135 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2136 * overflow below. This should never trigger if the hash algorithm
2137 * is implemented correctly. */
2138 /* The size checks against the ipad and opad buffers cannot be written
2139 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2140 * because that triggers -Wlogical-op on GCC 7.3. */
2141 if( block_size > sizeof( ipad ) )
2142 return( PSA_ERROR_NOT_SUPPORTED );
2143 if( block_size > sizeof( hmac->opad ) )
2144 return( PSA_ERROR_NOT_SUPPORTED );
2145 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002146 return( PSA_ERROR_NOT_SUPPORTED );
2147
Gilles Peskined223b522018-06-11 18:12:58 +02002148 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002149 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002150 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002151 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002152 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002153 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002154 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002155 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002156 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002157 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002158 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002159 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002160 }
Gilles Peskine96889972018-07-12 17:07:03 +02002161 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2162 * but it is permitted. It is common when HMAC is used in HKDF, for
2163 * example. Don't call `memcpy` in the 0-length because `key` could be
2164 * an invalid pointer which would make the behavior undefined. */
2165 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002166 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002167
Gilles Peskined223b522018-06-11 18:12:58 +02002168 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2169 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002170 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002171 ipad[i] ^= 0x36;
2172 memset( ipad + key_length, 0x36, block_size - key_length );
2173
2174 /* Copy the key material from ipad to opad, flipping the requisite bits,
2175 * and filling the rest of opad with the requisite constant. */
2176 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002177 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2178 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002179
Gilles Peskine01126fa2018-07-12 17:04:55 +02002180 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002181 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002182 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002183
Gilles Peskine01126fa2018-07-12 17:04:55 +02002184 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002185
2186cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002187 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002188
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002189 return( status );
2190}
Gilles Peskine248051a2018-06-20 16:09:38 +02002191#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002192
Gilles Peskine89167cb2018-07-08 20:12:23 +02002193static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002194 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002195 psa_algorithm_t alg,
2196 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002197{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002198 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002199 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002200 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002201 psa_key_usage_t usage =
2202 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002203 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002204 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002205
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002206 /* A context must be freshly initialized before it can be set up. */
2207 if( operation->alg != 0 )
2208 {
2209 return( PSA_ERROR_BAD_STATE );
2210 }
2211
Gilles Peskined911eb72018-08-14 15:18:45 +02002212 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002213 if( status != PSA_SUCCESS )
2214 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002215 if( is_sign )
2216 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002217
Gilles Peskinec5487a82018-12-03 18:08:14 +01002218 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002219 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002220 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002221 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002222
Gilles Peskine8c9def32018-02-08 10:02:12 +01002223#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002224 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002225 {
2226 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002227 mbedtls_cipher_info_from_psa( full_length_alg,
2228 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002229 int ret;
2230 if( cipher_info == NULL )
2231 {
2232 status = PSA_ERROR_NOT_SUPPORTED;
2233 goto exit;
2234 }
2235 operation->mac_size = cipher_info->block_size;
2236 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2237 status = mbedtls_to_psa_error( ret );
2238 }
2239 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002240#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002241#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002242 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002243 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002244 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002245 if( hash_alg == 0 )
2246 {
2247 status = PSA_ERROR_NOT_SUPPORTED;
2248 goto exit;
2249 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002250
2251 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2252 /* Sanity check. This shouldn't fail on a valid configuration. */
2253 if( operation->mac_size == 0 ||
2254 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2255 {
2256 status = PSA_ERROR_NOT_SUPPORTED;
2257 goto exit;
2258 }
2259
Gilles Peskine01126fa2018-07-12 17:04:55 +02002260 if( slot->type != PSA_KEY_TYPE_HMAC )
2261 {
2262 status = PSA_ERROR_INVALID_ARGUMENT;
2263 goto exit;
2264 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002265
Gilles Peskine01126fa2018-07-12 17:04:55 +02002266 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2267 slot->data.raw.data,
2268 slot->data.raw.bytes,
2269 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002270 }
2271 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002272#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002273 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002274 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002275 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002276 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002277
Gilles Peskined911eb72018-08-14 15:18:45 +02002278 if( truncated == 0 )
2279 {
2280 /* The "normal" case: untruncated algorithm. Nothing to do. */
2281 }
2282 else if( truncated < 4 )
2283 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002284 /* A very short MAC is too short for security since it can be
2285 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2286 * so we make this our minimum, even though 32 bits is still
2287 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002288 status = PSA_ERROR_NOT_SUPPORTED;
2289 }
2290 else if( truncated > operation->mac_size )
2291 {
2292 /* It's impossible to "truncate" to a larger length. */
2293 status = PSA_ERROR_INVALID_ARGUMENT;
2294 }
2295 else
2296 operation->mac_size = truncated;
2297
Gilles Peskinefbfac682018-07-08 20:51:54 +02002298exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002299 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002300 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002301 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002302 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002303 else
2304 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002305 operation->key_set = 1;
2306 }
2307 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002308}
2309
Gilles Peskine89167cb2018-07-08 20:12:23 +02002310psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002311 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002312 psa_algorithm_t alg )
2313{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002314 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002315}
2316
2317psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002318 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002319 psa_algorithm_t alg )
2320{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002321 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002322}
2323
Gilles Peskine8c9def32018-02-08 10:02:12 +01002324psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2325 const uint8_t *input,
2326 size_t input_length )
2327{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002328 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002329 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002330 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002331 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002332 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002333 operation->has_input = 1;
2334
Gilles Peskine8c9def32018-02-08 10:02:12 +01002335#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002336 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002337 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002338 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2339 input, input_length );
2340 status = mbedtls_to_psa_error( ret );
2341 }
2342 else
2343#endif /* MBEDTLS_CMAC_C */
2344#if defined(MBEDTLS_MD_C)
2345 if( PSA_ALG_IS_HMAC( operation->alg ) )
2346 {
2347 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2348 input_length );
2349 }
2350 else
2351#endif /* MBEDTLS_MD_C */
2352 {
2353 /* This shouldn't happen if `operation` was initialized by
2354 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002355 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002356 }
2357
Gilles Peskinefbfac682018-07-08 20:51:54 +02002358 if( status != PSA_SUCCESS )
2359 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002360 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002361}
2362
Gilles Peskine01126fa2018-07-12 17:04:55 +02002363#if defined(MBEDTLS_MD_C)
2364static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2365 uint8_t *mac,
2366 size_t mac_size )
2367{
2368 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2369 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2370 size_t hash_size = 0;
2371 size_t block_size = psa_get_hash_block_size( hash_alg );
2372 psa_status_t status;
2373
Gilles Peskine01126fa2018-07-12 17:04:55 +02002374 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2375 if( status != PSA_SUCCESS )
2376 return( status );
2377 /* From here on, tmp needs to be wiped. */
2378
2379 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2380 if( status != PSA_SUCCESS )
2381 goto exit;
2382
2383 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2384 if( status != PSA_SUCCESS )
2385 goto exit;
2386
2387 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2388 if( status != PSA_SUCCESS )
2389 goto exit;
2390
Gilles Peskined911eb72018-08-14 15:18:45 +02002391 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2392 if( status != PSA_SUCCESS )
2393 goto exit;
2394
2395 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002396
2397exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002398 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002399 return( status );
2400}
2401#endif /* MBEDTLS_MD_C */
2402
mohammad16036df908f2018-04-02 08:34:15 -07002403static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002404 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002405 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002406{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002407 if( ! operation->key_set )
2408 return( PSA_ERROR_BAD_STATE );
2409 if( operation->iv_required && ! operation->iv_set )
2410 return( PSA_ERROR_BAD_STATE );
2411
Gilles Peskine8c9def32018-02-08 10:02:12 +01002412 if( mac_size < operation->mac_size )
2413 return( PSA_ERROR_BUFFER_TOO_SMALL );
2414
Gilles Peskine8c9def32018-02-08 10:02:12 +01002415#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002416 if( operation->alg == PSA_ALG_CMAC )
2417 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002418 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2419 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2420 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002421 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002422 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002423 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002424 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002425 else
2426#endif /* MBEDTLS_CMAC_C */
2427#if defined(MBEDTLS_MD_C)
2428 if( PSA_ALG_IS_HMAC( operation->alg ) )
2429 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002430 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002431 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002432 }
2433 else
2434#endif /* MBEDTLS_MD_C */
2435 {
2436 /* This shouldn't happen if `operation` was initialized by
2437 * a setup function. */
2438 return( PSA_ERROR_BAD_STATE );
2439 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002440}
2441
Gilles Peskineacd4be32018-07-08 19:56:25 +02002442psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2443 uint8_t *mac,
2444 size_t mac_size,
2445 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002446{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002447 psa_status_t status;
2448
Jaeden Amero252ef282019-02-15 14:05:35 +00002449 if( operation->alg == 0 )
2450 {
2451 return( PSA_ERROR_BAD_STATE );
2452 }
2453
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002454 /* Fill the output buffer with something that isn't a valid mac
2455 * (barring an attack on the mac and deliberately-crafted input),
2456 * in case the caller doesn't check the return status properly. */
2457 *mac_length = mac_size;
2458 /* If mac_size is 0 then mac may be NULL and then the
2459 * call to memset would have undefined behavior. */
2460 if( mac_size != 0 )
2461 memset( mac, '!', mac_size );
2462
Gilles Peskine89167cb2018-07-08 20:12:23 +02002463 if( ! operation->is_sign )
2464 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002465 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002466 }
mohammad16036df908f2018-04-02 08:34:15 -07002467
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002468 status = psa_mac_finish_internal( operation, mac, mac_size );
2469
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002470 if( status == PSA_SUCCESS )
2471 {
2472 status = psa_mac_abort( operation );
2473 if( status == PSA_SUCCESS )
2474 *mac_length = operation->mac_size;
2475 else
2476 memset( mac, '!', mac_size );
2477 }
2478 else
2479 psa_mac_abort( operation );
2480 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002481}
2482
Gilles Peskineacd4be32018-07-08 19:56:25 +02002483psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2484 const uint8_t *mac,
2485 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486{
Gilles Peskine828ed142018-06-18 23:25:51 +02002487 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002488 psa_status_t status;
2489
Jaeden Amero252ef282019-02-15 14:05:35 +00002490 if( operation->alg == 0 )
2491 {
2492 return( PSA_ERROR_BAD_STATE );
2493 }
2494
Gilles Peskine89167cb2018-07-08 20:12:23 +02002495 if( operation->is_sign )
2496 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002497 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002498 }
2499 if( operation->mac_size != mac_length )
2500 {
2501 status = PSA_ERROR_INVALID_SIGNATURE;
2502 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002503 }
mohammad16036df908f2018-04-02 08:34:15 -07002504
2505 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002506 actual_mac, sizeof( actual_mac ) );
2507
2508 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2509 status = PSA_ERROR_INVALID_SIGNATURE;
2510
2511cleanup:
2512 if( status == PSA_SUCCESS )
2513 status = psa_mac_abort( operation );
2514 else
2515 psa_mac_abort( operation );
2516
Gilles Peskine3f108122018-12-07 18:14:53 +01002517 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002518
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002519 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520}
2521
2522
Gilles Peskine20035e32018-02-03 22:44:14 +01002523
Gilles Peskine20035e32018-02-03 22:44:14 +01002524/****************************************************************/
2525/* Asymmetric cryptography */
2526/****************************************************************/
2527
Gilles Peskine2b450e32018-06-27 15:42:46 +02002528#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002529/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002530 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002531static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2532 size_t hash_length,
2533 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002534{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002535 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002536 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002537 *md_alg = mbedtls_md_get_type( md_info );
2538
2539 /* The Mbed TLS RSA module uses an unsigned int for hash length
2540 * parameters. Validate that it fits so that we don't risk an
2541 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002542#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002543 if( hash_length > UINT_MAX )
2544 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002545#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002546
2547#if defined(MBEDTLS_PKCS1_V15)
2548 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2549 * must be correct. */
2550 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2551 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002552 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002553 if( md_info == NULL )
2554 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002555 if( mbedtls_md_get_size( md_info ) != hash_length )
2556 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002557 }
2558#endif /* MBEDTLS_PKCS1_V15 */
2559
2560#if defined(MBEDTLS_PKCS1_V21)
2561 /* PSS requires a hash internally. */
2562 if( PSA_ALG_IS_RSA_PSS( alg ) )
2563 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002564 if( md_info == NULL )
2565 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002566 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002567#endif /* MBEDTLS_PKCS1_V21 */
2568
Gilles Peskine61b91d42018-06-08 16:09:36 +02002569 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002570}
2571
Gilles Peskine2b450e32018-06-27 15:42:46 +02002572static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2573 psa_algorithm_t alg,
2574 const uint8_t *hash,
2575 size_t hash_length,
2576 uint8_t *signature,
2577 size_t signature_size,
2578 size_t *signature_length )
2579{
2580 psa_status_t status;
2581 int ret;
2582 mbedtls_md_type_t md_alg;
2583
2584 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2585 if( status != PSA_SUCCESS )
2586 return( status );
2587
Gilles Peskine630a18a2018-06-29 17:49:35 +02002588 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002589 return( PSA_ERROR_BUFFER_TOO_SMALL );
2590
2591#if defined(MBEDTLS_PKCS1_V15)
2592 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2593 {
2594 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2595 MBEDTLS_MD_NONE );
2596 ret = mbedtls_rsa_pkcs1_sign( rsa,
2597 mbedtls_ctr_drbg_random,
2598 &global_data.ctr_drbg,
2599 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002600 md_alg,
2601 (unsigned int) hash_length,
2602 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002603 signature );
2604 }
2605 else
2606#endif /* MBEDTLS_PKCS1_V15 */
2607#if defined(MBEDTLS_PKCS1_V21)
2608 if( PSA_ALG_IS_RSA_PSS( alg ) )
2609 {
2610 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2611 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2612 mbedtls_ctr_drbg_random,
2613 &global_data.ctr_drbg,
2614 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002615 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002616 (unsigned int) hash_length,
2617 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002618 signature );
2619 }
2620 else
2621#endif /* MBEDTLS_PKCS1_V21 */
2622 {
2623 return( PSA_ERROR_INVALID_ARGUMENT );
2624 }
2625
2626 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002627 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002628 return( mbedtls_to_psa_error( ret ) );
2629}
2630
2631static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2632 psa_algorithm_t alg,
2633 const uint8_t *hash,
2634 size_t hash_length,
2635 const uint8_t *signature,
2636 size_t signature_length )
2637{
2638 psa_status_t status;
2639 int ret;
2640 mbedtls_md_type_t md_alg;
2641
2642 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2643 if( status != PSA_SUCCESS )
2644 return( status );
2645
Gilles Peskine630a18a2018-06-29 17:49:35 +02002646 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002647 return( PSA_ERROR_BUFFER_TOO_SMALL );
2648
2649#if defined(MBEDTLS_PKCS1_V15)
2650 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2651 {
2652 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2653 MBEDTLS_MD_NONE );
2654 ret = mbedtls_rsa_pkcs1_verify( rsa,
2655 mbedtls_ctr_drbg_random,
2656 &global_data.ctr_drbg,
2657 MBEDTLS_RSA_PUBLIC,
2658 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002659 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002660 hash,
2661 signature );
2662 }
2663 else
2664#endif /* MBEDTLS_PKCS1_V15 */
2665#if defined(MBEDTLS_PKCS1_V21)
2666 if( PSA_ALG_IS_RSA_PSS( alg ) )
2667 {
2668 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2669 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2670 mbedtls_ctr_drbg_random,
2671 &global_data.ctr_drbg,
2672 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002673 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002674 (unsigned int) hash_length,
2675 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002676 signature );
2677 }
2678 else
2679#endif /* MBEDTLS_PKCS1_V21 */
2680 {
2681 return( PSA_ERROR_INVALID_ARGUMENT );
2682 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002683
2684 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2685 * the rest of the signature is invalid". This has little use in
2686 * practice and PSA doesn't report this distinction. */
2687 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2688 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002689 return( mbedtls_to_psa_error( ret ) );
2690}
2691#endif /* MBEDTLS_RSA_C */
2692
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002693#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002694/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2695 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2696 * (even though these functions don't modify it). */
2697static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2698 psa_algorithm_t alg,
2699 const uint8_t *hash,
2700 size_t hash_length,
2701 uint8_t *signature,
2702 size_t signature_size,
2703 size_t *signature_length )
2704{
2705 int ret;
2706 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002707 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002708 mbedtls_mpi_init( &r );
2709 mbedtls_mpi_init( &s );
2710
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002711 if( signature_size < 2 * curve_bytes )
2712 {
2713 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2714 goto cleanup;
2715 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002716
Gilles Peskinea05219c2018-11-16 16:02:56 +01002717#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002718 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2719 {
2720 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2721 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2722 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2723 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2724 hash, hash_length,
2725 md_alg ) );
2726 }
2727 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002728#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002729 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002730 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002731 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2732 hash, hash_length,
2733 mbedtls_ctr_drbg_random,
2734 &global_data.ctr_drbg ) );
2735 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002736
2737 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2738 signature,
2739 curve_bytes ) );
2740 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2741 signature + curve_bytes,
2742 curve_bytes ) );
2743
2744cleanup:
2745 mbedtls_mpi_free( &r );
2746 mbedtls_mpi_free( &s );
2747 if( ret == 0 )
2748 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002749 return( mbedtls_to_psa_error( ret ) );
2750}
2751
2752static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2753 const uint8_t *hash,
2754 size_t hash_length,
2755 const uint8_t *signature,
2756 size_t signature_length )
2757{
2758 int ret;
2759 mbedtls_mpi r, s;
2760 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2761 mbedtls_mpi_init( &r );
2762 mbedtls_mpi_init( &s );
2763
2764 if( signature_length != 2 * curve_bytes )
2765 return( PSA_ERROR_INVALID_SIGNATURE );
2766
2767 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2768 signature,
2769 curve_bytes ) );
2770 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2771 signature + curve_bytes,
2772 curve_bytes ) );
2773
2774 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2775 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002776
2777cleanup:
2778 mbedtls_mpi_free( &r );
2779 mbedtls_mpi_free( &s );
2780 return( mbedtls_to_psa_error( ret ) );
2781}
2782#endif /* MBEDTLS_ECDSA_C */
2783
Gilles Peskinec5487a82018-12-03 18:08:14 +01002784psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002785 psa_algorithm_t alg,
2786 const uint8_t *hash,
2787 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002788 uint8_t *signature,
2789 size_t signature_size,
2790 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002791{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002792 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002793 psa_status_t status;
2794
2795 *signature_length = signature_size;
2796
Gilles Peskinec5487a82018-12-03 18:08:14 +01002797 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002798 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002799 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002800 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002801 {
2802 status = PSA_ERROR_INVALID_ARGUMENT;
2803 goto exit;
2804 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002805
Gilles Peskine20035e32018-02-03 22:44:14 +01002806#if defined(MBEDTLS_RSA_C)
2807 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2808 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002809 status = psa_rsa_sign( slot->data.rsa,
2810 alg,
2811 hash, hash_length,
2812 signature, signature_size,
2813 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002814 }
2815 else
2816#endif /* defined(MBEDTLS_RSA_C) */
2817#if defined(MBEDTLS_ECP_C)
2818 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2819 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002820#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002821 if(
2822#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2823 PSA_ALG_IS_ECDSA( alg )
2824#else
2825 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2826#endif
2827 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002828 status = psa_ecdsa_sign( slot->data.ecp,
2829 alg,
2830 hash, hash_length,
2831 signature, signature_size,
2832 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002833 else
2834#endif /* defined(MBEDTLS_ECDSA_C) */
2835 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002836 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002837 }
itayzafrir5c753392018-05-08 11:18:38 +03002838 }
2839 else
2840#endif /* defined(MBEDTLS_ECP_C) */
2841 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002842 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002843 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002844
2845exit:
2846 /* Fill the unused part of the output buffer (the whole buffer on error,
2847 * the trailing part on success) with something that isn't a valid mac
2848 * (barring an attack on the mac and deliberately-crafted input),
2849 * in case the caller doesn't check the return status properly. */
2850 if( status == PSA_SUCCESS )
2851 memset( signature + *signature_length, '!',
2852 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002853 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002854 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002855 /* If signature_size is 0 then we have nothing to do. We must not call
2856 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002857 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002858}
2859
Gilles Peskinec5487a82018-12-03 18:08:14 +01002860psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002861 psa_algorithm_t alg,
2862 const uint8_t *hash,
2863 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002864 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002865 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002866{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002867 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002868 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002869
Gilles Peskinec5487a82018-12-03 18:08:14 +01002870 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002871 if( status != PSA_SUCCESS )
2872 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002873
Gilles Peskine61b91d42018-06-08 16:09:36 +02002874#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002875 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002876 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002877 return( psa_rsa_verify( slot->data.rsa,
2878 alg,
2879 hash, hash_length,
2880 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002881 }
2882 else
2883#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002884#if defined(MBEDTLS_ECP_C)
2885 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2886 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002887#if defined(MBEDTLS_ECDSA_C)
2888 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002889 return( psa_ecdsa_verify( slot->data.ecp,
2890 hash, hash_length,
2891 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002892 else
2893#endif /* defined(MBEDTLS_ECDSA_C) */
2894 {
2895 return( PSA_ERROR_INVALID_ARGUMENT );
2896 }
itayzafrir5c753392018-05-08 11:18:38 +03002897 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002898 else
2899#endif /* defined(MBEDTLS_ECP_C) */
2900 {
2901 return( PSA_ERROR_NOT_SUPPORTED );
2902 }
2903}
2904
Gilles Peskine072ac562018-06-30 00:21:29 +02002905#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2906static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2907 mbedtls_rsa_context *rsa )
2908{
2909 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2910 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2911 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2912 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2913}
2914#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2915
Gilles Peskinec5487a82018-12-03 18:08:14 +01002916psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002917 psa_algorithm_t alg,
2918 const uint8_t *input,
2919 size_t input_length,
2920 const uint8_t *salt,
2921 size_t salt_length,
2922 uint8_t *output,
2923 size_t output_size,
2924 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002925{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002926 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002927 psa_status_t status;
2928
Darryl Green5cc689a2018-07-24 15:34:10 +01002929 (void) input;
2930 (void) input_length;
2931 (void) salt;
2932 (void) output;
2933 (void) output_size;
2934
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002935 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002936
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002937 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2938 return( PSA_ERROR_INVALID_ARGUMENT );
2939
Gilles Peskinec5487a82018-12-03 18:08:14 +01002940 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002941 if( status != PSA_SUCCESS )
2942 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002943 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2944 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002945 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002946
2947#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002948 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002949 {
2950 mbedtls_rsa_context *rsa = slot->data.rsa;
2951 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002952 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002953 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002954#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002955 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002956 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002957 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2958 mbedtls_ctr_drbg_random,
2959 &global_data.ctr_drbg,
2960 MBEDTLS_RSA_PUBLIC,
2961 input_length,
2962 input,
2963 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002964 }
2965 else
2966#endif /* MBEDTLS_PKCS1_V15 */
2967#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002968 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002969 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002970 psa_rsa_oaep_set_padding_mode( alg, rsa );
2971 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2972 mbedtls_ctr_drbg_random,
2973 &global_data.ctr_drbg,
2974 MBEDTLS_RSA_PUBLIC,
2975 salt, salt_length,
2976 input_length,
2977 input,
2978 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002979 }
2980 else
2981#endif /* MBEDTLS_PKCS1_V21 */
2982 {
2983 return( PSA_ERROR_INVALID_ARGUMENT );
2984 }
2985 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002986 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002987 return( mbedtls_to_psa_error( ret ) );
2988 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002989 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002990#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002991 {
2992 return( PSA_ERROR_NOT_SUPPORTED );
2993 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002994}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002995
Gilles Peskinec5487a82018-12-03 18:08:14 +01002996psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002997 psa_algorithm_t alg,
2998 const uint8_t *input,
2999 size_t input_length,
3000 const uint8_t *salt,
3001 size_t salt_length,
3002 uint8_t *output,
3003 size_t output_size,
3004 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003005{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003006 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003007 psa_status_t status;
3008
Darryl Green5cc689a2018-07-24 15:34:10 +01003009 (void) input;
3010 (void) input_length;
3011 (void) salt;
3012 (void) output;
3013 (void) output_size;
3014
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003015 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003016
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003017 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3018 return( PSA_ERROR_INVALID_ARGUMENT );
3019
Gilles Peskinec5487a82018-12-03 18:08:14 +01003020 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003021 if( status != PSA_SUCCESS )
3022 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003023 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
3024 return( PSA_ERROR_INVALID_ARGUMENT );
3025
3026#if defined(MBEDTLS_RSA_C)
3027 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
3028 {
3029 mbedtls_rsa_context *rsa = slot->data.rsa;
3030 int ret;
3031
Gilles Peskine630a18a2018-06-29 17:49:35 +02003032 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003033 return( PSA_ERROR_INVALID_ARGUMENT );
3034
3035#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003036 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003037 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003038 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3039 mbedtls_ctr_drbg_random,
3040 &global_data.ctr_drbg,
3041 MBEDTLS_RSA_PRIVATE,
3042 output_length,
3043 input,
3044 output,
3045 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003046 }
3047 else
3048#endif /* MBEDTLS_PKCS1_V15 */
3049#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003050 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003051 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003052 psa_rsa_oaep_set_padding_mode( alg, rsa );
3053 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3054 mbedtls_ctr_drbg_random,
3055 &global_data.ctr_drbg,
3056 MBEDTLS_RSA_PRIVATE,
3057 salt, salt_length,
3058 output_length,
3059 input,
3060 output,
3061 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003062 }
3063 else
3064#endif /* MBEDTLS_PKCS1_V21 */
3065 {
3066 return( PSA_ERROR_INVALID_ARGUMENT );
3067 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003068
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003069 return( mbedtls_to_psa_error( ret ) );
3070 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003071 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003072#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003073 {
3074 return( PSA_ERROR_NOT_SUPPORTED );
3075 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003076}
Gilles Peskine20035e32018-02-03 22:44:14 +01003077
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003078
3079
mohammad1603503973b2018-03-12 15:59:30 +02003080/****************************************************************/
3081/* Symmetric cryptography */
3082/****************************************************************/
3083
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003084/* Initialize the cipher operation structure. Once this function has been
3085 * called, psa_cipher_abort can run and will do the right thing. */
3086static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3087 psa_algorithm_t alg )
3088{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003089 if( ! PSA_ALG_IS_CIPHER( alg ) )
3090 {
3091 memset( operation, 0, sizeof( *operation ) );
3092 return( PSA_ERROR_INVALID_ARGUMENT );
3093 }
3094
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003095 operation->alg = alg;
3096 operation->key_set = 0;
3097 operation->iv_set = 0;
3098 operation->iv_required = 1;
3099 operation->iv_size = 0;
3100 operation->block_size = 0;
3101 mbedtls_cipher_init( &operation->ctx.cipher );
3102 return( PSA_SUCCESS );
3103}
3104
Gilles Peskinee553c652018-06-04 16:22:46 +02003105static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003106 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003107 psa_algorithm_t alg,
3108 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003109{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003110 int ret = 0;
3111 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003112 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003113 size_t key_bits;
3114 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003115 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3116 PSA_KEY_USAGE_ENCRYPT :
3117 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003118
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003119 /* A context must be freshly initialized before it can be set up. */
3120 if( operation->alg != 0 )
3121 {
3122 return( PSA_ERROR_BAD_STATE );
3123 }
3124
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003125 status = psa_cipher_init( operation, alg );
3126 if( status != PSA_SUCCESS )
3127 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003128
Gilles Peskinec5487a82018-12-03 18:08:14 +01003129 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003130 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003131 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003132 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003133
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003134 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003135 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003136 {
3137 status = PSA_ERROR_NOT_SUPPORTED;
3138 goto exit;
3139 }
mohammad1603503973b2018-03-12 15:59:30 +02003140
mohammad1603503973b2018-03-12 15:59:30 +02003141 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003142 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003143 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003144
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003145#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003146 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003147 {
3148 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3149 unsigned char keys[24];
3150 memcpy( keys, slot->data.raw.data, 16 );
3151 memcpy( keys + 16, slot->data.raw.data, 8 );
3152 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3153 keys,
3154 192, cipher_operation );
3155 }
3156 else
3157#endif
3158 {
3159 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3160 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003161 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003162 }
Moran Peker41deec42018-04-04 15:43:05 +03003163 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003164 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003165
mohammad16038481e742018-03-18 13:57:31 +02003166#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003167 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003168 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003169 case PSA_ALG_CBC_NO_PADDING:
3170 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3171 MBEDTLS_PADDING_NONE );
3172 break;
3173 case PSA_ALG_CBC_PKCS7:
3174 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3175 MBEDTLS_PADDING_PKCS7 );
3176 break;
3177 default:
3178 /* The algorithm doesn't involve padding. */
3179 ret = 0;
3180 break;
3181 }
3182 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003183 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003184#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3185
mohammad1603503973b2018-03-12 15:59:30 +02003186 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003187 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3188 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3189 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003190 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003191 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003192 }
mohammad1603503973b2018-03-12 15:59:30 +02003193
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003194exit:
3195 if( status == 0 )
3196 status = mbedtls_to_psa_error( ret );
3197 if( status != 0 )
3198 psa_cipher_abort( operation );
3199 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003200}
3201
Gilles Peskinefe119512018-07-08 21:39:34 +02003202psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003203 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003204 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003205{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003206 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003207}
3208
Gilles Peskinefe119512018-07-08 21:39:34 +02003209psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003210 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003211 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003212{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003213 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003214}
3215
Gilles Peskinefe119512018-07-08 21:39:34 +02003216psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3217 unsigned char *iv,
3218 size_t iv_size,
3219 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003220{
itayzafrir534bd7c2018-08-02 13:56:32 +03003221 psa_status_t status;
3222 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003223 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003224 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003225 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003226 }
Moran Peker41deec42018-04-04 15:43:05 +03003227 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003228 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003229 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003230 goto exit;
3231 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003232 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3233 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003234 if( ret != 0 )
3235 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003236 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003237 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003238 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003239
mohammad16038481e742018-03-18 13:57:31 +02003240 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003241 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003242
Moran Peker395db872018-05-31 14:07:14 +03003243exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003244 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003245 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003246 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003247}
3248
Gilles Peskinefe119512018-07-08 21:39:34 +02003249psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3250 const unsigned char *iv,
3251 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003252{
itayzafrir534bd7c2018-08-02 13:56:32 +03003253 psa_status_t status;
3254 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003255 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003256 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003257 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003258 }
Moran Pekera28258c2018-05-29 16:25:04 +03003259 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003260 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003261 status = PSA_ERROR_INVALID_ARGUMENT;
3262 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003263 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003264 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3265 status = mbedtls_to_psa_error( ret );
3266exit:
3267 if( status == PSA_SUCCESS )
3268 operation->iv_set = 1;
3269 else
mohammad1603503973b2018-03-12 15:59:30 +02003270 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003271 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003272}
3273
Gilles Peskinee553c652018-06-04 16:22:46 +02003274psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3275 const uint8_t *input,
3276 size_t input_length,
3277 unsigned char *output,
3278 size_t output_size,
3279 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003280{
itayzafrir534bd7c2018-08-02 13:56:32 +03003281 psa_status_t status;
3282 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003283 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003284
3285 if( operation->alg == 0 )
3286 {
3287 return( PSA_ERROR_BAD_STATE );
3288 }
3289
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003290 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003291 {
3292 /* Take the unprocessed partial block left over from previous
3293 * update calls, if any, plus the input to this call. Remove
3294 * the last partial block, if any. You get the data that will be
3295 * output in this call. */
3296 expected_output_size =
3297 ( operation->ctx.cipher.unprocessed_len + input_length )
3298 / operation->block_size * operation->block_size;
3299 }
3300 else
3301 {
3302 expected_output_size = input_length;
3303 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003304
Gilles Peskine89d789c2018-06-04 17:17:16 +02003305 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003306 {
3307 status = PSA_ERROR_BUFFER_TOO_SMALL;
3308 goto exit;
3309 }
mohammad160382759612018-03-12 18:16:40 +02003310
mohammad1603503973b2018-03-12 15:59:30 +02003311 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003312 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003313 status = mbedtls_to_psa_error( ret );
3314exit:
3315 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003316 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003317 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003318}
3319
Gilles Peskinee553c652018-06-04 16:22:46 +02003320psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3321 uint8_t *output,
3322 size_t output_size,
3323 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003324{
David Saadab4ecc272019-02-14 13:48:10 +02003325 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003326 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003327 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003328
mohammad1603503973b2018-03-12 15:59:30 +02003329 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003330 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003331 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003332 }
3333 if( operation->iv_required && ! operation->iv_set )
3334 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003335 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003336 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003337
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003338 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003339 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3340 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003341 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003342 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003343 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003344 }
3345
Janos Follath315b51c2018-07-09 16:04:51 +01003346 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3347 temp_output_buffer,
3348 output_length );
3349 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003350 {
Janos Follath315b51c2018-07-09 16:04:51 +01003351 status = mbedtls_to_psa_error( cipher_ret );
3352 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003353 }
Janos Follath315b51c2018-07-09 16:04:51 +01003354
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003355 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003356 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003357 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003358 memcpy( output, temp_output_buffer, *output_length );
3359 else
3360 {
Janos Follath315b51c2018-07-09 16:04:51 +01003361 status = PSA_ERROR_BUFFER_TOO_SMALL;
3362 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003363 }
mohammad1603503973b2018-03-12 15:59:30 +02003364
Gilles Peskine3f108122018-12-07 18:14:53 +01003365 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003366 status = psa_cipher_abort( operation );
3367
3368 return( status );
3369
3370error:
3371
3372 *output_length = 0;
3373
Gilles Peskine3f108122018-12-07 18:14:53 +01003374 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003375 (void) psa_cipher_abort( operation );
3376
3377 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003378}
3379
Gilles Peskinee553c652018-06-04 16:22:46 +02003380psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3381{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003382 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003383 {
3384 /* The object has (apparently) been initialized but it is not
3385 * in use. It's ok to call abort on such an object, and there's
3386 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003387 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003388 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003389
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003390 /* Sanity check (shouldn't happen: operation->alg should
3391 * always have been initialized to a valid value). */
3392 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3393 return( PSA_ERROR_BAD_STATE );
3394
mohammad1603503973b2018-03-12 15:59:30 +02003395 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003396
Moran Peker41deec42018-04-04 15:43:05 +03003397 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003398 operation->key_set = 0;
3399 operation->iv_set = 0;
3400 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003401 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003402 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003403
Moran Peker395db872018-05-31 14:07:14 +03003404 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003405}
3406
Gilles Peskinea0655c32018-04-30 17:06:50 +02003407
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003408
mohammad16038cc1cee2018-03-28 01:21:33 +03003409/****************************************************************/
3410/* Key Policy */
3411/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003412
mohammad160327010052018-07-03 13:16:15 +03003413#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003414void psa_key_policy_set_usage( psa_key_policy_t *policy,
3415 psa_key_usage_t usage,
3416 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003417{
mohammad16034eed7572018-03-28 05:14:59 -07003418 policy->usage = usage;
3419 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003420}
3421
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003422psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003423{
mohammad16036df908f2018-04-02 08:34:15 -07003424 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003425}
3426
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003427psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003428{
mohammad16036df908f2018-04-02 08:34:15 -07003429 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003430}
mohammad160327010052018-07-03 13:16:15 +03003431#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003432
Gilles Peskinec5487a82018-12-03 18:08:14 +01003433psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003434 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003435{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003436 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003437 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003438
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003439 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003440 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003441
Gilles Peskinec5487a82018-12-03 18:08:14 +01003442 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003443 if( status != PSA_SUCCESS )
3444 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003445
Gilles Peskine4747d192019-04-17 15:05:45 +02003446 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003447}
3448
Gilles Peskinec5487a82018-12-03 18:08:14 +01003449psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003450 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003451{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003452 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003453 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003454
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003455 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003456 return( PSA_ERROR_INVALID_ARGUMENT );
3457
Gilles Peskinec5487a82018-12-03 18:08:14 +01003458 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003459 if( status != PSA_SUCCESS )
3460 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003461
mohammad16036df908f2018-04-02 08:34:15 -07003462 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003463
3464 return( PSA_SUCCESS );
3465}
Gilles Peskine20035e32018-02-03 22:44:14 +01003466
Gilles Peskinea0655c32018-04-30 17:06:50 +02003467
3468
mohammad1603804cd712018-03-20 22:44:08 +02003469/****************************************************************/
3470/* Key Lifetime */
3471/****************************************************************/
3472
Gilles Peskine87a5e562019-04-17 12:28:25 +02003473psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003474 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003475{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003476 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003477 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003478
Gilles Peskinec5487a82018-12-03 18:08:14 +01003479 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003480 if( status != PSA_SUCCESS )
3481 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003482
mohammad1603804cd712018-03-20 22:44:08 +02003483 *lifetime = slot->lifetime;
3484
3485 return( PSA_SUCCESS );
3486}
3487
Gilles Peskine20035e32018-02-03 22:44:14 +01003488
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003489
mohammad16035955c982018-04-26 00:53:03 +03003490/****************************************************************/
3491/* AEAD */
3492/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003493
Gilles Peskineedf9a652018-08-17 18:11:56 +02003494typedef struct
3495{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003496 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003497 const mbedtls_cipher_info_t *cipher_info;
3498 union
3499 {
3500#if defined(MBEDTLS_CCM_C)
3501 mbedtls_ccm_context ccm;
3502#endif /* MBEDTLS_CCM_C */
3503#if defined(MBEDTLS_GCM_C)
3504 mbedtls_gcm_context gcm;
3505#endif /* MBEDTLS_GCM_C */
3506 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003507 psa_algorithm_t core_alg;
3508 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003509 uint8_t tag_length;
3510} aead_operation_t;
3511
Gilles Peskine30a9e412019-01-14 18:36:12 +01003512static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003513{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003514 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003515 {
3516#if defined(MBEDTLS_CCM_C)
3517 case PSA_ALG_CCM:
3518 mbedtls_ccm_free( &operation->ctx.ccm );
3519 break;
3520#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003521#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003522 case PSA_ALG_GCM:
3523 mbedtls_gcm_free( &operation->ctx.gcm );
3524 break;
3525#endif /* MBEDTLS_GCM_C */
3526 }
3527}
3528
3529static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003530 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003531 psa_key_usage_t usage,
3532 psa_algorithm_t alg )
3533{
3534 psa_status_t status;
3535 size_t key_bits;
3536 mbedtls_cipher_id_t cipher_id;
3537
Gilles Peskinec5487a82018-12-03 18:08:14 +01003538 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003539 if( status != PSA_SUCCESS )
3540 return( status );
3541
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003542 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003543
3544 operation->cipher_info =
3545 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3546 &cipher_id );
3547 if( operation->cipher_info == NULL )
3548 return( PSA_ERROR_NOT_SUPPORTED );
3549
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003550 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003551 {
3552#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003553 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3554 operation->core_alg = PSA_ALG_CCM;
3555 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003556 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3557 return( PSA_ERROR_INVALID_ARGUMENT );
3558 mbedtls_ccm_init( &operation->ctx.ccm );
3559 status = mbedtls_to_psa_error(
3560 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3561 operation->slot->data.raw.data,
3562 (unsigned int) key_bits ) );
3563 if( status != 0 )
3564 goto cleanup;
3565 break;
3566#endif /* MBEDTLS_CCM_C */
3567
3568#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003569 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3570 operation->core_alg = PSA_ALG_GCM;
3571 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003572 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3573 return( PSA_ERROR_INVALID_ARGUMENT );
3574 mbedtls_gcm_init( &operation->ctx.gcm );
3575 status = mbedtls_to_psa_error(
3576 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3577 operation->slot->data.raw.data,
3578 (unsigned int) key_bits ) );
3579 break;
3580#endif /* MBEDTLS_GCM_C */
3581
3582 default:
3583 return( PSA_ERROR_NOT_SUPPORTED );
3584 }
3585
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003586 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3587 {
3588 status = PSA_ERROR_INVALID_ARGUMENT;
3589 goto cleanup;
3590 }
3591 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3592 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3593 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3594 * In both cases, mbedtls_xxx will validate the tag length below. */
3595
Gilles Peskineedf9a652018-08-17 18:11:56 +02003596 return( PSA_SUCCESS );
3597
3598cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003599 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003600 return( status );
3601}
3602
Gilles Peskinec5487a82018-12-03 18:08:14 +01003603psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003604 psa_algorithm_t alg,
3605 const uint8_t *nonce,
3606 size_t nonce_length,
3607 const uint8_t *additional_data,
3608 size_t additional_data_length,
3609 const uint8_t *plaintext,
3610 size_t plaintext_length,
3611 uint8_t *ciphertext,
3612 size_t ciphertext_size,
3613 size_t *ciphertext_length )
3614{
mohammad16035955c982018-04-26 00:53:03 +03003615 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003616 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003617 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003618
mohammad1603f08a5502018-06-03 15:05:47 +03003619 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003620
Gilles Peskinec5487a82018-12-03 18:08:14 +01003621 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003622 if( status != PSA_SUCCESS )
3623 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003624
Gilles Peskineedf9a652018-08-17 18:11:56 +02003625 /* For all currently supported modes, the tag is at the end of the
3626 * ciphertext. */
3627 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3628 {
3629 status = PSA_ERROR_BUFFER_TOO_SMALL;
3630 goto exit;
3631 }
3632 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003633
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003634#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003635 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003636 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003637 status = mbedtls_to_psa_error(
3638 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3639 MBEDTLS_GCM_ENCRYPT,
3640 plaintext_length,
3641 nonce, nonce_length,
3642 additional_data, additional_data_length,
3643 plaintext, ciphertext,
3644 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003645 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003646 else
3647#endif /* MBEDTLS_GCM_C */
3648#if defined(MBEDTLS_CCM_C)
3649 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003650 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003651 status = mbedtls_to_psa_error(
3652 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3653 plaintext_length,
3654 nonce, nonce_length,
3655 additional_data,
3656 additional_data_length,
3657 plaintext, ciphertext,
3658 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003659 }
mohammad16035c8845f2018-05-09 05:40:09 -07003660 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003661#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003662 {
mohammad1603554faad2018-06-03 15:07:38 +03003663 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003664 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003665
Gilles Peskineedf9a652018-08-17 18:11:56 +02003666 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3667 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003668
Gilles Peskineedf9a652018-08-17 18:11:56 +02003669exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003670 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003671 if( status == PSA_SUCCESS )
3672 *ciphertext_length = plaintext_length + operation.tag_length;
3673 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003674}
3675
Gilles Peskineee652a32018-06-01 19:23:52 +02003676/* Locate the tag in a ciphertext buffer containing the encrypted data
3677 * followed by the tag. Return the length of the part preceding the tag in
3678 * *plaintext_length. This is the size of the plaintext in modes where
3679 * the encrypted data has the same size as the plaintext, such as
3680 * CCM and GCM. */
3681static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3682 const uint8_t *ciphertext,
3683 size_t ciphertext_length,
3684 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003685 const uint8_t **p_tag )
3686{
3687 size_t payload_length;
3688 if( tag_length > ciphertext_length )
3689 return( PSA_ERROR_INVALID_ARGUMENT );
3690 payload_length = ciphertext_length - tag_length;
3691 if( payload_length > plaintext_size )
3692 return( PSA_ERROR_BUFFER_TOO_SMALL );
3693 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003694 return( PSA_SUCCESS );
3695}
3696
Gilles Peskinec5487a82018-12-03 18:08:14 +01003697psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003698 psa_algorithm_t alg,
3699 const uint8_t *nonce,
3700 size_t nonce_length,
3701 const uint8_t *additional_data,
3702 size_t additional_data_length,
3703 const uint8_t *ciphertext,
3704 size_t ciphertext_length,
3705 uint8_t *plaintext,
3706 size_t plaintext_size,
3707 size_t *plaintext_length )
3708{
mohammad16035955c982018-04-26 00:53:03 +03003709 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003710 aead_operation_t operation;
3711 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003712
Gilles Peskineee652a32018-06-01 19:23:52 +02003713 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003714
Gilles Peskinec5487a82018-12-03 18:08:14 +01003715 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003716 if( status != PSA_SUCCESS )
3717 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003718
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003719#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003720 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003721 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003722 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003723 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003724 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003725 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003726 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003727
Gilles Peskineedf9a652018-08-17 18:11:56 +02003728 status = mbedtls_to_psa_error(
3729 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3730 ciphertext_length - operation.tag_length,
3731 nonce, nonce_length,
3732 additional_data,
3733 additional_data_length,
3734 tag, operation.tag_length,
3735 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003736 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003737 else
3738#endif /* MBEDTLS_GCM_C */
3739#if defined(MBEDTLS_CCM_C)
3740 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003741 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003742 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003743 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003744 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003745 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003746 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003747
Gilles Peskineedf9a652018-08-17 18:11:56 +02003748 status = mbedtls_to_psa_error(
3749 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3750 ciphertext_length - operation.tag_length,
3751 nonce, nonce_length,
3752 additional_data,
3753 additional_data_length,
3754 ciphertext, plaintext,
3755 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003756 }
mohammad160339574652018-06-01 04:39:53 -07003757 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003758#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003759 {
mohammad1603554faad2018-06-03 15:07:38 +03003760 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003761 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003762
Gilles Peskineedf9a652018-08-17 18:11:56 +02003763 if( status != PSA_SUCCESS && plaintext_size != 0 )
3764 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003765
Gilles Peskineedf9a652018-08-17 18:11:56 +02003766exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003767 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003768 if( status == PSA_SUCCESS )
3769 *plaintext_length = ciphertext_length - operation.tag_length;
3770 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003771}
3772
Gilles Peskinea0655c32018-04-30 17:06:50 +02003773
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003774
Gilles Peskine20035e32018-02-03 22:44:14 +01003775/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003776/* Generators */
3777/****************************************************************/
3778
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003779#define HKDF_STATE_INIT 0 /* no input yet */
3780#define HKDF_STATE_STARTED 1 /* got salt */
3781#define HKDF_STATE_KEYED 2 /* got key */
3782#define HKDF_STATE_OUTPUT 3 /* output started */
3783
Gilles Peskine969c5d62019-01-16 15:53:06 +01003784static psa_algorithm_t psa_generator_get_kdf_alg(
3785 const psa_crypto_generator_t *generator )
3786{
3787 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3788 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3789 else
3790 return( generator->alg );
3791}
3792
3793
Gilles Peskineeab56e42018-07-12 17:12:33 +02003794psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3795{
3796 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003797 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3798 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003799 {
3800 /* The object has (apparently) been initialized but it is not
3801 * in use. It's ok to call abort on such an object, and there's
3802 * nothing to do. */
3803 }
3804 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003805 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003806 {
3807 if( generator->ctx.buffer.data != NULL )
3808 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003809 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003810 generator->ctx.buffer.size );
3811 mbedtls_free( generator->ctx.buffer.data );
3812 }
3813 }
3814 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003815#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003816 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003817 {
3818 mbedtls_free( generator->ctx.hkdf.info );
3819 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3820 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003821 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00003822 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003823 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003824 {
3825 if( generator->ctx.tls12_prf.key != NULL )
3826 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003827 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003828 generator->ctx.tls12_prf.key_len );
3829 mbedtls_free( generator->ctx.tls12_prf.key );
3830 }
Hanno Becker580fba12018-11-13 20:50:45 +00003831
3832 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3833 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003834 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003835 generator->ctx.tls12_prf.Ai_with_seed_len );
3836 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3837 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003838 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003839 else
3840#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003841 {
3842 status = PSA_ERROR_BAD_STATE;
3843 }
3844 memset( generator, 0, sizeof( *generator ) );
3845 return( status );
3846}
3847
Gilles Peskineeab56e42018-07-12 17:12:33 +02003848psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3849 size_t *capacity)
3850{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003851 if( generator->alg == 0 )
3852 {
3853 /* This is a blank generator. */
3854 return PSA_ERROR_BAD_STATE;
3855 }
3856
Gilles Peskineeab56e42018-07-12 17:12:33 +02003857 *capacity = generator->capacity;
3858 return( PSA_SUCCESS );
3859}
3860
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003861psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
3862 size_t capacity )
3863{
3864 if( generator->alg == 0 )
3865 return( PSA_ERROR_BAD_STATE );
3866 if( capacity > generator->capacity )
3867 return( PSA_ERROR_INVALID_ARGUMENT );
3868 generator->capacity = capacity;
3869 return( PSA_SUCCESS );
3870}
3871
Gilles Peskinebef7f142018-07-12 17:22:21 +02003872#if defined(MBEDTLS_MD_C)
3873/* Read some bytes from an HKDF-based generator. This performs a chunk
3874 * of the expand phase of the HKDF algorithm. */
3875static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3876 psa_algorithm_t hash_alg,
3877 uint8_t *output,
3878 size_t output_length )
3879{
3880 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3881 psa_status_t status;
3882
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003883 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3884 return( PSA_ERROR_BAD_STATE );
3885 hkdf->state = HKDF_STATE_OUTPUT;
3886
Gilles Peskinebef7f142018-07-12 17:22:21 +02003887 while( output_length != 0 )
3888 {
3889 /* Copy what remains of the current block */
3890 uint8_t n = hash_length - hkdf->offset_in_block;
3891 if( n > output_length )
3892 n = (uint8_t) output_length;
3893 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3894 output += n;
3895 output_length -= n;
3896 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003897 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003898 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003899 /* We can't be wanting more output after block 0xff, otherwise
3900 * the capacity check in psa_generator_read() would have
3901 * prevented this call. It could happen only if the generator
3902 * object was corrupted or if this function is called directly
3903 * inside the library. */
3904 if( hkdf->block_number == 0xff )
3905 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003906
3907 /* We need a new block */
3908 ++hkdf->block_number;
3909 hkdf->offset_in_block = 0;
3910 status = psa_hmac_setup_internal( &hkdf->hmac,
3911 hkdf->prk, hash_length,
3912 hash_alg );
3913 if( status != PSA_SUCCESS )
3914 return( status );
3915 if( hkdf->block_number != 1 )
3916 {
3917 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3918 hkdf->output_block,
3919 hash_length );
3920 if( status != PSA_SUCCESS )
3921 return( status );
3922 }
3923 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3924 hkdf->info,
3925 hkdf->info_length );
3926 if( status != PSA_SUCCESS )
3927 return( status );
3928 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3929 &hkdf->block_number, 1 );
3930 if( status != PSA_SUCCESS )
3931 return( status );
3932 status = psa_hmac_finish_internal( &hkdf->hmac,
3933 hkdf->output_block,
3934 sizeof( hkdf->output_block ) );
3935 if( status != PSA_SUCCESS )
3936 return( status );
3937 }
3938
3939 return( PSA_SUCCESS );
3940}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003941
3942static psa_status_t psa_generator_tls12_prf_generate_next_block(
3943 psa_tls12_prf_generator_t *tls12_prf,
3944 psa_algorithm_t alg )
3945{
3946 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3947 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3948 psa_hmac_internal_data hmac;
3949 psa_status_t status, cleanup_status;
3950
Hanno Becker3b339e22018-11-13 20:56:14 +00003951 unsigned char *Ai;
3952 size_t Ai_len;
3953
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003954 /* We can't be wanting more output after block 0xff, otherwise
3955 * the capacity check in psa_generator_read() would have
3956 * prevented this call. It could happen only if the generator
3957 * object was corrupted or if this function is called directly
3958 * inside the library. */
3959 if( tls12_prf->block_number == 0xff )
3960 return( PSA_ERROR_BAD_STATE );
3961
3962 /* We need a new block */
3963 ++tls12_prf->block_number;
3964 tls12_prf->offset_in_block = 0;
3965
3966 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3967 *
3968 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3969 *
3970 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3971 * HMAC_hash(secret, A(2) + seed) +
3972 * HMAC_hash(secret, A(3) + seed) + ...
3973 *
3974 * A(0) = seed
3975 * A(i) = HMAC_hash( secret, A(i-1) )
3976 *
3977 * The `psa_tls12_prf_generator` structures saves the block
3978 * `HMAC_hash(secret, A(i) + seed)` from which the output
3979 * is currently extracted as `output_block`, while
3980 * `A(i) + seed` is stored in `Ai_with_seed`.
3981 *
3982 * Generating a new block means recalculating `Ai_with_seed`
3983 * from the A(i)-part of it, and afterwards recalculating
3984 * `output_block`.
3985 *
3986 * A(0) is computed at setup time.
3987 *
3988 */
3989
3990 psa_hmac_init_internal( &hmac );
3991
3992 /* We must distinguish the calculation of A(1) from those
3993 * of A(2) and higher, because A(0)=seed has a different
3994 * length than the other A(i). */
3995 if( tls12_prf->block_number == 1 )
3996 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003997 Ai = tls12_prf->Ai_with_seed + hash_length;
3998 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003999 }
4000 else
4001 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004002 Ai = tls12_prf->Ai_with_seed;
4003 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004004 }
4005
Hanno Becker3b339e22018-11-13 20:56:14 +00004006 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4007 status = psa_hmac_setup_internal( &hmac,
4008 tls12_prf->key,
4009 tls12_prf->key_len,
4010 hash_alg );
4011 if( status != PSA_SUCCESS )
4012 goto cleanup;
4013
4014 status = psa_hash_update( &hmac.hash_ctx,
4015 Ai, Ai_len );
4016 if( status != PSA_SUCCESS )
4017 goto cleanup;
4018
4019 status = psa_hmac_finish_internal( &hmac,
4020 tls12_prf->Ai_with_seed,
4021 hash_length );
4022 if( status != PSA_SUCCESS )
4023 goto cleanup;
4024
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004025 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4026 status = psa_hmac_setup_internal( &hmac,
4027 tls12_prf->key,
4028 tls12_prf->key_len,
4029 hash_alg );
4030 if( status != PSA_SUCCESS )
4031 goto cleanup;
4032
4033 status = psa_hash_update( &hmac.hash_ctx,
4034 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004035 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004036 if( status != PSA_SUCCESS )
4037 goto cleanup;
4038
4039 status = psa_hmac_finish_internal( &hmac,
4040 tls12_prf->output_block,
4041 hash_length );
4042 if( status != PSA_SUCCESS )
4043 goto cleanup;
4044
4045cleanup:
4046
4047 cleanup_status = psa_hmac_abort_internal( &hmac );
4048 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4049 status = cleanup_status;
4050
4051 return( status );
4052}
4053
4054/* Read some bytes from an TLS-1.2-PRF-based generator.
4055 * See Section 5 of RFC 5246. */
4056static psa_status_t psa_generator_tls12_prf_read(
4057 psa_tls12_prf_generator_t *tls12_prf,
4058 psa_algorithm_t alg,
4059 uint8_t *output,
4060 size_t output_length )
4061{
4062 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4063 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4064 psa_status_t status;
4065
4066 while( output_length != 0 )
4067 {
4068 /* Copy what remains of the current block */
4069 uint8_t n = hash_length - tls12_prf->offset_in_block;
4070
4071 /* Check if we have fully processed the current block. */
4072 if( n == 0 )
4073 {
4074 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
4075 alg );
4076 if( status != PSA_SUCCESS )
4077 return( status );
4078
4079 continue;
4080 }
4081
4082 if( n > output_length )
4083 n = (uint8_t) output_length;
4084 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4085 n );
4086 output += n;
4087 output_length -= n;
4088 tls12_prf->offset_in_block += n;
4089 }
4090
4091 return( PSA_SUCCESS );
4092}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004093#endif /* MBEDTLS_MD_C */
4094
Gilles Peskineeab56e42018-07-12 17:12:33 +02004095psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
4096 uint8_t *output,
4097 size_t output_length )
4098{
4099 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004100 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004101
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004102 if( generator->alg == 0 )
4103 {
4104 /* This is a blank generator. */
4105 return PSA_ERROR_BAD_STATE;
4106 }
4107
Gilles Peskineeab56e42018-07-12 17:12:33 +02004108 if( output_length > generator->capacity )
4109 {
4110 generator->capacity = 0;
4111 /* Go through the error path to wipe all confidential data now
4112 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004113 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004114 goto exit;
4115 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004116 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004117 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004118 /* Edge case: this is a finished generator, and 0 bytes
4119 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004120 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4121 * INSUFFICIENT_CAPACITY, which is right for a finished
4122 * generator, for consistency with the case when
4123 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004124 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004125 }
4126 generator->capacity -= output_length;
4127
Gilles Peskine969c5d62019-01-16 15:53:06 +01004128 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004129 {
Gilles Peskine211a4362018-10-25 22:22:31 +02004130 /* Initially, the capacity of a selection generator is always
4131 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
4132 * abbreviated in this comment as `size`. When the remaining
4133 * capacity is `c`, the next bytes to serve start `c` bytes
4134 * from the end of the buffer, i.e. `size - c` from the
4135 * beginning of the buffer. Since `generator->capacity` was just
4136 * decremented above, we need to serve the bytes from
4137 * `size - generator->capacity - output_length` to
4138 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004139 size_t offset =
4140 generator->ctx.buffer.size - generator->capacity - output_length;
4141 memcpy( output, generator->ctx.buffer.data + offset, output_length );
4142 status = PSA_SUCCESS;
4143 }
4144 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004145#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004146 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004147 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004148 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004149 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
4150 output, output_length );
4151 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004152 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4153 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004154 {
4155 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004156 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004157 output_length );
4158 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004159 else
4160#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004161 {
4162 return( PSA_ERROR_BAD_STATE );
4163 }
4164
4165exit:
4166 if( status != PSA_SUCCESS )
4167 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004168 /* Preserve the algorithm upon errors, but clear all sensitive state.
4169 * This allows us to differentiate between exhausted generators and
4170 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
4171 * generators. */
4172 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004173 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004174 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004175 memset( output, '!', output_length );
4176 }
4177 return( status );
4178}
4179
Gilles Peskine08542d82018-07-19 17:05:42 +02004180#if defined(MBEDTLS_DES_C)
4181static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4182{
4183 if( data_size >= 8 )
4184 mbedtls_des_key_set_parity( data );
4185 if( data_size >= 16 )
4186 mbedtls_des_key_set_parity( data + 8 );
4187 if( data_size >= 24 )
4188 mbedtls_des_key_set_parity( data + 16 );
4189}
4190#endif /* MBEDTLS_DES_C */
4191
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004192static psa_status_t psa_generator_import_key_internal(
4193 psa_key_slot_t *slot,
4194 size_t bits,
4195 psa_crypto_generator_t *generator )
4196{
4197 uint8_t *data = NULL;
4198 size_t bytes = PSA_BITS_TO_BYTES( bits );
4199 psa_status_t status;
4200
4201 if( ! key_type_is_raw_bytes( slot->type ) )
4202 return( PSA_ERROR_INVALID_ARGUMENT );
4203 if( bits % 8 != 0 )
4204 return( PSA_ERROR_INVALID_ARGUMENT );
4205 data = mbedtls_calloc( 1, bytes );
4206 if( data == NULL )
4207 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4208
4209 status = psa_generator_read( generator, data, bytes );
4210 if( status != PSA_SUCCESS )
4211 goto exit;
4212#if defined(MBEDTLS_DES_C)
4213 if( slot->type == PSA_KEY_TYPE_DES )
4214 psa_des_set_key_parity( data, bytes );
4215#endif /* MBEDTLS_DES_C */
4216 status = psa_import_key_into_slot( slot, data, bytes );
4217
4218exit:
4219 mbedtls_free( data );
4220 return( status );
4221}
4222
4223psa_status_t psa_generator_import_key( const psa_key_attributes_t *attributes,
4224 psa_key_handle_t *handle,
4225 size_t bits,
4226 psa_crypto_generator_t *generator )
4227{
4228 psa_status_t status;
4229 psa_key_slot_t *slot = NULL;
4230 status = psa_start_key_creation( attributes, handle, &slot );
4231 if( status == PSA_SUCCESS )
4232 {
4233 status = psa_generator_import_key_internal( slot, bits, generator );
4234 }
4235 if( status == PSA_SUCCESS )
4236 status = psa_finish_key_creation( slot );
4237 if( status != PSA_SUCCESS )
4238 {
4239 psa_fail_key_creation( slot );
4240 *handle = 0;
4241 }
4242 return( status );
4243}
4244
Gilles Peskine87a5e562019-04-17 12:28:25 +02004245psa_status_t psa_generator_import_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004246 psa_key_type_t type,
4247 size_t bits,
4248 psa_crypto_generator_t *generator )
4249{
4250 uint8_t *data = NULL;
4251 size_t bytes = PSA_BITS_TO_BYTES( bits );
4252 psa_status_t status;
4253
4254 if( ! key_type_is_raw_bytes( type ) )
4255 return( PSA_ERROR_INVALID_ARGUMENT );
4256 if( bits % 8 != 0 )
4257 return( PSA_ERROR_INVALID_ARGUMENT );
4258 data = mbedtls_calloc( 1, bytes );
4259 if( data == NULL )
4260 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4261
4262 status = psa_generator_read( generator, data, bytes );
4263 if( status != PSA_SUCCESS )
4264 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004265#if defined(MBEDTLS_DES_C)
4266 if( type == PSA_KEY_TYPE_DES )
4267 psa_des_set_key_parity( data, bytes );
4268#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004269 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004270
4271exit:
4272 mbedtls_free( data );
4273 return( status );
4274}
4275
4276
4277
4278/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004279/* Key derivation */
4280/****************************************************************/
4281
Gilles Peskinea05219c2018-11-16 16:02:56 +01004282#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004283/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004284 * of the HKDF algorithm.
4285 *
4286 * Note that if this function fails, you must call psa_generator_abort()
4287 * to potentially free embedded data structures and wipe confidential data.
4288 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004289static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004290 const uint8_t *secret,
4291 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004292 psa_algorithm_t hash_alg,
4293 const uint8_t *salt,
4294 size_t salt_length,
4295 const uint8_t *label,
4296 size_t label_length )
4297{
4298 psa_status_t status;
4299 status = psa_hmac_setup_internal( &hkdf->hmac,
4300 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004301 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004302 if( status != PSA_SUCCESS )
4303 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004304 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004305 if( status != PSA_SUCCESS )
4306 return( status );
4307 status = psa_hmac_finish_internal( &hkdf->hmac,
4308 hkdf->prk,
4309 sizeof( hkdf->prk ) );
4310 if( status != PSA_SUCCESS )
4311 return( status );
4312 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4313 hkdf->block_number = 0;
4314 hkdf->info_length = label_length;
4315 if( label_length != 0 )
4316 {
4317 hkdf->info = mbedtls_calloc( 1, label_length );
4318 if( hkdf->info == NULL )
4319 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4320 memcpy( hkdf->info, label, label_length );
4321 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004322 hkdf->state = HKDF_STATE_KEYED;
4323 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004324 return( PSA_SUCCESS );
4325}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004326#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004327
Gilles Peskinea05219c2018-11-16 16:02:56 +01004328#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004329/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4330 *
4331 * Note that if this function fails, you must call psa_generator_abort()
4332 * to potentially free embedded data structures and wipe confidential data.
4333 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004334static psa_status_t psa_generator_tls12_prf_setup(
4335 psa_tls12_prf_generator_t *tls12_prf,
4336 const unsigned char *key,
4337 size_t key_len,
4338 psa_algorithm_t hash_alg,
4339 const uint8_t *salt,
4340 size_t salt_length,
4341 const uint8_t *label,
4342 size_t label_length )
4343{
4344 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004345 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4346 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004347
4348 tls12_prf->key = mbedtls_calloc( 1, key_len );
4349 if( tls12_prf->key == NULL )
4350 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4351 tls12_prf->key_len = key_len;
4352 memcpy( tls12_prf->key, key, key_len );
4353
Hanno Becker580fba12018-11-13 20:50:45 +00004354 overflow = ( salt_length + label_length < salt_length ) ||
4355 ( salt_length + label_length + hash_length < hash_length );
4356 if( overflow )
4357 return( PSA_ERROR_INVALID_ARGUMENT );
4358
4359 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4360 if( tls12_prf->Ai_with_seed == NULL )
4361 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4362 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4363
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004364 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4365 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004366 if( label_length != 0 )
4367 {
4368 memcpy( tls12_prf->Ai_with_seed + hash_length,
4369 label, label_length );
4370 }
4371
4372 if( salt_length != 0 )
4373 {
4374 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4375 salt, salt_length );
4376 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004377
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004378 /* The first block gets generated when
4379 * psa_generator_read() is called. */
4380 tls12_prf->block_number = 0;
4381 tls12_prf->offset_in_block = hash_length;
4382
4383 return( PSA_SUCCESS );
4384}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004385
4386/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4387static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4388 psa_tls12_prf_generator_t *tls12_prf,
4389 const unsigned char *psk,
4390 size_t psk_len,
4391 psa_algorithm_t hash_alg,
4392 const uint8_t *salt,
4393 size_t salt_length,
4394 const uint8_t *label,
4395 size_t label_length )
4396{
4397 psa_status_t status;
4398 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4399
4400 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4401 return( PSA_ERROR_INVALID_ARGUMENT );
4402
4403 /* Quoting RFC 4279, Section 2:
4404 *
4405 * The premaster secret is formed as follows: if the PSK is N octets
4406 * long, concatenate a uint16 with the value N, N zero octets, a second
4407 * uint16 with the value N, and the PSK itself.
4408 */
4409
4410 pms[0] = ( psk_len >> 8 ) & 0xff;
4411 pms[1] = ( psk_len >> 0 ) & 0xff;
4412 memset( pms + 2, 0, psk_len );
4413 pms[2 + psk_len + 0] = pms[0];
4414 pms[2 + psk_len + 1] = pms[1];
4415 memcpy( pms + 4 + psk_len, psk, psk_len );
4416
4417 status = psa_generator_tls12_prf_setup( tls12_prf,
4418 pms, 4 + 2 * psk_len,
4419 hash_alg,
4420 salt, salt_length,
4421 label, label_length );
4422
Gilles Peskine3f108122018-12-07 18:14:53 +01004423 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004424 return( status );
4425}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004426#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004427
Gilles Peskine346797d2018-11-16 16:05:06 +01004428/* Note that if this function fails, you must call psa_generator_abort()
4429 * to potentially free embedded data structures and wipe confidential data.
4430 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004431static psa_status_t psa_key_derivation_internal(
4432 psa_crypto_generator_t *generator,
4433 const uint8_t *secret, size_t secret_length,
4434 psa_algorithm_t alg,
4435 const uint8_t *salt, size_t salt_length,
4436 const uint8_t *label, size_t label_length,
4437 size_t capacity )
4438{
4439 psa_status_t status;
4440 size_t max_capacity;
4441
4442 /* Set generator->alg even on failure so that abort knows what to do. */
4443 generator->alg = alg;
4444
Gilles Peskine751d9652018-09-18 12:05:44 +02004445 if( alg == PSA_ALG_SELECT_RAW )
4446 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004447 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004448 if( salt_length != 0 )
4449 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004450 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004451 if( label_length != 0 )
4452 return( PSA_ERROR_INVALID_ARGUMENT );
4453 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4454 if( generator->ctx.buffer.data == NULL )
4455 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4456 memcpy( generator->ctx.buffer.data, secret, secret_length );
4457 generator->ctx.buffer.size = secret_length;
4458 max_capacity = secret_length;
4459 status = PSA_SUCCESS;
4460 }
4461 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004462#if defined(MBEDTLS_MD_C)
4463 if( PSA_ALG_IS_HKDF( alg ) )
4464 {
4465 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4466 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4467 if( hash_size == 0 )
4468 return( PSA_ERROR_NOT_SUPPORTED );
4469 max_capacity = 255 * hash_size;
4470 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4471 secret, secret_length,
4472 hash_alg,
4473 salt, salt_length,
4474 label, label_length );
4475 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004476 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4477 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4478 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004479 {
4480 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4481 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4482
4483 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4484 if( hash_alg != PSA_ALG_SHA_256 &&
4485 hash_alg != PSA_ALG_SHA_384 )
4486 {
4487 return( PSA_ERROR_NOT_SUPPORTED );
4488 }
4489
4490 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004491
4492 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4493 {
4494 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4495 secret, secret_length,
4496 hash_alg, salt, salt_length,
4497 label, label_length );
4498 }
4499 else
4500 {
4501 status = psa_generator_tls12_psk_to_ms_setup(
4502 &generator->ctx.tls12_prf,
4503 secret, secret_length,
4504 hash_alg, salt, salt_length,
4505 label, label_length );
4506 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004507 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004508 else
4509#endif
4510 {
4511 return( PSA_ERROR_NOT_SUPPORTED );
4512 }
4513
4514 if( status != PSA_SUCCESS )
4515 return( status );
4516
4517 if( capacity <= max_capacity )
4518 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004519 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4520 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004521 else
4522 return( PSA_ERROR_INVALID_ARGUMENT );
4523
4524 return( PSA_SUCCESS );
4525}
4526
Gilles Peskineea0fb492018-07-12 17:17:20 +02004527psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004528 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004529 psa_algorithm_t alg,
4530 const uint8_t *salt,
4531 size_t salt_length,
4532 const uint8_t *label,
4533 size_t label_length,
4534 size_t capacity )
4535{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004536 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004537 psa_status_t status;
4538
4539 if( generator->alg != 0 )
4540 return( PSA_ERROR_BAD_STATE );
4541
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004542 /* Make sure that alg is a key derivation algorithm. This prevents
4543 * key selection algorithms, which psa_key_derivation_internal
4544 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004545 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4546 return( PSA_ERROR_INVALID_ARGUMENT );
4547
Gilles Peskinec5487a82018-12-03 18:08:14 +01004548 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004549 if( status != PSA_SUCCESS )
4550 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004551
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004552 if( slot->type != PSA_KEY_TYPE_DERIVE )
4553 return( PSA_ERROR_INVALID_ARGUMENT );
4554
4555 status = psa_key_derivation_internal( generator,
4556 slot->data.raw.data,
4557 slot->data.raw.bytes,
4558 alg,
4559 salt, salt_length,
4560 label, label_length,
4561 capacity );
4562 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004563 psa_generator_abort( generator );
4564 return( status );
4565}
4566
Gilles Peskine969c5d62019-01-16 15:53:06 +01004567static psa_status_t psa_key_derivation_setup_kdf(
4568 psa_crypto_generator_t *generator,
4569 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004570{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004571 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004572#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004573 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4574 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4575 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004576 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004577 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004578 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4579 if( hash_size == 0 )
4580 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004581 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4582 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004583 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004584 {
4585 return( PSA_ERROR_NOT_SUPPORTED );
4586 }
4587 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004588 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004589 }
4590#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004591 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004592 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004593}
4594
4595psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4596 psa_algorithm_t alg )
4597{
4598 psa_status_t status;
4599
4600 if( generator->alg != 0 )
4601 return( PSA_ERROR_BAD_STATE );
4602
Gilles Peskine6843c292019-01-18 16:44:49 +01004603 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4604 return( PSA_ERROR_INVALID_ARGUMENT );
4605 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004606 {
4607 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004608 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004609 }
4610 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4611 {
4612 status = psa_key_derivation_setup_kdf( generator, alg );
4613 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004614 else
4615 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004616
4617 if( status == PSA_SUCCESS )
4618 generator->alg = alg;
4619 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004620}
4621
4622#if defined(MBEDTLS_MD_C)
4623static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4624 psa_algorithm_t hash_alg,
4625 psa_key_derivation_step_t step,
4626 const uint8_t *data,
4627 size_t data_length )
4628{
4629 psa_status_t status;
4630 switch( step )
4631 {
4632 case PSA_KDF_STEP_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004633 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004634 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004635 status = psa_hmac_setup_internal( &hkdf->hmac,
4636 data, data_length,
4637 hash_alg );
4638 if( status != PSA_SUCCESS )
4639 return( status );
4640 hkdf->state = HKDF_STATE_STARTED;
4641 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004642 case PSA_KDF_STEP_SECRET:
4643 /* If no salt was provided, use an empty salt. */
4644 if( hkdf->state == HKDF_STATE_INIT )
4645 {
4646 status = psa_hmac_setup_internal( &hkdf->hmac,
4647 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004648 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004649 if( status != PSA_SUCCESS )
4650 return( status );
4651 hkdf->state = HKDF_STATE_STARTED;
4652 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004653 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004654 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004655 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4656 data, data_length );
4657 if( status != PSA_SUCCESS )
4658 return( status );
4659 status = psa_hmac_finish_internal( &hkdf->hmac,
4660 hkdf->prk,
4661 sizeof( hkdf->prk ) );
4662 if( status != PSA_SUCCESS )
4663 return( status );
4664 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4665 hkdf->block_number = 0;
4666 hkdf->state = HKDF_STATE_KEYED;
4667 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004668 case PSA_KDF_STEP_INFO:
4669 if( hkdf->state == HKDF_STATE_OUTPUT )
4670 return( PSA_ERROR_BAD_STATE );
4671 if( hkdf->info_set )
4672 return( PSA_ERROR_BAD_STATE );
4673 hkdf->info_length = data_length;
4674 if( data_length != 0 )
4675 {
4676 hkdf->info = mbedtls_calloc( 1, data_length );
4677 if( hkdf->info == NULL )
4678 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4679 memcpy( hkdf->info, data, data_length );
4680 }
4681 hkdf->info_set = 1;
4682 return( PSA_SUCCESS );
4683 default:
4684 return( PSA_ERROR_INVALID_ARGUMENT );
4685 }
4686}
4687#endif /* MBEDTLS_MD_C */
4688
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004689static psa_status_t psa_key_derivation_input_raw(
4690 psa_crypto_generator_t *generator,
4691 psa_key_derivation_step_t step,
4692 const uint8_t *data,
4693 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004694{
4695 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004696 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004697
Gilles Peskine969c5d62019-01-16 15:53:06 +01004698 if( kdf_alg == PSA_ALG_SELECT_RAW )
4699 {
4700 if( generator->capacity != 0 )
4701 return( PSA_ERROR_INVALID_ARGUMENT );
4702 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4703 if( generator->ctx.buffer.data == NULL )
4704 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4705 memcpy( generator->ctx.buffer.data, data, data_length );
4706 generator->ctx.buffer.size = data_length;
4707 generator->capacity = data_length;
4708 status = PSA_SUCCESS;
4709 }
4710 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004711#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004712 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004713 {
4714 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004715 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004716 step, data, data_length );
4717 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004718 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004719#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004720#if defined(MBEDTLS_MD_C)
4721 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004722 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4723 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004724 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004725 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004726 status = PSA_ERROR_NOT_SUPPORTED;
4727 }
4728 else
4729#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004730 {
4731 /* This can't happen unless the generator object was not initialized */
4732 return( PSA_ERROR_BAD_STATE );
4733 }
4734
4735 if( status != PSA_SUCCESS )
4736 psa_generator_abort( generator );
4737 return( status );
4738}
4739
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004740psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4741 psa_key_derivation_step_t step,
4742 const uint8_t *data,
4743 size_t data_length )
4744{
4745 switch( step )
4746 {
4747 case PSA_KDF_STEP_LABEL:
4748 case PSA_KDF_STEP_SALT:
4749 case PSA_KDF_STEP_INFO:
4750 return( psa_key_derivation_input_raw( generator, step,
4751 data, data_length ) );
4752 default:
4753 return( PSA_ERROR_INVALID_ARGUMENT );
4754 }
4755}
4756
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004757psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4758 psa_key_derivation_step_t step,
4759 psa_key_handle_t handle )
4760{
4761 psa_key_slot_t *slot;
4762 psa_status_t status;
4763 status = psa_get_key_from_slot( handle, &slot,
4764 PSA_KEY_USAGE_DERIVE,
4765 generator->alg );
4766 if( status != PSA_SUCCESS )
4767 return( status );
4768 if( slot->type != PSA_KEY_TYPE_DERIVE )
4769 return( PSA_ERROR_INVALID_ARGUMENT );
4770 /* Don't allow a key to be used as an input that is usually public.
4771 * This is debatable. It's ok from a cryptographic perspective to
4772 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004773 * the material should be dedicated to a particular input step,
4774 * otherwise this may allow the key to be used in an unintended way
4775 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004776 if( step != PSA_KDF_STEP_SECRET )
4777 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004778 return( psa_key_derivation_input_raw( generator,
4779 step,
4780 slot->data.raw.data,
4781 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004782}
4783
Gilles Peskineea0fb492018-07-12 17:17:20 +02004784
4785
4786/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004787/* Key agreement */
4788/****************************************************************/
4789
Gilles Peskinea05219c2018-11-16 16:02:56 +01004790#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004791static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4792 size_t peer_key_length,
4793 const mbedtls_ecp_keypair *our_key,
4794 uint8_t *shared_secret,
4795 size_t shared_secret_size,
4796 size_t *shared_secret_length )
4797{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004798 mbedtls_ecp_keypair *their_key = NULL;
4799 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004800 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004801 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004802
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004803 status = psa_import_ec_public_key(
4804 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4805 peer_key, peer_key_length,
4806 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004807 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004808 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004809
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004810 status = mbedtls_to_psa_error(
4811 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4812 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004813 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004814 status = mbedtls_to_psa_error(
4815 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4816 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004817 goto exit;
4818
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004819 status = mbedtls_to_psa_error(
4820 mbedtls_ecdh_calc_secret( &ecdh,
4821 shared_secret_length,
4822 shared_secret, shared_secret_size,
4823 mbedtls_ctr_drbg_random,
4824 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004825
4826exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004827 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004828 mbedtls_ecp_keypair_free( their_key );
4829 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004830 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004831}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004832#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004833
Gilles Peskine01d718c2018-09-18 12:01:02 +02004834#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4835
Gilles Peskine0216fe12019-04-11 21:23:21 +02004836static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4837 psa_key_slot_t *private_key,
4838 const uint8_t *peer_key,
4839 size_t peer_key_length,
4840 uint8_t *shared_secret,
4841 size_t shared_secret_size,
4842 size_t *shared_secret_length )
4843{
4844 switch( alg )
4845 {
4846#if defined(MBEDTLS_ECDH_C)
4847 case PSA_ALG_ECDH:
4848 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4849 return( PSA_ERROR_INVALID_ARGUMENT );
4850 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
4851 private_key->data.ecp,
4852 shared_secret, shared_secret_size,
4853 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004854#endif /* MBEDTLS_ECDH_C */
4855 default:
4856 (void) private_key;
4857 (void) peer_key;
4858 (void) peer_key_length;
4859 (void) shared_secret;
4860 (void) shared_secret_size;
4861 (void) shared_secret_length;
4862 return( PSA_ERROR_NOT_SUPPORTED );
4863 }
4864}
4865
Gilles Peskine346797d2018-11-16 16:05:06 +01004866/* Note that if this function fails, you must call psa_generator_abort()
4867 * to potentially free embedded data structures and wipe confidential data.
4868 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004869static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004870 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004871 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004872 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004873 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004874{
4875 psa_status_t status;
4876 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4877 size_t shared_secret_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004878 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004879
4880 /* Step 1: run the secret agreement algorithm to generate the shared
4881 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004882 status = psa_key_agreement_raw_internal( ka_alg,
4883 private_key,
4884 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004885 shared_secret,
4886 sizeof( shared_secret ),
4887 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004888 if( status != PSA_SUCCESS )
4889 goto exit;
4890
4891 /* Step 2: set up the key derivation to generate key material from
4892 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004893 status = psa_key_derivation_input_raw( generator, step,
4894 shared_secret, shared_secret_length );
4895
Gilles Peskine01d718c2018-09-18 12:01:02 +02004896exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004897 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004898 return( status );
4899}
4900
4901psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004902 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004903 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004904 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004905 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004906{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004907 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004908 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004909 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004910 return( PSA_ERROR_INVALID_ARGUMENT );
4911 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004912 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004913 if( status != PSA_SUCCESS )
4914 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004915 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004916 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004917 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004918 if( status != PSA_SUCCESS )
4919 psa_generator_abort( generator );
4920 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004921}
4922
Gilles Peskine0216fe12019-04-11 21:23:21 +02004923psa_status_t psa_key_agreement_raw_shared_secret( psa_algorithm_t alg,
4924 psa_key_handle_t private_key,
4925 const uint8_t *peer_key,
4926 size_t peer_key_length,
4927 uint8_t *output,
4928 size_t output_size,
4929 size_t *output_length )
4930{
4931 psa_key_slot_t *slot;
4932 psa_status_t status;
4933
4934 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4935 {
4936 status = PSA_ERROR_INVALID_ARGUMENT;
4937 goto exit;
4938 }
4939 status = psa_get_key_from_slot( private_key, &slot,
4940 PSA_KEY_USAGE_DERIVE, alg );
4941 if( status != PSA_SUCCESS )
4942 goto exit;
4943
4944 status = psa_key_agreement_raw_internal( alg, slot,
4945 peer_key, peer_key_length,
4946 output, output_size,
4947 output_length );
4948
4949exit:
4950 if( status != PSA_SUCCESS )
4951 {
4952 /* If an error happens and is not handled properly, the output
4953 * may be used as a key to protect sensitive data. Arrange for such
4954 * a key to be random, which is likely to result in decryption or
4955 * verification errors. This is better than filling the buffer with
4956 * some constant data such as zeros, which would result in the data
4957 * being protected with a reproducible, easily knowable key.
4958 */
4959 psa_generate_random( output, output_size );
4960 *output_length = output_size;
4961 }
4962 return( status );
4963}
Gilles Peskine01d718c2018-09-18 12:01:02 +02004964
4965
4966/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004967/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004968/****************************************************************/
4969
4970psa_status_t psa_generate_random( uint8_t *output,
4971 size_t output_size )
4972{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004973 int ret;
4974 GUARD_MODULE_INITIALIZED;
4975
4976 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004977 return( mbedtls_to_psa_error( ret ) );
4978}
4979
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004980#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
4981#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02004982
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004983psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4984 size_t seed_size )
4985{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004986 if( global_data.initialized )
4987 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004988
4989 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4990 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4991 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4992 return( PSA_ERROR_INVALID_ARGUMENT );
4993
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004994 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004995}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004996#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004997
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004998static psa_status_t psa_generate_key_internal( psa_key_slot_t *slot,
4999 size_t bits,
5000 const void *extra,
5001 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005002{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005003 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005004
Gilles Peskine53d991e2018-07-12 01:14:59 +02005005 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005006 return( PSA_ERROR_INVALID_ARGUMENT );
5007
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005008 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005009 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005010 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005011 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005012 if( status != PSA_SUCCESS )
5013 return( status );
5014 status = psa_generate_random( slot->data.raw.data,
5015 slot->data.raw.bytes );
5016 if( status != PSA_SUCCESS )
5017 {
5018 mbedtls_free( slot->data.raw.data );
5019 return( status );
5020 }
5021#if defined(MBEDTLS_DES_C)
5022 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005023 psa_des_set_key_parity( slot->data.raw.data,
5024 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005025#endif /* MBEDTLS_DES_C */
5026 }
5027 else
5028
5029#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5030 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
5031 {
5032 mbedtls_rsa_context *rsa;
5033 int ret;
5034 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005035 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5036 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005037 /* Accept only byte-aligned keys, for the same reasons as
5038 * in psa_import_rsa_key(). */
5039 if( bits % 8 != 0 )
5040 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005041 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005042 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02005043 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02005044 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005045 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02005046#if INT_MAX < 0xffffffff
5047 /* Check that the uint32_t value passed by the caller fits
5048 * in the range supported by this implementation. */
5049 if( p->e > INT_MAX )
5050 return( PSA_ERROR_NOT_SUPPORTED );
5051#endif
5052 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005053 }
5054 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5055 if( rsa == NULL )
5056 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5057 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5058 ret = mbedtls_rsa_gen_key( rsa,
5059 mbedtls_ctr_drbg_random,
5060 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005061 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005062 exponent );
5063 if( ret != 0 )
5064 {
5065 mbedtls_rsa_free( rsa );
5066 mbedtls_free( rsa );
5067 return( mbedtls_to_psa_error( ret ) );
5068 }
5069 slot->data.rsa = rsa;
5070 }
5071 else
5072#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5073
5074#if defined(MBEDTLS_ECP_C)
5075 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
5076 {
5077 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5078 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5079 const mbedtls_ecp_curve_info *curve_info =
5080 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5081 mbedtls_ecp_keypair *ecp;
5082 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02005083 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005084 return( PSA_ERROR_NOT_SUPPORTED );
5085 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5086 return( PSA_ERROR_NOT_SUPPORTED );
5087 if( curve_info->bit_size != bits )
5088 return( PSA_ERROR_INVALID_ARGUMENT );
5089 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5090 if( ecp == NULL )
5091 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5092 mbedtls_ecp_keypair_init( ecp );
5093 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5094 mbedtls_ctr_drbg_random,
5095 &global_data.ctr_drbg );
5096 if( ret != 0 )
5097 {
5098 mbedtls_ecp_keypair_free( ecp );
5099 mbedtls_free( ecp );
5100 return( mbedtls_to_psa_error( ret ) );
5101 }
5102 slot->data.ecp = ecp;
5103 }
5104 else
5105#endif /* MBEDTLS_ECP_C */
5106
5107 return( PSA_ERROR_NOT_SUPPORTED );
5108
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005109 return( PSA_SUCCESS );
5110}
5111
5112psa_status_t psa_generate_key_to_handle( psa_key_handle_t handle,
5113 psa_key_type_t type,
5114 size_t bits,
5115 const void *extra,
5116 size_t extra_size )
5117{
5118 psa_key_slot_t *slot;
5119 psa_status_t status;
5120
5121 status = psa_get_empty_key_slot( handle, &slot );
5122 if( status != PSA_SUCCESS )
5123 return( status );
5124
Gilles Peskine12313cd2018-06-20 00:20:32 +02005125 slot->type = type;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005126 status = psa_generate_key_internal( slot, bits, extra, extra_size );
5127 if( status != PSA_SUCCESS )
5128 slot->type = 0;
Darryl Green0c6575a2018-11-07 16:05:30 +00005129
5130#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
5131 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
5132 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01005133 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005134 }
5135#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5136
5137 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005138}
5139
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005140psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
5141 psa_key_handle_t *handle,
5142 size_t bits,
5143 const void *extra,
5144 size_t extra_size )
5145{
5146 psa_status_t status;
5147 psa_key_slot_t *slot = NULL;
5148 status = psa_start_key_creation( attributes, handle, &slot );
5149 if( status == PSA_SUCCESS )
5150 {
5151 status = psa_generate_key_internal( slot, bits, extra, extra_size );
5152 }
5153 if( status == PSA_SUCCESS )
5154 status = psa_finish_key_creation( slot );
5155 if( status != PSA_SUCCESS )
5156 {
5157 psa_fail_key_creation( slot );
5158 *handle = 0;
5159 }
5160 return( status );
5161}
5162
5163
Gilles Peskine05d69892018-06-19 22:00:52 +02005164
5165/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005166/* Module setup */
5167/****************************************************************/
5168
Gilles Peskine5e769522018-11-20 21:59:56 +01005169psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5170 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5171 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5172{
5173 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5174 return( PSA_ERROR_BAD_STATE );
5175 global_data.entropy_init = entropy_init;
5176 global_data.entropy_free = entropy_free;
5177 return( PSA_SUCCESS );
5178}
5179
Gilles Peskinee59236f2018-01-27 23:32:46 +01005180void mbedtls_psa_crypto_free( void )
5181{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005182 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005183 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5184 {
5185 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005186 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005187 }
5188 /* Wipe all remaining data, including configuration.
5189 * In particular, this sets all state indicator to the value
5190 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005191 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005192}
5193
5194psa_status_t psa_crypto_init( void )
5195{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005196 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005197 const unsigned char drbg_seed[] = "PSA";
5198
Gilles Peskinec6b69072018-11-20 21:42:52 +01005199 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005200 if( global_data.initialized != 0 )
5201 return( PSA_SUCCESS );
5202
Gilles Peskine5e769522018-11-20 21:59:56 +01005203 /* Set default configuration if
5204 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5205 if( global_data.entropy_init == NULL )
5206 global_data.entropy_init = mbedtls_entropy_init;
5207 if( global_data.entropy_free == NULL )
5208 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005209
Gilles Peskinec6b69072018-11-20 21:42:52 +01005210 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005211 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005212 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005213 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005214 status = mbedtls_to_psa_error(
5215 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5216 mbedtls_entropy_func,
5217 &global_data.entropy,
5218 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5219 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005220 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005221 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005222
Gilles Peskine66fb1262018-12-10 16:29:04 +01005223 status = psa_initialize_key_slots( );
5224 if( status != PSA_SUCCESS )
5225 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005226
5227 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005228 global_data.initialized = 1;
5229
Gilles Peskinee59236f2018-01-27 23:32:46 +01005230exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005231 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005232 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005233 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005234}
5235
5236#endif /* MBEDTLS_PSA_CRYPTO_C */