blob: 9b43d13736f2a816959811f24fd352ab7dedab41 [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 Peskine2f060a82018-12-04 17:12:32 +0100968static size_t psa_get_key_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 Peskinec5487a82018-12-03 18:08:14 +0100984psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200985 psa_key_type_t *type,
986 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100987{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100988 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200989 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200992 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993 if( bits != NULL )
994 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100995 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200996 if( status != PSA_SUCCESS )
997 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200998
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001000 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001001 if( type != NULL )
1002 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001003 if( bits != NULL )
1004 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005 return( PSA_SUCCESS );
1006}
1007
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001008#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001009static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1010 unsigned char *buf, size_t size )
1011{
1012 int ret;
1013 unsigned char *c;
1014 size_t len = 0;
1015
1016 c = buf + size;
1017
1018 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1019
1020 return( (int) len );
1021}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001022#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001023
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001024static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1025 uint8_t *data,
1026 size_t data_size,
1027 size_t *data_length,
1028 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001029{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001030 *data_length = 0;
1031
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001032 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001033 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001034
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001035 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036 {
1037 if( slot->data.raw.bytes > data_size )
1038 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001039 if( data_size != 0 )
1040 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001041 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001042 memset( data + slot->data.raw.bytes, 0,
1043 data_size - slot->data.raw.bytes );
1044 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001045 *data_length = slot->data.raw.bytes;
1046 return( PSA_SUCCESS );
1047 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001048#if defined(MBEDTLS_ECP_C)
1049 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1050 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001051 psa_status_t status;
1052
Gilles Peskine188c71e2018-10-29 19:26:02 +01001053 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1054 if( bytes > data_size )
1055 return( PSA_ERROR_BUFFER_TOO_SMALL );
1056 status = mbedtls_to_psa_error(
1057 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1058 if( status != PSA_SUCCESS )
1059 return( status );
1060 memset( data + bytes, 0, data_size - bytes );
1061 *data_length = bytes;
1062 return( PSA_SUCCESS );
1063 }
1064#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001065 else
Moran Peker17e36e12018-05-02 12:55:20 +03001066 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001067#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001068 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001069 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001070 {
Moran Pekera998bc62018-04-16 18:16:20 +03001071 mbedtls_pk_context pk;
1072 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001073 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001074 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001075#if defined(MBEDTLS_RSA_C)
1076 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001077 pk.pk_info = &mbedtls_rsa_info;
1078 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001079#else
1080 return( PSA_ERROR_NOT_SUPPORTED );
1081#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001082 }
1083 else
1084 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001085#if defined(MBEDTLS_ECP_C)
1086 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001087 pk.pk_info = &mbedtls_eckey_info;
1088 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001089#else
1090 return( PSA_ERROR_NOT_SUPPORTED );
1091#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001092 }
Moran Pekerd7326592018-05-29 16:56:39 +03001093 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001094 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001095 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001096 }
Moran Peker17e36e12018-05-02 12:55:20 +03001097 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001098 {
Moran Peker17e36e12018-05-02 12:55:20 +03001099 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001100 }
Moran Peker60364322018-04-29 11:34:58 +03001101 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001102 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001103 /* If data_size is 0 then data may be NULL and then the
1104 * call to memset would have undefined behavior. */
1105 if( data_size != 0 )
1106 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001107 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001108 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001109 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1110 * Move the data to the beginning and erase remaining data
1111 * at the original location. */
1112 if( 2 * (size_t) ret <= data_size )
1113 {
1114 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001115 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001116 }
1117 else if( (size_t) ret < data_size )
1118 {
1119 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001120 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001121 }
Moran Pekera998bc62018-04-16 18:16:20 +03001122 *data_length = ret;
1123 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001124 }
1125 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001126#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001127 {
1128 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001129 it is valid for a special-purpose implementation to omit
1130 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001131 return( PSA_ERROR_NOT_SUPPORTED );
1132 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001133 }
1134}
1135
Gilles Peskinec5487a82018-12-03 18:08:14 +01001136psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001137 uint8_t *data,
1138 size_t data_size,
1139 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001140{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001141 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001142 psa_status_t status;
1143
1144 /* Set the key to empty now, so that even when there are errors, we always
1145 * set data_length to a value between 0 and data_size. On error, setting
1146 * the key to empty is a good choice because an empty key representation is
1147 * unlikely to be accepted anywhere. */
1148 *data_length = 0;
1149
1150 /* Export requires the EXPORT flag. There is an exception for public keys,
1151 * which don't require any flag, but psa_get_key_from_slot takes
1152 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001153 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001154 if( status != PSA_SUCCESS )
1155 return( status );
1156 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001157 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001158}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001159
Gilles Peskinec5487a82018-12-03 18:08:14 +01001160psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001161 uint8_t *data,
1162 size_t data_size,
1163 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001164{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001165 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001166 psa_status_t status;
1167
1168 /* Set the key to empty now, so that even when there are errors, we always
1169 * set data_length to a value between 0 and data_size. On error, setting
1170 * the key to empty is a good choice because an empty key representation is
1171 * unlikely to be accepted anywhere. */
1172 *data_length = 0;
1173
1174 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001175 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001176 if( status != PSA_SUCCESS )
1177 return( status );
1178 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001179 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001180}
1181
Darryl Green0c6575a2018-11-07 16:05:30 +00001182#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001183static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001184 size_t bits )
1185{
1186 psa_status_t status;
1187 uint8_t *data;
1188 size_t key_length;
1189 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1190 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001191 if( data == NULL )
1192 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001193 /* Get key data in export format */
1194 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1195 if( status != PSA_SUCCESS )
1196 {
1197 slot->type = PSA_KEY_TYPE_NONE;
1198 goto exit;
1199 }
1200 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001201 status = psa_save_persistent_key( slot->persistent_storage_id,
1202 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001203 data, key_length );
1204 if( status != PSA_SUCCESS )
1205 {
1206 slot->type = PSA_KEY_TYPE_NONE;
1207 }
1208exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001209 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001210 mbedtls_free( data );
1211 return( status );
1212}
1213#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1214
Gilles Peskine4747d192019-04-17 15:05:45 +02001215static psa_status_t psa_set_key_policy_internal(
1216 psa_key_slot_t *slot,
1217 const psa_key_policy_t *policy )
1218{
1219 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
1220 PSA_KEY_USAGE_ENCRYPT |
1221 PSA_KEY_USAGE_DECRYPT |
1222 PSA_KEY_USAGE_SIGN |
1223 PSA_KEY_USAGE_VERIFY |
1224 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1225 return( PSA_ERROR_INVALID_ARGUMENT );
1226
1227 slot->policy = *policy;
1228 return( PSA_SUCCESS );
1229}
1230
1231/** Prepare a key slot to receive key material.
1232 *
1233 * This function allocates a key slot and sets its metadata.
1234 *
1235 * If this function fails, call psa_fail_key_creation().
1236 *
1237 * \param attributes Key attributes for the new key.
1238 * \param handle On success, the allocated handle.
1239 * \param p_slot On success, a pointer to the prepared slot.
1240 */
1241static psa_status_t psa_start_key_creation(
1242 const psa_key_attributes_t *attributes,
1243 psa_key_handle_t *handle,
1244 psa_key_slot_t **p_slot )
1245{
1246 psa_status_t status;
1247 psa_key_slot_t *slot;
1248
1249 status = psa_allocate_key( handle );
1250 if( status != PSA_SUCCESS )
1251 return( status );
1252 status = psa_get_key_slot( *handle, p_slot );
1253 if( status != PSA_SUCCESS )
1254 return( status );
1255 slot = *p_slot;
1256
1257 status = psa_set_key_policy_internal( slot, &attributes->policy );
1258 if( status != PSA_SUCCESS )
1259 return( status );
1260 slot->lifetime = attributes->lifetime;
1261 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
1262 slot->persistent_storage_id = attributes->id;
1263 slot->type = attributes->type;
1264
1265 return( status );
1266}
1267
1268/** Finalize the creation of a key once its key material has been set.
1269 *
1270 * This entails writing the key to persistent storage.
1271 *
1272 * If this function fails, call psa_fail_key_creation().
1273 *
1274 * \param slot Pointer to the slot with key material.
1275 */
1276static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1277{
1278 psa_status_t status = PSA_SUCCESS;
1279
1280#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1281 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1282 {
1283 uint8_t *buffer = NULL;
1284 size_t buffer_size = 0;
1285 size_t length;
1286
1287 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
1288 psa_get_key_bits( slot ) );
1289 buffer = mbedtls_calloc( 1, buffer_size );
1290 if( buffer == NULL && buffer_size != 0 )
1291 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1292 status = psa_internal_export_key( slot,
1293 buffer, buffer_size, &length,
1294 0 );
1295
1296 if( status == PSA_SUCCESS )
1297 {
1298 status = psa_save_persistent_key( slot->persistent_storage_id,
1299 slot->type, &slot->policy,
1300 buffer, length );
1301 }
1302
1303 if( buffer_size != 0 )
1304 mbedtls_platform_zeroize( buffer, buffer_size );
1305 mbedtls_free( buffer );
1306 }
1307#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1308
1309 return( status );
1310}
1311
1312/** Abort the creation of a key.
1313 *
1314 * You may call this function after calling psa_start_key_creation(),
1315 * or after psa_finish_key_creation() fails. In other circumstances, this
1316 * function may not clean up persistent storage.
1317 *
1318 * \param slot Pointer to the slot with key material.
1319 */
1320static void psa_fail_key_creation( psa_key_slot_t *slot )
1321{
1322 if( slot == NULL )
1323 return;
1324 psa_wipe_key_slot( slot );
1325}
1326
1327psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
1328 psa_key_handle_t *handle,
1329 const uint8_t *data,
1330 size_t data_length )
1331{
1332 psa_status_t status;
1333 psa_key_slot_t *slot = NULL;
1334 status = psa_start_key_creation( attributes, handle, &slot );
1335 if( status == PSA_SUCCESS )
1336 {
1337 status = psa_import_key_into_slot( slot, data, data_length );
1338 }
1339 if( status == PSA_SUCCESS )
1340 status = psa_finish_key_creation( slot );
1341 if( status != PSA_SUCCESS )
1342 {
1343 psa_fail_key_creation( slot );
1344 *handle = 0;
1345 }
1346 return( status );
1347}
1348
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001349static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1350 psa_key_handle_t target )
1351{
1352 psa_status_t status;
1353 uint8_t *buffer = NULL;
1354 size_t buffer_size = 0;
1355 size_t length;
1356
1357 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1358 psa_get_key_bits( source ) );
1359 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001360 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001361 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001362 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1363 if( status != PSA_SUCCESS )
1364 goto exit;
Gilles Peskine87a5e562019-04-17 12:28:25 +02001365 status = psa_import_key_to_handle( target, source->type, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001366
1367exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001368 if( buffer_size != 0 )
1369 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001370 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001371 return( status );
1372}
1373
Gilles Peskine87a5e562019-04-17 12:28:25 +02001374psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001375 psa_key_handle_t target_handle,
1376 const psa_key_policy_t *constraint)
1377{
1378 psa_key_slot_t *source_slot = NULL;
1379 psa_key_slot_t *target_slot = NULL;
1380 psa_key_policy_t new_policy;
1381 psa_status_t status;
1382 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1383 if( status != PSA_SUCCESS )
1384 return( status );
1385 status = psa_get_empty_key_slot( target_handle, &target_slot );
1386 if( status != PSA_SUCCESS )
1387 return( status );
1388
1389 new_policy = target_slot->policy;
1390 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1391 if( status != PSA_SUCCESS )
1392 return( status );
1393 if( constraint != NULL )
1394 {
1395 status = psa_restrict_key_policy( &new_policy, constraint );
1396 if( status != PSA_SUCCESS )
1397 return( status );
1398 }
1399
1400 status = psa_copy_key_material( source_slot, target_handle );
1401 if( status != PSA_SUCCESS )
1402 return( status );
1403
1404 target_slot->policy = new_policy;
1405 return( PSA_SUCCESS );
1406}
1407
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001408
1409
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001410/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001411/* Message digests */
1412/****************************************************************/
1413
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001414static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001415{
1416 switch( alg )
1417 {
1418#if defined(MBEDTLS_MD2_C)
1419 case PSA_ALG_MD2:
1420 return( &mbedtls_md2_info );
1421#endif
1422#if defined(MBEDTLS_MD4_C)
1423 case PSA_ALG_MD4:
1424 return( &mbedtls_md4_info );
1425#endif
1426#if defined(MBEDTLS_MD5_C)
1427 case PSA_ALG_MD5:
1428 return( &mbedtls_md5_info );
1429#endif
1430#if defined(MBEDTLS_RIPEMD160_C)
1431 case PSA_ALG_RIPEMD160:
1432 return( &mbedtls_ripemd160_info );
1433#endif
1434#if defined(MBEDTLS_SHA1_C)
1435 case PSA_ALG_SHA_1:
1436 return( &mbedtls_sha1_info );
1437#endif
1438#if defined(MBEDTLS_SHA256_C)
1439 case PSA_ALG_SHA_224:
1440 return( &mbedtls_sha224_info );
1441 case PSA_ALG_SHA_256:
1442 return( &mbedtls_sha256_info );
1443#endif
1444#if defined(MBEDTLS_SHA512_C)
1445 case PSA_ALG_SHA_384:
1446 return( &mbedtls_sha384_info );
1447 case PSA_ALG_SHA_512:
1448 return( &mbedtls_sha512_info );
1449#endif
1450 default:
1451 return( NULL );
1452 }
1453}
1454
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001455psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1456{
1457 switch( operation->alg )
1458 {
Gilles Peskine81736312018-06-26 15:04:31 +02001459 case 0:
1460 /* The object has (apparently) been initialized but it is not
1461 * in use. It's ok to call abort on such an object, and there's
1462 * nothing to do. */
1463 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001464#if defined(MBEDTLS_MD2_C)
1465 case PSA_ALG_MD2:
1466 mbedtls_md2_free( &operation->ctx.md2 );
1467 break;
1468#endif
1469#if defined(MBEDTLS_MD4_C)
1470 case PSA_ALG_MD4:
1471 mbedtls_md4_free( &operation->ctx.md4 );
1472 break;
1473#endif
1474#if defined(MBEDTLS_MD5_C)
1475 case PSA_ALG_MD5:
1476 mbedtls_md5_free( &operation->ctx.md5 );
1477 break;
1478#endif
1479#if defined(MBEDTLS_RIPEMD160_C)
1480 case PSA_ALG_RIPEMD160:
1481 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1482 break;
1483#endif
1484#if defined(MBEDTLS_SHA1_C)
1485 case PSA_ALG_SHA_1:
1486 mbedtls_sha1_free( &operation->ctx.sha1 );
1487 break;
1488#endif
1489#if defined(MBEDTLS_SHA256_C)
1490 case PSA_ALG_SHA_224:
1491 case PSA_ALG_SHA_256:
1492 mbedtls_sha256_free( &operation->ctx.sha256 );
1493 break;
1494#endif
1495#if defined(MBEDTLS_SHA512_C)
1496 case PSA_ALG_SHA_384:
1497 case PSA_ALG_SHA_512:
1498 mbedtls_sha512_free( &operation->ctx.sha512 );
1499 break;
1500#endif
1501 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001502 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001503 }
1504 operation->alg = 0;
1505 return( PSA_SUCCESS );
1506}
1507
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001508psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001509 psa_algorithm_t alg )
1510{
1511 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001512
1513 /* A context must be freshly initialized before it can be set up. */
1514 if( operation->alg != 0 )
1515 {
1516 return( PSA_ERROR_BAD_STATE );
1517 }
1518
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001519 switch( alg )
1520 {
1521#if defined(MBEDTLS_MD2_C)
1522 case PSA_ALG_MD2:
1523 mbedtls_md2_init( &operation->ctx.md2 );
1524 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1525 break;
1526#endif
1527#if defined(MBEDTLS_MD4_C)
1528 case PSA_ALG_MD4:
1529 mbedtls_md4_init( &operation->ctx.md4 );
1530 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1531 break;
1532#endif
1533#if defined(MBEDTLS_MD5_C)
1534 case PSA_ALG_MD5:
1535 mbedtls_md5_init( &operation->ctx.md5 );
1536 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1537 break;
1538#endif
1539#if defined(MBEDTLS_RIPEMD160_C)
1540 case PSA_ALG_RIPEMD160:
1541 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1542 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1543 break;
1544#endif
1545#if defined(MBEDTLS_SHA1_C)
1546 case PSA_ALG_SHA_1:
1547 mbedtls_sha1_init( &operation->ctx.sha1 );
1548 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1549 break;
1550#endif
1551#if defined(MBEDTLS_SHA256_C)
1552 case PSA_ALG_SHA_224:
1553 mbedtls_sha256_init( &operation->ctx.sha256 );
1554 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1555 break;
1556 case PSA_ALG_SHA_256:
1557 mbedtls_sha256_init( &operation->ctx.sha256 );
1558 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1559 break;
1560#endif
1561#if defined(MBEDTLS_SHA512_C)
1562 case PSA_ALG_SHA_384:
1563 mbedtls_sha512_init( &operation->ctx.sha512 );
1564 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1565 break;
1566 case PSA_ALG_SHA_512:
1567 mbedtls_sha512_init( &operation->ctx.sha512 );
1568 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1569 break;
1570#endif
1571 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001572 return( PSA_ALG_IS_HASH( alg ) ?
1573 PSA_ERROR_NOT_SUPPORTED :
1574 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001575 }
1576 if( ret == 0 )
1577 operation->alg = alg;
1578 else
1579 psa_hash_abort( operation );
1580 return( mbedtls_to_psa_error( ret ) );
1581}
1582
1583psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1584 const uint8_t *input,
1585 size_t input_length )
1586{
1587 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001588
1589 /* Don't require hash implementations to behave correctly on a
1590 * zero-length input, which may have an invalid pointer. */
1591 if( input_length == 0 )
1592 return( PSA_SUCCESS );
1593
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001594 switch( operation->alg )
1595 {
1596#if defined(MBEDTLS_MD2_C)
1597 case PSA_ALG_MD2:
1598 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1599 input, input_length );
1600 break;
1601#endif
1602#if defined(MBEDTLS_MD4_C)
1603 case PSA_ALG_MD4:
1604 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1605 input, input_length );
1606 break;
1607#endif
1608#if defined(MBEDTLS_MD5_C)
1609 case PSA_ALG_MD5:
1610 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1611 input, input_length );
1612 break;
1613#endif
1614#if defined(MBEDTLS_RIPEMD160_C)
1615 case PSA_ALG_RIPEMD160:
1616 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1617 input, input_length );
1618 break;
1619#endif
1620#if defined(MBEDTLS_SHA1_C)
1621 case PSA_ALG_SHA_1:
1622 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1623 input, input_length );
1624 break;
1625#endif
1626#if defined(MBEDTLS_SHA256_C)
1627 case PSA_ALG_SHA_224:
1628 case PSA_ALG_SHA_256:
1629 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1630 input, input_length );
1631 break;
1632#endif
1633#if defined(MBEDTLS_SHA512_C)
1634 case PSA_ALG_SHA_384:
1635 case PSA_ALG_SHA_512:
1636 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1637 input, input_length );
1638 break;
1639#endif
1640 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001641 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001642 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001643
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001644 if( ret != 0 )
1645 psa_hash_abort( operation );
1646 return( mbedtls_to_psa_error( ret ) );
1647}
1648
1649psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1650 uint8_t *hash,
1651 size_t hash_size,
1652 size_t *hash_length )
1653{
itayzafrir40835d42018-08-02 13:14:17 +03001654 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001655 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001656 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001657
1658 /* Fill the output buffer with something that isn't a valid hash
1659 * (barring an attack on the hash and deliberately-crafted input),
1660 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001661 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001662 /* If hash_size is 0 then hash may be NULL and then the
1663 * call to memset would have undefined behavior. */
1664 if( hash_size != 0 )
1665 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001666
1667 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001668 {
1669 status = PSA_ERROR_BUFFER_TOO_SMALL;
1670 goto exit;
1671 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001672
1673 switch( operation->alg )
1674 {
1675#if defined(MBEDTLS_MD2_C)
1676 case PSA_ALG_MD2:
1677 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1678 break;
1679#endif
1680#if defined(MBEDTLS_MD4_C)
1681 case PSA_ALG_MD4:
1682 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1683 break;
1684#endif
1685#if defined(MBEDTLS_MD5_C)
1686 case PSA_ALG_MD5:
1687 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1688 break;
1689#endif
1690#if defined(MBEDTLS_RIPEMD160_C)
1691 case PSA_ALG_RIPEMD160:
1692 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1693 break;
1694#endif
1695#if defined(MBEDTLS_SHA1_C)
1696 case PSA_ALG_SHA_1:
1697 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1698 break;
1699#endif
1700#if defined(MBEDTLS_SHA256_C)
1701 case PSA_ALG_SHA_224:
1702 case PSA_ALG_SHA_256:
1703 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1704 break;
1705#endif
1706#if defined(MBEDTLS_SHA512_C)
1707 case PSA_ALG_SHA_384:
1708 case PSA_ALG_SHA_512:
1709 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1710 break;
1711#endif
1712 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001713 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001714 }
itayzafrir40835d42018-08-02 13:14:17 +03001715 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001716
itayzafrir40835d42018-08-02 13:14:17 +03001717exit:
1718 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001719 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001720 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001721 return( psa_hash_abort( operation ) );
1722 }
1723 else
1724 {
1725 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001726 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001727 }
1728}
1729
Gilles Peskine2d277862018-06-18 15:41:12 +02001730psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1731 const uint8_t *hash,
1732 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001733{
1734 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1735 size_t actual_hash_length;
1736 psa_status_t status = psa_hash_finish( operation,
1737 actual_hash, sizeof( actual_hash ),
1738 &actual_hash_length );
1739 if( status != PSA_SUCCESS )
1740 return( status );
1741 if( actual_hash_length != hash_length )
1742 return( PSA_ERROR_INVALID_SIGNATURE );
1743 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1744 return( PSA_ERROR_INVALID_SIGNATURE );
1745 return( PSA_SUCCESS );
1746}
1747
Gilles Peskineeb35d782019-01-22 17:56:16 +01001748psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1749 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001750{
1751 if( target_operation->alg != 0 )
1752 return( PSA_ERROR_BAD_STATE );
1753
1754 switch( source_operation->alg )
1755 {
1756 case 0:
1757 return( PSA_ERROR_BAD_STATE );
1758#if defined(MBEDTLS_MD2_C)
1759 case PSA_ALG_MD2:
1760 mbedtls_md2_clone( &target_operation->ctx.md2,
1761 &source_operation->ctx.md2 );
1762 break;
1763#endif
1764#if defined(MBEDTLS_MD4_C)
1765 case PSA_ALG_MD4:
1766 mbedtls_md4_clone( &target_operation->ctx.md4,
1767 &source_operation->ctx.md4 );
1768 break;
1769#endif
1770#if defined(MBEDTLS_MD5_C)
1771 case PSA_ALG_MD5:
1772 mbedtls_md5_clone( &target_operation->ctx.md5,
1773 &source_operation->ctx.md5 );
1774 break;
1775#endif
1776#if defined(MBEDTLS_RIPEMD160_C)
1777 case PSA_ALG_RIPEMD160:
1778 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1779 &source_operation->ctx.ripemd160 );
1780 break;
1781#endif
1782#if defined(MBEDTLS_SHA1_C)
1783 case PSA_ALG_SHA_1:
1784 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1785 &source_operation->ctx.sha1 );
1786 break;
1787#endif
1788#if defined(MBEDTLS_SHA256_C)
1789 case PSA_ALG_SHA_224:
1790 case PSA_ALG_SHA_256:
1791 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1792 &source_operation->ctx.sha256 );
1793 break;
1794#endif
1795#if defined(MBEDTLS_SHA512_C)
1796 case PSA_ALG_SHA_384:
1797 case PSA_ALG_SHA_512:
1798 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1799 &source_operation->ctx.sha512 );
1800 break;
1801#endif
1802 default:
1803 return( PSA_ERROR_NOT_SUPPORTED );
1804 }
1805
1806 target_operation->alg = source_operation->alg;
1807 return( PSA_SUCCESS );
1808}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001809
1810
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001811/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001812/* MAC */
1813/****************************************************************/
1814
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001815static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001816 psa_algorithm_t alg,
1817 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001818 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001819 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001820{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001821 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001822 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001823
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001824 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001825 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001826
Gilles Peskine8c9def32018-02-08 10:02:12 +01001827 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1828 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001829 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001830 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001831 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001832 mode = MBEDTLS_MODE_STREAM;
1833 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001834 case PSA_ALG_CTR:
1835 mode = MBEDTLS_MODE_CTR;
1836 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001837 case PSA_ALG_CFB:
1838 mode = MBEDTLS_MODE_CFB;
1839 break;
1840 case PSA_ALG_OFB:
1841 mode = MBEDTLS_MODE_OFB;
1842 break;
1843 case PSA_ALG_CBC_NO_PADDING:
1844 mode = MBEDTLS_MODE_CBC;
1845 break;
1846 case PSA_ALG_CBC_PKCS7:
1847 mode = MBEDTLS_MODE_CBC;
1848 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001849 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001850 mode = MBEDTLS_MODE_CCM;
1851 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001852 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001853 mode = MBEDTLS_MODE_GCM;
1854 break;
1855 default:
1856 return( NULL );
1857 }
1858 }
1859 else if( alg == PSA_ALG_CMAC )
1860 mode = MBEDTLS_MODE_ECB;
1861 else if( alg == PSA_ALG_GMAC )
1862 mode = MBEDTLS_MODE_GCM;
1863 else
1864 return( NULL );
1865
1866 switch( key_type )
1867 {
1868 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001869 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001870 break;
1871 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001872 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1873 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001874 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001875 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001876 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001877 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001878 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1879 * but two-key Triple-DES is functionally three-key Triple-DES
1880 * with K1=K3, so that's how we present it to mbedtls. */
1881 if( key_bits == 128 )
1882 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001883 break;
1884 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001885 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001886 break;
1887 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001888 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001889 break;
1890 default:
1891 return( NULL );
1892 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001893 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001894 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001895
Jaeden Amero23bbb752018-06-26 14:16:54 +01001896 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1897 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001898}
1899
Gilles Peskinea05219c2018-11-16 16:02:56 +01001900#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001901static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001902{
Gilles Peskine2d277862018-06-18 15:41:12 +02001903 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001904 {
1905 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001906 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001907 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001908 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001909 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001910 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001911 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001912 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001913 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001914 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001915 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001916 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001917 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001918 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001919 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001920 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001921 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001922 return( 128 );
1923 default:
1924 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001925 }
1926}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001927#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001928
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001929/* Initialize the MAC operation structure. Once this function has been
1930 * called, psa_mac_abort can run and will do the right thing. */
1931static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1932 psa_algorithm_t alg )
1933{
1934 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1935
1936 operation->alg = alg;
1937 operation->key_set = 0;
1938 operation->iv_set = 0;
1939 operation->iv_required = 0;
1940 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001941 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001942
1943#if defined(MBEDTLS_CMAC_C)
1944 if( alg == PSA_ALG_CMAC )
1945 {
1946 operation->iv_required = 0;
1947 mbedtls_cipher_init( &operation->ctx.cmac );
1948 status = PSA_SUCCESS;
1949 }
1950 else
1951#endif /* MBEDTLS_CMAC_C */
1952#if defined(MBEDTLS_MD_C)
1953 if( PSA_ALG_IS_HMAC( operation->alg ) )
1954 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001955 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1956 operation->ctx.hmac.hash_ctx.alg = 0;
1957 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001958 }
1959 else
1960#endif /* MBEDTLS_MD_C */
1961 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001962 if( ! PSA_ALG_IS_MAC( alg ) )
1963 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001964 }
1965
1966 if( status != PSA_SUCCESS )
1967 memset( operation, 0, sizeof( *operation ) );
1968 return( status );
1969}
1970
Gilles Peskine01126fa2018-07-12 17:04:55 +02001971#if defined(MBEDTLS_MD_C)
1972static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1973{
Gilles Peskine3f108122018-12-07 18:14:53 +01001974 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001975 return( psa_hash_abort( &hmac->hash_ctx ) );
1976}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001977
1978static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1979{
1980 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1981 memset( hmac, 0, sizeof( *hmac ) );
1982}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001983#endif /* MBEDTLS_MD_C */
1984
Gilles Peskine8c9def32018-02-08 10:02:12 +01001985psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1986{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001987 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001988 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001989 /* The object has (apparently) been initialized but it is not
1990 * in use. It's ok to call abort on such an object, and there's
1991 * nothing to do. */
1992 return( PSA_SUCCESS );
1993 }
1994 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001995#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001996 if( operation->alg == PSA_ALG_CMAC )
1997 {
1998 mbedtls_cipher_free( &operation->ctx.cmac );
1999 }
2000 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002001#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002002#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002003 if( PSA_ALG_IS_HMAC( operation->alg ) )
2004 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002005 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002006 }
2007 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002008#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002009 {
2010 /* Sanity check (shouldn't happen: operation->alg should
2011 * always have been initialized to a valid value). */
2012 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002013 }
Moran Peker41deec42018-04-04 15:43:05 +03002014
Gilles Peskine8c9def32018-02-08 10:02:12 +01002015 operation->alg = 0;
2016 operation->key_set = 0;
2017 operation->iv_set = 0;
2018 operation->iv_required = 0;
2019 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002020 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002021
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002023
2024bad_state:
2025 /* If abort is called on an uninitialized object, we can't trust
2026 * anything. Wipe the object in case it contains confidential data.
2027 * This may result in a memory leak if a pointer gets overwritten,
2028 * but it's too late to do anything about this. */
2029 memset( operation, 0, sizeof( *operation ) );
2030 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031}
2032
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002033#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002034static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002035 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002036 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002037 const mbedtls_cipher_info_t *cipher_info )
2038{
2039 int ret;
2040
2041 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002042
2043 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2044 if( ret != 0 )
2045 return( ret );
2046
2047 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2048 slot->data.raw.data,
2049 key_bits );
2050 return( ret );
2051}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002052#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002053
Gilles Peskine248051a2018-06-20 16:09:38 +02002054#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002055static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2056 const uint8_t *key,
2057 size_t key_length,
2058 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002059{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002060 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002061 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002062 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002063 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002064 psa_status_t status;
2065
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002066 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2067 * overflow below. This should never trigger if the hash algorithm
2068 * is implemented correctly. */
2069 /* The size checks against the ipad and opad buffers cannot be written
2070 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2071 * because that triggers -Wlogical-op on GCC 7.3. */
2072 if( block_size > sizeof( ipad ) )
2073 return( PSA_ERROR_NOT_SUPPORTED );
2074 if( block_size > sizeof( hmac->opad ) )
2075 return( PSA_ERROR_NOT_SUPPORTED );
2076 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002077 return( PSA_ERROR_NOT_SUPPORTED );
2078
Gilles Peskined223b522018-06-11 18:12:58 +02002079 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002080 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002081 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002082 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002083 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002084 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002085 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002086 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002087 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002088 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002089 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002090 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002091 }
Gilles Peskine96889972018-07-12 17:07:03 +02002092 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2093 * but it is permitted. It is common when HMAC is used in HKDF, for
2094 * example. Don't call `memcpy` in the 0-length because `key` could be
2095 * an invalid pointer which would make the behavior undefined. */
2096 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002097 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002098
Gilles Peskined223b522018-06-11 18:12:58 +02002099 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2100 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002101 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002102 ipad[i] ^= 0x36;
2103 memset( ipad + key_length, 0x36, block_size - key_length );
2104
2105 /* Copy the key material from ipad to opad, flipping the requisite bits,
2106 * and filling the rest of opad with the requisite constant. */
2107 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002108 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2109 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002110
Gilles Peskine01126fa2018-07-12 17:04:55 +02002111 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002112 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002113 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002114
Gilles Peskine01126fa2018-07-12 17:04:55 +02002115 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002116
2117cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002118 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002119
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002120 return( status );
2121}
Gilles Peskine248051a2018-06-20 16:09:38 +02002122#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002123
Gilles Peskine89167cb2018-07-08 20:12:23 +02002124static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002125 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002126 psa_algorithm_t alg,
2127 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002129 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002130 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002131 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002132 psa_key_usage_t usage =
2133 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002134 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002135 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002136
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002137 /* A context must be freshly initialized before it can be set up. */
2138 if( operation->alg != 0 )
2139 {
2140 return( PSA_ERROR_BAD_STATE );
2141 }
2142
Gilles Peskined911eb72018-08-14 15:18:45 +02002143 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002144 if( status != PSA_SUCCESS )
2145 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002146 if( is_sign )
2147 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002148
Gilles Peskinec5487a82018-12-03 18:08:14 +01002149 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002150 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002151 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002152 key_bits = psa_get_key_bits( slot );
2153
Gilles Peskine8c9def32018-02-08 10:02:12 +01002154#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002155 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002156 {
2157 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002158 mbedtls_cipher_info_from_psa( full_length_alg,
2159 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002160 int ret;
2161 if( cipher_info == NULL )
2162 {
2163 status = PSA_ERROR_NOT_SUPPORTED;
2164 goto exit;
2165 }
2166 operation->mac_size = cipher_info->block_size;
2167 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2168 status = mbedtls_to_psa_error( ret );
2169 }
2170 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002171#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002172#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002173 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002174 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002175 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002176 if( hash_alg == 0 )
2177 {
2178 status = PSA_ERROR_NOT_SUPPORTED;
2179 goto exit;
2180 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002181
2182 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2183 /* Sanity check. This shouldn't fail on a valid configuration. */
2184 if( operation->mac_size == 0 ||
2185 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2186 {
2187 status = PSA_ERROR_NOT_SUPPORTED;
2188 goto exit;
2189 }
2190
Gilles Peskine01126fa2018-07-12 17:04:55 +02002191 if( slot->type != PSA_KEY_TYPE_HMAC )
2192 {
2193 status = PSA_ERROR_INVALID_ARGUMENT;
2194 goto exit;
2195 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002196
Gilles Peskine01126fa2018-07-12 17:04:55 +02002197 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2198 slot->data.raw.data,
2199 slot->data.raw.bytes,
2200 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002201 }
2202 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002203#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002204 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002205 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002206 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002207 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002208
Gilles Peskined911eb72018-08-14 15:18:45 +02002209 if( truncated == 0 )
2210 {
2211 /* The "normal" case: untruncated algorithm. Nothing to do. */
2212 }
2213 else if( truncated < 4 )
2214 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002215 /* A very short MAC is too short for security since it can be
2216 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2217 * so we make this our minimum, even though 32 bits is still
2218 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002219 status = PSA_ERROR_NOT_SUPPORTED;
2220 }
2221 else if( truncated > operation->mac_size )
2222 {
2223 /* It's impossible to "truncate" to a larger length. */
2224 status = PSA_ERROR_INVALID_ARGUMENT;
2225 }
2226 else
2227 operation->mac_size = truncated;
2228
Gilles Peskinefbfac682018-07-08 20:51:54 +02002229exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002230 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002231 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002232 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002233 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002234 else
2235 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002236 operation->key_set = 1;
2237 }
2238 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002239}
2240
Gilles Peskine89167cb2018-07-08 20:12:23 +02002241psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002242 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002243 psa_algorithm_t alg )
2244{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002245 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002246}
2247
2248psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002249 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002250 psa_algorithm_t alg )
2251{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002252 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002253}
2254
Gilles Peskine8c9def32018-02-08 10:02:12 +01002255psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2256 const uint8_t *input,
2257 size_t input_length )
2258{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002259 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002260 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002261 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002262 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002263 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002264 operation->has_input = 1;
2265
Gilles Peskine8c9def32018-02-08 10:02:12 +01002266#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002267 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002268 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002269 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2270 input, input_length );
2271 status = mbedtls_to_psa_error( ret );
2272 }
2273 else
2274#endif /* MBEDTLS_CMAC_C */
2275#if defined(MBEDTLS_MD_C)
2276 if( PSA_ALG_IS_HMAC( operation->alg ) )
2277 {
2278 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2279 input_length );
2280 }
2281 else
2282#endif /* MBEDTLS_MD_C */
2283 {
2284 /* This shouldn't happen if `operation` was initialized by
2285 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002286 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002287 }
2288
Gilles Peskinefbfac682018-07-08 20:51:54 +02002289 if( status != PSA_SUCCESS )
2290 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002291 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002292}
2293
Gilles Peskine01126fa2018-07-12 17:04:55 +02002294#if defined(MBEDTLS_MD_C)
2295static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2296 uint8_t *mac,
2297 size_t mac_size )
2298{
2299 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2300 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2301 size_t hash_size = 0;
2302 size_t block_size = psa_get_hash_block_size( hash_alg );
2303 psa_status_t status;
2304
Gilles Peskine01126fa2018-07-12 17:04:55 +02002305 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2306 if( status != PSA_SUCCESS )
2307 return( status );
2308 /* From here on, tmp needs to be wiped. */
2309
2310 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2311 if( status != PSA_SUCCESS )
2312 goto exit;
2313
2314 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2315 if( status != PSA_SUCCESS )
2316 goto exit;
2317
2318 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2319 if( status != PSA_SUCCESS )
2320 goto exit;
2321
Gilles Peskined911eb72018-08-14 15:18:45 +02002322 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2323 if( status != PSA_SUCCESS )
2324 goto exit;
2325
2326 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002327
2328exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002329 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002330 return( status );
2331}
2332#endif /* MBEDTLS_MD_C */
2333
mohammad16036df908f2018-04-02 08:34:15 -07002334static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002335 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002336 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002337{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002338 if( ! operation->key_set )
2339 return( PSA_ERROR_BAD_STATE );
2340 if( operation->iv_required && ! operation->iv_set )
2341 return( PSA_ERROR_BAD_STATE );
2342
Gilles Peskine8c9def32018-02-08 10:02:12 +01002343 if( mac_size < operation->mac_size )
2344 return( PSA_ERROR_BUFFER_TOO_SMALL );
2345
Gilles Peskine8c9def32018-02-08 10:02:12 +01002346#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002347 if( operation->alg == PSA_ALG_CMAC )
2348 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002349 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2350 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2351 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002352 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002353 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002354 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002355 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002356 else
2357#endif /* MBEDTLS_CMAC_C */
2358#if defined(MBEDTLS_MD_C)
2359 if( PSA_ALG_IS_HMAC( operation->alg ) )
2360 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002361 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002362 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002363 }
2364 else
2365#endif /* MBEDTLS_MD_C */
2366 {
2367 /* This shouldn't happen if `operation` was initialized by
2368 * a setup function. */
2369 return( PSA_ERROR_BAD_STATE );
2370 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002371}
2372
Gilles Peskineacd4be32018-07-08 19:56:25 +02002373psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2374 uint8_t *mac,
2375 size_t mac_size,
2376 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002377{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002378 psa_status_t status;
2379
Jaeden Amero252ef282019-02-15 14:05:35 +00002380 if( operation->alg == 0 )
2381 {
2382 return( PSA_ERROR_BAD_STATE );
2383 }
2384
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002385 /* Fill the output buffer with something that isn't a valid mac
2386 * (barring an attack on the mac and deliberately-crafted input),
2387 * in case the caller doesn't check the return status properly. */
2388 *mac_length = mac_size;
2389 /* If mac_size is 0 then mac may be NULL and then the
2390 * call to memset would have undefined behavior. */
2391 if( mac_size != 0 )
2392 memset( mac, '!', mac_size );
2393
Gilles Peskine89167cb2018-07-08 20:12:23 +02002394 if( ! operation->is_sign )
2395 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002396 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002397 }
mohammad16036df908f2018-04-02 08:34:15 -07002398
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002399 status = psa_mac_finish_internal( operation, mac, mac_size );
2400
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002401 if( status == PSA_SUCCESS )
2402 {
2403 status = psa_mac_abort( operation );
2404 if( status == PSA_SUCCESS )
2405 *mac_length = operation->mac_size;
2406 else
2407 memset( mac, '!', mac_size );
2408 }
2409 else
2410 psa_mac_abort( operation );
2411 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002412}
2413
Gilles Peskineacd4be32018-07-08 19:56:25 +02002414psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2415 const uint8_t *mac,
2416 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002417{
Gilles Peskine828ed142018-06-18 23:25:51 +02002418 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002419 psa_status_t status;
2420
Jaeden Amero252ef282019-02-15 14:05:35 +00002421 if( operation->alg == 0 )
2422 {
2423 return( PSA_ERROR_BAD_STATE );
2424 }
2425
Gilles Peskine89167cb2018-07-08 20:12:23 +02002426 if( operation->is_sign )
2427 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002428 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002429 }
2430 if( operation->mac_size != mac_length )
2431 {
2432 status = PSA_ERROR_INVALID_SIGNATURE;
2433 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002434 }
mohammad16036df908f2018-04-02 08:34:15 -07002435
2436 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002437 actual_mac, sizeof( actual_mac ) );
2438
2439 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2440 status = PSA_ERROR_INVALID_SIGNATURE;
2441
2442cleanup:
2443 if( status == PSA_SUCCESS )
2444 status = psa_mac_abort( operation );
2445 else
2446 psa_mac_abort( operation );
2447
Gilles Peskine3f108122018-12-07 18:14:53 +01002448 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002449
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002450 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451}
2452
2453
Gilles Peskine20035e32018-02-03 22:44:14 +01002454
Gilles Peskine20035e32018-02-03 22:44:14 +01002455/****************************************************************/
2456/* Asymmetric cryptography */
2457/****************************************************************/
2458
Gilles Peskine2b450e32018-06-27 15:42:46 +02002459#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002460/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002461 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002462static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2463 size_t hash_length,
2464 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002465{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002466 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002467 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002468 *md_alg = mbedtls_md_get_type( md_info );
2469
2470 /* The Mbed TLS RSA module uses an unsigned int for hash length
2471 * parameters. Validate that it fits so that we don't risk an
2472 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002473#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002474 if( hash_length > UINT_MAX )
2475 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002476#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002477
2478#if defined(MBEDTLS_PKCS1_V15)
2479 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2480 * must be correct. */
2481 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2482 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002483 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002484 if( md_info == NULL )
2485 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002486 if( mbedtls_md_get_size( md_info ) != hash_length )
2487 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002488 }
2489#endif /* MBEDTLS_PKCS1_V15 */
2490
2491#if defined(MBEDTLS_PKCS1_V21)
2492 /* PSS requires a hash internally. */
2493 if( PSA_ALG_IS_RSA_PSS( alg ) )
2494 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002495 if( md_info == NULL )
2496 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002497 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002498#endif /* MBEDTLS_PKCS1_V21 */
2499
Gilles Peskine61b91d42018-06-08 16:09:36 +02002500 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002501}
2502
Gilles Peskine2b450e32018-06-27 15:42:46 +02002503static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2504 psa_algorithm_t alg,
2505 const uint8_t *hash,
2506 size_t hash_length,
2507 uint8_t *signature,
2508 size_t signature_size,
2509 size_t *signature_length )
2510{
2511 psa_status_t status;
2512 int ret;
2513 mbedtls_md_type_t md_alg;
2514
2515 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2516 if( status != PSA_SUCCESS )
2517 return( status );
2518
Gilles Peskine630a18a2018-06-29 17:49:35 +02002519 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002520 return( PSA_ERROR_BUFFER_TOO_SMALL );
2521
2522#if defined(MBEDTLS_PKCS1_V15)
2523 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2524 {
2525 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2526 MBEDTLS_MD_NONE );
2527 ret = mbedtls_rsa_pkcs1_sign( rsa,
2528 mbedtls_ctr_drbg_random,
2529 &global_data.ctr_drbg,
2530 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002531 md_alg,
2532 (unsigned int) hash_length,
2533 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002534 signature );
2535 }
2536 else
2537#endif /* MBEDTLS_PKCS1_V15 */
2538#if defined(MBEDTLS_PKCS1_V21)
2539 if( PSA_ALG_IS_RSA_PSS( alg ) )
2540 {
2541 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2542 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2543 mbedtls_ctr_drbg_random,
2544 &global_data.ctr_drbg,
2545 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002546 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002547 (unsigned int) hash_length,
2548 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002549 signature );
2550 }
2551 else
2552#endif /* MBEDTLS_PKCS1_V21 */
2553 {
2554 return( PSA_ERROR_INVALID_ARGUMENT );
2555 }
2556
2557 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002558 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002559 return( mbedtls_to_psa_error( ret ) );
2560}
2561
2562static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2563 psa_algorithm_t alg,
2564 const uint8_t *hash,
2565 size_t hash_length,
2566 const uint8_t *signature,
2567 size_t signature_length )
2568{
2569 psa_status_t status;
2570 int ret;
2571 mbedtls_md_type_t md_alg;
2572
2573 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2574 if( status != PSA_SUCCESS )
2575 return( status );
2576
Gilles Peskine630a18a2018-06-29 17:49:35 +02002577 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002578 return( PSA_ERROR_BUFFER_TOO_SMALL );
2579
2580#if defined(MBEDTLS_PKCS1_V15)
2581 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2582 {
2583 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2584 MBEDTLS_MD_NONE );
2585 ret = mbedtls_rsa_pkcs1_verify( rsa,
2586 mbedtls_ctr_drbg_random,
2587 &global_data.ctr_drbg,
2588 MBEDTLS_RSA_PUBLIC,
2589 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002590 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002591 hash,
2592 signature );
2593 }
2594 else
2595#endif /* MBEDTLS_PKCS1_V15 */
2596#if defined(MBEDTLS_PKCS1_V21)
2597 if( PSA_ALG_IS_RSA_PSS( alg ) )
2598 {
2599 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2600 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2601 mbedtls_ctr_drbg_random,
2602 &global_data.ctr_drbg,
2603 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002604 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002605 (unsigned int) hash_length,
2606 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002607 signature );
2608 }
2609 else
2610#endif /* MBEDTLS_PKCS1_V21 */
2611 {
2612 return( PSA_ERROR_INVALID_ARGUMENT );
2613 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002614
2615 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2616 * the rest of the signature is invalid". This has little use in
2617 * practice and PSA doesn't report this distinction. */
2618 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2619 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002620 return( mbedtls_to_psa_error( ret ) );
2621}
2622#endif /* MBEDTLS_RSA_C */
2623
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002624#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002625/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2626 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2627 * (even though these functions don't modify it). */
2628static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2629 psa_algorithm_t alg,
2630 const uint8_t *hash,
2631 size_t hash_length,
2632 uint8_t *signature,
2633 size_t signature_size,
2634 size_t *signature_length )
2635{
2636 int ret;
2637 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002638 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002639 mbedtls_mpi_init( &r );
2640 mbedtls_mpi_init( &s );
2641
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002642 if( signature_size < 2 * curve_bytes )
2643 {
2644 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2645 goto cleanup;
2646 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002647
Gilles Peskinea05219c2018-11-16 16:02:56 +01002648#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002649 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2650 {
2651 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2652 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2653 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2654 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2655 hash, hash_length,
2656 md_alg ) );
2657 }
2658 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002659#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002660 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002661 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002662 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2663 hash, hash_length,
2664 mbedtls_ctr_drbg_random,
2665 &global_data.ctr_drbg ) );
2666 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002667
2668 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2669 signature,
2670 curve_bytes ) );
2671 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2672 signature + curve_bytes,
2673 curve_bytes ) );
2674
2675cleanup:
2676 mbedtls_mpi_free( &r );
2677 mbedtls_mpi_free( &s );
2678 if( ret == 0 )
2679 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002680 return( mbedtls_to_psa_error( ret ) );
2681}
2682
2683static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2684 const uint8_t *hash,
2685 size_t hash_length,
2686 const uint8_t *signature,
2687 size_t signature_length )
2688{
2689 int ret;
2690 mbedtls_mpi r, s;
2691 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2692 mbedtls_mpi_init( &r );
2693 mbedtls_mpi_init( &s );
2694
2695 if( signature_length != 2 * curve_bytes )
2696 return( PSA_ERROR_INVALID_SIGNATURE );
2697
2698 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2699 signature,
2700 curve_bytes ) );
2701 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2702 signature + curve_bytes,
2703 curve_bytes ) );
2704
2705 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2706 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002707
2708cleanup:
2709 mbedtls_mpi_free( &r );
2710 mbedtls_mpi_free( &s );
2711 return( mbedtls_to_psa_error( ret ) );
2712}
2713#endif /* MBEDTLS_ECDSA_C */
2714
Gilles Peskinec5487a82018-12-03 18:08:14 +01002715psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002716 psa_algorithm_t alg,
2717 const uint8_t *hash,
2718 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002719 uint8_t *signature,
2720 size_t signature_size,
2721 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002722{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002723 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002724 psa_status_t status;
2725
2726 *signature_length = signature_size;
2727
Gilles Peskinec5487a82018-12-03 18:08:14 +01002728 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002729 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002730 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002731 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002732 {
2733 status = PSA_ERROR_INVALID_ARGUMENT;
2734 goto exit;
2735 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002736
Gilles Peskine20035e32018-02-03 22:44:14 +01002737#if defined(MBEDTLS_RSA_C)
2738 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2739 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002740 status = psa_rsa_sign( slot->data.rsa,
2741 alg,
2742 hash, hash_length,
2743 signature, signature_size,
2744 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002745 }
2746 else
2747#endif /* defined(MBEDTLS_RSA_C) */
2748#if defined(MBEDTLS_ECP_C)
2749 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2750 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002751#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002752 if(
2753#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2754 PSA_ALG_IS_ECDSA( alg )
2755#else
2756 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2757#endif
2758 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002759 status = psa_ecdsa_sign( slot->data.ecp,
2760 alg,
2761 hash, hash_length,
2762 signature, signature_size,
2763 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002764 else
2765#endif /* defined(MBEDTLS_ECDSA_C) */
2766 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002767 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002768 }
itayzafrir5c753392018-05-08 11:18:38 +03002769 }
2770 else
2771#endif /* defined(MBEDTLS_ECP_C) */
2772 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002773 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002774 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002775
2776exit:
2777 /* Fill the unused part of the output buffer (the whole buffer on error,
2778 * the trailing part on success) with something that isn't a valid mac
2779 * (barring an attack on the mac and deliberately-crafted input),
2780 * in case the caller doesn't check the return status properly. */
2781 if( status == PSA_SUCCESS )
2782 memset( signature + *signature_length, '!',
2783 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002784 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002785 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002786 /* If signature_size is 0 then we have nothing to do. We must not call
2787 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002788 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002789}
2790
Gilles Peskinec5487a82018-12-03 18:08:14 +01002791psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002792 psa_algorithm_t alg,
2793 const uint8_t *hash,
2794 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002795 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002796 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002797{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002798 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002799 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002800
Gilles Peskinec5487a82018-12-03 18:08:14 +01002801 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002802 if( status != PSA_SUCCESS )
2803 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002804
Gilles Peskine61b91d42018-06-08 16:09:36 +02002805#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002806 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002807 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002808 return( psa_rsa_verify( slot->data.rsa,
2809 alg,
2810 hash, hash_length,
2811 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002812 }
2813 else
2814#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002815#if defined(MBEDTLS_ECP_C)
2816 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2817 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002818#if defined(MBEDTLS_ECDSA_C)
2819 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002820 return( psa_ecdsa_verify( slot->data.ecp,
2821 hash, hash_length,
2822 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002823 else
2824#endif /* defined(MBEDTLS_ECDSA_C) */
2825 {
2826 return( PSA_ERROR_INVALID_ARGUMENT );
2827 }
itayzafrir5c753392018-05-08 11:18:38 +03002828 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002829 else
2830#endif /* defined(MBEDTLS_ECP_C) */
2831 {
2832 return( PSA_ERROR_NOT_SUPPORTED );
2833 }
2834}
2835
Gilles Peskine072ac562018-06-30 00:21:29 +02002836#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2837static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2838 mbedtls_rsa_context *rsa )
2839{
2840 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2841 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2842 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2843 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2844}
2845#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2846
Gilles Peskinec5487a82018-12-03 18:08:14 +01002847psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002848 psa_algorithm_t alg,
2849 const uint8_t *input,
2850 size_t input_length,
2851 const uint8_t *salt,
2852 size_t salt_length,
2853 uint8_t *output,
2854 size_t output_size,
2855 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002856{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002857 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002858 psa_status_t status;
2859
Darryl Green5cc689a2018-07-24 15:34:10 +01002860 (void) input;
2861 (void) input_length;
2862 (void) salt;
2863 (void) output;
2864 (void) output_size;
2865
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002866 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002867
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002868 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2869 return( PSA_ERROR_INVALID_ARGUMENT );
2870
Gilles Peskinec5487a82018-12-03 18:08:14 +01002871 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002872 if( status != PSA_SUCCESS )
2873 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002874 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2875 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002876 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002877
2878#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002879 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002880 {
2881 mbedtls_rsa_context *rsa = slot->data.rsa;
2882 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002883 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002884 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002885#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002886 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002887 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002888 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2889 mbedtls_ctr_drbg_random,
2890 &global_data.ctr_drbg,
2891 MBEDTLS_RSA_PUBLIC,
2892 input_length,
2893 input,
2894 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002895 }
2896 else
2897#endif /* MBEDTLS_PKCS1_V15 */
2898#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002899 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002900 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002901 psa_rsa_oaep_set_padding_mode( alg, rsa );
2902 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2903 mbedtls_ctr_drbg_random,
2904 &global_data.ctr_drbg,
2905 MBEDTLS_RSA_PUBLIC,
2906 salt, salt_length,
2907 input_length,
2908 input,
2909 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002910 }
2911 else
2912#endif /* MBEDTLS_PKCS1_V21 */
2913 {
2914 return( PSA_ERROR_INVALID_ARGUMENT );
2915 }
2916 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002917 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002918 return( mbedtls_to_psa_error( ret ) );
2919 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002920 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002921#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002922 {
2923 return( PSA_ERROR_NOT_SUPPORTED );
2924 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002925}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002926
Gilles Peskinec5487a82018-12-03 18:08:14 +01002927psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002928 psa_algorithm_t alg,
2929 const uint8_t *input,
2930 size_t input_length,
2931 const uint8_t *salt,
2932 size_t salt_length,
2933 uint8_t *output,
2934 size_t output_size,
2935 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002936{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002937 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002938 psa_status_t status;
2939
Darryl Green5cc689a2018-07-24 15:34:10 +01002940 (void) input;
2941 (void) input_length;
2942 (void) salt;
2943 (void) output;
2944 (void) output_size;
2945
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002946 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002947
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002948 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2949 return( PSA_ERROR_INVALID_ARGUMENT );
2950
Gilles Peskinec5487a82018-12-03 18:08:14 +01002951 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002952 if( status != PSA_SUCCESS )
2953 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002954 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2955 return( PSA_ERROR_INVALID_ARGUMENT );
2956
2957#if defined(MBEDTLS_RSA_C)
2958 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2959 {
2960 mbedtls_rsa_context *rsa = slot->data.rsa;
2961 int ret;
2962
Gilles Peskine630a18a2018-06-29 17:49:35 +02002963 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002964 return( PSA_ERROR_INVALID_ARGUMENT );
2965
2966#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002967 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002968 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002969 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2970 mbedtls_ctr_drbg_random,
2971 &global_data.ctr_drbg,
2972 MBEDTLS_RSA_PRIVATE,
2973 output_length,
2974 input,
2975 output,
2976 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002977 }
2978 else
2979#endif /* MBEDTLS_PKCS1_V15 */
2980#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002981 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002982 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002983 psa_rsa_oaep_set_padding_mode( alg, rsa );
2984 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2985 mbedtls_ctr_drbg_random,
2986 &global_data.ctr_drbg,
2987 MBEDTLS_RSA_PRIVATE,
2988 salt, salt_length,
2989 output_length,
2990 input,
2991 output,
2992 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002993 }
2994 else
2995#endif /* MBEDTLS_PKCS1_V21 */
2996 {
2997 return( PSA_ERROR_INVALID_ARGUMENT );
2998 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002999
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003000 return( mbedtls_to_psa_error( ret ) );
3001 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003002 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003003#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003004 {
3005 return( PSA_ERROR_NOT_SUPPORTED );
3006 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003007}
Gilles Peskine20035e32018-02-03 22:44:14 +01003008
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003009
3010
mohammad1603503973b2018-03-12 15:59:30 +02003011/****************************************************************/
3012/* Symmetric cryptography */
3013/****************************************************************/
3014
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003015/* Initialize the cipher operation structure. Once this function has been
3016 * called, psa_cipher_abort can run and will do the right thing. */
3017static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3018 psa_algorithm_t alg )
3019{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003020 if( ! PSA_ALG_IS_CIPHER( alg ) )
3021 {
3022 memset( operation, 0, sizeof( *operation ) );
3023 return( PSA_ERROR_INVALID_ARGUMENT );
3024 }
3025
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003026 operation->alg = alg;
3027 operation->key_set = 0;
3028 operation->iv_set = 0;
3029 operation->iv_required = 1;
3030 operation->iv_size = 0;
3031 operation->block_size = 0;
3032 mbedtls_cipher_init( &operation->ctx.cipher );
3033 return( PSA_SUCCESS );
3034}
3035
Gilles Peskinee553c652018-06-04 16:22:46 +02003036static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003037 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003038 psa_algorithm_t alg,
3039 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003040{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003041 int ret = 0;
3042 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003043 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003044 size_t key_bits;
3045 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003046 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3047 PSA_KEY_USAGE_ENCRYPT :
3048 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003049
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003050 /* A context must be freshly initialized before it can be set up. */
3051 if( operation->alg != 0 )
3052 {
3053 return( PSA_ERROR_BAD_STATE );
3054 }
3055
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003056 status = psa_cipher_init( operation, alg );
3057 if( status != PSA_SUCCESS )
3058 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003059
Gilles Peskinec5487a82018-12-03 18:08:14 +01003060 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003061 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003062 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003063 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003064
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003065 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003066 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003067 {
3068 status = PSA_ERROR_NOT_SUPPORTED;
3069 goto exit;
3070 }
mohammad1603503973b2018-03-12 15:59:30 +02003071
mohammad1603503973b2018-03-12 15:59:30 +02003072 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003073 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003074 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003075
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003076#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003077 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003078 {
3079 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3080 unsigned char keys[24];
3081 memcpy( keys, slot->data.raw.data, 16 );
3082 memcpy( keys + 16, slot->data.raw.data, 8 );
3083 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3084 keys,
3085 192, cipher_operation );
3086 }
3087 else
3088#endif
3089 {
3090 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3091 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003092 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003093 }
Moran Peker41deec42018-04-04 15:43:05 +03003094 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003095 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003096
mohammad16038481e742018-03-18 13:57:31 +02003097#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003098 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003099 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003100 case PSA_ALG_CBC_NO_PADDING:
3101 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3102 MBEDTLS_PADDING_NONE );
3103 break;
3104 case PSA_ALG_CBC_PKCS7:
3105 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3106 MBEDTLS_PADDING_PKCS7 );
3107 break;
3108 default:
3109 /* The algorithm doesn't involve padding. */
3110 ret = 0;
3111 break;
3112 }
3113 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003114 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003115#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3116
mohammad1603503973b2018-03-12 15:59:30 +02003117 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003118 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3119 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3120 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003121 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003122 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003123 }
mohammad1603503973b2018-03-12 15:59:30 +02003124
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003125exit:
3126 if( status == 0 )
3127 status = mbedtls_to_psa_error( ret );
3128 if( status != 0 )
3129 psa_cipher_abort( operation );
3130 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003131}
3132
Gilles Peskinefe119512018-07-08 21:39:34 +02003133psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003134 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003135 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003136{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003137 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003138}
3139
Gilles Peskinefe119512018-07-08 21:39:34 +02003140psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003141 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003142 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003143{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003144 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003145}
3146
Gilles Peskinefe119512018-07-08 21:39:34 +02003147psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3148 unsigned char *iv,
3149 size_t iv_size,
3150 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003151{
itayzafrir534bd7c2018-08-02 13:56:32 +03003152 psa_status_t status;
3153 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003154 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003155 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003156 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003157 }
Moran Peker41deec42018-04-04 15:43:05 +03003158 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003159 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003160 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003161 goto exit;
3162 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003163 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3164 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003165 if( ret != 0 )
3166 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003167 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003168 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003169 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003170
mohammad16038481e742018-03-18 13:57:31 +02003171 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003172 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003173
Moran Peker395db872018-05-31 14:07:14 +03003174exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003175 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003176 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003177 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003178}
3179
Gilles Peskinefe119512018-07-08 21:39:34 +02003180psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3181 const unsigned char *iv,
3182 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003183{
itayzafrir534bd7c2018-08-02 13:56:32 +03003184 psa_status_t status;
3185 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003186 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003187 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003188 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003189 }
Moran Pekera28258c2018-05-29 16:25:04 +03003190 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003191 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003192 status = PSA_ERROR_INVALID_ARGUMENT;
3193 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003194 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003195 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3196 status = mbedtls_to_psa_error( ret );
3197exit:
3198 if( status == PSA_SUCCESS )
3199 operation->iv_set = 1;
3200 else
mohammad1603503973b2018-03-12 15:59:30 +02003201 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003202 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003203}
3204
Gilles Peskinee553c652018-06-04 16:22:46 +02003205psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3206 const uint8_t *input,
3207 size_t input_length,
3208 unsigned char *output,
3209 size_t output_size,
3210 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003211{
itayzafrir534bd7c2018-08-02 13:56:32 +03003212 psa_status_t status;
3213 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003214 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003215
3216 if( operation->alg == 0 )
3217 {
3218 return( PSA_ERROR_BAD_STATE );
3219 }
3220
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003221 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003222 {
3223 /* Take the unprocessed partial block left over from previous
3224 * update calls, if any, plus the input to this call. Remove
3225 * the last partial block, if any. You get the data that will be
3226 * output in this call. */
3227 expected_output_size =
3228 ( operation->ctx.cipher.unprocessed_len + input_length )
3229 / operation->block_size * operation->block_size;
3230 }
3231 else
3232 {
3233 expected_output_size = input_length;
3234 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003235
Gilles Peskine89d789c2018-06-04 17:17:16 +02003236 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003237 {
3238 status = PSA_ERROR_BUFFER_TOO_SMALL;
3239 goto exit;
3240 }
mohammad160382759612018-03-12 18:16:40 +02003241
mohammad1603503973b2018-03-12 15:59:30 +02003242 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003243 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003244 status = mbedtls_to_psa_error( ret );
3245exit:
3246 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003247 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003248 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003249}
3250
Gilles Peskinee553c652018-06-04 16:22:46 +02003251psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3252 uint8_t *output,
3253 size_t output_size,
3254 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003255{
David Saadab4ecc272019-02-14 13:48:10 +02003256 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003257 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003258 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003259
mohammad1603503973b2018-03-12 15:59:30 +02003260 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003261 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003262 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003263 }
3264 if( operation->iv_required && ! operation->iv_set )
3265 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003266 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003267 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003268
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003269 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003270 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3271 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003272 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003273 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003274 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003275 }
3276
Janos Follath315b51c2018-07-09 16:04:51 +01003277 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3278 temp_output_buffer,
3279 output_length );
3280 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003281 {
Janos Follath315b51c2018-07-09 16:04:51 +01003282 status = mbedtls_to_psa_error( cipher_ret );
3283 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003284 }
Janos Follath315b51c2018-07-09 16:04:51 +01003285
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003286 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003287 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003288 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003289 memcpy( output, temp_output_buffer, *output_length );
3290 else
3291 {
Janos Follath315b51c2018-07-09 16:04:51 +01003292 status = PSA_ERROR_BUFFER_TOO_SMALL;
3293 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003294 }
mohammad1603503973b2018-03-12 15:59:30 +02003295
Gilles Peskine3f108122018-12-07 18:14:53 +01003296 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003297 status = psa_cipher_abort( operation );
3298
3299 return( status );
3300
3301error:
3302
3303 *output_length = 0;
3304
Gilles Peskine3f108122018-12-07 18:14:53 +01003305 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003306 (void) psa_cipher_abort( operation );
3307
3308 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003309}
3310
Gilles Peskinee553c652018-06-04 16:22:46 +02003311psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3312{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003313 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003314 {
3315 /* The object has (apparently) been initialized but it is not
3316 * in use. It's ok to call abort on such an object, and there's
3317 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003318 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003319 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003320
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003321 /* Sanity check (shouldn't happen: operation->alg should
3322 * always have been initialized to a valid value). */
3323 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3324 return( PSA_ERROR_BAD_STATE );
3325
mohammad1603503973b2018-03-12 15:59:30 +02003326 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003327
Moran Peker41deec42018-04-04 15:43:05 +03003328 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003329 operation->key_set = 0;
3330 operation->iv_set = 0;
3331 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003332 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003333 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003334
Moran Peker395db872018-05-31 14:07:14 +03003335 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003336}
3337
Gilles Peskinea0655c32018-04-30 17:06:50 +02003338
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003339
mohammad16038cc1cee2018-03-28 01:21:33 +03003340/****************************************************************/
3341/* Key Policy */
3342/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003343
mohammad160327010052018-07-03 13:16:15 +03003344#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003345void psa_key_policy_set_usage( psa_key_policy_t *policy,
3346 psa_key_usage_t usage,
3347 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003348{
mohammad16034eed7572018-03-28 05:14:59 -07003349 policy->usage = usage;
3350 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003351}
3352
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003353psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003354{
mohammad16036df908f2018-04-02 08:34:15 -07003355 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003356}
3357
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003358psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003359{
mohammad16036df908f2018-04-02 08:34:15 -07003360 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003361}
mohammad160327010052018-07-03 13:16:15 +03003362#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003363
Gilles Peskinec5487a82018-12-03 18:08:14 +01003364psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003365 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003366{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003367 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003368 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003369
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003370 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003371 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003372
Gilles Peskinec5487a82018-12-03 18:08:14 +01003373 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003374 if( status != PSA_SUCCESS )
3375 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003376
Gilles Peskine4747d192019-04-17 15:05:45 +02003377 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003378}
3379
Gilles Peskinec5487a82018-12-03 18:08:14 +01003380psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003381 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003382{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003383 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003384 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003385
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003386 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003387 return( PSA_ERROR_INVALID_ARGUMENT );
3388
Gilles Peskinec5487a82018-12-03 18:08:14 +01003389 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003390 if( status != PSA_SUCCESS )
3391 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003392
mohammad16036df908f2018-04-02 08:34:15 -07003393 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003394
3395 return( PSA_SUCCESS );
3396}
Gilles Peskine20035e32018-02-03 22:44:14 +01003397
Gilles Peskinea0655c32018-04-30 17:06:50 +02003398
3399
mohammad1603804cd712018-03-20 22:44:08 +02003400/****************************************************************/
3401/* Key Lifetime */
3402/****************************************************************/
3403
Gilles Peskine87a5e562019-04-17 12:28:25 +02003404psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003405 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003406{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003407 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003408 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003409
Gilles Peskinec5487a82018-12-03 18:08:14 +01003410 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003411 if( status != PSA_SUCCESS )
3412 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003413
mohammad1603804cd712018-03-20 22:44:08 +02003414 *lifetime = slot->lifetime;
3415
3416 return( PSA_SUCCESS );
3417}
3418
Gilles Peskine20035e32018-02-03 22:44:14 +01003419
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003420
mohammad16035955c982018-04-26 00:53:03 +03003421/****************************************************************/
3422/* AEAD */
3423/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003424
Gilles Peskineedf9a652018-08-17 18:11:56 +02003425typedef struct
3426{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003427 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003428 const mbedtls_cipher_info_t *cipher_info;
3429 union
3430 {
3431#if defined(MBEDTLS_CCM_C)
3432 mbedtls_ccm_context ccm;
3433#endif /* MBEDTLS_CCM_C */
3434#if defined(MBEDTLS_GCM_C)
3435 mbedtls_gcm_context gcm;
3436#endif /* MBEDTLS_GCM_C */
3437 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003438 psa_algorithm_t core_alg;
3439 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003440 uint8_t tag_length;
3441} aead_operation_t;
3442
Gilles Peskine30a9e412019-01-14 18:36:12 +01003443static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003444{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003445 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003446 {
3447#if defined(MBEDTLS_CCM_C)
3448 case PSA_ALG_CCM:
3449 mbedtls_ccm_free( &operation->ctx.ccm );
3450 break;
3451#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003452#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003453 case PSA_ALG_GCM:
3454 mbedtls_gcm_free( &operation->ctx.gcm );
3455 break;
3456#endif /* MBEDTLS_GCM_C */
3457 }
3458}
3459
3460static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003461 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003462 psa_key_usage_t usage,
3463 psa_algorithm_t alg )
3464{
3465 psa_status_t status;
3466 size_t key_bits;
3467 mbedtls_cipher_id_t cipher_id;
3468
Gilles Peskinec5487a82018-12-03 18:08:14 +01003469 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003470 if( status != PSA_SUCCESS )
3471 return( status );
3472
3473 key_bits = psa_get_key_bits( operation->slot );
3474
3475 operation->cipher_info =
3476 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3477 &cipher_id );
3478 if( operation->cipher_info == NULL )
3479 return( PSA_ERROR_NOT_SUPPORTED );
3480
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003481 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003482 {
3483#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003484 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3485 operation->core_alg = PSA_ALG_CCM;
3486 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003487 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3488 return( PSA_ERROR_INVALID_ARGUMENT );
3489 mbedtls_ccm_init( &operation->ctx.ccm );
3490 status = mbedtls_to_psa_error(
3491 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3492 operation->slot->data.raw.data,
3493 (unsigned int) key_bits ) );
3494 if( status != 0 )
3495 goto cleanup;
3496 break;
3497#endif /* MBEDTLS_CCM_C */
3498
3499#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003500 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3501 operation->core_alg = PSA_ALG_GCM;
3502 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003503 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3504 return( PSA_ERROR_INVALID_ARGUMENT );
3505 mbedtls_gcm_init( &operation->ctx.gcm );
3506 status = mbedtls_to_psa_error(
3507 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3508 operation->slot->data.raw.data,
3509 (unsigned int) key_bits ) );
3510 break;
3511#endif /* MBEDTLS_GCM_C */
3512
3513 default:
3514 return( PSA_ERROR_NOT_SUPPORTED );
3515 }
3516
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003517 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3518 {
3519 status = PSA_ERROR_INVALID_ARGUMENT;
3520 goto cleanup;
3521 }
3522 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3523 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3524 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3525 * In both cases, mbedtls_xxx will validate the tag length below. */
3526
Gilles Peskineedf9a652018-08-17 18:11:56 +02003527 return( PSA_SUCCESS );
3528
3529cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003530 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003531 return( status );
3532}
3533
Gilles Peskinec5487a82018-12-03 18:08:14 +01003534psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003535 psa_algorithm_t alg,
3536 const uint8_t *nonce,
3537 size_t nonce_length,
3538 const uint8_t *additional_data,
3539 size_t additional_data_length,
3540 const uint8_t *plaintext,
3541 size_t plaintext_length,
3542 uint8_t *ciphertext,
3543 size_t ciphertext_size,
3544 size_t *ciphertext_length )
3545{
mohammad16035955c982018-04-26 00:53:03 +03003546 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003547 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003548 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003549
mohammad1603f08a5502018-06-03 15:05:47 +03003550 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003551
Gilles Peskinec5487a82018-12-03 18:08:14 +01003552 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003553 if( status != PSA_SUCCESS )
3554 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003555
Gilles Peskineedf9a652018-08-17 18:11:56 +02003556 /* For all currently supported modes, the tag is at the end of the
3557 * ciphertext. */
3558 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3559 {
3560 status = PSA_ERROR_BUFFER_TOO_SMALL;
3561 goto exit;
3562 }
3563 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003564
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003565#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003566 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003567 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003568 status = mbedtls_to_psa_error(
3569 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3570 MBEDTLS_GCM_ENCRYPT,
3571 plaintext_length,
3572 nonce, nonce_length,
3573 additional_data, additional_data_length,
3574 plaintext, ciphertext,
3575 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003576 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003577 else
3578#endif /* MBEDTLS_GCM_C */
3579#if defined(MBEDTLS_CCM_C)
3580 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003581 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003582 status = mbedtls_to_psa_error(
3583 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3584 plaintext_length,
3585 nonce, nonce_length,
3586 additional_data,
3587 additional_data_length,
3588 plaintext, ciphertext,
3589 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003590 }
mohammad16035c8845f2018-05-09 05:40:09 -07003591 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003592#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003593 {
mohammad1603554faad2018-06-03 15:07:38 +03003594 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003595 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003596
Gilles Peskineedf9a652018-08-17 18:11:56 +02003597 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3598 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003599
Gilles Peskineedf9a652018-08-17 18:11:56 +02003600exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003601 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003602 if( status == PSA_SUCCESS )
3603 *ciphertext_length = plaintext_length + operation.tag_length;
3604 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003605}
3606
Gilles Peskineee652a32018-06-01 19:23:52 +02003607/* Locate the tag in a ciphertext buffer containing the encrypted data
3608 * followed by the tag. Return the length of the part preceding the tag in
3609 * *plaintext_length. This is the size of the plaintext in modes where
3610 * the encrypted data has the same size as the plaintext, such as
3611 * CCM and GCM. */
3612static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3613 const uint8_t *ciphertext,
3614 size_t ciphertext_length,
3615 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003616 const uint8_t **p_tag )
3617{
3618 size_t payload_length;
3619 if( tag_length > ciphertext_length )
3620 return( PSA_ERROR_INVALID_ARGUMENT );
3621 payload_length = ciphertext_length - tag_length;
3622 if( payload_length > plaintext_size )
3623 return( PSA_ERROR_BUFFER_TOO_SMALL );
3624 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003625 return( PSA_SUCCESS );
3626}
3627
Gilles Peskinec5487a82018-12-03 18:08:14 +01003628psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003629 psa_algorithm_t alg,
3630 const uint8_t *nonce,
3631 size_t nonce_length,
3632 const uint8_t *additional_data,
3633 size_t additional_data_length,
3634 const uint8_t *ciphertext,
3635 size_t ciphertext_length,
3636 uint8_t *plaintext,
3637 size_t plaintext_size,
3638 size_t *plaintext_length )
3639{
mohammad16035955c982018-04-26 00:53:03 +03003640 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003641 aead_operation_t operation;
3642 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003643
Gilles Peskineee652a32018-06-01 19:23:52 +02003644 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003645
Gilles Peskinec5487a82018-12-03 18:08:14 +01003646 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003647 if( status != PSA_SUCCESS )
3648 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003649
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003650#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003651 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003652 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003653 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003654 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003655 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003656 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003657 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003658
Gilles Peskineedf9a652018-08-17 18:11:56 +02003659 status = mbedtls_to_psa_error(
3660 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3661 ciphertext_length - operation.tag_length,
3662 nonce, nonce_length,
3663 additional_data,
3664 additional_data_length,
3665 tag, operation.tag_length,
3666 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003667 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003668 else
3669#endif /* MBEDTLS_GCM_C */
3670#if defined(MBEDTLS_CCM_C)
3671 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003672 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003673 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003674 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003675 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003676 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003677 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003678
Gilles Peskineedf9a652018-08-17 18:11:56 +02003679 status = mbedtls_to_psa_error(
3680 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3681 ciphertext_length - operation.tag_length,
3682 nonce, nonce_length,
3683 additional_data,
3684 additional_data_length,
3685 ciphertext, plaintext,
3686 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003687 }
mohammad160339574652018-06-01 04:39:53 -07003688 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003689#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003690 {
mohammad1603554faad2018-06-03 15:07:38 +03003691 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003692 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003693
Gilles Peskineedf9a652018-08-17 18:11:56 +02003694 if( status != PSA_SUCCESS && plaintext_size != 0 )
3695 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003696
Gilles Peskineedf9a652018-08-17 18:11:56 +02003697exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003698 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003699 if( status == PSA_SUCCESS )
3700 *plaintext_length = ciphertext_length - operation.tag_length;
3701 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003702}
3703
Gilles Peskinea0655c32018-04-30 17:06:50 +02003704
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003705
Gilles Peskine20035e32018-02-03 22:44:14 +01003706/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003707/* Generators */
3708/****************************************************************/
3709
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003710#define HKDF_STATE_INIT 0 /* no input yet */
3711#define HKDF_STATE_STARTED 1 /* got salt */
3712#define HKDF_STATE_KEYED 2 /* got key */
3713#define HKDF_STATE_OUTPUT 3 /* output started */
3714
Gilles Peskine969c5d62019-01-16 15:53:06 +01003715static psa_algorithm_t psa_generator_get_kdf_alg(
3716 const psa_crypto_generator_t *generator )
3717{
3718 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3719 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3720 else
3721 return( generator->alg );
3722}
3723
3724
Gilles Peskineeab56e42018-07-12 17:12:33 +02003725psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3726{
3727 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003728 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3729 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003730 {
3731 /* The object has (apparently) been initialized but it is not
3732 * in use. It's ok to call abort on such an object, and there's
3733 * nothing to do. */
3734 }
3735 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003736 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003737 {
3738 if( generator->ctx.buffer.data != NULL )
3739 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003740 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003741 generator->ctx.buffer.size );
3742 mbedtls_free( generator->ctx.buffer.data );
3743 }
3744 }
3745 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003746#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003747 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003748 {
3749 mbedtls_free( generator->ctx.hkdf.info );
3750 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3751 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003752 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00003753 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003754 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003755 {
3756 if( generator->ctx.tls12_prf.key != NULL )
3757 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003758 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003759 generator->ctx.tls12_prf.key_len );
3760 mbedtls_free( generator->ctx.tls12_prf.key );
3761 }
Hanno Becker580fba12018-11-13 20:50:45 +00003762
3763 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3764 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003765 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003766 generator->ctx.tls12_prf.Ai_with_seed_len );
3767 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3768 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003769 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003770 else
3771#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003772 {
3773 status = PSA_ERROR_BAD_STATE;
3774 }
3775 memset( generator, 0, sizeof( *generator ) );
3776 return( status );
3777}
3778
Gilles Peskineeab56e42018-07-12 17:12:33 +02003779psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3780 size_t *capacity)
3781{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003782 if( generator->alg == 0 )
3783 {
3784 /* This is a blank generator. */
3785 return PSA_ERROR_BAD_STATE;
3786 }
3787
Gilles Peskineeab56e42018-07-12 17:12:33 +02003788 *capacity = generator->capacity;
3789 return( PSA_SUCCESS );
3790}
3791
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003792psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
3793 size_t capacity )
3794{
3795 if( generator->alg == 0 )
3796 return( PSA_ERROR_BAD_STATE );
3797 if( capacity > generator->capacity )
3798 return( PSA_ERROR_INVALID_ARGUMENT );
3799 generator->capacity = capacity;
3800 return( PSA_SUCCESS );
3801}
3802
Gilles Peskinebef7f142018-07-12 17:22:21 +02003803#if defined(MBEDTLS_MD_C)
3804/* Read some bytes from an HKDF-based generator. This performs a chunk
3805 * of the expand phase of the HKDF algorithm. */
3806static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3807 psa_algorithm_t hash_alg,
3808 uint8_t *output,
3809 size_t output_length )
3810{
3811 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3812 psa_status_t status;
3813
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003814 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3815 return( PSA_ERROR_BAD_STATE );
3816 hkdf->state = HKDF_STATE_OUTPUT;
3817
Gilles Peskinebef7f142018-07-12 17:22:21 +02003818 while( output_length != 0 )
3819 {
3820 /* Copy what remains of the current block */
3821 uint8_t n = hash_length - hkdf->offset_in_block;
3822 if( n > output_length )
3823 n = (uint8_t) output_length;
3824 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3825 output += n;
3826 output_length -= n;
3827 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003828 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003829 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003830 /* We can't be wanting more output after block 0xff, otherwise
3831 * the capacity check in psa_generator_read() would have
3832 * prevented this call. It could happen only if the generator
3833 * object was corrupted or if this function is called directly
3834 * inside the library. */
3835 if( hkdf->block_number == 0xff )
3836 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003837
3838 /* We need a new block */
3839 ++hkdf->block_number;
3840 hkdf->offset_in_block = 0;
3841 status = psa_hmac_setup_internal( &hkdf->hmac,
3842 hkdf->prk, hash_length,
3843 hash_alg );
3844 if( status != PSA_SUCCESS )
3845 return( status );
3846 if( hkdf->block_number != 1 )
3847 {
3848 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3849 hkdf->output_block,
3850 hash_length );
3851 if( status != PSA_SUCCESS )
3852 return( status );
3853 }
3854 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3855 hkdf->info,
3856 hkdf->info_length );
3857 if( status != PSA_SUCCESS )
3858 return( status );
3859 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3860 &hkdf->block_number, 1 );
3861 if( status != PSA_SUCCESS )
3862 return( status );
3863 status = psa_hmac_finish_internal( &hkdf->hmac,
3864 hkdf->output_block,
3865 sizeof( hkdf->output_block ) );
3866 if( status != PSA_SUCCESS )
3867 return( status );
3868 }
3869
3870 return( PSA_SUCCESS );
3871}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003872
3873static psa_status_t psa_generator_tls12_prf_generate_next_block(
3874 psa_tls12_prf_generator_t *tls12_prf,
3875 psa_algorithm_t alg )
3876{
3877 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3878 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3879 psa_hmac_internal_data hmac;
3880 psa_status_t status, cleanup_status;
3881
Hanno Becker3b339e22018-11-13 20:56:14 +00003882 unsigned char *Ai;
3883 size_t Ai_len;
3884
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003885 /* We can't be wanting more output after block 0xff, otherwise
3886 * the capacity check in psa_generator_read() would have
3887 * prevented this call. It could happen only if the generator
3888 * object was corrupted or if this function is called directly
3889 * inside the library. */
3890 if( tls12_prf->block_number == 0xff )
3891 return( PSA_ERROR_BAD_STATE );
3892
3893 /* We need a new block */
3894 ++tls12_prf->block_number;
3895 tls12_prf->offset_in_block = 0;
3896
3897 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3898 *
3899 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3900 *
3901 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3902 * HMAC_hash(secret, A(2) + seed) +
3903 * HMAC_hash(secret, A(3) + seed) + ...
3904 *
3905 * A(0) = seed
3906 * A(i) = HMAC_hash( secret, A(i-1) )
3907 *
3908 * The `psa_tls12_prf_generator` structures saves the block
3909 * `HMAC_hash(secret, A(i) + seed)` from which the output
3910 * is currently extracted as `output_block`, while
3911 * `A(i) + seed` is stored in `Ai_with_seed`.
3912 *
3913 * Generating a new block means recalculating `Ai_with_seed`
3914 * from the A(i)-part of it, and afterwards recalculating
3915 * `output_block`.
3916 *
3917 * A(0) is computed at setup time.
3918 *
3919 */
3920
3921 psa_hmac_init_internal( &hmac );
3922
3923 /* We must distinguish the calculation of A(1) from those
3924 * of A(2) and higher, because A(0)=seed has a different
3925 * length than the other A(i). */
3926 if( tls12_prf->block_number == 1 )
3927 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003928 Ai = tls12_prf->Ai_with_seed + hash_length;
3929 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003930 }
3931 else
3932 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003933 Ai = tls12_prf->Ai_with_seed;
3934 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003935 }
3936
Hanno Becker3b339e22018-11-13 20:56:14 +00003937 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3938 status = psa_hmac_setup_internal( &hmac,
3939 tls12_prf->key,
3940 tls12_prf->key_len,
3941 hash_alg );
3942 if( status != PSA_SUCCESS )
3943 goto cleanup;
3944
3945 status = psa_hash_update( &hmac.hash_ctx,
3946 Ai, Ai_len );
3947 if( status != PSA_SUCCESS )
3948 goto cleanup;
3949
3950 status = psa_hmac_finish_internal( &hmac,
3951 tls12_prf->Ai_with_seed,
3952 hash_length );
3953 if( status != PSA_SUCCESS )
3954 goto cleanup;
3955
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003956 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3957 status = psa_hmac_setup_internal( &hmac,
3958 tls12_prf->key,
3959 tls12_prf->key_len,
3960 hash_alg );
3961 if( status != PSA_SUCCESS )
3962 goto cleanup;
3963
3964 status = psa_hash_update( &hmac.hash_ctx,
3965 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003966 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003967 if( status != PSA_SUCCESS )
3968 goto cleanup;
3969
3970 status = psa_hmac_finish_internal( &hmac,
3971 tls12_prf->output_block,
3972 hash_length );
3973 if( status != PSA_SUCCESS )
3974 goto cleanup;
3975
3976cleanup:
3977
3978 cleanup_status = psa_hmac_abort_internal( &hmac );
3979 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3980 status = cleanup_status;
3981
3982 return( status );
3983}
3984
3985/* Read some bytes from an TLS-1.2-PRF-based generator.
3986 * See Section 5 of RFC 5246. */
3987static psa_status_t psa_generator_tls12_prf_read(
3988 psa_tls12_prf_generator_t *tls12_prf,
3989 psa_algorithm_t alg,
3990 uint8_t *output,
3991 size_t output_length )
3992{
3993 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3994 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3995 psa_status_t status;
3996
3997 while( output_length != 0 )
3998 {
3999 /* Copy what remains of the current block */
4000 uint8_t n = hash_length - tls12_prf->offset_in_block;
4001
4002 /* Check if we have fully processed the current block. */
4003 if( n == 0 )
4004 {
4005 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
4006 alg );
4007 if( status != PSA_SUCCESS )
4008 return( status );
4009
4010 continue;
4011 }
4012
4013 if( n > output_length )
4014 n = (uint8_t) output_length;
4015 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4016 n );
4017 output += n;
4018 output_length -= n;
4019 tls12_prf->offset_in_block += n;
4020 }
4021
4022 return( PSA_SUCCESS );
4023}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004024#endif /* MBEDTLS_MD_C */
4025
Gilles Peskineeab56e42018-07-12 17:12:33 +02004026psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
4027 uint8_t *output,
4028 size_t output_length )
4029{
4030 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004031 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004032
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004033 if( generator->alg == 0 )
4034 {
4035 /* This is a blank generator. */
4036 return PSA_ERROR_BAD_STATE;
4037 }
4038
Gilles Peskineeab56e42018-07-12 17:12:33 +02004039 if( output_length > generator->capacity )
4040 {
4041 generator->capacity = 0;
4042 /* Go through the error path to wipe all confidential data now
4043 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004044 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004045 goto exit;
4046 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004047 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004048 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004049 /* Edge case: this is a finished generator, and 0 bytes
4050 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004051 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4052 * INSUFFICIENT_CAPACITY, which is right for a finished
4053 * generator, for consistency with the case when
4054 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004055 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004056 }
4057 generator->capacity -= output_length;
4058
Gilles Peskine969c5d62019-01-16 15:53:06 +01004059 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004060 {
Gilles Peskine211a4362018-10-25 22:22:31 +02004061 /* Initially, the capacity of a selection generator is always
4062 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
4063 * abbreviated in this comment as `size`. When the remaining
4064 * capacity is `c`, the next bytes to serve start `c` bytes
4065 * from the end of the buffer, i.e. `size - c` from the
4066 * beginning of the buffer. Since `generator->capacity` was just
4067 * decremented above, we need to serve the bytes from
4068 * `size - generator->capacity - output_length` to
4069 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004070 size_t offset =
4071 generator->ctx.buffer.size - generator->capacity - output_length;
4072 memcpy( output, generator->ctx.buffer.data + offset, output_length );
4073 status = PSA_SUCCESS;
4074 }
4075 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004076#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004077 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004078 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004079 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004080 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
4081 output, output_length );
4082 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004083 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4084 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004085 {
4086 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004087 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004088 output_length );
4089 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004090 else
4091#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004092 {
4093 return( PSA_ERROR_BAD_STATE );
4094 }
4095
4096exit:
4097 if( status != PSA_SUCCESS )
4098 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004099 /* Preserve the algorithm upon errors, but clear all sensitive state.
4100 * This allows us to differentiate between exhausted generators and
4101 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
4102 * generators. */
4103 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004104 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004105 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004106 memset( output, '!', output_length );
4107 }
4108 return( status );
4109}
4110
Gilles Peskine08542d82018-07-19 17:05:42 +02004111#if defined(MBEDTLS_DES_C)
4112static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4113{
4114 if( data_size >= 8 )
4115 mbedtls_des_key_set_parity( data );
4116 if( data_size >= 16 )
4117 mbedtls_des_key_set_parity( data + 8 );
4118 if( data_size >= 24 )
4119 mbedtls_des_key_set_parity( data + 16 );
4120}
4121#endif /* MBEDTLS_DES_C */
4122
Gilles Peskine87a5e562019-04-17 12:28:25 +02004123psa_status_t psa_generator_import_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004124 psa_key_type_t type,
4125 size_t bits,
4126 psa_crypto_generator_t *generator )
4127{
4128 uint8_t *data = NULL;
4129 size_t bytes = PSA_BITS_TO_BYTES( bits );
4130 psa_status_t status;
4131
4132 if( ! key_type_is_raw_bytes( type ) )
4133 return( PSA_ERROR_INVALID_ARGUMENT );
4134 if( bits % 8 != 0 )
4135 return( PSA_ERROR_INVALID_ARGUMENT );
4136 data = mbedtls_calloc( 1, bytes );
4137 if( data == NULL )
4138 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4139
4140 status = psa_generator_read( generator, data, bytes );
4141 if( status != PSA_SUCCESS )
4142 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004143#if defined(MBEDTLS_DES_C)
4144 if( type == PSA_KEY_TYPE_DES )
4145 psa_des_set_key_parity( data, bytes );
4146#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004147 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004148
4149exit:
4150 mbedtls_free( data );
4151 return( status );
4152}
4153
4154
4155
4156/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004157/* Key derivation */
4158/****************************************************************/
4159
Gilles Peskinea05219c2018-11-16 16:02:56 +01004160#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004161/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004162 * of the HKDF algorithm.
4163 *
4164 * Note that if this function fails, you must call psa_generator_abort()
4165 * to potentially free embedded data structures and wipe confidential data.
4166 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004167static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004168 const uint8_t *secret,
4169 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004170 psa_algorithm_t hash_alg,
4171 const uint8_t *salt,
4172 size_t salt_length,
4173 const uint8_t *label,
4174 size_t label_length )
4175{
4176 psa_status_t status;
4177 status = psa_hmac_setup_internal( &hkdf->hmac,
4178 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004179 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004180 if( status != PSA_SUCCESS )
4181 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004182 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004183 if( status != PSA_SUCCESS )
4184 return( status );
4185 status = psa_hmac_finish_internal( &hkdf->hmac,
4186 hkdf->prk,
4187 sizeof( hkdf->prk ) );
4188 if( status != PSA_SUCCESS )
4189 return( status );
4190 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4191 hkdf->block_number = 0;
4192 hkdf->info_length = label_length;
4193 if( label_length != 0 )
4194 {
4195 hkdf->info = mbedtls_calloc( 1, label_length );
4196 if( hkdf->info == NULL )
4197 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4198 memcpy( hkdf->info, label, label_length );
4199 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004200 hkdf->state = HKDF_STATE_KEYED;
4201 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004202 return( PSA_SUCCESS );
4203}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004204#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004205
Gilles Peskinea05219c2018-11-16 16:02:56 +01004206#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004207/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4208 *
4209 * Note that if this function fails, you must call psa_generator_abort()
4210 * to potentially free embedded data structures and wipe confidential data.
4211 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004212static psa_status_t psa_generator_tls12_prf_setup(
4213 psa_tls12_prf_generator_t *tls12_prf,
4214 const unsigned char *key,
4215 size_t key_len,
4216 psa_algorithm_t hash_alg,
4217 const uint8_t *salt,
4218 size_t salt_length,
4219 const uint8_t *label,
4220 size_t label_length )
4221{
4222 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004223 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4224 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004225
4226 tls12_prf->key = mbedtls_calloc( 1, key_len );
4227 if( tls12_prf->key == NULL )
4228 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4229 tls12_prf->key_len = key_len;
4230 memcpy( tls12_prf->key, key, key_len );
4231
Hanno Becker580fba12018-11-13 20:50:45 +00004232 overflow = ( salt_length + label_length < salt_length ) ||
4233 ( salt_length + label_length + hash_length < hash_length );
4234 if( overflow )
4235 return( PSA_ERROR_INVALID_ARGUMENT );
4236
4237 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4238 if( tls12_prf->Ai_with_seed == NULL )
4239 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4240 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4241
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004242 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4243 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004244 if( label_length != 0 )
4245 {
4246 memcpy( tls12_prf->Ai_with_seed + hash_length,
4247 label, label_length );
4248 }
4249
4250 if( salt_length != 0 )
4251 {
4252 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4253 salt, salt_length );
4254 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004255
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004256 /* The first block gets generated when
4257 * psa_generator_read() is called. */
4258 tls12_prf->block_number = 0;
4259 tls12_prf->offset_in_block = hash_length;
4260
4261 return( PSA_SUCCESS );
4262}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004263
4264/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4265static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4266 psa_tls12_prf_generator_t *tls12_prf,
4267 const unsigned char *psk,
4268 size_t psk_len,
4269 psa_algorithm_t hash_alg,
4270 const uint8_t *salt,
4271 size_t salt_length,
4272 const uint8_t *label,
4273 size_t label_length )
4274{
4275 psa_status_t status;
4276 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4277
4278 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4279 return( PSA_ERROR_INVALID_ARGUMENT );
4280
4281 /* Quoting RFC 4279, Section 2:
4282 *
4283 * The premaster secret is formed as follows: if the PSK is N octets
4284 * long, concatenate a uint16 with the value N, N zero octets, a second
4285 * uint16 with the value N, and the PSK itself.
4286 */
4287
4288 pms[0] = ( psk_len >> 8 ) & 0xff;
4289 pms[1] = ( psk_len >> 0 ) & 0xff;
4290 memset( pms + 2, 0, psk_len );
4291 pms[2 + psk_len + 0] = pms[0];
4292 pms[2 + psk_len + 1] = pms[1];
4293 memcpy( pms + 4 + psk_len, psk, psk_len );
4294
4295 status = psa_generator_tls12_prf_setup( tls12_prf,
4296 pms, 4 + 2 * psk_len,
4297 hash_alg,
4298 salt, salt_length,
4299 label, label_length );
4300
Gilles Peskine3f108122018-12-07 18:14:53 +01004301 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004302 return( status );
4303}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004304#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004305
Gilles Peskine346797d2018-11-16 16:05:06 +01004306/* Note that if this function fails, you must call psa_generator_abort()
4307 * to potentially free embedded data structures and wipe confidential data.
4308 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004309static psa_status_t psa_key_derivation_internal(
4310 psa_crypto_generator_t *generator,
4311 const uint8_t *secret, size_t secret_length,
4312 psa_algorithm_t alg,
4313 const uint8_t *salt, size_t salt_length,
4314 const uint8_t *label, size_t label_length,
4315 size_t capacity )
4316{
4317 psa_status_t status;
4318 size_t max_capacity;
4319
4320 /* Set generator->alg even on failure so that abort knows what to do. */
4321 generator->alg = alg;
4322
Gilles Peskine751d9652018-09-18 12:05:44 +02004323 if( alg == PSA_ALG_SELECT_RAW )
4324 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004325 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004326 if( salt_length != 0 )
4327 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004328 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004329 if( label_length != 0 )
4330 return( PSA_ERROR_INVALID_ARGUMENT );
4331 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4332 if( generator->ctx.buffer.data == NULL )
4333 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4334 memcpy( generator->ctx.buffer.data, secret, secret_length );
4335 generator->ctx.buffer.size = secret_length;
4336 max_capacity = secret_length;
4337 status = PSA_SUCCESS;
4338 }
4339 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004340#if defined(MBEDTLS_MD_C)
4341 if( PSA_ALG_IS_HKDF( alg ) )
4342 {
4343 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4344 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4345 if( hash_size == 0 )
4346 return( PSA_ERROR_NOT_SUPPORTED );
4347 max_capacity = 255 * hash_size;
4348 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4349 secret, secret_length,
4350 hash_alg,
4351 salt, salt_length,
4352 label, label_length );
4353 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004354 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4355 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4356 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004357 {
4358 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4359 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4360
4361 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4362 if( hash_alg != PSA_ALG_SHA_256 &&
4363 hash_alg != PSA_ALG_SHA_384 )
4364 {
4365 return( PSA_ERROR_NOT_SUPPORTED );
4366 }
4367
4368 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004369
4370 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4371 {
4372 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4373 secret, secret_length,
4374 hash_alg, salt, salt_length,
4375 label, label_length );
4376 }
4377 else
4378 {
4379 status = psa_generator_tls12_psk_to_ms_setup(
4380 &generator->ctx.tls12_prf,
4381 secret, secret_length,
4382 hash_alg, salt, salt_length,
4383 label, label_length );
4384 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004385 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004386 else
4387#endif
4388 {
4389 return( PSA_ERROR_NOT_SUPPORTED );
4390 }
4391
4392 if( status != PSA_SUCCESS )
4393 return( status );
4394
4395 if( capacity <= max_capacity )
4396 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004397 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4398 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004399 else
4400 return( PSA_ERROR_INVALID_ARGUMENT );
4401
4402 return( PSA_SUCCESS );
4403}
4404
Gilles Peskineea0fb492018-07-12 17:17:20 +02004405psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004406 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004407 psa_algorithm_t alg,
4408 const uint8_t *salt,
4409 size_t salt_length,
4410 const uint8_t *label,
4411 size_t label_length,
4412 size_t capacity )
4413{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004414 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004415 psa_status_t status;
4416
4417 if( generator->alg != 0 )
4418 return( PSA_ERROR_BAD_STATE );
4419
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004420 /* Make sure that alg is a key derivation algorithm. This prevents
4421 * key selection algorithms, which psa_key_derivation_internal
4422 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004423 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4424 return( PSA_ERROR_INVALID_ARGUMENT );
4425
Gilles Peskinec5487a82018-12-03 18:08:14 +01004426 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004427 if( status != PSA_SUCCESS )
4428 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004429
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004430 if( slot->type != PSA_KEY_TYPE_DERIVE )
4431 return( PSA_ERROR_INVALID_ARGUMENT );
4432
4433 status = psa_key_derivation_internal( generator,
4434 slot->data.raw.data,
4435 slot->data.raw.bytes,
4436 alg,
4437 salt, salt_length,
4438 label, label_length,
4439 capacity );
4440 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004441 psa_generator_abort( generator );
4442 return( status );
4443}
4444
Gilles Peskine969c5d62019-01-16 15:53:06 +01004445static psa_status_t psa_key_derivation_setup_kdf(
4446 psa_crypto_generator_t *generator,
4447 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004448{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004449 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004450#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004451 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4452 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4453 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004454 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004455 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004456 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4457 if( hash_size == 0 )
4458 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004459 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4460 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004461 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004462 {
4463 return( PSA_ERROR_NOT_SUPPORTED );
4464 }
4465 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004466 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004467 }
4468#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004469 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004470 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004471}
4472
4473psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4474 psa_algorithm_t alg )
4475{
4476 psa_status_t status;
4477
4478 if( generator->alg != 0 )
4479 return( PSA_ERROR_BAD_STATE );
4480
Gilles Peskine6843c292019-01-18 16:44:49 +01004481 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4482 return( PSA_ERROR_INVALID_ARGUMENT );
4483 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004484 {
4485 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004486 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004487 }
4488 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4489 {
4490 status = psa_key_derivation_setup_kdf( generator, alg );
4491 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004492 else
4493 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004494
4495 if( status == PSA_SUCCESS )
4496 generator->alg = alg;
4497 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004498}
4499
4500#if defined(MBEDTLS_MD_C)
4501static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4502 psa_algorithm_t hash_alg,
4503 psa_key_derivation_step_t step,
4504 const uint8_t *data,
4505 size_t data_length )
4506{
4507 psa_status_t status;
4508 switch( step )
4509 {
4510 case PSA_KDF_STEP_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004511 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004512 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004513 status = psa_hmac_setup_internal( &hkdf->hmac,
4514 data, data_length,
4515 hash_alg );
4516 if( status != PSA_SUCCESS )
4517 return( status );
4518 hkdf->state = HKDF_STATE_STARTED;
4519 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004520 case PSA_KDF_STEP_SECRET:
4521 /* If no salt was provided, use an empty salt. */
4522 if( hkdf->state == HKDF_STATE_INIT )
4523 {
4524 status = psa_hmac_setup_internal( &hkdf->hmac,
4525 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004526 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004527 if( status != PSA_SUCCESS )
4528 return( status );
4529 hkdf->state = HKDF_STATE_STARTED;
4530 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004531 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004532 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004533 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4534 data, data_length );
4535 if( status != PSA_SUCCESS )
4536 return( status );
4537 status = psa_hmac_finish_internal( &hkdf->hmac,
4538 hkdf->prk,
4539 sizeof( hkdf->prk ) );
4540 if( status != PSA_SUCCESS )
4541 return( status );
4542 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4543 hkdf->block_number = 0;
4544 hkdf->state = HKDF_STATE_KEYED;
4545 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004546 case PSA_KDF_STEP_INFO:
4547 if( hkdf->state == HKDF_STATE_OUTPUT )
4548 return( PSA_ERROR_BAD_STATE );
4549 if( hkdf->info_set )
4550 return( PSA_ERROR_BAD_STATE );
4551 hkdf->info_length = data_length;
4552 if( data_length != 0 )
4553 {
4554 hkdf->info = mbedtls_calloc( 1, data_length );
4555 if( hkdf->info == NULL )
4556 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4557 memcpy( hkdf->info, data, data_length );
4558 }
4559 hkdf->info_set = 1;
4560 return( PSA_SUCCESS );
4561 default:
4562 return( PSA_ERROR_INVALID_ARGUMENT );
4563 }
4564}
4565#endif /* MBEDTLS_MD_C */
4566
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004567static psa_status_t psa_key_derivation_input_raw(
4568 psa_crypto_generator_t *generator,
4569 psa_key_derivation_step_t step,
4570 const uint8_t *data,
4571 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004572{
4573 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004574 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004575
Gilles Peskine969c5d62019-01-16 15:53:06 +01004576 if( kdf_alg == PSA_ALG_SELECT_RAW )
4577 {
4578 if( generator->capacity != 0 )
4579 return( PSA_ERROR_INVALID_ARGUMENT );
4580 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4581 if( generator->ctx.buffer.data == NULL )
4582 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4583 memcpy( generator->ctx.buffer.data, data, data_length );
4584 generator->ctx.buffer.size = data_length;
4585 generator->capacity = data_length;
4586 status = PSA_SUCCESS;
4587 }
4588 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004589#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004590 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004591 {
4592 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004593 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004594 step, data, data_length );
4595 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004596 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004597#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004598#if defined(MBEDTLS_MD_C)
4599 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004600 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4601 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004602 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004603 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004604 status = PSA_ERROR_NOT_SUPPORTED;
4605 }
4606 else
4607#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004608 {
4609 /* This can't happen unless the generator object was not initialized */
4610 return( PSA_ERROR_BAD_STATE );
4611 }
4612
4613 if( status != PSA_SUCCESS )
4614 psa_generator_abort( generator );
4615 return( status );
4616}
4617
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004618psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4619 psa_key_derivation_step_t step,
4620 const uint8_t *data,
4621 size_t data_length )
4622{
4623 switch( step )
4624 {
4625 case PSA_KDF_STEP_LABEL:
4626 case PSA_KDF_STEP_SALT:
4627 case PSA_KDF_STEP_INFO:
4628 return( psa_key_derivation_input_raw( generator, step,
4629 data, data_length ) );
4630 default:
4631 return( PSA_ERROR_INVALID_ARGUMENT );
4632 }
4633}
4634
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004635psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4636 psa_key_derivation_step_t step,
4637 psa_key_handle_t handle )
4638{
4639 psa_key_slot_t *slot;
4640 psa_status_t status;
4641 status = psa_get_key_from_slot( handle, &slot,
4642 PSA_KEY_USAGE_DERIVE,
4643 generator->alg );
4644 if( status != PSA_SUCCESS )
4645 return( status );
4646 if( slot->type != PSA_KEY_TYPE_DERIVE )
4647 return( PSA_ERROR_INVALID_ARGUMENT );
4648 /* Don't allow a key to be used as an input that is usually public.
4649 * This is debatable. It's ok from a cryptographic perspective to
4650 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004651 * the material should be dedicated to a particular input step,
4652 * otherwise this may allow the key to be used in an unintended way
4653 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004654 if( step != PSA_KDF_STEP_SECRET )
4655 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004656 return( psa_key_derivation_input_raw( generator,
4657 step,
4658 slot->data.raw.data,
4659 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004660}
4661
Gilles Peskineea0fb492018-07-12 17:17:20 +02004662
4663
4664/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004665/* Key agreement */
4666/****************************************************************/
4667
Gilles Peskinea05219c2018-11-16 16:02:56 +01004668#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004669static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4670 size_t peer_key_length,
4671 const mbedtls_ecp_keypair *our_key,
4672 uint8_t *shared_secret,
4673 size_t shared_secret_size,
4674 size_t *shared_secret_length )
4675{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004676 mbedtls_ecp_keypair *their_key = NULL;
4677 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004678 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004679 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004680
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004681 status = psa_import_ec_public_key(
4682 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4683 peer_key, peer_key_length,
4684 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004685 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004686 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004687
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004688 status = mbedtls_to_psa_error(
4689 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4690 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004691 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004692 status = mbedtls_to_psa_error(
4693 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4694 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004695 goto exit;
4696
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004697 status = mbedtls_to_psa_error(
4698 mbedtls_ecdh_calc_secret( &ecdh,
4699 shared_secret_length,
4700 shared_secret, shared_secret_size,
4701 mbedtls_ctr_drbg_random,
4702 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004703
4704exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004705 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004706 mbedtls_ecp_keypair_free( their_key );
4707 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004708 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004709}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004710#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004711
Gilles Peskine01d718c2018-09-18 12:01:02 +02004712#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4713
Gilles Peskine0216fe12019-04-11 21:23:21 +02004714static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4715 psa_key_slot_t *private_key,
4716 const uint8_t *peer_key,
4717 size_t peer_key_length,
4718 uint8_t *shared_secret,
4719 size_t shared_secret_size,
4720 size_t *shared_secret_length )
4721{
4722 switch( alg )
4723 {
4724#if defined(MBEDTLS_ECDH_C)
4725 case PSA_ALG_ECDH:
4726 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4727 return( PSA_ERROR_INVALID_ARGUMENT );
4728 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
4729 private_key->data.ecp,
4730 shared_secret, shared_secret_size,
4731 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004732#endif /* MBEDTLS_ECDH_C */
4733 default:
4734 (void) private_key;
4735 (void) peer_key;
4736 (void) peer_key_length;
4737 (void) shared_secret;
4738 (void) shared_secret_size;
4739 (void) shared_secret_length;
4740 return( PSA_ERROR_NOT_SUPPORTED );
4741 }
4742}
4743
Gilles Peskine346797d2018-11-16 16:05:06 +01004744/* Note that if this function fails, you must call psa_generator_abort()
4745 * to potentially free embedded data structures and wipe confidential data.
4746 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004747static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004748 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004749 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004750 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004751 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004752{
4753 psa_status_t status;
4754 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4755 size_t shared_secret_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004756 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004757
4758 /* Step 1: run the secret agreement algorithm to generate the shared
4759 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004760 status = psa_key_agreement_raw_internal( ka_alg,
4761 private_key,
4762 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004763 shared_secret,
4764 sizeof( shared_secret ),
4765 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004766 if( status != PSA_SUCCESS )
4767 goto exit;
4768
4769 /* Step 2: set up the key derivation to generate key material from
4770 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004771 status = psa_key_derivation_input_raw( generator, step,
4772 shared_secret, shared_secret_length );
4773
Gilles Peskine01d718c2018-09-18 12:01:02 +02004774exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004775 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004776 return( status );
4777}
4778
4779psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004780 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004781 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004782 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004783 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004784{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004785 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004786 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004787 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004788 return( PSA_ERROR_INVALID_ARGUMENT );
4789 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004790 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004791 if( status != PSA_SUCCESS )
4792 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004793 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004794 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004795 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004796 if( status != PSA_SUCCESS )
4797 psa_generator_abort( generator );
4798 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004799}
4800
Gilles Peskine0216fe12019-04-11 21:23:21 +02004801psa_status_t psa_key_agreement_raw_shared_secret( psa_algorithm_t alg,
4802 psa_key_handle_t private_key,
4803 const uint8_t *peer_key,
4804 size_t peer_key_length,
4805 uint8_t *output,
4806 size_t output_size,
4807 size_t *output_length )
4808{
4809 psa_key_slot_t *slot;
4810 psa_status_t status;
4811
4812 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4813 {
4814 status = PSA_ERROR_INVALID_ARGUMENT;
4815 goto exit;
4816 }
4817 status = psa_get_key_from_slot( private_key, &slot,
4818 PSA_KEY_USAGE_DERIVE, alg );
4819 if( status != PSA_SUCCESS )
4820 goto exit;
4821
4822 status = psa_key_agreement_raw_internal( alg, slot,
4823 peer_key, peer_key_length,
4824 output, output_size,
4825 output_length );
4826
4827exit:
4828 if( status != PSA_SUCCESS )
4829 {
4830 /* If an error happens and is not handled properly, the output
4831 * may be used as a key to protect sensitive data. Arrange for such
4832 * a key to be random, which is likely to result in decryption or
4833 * verification errors. This is better than filling the buffer with
4834 * some constant data such as zeros, which would result in the data
4835 * being protected with a reproducible, easily knowable key.
4836 */
4837 psa_generate_random( output, output_size );
4838 *output_length = output_size;
4839 }
4840 return( status );
4841}
Gilles Peskine01d718c2018-09-18 12:01:02 +02004842
4843
4844/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004845/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004846/****************************************************************/
4847
4848psa_status_t psa_generate_random( uint8_t *output,
4849 size_t output_size )
4850{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004851 int ret;
4852 GUARD_MODULE_INITIALIZED;
4853
4854 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004855 return( mbedtls_to_psa_error( ret ) );
4856}
4857
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004858#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
4859#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02004860
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004861psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4862 size_t seed_size )
4863{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004864 if( global_data.initialized )
4865 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004866
4867 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4868 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4869 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4870 return( PSA_ERROR_INVALID_ARGUMENT );
4871
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004872 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004873}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004874#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004875
Gilles Peskine87a5e562019-04-17 12:28:25 +02004876psa_status_t psa_generate_key_to_handle( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004877 psa_key_type_t type,
4878 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004879 const void *extra,
4880 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004881{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004882 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004883 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004884
Gilles Peskine53d991e2018-07-12 01:14:59 +02004885 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004886 return( PSA_ERROR_INVALID_ARGUMENT );
4887
Gilles Peskinec5487a82018-12-03 18:08:14 +01004888 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004889 if( status != PSA_SUCCESS )
4890 return( status );
4891
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004892 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004893 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004894 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004895 if( status != PSA_SUCCESS )
4896 return( status );
4897 status = psa_generate_random( slot->data.raw.data,
4898 slot->data.raw.bytes );
4899 if( status != PSA_SUCCESS )
4900 {
4901 mbedtls_free( slot->data.raw.data );
4902 return( status );
4903 }
4904#if defined(MBEDTLS_DES_C)
4905 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004906 psa_des_set_key_parity( slot->data.raw.data,
4907 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004908#endif /* MBEDTLS_DES_C */
4909 }
4910 else
4911
4912#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4913 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4914 {
4915 mbedtls_rsa_context *rsa;
4916 int ret;
4917 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004918 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4919 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004920 /* Accept only byte-aligned keys, for the same reasons as
4921 * in psa_import_rsa_key(). */
4922 if( bits % 8 != 0 )
4923 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004924 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004925 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004926 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004927 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004928 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004929#if INT_MAX < 0xffffffff
4930 /* Check that the uint32_t value passed by the caller fits
4931 * in the range supported by this implementation. */
4932 if( p->e > INT_MAX )
4933 return( PSA_ERROR_NOT_SUPPORTED );
4934#endif
4935 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004936 }
4937 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4938 if( rsa == NULL )
4939 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4940 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4941 ret = mbedtls_rsa_gen_key( rsa,
4942 mbedtls_ctr_drbg_random,
4943 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004944 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004945 exponent );
4946 if( ret != 0 )
4947 {
4948 mbedtls_rsa_free( rsa );
4949 mbedtls_free( rsa );
4950 return( mbedtls_to_psa_error( ret ) );
4951 }
4952 slot->data.rsa = rsa;
4953 }
4954 else
4955#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4956
4957#if defined(MBEDTLS_ECP_C)
4958 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4959 {
4960 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4961 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4962 const mbedtls_ecp_curve_info *curve_info =
4963 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4964 mbedtls_ecp_keypair *ecp;
4965 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004966 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004967 return( PSA_ERROR_NOT_SUPPORTED );
4968 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4969 return( PSA_ERROR_NOT_SUPPORTED );
4970 if( curve_info->bit_size != bits )
4971 return( PSA_ERROR_INVALID_ARGUMENT );
4972 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4973 if( ecp == NULL )
4974 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4975 mbedtls_ecp_keypair_init( ecp );
4976 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4977 mbedtls_ctr_drbg_random,
4978 &global_data.ctr_drbg );
4979 if( ret != 0 )
4980 {
4981 mbedtls_ecp_keypair_free( ecp );
4982 mbedtls_free( ecp );
4983 return( mbedtls_to_psa_error( ret ) );
4984 }
4985 slot->data.ecp = ecp;
4986 }
4987 else
4988#endif /* MBEDTLS_ECP_C */
4989
4990 return( PSA_ERROR_NOT_SUPPORTED );
4991
4992 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004993
4994#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4995 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4996 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004997 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004998 }
4999#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5000
5001 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005002}
5003
5004
5005/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005006/* Module setup */
5007/****************************************************************/
5008
Gilles Peskine5e769522018-11-20 21:59:56 +01005009psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5010 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5011 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5012{
5013 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5014 return( PSA_ERROR_BAD_STATE );
5015 global_data.entropy_init = entropy_init;
5016 global_data.entropy_free = entropy_free;
5017 return( PSA_SUCCESS );
5018}
5019
Gilles Peskinee59236f2018-01-27 23:32:46 +01005020void mbedtls_psa_crypto_free( void )
5021{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005022 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005023 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5024 {
5025 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005026 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005027 }
5028 /* Wipe all remaining data, including configuration.
5029 * In particular, this sets all state indicator to the value
5030 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005031 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005032}
5033
5034psa_status_t psa_crypto_init( void )
5035{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005036 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005037 const unsigned char drbg_seed[] = "PSA";
5038
Gilles Peskinec6b69072018-11-20 21:42:52 +01005039 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005040 if( global_data.initialized != 0 )
5041 return( PSA_SUCCESS );
5042
Gilles Peskine5e769522018-11-20 21:59:56 +01005043 /* Set default configuration if
5044 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5045 if( global_data.entropy_init == NULL )
5046 global_data.entropy_init = mbedtls_entropy_init;
5047 if( global_data.entropy_free == NULL )
5048 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005049
Gilles Peskinec6b69072018-11-20 21:42:52 +01005050 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005051 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005052 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005053 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005054 status = mbedtls_to_psa_error(
5055 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5056 mbedtls_entropy_func,
5057 &global_data.entropy,
5058 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5059 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005060 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005061 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005062
Gilles Peskine66fb1262018-12-10 16:29:04 +01005063 status = psa_initialize_key_slots( );
5064 if( status != PSA_SUCCESS )
5065 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005066
5067 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005068 global_data.initialized = 1;
5069
Gilles Peskinee59236f2018-01-27 23:32:46 +01005070exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005071 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005072 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005073 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005074}
5075
5076#endif /* MBEDTLS_PSA_CRYPTO_C */