blob: efec00be5202070bedc8b9b2551bef205e3c331e [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;
1310
1311#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1312 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1313 {
1314 uint8_t *buffer = NULL;
1315 size_t buffer_size = 0;
1316 size_t length;
1317
1318 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001319 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001320 buffer = mbedtls_calloc( 1, buffer_size );
1321 if( buffer == NULL && buffer_size != 0 )
1322 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1323 status = psa_internal_export_key( slot,
1324 buffer, buffer_size, &length,
1325 0 );
1326
1327 if( status == PSA_SUCCESS )
1328 {
1329 status = psa_save_persistent_key( slot->persistent_storage_id,
1330 slot->type, &slot->policy,
1331 buffer, length );
1332 }
1333
1334 if( buffer_size != 0 )
1335 mbedtls_platform_zeroize( buffer, buffer_size );
1336 mbedtls_free( buffer );
1337 }
1338#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1339
1340 return( status );
1341}
1342
1343/** Abort the creation of a key.
1344 *
1345 * You may call this function after calling psa_start_key_creation(),
1346 * or after psa_finish_key_creation() fails. In other circumstances, this
1347 * function may not clean up persistent storage.
1348 *
1349 * \param slot Pointer to the slot with key material.
1350 */
1351static void psa_fail_key_creation( psa_key_slot_t *slot )
1352{
1353 if( slot == NULL )
1354 return;
1355 psa_wipe_key_slot( slot );
1356}
1357
1358psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
1359 psa_key_handle_t *handle,
1360 const uint8_t *data,
1361 size_t data_length )
1362{
1363 psa_status_t status;
1364 psa_key_slot_t *slot = NULL;
1365 status = psa_start_key_creation( attributes, handle, &slot );
1366 if( status == PSA_SUCCESS )
1367 {
1368 status = psa_import_key_into_slot( slot, data, data_length );
1369 }
1370 if( status == PSA_SUCCESS )
1371 status = psa_finish_key_creation( slot );
1372 if( status != PSA_SUCCESS )
1373 {
1374 psa_fail_key_creation( slot );
1375 *handle = 0;
1376 }
1377 return( status );
1378}
1379
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001380static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001381 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001382{
1383 psa_status_t status;
1384 uint8_t *buffer = NULL;
1385 size_t buffer_size = 0;
1386 size_t length;
1387
1388 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001389 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001390 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001391 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001392 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001393 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1394 if( status != PSA_SUCCESS )
1395 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001396 target->type = source->type;
1397 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001398
1399exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001400 if( buffer_size != 0 )
1401 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001402 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001403 return( status );
1404}
1405
Gilles Peskine87a5e562019-04-17 12:28:25 +02001406psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001407 psa_key_handle_t target_handle,
1408 const psa_key_policy_t *constraint)
1409{
1410 psa_key_slot_t *source_slot = NULL;
1411 psa_key_slot_t *target_slot = NULL;
1412 psa_key_policy_t new_policy;
1413 psa_status_t status;
1414 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1415 if( status != PSA_SUCCESS )
1416 return( status );
1417 status = psa_get_empty_key_slot( target_handle, &target_slot );
1418 if( status != PSA_SUCCESS )
1419 return( status );
1420
1421 new_policy = target_slot->policy;
1422 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1423 if( status != PSA_SUCCESS )
1424 return( status );
1425 if( constraint != NULL )
1426 {
1427 status = psa_restrict_key_policy( &new_policy, constraint );
1428 if( status != PSA_SUCCESS )
1429 return( status );
1430 }
1431
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001432 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001433 if( status != PSA_SUCCESS )
1434 return( status );
1435
1436 target_slot->policy = new_policy;
1437 return( PSA_SUCCESS );
1438}
1439
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001440psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1441 const psa_key_attributes_t *specified_attributes,
1442 psa_key_handle_t *target_handle )
1443{
1444 psa_status_t status;
1445 psa_key_slot_t *source_slot = NULL;
1446 psa_key_slot_t *target_slot = NULL;
1447 psa_key_attributes_t actual_attributes = *specified_attributes;
1448
1449 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1450 if( status != PSA_SUCCESS )
1451 goto exit;
1452
1453 status = psa_restrict_key_policy( &actual_attributes.policy,
1454 &source_slot->policy );
1455 if( status != PSA_SUCCESS )
1456 goto exit;
1457
1458 status = psa_start_key_creation( &actual_attributes,
1459 target_handle, &target_slot );
1460 if( status != PSA_SUCCESS )
1461 goto exit;
1462
1463 status = psa_copy_key_material( source_slot, target_slot );
1464
1465exit:
1466 if( status == PSA_SUCCESS )
1467 status = psa_finish_key_creation( target_slot );
1468 if( status != PSA_SUCCESS )
1469 {
1470 psa_fail_key_creation( target_slot );
1471 *target_handle = 0;
1472 }
1473 return( status );
1474}
1475
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001476
1477
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001478/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001479/* Message digests */
1480/****************************************************************/
1481
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001482static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001483{
1484 switch( alg )
1485 {
1486#if defined(MBEDTLS_MD2_C)
1487 case PSA_ALG_MD2:
1488 return( &mbedtls_md2_info );
1489#endif
1490#if defined(MBEDTLS_MD4_C)
1491 case PSA_ALG_MD4:
1492 return( &mbedtls_md4_info );
1493#endif
1494#if defined(MBEDTLS_MD5_C)
1495 case PSA_ALG_MD5:
1496 return( &mbedtls_md5_info );
1497#endif
1498#if defined(MBEDTLS_RIPEMD160_C)
1499 case PSA_ALG_RIPEMD160:
1500 return( &mbedtls_ripemd160_info );
1501#endif
1502#if defined(MBEDTLS_SHA1_C)
1503 case PSA_ALG_SHA_1:
1504 return( &mbedtls_sha1_info );
1505#endif
1506#if defined(MBEDTLS_SHA256_C)
1507 case PSA_ALG_SHA_224:
1508 return( &mbedtls_sha224_info );
1509 case PSA_ALG_SHA_256:
1510 return( &mbedtls_sha256_info );
1511#endif
1512#if defined(MBEDTLS_SHA512_C)
1513 case PSA_ALG_SHA_384:
1514 return( &mbedtls_sha384_info );
1515 case PSA_ALG_SHA_512:
1516 return( &mbedtls_sha512_info );
1517#endif
1518 default:
1519 return( NULL );
1520 }
1521}
1522
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001523psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1524{
1525 switch( operation->alg )
1526 {
Gilles Peskine81736312018-06-26 15:04:31 +02001527 case 0:
1528 /* The object has (apparently) been initialized but it is not
1529 * in use. It's ok to call abort on such an object, and there's
1530 * nothing to do. */
1531 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001532#if defined(MBEDTLS_MD2_C)
1533 case PSA_ALG_MD2:
1534 mbedtls_md2_free( &operation->ctx.md2 );
1535 break;
1536#endif
1537#if defined(MBEDTLS_MD4_C)
1538 case PSA_ALG_MD4:
1539 mbedtls_md4_free( &operation->ctx.md4 );
1540 break;
1541#endif
1542#if defined(MBEDTLS_MD5_C)
1543 case PSA_ALG_MD5:
1544 mbedtls_md5_free( &operation->ctx.md5 );
1545 break;
1546#endif
1547#if defined(MBEDTLS_RIPEMD160_C)
1548 case PSA_ALG_RIPEMD160:
1549 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1550 break;
1551#endif
1552#if defined(MBEDTLS_SHA1_C)
1553 case PSA_ALG_SHA_1:
1554 mbedtls_sha1_free( &operation->ctx.sha1 );
1555 break;
1556#endif
1557#if defined(MBEDTLS_SHA256_C)
1558 case PSA_ALG_SHA_224:
1559 case PSA_ALG_SHA_256:
1560 mbedtls_sha256_free( &operation->ctx.sha256 );
1561 break;
1562#endif
1563#if defined(MBEDTLS_SHA512_C)
1564 case PSA_ALG_SHA_384:
1565 case PSA_ALG_SHA_512:
1566 mbedtls_sha512_free( &operation->ctx.sha512 );
1567 break;
1568#endif
1569 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001570 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001571 }
1572 operation->alg = 0;
1573 return( PSA_SUCCESS );
1574}
1575
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001576psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001577 psa_algorithm_t alg )
1578{
1579 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001580
1581 /* A context must be freshly initialized before it can be set up. */
1582 if( operation->alg != 0 )
1583 {
1584 return( PSA_ERROR_BAD_STATE );
1585 }
1586
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001587 switch( alg )
1588 {
1589#if defined(MBEDTLS_MD2_C)
1590 case PSA_ALG_MD2:
1591 mbedtls_md2_init( &operation->ctx.md2 );
1592 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1593 break;
1594#endif
1595#if defined(MBEDTLS_MD4_C)
1596 case PSA_ALG_MD4:
1597 mbedtls_md4_init( &operation->ctx.md4 );
1598 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1599 break;
1600#endif
1601#if defined(MBEDTLS_MD5_C)
1602 case PSA_ALG_MD5:
1603 mbedtls_md5_init( &operation->ctx.md5 );
1604 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1605 break;
1606#endif
1607#if defined(MBEDTLS_RIPEMD160_C)
1608 case PSA_ALG_RIPEMD160:
1609 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1610 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1611 break;
1612#endif
1613#if defined(MBEDTLS_SHA1_C)
1614 case PSA_ALG_SHA_1:
1615 mbedtls_sha1_init( &operation->ctx.sha1 );
1616 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1617 break;
1618#endif
1619#if defined(MBEDTLS_SHA256_C)
1620 case PSA_ALG_SHA_224:
1621 mbedtls_sha256_init( &operation->ctx.sha256 );
1622 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1623 break;
1624 case PSA_ALG_SHA_256:
1625 mbedtls_sha256_init( &operation->ctx.sha256 );
1626 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1627 break;
1628#endif
1629#if defined(MBEDTLS_SHA512_C)
1630 case PSA_ALG_SHA_384:
1631 mbedtls_sha512_init( &operation->ctx.sha512 );
1632 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1633 break;
1634 case PSA_ALG_SHA_512:
1635 mbedtls_sha512_init( &operation->ctx.sha512 );
1636 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1637 break;
1638#endif
1639 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001640 return( PSA_ALG_IS_HASH( alg ) ?
1641 PSA_ERROR_NOT_SUPPORTED :
1642 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001643 }
1644 if( ret == 0 )
1645 operation->alg = alg;
1646 else
1647 psa_hash_abort( operation );
1648 return( mbedtls_to_psa_error( ret ) );
1649}
1650
1651psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1652 const uint8_t *input,
1653 size_t input_length )
1654{
1655 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001656
1657 /* Don't require hash implementations to behave correctly on a
1658 * zero-length input, which may have an invalid pointer. */
1659 if( input_length == 0 )
1660 return( PSA_SUCCESS );
1661
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001662 switch( operation->alg )
1663 {
1664#if defined(MBEDTLS_MD2_C)
1665 case PSA_ALG_MD2:
1666 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1667 input, input_length );
1668 break;
1669#endif
1670#if defined(MBEDTLS_MD4_C)
1671 case PSA_ALG_MD4:
1672 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1673 input, input_length );
1674 break;
1675#endif
1676#if defined(MBEDTLS_MD5_C)
1677 case PSA_ALG_MD5:
1678 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1679 input, input_length );
1680 break;
1681#endif
1682#if defined(MBEDTLS_RIPEMD160_C)
1683 case PSA_ALG_RIPEMD160:
1684 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1685 input, input_length );
1686 break;
1687#endif
1688#if defined(MBEDTLS_SHA1_C)
1689 case PSA_ALG_SHA_1:
1690 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1691 input, input_length );
1692 break;
1693#endif
1694#if defined(MBEDTLS_SHA256_C)
1695 case PSA_ALG_SHA_224:
1696 case PSA_ALG_SHA_256:
1697 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1698 input, input_length );
1699 break;
1700#endif
1701#if defined(MBEDTLS_SHA512_C)
1702 case PSA_ALG_SHA_384:
1703 case PSA_ALG_SHA_512:
1704 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1705 input, input_length );
1706 break;
1707#endif
1708 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001709 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001710 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001711
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001712 if( ret != 0 )
1713 psa_hash_abort( operation );
1714 return( mbedtls_to_psa_error( ret ) );
1715}
1716
1717psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1718 uint8_t *hash,
1719 size_t hash_size,
1720 size_t *hash_length )
1721{
itayzafrir40835d42018-08-02 13:14:17 +03001722 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001723 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001724 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001725
1726 /* Fill the output buffer with something that isn't a valid hash
1727 * (barring an attack on the hash and deliberately-crafted input),
1728 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001729 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001730 /* If hash_size is 0 then hash may be NULL and then the
1731 * call to memset would have undefined behavior. */
1732 if( hash_size != 0 )
1733 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001734
1735 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001736 {
1737 status = PSA_ERROR_BUFFER_TOO_SMALL;
1738 goto exit;
1739 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001740
1741 switch( operation->alg )
1742 {
1743#if defined(MBEDTLS_MD2_C)
1744 case PSA_ALG_MD2:
1745 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1746 break;
1747#endif
1748#if defined(MBEDTLS_MD4_C)
1749 case PSA_ALG_MD4:
1750 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1751 break;
1752#endif
1753#if defined(MBEDTLS_MD5_C)
1754 case PSA_ALG_MD5:
1755 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1756 break;
1757#endif
1758#if defined(MBEDTLS_RIPEMD160_C)
1759 case PSA_ALG_RIPEMD160:
1760 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1761 break;
1762#endif
1763#if defined(MBEDTLS_SHA1_C)
1764 case PSA_ALG_SHA_1:
1765 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1766 break;
1767#endif
1768#if defined(MBEDTLS_SHA256_C)
1769 case PSA_ALG_SHA_224:
1770 case PSA_ALG_SHA_256:
1771 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1772 break;
1773#endif
1774#if defined(MBEDTLS_SHA512_C)
1775 case PSA_ALG_SHA_384:
1776 case PSA_ALG_SHA_512:
1777 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1778 break;
1779#endif
1780 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001781 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001782 }
itayzafrir40835d42018-08-02 13:14:17 +03001783 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001784
itayzafrir40835d42018-08-02 13:14:17 +03001785exit:
1786 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001787 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001788 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001789 return( psa_hash_abort( operation ) );
1790 }
1791 else
1792 {
1793 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001794 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001795 }
1796}
1797
Gilles Peskine2d277862018-06-18 15:41:12 +02001798psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1799 const uint8_t *hash,
1800 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001801{
1802 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1803 size_t actual_hash_length;
1804 psa_status_t status = psa_hash_finish( operation,
1805 actual_hash, sizeof( actual_hash ),
1806 &actual_hash_length );
1807 if( status != PSA_SUCCESS )
1808 return( status );
1809 if( actual_hash_length != hash_length )
1810 return( PSA_ERROR_INVALID_SIGNATURE );
1811 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1812 return( PSA_ERROR_INVALID_SIGNATURE );
1813 return( PSA_SUCCESS );
1814}
1815
Gilles Peskineeb35d782019-01-22 17:56:16 +01001816psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1817 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001818{
1819 if( target_operation->alg != 0 )
1820 return( PSA_ERROR_BAD_STATE );
1821
1822 switch( source_operation->alg )
1823 {
1824 case 0:
1825 return( PSA_ERROR_BAD_STATE );
1826#if defined(MBEDTLS_MD2_C)
1827 case PSA_ALG_MD2:
1828 mbedtls_md2_clone( &target_operation->ctx.md2,
1829 &source_operation->ctx.md2 );
1830 break;
1831#endif
1832#if defined(MBEDTLS_MD4_C)
1833 case PSA_ALG_MD4:
1834 mbedtls_md4_clone( &target_operation->ctx.md4,
1835 &source_operation->ctx.md4 );
1836 break;
1837#endif
1838#if defined(MBEDTLS_MD5_C)
1839 case PSA_ALG_MD5:
1840 mbedtls_md5_clone( &target_operation->ctx.md5,
1841 &source_operation->ctx.md5 );
1842 break;
1843#endif
1844#if defined(MBEDTLS_RIPEMD160_C)
1845 case PSA_ALG_RIPEMD160:
1846 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1847 &source_operation->ctx.ripemd160 );
1848 break;
1849#endif
1850#if defined(MBEDTLS_SHA1_C)
1851 case PSA_ALG_SHA_1:
1852 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1853 &source_operation->ctx.sha1 );
1854 break;
1855#endif
1856#if defined(MBEDTLS_SHA256_C)
1857 case PSA_ALG_SHA_224:
1858 case PSA_ALG_SHA_256:
1859 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1860 &source_operation->ctx.sha256 );
1861 break;
1862#endif
1863#if defined(MBEDTLS_SHA512_C)
1864 case PSA_ALG_SHA_384:
1865 case PSA_ALG_SHA_512:
1866 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1867 &source_operation->ctx.sha512 );
1868 break;
1869#endif
1870 default:
1871 return( PSA_ERROR_NOT_SUPPORTED );
1872 }
1873
1874 target_operation->alg = source_operation->alg;
1875 return( PSA_SUCCESS );
1876}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001877
1878
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001879/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001880/* MAC */
1881/****************************************************************/
1882
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001883static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001884 psa_algorithm_t alg,
1885 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001886 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001887 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001888{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001889 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001890 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001891
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001892 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001893 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001894
Gilles Peskine8c9def32018-02-08 10:02:12 +01001895 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1896 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001897 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001898 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001899 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001900 mode = MBEDTLS_MODE_STREAM;
1901 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001902 case PSA_ALG_CTR:
1903 mode = MBEDTLS_MODE_CTR;
1904 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001905 case PSA_ALG_CFB:
1906 mode = MBEDTLS_MODE_CFB;
1907 break;
1908 case PSA_ALG_OFB:
1909 mode = MBEDTLS_MODE_OFB;
1910 break;
1911 case PSA_ALG_CBC_NO_PADDING:
1912 mode = MBEDTLS_MODE_CBC;
1913 break;
1914 case PSA_ALG_CBC_PKCS7:
1915 mode = MBEDTLS_MODE_CBC;
1916 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001917 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001918 mode = MBEDTLS_MODE_CCM;
1919 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001920 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001921 mode = MBEDTLS_MODE_GCM;
1922 break;
1923 default:
1924 return( NULL );
1925 }
1926 }
1927 else if( alg == PSA_ALG_CMAC )
1928 mode = MBEDTLS_MODE_ECB;
1929 else if( alg == PSA_ALG_GMAC )
1930 mode = MBEDTLS_MODE_GCM;
1931 else
1932 return( NULL );
1933
1934 switch( key_type )
1935 {
1936 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001937 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001938 break;
1939 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001940 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1941 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001942 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001943 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001944 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001945 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001946 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1947 * but two-key Triple-DES is functionally three-key Triple-DES
1948 * with K1=K3, so that's how we present it to mbedtls. */
1949 if( key_bits == 128 )
1950 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001951 break;
1952 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001953 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001954 break;
1955 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001956 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001957 break;
1958 default:
1959 return( NULL );
1960 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001961 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001962 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001963
Jaeden Amero23bbb752018-06-26 14:16:54 +01001964 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1965 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001966}
1967
Gilles Peskinea05219c2018-11-16 16:02:56 +01001968#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001969static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001970{
Gilles Peskine2d277862018-06-18 15:41:12 +02001971 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001972 {
1973 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001974 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001975 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001976 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001977 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001978 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001979 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001980 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001981 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001982 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001983 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001984 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001985 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001986 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001987 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001988 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001989 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001990 return( 128 );
1991 default:
1992 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001993 }
1994}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001995#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001996
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001997/* Initialize the MAC operation structure. Once this function has been
1998 * called, psa_mac_abort can run and will do the right thing. */
1999static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2000 psa_algorithm_t alg )
2001{
2002 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2003
2004 operation->alg = alg;
2005 operation->key_set = 0;
2006 operation->iv_set = 0;
2007 operation->iv_required = 0;
2008 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002009 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002010
2011#if defined(MBEDTLS_CMAC_C)
2012 if( alg == PSA_ALG_CMAC )
2013 {
2014 operation->iv_required = 0;
2015 mbedtls_cipher_init( &operation->ctx.cmac );
2016 status = PSA_SUCCESS;
2017 }
2018 else
2019#endif /* MBEDTLS_CMAC_C */
2020#if defined(MBEDTLS_MD_C)
2021 if( PSA_ALG_IS_HMAC( operation->alg ) )
2022 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002023 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2024 operation->ctx.hmac.hash_ctx.alg = 0;
2025 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002026 }
2027 else
2028#endif /* MBEDTLS_MD_C */
2029 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002030 if( ! PSA_ALG_IS_MAC( alg ) )
2031 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002032 }
2033
2034 if( status != PSA_SUCCESS )
2035 memset( operation, 0, sizeof( *operation ) );
2036 return( status );
2037}
2038
Gilles Peskine01126fa2018-07-12 17:04:55 +02002039#if defined(MBEDTLS_MD_C)
2040static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2041{
Gilles Peskine3f108122018-12-07 18:14:53 +01002042 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002043 return( psa_hash_abort( &hmac->hash_ctx ) );
2044}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002045
2046static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2047{
2048 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2049 memset( hmac, 0, sizeof( *hmac ) );
2050}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002051#endif /* MBEDTLS_MD_C */
2052
Gilles Peskine8c9def32018-02-08 10:02:12 +01002053psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2054{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002055 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002056 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002057 /* The object has (apparently) been initialized but it is not
2058 * in use. It's ok to call abort on such an object, and there's
2059 * nothing to do. */
2060 return( PSA_SUCCESS );
2061 }
2062 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002063#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002064 if( operation->alg == PSA_ALG_CMAC )
2065 {
2066 mbedtls_cipher_free( &operation->ctx.cmac );
2067 }
2068 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002069#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002070#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002071 if( PSA_ALG_IS_HMAC( operation->alg ) )
2072 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002073 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002074 }
2075 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002076#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002077 {
2078 /* Sanity check (shouldn't happen: operation->alg should
2079 * always have been initialized to a valid value). */
2080 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002081 }
Moran Peker41deec42018-04-04 15:43:05 +03002082
Gilles Peskine8c9def32018-02-08 10:02:12 +01002083 operation->alg = 0;
2084 operation->key_set = 0;
2085 operation->iv_set = 0;
2086 operation->iv_required = 0;
2087 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002088 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002089
Gilles Peskine8c9def32018-02-08 10:02:12 +01002090 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002091
2092bad_state:
2093 /* If abort is called on an uninitialized object, we can't trust
2094 * anything. Wipe the object in case it contains confidential data.
2095 * This may result in a memory leak if a pointer gets overwritten,
2096 * but it's too late to do anything about this. */
2097 memset( operation, 0, sizeof( *operation ) );
2098 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002099}
2100
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002101#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002102static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002103 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002104 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002105 const mbedtls_cipher_info_t *cipher_info )
2106{
2107 int ret;
2108
2109 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002110
2111 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2112 if( ret != 0 )
2113 return( ret );
2114
2115 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2116 slot->data.raw.data,
2117 key_bits );
2118 return( ret );
2119}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002120#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002121
Gilles Peskine248051a2018-06-20 16:09:38 +02002122#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002123static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2124 const uint8_t *key,
2125 size_t key_length,
2126 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002127{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002128 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002129 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002130 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002131 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002132 psa_status_t status;
2133
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002134 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2135 * overflow below. This should never trigger if the hash algorithm
2136 * is implemented correctly. */
2137 /* The size checks against the ipad and opad buffers cannot be written
2138 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2139 * because that triggers -Wlogical-op on GCC 7.3. */
2140 if( block_size > sizeof( ipad ) )
2141 return( PSA_ERROR_NOT_SUPPORTED );
2142 if( block_size > sizeof( hmac->opad ) )
2143 return( PSA_ERROR_NOT_SUPPORTED );
2144 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002145 return( PSA_ERROR_NOT_SUPPORTED );
2146
Gilles Peskined223b522018-06-11 18:12:58 +02002147 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002148 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002149 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002150 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002151 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002152 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002153 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002154 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002155 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002156 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002157 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002158 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002159 }
Gilles Peskine96889972018-07-12 17:07:03 +02002160 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2161 * but it is permitted. It is common when HMAC is used in HKDF, for
2162 * example. Don't call `memcpy` in the 0-length because `key` could be
2163 * an invalid pointer which would make the behavior undefined. */
2164 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002165 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002166
Gilles Peskined223b522018-06-11 18:12:58 +02002167 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2168 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002169 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002170 ipad[i] ^= 0x36;
2171 memset( ipad + key_length, 0x36, block_size - key_length );
2172
2173 /* Copy the key material from ipad to opad, flipping the requisite bits,
2174 * and filling the rest of opad with the requisite constant. */
2175 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002176 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2177 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002178
Gilles Peskine01126fa2018-07-12 17:04:55 +02002179 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002180 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002181 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002182
Gilles Peskine01126fa2018-07-12 17:04:55 +02002183 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002184
2185cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002186 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002187
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002188 return( status );
2189}
Gilles Peskine248051a2018-06-20 16:09:38 +02002190#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002191
Gilles Peskine89167cb2018-07-08 20:12:23 +02002192static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002193 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002194 psa_algorithm_t alg,
2195 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002196{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002197 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002198 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002199 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002200 psa_key_usage_t usage =
2201 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002202 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002203 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002204
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002205 /* A context must be freshly initialized before it can be set up. */
2206 if( operation->alg != 0 )
2207 {
2208 return( PSA_ERROR_BAD_STATE );
2209 }
2210
Gilles Peskined911eb72018-08-14 15:18:45 +02002211 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002212 if( status != PSA_SUCCESS )
2213 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002214 if( is_sign )
2215 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002216
Gilles Peskinec5487a82018-12-03 18:08:14 +01002217 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002218 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002219 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002220 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002221
Gilles Peskine8c9def32018-02-08 10:02:12 +01002222#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002223 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002224 {
2225 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002226 mbedtls_cipher_info_from_psa( full_length_alg,
2227 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002228 int ret;
2229 if( cipher_info == NULL )
2230 {
2231 status = PSA_ERROR_NOT_SUPPORTED;
2232 goto exit;
2233 }
2234 operation->mac_size = cipher_info->block_size;
2235 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2236 status = mbedtls_to_psa_error( ret );
2237 }
2238 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002239#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002240#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002241 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002242 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002243 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002244 if( hash_alg == 0 )
2245 {
2246 status = PSA_ERROR_NOT_SUPPORTED;
2247 goto exit;
2248 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002249
2250 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2251 /* Sanity check. This shouldn't fail on a valid configuration. */
2252 if( operation->mac_size == 0 ||
2253 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2254 {
2255 status = PSA_ERROR_NOT_SUPPORTED;
2256 goto exit;
2257 }
2258
Gilles Peskine01126fa2018-07-12 17:04:55 +02002259 if( slot->type != PSA_KEY_TYPE_HMAC )
2260 {
2261 status = PSA_ERROR_INVALID_ARGUMENT;
2262 goto exit;
2263 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002264
Gilles Peskine01126fa2018-07-12 17:04:55 +02002265 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2266 slot->data.raw.data,
2267 slot->data.raw.bytes,
2268 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002269 }
2270 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002271#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002272 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002273 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002274 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002275 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002276
Gilles Peskined911eb72018-08-14 15:18:45 +02002277 if( truncated == 0 )
2278 {
2279 /* The "normal" case: untruncated algorithm. Nothing to do. */
2280 }
2281 else if( truncated < 4 )
2282 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002283 /* A very short MAC is too short for security since it can be
2284 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2285 * so we make this our minimum, even though 32 bits is still
2286 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002287 status = PSA_ERROR_NOT_SUPPORTED;
2288 }
2289 else if( truncated > operation->mac_size )
2290 {
2291 /* It's impossible to "truncate" to a larger length. */
2292 status = PSA_ERROR_INVALID_ARGUMENT;
2293 }
2294 else
2295 operation->mac_size = truncated;
2296
Gilles Peskinefbfac682018-07-08 20:51:54 +02002297exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002298 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002299 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002300 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002301 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002302 else
2303 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002304 operation->key_set = 1;
2305 }
2306 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002307}
2308
Gilles Peskine89167cb2018-07-08 20:12:23 +02002309psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002310 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002311 psa_algorithm_t alg )
2312{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002313 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002314}
2315
2316psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002317 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002318 psa_algorithm_t alg )
2319{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002320 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002321}
2322
Gilles Peskine8c9def32018-02-08 10:02:12 +01002323psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2324 const uint8_t *input,
2325 size_t input_length )
2326{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002327 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002328 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002329 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002330 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002331 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002332 operation->has_input = 1;
2333
Gilles Peskine8c9def32018-02-08 10:02:12 +01002334#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002335 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002336 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002337 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2338 input, input_length );
2339 status = mbedtls_to_psa_error( ret );
2340 }
2341 else
2342#endif /* MBEDTLS_CMAC_C */
2343#if defined(MBEDTLS_MD_C)
2344 if( PSA_ALG_IS_HMAC( operation->alg ) )
2345 {
2346 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2347 input_length );
2348 }
2349 else
2350#endif /* MBEDTLS_MD_C */
2351 {
2352 /* This shouldn't happen if `operation` was initialized by
2353 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002354 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002355 }
2356
Gilles Peskinefbfac682018-07-08 20:51:54 +02002357 if( status != PSA_SUCCESS )
2358 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002359 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002360}
2361
Gilles Peskine01126fa2018-07-12 17:04:55 +02002362#if defined(MBEDTLS_MD_C)
2363static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2364 uint8_t *mac,
2365 size_t mac_size )
2366{
2367 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2368 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2369 size_t hash_size = 0;
2370 size_t block_size = psa_get_hash_block_size( hash_alg );
2371 psa_status_t status;
2372
Gilles Peskine01126fa2018-07-12 17:04:55 +02002373 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2374 if( status != PSA_SUCCESS )
2375 return( status );
2376 /* From here on, tmp needs to be wiped. */
2377
2378 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2379 if( status != PSA_SUCCESS )
2380 goto exit;
2381
2382 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2383 if( status != PSA_SUCCESS )
2384 goto exit;
2385
2386 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2387 if( status != PSA_SUCCESS )
2388 goto exit;
2389
Gilles Peskined911eb72018-08-14 15:18:45 +02002390 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2391 if( status != PSA_SUCCESS )
2392 goto exit;
2393
2394 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002395
2396exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002397 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002398 return( status );
2399}
2400#endif /* MBEDTLS_MD_C */
2401
mohammad16036df908f2018-04-02 08:34:15 -07002402static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002403 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002404 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002405{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002406 if( ! operation->key_set )
2407 return( PSA_ERROR_BAD_STATE );
2408 if( operation->iv_required && ! operation->iv_set )
2409 return( PSA_ERROR_BAD_STATE );
2410
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411 if( mac_size < operation->mac_size )
2412 return( PSA_ERROR_BUFFER_TOO_SMALL );
2413
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002415 if( operation->alg == PSA_ALG_CMAC )
2416 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002417 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2418 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2419 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002420 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002421 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002422 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002423 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002424 else
2425#endif /* MBEDTLS_CMAC_C */
2426#if defined(MBEDTLS_MD_C)
2427 if( PSA_ALG_IS_HMAC( operation->alg ) )
2428 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002429 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002430 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002431 }
2432 else
2433#endif /* MBEDTLS_MD_C */
2434 {
2435 /* This shouldn't happen if `operation` was initialized by
2436 * a setup function. */
2437 return( PSA_ERROR_BAD_STATE );
2438 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002439}
2440
Gilles Peskineacd4be32018-07-08 19:56:25 +02002441psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2442 uint8_t *mac,
2443 size_t mac_size,
2444 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002445{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002446 psa_status_t status;
2447
Jaeden Amero252ef282019-02-15 14:05:35 +00002448 if( operation->alg == 0 )
2449 {
2450 return( PSA_ERROR_BAD_STATE );
2451 }
2452
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002453 /* Fill the output buffer with something that isn't a valid mac
2454 * (barring an attack on the mac and deliberately-crafted input),
2455 * in case the caller doesn't check the return status properly. */
2456 *mac_length = mac_size;
2457 /* If mac_size is 0 then mac may be NULL and then the
2458 * call to memset would have undefined behavior. */
2459 if( mac_size != 0 )
2460 memset( mac, '!', mac_size );
2461
Gilles Peskine89167cb2018-07-08 20:12:23 +02002462 if( ! operation->is_sign )
2463 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002464 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002465 }
mohammad16036df908f2018-04-02 08:34:15 -07002466
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002467 status = psa_mac_finish_internal( operation, mac, mac_size );
2468
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002469 if( status == PSA_SUCCESS )
2470 {
2471 status = psa_mac_abort( operation );
2472 if( status == PSA_SUCCESS )
2473 *mac_length = operation->mac_size;
2474 else
2475 memset( mac, '!', mac_size );
2476 }
2477 else
2478 psa_mac_abort( operation );
2479 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002480}
2481
Gilles Peskineacd4be32018-07-08 19:56:25 +02002482psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2483 const uint8_t *mac,
2484 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485{
Gilles Peskine828ed142018-06-18 23:25:51 +02002486 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002487 psa_status_t status;
2488
Jaeden Amero252ef282019-02-15 14:05:35 +00002489 if( operation->alg == 0 )
2490 {
2491 return( PSA_ERROR_BAD_STATE );
2492 }
2493
Gilles Peskine89167cb2018-07-08 20:12:23 +02002494 if( operation->is_sign )
2495 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002496 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002497 }
2498 if( operation->mac_size != mac_length )
2499 {
2500 status = PSA_ERROR_INVALID_SIGNATURE;
2501 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002502 }
mohammad16036df908f2018-04-02 08:34:15 -07002503
2504 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002505 actual_mac, sizeof( actual_mac ) );
2506
2507 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2508 status = PSA_ERROR_INVALID_SIGNATURE;
2509
2510cleanup:
2511 if( status == PSA_SUCCESS )
2512 status = psa_mac_abort( operation );
2513 else
2514 psa_mac_abort( operation );
2515
Gilles Peskine3f108122018-12-07 18:14:53 +01002516 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002517
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002518 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002519}
2520
2521
Gilles Peskine20035e32018-02-03 22:44:14 +01002522
Gilles Peskine20035e32018-02-03 22:44:14 +01002523/****************************************************************/
2524/* Asymmetric cryptography */
2525/****************************************************************/
2526
Gilles Peskine2b450e32018-06-27 15:42:46 +02002527#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002528/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002529 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002530static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2531 size_t hash_length,
2532 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002533{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002534 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002535 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002536 *md_alg = mbedtls_md_get_type( md_info );
2537
2538 /* The Mbed TLS RSA module uses an unsigned int for hash length
2539 * parameters. Validate that it fits so that we don't risk an
2540 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002541#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002542 if( hash_length > UINT_MAX )
2543 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002544#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002545
2546#if defined(MBEDTLS_PKCS1_V15)
2547 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2548 * must be correct. */
2549 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2550 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002551 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002552 if( md_info == NULL )
2553 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002554 if( mbedtls_md_get_size( md_info ) != hash_length )
2555 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002556 }
2557#endif /* MBEDTLS_PKCS1_V15 */
2558
2559#if defined(MBEDTLS_PKCS1_V21)
2560 /* PSS requires a hash internally. */
2561 if( PSA_ALG_IS_RSA_PSS( alg ) )
2562 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002563 if( md_info == NULL )
2564 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002565 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002566#endif /* MBEDTLS_PKCS1_V21 */
2567
Gilles Peskine61b91d42018-06-08 16:09:36 +02002568 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002569}
2570
Gilles Peskine2b450e32018-06-27 15:42:46 +02002571static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2572 psa_algorithm_t alg,
2573 const uint8_t *hash,
2574 size_t hash_length,
2575 uint8_t *signature,
2576 size_t signature_size,
2577 size_t *signature_length )
2578{
2579 psa_status_t status;
2580 int ret;
2581 mbedtls_md_type_t md_alg;
2582
2583 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2584 if( status != PSA_SUCCESS )
2585 return( status );
2586
Gilles Peskine630a18a2018-06-29 17:49:35 +02002587 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002588 return( PSA_ERROR_BUFFER_TOO_SMALL );
2589
2590#if defined(MBEDTLS_PKCS1_V15)
2591 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2592 {
2593 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2594 MBEDTLS_MD_NONE );
2595 ret = mbedtls_rsa_pkcs1_sign( rsa,
2596 mbedtls_ctr_drbg_random,
2597 &global_data.ctr_drbg,
2598 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002599 md_alg,
2600 (unsigned int) hash_length,
2601 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002602 signature );
2603 }
2604 else
2605#endif /* MBEDTLS_PKCS1_V15 */
2606#if defined(MBEDTLS_PKCS1_V21)
2607 if( PSA_ALG_IS_RSA_PSS( alg ) )
2608 {
2609 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2610 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2611 mbedtls_ctr_drbg_random,
2612 &global_data.ctr_drbg,
2613 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002614 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002615 (unsigned int) hash_length,
2616 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002617 signature );
2618 }
2619 else
2620#endif /* MBEDTLS_PKCS1_V21 */
2621 {
2622 return( PSA_ERROR_INVALID_ARGUMENT );
2623 }
2624
2625 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002626 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002627 return( mbedtls_to_psa_error( ret ) );
2628}
2629
2630static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2631 psa_algorithm_t alg,
2632 const uint8_t *hash,
2633 size_t hash_length,
2634 const uint8_t *signature,
2635 size_t signature_length )
2636{
2637 psa_status_t status;
2638 int ret;
2639 mbedtls_md_type_t md_alg;
2640
2641 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2642 if( status != PSA_SUCCESS )
2643 return( status );
2644
Gilles Peskine630a18a2018-06-29 17:49:35 +02002645 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002646 return( PSA_ERROR_BUFFER_TOO_SMALL );
2647
2648#if defined(MBEDTLS_PKCS1_V15)
2649 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2650 {
2651 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2652 MBEDTLS_MD_NONE );
2653 ret = mbedtls_rsa_pkcs1_verify( rsa,
2654 mbedtls_ctr_drbg_random,
2655 &global_data.ctr_drbg,
2656 MBEDTLS_RSA_PUBLIC,
2657 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002658 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002659 hash,
2660 signature );
2661 }
2662 else
2663#endif /* MBEDTLS_PKCS1_V15 */
2664#if defined(MBEDTLS_PKCS1_V21)
2665 if( PSA_ALG_IS_RSA_PSS( alg ) )
2666 {
2667 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2668 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2669 mbedtls_ctr_drbg_random,
2670 &global_data.ctr_drbg,
2671 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002672 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002673 (unsigned int) hash_length,
2674 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002675 signature );
2676 }
2677 else
2678#endif /* MBEDTLS_PKCS1_V21 */
2679 {
2680 return( PSA_ERROR_INVALID_ARGUMENT );
2681 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002682
2683 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2684 * the rest of the signature is invalid". This has little use in
2685 * practice and PSA doesn't report this distinction. */
2686 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2687 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002688 return( mbedtls_to_psa_error( ret ) );
2689}
2690#endif /* MBEDTLS_RSA_C */
2691
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002692#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002693/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2694 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2695 * (even though these functions don't modify it). */
2696static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2697 psa_algorithm_t alg,
2698 const uint8_t *hash,
2699 size_t hash_length,
2700 uint8_t *signature,
2701 size_t signature_size,
2702 size_t *signature_length )
2703{
2704 int ret;
2705 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002706 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002707 mbedtls_mpi_init( &r );
2708 mbedtls_mpi_init( &s );
2709
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002710 if( signature_size < 2 * curve_bytes )
2711 {
2712 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2713 goto cleanup;
2714 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002715
Gilles Peskinea05219c2018-11-16 16:02:56 +01002716#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002717 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2718 {
2719 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2720 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2721 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2722 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2723 hash, hash_length,
2724 md_alg ) );
2725 }
2726 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002727#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002728 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002729 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002730 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2731 hash, hash_length,
2732 mbedtls_ctr_drbg_random,
2733 &global_data.ctr_drbg ) );
2734 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002735
2736 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2737 signature,
2738 curve_bytes ) );
2739 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2740 signature + curve_bytes,
2741 curve_bytes ) );
2742
2743cleanup:
2744 mbedtls_mpi_free( &r );
2745 mbedtls_mpi_free( &s );
2746 if( ret == 0 )
2747 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002748 return( mbedtls_to_psa_error( ret ) );
2749}
2750
2751static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2752 const uint8_t *hash,
2753 size_t hash_length,
2754 const uint8_t *signature,
2755 size_t signature_length )
2756{
2757 int ret;
2758 mbedtls_mpi r, s;
2759 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2760 mbedtls_mpi_init( &r );
2761 mbedtls_mpi_init( &s );
2762
2763 if( signature_length != 2 * curve_bytes )
2764 return( PSA_ERROR_INVALID_SIGNATURE );
2765
2766 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2767 signature,
2768 curve_bytes ) );
2769 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2770 signature + curve_bytes,
2771 curve_bytes ) );
2772
2773 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2774 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002775
2776cleanup:
2777 mbedtls_mpi_free( &r );
2778 mbedtls_mpi_free( &s );
2779 return( mbedtls_to_psa_error( ret ) );
2780}
2781#endif /* MBEDTLS_ECDSA_C */
2782
Gilles Peskinec5487a82018-12-03 18:08:14 +01002783psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002784 psa_algorithm_t alg,
2785 const uint8_t *hash,
2786 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002787 uint8_t *signature,
2788 size_t signature_size,
2789 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002790{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002791 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002792 psa_status_t status;
2793
2794 *signature_length = signature_size;
2795
Gilles Peskinec5487a82018-12-03 18:08:14 +01002796 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002797 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002798 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002799 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002800 {
2801 status = PSA_ERROR_INVALID_ARGUMENT;
2802 goto exit;
2803 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002804
Gilles Peskine20035e32018-02-03 22:44:14 +01002805#if defined(MBEDTLS_RSA_C)
2806 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2807 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002808 status = psa_rsa_sign( slot->data.rsa,
2809 alg,
2810 hash, hash_length,
2811 signature, signature_size,
2812 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002813 }
2814 else
2815#endif /* defined(MBEDTLS_RSA_C) */
2816#if defined(MBEDTLS_ECP_C)
2817 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2818 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002819#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002820 if(
2821#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2822 PSA_ALG_IS_ECDSA( alg )
2823#else
2824 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2825#endif
2826 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002827 status = psa_ecdsa_sign( slot->data.ecp,
2828 alg,
2829 hash, hash_length,
2830 signature, signature_size,
2831 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002832 else
2833#endif /* defined(MBEDTLS_ECDSA_C) */
2834 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002835 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002836 }
itayzafrir5c753392018-05-08 11:18:38 +03002837 }
2838 else
2839#endif /* defined(MBEDTLS_ECP_C) */
2840 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002841 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002842 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002843
2844exit:
2845 /* Fill the unused part of the output buffer (the whole buffer on error,
2846 * the trailing part on success) with something that isn't a valid mac
2847 * (barring an attack on the mac and deliberately-crafted input),
2848 * in case the caller doesn't check the return status properly. */
2849 if( status == PSA_SUCCESS )
2850 memset( signature + *signature_length, '!',
2851 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002852 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002853 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002854 /* If signature_size is 0 then we have nothing to do. We must not call
2855 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002856 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002857}
2858
Gilles Peskinec5487a82018-12-03 18:08:14 +01002859psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002860 psa_algorithm_t alg,
2861 const uint8_t *hash,
2862 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002863 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002864 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002865{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002866 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002867 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002868
Gilles Peskinec5487a82018-12-03 18:08:14 +01002869 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002870 if( status != PSA_SUCCESS )
2871 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002872
Gilles Peskine61b91d42018-06-08 16:09:36 +02002873#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002874 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002875 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002876 return( psa_rsa_verify( slot->data.rsa,
2877 alg,
2878 hash, hash_length,
2879 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002880 }
2881 else
2882#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002883#if defined(MBEDTLS_ECP_C)
2884 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2885 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002886#if defined(MBEDTLS_ECDSA_C)
2887 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002888 return( psa_ecdsa_verify( slot->data.ecp,
2889 hash, hash_length,
2890 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002891 else
2892#endif /* defined(MBEDTLS_ECDSA_C) */
2893 {
2894 return( PSA_ERROR_INVALID_ARGUMENT );
2895 }
itayzafrir5c753392018-05-08 11:18:38 +03002896 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002897 else
2898#endif /* defined(MBEDTLS_ECP_C) */
2899 {
2900 return( PSA_ERROR_NOT_SUPPORTED );
2901 }
2902}
2903
Gilles Peskine072ac562018-06-30 00:21:29 +02002904#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2905static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2906 mbedtls_rsa_context *rsa )
2907{
2908 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2909 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2910 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2911 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2912}
2913#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2914
Gilles Peskinec5487a82018-12-03 18:08:14 +01002915psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002916 psa_algorithm_t alg,
2917 const uint8_t *input,
2918 size_t input_length,
2919 const uint8_t *salt,
2920 size_t salt_length,
2921 uint8_t *output,
2922 size_t output_size,
2923 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002924{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002925 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002926 psa_status_t status;
2927
Darryl Green5cc689a2018-07-24 15:34:10 +01002928 (void) input;
2929 (void) input_length;
2930 (void) salt;
2931 (void) output;
2932 (void) output_size;
2933
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002934 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002935
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002936 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2937 return( PSA_ERROR_INVALID_ARGUMENT );
2938
Gilles Peskinec5487a82018-12-03 18:08:14 +01002939 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002940 if( status != PSA_SUCCESS )
2941 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002942 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2943 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002944 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002945
2946#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002947 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002948 {
2949 mbedtls_rsa_context *rsa = slot->data.rsa;
2950 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002951 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002952 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002953#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002954 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002955 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002956 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2957 mbedtls_ctr_drbg_random,
2958 &global_data.ctr_drbg,
2959 MBEDTLS_RSA_PUBLIC,
2960 input_length,
2961 input,
2962 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002963 }
2964 else
2965#endif /* MBEDTLS_PKCS1_V15 */
2966#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002967 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002968 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002969 psa_rsa_oaep_set_padding_mode( alg, rsa );
2970 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2971 mbedtls_ctr_drbg_random,
2972 &global_data.ctr_drbg,
2973 MBEDTLS_RSA_PUBLIC,
2974 salt, salt_length,
2975 input_length,
2976 input,
2977 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002978 }
2979 else
2980#endif /* MBEDTLS_PKCS1_V21 */
2981 {
2982 return( PSA_ERROR_INVALID_ARGUMENT );
2983 }
2984 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002985 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002986 return( mbedtls_to_psa_error( ret ) );
2987 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002988 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002989#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002990 {
2991 return( PSA_ERROR_NOT_SUPPORTED );
2992 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002993}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002994
Gilles Peskinec5487a82018-12-03 18:08:14 +01002995psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002996 psa_algorithm_t alg,
2997 const uint8_t *input,
2998 size_t input_length,
2999 const uint8_t *salt,
3000 size_t salt_length,
3001 uint8_t *output,
3002 size_t output_size,
3003 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003004{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003005 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003006 psa_status_t status;
3007
Darryl Green5cc689a2018-07-24 15:34:10 +01003008 (void) input;
3009 (void) input_length;
3010 (void) salt;
3011 (void) output;
3012 (void) output_size;
3013
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003014 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003015
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003016 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3017 return( PSA_ERROR_INVALID_ARGUMENT );
3018
Gilles Peskinec5487a82018-12-03 18:08:14 +01003019 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003020 if( status != PSA_SUCCESS )
3021 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003022 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
3023 return( PSA_ERROR_INVALID_ARGUMENT );
3024
3025#if defined(MBEDTLS_RSA_C)
3026 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
3027 {
3028 mbedtls_rsa_context *rsa = slot->data.rsa;
3029 int ret;
3030
Gilles Peskine630a18a2018-06-29 17:49:35 +02003031 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032 return( PSA_ERROR_INVALID_ARGUMENT );
3033
3034#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003035 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003036 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003037 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3038 mbedtls_ctr_drbg_random,
3039 &global_data.ctr_drbg,
3040 MBEDTLS_RSA_PRIVATE,
3041 output_length,
3042 input,
3043 output,
3044 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003045 }
3046 else
3047#endif /* MBEDTLS_PKCS1_V15 */
3048#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003049 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003050 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003051 psa_rsa_oaep_set_padding_mode( alg, rsa );
3052 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3053 mbedtls_ctr_drbg_random,
3054 &global_data.ctr_drbg,
3055 MBEDTLS_RSA_PRIVATE,
3056 salt, salt_length,
3057 output_length,
3058 input,
3059 output,
3060 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003061 }
3062 else
3063#endif /* MBEDTLS_PKCS1_V21 */
3064 {
3065 return( PSA_ERROR_INVALID_ARGUMENT );
3066 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003067
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003068 return( mbedtls_to_psa_error( ret ) );
3069 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003070 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003071#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003072 {
3073 return( PSA_ERROR_NOT_SUPPORTED );
3074 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003075}
Gilles Peskine20035e32018-02-03 22:44:14 +01003076
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003077
3078
mohammad1603503973b2018-03-12 15:59:30 +02003079/****************************************************************/
3080/* Symmetric cryptography */
3081/****************************************************************/
3082
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003083/* Initialize the cipher operation structure. Once this function has been
3084 * called, psa_cipher_abort can run and will do the right thing. */
3085static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3086 psa_algorithm_t alg )
3087{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003088 if( ! PSA_ALG_IS_CIPHER( alg ) )
3089 {
3090 memset( operation, 0, sizeof( *operation ) );
3091 return( PSA_ERROR_INVALID_ARGUMENT );
3092 }
3093
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003094 operation->alg = alg;
3095 operation->key_set = 0;
3096 operation->iv_set = 0;
3097 operation->iv_required = 1;
3098 operation->iv_size = 0;
3099 operation->block_size = 0;
3100 mbedtls_cipher_init( &operation->ctx.cipher );
3101 return( PSA_SUCCESS );
3102}
3103
Gilles Peskinee553c652018-06-04 16:22:46 +02003104static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003105 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003106 psa_algorithm_t alg,
3107 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003108{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003109 int ret = 0;
3110 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003111 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003112 size_t key_bits;
3113 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003114 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3115 PSA_KEY_USAGE_ENCRYPT :
3116 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003117
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003118 /* A context must be freshly initialized before it can be set up. */
3119 if( operation->alg != 0 )
3120 {
3121 return( PSA_ERROR_BAD_STATE );
3122 }
3123
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003124 status = psa_cipher_init( operation, alg );
3125 if( status != PSA_SUCCESS )
3126 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003127
Gilles Peskinec5487a82018-12-03 18:08:14 +01003128 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003129 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003130 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003131 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003132
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003133 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003134 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003135 {
3136 status = PSA_ERROR_NOT_SUPPORTED;
3137 goto exit;
3138 }
mohammad1603503973b2018-03-12 15:59:30 +02003139
mohammad1603503973b2018-03-12 15:59:30 +02003140 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003141 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003142 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003143
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003144#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003145 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003146 {
3147 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3148 unsigned char keys[24];
3149 memcpy( keys, slot->data.raw.data, 16 );
3150 memcpy( keys + 16, slot->data.raw.data, 8 );
3151 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3152 keys,
3153 192, cipher_operation );
3154 }
3155 else
3156#endif
3157 {
3158 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3159 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003160 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003161 }
Moran Peker41deec42018-04-04 15:43:05 +03003162 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003163 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003164
mohammad16038481e742018-03-18 13:57:31 +02003165#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003166 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003167 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003168 case PSA_ALG_CBC_NO_PADDING:
3169 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3170 MBEDTLS_PADDING_NONE );
3171 break;
3172 case PSA_ALG_CBC_PKCS7:
3173 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3174 MBEDTLS_PADDING_PKCS7 );
3175 break;
3176 default:
3177 /* The algorithm doesn't involve padding. */
3178 ret = 0;
3179 break;
3180 }
3181 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003182 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003183#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3184
mohammad1603503973b2018-03-12 15:59:30 +02003185 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003186 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3187 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3188 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003189 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003190 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003191 }
mohammad1603503973b2018-03-12 15:59:30 +02003192
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003193exit:
3194 if( status == 0 )
3195 status = mbedtls_to_psa_error( ret );
3196 if( status != 0 )
3197 psa_cipher_abort( operation );
3198 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003199}
3200
Gilles Peskinefe119512018-07-08 21:39:34 +02003201psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003202 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003203 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003204{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003205 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003206}
3207
Gilles Peskinefe119512018-07-08 21:39:34 +02003208psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003209 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003210 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003211{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003212 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003213}
3214
Gilles Peskinefe119512018-07-08 21:39:34 +02003215psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3216 unsigned char *iv,
3217 size_t iv_size,
3218 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003219{
itayzafrir534bd7c2018-08-02 13:56:32 +03003220 psa_status_t status;
3221 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003222 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003223 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003224 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003225 }
Moran Peker41deec42018-04-04 15:43:05 +03003226 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003227 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003228 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003229 goto exit;
3230 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003231 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3232 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003233 if( ret != 0 )
3234 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003235 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003236 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003237 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003238
mohammad16038481e742018-03-18 13:57:31 +02003239 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003240 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003241
Moran Peker395db872018-05-31 14:07:14 +03003242exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003243 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003244 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003245 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003246}
3247
Gilles Peskinefe119512018-07-08 21:39:34 +02003248psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3249 const unsigned char *iv,
3250 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003251{
itayzafrir534bd7c2018-08-02 13:56:32 +03003252 psa_status_t status;
3253 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003254 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003255 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003256 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003257 }
Moran Pekera28258c2018-05-29 16:25:04 +03003258 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003259 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003260 status = PSA_ERROR_INVALID_ARGUMENT;
3261 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003262 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003263 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3264 status = mbedtls_to_psa_error( ret );
3265exit:
3266 if( status == PSA_SUCCESS )
3267 operation->iv_set = 1;
3268 else
mohammad1603503973b2018-03-12 15:59:30 +02003269 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003270 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003271}
3272
Gilles Peskinee553c652018-06-04 16:22:46 +02003273psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3274 const uint8_t *input,
3275 size_t input_length,
3276 unsigned char *output,
3277 size_t output_size,
3278 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003279{
itayzafrir534bd7c2018-08-02 13:56:32 +03003280 psa_status_t status;
3281 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003282 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003283
3284 if( operation->alg == 0 )
3285 {
3286 return( PSA_ERROR_BAD_STATE );
3287 }
3288
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003289 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003290 {
3291 /* Take the unprocessed partial block left over from previous
3292 * update calls, if any, plus the input to this call. Remove
3293 * the last partial block, if any. You get the data that will be
3294 * output in this call. */
3295 expected_output_size =
3296 ( operation->ctx.cipher.unprocessed_len + input_length )
3297 / operation->block_size * operation->block_size;
3298 }
3299 else
3300 {
3301 expected_output_size = input_length;
3302 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003303
Gilles Peskine89d789c2018-06-04 17:17:16 +02003304 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003305 {
3306 status = PSA_ERROR_BUFFER_TOO_SMALL;
3307 goto exit;
3308 }
mohammad160382759612018-03-12 18:16:40 +02003309
mohammad1603503973b2018-03-12 15:59:30 +02003310 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003311 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003312 status = mbedtls_to_psa_error( ret );
3313exit:
3314 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003315 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003316 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003317}
3318
Gilles Peskinee553c652018-06-04 16:22:46 +02003319psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3320 uint8_t *output,
3321 size_t output_size,
3322 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003323{
David Saadab4ecc272019-02-14 13:48:10 +02003324 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003325 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003326 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003327
mohammad1603503973b2018-03-12 15:59:30 +02003328 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003329 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003330 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003331 }
3332 if( operation->iv_required && ! operation->iv_set )
3333 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003334 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003335 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003336
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003337 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003338 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3339 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003340 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003341 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003342 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003343 }
3344
Janos Follath315b51c2018-07-09 16:04:51 +01003345 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3346 temp_output_buffer,
3347 output_length );
3348 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003349 {
Janos Follath315b51c2018-07-09 16:04:51 +01003350 status = mbedtls_to_psa_error( cipher_ret );
3351 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003352 }
Janos Follath315b51c2018-07-09 16:04:51 +01003353
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003354 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003355 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003356 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003357 memcpy( output, temp_output_buffer, *output_length );
3358 else
3359 {
Janos Follath315b51c2018-07-09 16:04:51 +01003360 status = PSA_ERROR_BUFFER_TOO_SMALL;
3361 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003362 }
mohammad1603503973b2018-03-12 15:59:30 +02003363
Gilles Peskine3f108122018-12-07 18:14:53 +01003364 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003365 status = psa_cipher_abort( operation );
3366
3367 return( status );
3368
3369error:
3370
3371 *output_length = 0;
3372
Gilles Peskine3f108122018-12-07 18:14:53 +01003373 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003374 (void) psa_cipher_abort( operation );
3375
3376 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003377}
3378
Gilles Peskinee553c652018-06-04 16:22:46 +02003379psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3380{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003381 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003382 {
3383 /* The object has (apparently) been initialized but it is not
3384 * in use. It's ok to call abort on such an object, and there's
3385 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003386 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003387 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003388
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003389 /* Sanity check (shouldn't happen: operation->alg should
3390 * always have been initialized to a valid value). */
3391 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3392 return( PSA_ERROR_BAD_STATE );
3393
mohammad1603503973b2018-03-12 15:59:30 +02003394 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003395
Moran Peker41deec42018-04-04 15:43:05 +03003396 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003397 operation->key_set = 0;
3398 operation->iv_set = 0;
3399 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003400 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003401 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003402
Moran Peker395db872018-05-31 14:07:14 +03003403 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003404}
3405
Gilles Peskinea0655c32018-04-30 17:06:50 +02003406
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003407
mohammad16038cc1cee2018-03-28 01:21:33 +03003408/****************************************************************/
3409/* Key Policy */
3410/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003411
mohammad160327010052018-07-03 13:16:15 +03003412#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003413void psa_key_policy_set_usage( psa_key_policy_t *policy,
3414 psa_key_usage_t usage,
3415 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003416{
mohammad16034eed7572018-03-28 05:14:59 -07003417 policy->usage = usage;
3418 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003419}
3420
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003421psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003422{
mohammad16036df908f2018-04-02 08:34:15 -07003423 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003424}
3425
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003426psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003427{
mohammad16036df908f2018-04-02 08:34:15 -07003428 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003429}
mohammad160327010052018-07-03 13:16:15 +03003430#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003431
Gilles Peskinec5487a82018-12-03 18:08:14 +01003432psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003433 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003434{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003435 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003436 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003437
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003438 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003439 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003440
Gilles Peskinec5487a82018-12-03 18:08:14 +01003441 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003442 if( status != PSA_SUCCESS )
3443 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003444
Gilles Peskine4747d192019-04-17 15:05:45 +02003445 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003446}
3447
Gilles Peskinec5487a82018-12-03 18:08:14 +01003448psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003449 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003450{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003451 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003452 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003453
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003454 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003455 return( PSA_ERROR_INVALID_ARGUMENT );
3456
Gilles Peskinec5487a82018-12-03 18:08:14 +01003457 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003458 if( status != PSA_SUCCESS )
3459 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003460
mohammad16036df908f2018-04-02 08:34:15 -07003461 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003462
3463 return( PSA_SUCCESS );
3464}
Gilles Peskine20035e32018-02-03 22:44:14 +01003465
Gilles Peskinea0655c32018-04-30 17:06:50 +02003466
3467
mohammad1603804cd712018-03-20 22:44:08 +02003468/****************************************************************/
3469/* Key Lifetime */
3470/****************************************************************/
3471
Gilles Peskine87a5e562019-04-17 12:28:25 +02003472psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003473 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003474{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003475 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003476 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003477
Gilles Peskinec5487a82018-12-03 18:08:14 +01003478 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003479 if( status != PSA_SUCCESS )
3480 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003481
mohammad1603804cd712018-03-20 22:44:08 +02003482 *lifetime = slot->lifetime;
3483
3484 return( PSA_SUCCESS );
3485}
3486
Gilles Peskine20035e32018-02-03 22:44:14 +01003487
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003488
mohammad16035955c982018-04-26 00:53:03 +03003489/****************************************************************/
3490/* AEAD */
3491/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003492
Gilles Peskineedf9a652018-08-17 18:11:56 +02003493typedef struct
3494{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003495 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003496 const mbedtls_cipher_info_t *cipher_info;
3497 union
3498 {
3499#if defined(MBEDTLS_CCM_C)
3500 mbedtls_ccm_context ccm;
3501#endif /* MBEDTLS_CCM_C */
3502#if defined(MBEDTLS_GCM_C)
3503 mbedtls_gcm_context gcm;
3504#endif /* MBEDTLS_GCM_C */
3505 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003506 psa_algorithm_t core_alg;
3507 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003508 uint8_t tag_length;
3509} aead_operation_t;
3510
Gilles Peskine30a9e412019-01-14 18:36:12 +01003511static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003512{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003513 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003514 {
3515#if defined(MBEDTLS_CCM_C)
3516 case PSA_ALG_CCM:
3517 mbedtls_ccm_free( &operation->ctx.ccm );
3518 break;
3519#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003520#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003521 case PSA_ALG_GCM:
3522 mbedtls_gcm_free( &operation->ctx.gcm );
3523 break;
3524#endif /* MBEDTLS_GCM_C */
3525 }
3526}
3527
3528static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003529 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003530 psa_key_usage_t usage,
3531 psa_algorithm_t alg )
3532{
3533 psa_status_t status;
3534 size_t key_bits;
3535 mbedtls_cipher_id_t cipher_id;
3536
Gilles Peskinec5487a82018-12-03 18:08:14 +01003537 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003538 if( status != PSA_SUCCESS )
3539 return( status );
3540
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003541 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003542
3543 operation->cipher_info =
3544 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3545 &cipher_id );
3546 if( operation->cipher_info == NULL )
3547 return( PSA_ERROR_NOT_SUPPORTED );
3548
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003549 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003550 {
3551#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003552 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3553 operation->core_alg = PSA_ALG_CCM;
3554 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003555 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3556 return( PSA_ERROR_INVALID_ARGUMENT );
3557 mbedtls_ccm_init( &operation->ctx.ccm );
3558 status = mbedtls_to_psa_error(
3559 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3560 operation->slot->data.raw.data,
3561 (unsigned int) key_bits ) );
3562 if( status != 0 )
3563 goto cleanup;
3564 break;
3565#endif /* MBEDTLS_CCM_C */
3566
3567#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003568 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3569 operation->core_alg = PSA_ALG_GCM;
3570 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003571 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3572 return( PSA_ERROR_INVALID_ARGUMENT );
3573 mbedtls_gcm_init( &operation->ctx.gcm );
3574 status = mbedtls_to_psa_error(
3575 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3576 operation->slot->data.raw.data,
3577 (unsigned int) key_bits ) );
3578 break;
3579#endif /* MBEDTLS_GCM_C */
3580
3581 default:
3582 return( PSA_ERROR_NOT_SUPPORTED );
3583 }
3584
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003585 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3586 {
3587 status = PSA_ERROR_INVALID_ARGUMENT;
3588 goto cleanup;
3589 }
3590 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3591 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3592 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3593 * In both cases, mbedtls_xxx will validate the tag length below. */
3594
Gilles Peskineedf9a652018-08-17 18:11:56 +02003595 return( PSA_SUCCESS );
3596
3597cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003598 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003599 return( status );
3600}
3601
Gilles Peskinec5487a82018-12-03 18:08:14 +01003602psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003603 psa_algorithm_t alg,
3604 const uint8_t *nonce,
3605 size_t nonce_length,
3606 const uint8_t *additional_data,
3607 size_t additional_data_length,
3608 const uint8_t *plaintext,
3609 size_t plaintext_length,
3610 uint8_t *ciphertext,
3611 size_t ciphertext_size,
3612 size_t *ciphertext_length )
3613{
mohammad16035955c982018-04-26 00:53:03 +03003614 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003615 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003616 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003617
mohammad1603f08a5502018-06-03 15:05:47 +03003618 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003619
Gilles Peskinec5487a82018-12-03 18:08:14 +01003620 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003621 if( status != PSA_SUCCESS )
3622 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003623
Gilles Peskineedf9a652018-08-17 18:11:56 +02003624 /* For all currently supported modes, the tag is at the end of the
3625 * ciphertext. */
3626 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3627 {
3628 status = PSA_ERROR_BUFFER_TOO_SMALL;
3629 goto exit;
3630 }
3631 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003632
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003633#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003634 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003635 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003636 status = mbedtls_to_psa_error(
3637 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3638 MBEDTLS_GCM_ENCRYPT,
3639 plaintext_length,
3640 nonce, nonce_length,
3641 additional_data, additional_data_length,
3642 plaintext, ciphertext,
3643 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003644 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003645 else
3646#endif /* MBEDTLS_GCM_C */
3647#if defined(MBEDTLS_CCM_C)
3648 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003649 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003650 status = mbedtls_to_psa_error(
3651 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3652 plaintext_length,
3653 nonce, nonce_length,
3654 additional_data,
3655 additional_data_length,
3656 plaintext, ciphertext,
3657 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003658 }
mohammad16035c8845f2018-05-09 05:40:09 -07003659 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003660#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003661 {
mohammad1603554faad2018-06-03 15:07:38 +03003662 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003663 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003664
Gilles Peskineedf9a652018-08-17 18:11:56 +02003665 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3666 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003667
Gilles Peskineedf9a652018-08-17 18:11:56 +02003668exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003669 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003670 if( status == PSA_SUCCESS )
3671 *ciphertext_length = plaintext_length + operation.tag_length;
3672 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003673}
3674
Gilles Peskineee652a32018-06-01 19:23:52 +02003675/* Locate the tag in a ciphertext buffer containing the encrypted data
3676 * followed by the tag. Return the length of the part preceding the tag in
3677 * *plaintext_length. This is the size of the plaintext in modes where
3678 * the encrypted data has the same size as the plaintext, such as
3679 * CCM and GCM. */
3680static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3681 const uint8_t *ciphertext,
3682 size_t ciphertext_length,
3683 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003684 const uint8_t **p_tag )
3685{
3686 size_t payload_length;
3687 if( tag_length > ciphertext_length )
3688 return( PSA_ERROR_INVALID_ARGUMENT );
3689 payload_length = ciphertext_length - tag_length;
3690 if( payload_length > plaintext_size )
3691 return( PSA_ERROR_BUFFER_TOO_SMALL );
3692 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003693 return( PSA_SUCCESS );
3694}
3695
Gilles Peskinec5487a82018-12-03 18:08:14 +01003696psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003697 psa_algorithm_t alg,
3698 const uint8_t *nonce,
3699 size_t nonce_length,
3700 const uint8_t *additional_data,
3701 size_t additional_data_length,
3702 const uint8_t *ciphertext,
3703 size_t ciphertext_length,
3704 uint8_t *plaintext,
3705 size_t plaintext_size,
3706 size_t *plaintext_length )
3707{
mohammad16035955c982018-04-26 00:53:03 +03003708 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003709 aead_operation_t operation;
3710 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003711
Gilles Peskineee652a32018-06-01 19:23:52 +02003712 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003713
Gilles Peskinec5487a82018-12-03 18:08:14 +01003714 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003715 if( status != PSA_SUCCESS )
3716 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003717
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003718#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003719 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003720 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003721 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003722 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003723 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003724 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003725 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003726
Gilles Peskineedf9a652018-08-17 18:11:56 +02003727 status = mbedtls_to_psa_error(
3728 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3729 ciphertext_length - operation.tag_length,
3730 nonce, nonce_length,
3731 additional_data,
3732 additional_data_length,
3733 tag, operation.tag_length,
3734 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003735 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003736 else
3737#endif /* MBEDTLS_GCM_C */
3738#if defined(MBEDTLS_CCM_C)
3739 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003740 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003741 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003742 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003743 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003744 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003745 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003746
Gilles Peskineedf9a652018-08-17 18:11:56 +02003747 status = mbedtls_to_psa_error(
3748 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3749 ciphertext_length - operation.tag_length,
3750 nonce, nonce_length,
3751 additional_data,
3752 additional_data_length,
3753 ciphertext, plaintext,
3754 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003755 }
mohammad160339574652018-06-01 04:39:53 -07003756 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003757#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003758 {
mohammad1603554faad2018-06-03 15:07:38 +03003759 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003760 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003761
Gilles Peskineedf9a652018-08-17 18:11:56 +02003762 if( status != PSA_SUCCESS && plaintext_size != 0 )
3763 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003764
Gilles Peskineedf9a652018-08-17 18:11:56 +02003765exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003766 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003767 if( status == PSA_SUCCESS )
3768 *plaintext_length = ciphertext_length - operation.tag_length;
3769 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003770}
3771
Gilles Peskinea0655c32018-04-30 17:06:50 +02003772
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003773
Gilles Peskine20035e32018-02-03 22:44:14 +01003774/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003775/* Generators */
3776/****************************************************************/
3777
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003778#define HKDF_STATE_INIT 0 /* no input yet */
3779#define HKDF_STATE_STARTED 1 /* got salt */
3780#define HKDF_STATE_KEYED 2 /* got key */
3781#define HKDF_STATE_OUTPUT 3 /* output started */
3782
Gilles Peskine969c5d62019-01-16 15:53:06 +01003783static psa_algorithm_t psa_generator_get_kdf_alg(
3784 const psa_crypto_generator_t *generator )
3785{
3786 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3787 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3788 else
3789 return( generator->alg );
3790}
3791
3792
Gilles Peskineeab56e42018-07-12 17:12:33 +02003793psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3794{
3795 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003796 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3797 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003798 {
3799 /* The object has (apparently) been initialized but it is not
3800 * in use. It's ok to call abort on such an object, and there's
3801 * nothing to do. */
3802 }
3803 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003804 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003805 {
3806 if( generator->ctx.buffer.data != NULL )
3807 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003808 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003809 generator->ctx.buffer.size );
3810 mbedtls_free( generator->ctx.buffer.data );
3811 }
3812 }
3813 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003814#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003815 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003816 {
3817 mbedtls_free( generator->ctx.hkdf.info );
3818 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3819 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003820 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00003821 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003822 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003823 {
3824 if( generator->ctx.tls12_prf.key != NULL )
3825 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003826 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003827 generator->ctx.tls12_prf.key_len );
3828 mbedtls_free( generator->ctx.tls12_prf.key );
3829 }
Hanno Becker580fba12018-11-13 20:50:45 +00003830
3831 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3832 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003833 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003834 generator->ctx.tls12_prf.Ai_with_seed_len );
3835 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3836 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003837 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003838 else
3839#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003840 {
3841 status = PSA_ERROR_BAD_STATE;
3842 }
3843 memset( generator, 0, sizeof( *generator ) );
3844 return( status );
3845}
3846
Gilles Peskineeab56e42018-07-12 17:12:33 +02003847psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3848 size_t *capacity)
3849{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003850 if( generator->alg == 0 )
3851 {
3852 /* This is a blank generator. */
3853 return PSA_ERROR_BAD_STATE;
3854 }
3855
Gilles Peskineeab56e42018-07-12 17:12:33 +02003856 *capacity = generator->capacity;
3857 return( PSA_SUCCESS );
3858}
3859
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003860psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
3861 size_t capacity )
3862{
3863 if( generator->alg == 0 )
3864 return( PSA_ERROR_BAD_STATE );
3865 if( capacity > generator->capacity )
3866 return( PSA_ERROR_INVALID_ARGUMENT );
3867 generator->capacity = capacity;
3868 return( PSA_SUCCESS );
3869}
3870
Gilles Peskinebef7f142018-07-12 17:22:21 +02003871#if defined(MBEDTLS_MD_C)
3872/* Read some bytes from an HKDF-based generator. This performs a chunk
3873 * of the expand phase of the HKDF algorithm. */
3874static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3875 psa_algorithm_t hash_alg,
3876 uint8_t *output,
3877 size_t output_length )
3878{
3879 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3880 psa_status_t status;
3881
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003882 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3883 return( PSA_ERROR_BAD_STATE );
3884 hkdf->state = HKDF_STATE_OUTPUT;
3885
Gilles Peskinebef7f142018-07-12 17:22:21 +02003886 while( output_length != 0 )
3887 {
3888 /* Copy what remains of the current block */
3889 uint8_t n = hash_length - hkdf->offset_in_block;
3890 if( n > output_length )
3891 n = (uint8_t) output_length;
3892 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3893 output += n;
3894 output_length -= n;
3895 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003896 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003897 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003898 /* We can't be wanting more output after block 0xff, otherwise
3899 * the capacity check in psa_generator_read() would have
3900 * prevented this call. It could happen only if the generator
3901 * object was corrupted or if this function is called directly
3902 * inside the library. */
3903 if( hkdf->block_number == 0xff )
3904 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003905
3906 /* We need a new block */
3907 ++hkdf->block_number;
3908 hkdf->offset_in_block = 0;
3909 status = psa_hmac_setup_internal( &hkdf->hmac,
3910 hkdf->prk, hash_length,
3911 hash_alg );
3912 if( status != PSA_SUCCESS )
3913 return( status );
3914 if( hkdf->block_number != 1 )
3915 {
3916 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3917 hkdf->output_block,
3918 hash_length );
3919 if( status != PSA_SUCCESS )
3920 return( status );
3921 }
3922 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3923 hkdf->info,
3924 hkdf->info_length );
3925 if( status != PSA_SUCCESS )
3926 return( status );
3927 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3928 &hkdf->block_number, 1 );
3929 if( status != PSA_SUCCESS )
3930 return( status );
3931 status = psa_hmac_finish_internal( &hkdf->hmac,
3932 hkdf->output_block,
3933 sizeof( hkdf->output_block ) );
3934 if( status != PSA_SUCCESS )
3935 return( status );
3936 }
3937
3938 return( PSA_SUCCESS );
3939}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003940
3941static psa_status_t psa_generator_tls12_prf_generate_next_block(
3942 psa_tls12_prf_generator_t *tls12_prf,
3943 psa_algorithm_t alg )
3944{
3945 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3946 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3947 psa_hmac_internal_data hmac;
3948 psa_status_t status, cleanup_status;
3949
Hanno Becker3b339e22018-11-13 20:56:14 +00003950 unsigned char *Ai;
3951 size_t Ai_len;
3952
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003953 /* We can't be wanting more output after block 0xff, otherwise
3954 * the capacity check in psa_generator_read() would have
3955 * prevented this call. It could happen only if the generator
3956 * object was corrupted or if this function is called directly
3957 * inside the library. */
3958 if( tls12_prf->block_number == 0xff )
3959 return( PSA_ERROR_BAD_STATE );
3960
3961 /* We need a new block */
3962 ++tls12_prf->block_number;
3963 tls12_prf->offset_in_block = 0;
3964
3965 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3966 *
3967 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3968 *
3969 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3970 * HMAC_hash(secret, A(2) + seed) +
3971 * HMAC_hash(secret, A(3) + seed) + ...
3972 *
3973 * A(0) = seed
3974 * A(i) = HMAC_hash( secret, A(i-1) )
3975 *
3976 * The `psa_tls12_prf_generator` structures saves the block
3977 * `HMAC_hash(secret, A(i) + seed)` from which the output
3978 * is currently extracted as `output_block`, while
3979 * `A(i) + seed` is stored in `Ai_with_seed`.
3980 *
3981 * Generating a new block means recalculating `Ai_with_seed`
3982 * from the A(i)-part of it, and afterwards recalculating
3983 * `output_block`.
3984 *
3985 * A(0) is computed at setup time.
3986 *
3987 */
3988
3989 psa_hmac_init_internal( &hmac );
3990
3991 /* We must distinguish the calculation of A(1) from those
3992 * of A(2) and higher, because A(0)=seed has a different
3993 * length than the other A(i). */
3994 if( tls12_prf->block_number == 1 )
3995 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003996 Ai = tls12_prf->Ai_with_seed + hash_length;
3997 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003998 }
3999 else
4000 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004001 Ai = tls12_prf->Ai_with_seed;
4002 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004003 }
4004
Hanno Becker3b339e22018-11-13 20:56:14 +00004005 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4006 status = psa_hmac_setup_internal( &hmac,
4007 tls12_prf->key,
4008 tls12_prf->key_len,
4009 hash_alg );
4010 if( status != PSA_SUCCESS )
4011 goto cleanup;
4012
4013 status = psa_hash_update( &hmac.hash_ctx,
4014 Ai, Ai_len );
4015 if( status != PSA_SUCCESS )
4016 goto cleanup;
4017
4018 status = psa_hmac_finish_internal( &hmac,
4019 tls12_prf->Ai_with_seed,
4020 hash_length );
4021 if( status != PSA_SUCCESS )
4022 goto cleanup;
4023
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004024 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4025 status = psa_hmac_setup_internal( &hmac,
4026 tls12_prf->key,
4027 tls12_prf->key_len,
4028 hash_alg );
4029 if( status != PSA_SUCCESS )
4030 goto cleanup;
4031
4032 status = psa_hash_update( &hmac.hash_ctx,
4033 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004034 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004035 if( status != PSA_SUCCESS )
4036 goto cleanup;
4037
4038 status = psa_hmac_finish_internal( &hmac,
4039 tls12_prf->output_block,
4040 hash_length );
4041 if( status != PSA_SUCCESS )
4042 goto cleanup;
4043
4044cleanup:
4045
4046 cleanup_status = psa_hmac_abort_internal( &hmac );
4047 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4048 status = cleanup_status;
4049
4050 return( status );
4051}
4052
4053/* Read some bytes from an TLS-1.2-PRF-based generator.
4054 * See Section 5 of RFC 5246. */
4055static psa_status_t psa_generator_tls12_prf_read(
4056 psa_tls12_prf_generator_t *tls12_prf,
4057 psa_algorithm_t alg,
4058 uint8_t *output,
4059 size_t output_length )
4060{
4061 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4062 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4063 psa_status_t status;
4064
4065 while( output_length != 0 )
4066 {
4067 /* Copy what remains of the current block */
4068 uint8_t n = hash_length - tls12_prf->offset_in_block;
4069
4070 /* Check if we have fully processed the current block. */
4071 if( n == 0 )
4072 {
4073 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
4074 alg );
4075 if( status != PSA_SUCCESS )
4076 return( status );
4077
4078 continue;
4079 }
4080
4081 if( n > output_length )
4082 n = (uint8_t) output_length;
4083 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4084 n );
4085 output += n;
4086 output_length -= n;
4087 tls12_prf->offset_in_block += n;
4088 }
4089
4090 return( PSA_SUCCESS );
4091}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004092#endif /* MBEDTLS_MD_C */
4093
Gilles Peskineeab56e42018-07-12 17:12:33 +02004094psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
4095 uint8_t *output,
4096 size_t output_length )
4097{
4098 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004099 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004100
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004101 if( generator->alg == 0 )
4102 {
4103 /* This is a blank generator. */
4104 return PSA_ERROR_BAD_STATE;
4105 }
4106
Gilles Peskineeab56e42018-07-12 17:12:33 +02004107 if( output_length > generator->capacity )
4108 {
4109 generator->capacity = 0;
4110 /* Go through the error path to wipe all confidential data now
4111 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004112 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004113 goto exit;
4114 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004115 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004116 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004117 /* Edge case: this is a finished generator, and 0 bytes
4118 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004119 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4120 * INSUFFICIENT_CAPACITY, which is right for a finished
4121 * generator, for consistency with the case when
4122 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004123 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004124 }
4125 generator->capacity -= output_length;
4126
Gilles Peskine969c5d62019-01-16 15:53:06 +01004127 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004128 {
Gilles Peskine211a4362018-10-25 22:22:31 +02004129 /* Initially, the capacity of a selection generator is always
4130 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
4131 * abbreviated in this comment as `size`. When the remaining
4132 * capacity is `c`, the next bytes to serve start `c` bytes
4133 * from the end of the buffer, i.e. `size - c` from the
4134 * beginning of the buffer. Since `generator->capacity` was just
4135 * decremented above, we need to serve the bytes from
4136 * `size - generator->capacity - output_length` to
4137 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004138 size_t offset =
4139 generator->ctx.buffer.size - generator->capacity - output_length;
4140 memcpy( output, generator->ctx.buffer.data + offset, output_length );
4141 status = PSA_SUCCESS;
4142 }
4143 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004144#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004145 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004146 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004147 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004148 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
4149 output, output_length );
4150 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004151 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4152 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004153 {
4154 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004155 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004156 output_length );
4157 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004158 else
4159#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004160 {
4161 return( PSA_ERROR_BAD_STATE );
4162 }
4163
4164exit:
4165 if( status != PSA_SUCCESS )
4166 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004167 /* Preserve the algorithm upon errors, but clear all sensitive state.
4168 * This allows us to differentiate between exhausted generators and
4169 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
4170 * generators. */
4171 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004172 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004173 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004174 memset( output, '!', output_length );
4175 }
4176 return( status );
4177}
4178
Gilles Peskine08542d82018-07-19 17:05:42 +02004179#if defined(MBEDTLS_DES_C)
4180static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4181{
4182 if( data_size >= 8 )
4183 mbedtls_des_key_set_parity( data );
4184 if( data_size >= 16 )
4185 mbedtls_des_key_set_parity( data + 8 );
4186 if( data_size >= 24 )
4187 mbedtls_des_key_set_parity( data + 16 );
4188}
4189#endif /* MBEDTLS_DES_C */
4190
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004191static psa_status_t psa_generator_import_key_internal(
4192 psa_key_slot_t *slot,
4193 size_t bits,
4194 psa_crypto_generator_t *generator )
4195{
4196 uint8_t *data = NULL;
4197 size_t bytes = PSA_BITS_TO_BYTES( bits );
4198 psa_status_t status;
4199
4200 if( ! key_type_is_raw_bytes( slot->type ) )
4201 return( PSA_ERROR_INVALID_ARGUMENT );
4202 if( bits % 8 != 0 )
4203 return( PSA_ERROR_INVALID_ARGUMENT );
4204 data = mbedtls_calloc( 1, bytes );
4205 if( data == NULL )
4206 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4207
4208 status = psa_generator_read( generator, data, bytes );
4209 if( status != PSA_SUCCESS )
4210 goto exit;
4211#if defined(MBEDTLS_DES_C)
4212 if( slot->type == PSA_KEY_TYPE_DES )
4213 psa_des_set_key_parity( data, bytes );
4214#endif /* MBEDTLS_DES_C */
4215 status = psa_import_key_into_slot( slot, data, bytes );
4216
4217exit:
4218 mbedtls_free( data );
4219 return( status );
4220}
4221
4222psa_status_t psa_generator_import_key( const psa_key_attributes_t *attributes,
4223 psa_key_handle_t *handle,
4224 size_t bits,
4225 psa_crypto_generator_t *generator )
4226{
4227 psa_status_t status;
4228 psa_key_slot_t *slot = NULL;
4229 status = psa_start_key_creation( attributes, handle, &slot );
4230 if( status == PSA_SUCCESS )
4231 {
4232 status = psa_generator_import_key_internal( slot, bits, generator );
4233 }
4234 if( status == PSA_SUCCESS )
4235 status = psa_finish_key_creation( slot );
4236 if( status != PSA_SUCCESS )
4237 {
4238 psa_fail_key_creation( slot );
4239 *handle = 0;
4240 }
4241 return( status );
4242}
4243
Gilles Peskine87a5e562019-04-17 12:28:25 +02004244psa_status_t psa_generator_import_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004245 psa_key_type_t type,
4246 size_t bits,
4247 psa_crypto_generator_t *generator )
4248{
4249 uint8_t *data = NULL;
4250 size_t bytes = PSA_BITS_TO_BYTES( bits );
4251 psa_status_t status;
4252
4253 if( ! key_type_is_raw_bytes( type ) )
4254 return( PSA_ERROR_INVALID_ARGUMENT );
4255 if( bits % 8 != 0 )
4256 return( PSA_ERROR_INVALID_ARGUMENT );
4257 data = mbedtls_calloc( 1, bytes );
4258 if( data == NULL )
4259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4260
4261 status = psa_generator_read( generator, data, bytes );
4262 if( status != PSA_SUCCESS )
4263 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004264#if defined(MBEDTLS_DES_C)
4265 if( type == PSA_KEY_TYPE_DES )
4266 psa_des_set_key_parity( data, bytes );
4267#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004268 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004269
4270exit:
4271 mbedtls_free( data );
4272 return( status );
4273}
4274
4275
4276
4277/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004278/* Key derivation */
4279/****************************************************************/
4280
Gilles Peskinea05219c2018-11-16 16:02:56 +01004281#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004282/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004283 * of the HKDF algorithm.
4284 *
4285 * Note that if this function fails, you must call psa_generator_abort()
4286 * to potentially free embedded data structures and wipe confidential data.
4287 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004288static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004289 const uint8_t *secret,
4290 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004291 psa_algorithm_t hash_alg,
4292 const uint8_t *salt,
4293 size_t salt_length,
4294 const uint8_t *label,
4295 size_t label_length )
4296{
4297 psa_status_t status;
4298 status = psa_hmac_setup_internal( &hkdf->hmac,
4299 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004300 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004301 if( status != PSA_SUCCESS )
4302 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004303 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004304 if( status != PSA_SUCCESS )
4305 return( status );
4306 status = psa_hmac_finish_internal( &hkdf->hmac,
4307 hkdf->prk,
4308 sizeof( hkdf->prk ) );
4309 if( status != PSA_SUCCESS )
4310 return( status );
4311 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4312 hkdf->block_number = 0;
4313 hkdf->info_length = label_length;
4314 if( label_length != 0 )
4315 {
4316 hkdf->info = mbedtls_calloc( 1, label_length );
4317 if( hkdf->info == NULL )
4318 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4319 memcpy( hkdf->info, label, label_length );
4320 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004321 hkdf->state = HKDF_STATE_KEYED;
4322 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004323 return( PSA_SUCCESS );
4324}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004325#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004326
Gilles Peskinea05219c2018-11-16 16:02:56 +01004327#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004328/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4329 *
4330 * Note that if this function fails, you must call psa_generator_abort()
4331 * to potentially free embedded data structures and wipe confidential data.
4332 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004333static psa_status_t psa_generator_tls12_prf_setup(
4334 psa_tls12_prf_generator_t *tls12_prf,
4335 const unsigned char *key,
4336 size_t key_len,
4337 psa_algorithm_t hash_alg,
4338 const uint8_t *salt,
4339 size_t salt_length,
4340 const uint8_t *label,
4341 size_t label_length )
4342{
4343 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004344 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4345 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004346
4347 tls12_prf->key = mbedtls_calloc( 1, key_len );
4348 if( tls12_prf->key == NULL )
4349 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4350 tls12_prf->key_len = key_len;
4351 memcpy( tls12_prf->key, key, key_len );
4352
Hanno Becker580fba12018-11-13 20:50:45 +00004353 overflow = ( salt_length + label_length < salt_length ) ||
4354 ( salt_length + label_length + hash_length < hash_length );
4355 if( overflow )
4356 return( PSA_ERROR_INVALID_ARGUMENT );
4357
4358 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4359 if( tls12_prf->Ai_with_seed == NULL )
4360 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4361 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4362
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004363 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4364 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004365 if( label_length != 0 )
4366 {
4367 memcpy( tls12_prf->Ai_with_seed + hash_length,
4368 label, label_length );
4369 }
4370
4371 if( salt_length != 0 )
4372 {
4373 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4374 salt, salt_length );
4375 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004376
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004377 /* The first block gets generated when
4378 * psa_generator_read() is called. */
4379 tls12_prf->block_number = 0;
4380 tls12_prf->offset_in_block = hash_length;
4381
4382 return( PSA_SUCCESS );
4383}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004384
4385/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4386static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4387 psa_tls12_prf_generator_t *tls12_prf,
4388 const unsigned char *psk,
4389 size_t psk_len,
4390 psa_algorithm_t hash_alg,
4391 const uint8_t *salt,
4392 size_t salt_length,
4393 const uint8_t *label,
4394 size_t label_length )
4395{
4396 psa_status_t status;
4397 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4398
4399 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4400 return( PSA_ERROR_INVALID_ARGUMENT );
4401
4402 /* Quoting RFC 4279, Section 2:
4403 *
4404 * The premaster secret is formed as follows: if the PSK is N octets
4405 * long, concatenate a uint16 with the value N, N zero octets, a second
4406 * uint16 with the value N, and the PSK itself.
4407 */
4408
4409 pms[0] = ( psk_len >> 8 ) & 0xff;
4410 pms[1] = ( psk_len >> 0 ) & 0xff;
4411 memset( pms + 2, 0, psk_len );
4412 pms[2 + psk_len + 0] = pms[0];
4413 pms[2 + psk_len + 1] = pms[1];
4414 memcpy( pms + 4 + psk_len, psk, psk_len );
4415
4416 status = psa_generator_tls12_prf_setup( tls12_prf,
4417 pms, 4 + 2 * psk_len,
4418 hash_alg,
4419 salt, salt_length,
4420 label, label_length );
4421
Gilles Peskine3f108122018-12-07 18:14:53 +01004422 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004423 return( status );
4424}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004425#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004426
Gilles Peskine346797d2018-11-16 16:05:06 +01004427/* Note that if this function fails, you must call psa_generator_abort()
4428 * to potentially free embedded data structures and wipe confidential data.
4429 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004430static psa_status_t psa_key_derivation_internal(
4431 psa_crypto_generator_t *generator,
4432 const uint8_t *secret, size_t secret_length,
4433 psa_algorithm_t alg,
4434 const uint8_t *salt, size_t salt_length,
4435 const uint8_t *label, size_t label_length,
4436 size_t capacity )
4437{
4438 psa_status_t status;
4439 size_t max_capacity;
4440
4441 /* Set generator->alg even on failure so that abort knows what to do. */
4442 generator->alg = alg;
4443
Gilles Peskine751d9652018-09-18 12:05:44 +02004444 if( alg == PSA_ALG_SELECT_RAW )
4445 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004446 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004447 if( salt_length != 0 )
4448 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004449 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004450 if( label_length != 0 )
4451 return( PSA_ERROR_INVALID_ARGUMENT );
4452 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4453 if( generator->ctx.buffer.data == NULL )
4454 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4455 memcpy( generator->ctx.buffer.data, secret, secret_length );
4456 generator->ctx.buffer.size = secret_length;
4457 max_capacity = secret_length;
4458 status = PSA_SUCCESS;
4459 }
4460 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004461#if defined(MBEDTLS_MD_C)
4462 if( PSA_ALG_IS_HKDF( alg ) )
4463 {
4464 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4465 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4466 if( hash_size == 0 )
4467 return( PSA_ERROR_NOT_SUPPORTED );
4468 max_capacity = 255 * hash_size;
4469 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4470 secret, secret_length,
4471 hash_alg,
4472 salt, salt_length,
4473 label, label_length );
4474 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004475 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4476 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4477 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004478 {
4479 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4480 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4481
4482 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4483 if( hash_alg != PSA_ALG_SHA_256 &&
4484 hash_alg != PSA_ALG_SHA_384 )
4485 {
4486 return( PSA_ERROR_NOT_SUPPORTED );
4487 }
4488
4489 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004490
4491 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4492 {
4493 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4494 secret, secret_length,
4495 hash_alg, salt, salt_length,
4496 label, label_length );
4497 }
4498 else
4499 {
4500 status = psa_generator_tls12_psk_to_ms_setup(
4501 &generator->ctx.tls12_prf,
4502 secret, secret_length,
4503 hash_alg, salt, salt_length,
4504 label, label_length );
4505 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004506 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004507 else
4508#endif
4509 {
4510 return( PSA_ERROR_NOT_SUPPORTED );
4511 }
4512
4513 if( status != PSA_SUCCESS )
4514 return( status );
4515
4516 if( capacity <= max_capacity )
4517 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004518 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4519 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004520 else
4521 return( PSA_ERROR_INVALID_ARGUMENT );
4522
4523 return( PSA_SUCCESS );
4524}
4525
Gilles Peskineea0fb492018-07-12 17:17:20 +02004526psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004527 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004528 psa_algorithm_t alg,
4529 const uint8_t *salt,
4530 size_t salt_length,
4531 const uint8_t *label,
4532 size_t label_length,
4533 size_t capacity )
4534{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004535 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004536 psa_status_t status;
4537
4538 if( generator->alg != 0 )
4539 return( PSA_ERROR_BAD_STATE );
4540
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004541 /* Make sure that alg is a key derivation algorithm. This prevents
4542 * key selection algorithms, which psa_key_derivation_internal
4543 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004544 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4545 return( PSA_ERROR_INVALID_ARGUMENT );
4546
Gilles Peskinec5487a82018-12-03 18:08:14 +01004547 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004548 if( status != PSA_SUCCESS )
4549 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004550
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004551 if( slot->type != PSA_KEY_TYPE_DERIVE )
4552 return( PSA_ERROR_INVALID_ARGUMENT );
4553
4554 status = psa_key_derivation_internal( generator,
4555 slot->data.raw.data,
4556 slot->data.raw.bytes,
4557 alg,
4558 salt, salt_length,
4559 label, label_length,
4560 capacity );
4561 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004562 psa_generator_abort( generator );
4563 return( status );
4564}
4565
Gilles Peskine969c5d62019-01-16 15:53:06 +01004566static psa_status_t psa_key_derivation_setup_kdf(
4567 psa_crypto_generator_t *generator,
4568 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004569{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004570 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004571#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004572 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4573 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4574 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004575 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004576 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004577 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4578 if( hash_size == 0 )
4579 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004580 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4581 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004582 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004583 {
4584 return( PSA_ERROR_NOT_SUPPORTED );
4585 }
4586 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004587 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004588 }
4589#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004590 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004591 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004592}
4593
4594psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4595 psa_algorithm_t alg )
4596{
4597 psa_status_t status;
4598
4599 if( generator->alg != 0 )
4600 return( PSA_ERROR_BAD_STATE );
4601
Gilles Peskine6843c292019-01-18 16:44:49 +01004602 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4603 return( PSA_ERROR_INVALID_ARGUMENT );
4604 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004605 {
4606 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004607 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004608 }
4609 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4610 {
4611 status = psa_key_derivation_setup_kdf( generator, alg );
4612 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004613 else
4614 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004615
4616 if( status == PSA_SUCCESS )
4617 generator->alg = alg;
4618 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004619}
4620
4621#if defined(MBEDTLS_MD_C)
4622static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4623 psa_algorithm_t hash_alg,
4624 psa_key_derivation_step_t step,
4625 const uint8_t *data,
4626 size_t data_length )
4627{
4628 psa_status_t status;
4629 switch( step )
4630 {
4631 case PSA_KDF_STEP_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004632 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004633 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004634 status = psa_hmac_setup_internal( &hkdf->hmac,
4635 data, data_length,
4636 hash_alg );
4637 if( status != PSA_SUCCESS )
4638 return( status );
4639 hkdf->state = HKDF_STATE_STARTED;
4640 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004641 case PSA_KDF_STEP_SECRET:
4642 /* If no salt was provided, use an empty salt. */
4643 if( hkdf->state == HKDF_STATE_INIT )
4644 {
4645 status = psa_hmac_setup_internal( &hkdf->hmac,
4646 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004647 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004648 if( status != PSA_SUCCESS )
4649 return( status );
4650 hkdf->state = HKDF_STATE_STARTED;
4651 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004652 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004653 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004654 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4655 data, data_length );
4656 if( status != PSA_SUCCESS )
4657 return( status );
4658 status = psa_hmac_finish_internal( &hkdf->hmac,
4659 hkdf->prk,
4660 sizeof( hkdf->prk ) );
4661 if( status != PSA_SUCCESS )
4662 return( status );
4663 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4664 hkdf->block_number = 0;
4665 hkdf->state = HKDF_STATE_KEYED;
4666 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004667 case PSA_KDF_STEP_INFO:
4668 if( hkdf->state == HKDF_STATE_OUTPUT )
4669 return( PSA_ERROR_BAD_STATE );
4670 if( hkdf->info_set )
4671 return( PSA_ERROR_BAD_STATE );
4672 hkdf->info_length = data_length;
4673 if( data_length != 0 )
4674 {
4675 hkdf->info = mbedtls_calloc( 1, data_length );
4676 if( hkdf->info == NULL )
4677 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4678 memcpy( hkdf->info, data, data_length );
4679 }
4680 hkdf->info_set = 1;
4681 return( PSA_SUCCESS );
4682 default:
4683 return( PSA_ERROR_INVALID_ARGUMENT );
4684 }
4685}
4686#endif /* MBEDTLS_MD_C */
4687
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004688static psa_status_t psa_key_derivation_input_raw(
4689 psa_crypto_generator_t *generator,
4690 psa_key_derivation_step_t step,
4691 const uint8_t *data,
4692 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004693{
4694 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004695 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004696
Gilles Peskine969c5d62019-01-16 15:53:06 +01004697 if( kdf_alg == PSA_ALG_SELECT_RAW )
4698 {
4699 if( generator->capacity != 0 )
4700 return( PSA_ERROR_INVALID_ARGUMENT );
4701 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4702 if( generator->ctx.buffer.data == NULL )
4703 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4704 memcpy( generator->ctx.buffer.data, data, data_length );
4705 generator->ctx.buffer.size = data_length;
4706 generator->capacity = data_length;
4707 status = PSA_SUCCESS;
4708 }
4709 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004710#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004711 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004712 {
4713 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004714 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004715 step, data, data_length );
4716 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004717 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004718#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004719#if defined(MBEDTLS_MD_C)
4720 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004721 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4722 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004723 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004724 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004725 status = PSA_ERROR_NOT_SUPPORTED;
4726 }
4727 else
4728#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004729 {
4730 /* This can't happen unless the generator object was not initialized */
4731 return( PSA_ERROR_BAD_STATE );
4732 }
4733
4734 if( status != PSA_SUCCESS )
4735 psa_generator_abort( generator );
4736 return( status );
4737}
4738
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004739psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4740 psa_key_derivation_step_t step,
4741 const uint8_t *data,
4742 size_t data_length )
4743{
4744 switch( step )
4745 {
4746 case PSA_KDF_STEP_LABEL:
4747 case PSA_KDF_STEP_SALT:
4748 case PSA_KDF_STEP_INFO:
4749 return( psa_key_derivation_input_raw( generator, step,
4750 data, data_length ) );
4751 default:
4752 return( PSA_ERROR_INVALID_ARGUMENT );
4753 }
4754}
4755
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004756psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4757 psa_key_derivation_step_t step,
4758 psa_key_handle_t handle )
4759{
4760 psa_key_slot_t *slot;
4761 psa_status_t status;
4762 status = psa_get_key_from_slot( handle, &slot,
4763 PSA_KEY_USAGE_DERIVE,
4764 generator->alg );
4765 if( status != PSA_SUCCESS )
4766 return( status );
4767 if( slot->type != PSA_KEY_TYPE_DERIVE )
4768 return( PSA_ERROR_INVALID_ARGUMENT );
4769 /* Don't allow a key to be used as an input that is usually public.
4770 * This is debatable. It's ok from a cryptographic perspective to
4771 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004772 * the material should be dedicated to a particular input step,
4773 * otherwise this may allow the key to be used in an unintended way
4774 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004775 if( step != PSA_KDF_STEP_SECRET )
4776 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004777 return( psa_key_derivation_input_raw( generator,
4778 step,
4779 slot->data.raw.data,
4780 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004781}
4782
Gilles Peskineea0fb492018-07-12 17:17:20 +02004783
4784
4785/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004786/* Key agreement */
4787/****************************************************************/
4788
Gilles Peskinea05219c2018-11-16 16:02:56 +01004789#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004790static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4791 size_t peer_key_length,
4792 const mbedtls_ecp_keypair *our_key,
4793 uint8_t *shared_secret,
4794 size_t shared_secret_size,
4795 size_t *shared_secret_length )
4796{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004797 mbedtls_ecp_keypair *their_key = NULL;
4798 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004799 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004800 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004801
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004802 status = psa_import_ec_public_key(
4803 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4804 peer_key, peer_key_length,
4805 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004806 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004807 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004808
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004809 status = mbedtls_to_psa_error(
4810 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4811 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004812 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004813 status = mbedtls_to_psa_error(
4814 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4815 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004816 goto exit;
4817
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004818 status = mbedtls_to_psa_error(
4819 mbedtls_ecdh_calc_secret( &ecdh,
4820 shared_secret_length,
4821 shared_secret, shared_secret_size,
4822 mbedtls_ctr_drbg_random,
4823 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004824
4825exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004826 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004827 mbedtls_ecp_keypair_free( their_key );
4828 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004829 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004830}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004831#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004832
Gilles Peskine01d718c2018-09-18 12:01:02 +02004833#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4834
Gilles Peskine0216fe12019-04-11 21:23:21 +02004835static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4836 psa_key_slot_t *private_key,
4837 const uint8_t *peer_key,
4838 size_t peer_key_length,
4839 uint8_t *shared_secret,
4840 size_t shared_secret_size,
4841 size_t *shared_secret_length )
4842{
4843 switch( alg )
4844 {
4845#if defined(MBEDTLS_ECDH_C)
4846 case PSA_ALG_ECDH:
4847 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4848 return( PSA_ERROR_INVALID_ARGUMENT );
4849 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
4850 private_key->data.ecp,
4851 shared_secret, shared_secret_size,
4852 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004853#endif /* MBEDTLS_ECDH_C */
4854 default:
4855 (void) private_key;
4856 (void) peer_key;
4857 (void) peer_key_length;
4858 (void) shared_secret;
4859 (void) shared_secret_size;
4860 (void) shared_secret_length;
4861 return( PSA_ERROR_NOT_SUPPORTED );
4862 }
4863}
4864
Gilles Peskine346797d2018-11-16 16:05:06 +01004865/* Note that if this function fails, you must call psa_generator_abort()
4866 * to potentially free embedded data structures and wipe confidential data.
4867 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004868static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004869 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004870 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004871 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004872 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004873{
4874 psa_status_t status;
4875 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4876 size_t shared_secret_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004877 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004878
4879 /* Step 1: run the secret agreement algorithm to generate the shared
4880 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004881 status = psa_key_agreement_raw_internal( ka_alg,
4882 private_key,
4883 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004884 shared_secret,
4885 sizeof( shared_secret ),
4886 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004887 if( status != PSA_SUCCESS )
4888 goto exit;
4889
4890 /* Step 2: set up the key derivation to generate key material from
4891 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004892 status = psa_key_derivation_input_raw( generator, step,
4893 shared_secret, shared_secret_length );
4894
Gilles Peskine01d718c2018-09-18 12:01:02 +02004895exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004896 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004897 return( status );
4898}
4899
4900psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004901 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004902 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004903 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004904 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004905{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004906 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004907 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004908 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004909 return( PSA_ERROR_INVALID_ARGUMENT );
4910 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004911 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004912 if( status != PSA_SUCCESS )
4913 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004914 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004915 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004916 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004917 if( status != PSA_SUCCESS )
4918 psa_generator_abort( generator );
4919 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004920}
4921
Gilles Peskine0216fe12019-04-11 21:23:21 +02004922psa_status_t psa_key_agreement_raw_shared_secret( psa_algorithm_t alg,
4923 psa_key_handle_t private_key,
4924 const uint8_t *peer_key,
4925 size_t peer_key_length,
4926 uint8_t *output,
4927 size_t output_size,
4928 size_t *output_length )
4929{
4930 psa_key_slot_t *slot;
4931 psa_status_t status;
4932
4933 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4934 {
4935 status = PSA_ERROR_INVALID_ARGUMENT;
4936 goto exit;
4937 }
4938 status = psa_get_key_from_slot( private_key, &slot,
4939 PSA_KEY_USAGE_DERIVE, alg );
4940 if( status != PSA_SUCCESS )
4941 goto exit;
4942
4943 status = psa_key_agreement_raw_internal( alg, slot,
4944 peer_key, peer_key_length,
4945 output, output_size,
4946 output_length );
4947
4948exit:
4949 if( status != PSA_SUCCESS )
4950 {
4951 /* If an error happens and is not handled properly, the output
4952 * may be used as a key to protect sensitive data. Arrange for such
4953 * a key to be random, which is likely to result in decryption or
4954 * verification errors. This is better than filling the buffer with
4955 * some constant data such as zeros, which would result in the data
4956 * being protected with a reproducible, easily knowable key.
4957 */
4958 psa_generate_random( output, output_size );
4959 *output_length = output_size;
4960 }
4961 return( status );
4962}
Gilles Peskine01d718c2018-09-18 12:01:02 +02004963
4964
4965/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004966/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004967/****************************************************************/
4968
4969psa_status_t psa_generate_random( uint8_t *output,
4970 size_t output_size )
4971{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004972 int ret;
4973 GUARD_MODULE_INITIALIZED;
4974
4975 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004976 return( mbedtls_to_psa_error( ret ) );
4977}
4978
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004979#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
4980#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02004981
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004982psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4983 size_t seed_size )
4984{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004985 if( global_data.initialized )
4986 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004987
4988 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4989 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4990 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4991 return( PSA_ERROR_INVALID_ARGUMENT );
4992
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004993 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004994}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004995#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004996
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004997static psa_status_t psa_generate_key_internal( psa_key_slot_t *slot,
4998 size_t bits,
4999 const void *extra,
5000 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005001{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005002 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005003
Gilles Peskine53d991e2018-07-12 01:14:59 +02005004 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005005 return( PSA_ERROR_INVALID_ARGUMENT );
5006
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005007 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005008 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005009 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005010 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005011 if( status != PSA_SUCCESS )
5012 return( status );
5013 status = psa_generate_random( slot->data.raw.data,
5014 slot->data.raw.bytes );
5015 if( status != PSA_SUCCESS )
5016 {
5017 mbedtls_free( slot->data.raw.data );
5018 return( status );
5019 }
5020#if defined(MBEDTLS_DES_C)
5021 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005022 psa_des_set_key_parity( slot->data.raw.data,
5023 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005024#endif /* MBEDTLS_DES_C */
5025 }
5026 else
5027
5028#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5029 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
5030 {
5031 mbedtls_rsa_context *rsa;
5032 int ret;
5033 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005034 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5035 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005036 /* Accept only byte-aligned keys, for the same reasons as
5037 * in psa_import_rsa_key(). */
5038 if( bits % 8 != 0 )
5039 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005040 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005041 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02005042 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02005043 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005044 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02005045#if INT_MAX < 0xffffffff
5046 /* Check that the uint32_t value passed by the caller fits
5047 * in the range supported by this implementation. */
5048 if( p->e > INT_MAX )
5049 return( PSA_ERROR_NOT_SUPPORTED );
5050#endif
5051 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005052 }
5053 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5054 if( rsa == NULL )
5055 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5056 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5057 ret = mbedtls_rsa_gen_key( rsa,
5058 mbedtls_ctr_drbg_random,
5059 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005060 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005061 exponent );
5062 if( ret != 0 )
5063 {
5064 mbedtls_rsa_free( rsa );
5065 mbedtls_free( rsa );
5066 return( mbedtls_to_psa_error( ret ) );
5067 }
5068 slot->data.rsa = rsa;
5069 }
5070 else
5071#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5072
5073#if defined(MBEDTLS_ECP_C)
5074 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
5075 {
5076 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5077 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5078 const mbedtls_ecp_curve_info *curve_info =
5079 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5080 mbedtls_ecp_keypair *ecp;
5081 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02005082 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005083 return( PSA_ERROR_NOT_SUPPORTED );
5084 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5085 return( PSA_ERROR_NOT_SUPPORTED );
5086 if( curve_info->bit_size != bits )
5087 return( PSA_ERROR_INVALID_ARGUMENT );
5088 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5089 if( ecp == NULL )
5090 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5091 mbedtls_ecp_keypair_init( ecp );
5092 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5093 mbedtls_ctr_drbg_random,
5094 &global_data.ctr_drbg );
5095 if( ret != 0 )
5096 {
5097 mbedtls_ecp_keypair_free( ecp );
5098 mbedtls_free( ecp );
5099 return( mbedtls_to_psa_error( ret ) );
5100 }
5101 slot->data.ecp = ecp;
5102 }
5103 else
5104#endif /* MBEDTLS_ECP_C */
5105
5106 return( PSA_ERROR_NOT_SUPPORTED );
5107
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005108 return( PSA_SUCCESS );
5109}
5110
5111psa_status_t psa_generate_key_to_handle( psa_key_handle_t handle,
5112 psa_key_type_t type,
5113 size_t bits,
5114 const void *extra,
5115 size_t extra_size )
5116{
5117 psa_key_slot_t *slot;
5118 psa_status_t status;
5119
5120 status = psa_get_empty_key_slot( handle, &slot );
5121 if( status != PSA_SUCCESS )
5122 return( status );
5123
Gilles Peskine12313cd2018-06-20 00:20:32 +02005124 slot->type = type;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005125 status = psa_generate_key_internal( slot, bits, extra, extra_size );
5126 if( status != PSA_SUCCESS )
5127 slot->type = 0;
Darryl Green0c6575a2018-11-07 16:05:30 +00005128
5129#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
5130 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
5131 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01005132 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005133 }
5134#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5135
5136 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005137}
5138
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005139psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
5140 psa_key_handle_t *handle,
5141 size_t bits,
5142 const void *extra,
5143 size_t extra_size )
5144{
5145 psa_status_t status;
5146 psa_key_slot_t *slot = NULL;
5147 status = psa_start_key_creation( attributes, handle, &slot );
5148 if( status == PSA_SUCCESS )
5149 {
5150 status = psa_generate_key_internal( slot, bits, extra, extra_size );
5151 }
5152 if( status == PSA_SUCCESS )
5153 status = psa_finish_key_creation( slot );
5154 if( status != PSA_SUCCESS )
5155 {
5156 psa_fail_key_creation( slot );
5157 *handle = 0;
5158 }
5159 return( status );
5160}
5161
5162
Gilles Peskine05d69892018-06-19 22:00:52 +02005163
5164/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005165/* Module setup */
5166/****************************************************************/
5167
Gilles Peskine5e769522018-11-20 21:59:56 +01005168psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5169 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5170 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5171{
5172 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5173 return( PSA_ERROR_BAD_STATE );
5174 global_data.entropy_init = entropy_init;
5175 global_data.entropy_free = entropy_free;
5176 return( PSA_SUCCESS );
5177}
5178
Gilles Peskinee59236f2018-01-27 23:32:46 +01005179void mbedtls_psa_crypto_free( void )
5180{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005181 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005182 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5183 {
5184 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005185 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005186 }
5187 /* Wipe all remaining data, including configuration.
5188 * In particular, this sets all state indicator to the value
5189 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005190 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005191}
5192
5193psa_status_t psa_crypto_init( void )
5194{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005195 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005196 const unsigned char drbg_seed[] = "PSA";
5197
Gilles Peskinec6b69072018-11-20 21:42:52 +01005198 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005199 if( global_data.initialized != 0 )
5200 return( PSA_SUCCESS );
5201
Gilles Peskine5e769522018-11-20 21:59:56 +01005202 /* Set default configuration if
5203 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5204 if( global_data.entropy_init == NULL )
5205 global_data.entropy_init = mbedtls_entropy_init;
5206 if( global_data.entropy_free == NULL )
5207 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005208
Gilles Peskinec6b69072018-11-20 21:42:52 +01005209 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005210 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005211 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005212 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005213 status = mbedtls_to_psa_error(
5214 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5215 mbedtls_entropy_func,
5216 &global_data.entropy,
5217 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5218 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005219 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005220 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005221
Gilles Peskine66fb1262018-12-10 16:29:04 +01005222 status = psa_initialize_key_slots( );
5223 if( status != PSA_SUCCESS )
5224 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005225
5226 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005227 global_data.initialized = 1;
5228
Gilles Peskinee59236f2018-01-27 23:32:46 +01005229exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005230 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005231 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005232 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005233}
5234
5235#endif /* MBEDTLS_PSA_CRYPTO_C */