blob: dba244a40655f50168f88f914975b866e97a9546 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
42#if defined(MBEDTLS_PLATFORM_C)
43#include "mbedtls/platform.h"
44#else
45#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020050#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000051#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020052#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/blowfish.h"
54#include "mbedtls/camellia.h"
55#include "mbedtls/cipher.h"
56#include "mbedtls/ccm.h"
57#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010058#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020060#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010061#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/error.h"
64#include "mbedtls/gcm.h"
65#include "mbedtls/md2.h"
66#include "mbedtls/md4.h"
67#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010068#include "mbedtls/md.h"
69#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010070#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010072#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010074#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/sha1.h"
76#include "mbedtls/sha256.h"
77#include "mbedtls/sha512.h"
78#include "mbedtls/xtea.h"
79
Gilles Peskine996deb12018-08-01 15:45:45 +020080#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
81
Gilles Peskine9ef733f2018-02-07 21:05:37 +010082/* constant-time buffer comparison */
83static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
84{
85 size_t i;
86 unsigned char diff = 0;
87
88 for( i = 0; i < n; i++ )
89 diff |= a[i] ^ b[i];
90
91 return( diff );
92}
93
94
95
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010096/****************************************************************/
97/* Global data, support functions and library management */
98/****************************************************************/
99
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100static int key_type_is_raw_bytes( psa_key_type_t type )
101{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200102 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200103}
104
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100105/* Values for psa_global_data_t::rng_state */
106#define RNG_NOT_INITIALIZED 0
107#define RNG_INITIALIZED 1
108#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100109
Gilles Peskine2d277862018-06-18 15:41:12 +0200110typedef struct
111{
Gilles Peskine5e769522018-11-20 21:59:56 +0100112 void (* entropy_init )( mbedtls_entropy_context *ctx );
113 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100114 mbedtls_entropy_context entropy;
115 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100116 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100117 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
itayzafrir0adf0fc2018-09-06 16:24:41 +0300122#define GUARD_MODULE_INITIALIZED \
123 if( global_data.initialized == 0 ) \
124 return( PSA_ERROR_BAD_STATE );
125
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126static psa_status_t mbedtls_to_psa_error( int ret )
127{
Gilles Peskinea5905292018-02-07 20:59:33 +0100128 /* If there's both a high-level code and low-level code, dispatch on
129 * the high-level code. */
130 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131 {
132 case 0:
133 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100134
135 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
136 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
137 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
138 return( PSA_ERROR_NOT_SUPPORTED );
139 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
140 return( PSA_ERROR_HARDWARE_FAILURE );
141
142 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
Gilles Peskine9a944802018-06-21 09:35:35 +0200145 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
146 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
147 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
148 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
149 case MBEDTLS_ERR_ASN1_INVALID_DATA:
150 return( PSA_ERROR_INVALID_ARGUMENT );
151 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
152 return( PSA_ERROR_INSUFFICIENT_MEMORY );
153 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
154 return( PSA_ERROR_BUFFER_TOO_SMALL );
155
Jaeden Amero93e21112019-02-20 13:57:28 +0000156#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000157 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100159 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100161 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
162 return( PSA_ERROR_NOT_SUPPORTED );
163 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
164 return( PSA_ERROR_HARDWARE_FAILURE );
165
Jaeden Amero93e21112019-02-20 13:57:28 +0000166#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000167 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
172 return( PSA_ERROR_NOT_SUPPORTED );
173 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
174 return( PSA_ERROR_HARDWARE_FAILURE );
175
176 case MBEDTLS_ERR_CCM_BAD_INPUT:
177 return( PSA_ERROR_INVALID_ARGUMENT );
178 case MBEDTLS_ERR_CCM_AUTH_FAILED:
179 return( PSA_ERROR_INVALID_SIGNATURE );
180 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
181 return( PSA_ERROR_HARDWARE_FAILURE );
182
183 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
188 return( PSA_ERROR_INSUFFICIENT_MEMORY );
189 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
190 return( PSA_ERROR_INVALID_PADDING );
191 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
196 return( PSA_ERROR_TAMPERING_DETECTED );
197 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
198 return( PSA_ERROR_HARDWARE_FAILURE );
199
200 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
201 return( PSA_ERROR_HARDWARE_FAILURE );
202
203 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
204 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
205 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
206 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
209 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
210
211 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
212 return( PSA_ERROR_NOT_SUPPORTED );
213 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
Gilles Peskinee59236f2018-01-27 23:32:46 +0100216 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
217 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
218 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
221 case MBEDTLS_ERR_GCM_AUTH_FAILED:
222 return( PSA_ERROR_INVALID_SIGNATURE );
223 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200224 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
228 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
229 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
230 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
231 return( PSA_ERROR_HARDWARE_FAILURE );
232
233 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
234 return( PSA_ERROR_NOT_SUPPORTED );
235 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
236 return( PSA_ERROR_INVALID_ARGUMENT );
237 case MBEDTLS_ERR_MD_ALLOC_FAILED:
238 return( PSA_ERROR_INSUFFICIENT_MEMORY );
239 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
240 return( PSA_ERROR_STORAGE_FAILURE );
241 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskinef76aa772018-10-29 19:24:33 +0100244 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
245 return( PSA_ERROR_STORAGE_FAILURE );
246 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
251 return( PSA_ERROR_BUFFER_TOO_SMALL );
252 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
253 return( PSA_ERROR_INVALID_ARGUMENT );
254 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
255 return( PSA_ERROR_INVALID_ARGUMENT );
256 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
260
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_ALLOC_FAILED:
262 return( PSA_ERROR_INSUFFICIENT_MEMORY );
263 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
264 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100268 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
269 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
274 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
275 return( PSA_ERROR_NOT_PERMITTED );
276 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_INVALID_ALG:
279 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
280 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
283 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
285 return( PSA_ERROR_HARDWARE_FAILURE );
286
287 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
288 return( PSA_ERROR_HARDWARE_FAILURE );
289
290 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_RSA_INVALID_PADDING:
293 return( PSA_ERROR_INVALID_PADDING );
294 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
299 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
300 return( PSA_ERROR_TAMPERING_DETECTED );
301 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
302 return( PSA_ERROR_INVALID_SIGNATURE );
303 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
304 return( PSA_ERROR_BUFFER_TOO_SMALL );
305 case MBEDTLS_ERR_RSA_RNG_FAILED:
306 return( PSA_ERROR_INSUFFICIENT_MEMORY );
307 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
308 return( PSA_ERROR_NOT_SUPPORTED );
309 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311
312 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
313 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
314 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316
317 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
itayzafrir5c753392018-05-08 11:18:38 +0300322 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300324 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
326 return( PSA_ERROR_BUFFER_TOO_SMALL );
327 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
328 return( PSA_ERROR_NOT_SUPPORTED );
329 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
330 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
331 return( PSA_ERROR_INVALID_SIGNATURE );
332 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
333 return( PSA_ERROR_INSUFFICIENT_MEMORY );
334 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300336
Gilles Peskinee59236f2018-01-27 23:32:46 +0100337 default:
David Saadab4ecc272019-02-14 13:48:10 +0200338 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100339 }
340}
341
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200342
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200343
344
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100345/****************************************************************/
346/* Key management */
347/****************************************************************/
348
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100349#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200350static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
351{
352 switch( grpid )
353 {
354 case MBEDTLS_ECP_DP_SECP192R1:
355 return( PSA_ECC_CURVE_SECP192R1 );
356 case MBEDTLS_ECP_DP_SECP224R1:
357 return( PSA_ECC_CURVE_SECP224R1 );
358 case MBEDTLS_ECP_DP_SECP256R1:
359 return( PSA_ECC_CURVE_SECP256R1 );
360 case MBEDTLS_ECP_DP_SECP384R1:
361 return( PSA_ECC_CURVE_SECP384R1 );
362 case MBEDTLS_ECP_DP_SECP521R1:
363 return( PSA_ECC_CURVE_SECP521R1 );
364 case MBEDTLS_ECP_DP_BP256R1:
365 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
366 case MBEDTLS_ECP_DP_BP384R1:
367 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
368 case MBEDTLS_ECP_DP_BP512R1:
369 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
370 case MBEDTLS_ECP_DP_CURVE25519:
371 return( PSA_ECC_CURVE_CURVE25519 );
372 case MBEDTLS_ECP_DP_SECP192K1:
373 return( PSA_ECC_CURVE_SECP192K1 );
374 case MBEDTLS_ECP_DP_SECP224K1:
375 return( PSA_ECC_CURVE_SECP224K1 );
376 case MBEDTLS_ECP_DP_SECP256K1:
377 return( PSA_ECC_CURVE_SECP256K1 );
378 case MBEDTLS_ECP_DP_CURVE448:
379 return( PSA_ECC_CURVE_CURVE448 );
380 default:
381 return( 0 );
382 }
383}
384
Gilles Peskine12313cd2018-06-20 00:20:32 +0200385static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
386{
387 switch( curve )
388 {
389 case PSA_ECC_CURVE_SECP192R1:
390 return( MBEDTLS_ECP_DP_SECP192R1 );
391 case PSA_ECC_CURVE_SECP224R1:
392 return( MBEDTLS_ECP_DP_SECP224R1 );
393 case PSA_ECC_CURVE_SECP256R1:
394 return( MBEDTLS_ECP_DP_SECP256R1 );
395 case PSA_ECC_CURVE_SECP384R1:
396 return( MBEDTLS_ECP_DP_SECP384R1 );
397 case PSA_ECC_CURVE_SECP521R1:
398 return( MBEDTLS_ECP_DP_SECP521R1 );
399 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
400 return( MBEDTLS_ECP_DP_BP256R1 );
401 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
402 return( MBEDTLS_ECP_DP_BP384R1 );
403 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
404 return( MBEDTLS_ECP_DP_BP512R1 );
405 case PSA_ECC_CURVE_CURVE25519:
406 return( MBEDTLS_ECP_DP_CURVE25519 );
407 case PSA_ECC_CURVE_SECP192K1:
408 return( MBEDTLS_ECP_DP_SECP192K1 );
409 case PSA_ECC_CURVE_SECP224K1:
410 return( MBEDTLS_ECP_DP_SECP224K1 );
411 case PSA_ECC_CURVE_SECP256K1:
412 return( MBEDTLS_ECP_DP_SECP256K1 );
413 case PSA_ECC_CURVE_CURVE448:
414 return( MBEDTLS_ECP_DP_CURVE448 );
415 default:
416 return( MBEDTLS_ECP_DP_NONE );
417 }
418}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100419#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200420
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200421static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
422 size_t bits,
423 struct raw_data *raw )
424{
425 /* Check that the bit size is acceptable for the key type */
426 switch( type )
427 {
428 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200429 if( bits == 0 )
430 {
431 raw->bytes = 0;
432 raw->data = NULL;
433 return( PSA_SUCCESS );
434 }
435 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436#if defined(MBEDTLS_MD_C)
437 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200438#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200439 case PSA_KEY_TYPE_DERIVE:
440 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200441#if defined(MBEDTLS_AES_C)
442 case PSA_KEY_TYPE_AES:
443 if( bits != 128 && bits != 192 && bits != 256 )
444 return( PSA_ERROR_INVALID_ARGUMENT );
445 break;
446#endif
447#if defined(MBEDTLS_CAMELLIA_C)
448 case PSA_KEY_TYPE_CAMELLIA:
449 if( bits != 128 && bits != 192 && bits != 256 )
450 return( PSA_ERROR_INVALID_ARGUMENT );
451 break;
452#endif
453#if defined(MBEDTLS_DES_C)
454 case PSA_KEY_TYPE_DES:
455 if( bits != 64 && bits != 128 && bits != 192 )
456 return( PSA_ERROR_INVALID_ARGUMENT );
457 break;
458#endif
459#if defined(MBEDTLS_ARC4_C)
460 case PSA_KEY_TYPE_ARC4:
461 if( bits < 8 || bits > 2048 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465 default:
466 return( PSA_ERROR_NOT_SUPPORTED );
467 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200468 if( bits % 8 != 0 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200470
471 /* Allocate memory for the key */
472 raw->bytes = PSA_BITS_TO_BYTES( bits );
473 raw->data = mbedtls_calloc( 1, raw->bytes );
474 if( raw->data == NULL )
475 {
476 raw->bytes = 0;
477 return( PSA_ERROR_INSUFFICIENT_MEMORY );
478 }
479 return( PSA_SUCCESS );
480}
481
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200482#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100483/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
484 * that are not a multiple of 8) well. For example, there is only
485 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
486 * way to return the exact bit size of a key.
487 * To keep things simple, reject non-byte-aligned key sizes. */
488static psa_status_t psa_check_rsa_key_byte_aligned(
489 const mbedtls_rsa_context *rsa )
490{
491 mbedtls_mpi n;
492 psa_status_t status;
493 mbedtls_mpi_init( &n );
494 status = mbedtls_to_psa_error(
495 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
496 if( status == PSA_SUCCESS )
497 {
498 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
499 status = PSA_ERROR_NOT_SUPPORTED;
500 }
501 mbedtls_mpi_free( &n );
502 return( status );
503}
504
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000505static psa_status_t psa_import_rsa_key( psa_key_type_t type,
506 const uint8_t *data,
507 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200508 mbedtls_rsa_context **p_rsa )
509{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000510 psa_status_t status;
511 mbedtls_pk_context pk;
512 mbedtls_rsa_context *rsa;
513 size_t bits;
514
515 mbedtls_pk_init( &pk );
516
517 /* Parse the data. */
518 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
519 status = mbedtls_to_psa_error(
520 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200521 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000522 status = mbedtls_to_psa_error(
523 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
524 if( status != PSA_SUCCESS )
525 goto exit;
526
527 /* We have something that the pkparse module recognizes. If it is a
528 * valid RSA key, store it. */
529 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200530 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000531 status = PSA_ERROR_INVALID_ARGUMENT;
532 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000534
535 rsa = mbedtls_pk_rsa( pk );
536 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
537 * supports non-byte-aligned key sizes, but not well. For example,
538 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
539 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
540 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
541 {
542 status = PSA_ERROR_NOT_SUPPORTED;
543 goto exit;
544 }
545 status = psa_check_rsa_key_byte_aligned( rsa );
546
547exit:
548 /* Free the content of the pk object only on error. */
549 if( status != PSA_SUCCESS )
550 {
551 mbedtls_pk_free( &pk );
552 return( status );
553 }
554
555 /* On success, store the content of the object in the RSA context. */
556 *p_rsa = rsa;
557
558 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200559}
560#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
561
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000562#if defined(MBEDTLS_ECP_C)
563
564/* Import a public key given as the uncompressed representation defined by SEC1
565 * 2.3.3 as the content of an ECPoint. */
566static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
567 const uint8_t *data,
568 size_t data_length,
569 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570{
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000571 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
572 mbedtls_ecp_keypair *ecp = NULL;
573 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
574
575 *p_ecp = NULL;
576 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
577 if( ecp == NULL )
578 return( PSA_ERROR_INSUFFICIENT_MEMORY );
579 mbedtls_ecp_keypair_init( ecp );
580
581 /* Load the group. */
582 status = mbedtls_to_psa_error(
583 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
584 if( status != PSA_SUCCESS )
585 goto exit;
586 /* Load the public value. */
587 status = mbedtls_to_psa_error(
588 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
589 data, data_length ) );
590 if( status != PSA_SUCCESS )
591 goto exit;
592
593 /* Check that the point is on the curve. */
594 status = mbedtls_to_psa_error(
595 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
596 if( status != PSA_SUCCESS )
597 goto exit;
598
599 *p_ecp = ecp;
600 return( PSA_SUCCESS );
601
602exit:
603 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200604 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000605 mbedtls_ecp_keypair_free( ecp );
606 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200607 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000608 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200609}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000610#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200611
Gilles Peskinef76aa772018-10-29 19:24:33 +0100612#if defined(MBEDTLS_ECP_C)
613/* Import a private key given as a byte string which is the private value
614 * in big-endian order. */
615static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
616 const uint8_t *data,
617 size_t data_length,
618 mbedtls_ecp_keypair **p_ecp )
619{
620 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
621 mbedtls_ecp_keypair *ecp = NULL;
622 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
623
624 *p_ecp = NULL;
625 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
626 if( ecp == NULL )
627 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000628 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100629
630 /* Load the group. */
631 status = mbedtls_to_psa_error(
632 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
633 if( status != PSA_SUCCESS )
634 goto exit;
635 /* Load the secret value. */
636 status = mbedtls_to_psa_error(
637 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640 /* Validate the private key. */
641 status = mbedtls_to_psa_error(
642 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
643 if( status != PSA_SUCCESS )
644 goto exit;
645 /* Calculate the public key from the private key. */
646 status = mbedtls_to_psa_error(
647 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
648 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
649 if( status != PSA_SUCCESS )
650 goto exit;
651
652 *p_ecp = ecp;
653 return( PSA_SUCCESS );
654
655exit:
656 if( ecp != NULL )
657 {
658 mbedtls_ecp_keypair_free( ecp );
659 mbedtls_free( ecp );
660 }
661 return( status );
662}
663#endif /* defined(MBEDTLS_ECP_C) */
664
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100665/** Import key data into a slot. `slot->type` must have been set
666 * previously. This function assumes that the slot does not contain
667 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100668psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
669 const uint8_t *data,
670 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100671{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200672 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Darryl Green940d72c2018-07-13 13:18:51 +0100674 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100676 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 if( data_length > SIZE_MAX / 8 )
678 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100679 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200680 PSA_BYTES_TO_BITS( data_length ),
681 &slot->data.raw );
682 if( status != PSA_SUCCESS )
683 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200684 if( data_length != 0 )
685 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 }
687 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100688#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100689 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100690 {
Darryl Green940d72c2018-07-13 13:18:51 +0100691 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100692 data, data_length,
693 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100694 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000695 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
696 {
697 status = psa_import_ec_public_key(
698 PSA_KEY_TYPE_GET_CURVE( slot->type ),
699 data, data_length,
700 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000701 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100702 else
703#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000704#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
705 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000707 status = psa_import_rsa_key( slot->type,
708 data, data_length,
709 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 }
711 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000712#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713 {
714 return( PSA_ERROR_NOT_SUPPORTED );
715 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000716 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100717}
718
Darryl Green06fd18d2018-07-16 11:21:11 +0100719/* Retrieve an empty key slot (slot with no key data, but possibly
Jaeden Amero283dfd12019-01-11 12:06:22 +0000720 * with some metadata such as a policy or domain parameters). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100721static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100722 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100723{
724 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100725 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100726
727 *p_slot = NULL;
728
Gilles Peskinec5487a82018-12-03 18:08:14 +0100729 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100730 if( status != PSA_SUCCESS )
731 return( status );
732
733 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200734 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100735
736 *p_slot = slot;
737 return( status );
738}
739
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100740/** Calculate the intersection of two algorithm usage policies.
741 *
742 * Return 0 (which allows no operation) on incompatibility.
743 */
744static psa_algorithm_t psa_key_policy_algorithm_intersection(
745 psa_algorithm_t alg1,
746 psa_algorithm_t alg2 )
747{
748 /* Common case: the policy only allows alg. */
749 if( alg1 == alg2 )
750 return( alg1 );
751 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100752 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100753 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
754 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
755 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
756 {
757 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
758 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100759 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100760 return( alg1 );
761 }
762 /* If the policies are incompatible, allow nothing. */
763 return( 0 );
764}
765
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100766/** Test whether a policy permits an algorithm.
767 *
768 * The caller must test usage flags separately.
769 */
770static int psa_key_policy_permits( const psa_key_policy_t *policy,
771 psa_algorithm_t alg )
772{
773 /* Common case: the policy only allows alg. */
774 if( alg == policy->alg )
775 return( 1 );
776 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
777 * and alg is the same hash-and-sign family with any hash,
778 * then alg is compliant with policy->alg. */
779 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
780 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
781 {
782 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
783 ( alg & ~PSA_ALG_HASH_MASK ) );
784 }
785 /* If it isn't permitted, it's forbidden. */
786 return( 0 );
787}
788
Gilles Peskinef603c712019-01-19 13:40:11 +0100789/** Restrict a key policy based on a constraint.
790 *
791 * \param[in,out] policy The policy to restrict.
792 * \param[in] constraint The policy constraint to apply.
793 *
794 * \retval #PSA_SUCCESS
795 * \c *policy contains the intersection of the original value of
796 * \c *policy and \c *constraint.
797 * \retval #PSA_ERROR_INVALID_ARGUMENT
798 * \c *policy and \c *constraint are incompatible.
799 * \c *policy is unchanged.
800 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100801static psa_status_t psa_restrict_key_policy(
802 psa_key_policy_t *policy,
803 const psa_key_policy_t *constraint )
804{
805 psa_algorithm_t intersection_alg =
806 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
807 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
808 return( PSA_ERROR_INVALID_ARGUMENT );
809 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100810 policy->alg = intersection_alg;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100811 return( PSA_SUCCESS );
812}
813
Darryl Green06fd18d2018-07-16 11:21:11 +0100814/** Retrieve a slot which must contain a key. The key must have allow all the
815 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
816 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100817static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100818 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100819 psa_key_usage_t usage,
820 psa_algorithm_t alg )
821{
822 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100823 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100824
825 *p_slot = NULL;
826
Gilles Peskinec5487a82018-12-03 18:08:14 +0100827 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100828 if( status != PSA_SUCCESS )
829 return( status );
830 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200831 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100832
833 /* Enforce that usage policy for the key slot contains all the flags
834 * required by the usage parameter. There is one exception: public
835 * keys can always be exported, so we treat public key objects as
836 * if they had the export flag. */
837 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
838 usage &= ~PSA_KEY_USAGE_EXPORT;
839 if( ( slot->policy.usage & usage ) != usage )
840 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841
842 /* Enforce that the usage policy permits the requested algortihm. */
843 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100844 return( PSA_ERROR_NOT_PERMITTED );
845
846 *p_slot = slot;
847 return( PSA_SUCCESS );
848}
Darryl Green940d72c2018-07-13 13:18:51 +0100849
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100850/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100851static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000852{
853 if( slot->type == PSA_KEY_TYPE_NONE )
854 {
855 /* No key material to clean. */
856 }
857 else if( key_type_is_raw_bytes( slot->type ) )
858 {
859 mbedtls_free( slot->data.raw.data );
860 }
861 else
862#if defined(MBEDTLS_RSA_C)
863 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
864 {
865 mbedtls_rsa_free( slot->data.rsa );
866 mbedtls_free( slot->data.rsa );
867 }
868 else
869#endif /* defined(MBEDTLS_RSA_C) */
870#if defined(MBEDTLS_ECP_C)
871 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
872 {
873 mbedtls_ecp_keypair_free( slot->data.ecp );
874 mbedtls_free( slot->data.ecp );
875 }
876 else
877#endif /* defined(MBEDTLS_ECP_C) */
878 {
879 /* Shouldn't happen: the key type is not any type that we
880 * put in. */
881 return( PSA_ERROR_TAMPERING_DETECTED );
882 }
883
884 return( PSA_SUCCESS );
885}
886
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100887static void psa_abort_operations_using_key( psa_key_slot_t *slot )
888{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200889 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100890 (void) slot;
891}
892
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100893/** Completely wipe a slot in memory, including its policy.
894 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100895psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100896{
897 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100898 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100899 /* At this point, key material and other type-specific content has
900 * been wiped. Clear remaining metadata. We can call memset and not
901 * zeroize because the metadata is not particularly sensitive. */
902 memset( slot, 0, sizeof( *slot ) );
903 return( status );
904}
905
Gilles Peskine87a5e562019-04-17 12:28:25 +0200906psa_status_t psa_import_key_to_handle( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100907 psa_key_type_t type,
908 const uint8_t *data,
909 size_t data_length )
910{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100911 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100912 psa_status_t status;
913
Gilles Peskinec5487a82018-12-03 18:08:14 +0100914 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100915 if( status != PSA_SUCCESS )
916 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100917
918 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100919
920 status = psa_import_key_into_slot( slot, data, data_length );
921 if( status != PSA_SUCCESS )
922 {
923 slot->type = PSA_KEY_TYPE_NONE;
924 return( status );
925 }
926
Darryl Greend49a4992018-06-18 17:27:26 +0100927#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
928 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
929 {
930 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100931 status = psa_save_persistent_key( slot->persistent_storage_id,
932 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100933 data_length );
934 if( status != PSA_SUCCESS )
935 {
936 (void) psa_remove_key_data_from_memory( slot );
937 slot->type = PSA_KEY_TYPE_NONE;
938 }
939 }
940#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
941
942 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943}
944
Gilles Peskinec5487a82018-12-03 18:08:14 +0100945psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100948 psa_status_t status = PSA_SUCCESS;
949 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950
Gilles Peskinec5487a82018-12-03 18:08:14 +0100951 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200952 if( status != PSA_SUCCESS )
953 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100954#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
955 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
956 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100957 storage_status =
958 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100959 }
960#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100961 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100962 if( status != PSA_SUCCESS )
963 return( status );
964 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100965}
966
Gilles Peskineb870b182018-07-06 16:02:09 +0200967/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200968static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200969{
970 if( key_type_is_raw_bytes( slot->type ) )
971 return( slot->data.raw.bytes * 8 );
972#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100974 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200975#endif /* defined(MBEDTLS_RSA_C) */
976#if defined(MBEDTLS_ECP_C)
977 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
978 return( slot->data.ecp->grp.pbits );
979#endif /* defined(MBEDTLS_ECP_C) */
980 /* Shouldn't happen except on an empty slot. */
981 return( 0 );
982}
983
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200984void psa_reset_key_attributes( psa_key_attributes_t *attributes )
985{
Gilles Peskineb699f072019-04-26 16:06:02 +0200986 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200987 memset( attributes, 0, sizeof( *attributes ) );
988}
989
Gilles Peskineb699f072019-04-26 16:06:02 +0200990psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
991 psa_key_type_t type,
992 const uint8_t *data,
993 size_t data_length )
994{
995 uint8_t *copy = NULL;
996
997 if( data_length != 0 )
998 {
999 copy = mbedtls_calloc( 1, data_length );
1000 if( copy == NULL )
1001 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1002 memcpy( copy, data, data_length );
1003 }
1004 /* After this point, this function is guaranteed to succeed, so it
1005 * can start modifying `*attributes`. */
1006
1007 if( attributes->domain_parameters != NULL )
1008 {
1009 mbedtls_free( attributes->domain_parameters );
1010 attributes->domain_parameters = NULL;
1011 attributes->domain_parameters_size = 0;
1012 }
1013
1014 attributes->domain_parameters = copy;
1015 attributes->domain_parameters_size = data_length;
1016 attributes->type = type;
1017 return( PSA_SUCCESS );
1018}
1019
1020psa_status_t psa_get_key_domain_parameters(
1021 const psa_key_attributes_t *attributes,
1022 uint8_t *data, size_t data_size, size_t *data_length )
1023{
1024 if( attributes->domain_parameters_size > data_size )
1025 return( PSA_ERROR_BUFFER_TOO_SMALL );
1026 *data_length = attributes->domain_parameters_size;
1027 if( attributes->domain_parameters_size != 0 )
1028 memcpy( data, attributes->domain_parameters,
1029 attributes->domain_parameters_size );
1030 return( PSA_SUCCESS );
1031}
1032
1033#if defined(MBEDTLS_RSA_C)
1034static psa_status_t psa_get_rsa_public_exponent(
1035 const mbedtls_rsa_context *rsa,
1036 psa_key_attributes_t *attributes )
1037{
1038 mbedtls_mpi mpi;
1039 int ret;
1040 uint8_t *buffer = NULL;
1041 size_t buflen;
1042 mbedtls_mpi_init( &mpi );
1043
1044 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1045 if( ret != 0 )
1046 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001047 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1048 {
1049 /* It's the default value, which is reported as an empty string,
1050 * so there's nothing to do. */
1051 goto exit;
1052 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001053
1054 buflen = mbedtls_mpi_size( &mpi );
1055 buffer = mbedtls_calloc( 1, buflen );
1056 if( buffer == NULL )
1057 {
1058 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1059 goto exit;
1060 }
1061 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1062 if( ret != 0 )
1063 goto exit;
1064 attributes->domain_parameters = buffer;
1065 attributes->domain_parameters_size = buflen;
1066
1067exit:
1068 mbedtls_mpi_free( &mpi );
1069 if( ret != 0 )
1070 mbedtls_free( buffer );
1071 return( mbedtls_to_psa_error( ret ) );
1072}
1073#endif /* MBEDTLS_RSA_C */
1074
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001075psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1076 psa_key_attributes_t *attributes )
1077{
1078 psa_key_slot_t *slot;
1079 psa_status_t status;
1080
1081 psa_reset_key_attributes( attributes );
1082
1083 status = psa_get_key_slot( handle, &slot );
1084 if( status != PSA_SUCCESS )
1085 return( status );
1086
1087 attributes->id = slot->persistent_storage_id;
1088 attributes->lifetime = slot->lifetime;
1089 attributes->policy = slot->policy;
1090 attributes->type = slot->type;
1091 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001092
1093 switch( slot->type )
1094 {
1095#if defined(MBEDTLS_RSA_C)
1096 case PSA_KEY_TYPE_RSA_KEYPAIR:
1097 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1098 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1099 break;
1100#endif
1101 default:
1102 /* Nothing else to do. */
1103 break;
1104 }
1105
1106 if( status != PSA_SUCCESS )
1107 psa_reset_key_attributes( attributes );
1108 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001109}
1110
Gilles Peskinec5487a82018-12-03 18:08:14 +01001111psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001112 psa_key_type_t *type,
1113 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001114{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001115 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001116 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001117
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001118 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001119 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001120 if( bits != NULL )
1121 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001122 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001123 if( status != PSA_SUCCESS )
1124 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001125
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001126 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001127 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001128 if( type != NULL )
1129 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001130 if( bits != NULL )
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001131 *bits = psa_get_key_slot_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001132 return( PSA_SUCCESS );
1133}
1134
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001135#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001136static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1137 unsigned char *buf, size_t size )
1138{
1139 int ret;
1140 unsigned char *c;
1141 size_t len = 0;
1142
1143 c = buf + size;
1144
1145 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1146
1147 return( (int) len );
1148}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001149#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001150
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001151static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1152 uint8_t *data,
1153 size_t data_size,
1154 size_t *data_length,
1155 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001156{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001157 *data_length = 0;
1158
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001159 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001160 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001161
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001162 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001163 {
1164 if( slot->data.raw.bytes > data_size )
1165 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001166 if( data_size != 0 )
1167 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001168 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001169 memset( data + slot->data.raw.bytes, 0,
1170 data_size - slot->data.raw.bytes );
1171 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001172 *data_length = slot->data.raw.bytes;
1173 return( PSA_SUCCESS );
1174 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001175#if defined(MBEDTLS_ECP_C)
1176 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1177 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001178 psa_status_t status;
1179
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001180 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001181 if( bytes > data_size )
1182 return( PSA_ERROR_BUFFER_TOO_SMALL );
1183 status = mbedtls_to_psa_error(
1184 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1185 if( status != PSA_SUCCESS )
1186 return( status );
1187 memset( data + bytes, 0, data_size - bytes );
1188 *data_length = bytes;
1189 return( PSA_SUCCESS );
1190 }
1191#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001192 else
Moran Peker17e36e12018-05-02 12:55:20 +03001193 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001194#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001195 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001196 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001197 {
Moran Pekera998bc62018-04-16 18:16:20 +03001198 mbedtls_pk_context pk;
1199 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001200 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001201 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001202#if defined(MBEDTLS_RSA_C)
1203 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001204 pk.pk_info = &mbedtls_rsa_info;
1205 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001206#else
1207 return( PSA_ERROR_NOT_SUPPORTED );
1208#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001209 }
1210 else
1211 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001212#if defined(MBEDTLS_ECP_C)
1213 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001214 pk.pk_info = &mbedtls_eckey_info;
1215 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001216#else
1217 return( PSA_ERROR_NOT_SUPPORTED );
1218#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001219 }
Moran Pekerd7326592018-05-29 16:56:39 +03001220 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001221 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001222 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001223 }
Moran Peker17e36e12018-05-02 12:55:20 +03001224 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001225 {
Moran Peker17e36e12018-05-02 12:55:20 +03001226 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001227 }
Moran Peker60364322018-04-29 11:34:58 +03001228 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001229 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001230 /* If data_size is 0 then data may be NULL and then the
1231 * call to memset would have undefined behavior. */
1232 if( data_size != 0 )
1233 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001234 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001235 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001236 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1237 * Move the data to the beginning and erase remaining data
1238 * at the original location. */
1239 if( 2 * (size_t) ret <= data_size )
1240 {
1241 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001242 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001243 }
1244 else if( (size_t) ret < data_size )
1245 {
1246 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001247 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001248 }
Moran Pekera998bc62018-04-16 18:16:20 +03001249 *data_length = ret;
1250 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001251 }
1252 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001253#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001254 {
1255 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001256 it is valid for a special-purpose implementation to omit
1257 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001258 return( PSA_ERROR_NOT_SUPPORTED );
1259 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001260 }
1261}
1262
Gilles Peskinec5487a82018-12-03 18:08:14 +01001263psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001264 uint8_t *data,
1265 size_t data_size,
1266 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001267{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001268 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001269 psa_status_t status;
1270
1271 /* Set the key to empty now, so that even when there are errors, we always
1272 * set data_length to a value between 0 and data_size. On error, setting
1273 * the key to empty is a good choice because an empty key representation is
1274 * unlikely to be accepted anywhere. */
1275 *data_length = 0;
1276
1277 /* Export requires the EXPORT flag. There is an exception for public keys,
1278 * which don't require any flag, but psa_get_key_from_slot takes
1279 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001280 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001281 if( status != PSA_SUCCESS )
1282 return( status );
1283 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001284 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001286
Gilles Peskinec5487a82018-12-03 18:08:14 +01001287psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001288 uint8_t *data,
1289 size_t data_size,
1290 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001291{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001292 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001293 psa_status_t status;
1294
1295 /* Set the key to empty now, so that even when there are errors, we always
1296 * set data_length to a value between 0 and data_size. On error, setting
1297 * the key to empty is a good choice because an empty key representation is
1298 * unlikely to be accepted anywhere. */
1299 *data_length = 0;
1300
1301 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001302 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001303 if( status != PSA_SUCCESS )
1304 return( status );
1305 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001306 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001307}
1308
Darryl Green0c6575a2018-11-07 16:05:30 +00001309#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001310static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001311 size_t bits )
1312{
1313 psa_status_t status;
1314 uint8_t *data;
1315 size_t key_length;
1316 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1317 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001318 if( data == NULL )
1319 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001320 /* Get key data in export format */
1321 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1322 if( status != PSA_SUCCESS )
1323 {
1324 slot->type = PSA_KEY_TYPE_NONE;
1325 goto exit;
1326 }
1327 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001328 status = psa_save_persistent_key( slot->persistent_storage_id,
1329 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001330 data, key_length );
1331 if( status != PSA_SUCCESS )
1332 {
1333 slot->type = PSA_KEY_TYPE_NONE;
1334 }
1335exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001336 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001337 mbedtls_free( data );
1338 return( status );
1339}
1340#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1341
Gilles Peskine4747d192019-04-17 15:05:45 +02001342static psa_status_t psa_set_key_policy_internal(
1343 psa_key_slot_t *slot,
1344 const psa_key_policy_t *policy )
1345{
1346 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
1347 PSA_KEY_USAGE_ENCRYPT |
1348 PSA_KEY_USAGE_DECRYPT |
1349 PSA_KEY_USAGE_SIGN |
1350 PSA_KEY_USAGE_VERIFY |
1351 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1352 return( PSA_ERROR_INVALID_ARGUMENT );
1353
1354 slot->policy = *policy;
1355 return( PSA_SUCCESS );
1356}
1357
1358/** Prepare a key slot to receive key material.
1359 *
1360 * This function allocates a key slot and sets its metadata.
1361 *
1362 * If this function fails, call psa_fail_key_creation().
1363 *
1364 * \param attributes Key attributes for the new key.
1365 * \param handle On success, the allocated handle.
1366 * \param p_slot On success, a pointer to the prepared slot.
1367 */
1368static psa_status_t psa_start_key_creation(
1369 const psa_key_attributes_t *attributes,
1370 psa_key_handle_t *handle,
1371 psa_key_slot_t **p_slot )
1372{
1373 psa_status_t status;
1374 psa_key_slot_t *slot;
1375
1376 status = psa_allocate_key( handle );
1377 if( status != PSA_SUCCESS )
1378 return( status );
1379 status = psa_get_key_slot( *handle, p_slot );
1380 if( status != PSA_SUCCESS )
1381 return( status );
1382 slot = *p_slot;
1383
1384 status = psa_set_key_policy_internal( slot, &attributes->policy );
1385 if( status != PSA_SUCCESS )
1386 return( status );
1387 slot->lifetime = attributes->lifetime;
1388 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001389 {
1390 status = psa_validate_persistent_key_parameters( attributes->lifetime,
1391 attributes->id );
1392 if( status != PSA_SUCCESS )
1393 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001394 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001395 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001396 slot->type = attributes->type;
1397
1398 return( status );
1399}
1400
1401/** Finalize the creation of a key once its key material has been set.
1402 *
1403 * This entails writing the key to persistent storage.
1404 *
1405 * If this function fails, call psa_fail_key_creation().
1406 *
1407 * \param slot Pointer to the slot with key material.
1408 */
1409static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1410{
1411 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001412 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001413
1414#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1415 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1416 {
1417 uint8_t *buffer = NULL;
1418 size_t buffer_size = 0;
1419 size_t length;
1420
1421 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001422 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001423 buffer = mbedtls_calloc( 1, buffer_size );
1424 if( buffer == NULL && buffer_size != 0 )
1425 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1426 status = psa_internal_export_key( slot,
1427 buffer, buffer_size, &length,
1428 0 );
1429
1430 if( status == PSA_SUCCESS )
1431 {
1432 status = psa_save_persistent_key( slot->persistent_storage_id,
1433 slot->type, &slot->policy,
1434 buffer, length );
1435 }
1436
1437 if( buffer_size != 0 )
1438 mbedtls_platform_zeroize( buffer, buffer_size );
1439 mbedtls_free( buffer );
1440 }
1441#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1442
1443 return( status );
1444}
1445
1446/** Abort the creation of a key.
1447 *
1448 * You may call this function after calling psa_start_key_creation(),
1449 * or after psa_finish_key_creation() fails. In other circumstances, this
1450 * function may not clean up persistent storage.
1451 *
1452 * \param slot Pointer to the slot with key material.
1453 */
1454static void psa_fail_key_creation( psa_key_slot_t *slot )
1455{
1456 if( slot == NULL )
1457 return;
1458 psa_wipe_key_slot( slot );
1459}
1460
1461psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
1462 psa_key_handle_t *handle,
1463 const uint8_t *data,
1464 size_t data_length )
1465{
1466 psa_status_t status;
1467 psa_key_slot_t *slot = NULL;
1468 status = psa_start_key_creation( attributes, handle, &slot );
1469 if( status == PSA_SUCCESS )
1470 {
1471 status = psa_import_key_into_slot( slot, data, data_length );
1472 }
1473 if( status == PSA_SUCCESS )
1474 status = psa_finish_key_creation( slot );
1475 if( status != PSA_SUCCESS )
1476 {
1477 psa_fail_key_creation( slot );
1478 *handle = 0;
1479 }
1480 return( status );
1481}
1482
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001483static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001484 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001485{
1486 psa_status_t status;
1487 uint8_t *buffer = NULL;
1488 size_t buffer_size = 0;
1489 size_t length;
1490
1491 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001492 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001493 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001494 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001495 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001496 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1497 if( status != PSA_SUCCESS )
1498 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001499 target->type = source->type;
1500 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001501
1502exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001503 if( buffer_size != 0 )
1504 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001505 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001506 return( status );
1507}
1508
Gilles Peskine87a5e562019-04-17 12:28:25 +02001509psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001510 psa_key_handle_t target_handle,
1511 const psa_key_policy_t *constraint)
1512{
1513 psa_key_slot_t *source_slot = NULL;
1514 psa_key_slot_t *target_slot = NULL;
1515 psa_key_policy_t new_policy;
1516 psa_status_t status;
1517 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1518 if( status != PSA_SUCCESS )
1519 return( status );
1520 status = psa_get_empty_key_slot( target_handle, &target_slot );
1521 if( status != PSA_SUCCESS )
1522 return( status );
1523
1524 new_policy = target_slot->policy;
1525 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1526 if( status != PSA_SUCCESS )
1527 return( status );
1528 if( constraint != NULL )
1529 {
1530 status = psa_restrict_key_policy( &new_policy, constraint );
1531 if( status != PSA_SUCCESS )
1532 return( status );
1533 }
1534
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001535 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001536 if( status != PSA_SUCCESS )
1537 return( status );
1538
1539 target_slot->policy = new_policy;
1540 return( PSA_SUCCESS );
1541}
1542
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001543psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1544 const psa_key_attributes_t *specified_attributes,
1545 psa_key_handle_t *target_handle )
1546{
1547 psa_status_t status;
1548 psa_key_slot_t *source_slot = NULL;
1549 psa_key_slot_t *target_slot = NULL;
1550 psa_key_attributes_t actual_attributes = *specified_attributes;
1551
1552 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1553 if( status != PSA_SUCCESS )
1554 goto exit;
1555
1556 status = psa_restrict_key_policy( &actual_attributes.policy,
1557 &source_slot->policy );
1558 if( status != PSA_SUCCESS )
1559 goto exit;
1560
1561 status = psa_start_key_creation( &actual_attributes,
1562 target_handle, &target_slot );
1563 if( status != PSA_SUCCESS )
1564 goto exit;
1565
1566 status = psa_copy_key_material( source_slot, target_slot );
1567
1568exit:
1569 if( status == PSA_SUCCESS )
1570 status = psa_finish_key_creation( target_slot );
1571 if( status != PSA_SUCCESS )
1572 {
1573 psa_fail_key_creation( target_slot );
1574 *target_handle = 0;
1575 }
1576 return( status );
1577}
1578
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001579
1580
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001581/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001582/* Message digests */
1583/****************************************************************/
1584
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001585static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001586{
1587 switch( alg )
1588 {
1589#if defined(MBEDTLS_MD2_C)
1590 case PSA_ALG_MD2:
1591 return( &mbedtls_md2_info );
1592#endif
1593#if defined(MBEDTLS_MD4_C)
1594 case PSA_ALG_MD4:
1595 return( &mbedtls_md4_info );
1596#endif
1597#if defined(MBEDTLS_MD5_C)
1598 case PSA_ALG_MD5:
1599 return( &mbedtls_md5_info );
1600#endif
1601#if defined(MBEDTLS_RIPEMD160_C)
1602 case PSA_ALG_RIPEMD160:
1603 return( &mbedtls_ripemd160_info );
1604#endif
1605#if defined(MBEDTLS_SHA1_C)
1606 case PSA_ALG_SHA_1:
1607 return( &mbedtls_sha1_info );
1608#endif
1609#if defined(MBEDTLS_SHA256_C)
1610 case PSA_ALG_SHA_224:
1611 return( &mbedtls_sha224_info );
1612 case PSA_ALG_SHA_256:
1613 return( &mbedtls_sha256_info );
1614#endif
1615#if defined(MBEDTLS_SHA512_C)
1616 case PSA_ALG_SHA_384:
1617 return( &mbedtls_sha384_info );
1618 case PSA_ALG_SHA_512:
1619 return( &mbedtls_sha512_info );
1620#endif
1621 default:
1622 return( NULL );
1623 }
1624}
1625
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001626psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1627{
1628 switch( operation->alg )
1629 {
Gilles Peskine81736312018-06-26 15:04:31 +02001630 case 0:
1631 /* The object has (apparently) been initialized but it is not
1632 * in use. It's ok to call abort on such an object, and there's
1633 * nothing to do. */
1634 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001635#if defined(MBEDTLS_MD2_C)
1636 case PSA_ALG_MD2:
1637 mbedtls_md2_free( &operation->ctx.md2 );
1638 break;
1639#endif
1640#if defined(MBEDTLS_MD4_C)
1641 case PSA_ALG_MD4:
1642 mbedtls_md4_free( &operation->ctx.md4 );
1643 break;
1644#endif
1645#if defined(MBEDTLS_MD5_C)
1646 case PSA_ALG_MD5:
1647 mbedtls_md5_free( &operation->ctx.md5 );
1648 break;
1649#endif
1650#if defined(MBEDTLS_RIPEMD160_C)
1651 case PSA_ALG_RIPEMD160:
1652 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1653 break;
1654#endif
1655#if defined(MBEDTLS_SHA1_C)
1656 case PSA_ALG_SHA_1:
1657 mbedtls_sha1_free( &operation->ctx.sha1 );
1658 break;
1659#endif
1660#if defined(MBEDTLS_SHA256_C)
1661 case PSA_ALG_SHA_224:
1662 case PSA_ALG_SHA_256:
1663 mbedtls_sha256_free( &operation->ctx.sha256 );
1664 break;
1665#endif
1666#if defined(MBEDTLS_SHA512_C)
1667 case PSA_ALG_SHA_384:
1668 case PSA_ALG_SHA_512:
1669 mbedtls_sha512_free( &operation->ctx.sha512 );
1670 break;
1671#endif
1672 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001673 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001674 }
1675 operation->alg = 0;
1676 return( PSA_SUCCESS );
1677}
1678
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001679psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001680 psa_algorithm_t alg )
1681{
1682 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001683
1684 /* A context must be freshly initialized before it can be set up. */
1685 if( operation->alg != 0 )
1686 {
1687 return( PSA_ERROR_BAD_STATE );
1688 }
1689
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001690 switch( alg )
1691 {
1692#if defined(MBEDTLS_MD2_C)
1693 case PSA_ALG_MD2:
1694 mbedtls_md2_init( &operation->ctx.md2 );
1695 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1696 break;
1697#endif
1698#if defined(MBEDTLS_MD4_C)
1699 case PSA_ALG_MD4:
1700 mbedtls_md4_init( &operation->ctx.md4 );
1701 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1702 break;
1703#endif
1704#if defined(MBEDTLS_MD5_C)
1705 case PSA_ALG_MD5:
1706 mbedtls_md5_init( &operation->ctx.md5 );
1707 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1708 break;
1709#endif
1710#if defined(MBEDTLS_RIPEMD160_C)
1711 case PSA_ALG_RIPEMD160:
1712 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1713 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1714 break;
1715#endif
1716#if defined(MBEDTLS_SHA1_C)
1717 case PSA_ALG_SHA_1:
1718 mbedtls_sha1_init( &operation->ctx.sha1 );
1719 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1720 break;
1721#endif
1722#if defined(MBEDTLS_SHA256_C)
1723 case PSA_ALG_SHA_224:
1724 mbedtls_sha256_init( &operation->ctx.sha256 );
1725 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1726 break;
1727 case PSA_ALG_SHA_256:
1728 mbedtls_sha256_init( &operation->ctx.sha256 );
1729 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1730 break;
1731#endif
1732#if defined(MBEDTLS_SHA512_C)
1733 case PSA_ALG_SHA_384:
1734 mbedtls_sha512_init( &operation->ctx.sha512 );
1735 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1736 break;
1737 case PSA_ALG_SHA_512:
1738 mbedtls_sha512_init( &operation->ctx.sha512 );
1739 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1740 break;
1741#endif
1742 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001743 return( PSA_ALG_IS_HASH( alg ) ?
1744 PSA_ERROR_NOT_SUPPORTED :
1745 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001746 }
1747 if( ret == 0 )
1748 operation->alg = alg;
1749 else
1750 psa_hash_abort( operation );
1751 return( mbedtls_to_psa_error( ret ) );
1752}
1753
1754psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1755 const uint8_t *input,
1756 size_t input_length )
1757{
1758 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001759
1760 /* Don't require hash implementations to behave correctly on a
1761 * zero-length input, which may have an invalid pointer. */
1762 if( input_length == 0 )
1763 return( PSA_SUCCESS );
1764
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001765 switch( operation->alg )
1766 {
1767#if defined(MBEDTLS_MD2_C)
1768 case PSA_ALG_MD2:
1769 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1770 input, input_length );
1771 break;
1772#endif
1773#if defined(MBEDTLS_MD4_C)
1774 case PSA_ALG_MD4:
1775 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1776 input, input_length );
1777 break;
1778#endif
1779#if defined(MBEDTLS_MD5_C)
1780 case PSA_ALG_MD5:
1781 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1782 input, input_length );
1783 break;
1784#endif
1785#if defined(MBEDTLS_RIPEMD160_C)
1786 case PSA_ALG_RIPEMD160:
1787 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1788 input, input_length );
1789 break;
1790#endif
1791#if defined(MBEDTLS_SHA1_C)
1792 case PSA_ALG_SHA_1:
1793 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1794 input, input_length );
1795 break;
1796#endif
1797#if defined(MBEDTLS_SHA256_C)
1798 case PSA_ALG_SHA_224:
1799 case PSA_ALG_SHA_256:
1800 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1801 input, input_length );
1802 break;
1803#endif
1804#if defined(MBEDTLS_SHA512_C)
1805 case PSA_ALG_SHA_384:
1806 case PSA_ALG_SHA_512:
1807 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1808 input, input_length );
1809 break;
1810#endif
1811 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001812 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001813 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001814
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001815 if( ret != 0 )
1816 psa_hash_abort( operation );
1817 return( mbedtls_to_psa_error( ret ) );
1818}
1819
1820psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1821 uint8_t *hash,
1822 size_t hash_size,
1823 size_t *hash_length )
1824{
itayzafrir40835d42018-08-02 13:14:17 +03001825 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001826 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001827 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001828
1829 /* Fill the output buffer with something that isn't a valid hash
1830 * (barring an attack on the hash and deliberately-crafted input),
1831 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001832 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001833 /* If hash_size is 0 then hash may be NULL and then the
1834 * call to memset would have undefined behavior. */
1835 if( hash_size != 0 )
1836 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001837
1838 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001839 {
1840 status = PSA_ERROR_BUFFER_TOO_SMALL;
1841 goto exit;
1842 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001843
1844 switch( operation->alg )
1845 {
1846#if defined(MBEDTLS_MD2_C)
1847 case PSA_ALG_MD2:
1848 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1849 break;
1850#endif
1851#if defined(MBEDTLS_MD4_C)
1852 case PSA_ALG_MD4:
1853 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1854 break;
1855#endif
1856#if defined(MBEDTLS_MD5_C)
1857 case PSA_ALG_MD5:
1858 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1859 break;
1860#endif
1861#if defined(MBEDTLS_RIPEMD160_C)
1862 case PSA_ALG_RIPEMD160:
1863 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1864 break;
1865#endif
1866#if defined(MBEDTLS_SHA1_C)
1867 case PSA_ALG_SHA_1:
1868 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1869 break;
1870#endif
1871#if defined(MBEDTLS_SHA256_C)
1872 case PSA_ALG_SHA_224:
1873 case PSA_ALG_SHA_256:
1874 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1875 break;
1876#endif
1877#if defined(MBEDTLS_SHA512_C)
1878 case PSA_ALG_SHA_384:
1879 case PSA_ALG_SHA_512:
1880 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1881 break;
1882#endif
1883 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001884 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001885 }
itayzafrir40835d42018-08-02 13:14:17 +03001886 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001887
itayzafrir40835d42018-08-02 13:14:17 +03001888exit:
1889 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001890 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001891 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001892 return( psa_hash_abort( operation ) );
1893 }
1894 else
1895 {
1896 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001897 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001898 }
1899}
1900
Gilles Peskine2d277862018-06-18 15:41:12 +02001901psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1902 const uint8_t *hash,
1903 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001904{
1905 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1906 size_t actual_hash_length;
1907 psa_status_t status = psa_hash_finish( operation,
1908 actual_hash, sizeof( actual_hash ),
1909 &actual_hash_length );
1910 if( status != PSA_SUCCESS )
1911 return( status );
1912 if( actual_hash_length != hash_length )
1913 return( PSA_ERROR_INVALID_SIGNATURE );
1914 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1915 return( PSA_ERROR_INVALID_SIGNATURE );
1916 return( PSA_SUCCESS );
1917}
1918
Gilles Peskineeb35d782019-01-22 17:56:16 +01001919psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1920 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001921{
1922 if( target_operation->alg != 0 )
1923 return( PSA_ERROR_BAD_STATE );
1924
1925 switch( source_operation->alg )
1926 {
1927 case 0:
1928 return( PSA_ERROR_BAD_STATE );
1929#if defined(MBEDTLS_MD2_C)
1930 case PSA_ALG_MD2:
1931 mbedtls_md2_clone( &target_operation->ctx.md2,
1932 &source_operation->ctx.md2 );
1933 break;
1934#endif
1935#if defined(MBEDTLS_MD4_C)
1936 case PSA_ALG_MD4:
1937 mbedtls_md4_clone( &target_operation->ctx.md4,
1938 &source_operation->ctx.md4 );
1939 break;
1940#endif
1941#if defined(MBEDTLS_MD5_C)
1942 case PSA_ALG_MD5:
1943 mbedtls_md5_clone( &target_operation->ctx.md5,
1944 &source_operation->ctx.md5 );
1945 break;
1946#endif
1947#if defined(MBEDTLS_RIPEMD160_C)
1948 case PSA_ALG_RIPEMD160:
1949 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1950 &source_operation->ctx.ripemd160 );
1951 break;
1952#endif
1953#if defined(MBEDTLS_SHA1_C)
1954 case PSA_ALG_SHA_1:
1955 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1956 &source_operation->ctx.sha1 );
1957 break;
1958#endif
1959#if defined(MBEDTLS_SHA256_C)
1960 case PSA_ALG_SHA_224:
1961 case PSA_ALG_SHA_256:
1962 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1963 &source_operation->ctx.sha256 );
1964 break;
1965#endif
1966#if defined(MBEDTLS_SHA512_C)
1967 case PSA_ALG_SHA_384:
1968 case PSA_ALG_SHA_512:
1969 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1970 &source_operation->ctx.sha512 );
1971 break;
1972#endif
1973 default:
1974 return( PSA_ERROR_NOT_SUPPORTED );
1975 }
1976
1977 target_operation->alg = source_operation->alg;
1978 return( PSA_SUCCESS );
1979}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001980
1981
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001982/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001983/* MAC */
1984/****************************************************************/
1985
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001986static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001987 psa_algorithm_t alg,
1988 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001989 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001990 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001991{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001992 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001993 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001994
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001995 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001996 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001997
Gilles Peskine8c9def32018-02-08 10:02:12 +01001998 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1999 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002000 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002001 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002002 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002003 mode = MBEDTLS_MODE_STREAM;
2004 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002005 case PSA_ALG_CTR:
2006 mode = MBEDTLS_MODE_CTR;
2007 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002008 case PSA_ALG_CFB:
2009 mode = MBEDTLS_MODE_CFB;
2010 break;
2011 case PSA_ALG_OFB:
2012 mode = MBEDTLS_MODE_OFB;
2013 break;
2014 case PSA_ALG_CBC_NO_PADDING:
2015 mode = MBEDTLS_MODE_CBC;
2016 break;
2017 case PSA_ALG_CBC_PKCS7:
2018 mode = MBEDTLS_MODE_CBC;
2019 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002020 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002021 mode = MBEDTLS_MODE_CCM;
2022 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002023 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002024 mode = MBEDTLS_MODE_GCM;
2025 break;
2026 default:
2027 return( NULL );
2028 }
2029 }
2030 else if( alg == PSA_ALG_CMAC )
2031 mode = MBEDTLS_MODE_ECB;
2032 else if( alg == PSA_ALG_GMAC )
2033 mode = MBEDTLS_MODE_GCM;
2034 else
2035 return( NULL );
2036
2037 switch( key_type )
2038 {
2039 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002040 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002041 break;
2042 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002043 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2044 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002045 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002046 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002047 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002048 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002049 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2050 * but two-key Triple-DES is functionally three-key Triple-DES
2051 * with K1=K3, so that's how we present it to mbedtls. */
2052 if( key_bits == 128 )
2053 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002054 break;
2055 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002056 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002057 break;
2058 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002059 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002060 break;
2061 default:
2062 return( NULL );
2063 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002064 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002065 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002066
Jaeden Amero23bbb752018-06-26 14:16:54 +01002067 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2068 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002069}
2070
Gilles Peskinea05219c2018-11-16 16:02:56 +01002071#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002072static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002073{
Gilles Peskine2d277862018-06-18 15:41:12 +02002074 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002075 {
2076 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002077 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002078 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002079 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002080 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002081 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002082 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002083 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002084 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002085 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002086 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002087 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002088 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002089 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002090 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002091 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002092 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002093 return( 128 );
2094 default:
2095 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002096 }
2097}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002098#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002099
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002100/* Initialize the MAC operation structure. Once this function has been
2101 * called, psa_mac_abort can run and will do the right thing. */
2102static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2103 psa_algorithm_t alg )
2104{
2105 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2106
2107 operation->alg = alg;
2108 operation->key_set = 0;
2109 operation->iv_set = 0;
2110 operation->iv_required = 0;
2111 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002112 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002113
2114#if defined(MBEDTLS_CMAC_C)
2115 if( alg == PSA_ALG_CMAC )
2116 {
2117 operation->iv_required = 0;
2118 mbedtls_cipher_init( &operation->ctx.cmac );
2119 status = PSA_SUCCESS;
2120 }
2121 else
2122#endif /* MBEDTLS_CMAC_C */
2123#if defined(MBEDTLS_MD_C)
2124 if( PSA_ALG_IS_HMAC( operation->alg ) )
2125 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002126 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2127 operation->ctx.hmac.hash_ctx.alg = 0;
2128 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002129 }
2130 else
2131#endif /* MBEDTLS_MD_C */
2132 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002133 if( ! PSA_ALG_IS_MAC( alg ) )
2134 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002135 }
2136
2137 if( status != PSA_SUCCESS )
2138 memset( operation, 0, sizeof( *operation ) );
2139 return( status );
2140}
2141
Gilles Peskine01126fa2018-07-12 17:04:55 +02002142#if defined(MBEDTLS_MD_C)
2143static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2144{
Gilles Peskine3f108122018-12-07 18:14:53 +01002145 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002146 return( psa_hash_abort( &hmac->hash_ctx ) );
2147}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002148
2149static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2150{
2151 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2152 memset( hmac, 0, sizeof( *hmac ) );
2153}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002154#endif /* MBEDTLS_MD_C */
2155
Gilles Peskine8c9def32018-02-08 10:02:12 +01002156psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2157{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002158 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002159 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002160 /* The object has (apparently) been initialized but it is not
2161 * in use. It's ok to call abort on such an object, and there's
2162 * nothing to do. */
2163 return( PSA_SUCCESS );
2164 }
2165 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002166#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002167 if( operation->alg == PSA_ALG_CMAC )
2168 {
2169 mbedtls_cipher_free( &operation->ctx.cmac );
2170 }
2171 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002172#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002173#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002174 if( PSA_ALG_IS_HMAC( operation->alg ) )
2175 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002176 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002177 }
2178 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002179#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002180 {
2181 /* Sanity check (shouldn't happen: operation->alg should
2182 * always have been initialized to a valid value). */
2183 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002184 }
Moran Peker41deec42018-04-04 15:43:05 +03002185
Gilles Peskine8c9def32018-02-08 10:02:12 +01002186 operation->alg = 0;
2187 operation->key_set = 0;
2188 operation->iv_set = 0;
2189 operation->iv_required = 0;
2190 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002191 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002192
Gilles Peskine8c9def32018-02-08 10:02:12 +01002193 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002194
2195bad_state:
2196 /* If abort is called on an uninitialized object, we can't trust
2197 * anything. Wipe the object in case it contains confidential data.
2198 * This may result in a memory leak if a pointer gets overwritten,
2199 * but it's too late to do anything about this. */
2200 memset( operation, 0, sizeof( *operation ) );
2201 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002202}
2203
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002204#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002205static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002206 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002207 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002208 const mbedtls_cipher_info_t *cipher_info )
2209{
2210 int ret;
2211
2212 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002213
2214 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2215 if( ret != 0 )
2216 return( ret );
2217
2218 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2219 slot->data.raw.data,
2220 key_bits );
2221 return( ret );
2222}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002223#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002224
Gilles Peskine248051a2018-06-20 16:09:38 +02002225#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002226static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2227 const uint8_t *key,
2228 size_t key_length,
2229 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002230{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002231 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002232 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002233 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002234 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002235 psa_status_t status;
2236
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002237 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2238 * overflow below. This should never trigger if the hash algorithm
2239 * is implemented correctly. */
2240 /* The size checks against the ipad and opad buffers cannot be written
2241 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2242 * because that triggers -Wlogical-op on GCC 7.3. */
2243 if( block_size > sizeof( ipad ) )
2244 return( PSA_ERROR_NOT_SUPPORTED );
2245 if( block_size > sizeof( hmac->opad ) )
2246 return( PSA_ERROR_NOT_SUPPORTED );
2247 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002248 return( PSA_ERROR_NOT_SUPPORTED );
2249
Gilles Peskined223b522018-06-11 18:12:58 +02002250 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002251 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002252 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002253 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002254 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002255 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002256 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002257 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002258 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002259 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002260 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002261 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002262 }
Gilles Peskine96889972018-07-12 17:07:03 +02002263 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2264 * but it is permitted. It is common when HMAC is used in HKDF, for
2265 * example. Don't call `memcpy` in the 0-length because `key` could be
2266 * an invalid pointer which would make the behavior undefined. */
2267 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002268 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002269
Gilles Peskined223b522018-06-11 18:12:58 +02002270 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2271 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002272 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002273 ipad[i] ^= 0x36;
2274 memset( ipad + key_length, 0x36, block_size - key_length );
2275
2276 /* Copy the key material from ipad to opad, flipping the requisite bits,
2277 * and filling the rest of opad with the requisite constant. */
2278 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002279 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2280 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002281
Gilles Peskine01126fa2018-07-12 17:04:55 +02002282 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002283 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002284 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002285
Gilles Peskine01126fa2018-07-12 17:04:55 +02002286 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002287
2288cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002289 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002290
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002291 return( status );
2292}
Gilles Peskine248051a2018-06-20 16:09:38 +02002293#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002294
Gilles Peskine89167cb2018-07-08 20:12:23 +02002295static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002296 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002297 psa_algorithm_t alg,
2298 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002299{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002300 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002301 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002302 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002303 psa_key_usage_t usage =
2304 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002305 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002306 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002307
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002308 /* A context must be freshly initialized before it can be set up. */
2309 if( operation->alg != 0 )
2310 {
2311 return( PSA_ERROR_BAD_STATE );
2312 }
2313
Gilles Peskined911eb72018-08-14 15:18:45 +02002314 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002315 if( status != PSA_SUCCESS )
2316 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002317 if( is_sign )
2318 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002319
Gilles Peskinec5487a82018-12-03 18:08:14 +01002320 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002321 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002322 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002323 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002324
Gilles Peskine8c9def32018-02-08 10:02:12 +01002325#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002326 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002327 {
2328 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002329 mbedtls_cipher_info_from_psa( full_length_alg,
2330 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002331 int ret;
2332 if( cipher_info == NULL )
2333 {
2334 status = PSA_ERROR_NOT_SUPPORTED;
2335 goto exit;
2336 }
2337 operation->mac_size = cipher_info->block_size;
2338 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2339 status = mbedtls_to_psa_error( ret );
2340 }
2341 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002342#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002343#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002344 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002345 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002346 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002347 if( hash_alg == 0 )
2348 {
2349 status = PSA_ERROR_NOT_SUPPORTED;
2350 goto exit;
2351 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002352
2353 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2354 /* Sanity check. This shouldn't fail on a valid configuration. */
2355 if( operation->mac_size == 0 ||
2356 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2357 {
2358 status = PSA_ERROR_NOT_SUPPORTED;
2359 goto exit;
2360 }
2361
Gilles Peskine01126fa2018-07-12 17:04:55 +02002362 if( slot->type != PSA_KEY_TYPE_HMAC )
2363 {
2364 status = PSA_ERROR_INVALID_ARGUMENT;
2365 goto exit;
2366 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002367
Gilles Peskine01126fa2018-07-12 17:04:55 +02002368 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2369 slot->data.raw.data,
2370 slot->data.raw.bytes,
2371 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002372 }
2373 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002375 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002376 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002377 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002378 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002379
Gilles Peskined911eb72018-08-14 15:18:45 +02002380 if( truncated == 0 )
2381 {
2382 /* The "normal" case: untruncated algorithm. Nothing to do. */
2383 }
2384 else if( truncated < 4 )
2385 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002386 /* A very short MAC is too short for security since it can be
2387 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2388 * so we make this our minimum, even though 32 bits is still
2389 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002390 status = PSA_ERROR_NOT_SUPPORTED;
2391 }
2392 else if( truncated > operation->mac_size )
2393 {
2394 /* It's impossible to "truncate" to a larger length. */
2395 status = PSA_ERROR_INVALID_ARGUMENT;
2396 }
2397 else
2398 operation->mac_size = truncated;
2399
Gilles Peskinefbfac682018-07-08 20:51:54 +02002400exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002401 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002402 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002403 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002405 else
2406 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002407 operation->key_set = 1;
2408 }
2409 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002410}
2411
Gilles Peskine89167cb2018-07-08 20:12:23 +02002412psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002413 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002414 psa_algorithm_t alg )
2415{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002416 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002417}
2418
2419psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002420 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002421 psa_algorithm_t alg )
2422{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002423 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002424}
2425
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2427 const uint8_t *input,
2428 size_t input_length )
2429{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002430 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002432 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002434 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002435 operation->has_input = 1;
2436
Gilles Peskine8c9def32018-02-08 10:02:12 +01002437#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002438 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002439 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002440 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2441 input, input_length );
2442 status = mbedtls_to_psa_error( ret );
2443 }
2444 else
2445#endif /* MBEDTLS_CMAC_C */
2446#if defined(MBEDTLS_MD_C)
2447 if( PSA_ALG_IS_HMAC( operation->alg ) )
2448 {
2449 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2450 input_length );
2451 }
2452 else
2453#endif /* MBEDTLS_MD_C */
2454 {
2455 /* This shouldn't happen if `operation` was initialized by
2456 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002457 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002458 }
2459
Gilles Peskinefbfac682018-07-08 20:51:54 +02002460 if( status != PSA_SUCCESS )
2461 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002462 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002463}
2464
Gilles Peskine01126fa2018-07-12 17:04:55 +02002465#if defined(MBEDTLS_MD_C)
2466static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2467 uint8_t *mac,
2468 size_t mac_size )
2469{
2470 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2471 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2472 size_t hash_size = 0;
2473 size_t block_size = psa_get_hash_block_size( hash_alg );
2474 psa_status_t status;
2475
Gilles Peskine01126fa2018-07-12 17:04:55 +02002476 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2477 if( status != PSA_SUCCESS )
2478 return( status );
2479 /* From here on, tmp needs to be wiped. */
2480
2481 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2482 if( status != PSA_SUCCESS )
2483 goto exit;
2484
2485 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2486 if( status != PSA_SUCCESS )
2487 goto exit;
2488
2489 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2490 if( status != PSA_SUCCESS )
2491 goto exit;
2492
Gilles Peskined911eb72018-08-14 15:18:45 +02002493 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2494 if( status != PSA_SUCCESS )
2495 goto exit;
2496
2497 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002498
2499exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002500 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002501 return( status );
2502}
2503#endif /* MBEDTLS_MD_C */
2504
mohammad16036df908f2018-04-02 08:34:15 -07002505static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002506 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002507 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002509 if( ! operation->key_set )
2510 return( PSA_ERROR_BAD_STATE );
2511 if( operation->iv_required && ! operation->iv_set )
2512 return( PSA_ERROR_BAD_STATE );
2513
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514 if( mac_size < operation->mac_size )
2515 return( PSA_ERROR_BUFFER_TOO_SMALL );
2516
Gilles Peskine8c9def32018-02-08 10:02:12 +01002517#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002518 if( operation->alg == PSA_ALG_CMAC )
2519 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002520 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2521 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2522 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002523 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002524 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002525 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002527 else
2528#endif /* MBEDTLS_CMAC_C */
2529#if defined(MBEDTLS_MD_C)
2530 if( PSA_ALG_IS_HMAC( operation->alg ) )
2531 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002532 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002533 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002534 }
2535 else
2536#endif /* MBEDTLS_MD_C */
2537 {
2538 /* This shouldn't happen if `operation` was initialized by
2539 * a setup function. */
2540 return( PSA_ERROR_BAD_STATE );
2541 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542}
2543
Gilles Peskineacd4be32018-07-08 19:56:25 +02002544psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2545 uint8_t *mac,
2546 size_t mac_size,
2547 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002548{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002549 psa_status_t status;
2550
Jaeden Amero252ef282019-02-15 14:05:35 +00002551 if( operation->alg == 0 )
2552 {
2553 return( PSA_ERROR_BAD_STATE );
2554 }
2555
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002556 /* Fill the output buffer with something that isn't a valid mac
2557 * (barring an attack on the mac and deliberately-crafted input),
2558 * in case the caller doesn't check the return status properly. */
2559 *mac_length = mac_size;
2560 /* If mac_size is 0 then mac may be NULL and then the
2561 * call to memset would have undefined behavior. */
2562 if( mac_size != 0 )
2563 memset( mac, '!', mac_size );
2564
Gilles Peskine89167cb2018-07-08 20:12:23 +02002565 if( ! operation->is_sign )
2566 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002567 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002568 }
mohammad16036df908f2018-04-02 08:34:15 -07002569
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002570 status = psa_mac_finish_internal( operation, mac, mac_size );
2571
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002572 if( status == PSA_SUCCESS )
2573 {
2574 status = psa_mac_abort( operation );
2575 if( status == PSA_SUCCESS )
2576 *mac_length = operation->mac_size;
2577 else
2578 memset( mac, '!', mac_size );
2579 }
2580 else
2581 psa_mac_abort( operation );
2582 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002583}
2584
Gilles Peskineacd4be32018-07-08 19:56:25 +02002585psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2586 const uint8_t *mac,
2587 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588{
Gilles Peskine828ed142018-06-18 23:25:51 +02002589 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002590 psa_status_t status;
2591
Jaeden Amero252ef282019-02-15 14:05:35 +00002592 if( operation->alg == 0 )
2593 {
2594 return( PSA_ERROR_BAD_STATE );
2595 }
2596
Gilles Peskine89167cb2018-07-08 20:12:23 +02002597 if( operation->is_sign )
2598 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002599 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002600 }
2601 if( operation->mac_size != mac_length )
2602 {
2603 status = PSA_ERROR_INVALID_SIGNATURE;
2604 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002605 }
mohammad16036df908f2018-04-02 08:34:15 -07002606
2607 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002608 actual_mac, sizeof( actual_mac ) );
2609
2610 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2611 status = PSA_ERROR_INVALID_SIGNATURE;
2612
2613cleanup:
2614 if( status == PSA_SUCCESS )
2615 status = psa_mac_abort( operation );
2616 else
2617 psa_mac_abort( operation );
2618
Gilles Peskine3f108122018-12-07 18:14:53 +01002619 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002620
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002621 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622}
2623
2624
Gilles Peskine20035e32018-02-03 22:44:14 +01002625
Gilles Peskine20035e32018-02-03 22:44:14 +01002626/****************************************************************/
2627/* Asymmetric cryptography */
2628/****************************************************************/
2629
Gilles Peskine2b450e32018-06-27 15:42:46 +02002630#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002631/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002632 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002633static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2634 size_t hash_length,
2635 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002636{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002637 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002638 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002639 *md_alg = mbedtls_md_get_type( md_info );
2640
2641 /* The Mbed TLS RSA module uses an unsigned int for hash length
2642 * parameters. Validate that it fits so that we don't risk an
2643 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002644#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002645 if( hash_length > UINT_MAX )
2646 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002647#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002648
2649#if defined(MBEDTLS_PKCS1_V15)
2650 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2651 * must be correct. */
2652 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2653 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002654 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002655 if( md_info == NULL )
2656 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002657 if( mbedtls_md_get_size( md_info ) != hash_length )
2658 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002659 }
2660#endif /* MBEDTLS_PKCS1_V15 */
2661
2662#if defined(MBEDTLS_PKCS1_V21)
2663 /* PSS requires a hash internally. */
2664 if( PSA_ALG_IS_RSA_PSS( alg ) )
2665 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002666 if( md_info == NULL )
2667 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002668 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002669#endif /* MBEDTLS_PKCS1_V21 */
2670
Gilles Peskine61b91d42018-06-08 16:09:36 +02002671 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002672}
2673
Gilles Peskine2b450e32018-06-27 15:42:46 +02002674static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2675 psa_algorithm_t alg,
2676 const uint8_t *hash,
2677 size_t hash_length,
2678 uint8_t *signature,
2679 size_t signature_size,
2680 size_t *signature_length )
2681{
2682 psa_status_t status;
2683 int ret;
2684 mbedtls_md_type_t md_alg;
2685
2686 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2687 if( status != PSA_SUCCESS )
2688 return( status );
2689
Gilles Peskine630a18a2018-06-29 17:49:35 +02002690 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002691 return( PSA_ERROR_BUFFER_TOO_SMALL );
2692
2693#if defined(MBEDTLS_PKCS1_V15)
2694 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2695 {
2696 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2697 MBEDTLS_MD_NONE );
2698 ret = mbedtls_rsa_pkcs1_sign( rsa,
2699 mbedtls_ctr_drbg_random,
2700 &global_data.ctr_drbg,
2701 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002702 md_alg,
2703 (unsigned int) hash_length,
2704 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002705 signature );
2706 }
2707 else
2708#endif /* MBEDTLS_PKCS1_V15 */
2709#if defined(MBEDTLS_PKCS1_V21)
2710 if( PSA_ALG_IS_RSA_PSS( alg ) )
2711 {
2712 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2713 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2714 mbedtls_ctr_drbg_random,
2715 &global_data.ctr_drbg,
2716 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002717 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002718 (unsigned int) hash_length,
2719 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002720 signature );
2721 }
2722 else
2723#endif /* MBEDTLS_PKCS1_V21 */
2724 {
2725 return( PSA_ERROR_INVALID_ARGUMENT );
2726 }
2727
2728 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002729 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002730 return( mbedtls_to_psa_error( ret ) );
2731}
2732
2733static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2734 psa_algorithm_t alg,
2735 const uint8_t *hash,
2736 size_t hash_length,
2737 const uint8_t *signature,
2738 size_t signature_length )
2739{
2740 psa_status_t status;
2741 int ret;
2742 mbedtls_md_type_t md_alg;
2743
2744 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2745 if( status != PSA_SUCCESS )
2746 return( status );
2747
Gilles Peskine630a18a2018-06-29 17:49:35 +02002748 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002749 return( PSA_ERROR_BUFFER_TOO_SMALL );
2750
2751#if defined(MBEDTLS_PKCS1_V15)
2752 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2753 {
2754 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2755 MBEDTLS_MD_NONE );
2756 ret = mbedtls_rsa_pkcs1_verify( rsa,
2757 mbedtls_ctr_drbg_random,
2758 &global_data.ctr_drbg,
2759 MBEDTLS_RSA_PUBLIC,
2760 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002761 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002762 hash,
2763 signature );
2764 }
2765 else
2766#endif /* MBEDTLS_PKCS1_V15 */
2767#if defined(MBEDTLS_PKCS1_V21)
2768 if( PSA_ALG_IS_RSA_PSS( alg ) )
2769 {
2770 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2771 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2772 mbedtls_ctr_drbg_random,
2773 &global_data.ctr_drbg,
2774 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002775 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002776 (unsigned int) hash_length,
2777 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002778 signature );
2779 }
2780 else
2781#endif /* MBEDTLS_PKCS1_V21 */
2782 {
2783 return( PSA_ERROR_INVALID_ARGUMENT );
2784 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002785
2786 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2787 * the rest of the signature is invalid". This has little use in
2788 * practice and PSA doesn't report this distinction. */
2789 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2790 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002791 return( mbedtls_to_psa_error( ret ) );
2792}
2793#endif /* MBEDTLS_RSA_C */
2794
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002795#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002796/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2797 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2798 * (even though these functions don't modify it). */
2799static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2800 psa_algorithm_t alg,
2801 const uint8_t *hash,
2802 size_t hash_length,
2803 uint8_t *signature,
2804 size_t signature_size,
2805 size_t *signature_length )
2806{
2807 int ret;
2808 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002809 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002810 mbedtls_mpi_init( &r );
2811 mbedtls_mpi_init( &s );
2812
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002813 if( signature_size < 2 * curve_bytes )
2814 {
2815 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2816 goto cleanup;
2817 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002818
Gilles Peskinea05219c2018-11-16 16:02:56 +01002819#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002820 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2821 {
2822 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2823 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2824 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2825 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2826 hash, hash_length,
2827 md_alg ) );
2828 }
2829 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002830#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002831 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002832 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002833 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2834 hash, hash_length,
2835 mbedtls_ctr_drbg_random,
2836 &global_data.ctr_drbg ) );
2837 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002838
2839 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2840 signature,
2841 curve_bytes ) );
2842 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2843 signature + curve_bytes,
2844 curve_bytes ) );
2845
2846cleanup:
2847 mbedtls_mpi_free( &r );
2848 mbedtls_mpi_free( &s );
2849 if( ret == 0 )
2850 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002851 return( mbedtls_to_psa_error( ret ) );
2852}
2853
2854static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2855 const uint8_t *hash,
2856 size_t hash_length,
2857 const uint8_t *signature,
2858 size_t signature_length )
2859{
2860 int ret;
2861 mbedtls_mpi r, s;
2862 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2863 mbedtls_mpi_init( &r );
2864 mbedtls_mpi_init( &s );
2865
2866 if( signature_length != 2 * curve_bytes )
2867 return( PSA_ERROR_INVALID_SIGNATURE );
2868
2869 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2870 signature,
2871 curve_bytes ) );
2872 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2873 signature + curve_bytes,
2874 curve_bytes ) );
2875
2876 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2877 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002878
2879cleanup:
2880 mbedtls_mpi_free( &r );
2881 mbedtls_mpi_free( &s );
2882 return( mbedtls_to_psa_error( ret ) );
2883}
2884#endif /* MBEDTLS_ECDSA_C */
2885
Gilles Peskinec5487a82018-12-03 18:08:14 +01002886psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002887 psa_algorithm_t alg,
2888 const uint8_t *hash,
2889 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002890 uint8_t *signature,
2891 size_t signature_size,
2892 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002893{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002894 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002895 psa_status_t status;
2896
2897 *signature_length = signature_size;
2898
Gilles Peskinec5487a82018-12-03 18:08:14 +01002899 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002900 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002901 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002902 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002903 {
2904 status = PSA_ERROR_INVALID_ARGUMENT;
2905 goto exit;
2906 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002907
Gilles Peskine20035e32018-02-03 22:44:14 +01002908#if defined(MBEDTLS_RSA_C)
2909 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2910 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002911 status = psa_rsa_sign( slot->data.rsa,
2912 alg,
2913 hash, hash_length,
2914 signature, signature_size,
2915 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002916 }
2917 else
2918#endif /* defined(MBEDTLS_RSA_C) */
2919#if defined(MBEDTLS_ECP_C)
2920 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2921 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002922#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002923 if(
2924#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2925 PSA_ALG_IS_ECDSA( alg )
2926#else
2927 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2928#endif
2929 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002930 status = psa_ecdsa_sign( slot->data.ecp,
2931 alg,
2932 hash, hash_length,
2933 signature, signature_size,
2934 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002935 else
2936#endif /* defined(MBEDTLS_ECDSA_C) */
2937 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002938 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002939 }
itayzafrir5c753392018-05-08 11:18:38 +03002940 }
2941 else
2942#endif /* defined(MBEDTLS_ECP_C) */
2943 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002944 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002945 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002946
2947exit:
2948 /* Fill the unused part of the output buffer (the whole buffer on error,
2949 * the trailing part on success) with something that isn't a valid mac
2950 * (barring an attack on the mac and deliberately-crafted input),
2951 * in case the caller doesn't check the return status properly. */
2952 if( status == PSA_SUCCESS )
2953 memset( signature + *signature_length, '!',
2954 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002955 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002956 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002957 /* If signature_size is 0 then we have nothing to do. We must not call
2958 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002959 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002960}
2961
Gilles Peskinec5487a82018-12-03 18:08:14 +01002962psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002963 psa_algorithm_t alg,
2964 const uint8_t *hash,
2965 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002966 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002967 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002968{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002969 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002970 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002971
Gilles Peskinec5487a82018-12-03 18:08:14 +01002972 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002973 if( status != PSA_SUCCESS )
2974 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002975
Gilles Peskine61b91d42018-06-08 16:09:36 +02002976#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002977 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002978 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002979 return( psa_rsa_verify( slot->data.rsa,
2980 alg,
2981 hash, hash_length,
2982 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002983 }
2984 else
2985#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002986#if defined(MBEDTLS_ECP_C)
2987 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2988 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002989#if defined(MBEDTLS_ECDSA_C)
2990 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002991 return( psa_ecdsa_verify( slot->data.ecp,
2992 hash, hash_length,
2993 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002994 else
2995#endif /* defined(MBEDTLS_ECDSA_C) */
2996 {
2997 return( PSA_ERROR_INVALID_ARGUMENT );
2998 }
itayzafrir5c753392018-05-08 11:18:38 +03002999 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003000 else
3001#endif /* defined(MBEDTLS_ECP_C) */
3002 {
3003 return( PSA_ERROR_NOT_SUPPORTED );
3004 }
3005}
3006
Gilles Peskine072ac562018-06-30 00:21:29 +02003007#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3008static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3009 mbedtls_rsa_context *rsa )
3010{
3011 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3012 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3013 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3014 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3015}
3016#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3017
Gilles Peskinec5487a82018-12-03 18:08:14 +01003018psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003019 psa_algorithm_t alg,
3020 const uint8_t *input,
3021 size_t input_length,
3022 const uint8_t *salt,
3023 size_t salt_length,
3024 uint8_t *output,
3025 size_t output_size,
3026 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003027{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003028 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003029 psa_status_t status;
3030
Darryl Green5cc689a2018-07-24 15:34:10 +01003031 (void) input;
3032 (void) input_length;
3033 (void) salt;
3034 (void) output;
3035 (void) output_size;
3036
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003037 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003038
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003039 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3040 return( PSA_ERROR_INVALID_ARGUMENT );
3041
Gilles Peskinec5487a82018-12-03 18:08:14 +01003042 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003043 if( status != PSA_SUCCESS )
3044 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003045 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
3046 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003047 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003048
3049#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003050 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003051 {
3052 mbedtls_rsa_context *rsa = slot->data.rsa;
3053 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003054 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003055 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003056#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003057 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003058 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003059 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3060 mbedtls_ctr_drbg_random,
3061 &global_data.ctr_drbg,
3062 MBEDTLS_RSA_PUBLIC,
3063 input_length,
3064 input,
3065 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003066 }
3067 else
3068#endif /* MBEDTLS_PKCS1_V15 */
3069#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003070 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003071 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003072 psa_rsa_oaep_set_padding_mode( alg, rsa );
3073 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3074 mbedtls_ctr_drbg_random,
3075 &global_data.ctr_drbg,
3076 MBEDTLS_RSA_PUBLIC,
3077 salt, salt_length,
3078 input_length,
3079 input,
3080 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003081 }
3082 else
3083#endif /* MBEDTLS_PKCS1_V21 */
3084 {
3085 return( PSA_ERROR_INVALID_ARGUMENT );
3086 }
3087 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003088 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003089 return( mbedtls_to_psa_error( ret ) );
3090 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003091 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003092#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093 {
3094 return( PSA_ERROR_NOT_SUPPORTED );
3095 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003096}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003097
Gilles Peskinec5487a82018-12-03 18:08:14 +01003098psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003099 psa_algorithm_t alg,
3100 const uint8_t *input,
3101 size_t input_length,
3102 const uint8_t *salt,
3103 size_t salt_length,
3104 uint8_t *output,
3105 size_t output_size,
3106 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003107{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003108 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003109 psa_status_t status;
3110
Darryl Green5cc689a2018-07-24 15:34:10 +01003111 (void) input;
3112 (void) input_length;
3113 (void) salt;
3114 (void) output;
3115 (void) output_size;
3116
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003117 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003118
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003119 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3120 return( PSA_ERROR_INVALID_ARGUMENT );
3121
Gilles Peskinec5487a82018-12-03 18:08:14 +01003122 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003123 if( status != PSA_SUCCESS )
3124 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
3126 return( PSA_ERROR_INVALID_ARGUMENT );
3127
3128#if defined(MBEDTLS_RSA_C)
3129 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
3130 {
3131 mbedtls_rsa_context *rsa = slot->data.rsa;
3132 int ret;
3133
Gilles Peskine630a18a2018-06-29 17:49:35 +02003134 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003135 return( PSA_ERROR_INVALID_ARGUMENT );
3136
3137#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003138 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003139 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003140 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3141 mbedtls_ctr_drbg_random,
3142 &global_data.ctr_drbg,
3143 MBEDTLS_RSA_PRIVATE,
3144 output_length,
3145 input,
3146 output,
3147 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003148 }
3149 else
3150#endif /* MBEDTLS_PKCS1_V15 */
3151#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003152 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003153 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003154 psa_rsa_oaep_set_padding_mode( alg, rsa );
3155 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3156 mbedtls_ctr_drbg_random,
3157 &global_data.ctr_drbg,
3158 MBEDTLS_RSA_PRIVATE,
3159 salt, salt_length,
3160 output_length,
3161 input,
3162 output,
3163 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003164 }
3165 else
3166#endif /* MBEDTLS_PKCS1_V21 */
3167 {
3168 return( PSA_ERROR_INVALID_ARGUMENT );
3169 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003170
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003171 return( mbedtls_to_psa_error( ret ) );
3172 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003173 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003174#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003175 {
3176 return( PSA_ERROR_NOT_SUPPORTED );
3177 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003178}
Gilles Peskine20035e32018-02-03 22:44:14 +01003179
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003180
3181
mohammad1603503973b2018-03-12 15:59:30 +02003182/****************************************************************/
3183/* Symmetric cryptography */
3184/****************************************************************/
3185
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003186/* Initialize the cipher operation structure. Once this function has been
3187 * called, psa_cipher_abort can run and will do the right thing. */
3188static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3189 psa_algorithm_t alg )
3190{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003191 if( ! PSA_ALG_IS_CIPHER( alg ) )
3192 {
3193 memset( operation, 0, sizeof( *operation ) );
3194 return( PSA_ERROR_INVALID_ARGUMENT );
3195 }
3196
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003197 operation->alg = alg;
3198 operation->key_set = 0;
3199 operation->iv_set = 0;
3200 operation->iv_required = 1;
3201 operation->iv_size = 0;
3202 operation->block_size = 0;
3203 mbedtls_cipher_init( &operation->ctx.cipher );
3204 return( PSA_SUCCESS );
3205}
3206
Gilles Peskinee553c652018-06-04 16:22:46 +02003207static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003208 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003209 psa_algorithm_t alg,
3210 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003211{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003212 int ret = 0;
3213 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003214 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003215 size_t key_bits;
3216 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003217 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3218 PSA_KEY_USAGE_ENCRYPT :
3219 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003220
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003221 /* A context must be freshly initialized before it can be set up. */
3222 if( operation->alg != 0 )
3223 {
3224 return( PSA_ERROR_BAD_STATE );
3225 }
3226
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003227 status = psa_cipher_init( operation, alg );
3228 if( status != PSA_SUCCESS )
3229 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003230
Gilles Peskinec5487a82018-12-03 18:08:14 +01003231 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003232 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003233 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003234 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003235
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003236 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003237 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003238 {
3239 status = PSA_ERROR_NOT_SUPPORTED;
3240 goto exit;
3241 }
mohammad1603503973b2018-03-12 15:59:30 +02003242
mohammad1603503973b2018-03-12 15:59:30 +02003243 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003244 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003245 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003246
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003247#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003248 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003249 {
3250 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3251 unsigned char keys[24];
3252 memcpy( keys, slot->data.raw.data, 16 );
3253 memcpy( keys + 16, slot->data.raw.data, 8 );
3254 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3255 keys,
3256 192, cipher_operation );
3257 }
3258 else
3259#endif
3260 {
3261 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3262 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003263 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003264 }
Moran Peker41deec42018-04-04 15:43:05 +03003265 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003266 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003267
mohammad16038481e742018-03-18 13:57:31 +02003268#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003269 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003270 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003271 case PSA_ALG_CBC_NO_PADDING:
3272 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3273 MBEDTLS_PADDING_NONE );
3274 break;
3275 case PSA_ALG_CBC_PKCS7:
3276 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3277 MBEDTLS_PADDING_PKCS7 );
3278 break;
3279 default:
3280 /* The algorithm doesn't involve padding. */
3281 ret = 0;
3282 break;
3283 }
3284 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003285 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003286#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3287
mohammad1603503973b2018-03-12 15:59:30 +02003288 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003289 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3290 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3291 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003292 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003293 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003294 }
mohammad1603503973b2018-03-12 15:59:30 +02003295
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003296exit:
3297 if( status == 0 )
3298 status = mbedtls_to_psa_error( ret );
3299 if( status != 0 )
3300 psa_cipher_abort( operation );
3301 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003302}
3303
Gilles Peskinefe119512018-07-08 21:39:34 +02003304psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003305 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003306 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003307{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003308 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003309}
3310
Gilles Peskinefe119512018-07-08 21:39:34 +02003311psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003312 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003313 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003314{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003315 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003316}
3317
Gilles Peskinefe119512018-07-08 21:39:34 +02003318psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3319 unsigned char *iv,
3320 size_t iv_size,
3321 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003322{
itayzafrir534bd7c2018-08-02 13:56:32 +03003323 psa_status_t status;
3324 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003325 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003326 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003327 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003328 }
Moran Peker41deec42018-04-04 15:43:05 +03003329 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003330 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003331 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003332 goto exit;
3333 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003334 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3335 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003336 if( ret != 0 )
3337 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003338 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003339 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003340 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003341
mohammad16038481e742018-03-18 13:57:31 +02003342 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003343 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003344
Moran Peker395db872018-05-31 14:07:14 +03003345exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003346 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003347 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003348 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003349}
3350
Gilles Peskinefe119512018-07-08 21:39:34 +02003351psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3352 const unsigned char *iv,
3353 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003354{
itayzafrir534bd7c2018-08-02 13:56:32 +03003355 psa_status_t status;
3356 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003357 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003358 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003359 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003360 }
Moran Pekera28258c2018-05-29 16:25:04 +03003361 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003362 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003363 status = PSA_ERROR_INVALID_ARGUMENT;
3364 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003365 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003366 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3367 status = mbedtls_to_psa_error( ret );
3368exit:
3369 if( status == PSA_SUCCESS )
3370 operation->iv_set = 1;
3371 else
mohammad1603503973b2018-03-12 15:59:30 +02003372 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003373 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003374}
3375
Gilles Peskinee553c652018-06-04 16:22:46 +02003376psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3377 const uint8_t *input,
3378 size_t input_length,
3379 unsigned char *output,
3380 size_t output_size,
3381 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003382{
itayzafrir534bd7c2018-08-02 13:56:32 +03003383 psa_status_t status;
3384 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003385 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003386
3387 if( operation->alg == 0 )
3388 {
3389 return( PSA_ERROR_BAD_STATE );
3390 }
3391
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003392 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003393 {
3394 /* Take the unprocessed partial block left over from previous
3395 * update calls, if any, plus the input to this call. Remove
3396 * the last partial block, if any. You get the data that will be
3397 * output in this call. */
3398 expected_output_size =
3399 ( operation->ctx.cipher.unprocessed_len + input_length )
3400 / operation->block_size * operation->block_size;
3401 }
3402 else
3403 {
3404 expected_output_size = input_length;
3405 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003406
Gilles Peskine89d789c2018-06-04 17:17:16 +02003407 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003408 {
3409 status = PSA_ERROR_BUFFER_TOO_SMALL;
3410 goto exit;
3411 }
mohammad160382759612018-03-12 18:16:40 +02003412
mohammad1603503973b2018-03-12 15:59:30 +02003413 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003414 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003415 status = mbedtls_to_psa_error( ret );
3416exit:
3417 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003418 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003419 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003420}
3421
Gilles Peskinee553c652018-06-04 16:22:46 +02003422psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3423 uint8_t *output,
3424 size_t output_size,
3425 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003426{
David Saadab4ecc272019-02-14 13:48:10 +02003427 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003428 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003429 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003430
mohammad1603503973b2018-03-12 15:59:30 +02003431 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003432 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003433 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003434 }
3435 if( operation->iv_required && ! operation->iv_set )
3436 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003437 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003438 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003439
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003440 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003441 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3442 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003443 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003444 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003445 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003446 }
3447
Janos Follath315b51c2018-07-09 16:04:51 +01003448 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3449 temp_output_buffer,
3450 output_length );
3451 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003452 {
Janos Follath315b51c2018-07-09 16:04:51 +01003453 status = mbedtls_to_psa_error( cipher_ret );
3454 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003455 }
Janos Follath315b51c2018-07-09 16:04:51 +01003456
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003457 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003458 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003459 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003460 memcpy( output, temp_output_buffer, *output_length );
3461 else
3462 {
Janos Follath315b51c2018-07-09 16:04:51 +01003463 status = PSA_ERROR_BUFFER_TOO_SMALL;
3464 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003465 }
mohammad1603503973b2018-03-12 15:59:30 +02003466
Gilles Peskine3f108122018-12-07 18:14:53 +01003467 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003468 status = psa_cipher_abort( operation );
3469
3470 return( status );
3471
3472error:
3473
3474 *output_length = 0;
3475
Gilles Peskine3f108122018-12-07 18:14:53 +01003476 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003477 (void) psa_cipher_abort( operation );
3478
3479 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003480}
3481
Gilles Peskinee553c652018-06-04 16:22:46 +02003482psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3483{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003484 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003485 {
3486 /* The object has (apparently) been initialized but it is not
3487 * in use. It's ok to call abort on such an object, and there's
3488 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003489 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003490 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003491
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003492 /* Sanity check (shouldn't happen: operation->alg should
3493 * always have been initialized to a valid value). */
3494 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3495 return( PSA_ERROR_BAD_STATE );
3496
mohammad1603503973b2018-03-12 15:59:30 +02003497 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003498
Moran Peker41deec42018-04-04 15:43:05 +03003499 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003500 operation->key_set = 0;
3501 operation->iv_set = 0;
3502 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003503 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003504 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003505
Moran Peker395db872018-05-31 14:07:14 +03003506 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003507}
3508
Gilles Peskinea0655c32018-04-30 17:06:50 +02003509
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003510
mohammad16038cc1cee2018-03-28 01:21:33 +03003511/****************************************************************/
3512/* Key Policy */
3513/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003514
mohammad160327010052018-07-03 13:16:15 +03003515#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003516void psa_key_policy_set_usage( psa_key_policy_t *policy,
3517 psa_key_usage_t usage,
3518 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003519{
mohammad16034eed7572018-03-28 05:14:59 -07003520 policy->usage = usage;
3521 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003522}
3523
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003524psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003525{
mohammad16036df908f2018-04-02 08:34:15 -07003526 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003527}
3528
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003529psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003530{
mohammad16036df908f2018-04-02 08:34:15 -07003531 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003532}
mohammad160327010052018-07-03 13:16:15 +03003533#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003534
Gilles Peskinec5487a82018-12-03 18:08:14 +01003535psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003536 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003537{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003538 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003539 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003540
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003541 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003542 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003543
Gilles Peskinec5487a82018-12-03 18:08:14 +01003544 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003545 if( status != PSA_SUCCESS )
3546 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003547
Gilles Peskine4747d192019-04-17 15:05:45 +02003548 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003549}
3550
Gilles Peskinec5487a82018-12-03 18:08:14 +01003551psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003552 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003553{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003554 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003555 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003556
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003557 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003558 return( PSA_ERROR_INVALID_ARGUMENT );
3559
Gilles Peskinec5487a82018-12-03 18:08:14 +01003560 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003561 if( status != PSA_SUCCESS )
3562 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003563
mohammad16036df908f2018-04-02 08:34:15 -07003564 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003565
3566 return( PSA_SUCCESS );
3567}
Gilles Peskine20035e32018-02-03 22:44:14 +01003568
Gilles Peskinea0655c32018-04-30 17:06:50 +02003569
3570
mohammad1603804cd712018-03-20 22:44:08 +02003571/****************************************************************/
3572/* Key Lifetime */
3573/****************************************************************/
3574
Gilles Peskine87a5e562019-04-17 12:28:25 +02003575psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003576 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003577{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003578 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003579 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003580
Gilles Peskinec5487a82018-12-03 18:08:14 +01003581 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003582 if( status != PSA_SUCCESS )
3583 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003584
mohammad1603804cd712018-03-20 22:44:08 +02003585 *lifetime = slot->lifetime;
3586
3587 return( PSA_SUCCESS );
3588}
3589
Gilles Peskine20035e32018-02-03 22:44:14 +01003590
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003591
mohammad16035955c982018-04-26 00:53:03 +03003592/****************************************************************/
3593/* AEAD */
3594/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003595
Gilles Peskineedf9a652018-08-17 18:11:56 +02003596typedef struct
3597{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003598 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003599 const mbedtls_cipher_info_t *cipher_info;
3600 union
3601 {
3602#if defined(MBEDTLS_CCM_C)
3603 mbedtls_ccm_context ccm;
3604#endif /* MBEDTLS_CCM_C */
3605#if defined(MBEDTLS_GCM_C)
3606 mbedtls_gcm_context gcm;
3607#endif /* MBEDTLS_GCM_C */
3608 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003609 psa_algorithm_t core_alg;
3610 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003611 uint8_t tag_length;
3612} aead_operation_t;
3613
Gilles Peskine30a9e412019-01-14 18:36:12 +01003614static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003615{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003616 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003617 {
3618#if defined(MBEDTLS_CCM_C)
3619 case PSA_ALG_CCM:
3620 mbedtls_ccm_free( &operation->ctx.ccm );
3621 break;
3622#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003623#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003624 case PSA_ALG_GCM:
3625 mbedtls_gcm_free( &operation->ctx.gcm );
3626 break;
3627#endif /* MBEDTLS_GCM_C */
3628 }
3629}
3630
3631static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003632 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003633 psa_key_usage_t usage,
3634 psa_algorithm_t alg )
3635{
3636 psa_status_t status;
3637 size_t key_bits;
3638 mbedtls_cipher_id_t cipher_id;
3639
Gilles Peskinec5487a82018-12-03 18:08:14 +01003640 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003641 if( status != PSA_SUCCESS )
3642 return( status );
3643
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003644 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003645
3646 operation->cipher_info =
3647 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3648 &cipher_id );
3649 if( operation->cipher_info == NULL )
3650 return( PSA_ERROR_NOT_SUPPORTED );
3651
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003652 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003653 {
3654#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003655 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3656 operation->core_alg = PSA_ALG_CCM;
3657 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003658 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3659 return( PSA_ERROR_INVALID_ARGUMENT );
3660 mbedtls_ccm_init( &operation->ctx.ccm );
3661 status = mbedtls_to_psa_error(
3662 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3663 operation->slot->data.raw.data,
3664 (unsigned int) key_bits ) );
3665 if( status != 0 )
3666 goto cleanup;
3667 break;
3668#endif /* MBEDTLS_CCM_C */
3669
3670#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003671 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3672 operation->core_alg = PSA_ALG_GCM;
3673 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003674 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3675 return( PSA_ERROR_INVALID_ARGUMENT );
3676 mbedtls_gcm_init( &operation->ctx.gcm );
3677 status = mbedtls_to_psa_error(
3678 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3679 operation->slot->data.raw.data,
3680 (unsigned int) key_bits ) );
3681 break;
3682#endif /* MBEDTLS_GCM_C */
3683
3684 default:
3685 return( PSA_ERROR_NOT_SUPPORTED );
3686 }
3687
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003688 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3689 {
3690 status = PSA_ERROR_INVALID_ARGUMENT;
3691 goto cleanup;
3692 }
3693 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3694 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3695 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3696 * In both cases, mbedtls_xxx will validate the tag length below. */
3697
Gilles Peskineedf9a652018-08-17 18:11:56 +02003698 return( PSA_SUCCESS );
3699
3700cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003701 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003702 return( status );
3703}
3704
Gilles Peskinec5487a82018-12-03 18:08:14 +01003705psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003706 psa_algorithm_t alg,
3707 const uint8_t *nonce,
3708 size_t nonce_length,
3709 const uint8_t *additional_data,
3710 size_t additional_data_length,
3711 const uint8_t *plaintext,
3712 size_t plaintext_length,
3713 uint8_t *ciphertext,
3714 size_t ciphertext_size,
3715 size_t *ciphertext_length )
3716{
mohammad16035955c982018-04-26 00:53:03 +03003717 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003718 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003719 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003720
mohammad1603f08a5502018-06-03 15:05:47 +03003721 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003722
Gilles Peskinec5487a82018-12-03 18:08:14 +01003723 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003724 if( status != PSA_SUCCESS )
3725 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003726
Gilles Peskineedf9a652018-08-17 18:11:56 +02003727 /* For all currently supported modes, the tag is at the end of the
3728 * ciphertext. */
3729 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3730 {
3731 status = PSA_ERROR_BUFFER_TOO_SMALL;
3732 goto exit;
3733 }
3734 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003735
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003736#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003737 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003738 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003739 status = mbedtls_to_psa_error(
3740 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3741 MBEDTLS_GCM_ENCRYPT,
3742 plaintext_length,
3743 nonce, nonce_length,
3744 additional_data, additional_data_length,
3745 plaintext, ciphertext,
3746 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003747 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003748 else
3749#endif /* MBEDTLS_GCM_C */
3750#if defined(MBEDTLS_CCM_C)
3751 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003752 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003753 status = mbedtls_to_psa_error(
3754 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3755 plaintext_length,
3756 nonce, nonce_length,
3757 additional_data,
3758 additional_data_length,
3759 plaintext, ciphertext,
3760 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003761 }
mohammad16035c8845f2018-05-09 05:40:09 -07003762 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003763#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003764 {
mohammad1603554faad2018-06-03 15:07:38 +03003765 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003766 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003767
Gilles Peskineedf9a652018-08-17 18:11:56 +02003768 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3769 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003770
Gilles Peskineedf9a652018-08-17 18:11:56 +02003771exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003772 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003773 if( status == PSA_SUCCESS )
3774 *ciphertext_length = plaintext_length + operation.tag_length;
3775 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003776}
3777
Gilles Peskineee652a32018-06-01 19:23:52 +02003778/* Locate the tag in a ciphertext buffer containing the encrypted data
3779 * followed by the tag. Return the length of the part preceding the tag in
3780 * *plaintext_length. This is the size of the plaintext in modes where
3781 * the encrypted data has the same size as the plaintext, such as
3782 * CCM and GCM. */
3783static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3784 const uint8_t *ciphertext,
3785 size_t ciphertext_length,
3786 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003787 const uint8_t **p_tag )
3788{
3789 size_t payload_length;
3790 if( tag_length > ciphertext_length )
3791 return( PSA_ERROR_INVALID_ARGUMENT );
3792 payload_length = ciphertext_length - tag_length;
3793 if( payload_length > plaintext_size )
3794 return( PSA_ERROR_BUFFER_TOO_SMALL );
3795 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003796 return( PSA_SUCCESS );
3797}
3798
Gilles Peskinec5487a82018-12-03 18:08:14 +01003799psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003800 psa_algorithm_t alg,
3801 const uint8_t *nonce,
3802 size_t nonce_length,
3803 const uint8_t *additional_data,
3804 size_t additional_data_length,
3805 const uint8_t *ciphertext,
3806 size_t ciphertext_length,
3807 uint8_t *plaintext,
3808 size_t plaintext_size,
3809 size_t *plaintext_length )
3810{
mohammad16035955c982018-04-26 00:53:03 +03003811 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003812 aead_operation_t operation;
3813 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003814
Gilles Peskineee652a32018-06-01 19:23:52 +02003815 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003816
Gilles Peskinec5487a82018-12-03 18:08:14 +01003817 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003818 if( status != PSA_SUCCESS )
3819 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003820
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003821#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003822 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003823 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003824 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003825 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003826 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003827 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003828 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003829
Gilles Peskineedf9a652018-08-17 18:11:56 +02003830 status = mbedtls_to_psa_error(
3831 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3832 ciphertext_length - operation.tag_length,
3833 nonce, nonce_length,
3834 additional_data,
3835 additional_data_length,
3836 tag, operation.tag_length,
3837 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003838 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003839 else
3840#endif /* MBEDTLS_GCM_C */
3841#if defined(MBEDTLS_CCM_C)
3842 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003843 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003844 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003845 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003846 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003847 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003848 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003849
Gilles Peskineedf9a652018-08-17 18:11:56 +02003850 status = mbedtls_to_psa_error(
3851 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3852 ciphertext_length - operation.tag_length,
3853 nonce, nonce_length,
3854 additional_data,
3855 additional_data_length,
3856 ciphertext, plaintext,
3857 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003858 }
mohammad160339574652018-06-01 04:39:53 -07003859 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003860#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003861 {
mohammad1603554faad2018-06-03 15:07:38 +03003862 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003863 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003864
Gilles Peskineedf9a652018-08-17 18:11:56 +02003865 if( status != PSA_SUCCESS && plaintext_size != 0 )
3866 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003867
Gilles Peskineedf9a652018-08-17 18:11:56 +02003868exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003869 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003870 if( status == PSA_SUCCESS )
3871 *plaintext_length = ciphertext_length - operation.tag_length;
3872 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003873}
3874
Gilles Peskinea0655c32018-04-30 17:06:50 +02003875
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003876
Gilles Peskine20035e32018-02-03 22:44:14 +01003877/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003878/* Generators */
3879/****************************************************************/
3880
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003881#define HKDF_STATE_INIT 0 /* no input yet */
3882#define HKDF_STATE_STARTED 1 /* got salt */
3883#define HKDF_STATE_KEYED 2 /* got key */
3884#define HKDF_STATE_OUTPUT 3 /* output started */
3885
Gilles Peskine969c5d62019-01-16 15:53:06 +01003886static psa_algorithm_t psa_generator_get_kdf_alg(
3887 const psa_crypto_generator_t *generator )
3888{
3889 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3890 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3891 else
3892 return( generator->alg );
3893}
3894
3895
Gilles Peskineeab56e42018-07-12 17:12:33 +02003896psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3897{
3898 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003899 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3900 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003901 {
3902 /* The object has (apparently) been initialized but it is not
3903 * in use. It's ok to call abort on such an object, and there's
3904 * nothing to do. */
3905 }
3906 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003907 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003908 {
3909 if( generator->ctx.buffer.data != NULL )
3910 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003911 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003912 generator->ctx.buffer.size );
3913 mbedtls_free( generator->ctx.buffer.data );
3914 }
3915 }
3916 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003917#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003918 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003919 {
3920 mbedtls_free( generator->ctx.hkdf.info );
3921 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3922 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003923 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00003924 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003925 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003926 {
3927 if( generator->ctx.tls12_prf.key != NULL )
3928 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003929 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003930 generator->ctx.tls12_prf.key_len );
3931 mbedtls_free( generator->ctx.tls12_prf.key );
3932 }
Hanno Becker580fba12018-11-13 20:50:45 +00003933
3934 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3935 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003936 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003937 generator->ctx.tls12_prf.Ai_with_seed_len );
3938 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3939 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003940 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003941 else
3942#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003943 {
3944 status = PSA_ERROR_BAD_STATE;
3945 }
3946 memset( generator, 0, sizeof( *generator ) );
3947 return( status );
3948}
3949
Gilles Peskineeab56e42018-07-12 17:12:33 +02003950psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3951 size_t *capacity)
3952{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003953 if( generator->alg == 0 )
3954 {
3955 /* This is a blank generator. */
3956 return PSA_ERROR_BAD_STATE;
3957 }
3958
Gilles Peskineeab56e42018-07-12 17:12:33 +02003959 *capacity = generator->capacity;
3960 return( PSA_SUCCESS );
3961}
3962
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003963psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
3964 size_t capacity )
3965{
3966 if( generator->alg == 0 )
3967 return( PSA_ERROR_BAD_STATE );
3968 if( capacity > generator->capacity )
3969 return( PSA_ERROR_INVALID_ARGUMENT );
3970 generator->capacity = capacity;
3971 return( PSA_SUCCESS );
3972}
3973
Gilles Peskinebef7f142018-07-12 17:22:21 +02003974#if defined(MBEDTLS_MD_C)
3975/* Read some bytes from an HKDF-based generator. This performs a chunk
3976 * of the expand phase of the HKDF algorithm. */
3977static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3978 psa_algorithm_t hash_alg,
3979 uint8_t *output,
3980 size_t output_length )
3981{
3982 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3983 psa_status_t status;
3984
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003985 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3986 return( PSA_ERROR_BAD_STATE );
3987 hkdf->state = HKDF_STATE_OUTPUT;
3988
Gilles Peskinebef7f142018-07-12 17:22:21 +02003989 while( output_length != 0 )
3990 {
3991 /* Copy what remains of the current block */
3992 uint8_t n = hash_length - hkdf->offset_in_block;
3993 if( n > output_length )
3994 n = (uint8_t) output_length;
3995 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3996 output += n;
3997 output_length -= n;
3998 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003999 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004000 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004001 /* We can't be wanting more output after block 0xff, otherwise
4002 * the capacity check in psa_generator_read() would have
4003 * prevented this call. It could happen only if the generator
4004 * object was corrupted or if this function is called directly
4005 * inside the library. */
4006 if( hkdf->block_number == 0xff )
4007 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004008
4009 /* We need a new block */
4010 ++hkdf->block_number;
4011 hkdf->offset_in_block = 0;
4012 status = psa_hmac_setup_internal( &hkdf->hmac,
4013 hkdf->prk, hash_length,
4014 hash_alg );
4015 if( status != PSA_SUCCESS )
4016 return( status );
4017 if( hkdf->block_number != 1 )
4018 {
4019 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4020 hkdf->output_block,
4021 hash_length );
4022 if( status != PSA_SUCCESS )
4023 return( status );
4024 }
4025 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4026 hkdf->info,
4027 hkdf->info_length );
4028 if( status != PSA_SUCCESS )
4029 return( status );
4030 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4031 &hkdf->block_number, 1 );
4032 if( status != PSA_SUCCESS )
4033 return( status );
4034 status = psa_hmac_finish_internal( &hkdf->hmac,
4035 hkdf->output_block,
4036 sizeof( hkdf->output_block ) );
4037 if( status != PSA_SUCCESS )
4038 return( status );
4039 }
4040
4041 return( PSA_SUCCESS );
4042}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004043
4044static psa_status_t psa_generator_tls12_prf_generate_next_block(
4045 psa_tls12_prf_generator_t *tls12_prf,
4046 psa_algorithm_t alg )
4047{
4048 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4049 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4050 psa_hmac_internal_data hmac;
4051 psa_status_t status, cleanup_status;
4052
Hanno Becker3b339e22018-11-13 20:56:14 +00004053 unsigned char *Ai;
4054 size_t Ai_len;
4055
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004056 /* We can't be wanting more output after block 0xff, otherwise
4057 * the capacity check in psa_generator_read() would have
4058 * prevented this call. It could happen only if the generator
4059 * object was corrupted or if this function is called directly
4060 * inside the library. */
4061 if( tls12_prf->block_number == 0xff )
4062 return( PSA_ERROR_BAD_STATE );
4063
4064 /* We need a new block */
4065 ++tls12_prf->block_number;
4066 tls12_prf->offset_in_block = 0;
4067
4068 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4069 *
4070 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4071 *
4072 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4073 * HMAC_hash(secret, A(2) + seed) +
4074 * HMAC_hash(secret, A(3) + seed) + ...
4075 *
4076 * A(0) = seed
4077 * A(i) = HMAC_hash( secret, A(i-1) )
4078 *
4079 * The `psa_tls12_prf_generator` structures saves the block
4080 * `HMAC_hash(secret, A(i) + seed)` from which the output
4081 * is currently extracted as `output_block`, while
4082 * `A(i) + seed` is stored in `Ai_with_seed`.
4083 *
4084 * Generating a new block means recalculating `Ai_with_seed`
4085 * from the A(i)-part of it, and afterwards recalculating
4086 * `output_block`.
4087 *
4088 * A(0) is computed at setup time.
4089 *
4090 */
4091
4092 psa_hmac_init_internal( &hmac );
4093
4094 /* We must distinguish the calculation of A(1) from those
4095 * of A(2) and higher, because A(0)=seed has a different
4096 * length than the other A(i). */
4097 if( tls12_prf->block_number == 1 )
4098 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004099 Ai = tls12_prf->Ai_with_seed + hash_length;
4100 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004101 }
4102 else
4103 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004104 Ai = tls12_prf->Ai_with_seed;
4105 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004106 }
4107
Hanno Becker3b339e22018-11-13 20:56:14 +00004108 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4109 status = psa_hmac_setup_internal( &hmac,
4110 tls12_prf->key,
4111 tls12_prf->key_len,
4112 hash_alg );
4113 if( status != PSA_SUCCESS )
4114 goto cleanup;
4115
4116 status = psa_hash_update( &hmac.hash_ctx,
4117 Ai, Ai_len );
4118 if( status != PSA_SUCCESS )
4119 goto cleanup;
4120
4121 status = psa_hmac_finish_internal( &hmac,
4122 tls12_prf->Ai_with_seed,
4123 hash_length );
4124 if( status != PSA_SUCCESS )
4125 goto cleanup;
4126
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004127 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4128 status = psa_hmac_setup_internal( &hmac,
4129 tls12_prf->key,
4130 tls12_prf->key_len,
4131 hash_alg );
4132 if( status != PSA_SUCCESS )
4133 goto cleanup;
4134
4135 status = psa_hash_update( &hmac.hash_ctx,
4136 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004137 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004138 if( status != PSA_SUCCESS )
4139 goto cleanup;
4140
4141 status = psa_hmac_finish_internal( &hmac,
4142 tls12_prf->output_block,
4143 hash_length );
4144 if( status != PSA_SUCCESS )
4145 goto cleanup;
4146
4147cleanup:
4148
4149 cleanup_status = psa_hmac_abort_internal( &hmac );
4150 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4151 status = cleanup_status;
4152
4153 return( status );
4154}
4155
4156/* Read some bytes from an TLS-1.2-PRF-based generator.
4157 * See Section 5 of RFC 5246. */
4158static psa_status_t psa_generator_tls12_prf_read(
4159 psa_tls12_prf_generator_t *tls12_prf,
4160 psa_algorithm_t alg,
4161 uint8_t *output,
4162 size_t output_length )
4163{
4164 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4165 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4166 psa_status_t status;
4167
4168 while( output_length != 0 )
4169 {
4170 /* Copy what remains of the current block */
4171 uint8_t n = hash_length - tls12_prf->offset_in_block;
4172
4173 /* Check if we have fully processed the current block. */
4174 if( n == 0 )
4175 {
4176 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
4177 alg );
4178 if( status != PSA_SUCCESS )
4179 return( status );
4180
4181 continue;
4182 }
4183
4184 if( n > output_length )
4185 n = (uint8_t) output_length;
4186 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4187 n );
4188 output += n;
4189 output_length -= n;
4190 tls12_prf->offset_in_block += n;
4191 }
4192
4193 return( PSA_SUCCESS );
4194}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004195#endif /* MBEDTLS_MD_C */
4196
Gilles Peskineeab56e42018-07-12 17:12:33 +02004197psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
4198 uint8_t *output,
4199 size_t output_length )
4200{
4201 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004202 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004203
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004204 if( generator->alg == 0 )
4205 {
4206 /* This is a blank generator. */
4207 return PSA_ERROR_BAD_STATE;
4208 }
4209
Gilles Peskineeab56e42018-07-12 17:12:33 +02004210 if( output_length > generator->capacity )
4211 {
4212 generator->capacity = 0;
4213 /* Go through the error path to wipe all confidential data now
4214 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004215 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004216 goto exit;
4217 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004218 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004219 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004220 /* Edge case: this is a finished generator, and 0 bytes
4221 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004222 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4223 * INSUFFICIENT_CAPACITY, which is right for a finished
4224 * generator, for consistency with the case when
4225 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004226 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004227 }
4228 generator->capacity -= output_length;
4229
Gilles Peskine969c5d62019-01-16 15:53:06 +01004230 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004231 {
Gilles Peskine211a4362018-10-25 22:22:31 +02004232 /* Initially, the capacity of a selection generator is always
4233 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
4234 * abbreviated in this comment as `size`. When the remaining
4235 * capacity is `c`, the next bytes to serve start `c` bytes
4236 * from the end of the buffer, i.e. `size - c` from the
4237 * beginning of the buffer. Since `generator->capacity` was just
4238 * decremented above, we need to serve the bytes from
4239 * `size - generator->capacity - output_length` to
4240 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004241 size_t offset =
4242 generator->ctx.buffer.size - generator->capacity - output_length;
4243 memcpy( output, generator->ctx.buffer.data + offset, output_length );
4244 status = PSA_SUCCESS;
4245 }
4246 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004247#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004248 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004249 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004250 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004251 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
4252 output, output_length );
4253 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004254 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4255 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004256 {
4257 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004258 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004259 output_length );
4260 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004261 else
4262#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004263 {
4264 return( PSA_ERROR_BAD_STATE );
4265 }
4266
4267exit:
4268 if( status != PSA_SUCCESS )
4269 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004270 /* Preserve the algorithm upon errors, but clear all sensitive state.
4271 * This allows us to differentiate between exhausted generators and
4272 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
4273 * generators. */
4274 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004275 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004276 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004277 memset( output, '!', output_length );
4278 }
4279 return( status );
4280}
4281
Gilles Peskine08542d82018-07-19 17:05:42 +02004282#if defined(MBEDTLS_DES_C)
4283static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4284{
4285 if( data_size >= 8 )
4286 mbedtls_des_key_set_parity( data );
4287 if( data_size >= 16 )
4288 mbedtls_des_key_set_parity( data + 8 );
4289 if( data_size >= 24 )
4290 mbedtls_des_key_set_parity( data + 16 );
4291}
4292#endif /* MBEDTLS_DES_C */
4293
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004294static psa_status_t psa_generator_import_key_internal(
4295 psa_key_slot_t *slot,
4296 size_t bits,
4297 psa_crypto_generator_t *generator )
4298{
4299 uint8_t *data = NULL;
4300 size_t bytes = PSA_BITS_TO_BYTES( bits );
4301 psa_status_t status;
4302
4303 if( ! key_type_is_raw_bytes( slot->type ) )
4304 return( PSA_ERROR_INVALID_ARGUMENT );
4305 if( bits % 8 != 0 )
4306 return( PSA_ERROR_INVALID_ARGUMENT );
4307 data = mbedtls_calloc( 1, bytes );
4308 if( data == NULL )
4309 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4310
4311 status = psa_generator_read( generator, data, bytes );
4312 if( status != PSA_SUCCESS )
4313 goto exit;
4314#if defined(MBEDTLS_DES_C)
4315 if( slot->type == PSA_KEY_TYPE_DES )
4316 psa_des_set_key_parity( data, bytes );
4317#endif /* MBEDTLS_DES_C */
4318 status = psa_import_key_into_slot( slot, data, bytes );
4319
4320exit:
4321 mbedtls_free( data );
4322 return( status );
4323}
4324
4325psa_status_t psa_generator_import_key( const psa_key_attributes_t *attributes,
4326 psa_key_handle_t *handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004327 psa_crypto_generator_t *generator )
4328{
4329 psa_status_t status;
4330 psa_key_slot_t *slot = NULL;
4331 status = psa_start_key_creation( attributes, handle, &slot );
4332 if( status == PSA_SUCCESS )
4333 {
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004334 status = psa_generator_import_key_internal( slot,
4335 attributes->bits,
4336 generator );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004337 }
4338 if( status == PSA_SUCCESS )
4339 status = psa_finish_key_creation( slot );
4340 if( status != PSA_SUCCESS )
4341 {
4342 psa_fail_key_creation( slot );
4343 *handle = 0;
4344 }
4345 return( status );
4346}
4347
Gilles Peskine87a5e562019-04-17 12:28:25 +02004348psa_status_t psa_generator_import_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004349 psa_key_type_t type,
4350 size_t bits,
4351 psa_crypto_generator_t *generator )
4352{
4353 uint8_t *data = NULL;
4354 size_t bytes = PSA_BITS_TO_BYTES( bits );
4355 psa_status_t status;
4356
4357 if( ! key_type_is_raw_bytes( type ) )
4358 return( PSA_ERROR_INVALID_ARGUMENT );
4359 if( bits % 8 != 0 )
4360 return( PSA_ERROR_INVALID_ARGUMENT );
4361 data = mbedtls_calloc( 1, bytes );
4362 if( data == NULL )
4363 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4364
4365 status = psa_generator_read( generator, data, bytes );
4366 if( status != PSA_SUCCESS )
4367 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004368#if defined(MBEDTLS_DES_C)
4369 if( type == PSA_KEY_TYPE_DES )
4370 psa_des_set_key_parity( data, bytes );
4371#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004372 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004373
4374exit:
4375 mbedtls_free( data );
4376 return( status );
4377}
4378
4379
4380
4381/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004382/* Key derivation */
4383/****************************************************************/
4384
Gilles Peskinea05219c2018-11-16 16:02:56 +01004385#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004386/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004387 * of the HKDF algorithm.
4388 *
4389 * Note that if this function fails, you must call psa_generator_abort()
4390 * to potentially free embedded data structures and wipe confidential data.
4391 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004392static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004393 const uint8_t *secret,
4394 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004395 psa_algorithm_t hash_alg,
4396 const uint8_t *salt,
4397 size_t salt_length,
4398 const uint8_t *label,
4399 size_t label_length )
4400{
4401 psa_status_t status;
4402 status = psa_hmac_setup_internal( &hkdf->hmac,
4403 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004404 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004405 if( status != PSA_SUCCESS )
4406 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004407 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004408 if( status != PSA_SUCCESS )
4409 return( status );
4410 status = psa_hmac_finish_internal( &hkdf->hmac,
4411 hkdf->prk,
4412 sizeof( hkdf->prk ) );
4413 if( status != PSA_SUCCESS )
4414 return( status );
4415 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4416 hkdf->block_number = 0;
4417 hkdf->info_length = label_length;
4418 if( label_length != 0 )
4419 {
4420 hkdf->info = mbedtls_calloc( 1, label_length );
4421 if( hkdf->info == NULL )
4422 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4423 memcpy( hkdf->info, label, label_length );
4424 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004425 hkdf->state = HKDF_STATE_KEYED;
4426 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004427 return( PSA_SUCCESS );
4428}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004429#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004430
Gilles Peskinea05219c2018-11-16 16:02:56 +01004431#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004432/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4433 *
4434 * Note that if this function fails, you must call psa_generator_abort()
4435 * to potentially free embedded data structures and wipe confidential data.
4436 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004437static psa_status_t psa_generator_tls12_prf_setup(
4438 psa_tls12_prf_generator_t *tls12_prf,
4439 const unsigned char *key,
4440 size_t key_len,
4441 psa_algorithm_t hash_alg,
4442 const uint8_t *salt,
4443 size_t salt_length,
4444 const uint8_t *label,
4445 size_t label_length )
4446{
4447 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004448 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4449 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004450
4451 tls12_prf->key = mbedtls_calloc( 1, key_len );
4452 if( tls12_prf->key == NULL )
4453 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4454 tls12_prf->key_len = key_len;
4455 memcpy( tls12_prf->key, key, key_len );
4456
Hanno Becker580fba12018-11-13 20:50:45 +00004457 overflow = ( salt_length + label_length < salt_length ) ||
4458 ( salt_length + label_length + hash_length < hash_length );
4459 if( overflow )
4460 return( PSA_ERROR_INVALID_ARGUMENT );
4461
4462 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4463 if( tls12_prf->Ai_with_seed == NULL )
4464 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4465 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4466
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004467 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4468 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004469 if( label_length != 0 )
4470 {
4471 memcpy( tls12_prf->Ai_with_seed + hash_length,
4472 label, label_length );
4473 }
4474
4475 if( salt_length != 0 )
4476 {
4477 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4478 salt, salt_length );
4479 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004480
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004481 /* The first block gets generated when
4482 * psa_generator_read() is called. */
4483 tls12_prf->block_number = 0;
4484 tls12_prf->offset_in_block = hash_length;
4485
4486 return( PSA_SUCCESS );
4487}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004488
4489/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4490static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4491 psa_tls12_prf_generator_t *tls12_prf,
4492 const unsigned char *psk,
4493 size_t psk_len,
4494 psa_algorithm_t hash_alg,
4495 const uint8_t *salt,
4496 size_t salt_length,
4497 const uint8_t *label,
4498 size_t label_length )
4499{
4500 psa_status_t status;
4501 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4502
4503 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4504 return( PSA_ERROR_INVALID_ARGUMENT );
4505
4506 /* Quoting RFC 4279, Section 2:
4507 *
4508 * The premaster secret is formed as follows: if the PSK is N octets
4509 * long, concatenate a uint16 with the value N, N zero octets, a second
4510 * uint16 with the value N, and the PSK itself.
4511 */
4512
4513 pms[0] = ( psk_len >> 8 ) & 0xff;
4514 pms[1] = ( psk_len >> 0 ) & 0xff;
4515 memset( pms + 2, 0, psk_len );
4516 pms[2 + psk_len + 0] = pms[0];
4517 pms[2 + psk_len + 1] = pms[1];
4518 memcpy( pms + 4 + psk_len, psk, psk_len );
4519
4520 status = psa_generator_tls12_prf_setup( tls12_prf,
4521 pms, 4 + 2 * psk_len,
4522 hash_alg,
4523 salt, salt_length,
4524 label, label_length );
4525
Gilles Peskine3f108122018-12-07 18:14:53 +01004526 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004527 return( status );
4528}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004529#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004530
Gilles Peskine346797d2018-11-16 16:05:06 +01004531/* Note that if this function fails, you must call psa_generator_abort()
4532 * to potentially free embedded data structures and wipe confidential data.
4533 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004534static psa_status_t psa_key_derivation_internal(
4535 psa_crypto_generator_t *generator,
4536 const uint8_t *secret, size_t secret_length,
4537 psa_algorithm_t alg,
4538 const uint8_t *salt, size_t salt_length,
4539 const uint8_t *label, size_t label_length,
4540 size_t capacity )
4541{
4542 psa_status_t status;
4543 size_t max_capacity;
4544
4545 /* Set generator->alg even on failure so that abort knows what to do. */
4546 generator->alg = alg;
4547
Gilles Peskine751d9652018-09-18 12:05:44 +02004548 if( alg == PSA_ALG_SELECT_RAW )
4549 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004550 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004551 if( salt_length != 0 )
4552 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004553 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004554 if( label_length != 0 )
4555 return( PSA_ERROR_INVALID_ARGUMENT );
4556 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4557 if( generator->ctx.buffer.data == NULL )
4558 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4559 memcpy( generator->ctx.buffer.data, secret, secret_length );
4560 generator->ctx.buffer.size = secret_length;
4561 max_capacity = secret_length;
4562 status = PSA_SUCCESS;
4563 }
4564 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004565#if defined(MBEDTLS_MD_C)
4566 if( PSA_ALG_IS_HKDF( alg ) )
4567 {
4568 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4569 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4570 if( hash_size == 0 )
4571 return( PSA_ERROR_NOT_SUPPORTED );
4572 max_capacity = 255 * hash_size;
4573 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4574 secret, secret_length,
4575 hash_alg,
4576 salt, salt_length,
4577 label, label_length );
4578 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004579 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4580 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4581 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004582 {
4583 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4584 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4585
4586 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4587 if( hash_alg != PSA_ALG_SHA_256 &&
4588 hash_alg != PSA_ALG_SHA_384 )
4589 {
4590 return( PSA_ERROR_NOT_SUPPORTED );
4591 }
4592
4593 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004594
4595 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4596 {
4597 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4598 secret, secret_length,
4599 hash_alg, salt, salt_length,
4600 label, label_length );
4601 }
4602 else
4603 {
4604 status = psa_generator_tls12_psk_to_ms_setup(
4605 &generator->ctx.tls12_prf,
4606 secret, secret_length,
4607 hash_alg, salt, salt_length,
4608 label, label_length );
4609 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004610 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004611 else
4612#endif
4613 {
4614 return( PSA_ERROR_NOT_SUPPORTED );
4615 }
4616
4617 if( status != PSA_SUCCESS )
4618 return( status );
4619
4620 if( capacity <= max_capacity )
4621 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004622 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4623 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004624 else
4625 return( PSA_ERROR_INVALID_ARGUMENT );
4626
4627 return( PSA_SUCCESS );
4628}
4629
Gilles Peskineea0fb492018-07-12 17:17:20 +02004630psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004631 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004632 psa_algorithm_t alg,
4633 const uint8_t *salt,
4634 size_t salt_length,
4635 const uint8_t *label,
4636 size_t label_length,
4637 size_t capacity )
4638{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004639 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004640 psa_status_t status;
4641
4642 if( generator->alg != 0 )
4643 return( PSA_ERROR_BAD_STATE );
4644
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004645 /* Make sure that alg is a key derivation algorithm. This prevents
4646 * key selection algorithms, which psa_key_derivation_internal
4647 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004648 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4649 return( PSA_ERROR_INVALID_ARGUMENT );
4650
Gilles Peskinec5487a82018-12-03 18:08:14 +01004651 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004652 if( status != PSA_SUCCESS )
4653 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004654
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004655 if( slot->type != PSA_KEY_TYPE_DERIVE )
4656 return( PSA_ERROR_INVALID_ARGUMENT );
4657
4658 status = psa_key_derivation_internal( generator,
4659 slot->data.raw.data,
4660 slot->data.raw.bytes,
4661 alg,
4662 salt, salt_length,
4663 label, label_length,
4664 capacity );
4665 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004666 psa_generator_abort( generator );
4667 return( status );
4668}
4669
Gilles Peskine969c5d62019-01-16 15:53:06 +01004670static psa_status_t psa_key_derivation_setup_kdf(
4671 psa_crypto_generator_t *generator,
4672 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004673{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004674 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004675#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004676 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4677 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4678 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004679 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004680 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004681 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4682 if( hash_size == 0 )
4683 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004684 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4685 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004686 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004687 {
4688 return( PSA_ERROR_NOT_SUPPORTED );
4689 }
4690 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004691 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004692 }
4693#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004694 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004695 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004696}
4697
4698psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4699 psa_algorithm_t alg )
4700{
4701 psa_status_t status;
4702
4703 if( generator->alg != 0 )
4704 return( PSA_ERROR_BAD_STATE );
4705
Gilles Peskine6843c292019-01-18 16:44:49 +01004706 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4707 return( PSA_ERROR_INVALID_ARGUMENT );
4708 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004709 {
4710 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004711 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004712 }
4713 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4714 {
4715 status = psa_key_derivation_setup_kdf( generator, alg );
4716 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004717 else
4718 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004719
4720 if( status == PSA_SUCCESS )
4721 generator->alg = alg;
4722 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004723}
4724
4725#if defined(MBEDTLS_MD_C)
4726static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4727 psa_algorithm_t hash_alg,
4728 psa_key_derivation_step_t step,
4729 const uint8_t *data,
4730 size_t data_length )
4731{
4732 psa_status_t status;
4733 switch( step )
4734 {
4735 case PSA_KDF_STEP_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004736 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004737 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004738 status = psa_hmac_setup_internal( &hkdf->hmac,
4739 data, data_length,
4740 hash_alg );
4741 if( status != PSA_SUCCESS )
4742 return( status );
4743 hkdf->state = HKDF_STATE_STARTED;
4744 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004745 case PSA_KDF_STEP_SECRET:
4746 /* If no salt was provided, use an empty salt. */
4747 if( hkdf->state == HKDF_STATE_INIT )
4748 {
4749 status = psa_hmac_setup_internal( &hkdf->hmac,
4750 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004751 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004752 if( status != PSA_SUCCESS )
4753 return( status );
4754 hkdf->state = HKDF_STATE_STARTED;
4755 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004756 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004757 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004758 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4759 data, data_length );
4760 if( status != PSA_SUCCESS )
4761 return( status );
4762 status = psa_hmac_finish_internal( &hkdf->hmac,
4763 hkdf->prk,
4764 sizeof( hkdf->prk ) );
4765 if( status != PSA_SUCCESS )
4766 return( status );
4767 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4768 hkdf->block_number = 0;
4769 hkdf->state = HKDF_STATE_KEYED;
4770 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004771 case PSA_KDF_STEP_INFO:
4772 if( hkdf->state == HKDF_STATE_OUTPUT )
4773 return( PSA_ERROR_BAD_STATE );
4774 if( hkdf->info_set )
4775 return( PSA_ERROR_BAD_STATE );
4776 hkdf->info_length = data_length;
4777 if( data_length != 0 )
4778 {
4779 hkdf->info = mbedtls_calloc( 1, data_length );
4780 if( hkdf->info == NULL )
4781 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4782 memcpy( hkdf->info, data, data_length );
4783 }
4784 hkdf->info_set = 1;
4785 return( PSA_SUCCESS );
4786 default:
4787 return( PSA_ERROR_INVALID_ARGUMENT );
4788 }
4789}
4790#endif /* MBEDTLS_MD_C */
4791
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004792static psa_status_t psa_key_derivation_input_raw(
4793 psa_crypto_generator_t *generator,
4794 psa_key_derivation_step_t step,
4795 const uint8_t *data,
4796 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004797{
4798 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004799 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004800
Gilles Peskine969c5d62019-01-16 15:53:06 +01004801 if( kdf_alg == PSA_ALG_SELECT_RAW )
4802 {
4803 if( generator->capacity != 0 )
4804 return( PSA_ERROR_INVALID_ARGUMENT );
4805 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4806 if( generator->ctx.buffer.data == NULL )
4807 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4808 memcpy( generator->ctx.buffer.data, data, data_length );
4809 generator->ctx.buffer.size = data_length;
4810 generator->capacity = data_length;
4811 status = PSA_SUCCESS;
4812 }
4813 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004814#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004815 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004816 {
4817 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004818 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004819 step, data, data_length );
4820 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004821 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004822#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004823#if defined(MBEDTLS_MD_C)
4824 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004825 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4826 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004827 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004828 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004829 status = PSA_ERROR_NOT_SUPPORTED;
4830 }
4831 else
4832#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004833 {
4834 /* This can't happen unless the generator object was not initialized */
4835 return( PSA_ERROR_BAD_STATE );
4836 }
4837
4838 if( status != PSA_SUCCESS )
4839 psa_generator_abort( generator );
4840 return( status );
4841}
4842
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004843psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4844 psa_key_derivation_step_t step,
4845 const uint8_t *data,
4846 size_t data_length )
4847{
4848 switch( step )
4849 {
4850 case PSA_KDF_STEP_LABEL:
4851 case PSA_KDF_STEP_SALT:
4852 case PSA_KDF_STEP_INFO:
4853 return( psa_key_derivation_input_raw( generator, step,
4854 data, data_length ) );
4855 default:
4856 return( PSA_ERROR_INVALID_ARGUMENT );
4857 }
4858}
4859
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004860psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4861 psa_key_derivation_step_t step,
4862 psa_key_handle_t handle )
4863{
4864 psa_key_slot_t *slot;
4865 psa_status_t status;
4866 status = psa_get_key_from_slot( handle, &slot,
4867 PSA_KEY_USAGE_DERIVE,
4868 generator->alg );
4869 if( status != PSA_SUCCESS )
4870 return( status );
4871 if( slot->type != PSA_KEY_TYPE_DERIVE )
4872 return( PSA_ERROR_INVALID_ARGUMENT );
4873 /* Don't allow a key to be used as an input that is usually public.
4874 * This is debatable. It's ok from a cryptographic perspective to
4875 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004876 * the material should be dedicated to a particular input step,
4877 * otherwise this may allow the key to be used in an unintended way
4878 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004879 if( step != PSA_KDF_STEP_SECRET )
4880 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004881 return( psa_key_derivation_input_raw( generator,
4882 step,
4883 slot->data.raw.data,
4884 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004885}
4886
Gilles Peskineea0fb492018-07-12 17:17:20 +02004887
4888
4889/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004890/* Key agreement */
4891/****************************************************************/
4892
Gilles Peskinea05219c2018-11-16 16:02:56 +01004893#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004894static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4895 size_t peer_key_length,
4896 const mbedtls_ecp_keypair *our_key,
4897 uint8_t *shared_secret,
4898 size_t shared_secret_size,
4899 size_t *shared_secret_length )
4900{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004901 mbedtls_ecp_keypair *their_key = NULL;
4902 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004903 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004904 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004905
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004906 status = psa_import_ec_public_key(
4907 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4908 peer_key, peer_key_length,
4909 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004910 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004911 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004912
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004913 status = mbedtls_to_psa_error(
4914 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4915 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004916 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004917 status = mbedtls_to_psa_error(
4918 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4919 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004920 goto exit;
4921
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004922 status = mbedtls_to_psa_error(
4923 mbedtls_ecdh_calc_secret( &ecdh,
4924 shared_secret_length,
4925 shared_secret, shared_secret_size,
4926 mbedtls_ctr_drbg_random,
4927 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004928
4929exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004930 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004931 mbedtls_ecp_keypair_free( their_key );
4932 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004933 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004934}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004935#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004936
Gilles Peskine01d718c2018-09-18 12:01:02 +02004937#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4938
Gilles Peskine0216fe12019-04-11 21:23:21 +02004939static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4940 psa_key_slot_t *private_key,
4941 const uint8_t *peer_key,
4942 size_t peer_key_length,
4943 uint8_t *shared_secret,
4944 size_t shared_secret_size,
4945 size_t *shared_secret_length )
4946{
4947 switch( alg )
4948 {
4949#if defined(MBEDTLS_ECDH_C)
4950 case PSA_ALG_ECDH:
4951 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4952 return( PSA_ERROR_INVALID_ARGUMENT );
4953 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
4954 private_key->data.ecp,
4955 shared_secret, shared_secret_size,
4956 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004957#endif /* MBEDTLS_ECDH_C */
4958 default:
4959 (void) private_key;
4960 (void) peer_key;
4961 (void) peer_key_length;
4962 (void) shared_secret;
4963 (void) shared_secret_size;
4964 (void) shared_secret_length;
4965 return( PSA_ERROR_NOT_SUPPORTED );
4966 }
4967}
4968
Gilles Peskine346797d2018-11-16 16:05:06 +01004969/* Note that if this function fails, you must call psa_generator_abort()
4970 * to potentially free embedded data structures and wipe confidential data.
4971 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004972static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004973 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004974 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004975 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004976 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004977{
4978 psa_status_t status;
4979 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4980 size_t shared_secret_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004981 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004982
4983 /* Step 1: run the secret agreement algorithm to generate the shared
4984 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004985 status = psa_key_agreement_raw_internal( ka_alg,
4986 private_key,
4987 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004988 shared_secret,
4989 sizeof( shared_secret ),
4990 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004991 if( status != PSA_SUCCESS )
4992 goto exit;
4993
4994 /* Step 2: set up the key derivation to generate key material from
4995 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004996 status = psa_key_derivation_input_raw( generator, step,
4997 shared_secret, shared_secret_length );
4998
Gilles Peskine01d718c2018-09-18 12:01:02 +02004999exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005000 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005001 return( status );
5002}
5003
5004psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005005 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005006 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005007 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005008 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005009{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005010 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005011 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005012 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005013 return( PSA_ERROR_INVALID_ARGUMENT );
5014 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005015 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005016 if( status != PSA_SUCCESS )
5017 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005018 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005019 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005020 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005021 if( status != PSA_SUCCESS )
5022 psa_generator_abort( generator );
5023 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005024}
5025
Gilles Peskine0216fe12019-04-11 21:23:21 +02005026psa_status_t psa_key_agreement_raw_shared_secret( psa_algorithm_t alg,
5027 psa_key_handle_t private_key,
5028 const uint8_t *peer_key,
5029 size_t peer_key_length,
5030 uint8_t *output,
5031 size_t output_size,
5032 size_t *output_length )
5033{
5034 psa_key_slot_t *slot;
5035 psa_status_t status;
5036
5037 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5038 {
5039 status = PSA_ERROR_INVALID_ARGUMENT;
5040 goto exit;
5041 }
5042 status = psa_get_key_from_slot( private_key, &slot,
5043 PSA_KEY_USAGE_DERIVE, alg );
5044 if( status != PSA_SUCCESS )
5045 goto exit;
5046
5047 status = psa_key_agreement_raw_internal( alg, slot,
5048 peer_key, peer_key_length,
5049 output, output_size,
5050 output_length );
5051
5052exit:
5053 if( status != PSA_SUCCESS )
5054 {
5055 /* If an error happens and is not handled properly, the output
5056 * may be used as a key to protect sensitive data. Arrange for such
5057 * a key to be random, which is likely to result in decryption or
5058 * verification errors. This is better than filling the buffer with
5059 * some constant data such as zeros, which would result in the data
5060 * being protected with a reproducible, easily knowable key.
5061 */
5062 psa_generate_random( output, output_size );
5063 *output_length = output_size;
5064 }
5065 return( status );
5066}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005067
5068
5069/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005070/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005071/****************************************************************/
5072
5073psa_status_t psa_generate_random( uint8_t *output,
5074 size_t output_size )
5075{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005076 int ret;
5077 GUARD_MODULE_INITIALIZED;
5078
5079 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005080 return( mbedtls_to_psa_error( ret ) );
5081}
5082
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005083#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5084#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005085
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005086psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5087 size_t seed_size )
5088{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005089 if( global_data.initialized )
5090 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005091
5092 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5093 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5094 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5095 return( PSA_ERROR_INVALID_ARGUMENT );
5096
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005097 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005098}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005099#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005100
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005101static psa_status_t psa_generate_key_internal( psa_key_slot_t *slot,
5102 size_t bits,
5103 const void *extra,
5104 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005105{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005106 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005107
Gilles Peskine53d991e2018-07-12 01:14:59 +02005108 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005109 return( PSA_ERROR_INVALID_ARGUMENT );
5110
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005111 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005112 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005113 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005114 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005115 if( status != PSA_SUCCESS )
5116 return( status );
5117 status = psa_generate_random( slot->data.raw.data,
5118 slot->data.raw.bytes );
5119 if( status != PSA_SUCCESS )
5120 {
5121 mbedtls_free( slot->data.raw.data );
5122 return( status );
5123 }
5124#if defined(MBEDTLS_DES_C)
5125 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005126 psa_des_set_key_parity( slot->data.raw.data,
5127 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005128#endif /* MBEDTLS_DES_C */
5129 }
5130 else
5131
5132#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5133 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
5134 {
5135 mbedtls_rsa_context *rsa;
5136 int ret;
5137 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005138 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5139 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005140 /* Accept only byte-aligned keys, for the same reasons as
5141 * in psa_import_rsa_key(). */
5142 if( bits % 8 != 0 )
5143 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005144 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005145 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02005146 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02005147 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005148 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02005149#if INT_MAX < 0xffffffff
5150 /* Check that the uint32_t value passed by the caller fits
5151 * in the range supported by this implementation. */
5152 if( p->e > INT_MAX )
5153 return( PSA_ERROR_NOT_SUPPORTED );
5154#endif
5155 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005156 }
5157 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5158 if( rsa == NULL )
5159 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5160 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5161 ret = mbedtls_rsa_gen_key( rsa,
5162 mbedtls_ctr_drbg_random,
5163 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005164 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005165 exponent );
5166 if( ret != 0 )
5167 {
5168 mbedtls_rsa_free( rsa );
5169 mbedtls_free( rsa );
5170 return( mbedtls_to_psa_error( ret ) );
5171 }
5172 slot->data.rsa = rsa;
5173 }
5174 else
5175#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5176
5177#if defined(MBEDTLS_ECP_C)
5178 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
5179 {
5180 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5181 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5182 const mbedtls_ecp_curve_info *curve_info =
5183 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5184 mbedtls_ecp_keypair *ecp;
5185 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02005186 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005187 return( PSA_ERROR_NOT_SUPPORTED );
5188 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5189 return( PSA_ERROR_NOT_SUPPORTED );
5190 if( curve_info->bit_size != bits )
5191 return( PSA_ERROR_INVALID_ARGUMENT );
5192 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5193 if( ecp == NULL )
5194 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5195 mbedtls_ecp_keypair_init( ecp );
5196 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5197 mbedtls_ctr_drbg_random,
5198 &global_data.ctr_drbg );
5199 if( ret != 0 )
5200 {
5201 mbedtls_ecp_keypair_free( ecp );
5202 mbedtls_free( ecp );
5203 return( mbedtls_to_psa_error( ret ) );
5204 }
5205 slot->data.ecp = ecp;
5206 }
5207 else
5208#endif /* MBEDTLS_ECP_C */
5209
5210 return( PSA_ERROR_NOT_SUPPORTED );
5211
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005212 return( PSA_SUCCESS );
5213}
5214
5215psa_status_t psa_generate_key_to_handle( psa_key_handle_t handle,
5216 psa_key_type_t type,
5217 size_t bits,
5218 const void *extra,
5219 size_t extra_size )
5220{
5221 psa_key_slot_t *slot;
5222 psa_status_t status;
5223
5224 status = psa_get_empty_key_slot( handle, &slot );
5225 if( status != PSA_SUCCESS )
5226 return( status );
5227
Gilles Peskine12313cd2018-06-20 00:20:32 +02005228 slot->type = type;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005229 status = psa_generate_key_internal( slot, bits, extra, extra_size );
5230 if( status != PSA_SUCCESS )
5231 slot->type = 0;
Darryl Green0c6575a2018-11-07 16:05:30 +00005232
5233#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
5234 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
5235 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01005236 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005237 }
5238#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5239
5240 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005241}
5242
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005243psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
5244 psa_key_handle_t *handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005245 const void *extra,
5246 size_t extra_size )
5247{
5248 psa_status_t status;
5249 psa_key_slot_t *slot = NULL;
5250 status = psa_start_key_creation( attributes, handle, &slot );
5251 if( status == PSA_SUCCESS )
5252 {
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005253 status = psa_generate_key_internal( slot, attributes->bits,
5254 extra, extra_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005255 }
5256 if( status == PSA_SUCCESS )
5257 status = psa_finish_key_creation( slot );
5258 if( status != PSA_SUCCESS )
5259 {
5260 psa_fail_key_creation( slot );
5261 *handle = 0;
5262 }
5263 return( status );
5264}
5265
5266
Gilles Peskine05d69892018-06-19 22:00:52 +02005267
5268/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005269/* Module setup */
5270/****************************************************************/
5271
Gilles Peskine5e769522018-11-20 21:59:56 +01005272psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5273 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5274 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5275{
5276 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5277 return( PSA_ERROR_BAD_STATE );
5278 global_data.entropy_init = entropy_init;
5279 global_data.entropy_free = entropy_free;
5280 return( PSA_SUCCESS );
5281}
5282
Gilles Peskinee59236f2018-01-27 23:32:46 +01005283void mbedtls_psa_crypto_free( void )
5284{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005285 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005286 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5287 {
5288 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005289 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005290 }
5291 /* Wipe all remaining data, including configuration.
5292 * In particular, this sets all state indicator to the value
5293 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005294 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005295}
5296
5297psa_status_t psa_crypto_init( void )
5298{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005299 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005300 const unsigned char drbg_seed[] = "PSA";
5301
Gilles Peskinec6b69072018-11-20 21:42:52 +01005302 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005303 if( global_data.initialized != 0 )
5304 return( PSA_SUCCESS );
5305
Gilles Peskine5e769522018-11-20 21:59:56 +01005306 /* Set default configuration if
5307 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5308 if( global_data.entropy_init == NULL )
5309 global_data.entropy_init = mbedtls_entropy_init;
5310 if( global_data.entropy_free == NULL )
5311 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005312
Gilles Peskinec6b69072018-11-20 21:42:52 +01005313 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005314 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005315 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005316 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005317 status = mbedtls_to_psa_error(
5318 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5319 mbedtls_entropy_func,
5320 &global_data.entropy,
5321 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5322 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005323 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005324 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005325
Gilles Peskine66fb1262018-12-10 16:29:04 +01005326 status = psa_initialize_key_slots( );
5327 if( status != PSA_SUCCESS )
5328 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005329
5330 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005331 global_data.initialized = 1;
5332
Gilles Peskinee59236f2018-01-27 23:32:46 +01005333exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005334 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005335 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005336 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005337}
5338
5339#endif /* MBEDTLS_PSA_CRYPTO_C */