blob: fba1936475aa2a1fc67edfc75240ec2a4778e405 [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;
1047
1048 buflen = mbedtls_mpi_size( &mpi );
1049 buffer = mbedtls_calloc( 1, buflen );
1050 if( buffer == NULL )
1051 {
1052 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1053 goto exit;
1054 }
1055 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1056 if( ret != 0 )
1057 goto exit;
1058 attributes->domain_parameters = buffer;
1059 attributes->domain_parameters_size = buflen;
1060
1061exit:
1062 mbedtls_mpi_free( &mpi );
1063 if( ret != 0 )
1064 mbedtls_free( buffer );
1065 return( mbedtls_to_psa_error( ret ) );
1066}
1067#endif /* MBEDTLS_RSA_C */
1068
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001069psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1070 psa_key_attributes_t *attributes )
1071{
1072 psa_key_slot_t *slot;
1073 psa_status_t status;
1074
1075 psa_reset_key_attributes( attributes );
1076
1077 status = psa_get_key_slot( handle, &slot );
1078 if( status != PSA_SUCCESS )
1079 return( status );
1080
1081 attributes->id = slot->persistent_storage_id;
1082 attributes->lifetime = slot->lifetime;
1083 attributes->policy = slot->policy;
1084 attributes->type = slot->type;
1085 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001086
1087 switch( slot->type )
1088 {
1089#if defined(MBEDTLS_RSA_C)
1090 case PSA_KEY_TYPE_RSA_KEYPAIR:
1091 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1092 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1093 break;
1094#endif
1095 default:
1096 /* Nothing else to do. */
1097 break;
1098 }
1099
1100 if( status != PSA_SUCCESS )
1101 psa_reset_key_attributes( attributes );
1102 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001103}
1104
Gilles Peskinec5487a82018-12-03 18:08:14 +01001105psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001106 psa_key_type_t *type,
1107 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001108{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001109 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001110 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001111
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001112 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001113 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001114 if( bits != NULL )
1115 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001116 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001117 if( status != PSA_SUCCESS )
1118 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001119
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001120 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001121 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001122 if( type != NULL )
1123 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001124 if( bits != NULL )
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001125 *bits = psa_get_key_slot_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001126 return( PSA_SUCCESS );
1127}
1128
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001129#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001130static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1131 unsigned char *buf, size_t size )
1132{
1133 int ret;
1134 unsigned char *c;
1135 size_t len = 0;
1136
1137 c = buf + size;
1138
1139 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1140
1141 return( (int) len );
1142}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001143#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001144
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001145static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1146 uint8_t *data,
1147 size_t data_size,
1148 size_t *data_length,
1149 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001150{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001151 *data_length = 0;
1152
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001153 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001154 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001155
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001156 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001157 {
1158 if( slot->data.raw.bytes > data_size )
1159 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001160 if( data_size != 0 )
1161 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001162 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001163 memset( data + slot->data.raw.bytes, 0,
1164 data_size - slot->data.raw.bytes );
1165 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001166 *data_length = slot->data.raw.bytes;
1167 return( PSA_SUCCESS );
1168 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001169#if defined(MBEDTLS_ECP_C)
1170 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1171 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001172 psa_status_t status;
1173
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001174 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001175 if( bytes > data_size )
1176 return( PSA_ERROR_BUFFER_TOO_SMALL );
1177 status = mbedtls_to_psa_error(
1178 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1179 if( status != PSA_SUCCESS )
1180 return( status );
1181 memset( data + bytes, 0, data_size - bytes );
1182 *data_length = bytes;
1183 return( PSA_SUCCESS );
1184 }
1185#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001186 else
Moran Peker17e36e12018-05-02 12:55:20 +03001187 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001188#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001189 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001190 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001191 {
Moran Pekera998bc62018-04-16 18:16:20 +03001192 mbedtls_pk_context pk;
1193 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001194 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001195 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001196#if defined(MBEDTLS_RSA_C)
1197 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001198 pk.pk_info = &mbedtls_rsa_info;
1199 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001200#else
1201 return( PSA_ERROR_NOT_SUPPORTED );
1202#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001203 }
1204 else
1205 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001206#if defined(MBEDTLS_ECP_C)
1207 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001208 pk.pk_info = &mbedtls_eckey_info;
1209 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001210#else
1211 return( PSA_ERROR_NOT_SUPPORTED );
1212#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001213 }
Moran Pekerd7326592018-05-29 16:56:39 +03001214 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001215 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001216 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001217 }
Moran Peker17e36e12018-05-02 12:55:20 +03001218 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001219 {
Moran Peker17e36e12018-05-02 12:55:20 +03001220 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001221 }
Moran Peker60364322018-04-29 11:34:58 +03001222 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001223 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001224 /* If data_size is 0 then data may be NULL and then the
1225 * call to memset would have undefined behavior. */
1226 if( data_size != 0 )
1227 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001228 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001229 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001230 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1231 * Move the data to the beginning and erase remaining data
1232 * at the original location. */
1233 if( 2 * (size_t) ret <= data_size )
1234 {
1235 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001236 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001237 }
1238 else if( (size_t) ret < data_size )
1239 {
1240 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001241 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001242 }
Moran Pekera998bc62018-04-16 18:16:20 +03001243 *data_length = ret;
1244 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001245 }
1246 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001247#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001248 {
1249 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001250 it is valid for a special-purpose implementation to omit
1251 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001252 return( PSA_ERROR_NOT_SUPPORTED );
1253 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254 }
1255}
1256
Gilles Peskinec5487a82018-12-03 18:08:14 +01001257psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001258 uint8_t *data,
1259 size_t data_size,
1260 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001261{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001262 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001263 psa_status_t status;
1264
1265 /* Set the key to empty now, so that even when there are errors, we always
1266 * set data_length to a value between 0 and data_size. On error, setting
1267 * the key to empty is a good choice because an empty key representation is
1268 * unlikely to be accepted anywhere. */
1269 *data_length = 0;
1270
1271 /* Export requires the EXPORT flag. There is an exception for public keys,
1272 * which don't require any flag, but psa_get_key_from_slot takes
1273 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001274 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001275 if( status != PSA_SUCCESS )
1276 return( status );
1277 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001278 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001279}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280
Gilles Peskinec5487a82018-12-03 18:08:14 +01001281psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001282 uint8_t *data,
1283 size_t data_size,
1284 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001285{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001286 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001287 psa_status_t status;
1288
1289 /* Set the key to empty now, so that even when there are errors, we always
1290 * set data_length to a value between 0 and data_size. On error, setting
1291 * the key to empty is a good choice because an empty key representation is
1292 * unlikely to be accepted anywhere. */
1293 *data_length = 0;
1294
1295 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001296 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001297 if( status != PSA_SUCCESS )
1298 return( status );
1299 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001300 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001301}
1302
Darryl Green0c6575a2018-11-07 16:05:30 +00001303#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001304static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001305 size_t bits )
1306{
1307 psa_status_t status;
1308 uint8_t *data;
1309 size_t key_length;
1310 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1311 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001312 if( data == NULL )
1313 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001314 /* Get key data in export format */
1315 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1316 if( status != PSA_SUCCESS )
1317 {
1318 slot->type = PSA_KEY_TYPE_NONE;
1319 goto exit;
1320 }
1321 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001322 status = psa_save_persistent_key( slot->persistent_storage_id,
1323 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001324 data, key_length );
1325 if( status != PSA_SUCCESS )
1326 {
1327 slot->type = PSA_KEY_TYPE_NONE;
1328 }
1329exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001330 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001331 mbedtls_free( data );
1332 return( status );
1333}
1334#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1335
Gilles Peskine4747d192019-04-17 15:05:45 +02001336static psa_status_t psa_set_key_policy_internal(
1337 psa_key_slot_t *slot,
1338 const psa_key_policy_t *policy )
1339{
1340 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
1341 PSA_KEY_USAGE_ENCRYPT |
1342 PSA_KEY_USAGE_DECRYPT |
1343 PSA_KEY_USAGE_SIGN |
1344 PSA_KEY_USAGE_VERIFY |
1345 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1346 return( PSA_ERROR_INVALID_ARGUMENT );
1347
1348 slot->policy = *policy;
1349 return( PSA_SUCCESS );
1350}
1351
1352/** Prepare a key slot to receive key material.
1353 *
1354 * This function allocates a key slot and sets its metadata.
1355 *
1356 * If this function fails, call psa_fail_key_creation().
1357 *
1358 * \param attributes Key attributes for the new key.
1359 * \param handle On success, the allocated handle.
1360 * \param p_slot On success, a pointer to the prepared slot.
1361 */
1362static psa_status_t psa_start_key_creation(
1363 const psa_key_attributes_t *attributes,
1364 psa_key_handle_t *handle,
1365 psa_key_slot_t **p_slot )
1366{
1367 psa_status_t status;
1368 psa_key_slot_t *slot;
1369
1370 status = psa_allocate_key( handle );
1371 if( status != PSA_SUCCESS )
1372 return( status );
1373 status = psa_get_key_slot( *handle, p_slot );
1374 if( status != PSA_SUCCESS )
1375 return( status );
1376 slot = *p_slot;
1377
1378 status = psa_set_key_policy_internal( slot, &attributes->policy );
1379 if( status != PSA_SUCCESS )
1380 return( status );
1381 slot->lifetime = attributes->lifetime;
1382 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001383 {
1384 status = psa_validate_persistent_key_parameters( attributes->lifetime,
1385 attributes->id );
1386 if( status != PSA_SUCCESS )
1387 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001388 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001389 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001390 slot->type = attributes->type;
1391
1392 return( status );
1393}
1394
1395/** Finalize the creation of a key once its key material has been set.
1396 *
1397 * This entails writing the key to persistent storage.
1398 *
1399 * If this function fails, call psa_fail_key_creation().
1400 *
1401 * \param slot Pointer to the slot with key material.
1402 */
1403static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1404{
1405 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001406 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001407
1408#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1409 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1410 {
1411 uint8_t *buffer = NULL;
1412 size_t buffer_size = 0;
1413 size_t length;
1414
1415 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001416 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001417 buffer = mbedtls_calloc( 1, buffer_size );
1418 if( buffer == NULL && buffer_size != 0 )
1419 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1420 status = psa_internal_export_key( slot,
1421 buffer, buffer_size, &length,
1422 0 );
1423
1424 if( status == PSA_SUCCESS )
1425 {
1426 status = psa_save_persistent_key( slot->persistent_storage_id,
1427 slot->type, &slot->policy,
1428 buffer, length );
1429 }
1430
1431 if( buffer_size != 0 )
1432 mbedtls_platform_zeroize( buffer, buffer_size );
1433 mbedtls_free( buffer );
1434 }
1435#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1436
1437 return( status );
1438}
1439
1440/** Abort the creation of a key.
1441 *
1442 * You may call this function after calling psa_start_key_creation(),
1443 * or after psa_finish_key_creation() fails. In other circumstances, this
1444 * function may not clean up persistent storage.
1445 *
1446 * \param slot Pointer to the slot with key material.
1447 */
1448static void psa_fail_key_creation( psa_key_slot_t *slot )
1449{
1450 if( slot == NULL )
1451 return;
1452 psa_wipe_key_slot( slot );
1453}
1454
1455psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
1456 psa_key_handle_t *handle,
1457 const uint8_t *data,
1458 size_t data_length )
1459{
1460 psa_status_t status;
1461 psa_key_slot_t *slot = NULL;
1462 status = psa_start_key_creation( attributes, handle, &slot );
1463 if( status == PSA_SUCCESS )
1464 {
1465 status = psa_import_key_into_slot( slot, data, data_length );
1466 }
1467 if( status == PSA_SUCCESS )
1468 status = psa_finish_key_creation( slot );
1469 if( status != PSA_SUCCESS )
1470 {
1471 psa_fail_key_creation( slot );
1472 *handle = 0;
1473 }
1474 return( status );
1475}
1476
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001477static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001478 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001479{
1480 psa_status_t status;
1481 uint8_t *buffer = NULL;
1482 size_t buffer_size = 0;
1483 size_t length;
1484
1485 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001486 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001487 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001488 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001489 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001490 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1491 if( status != PSA_SUCCESS )
1492 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001493 target->type = source->type;
1494 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001495
1496exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001497 if( buffer_size != 0 )
1498 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001499 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001500 return( status );
1501}
1502
Gilles Peskine87a5e562019-04-17 12:28:25 +02001503psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001504 psa_key_handle_t target_handle,
1505 const psa_key_policy_t *constraint)
1506{
1507 psa_key_slot_t *source_slot = NULL;
1508 psa_key_slot_t *target_slot = NULL;
1509 psa_key_policy_t new_policy;
1510 psa_status_t status;
1511 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1512 if( status != PSA_SUCCESS )
1513 return( status );
1514 status = psa_get_empty_key_slot( target_handle, &target_slot );
1515 if( status != PSA_SUCCESS )
1516 return( status );
1517
1518 new_policy = target_slot->policy;
1519 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1520 if( status != PSA_SUCCESS )
1521 return( status );
1522 if( constraint != NULL )
1523 {
1524 status = psa_restrict_key_policy( &new_policy, constraint );
1525 if( status != PSA_SUCCESS )
1526 return( status );
1527 }
1528
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001529 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001530 if( status != PSA_SUCCESS )
1531 return( status );
1532
1533 target_slot->policy = new_policy;
1534 return( PSA_SUCCESS );
1535}
1536
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001537psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1538 const psa_key_attributes_t *specified_attributes,
1539 psa_key_handle_t *target_handle )
1540{
1541 psa_status_t status;
1542 psa_key_slot_t *source_slot = NULL;
1543 psa_key_slot_t *target_slot = NULL;
1544 psa_key_attributes_t actual_attributes = *specified_attributes;
1545
1546 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1547 if( status != PSA_SUCCESS )
1548 goto exit;
1549
1550 status = psa_restrict_key_policy( &actual_attributes.policy,
1551 &source_slot->policy );
1552 if( status != PSA_SUCCESS )
1553 goto exit;
1554
1555 status = psa_start_key_creation( &actual_attributes,
1556 target_handle, &target_slot );
1557 if( status != PSA_SUCCESS )
1558 goto exit;
1559
1560 status = psa_copy_key_material( source_slot, target_slot );
1561
1562exit:
1563 if( status == PSA_SUCCESS )
1564 status = psa_finish_key_creation( target_slot );
1565 if( status != PSA_SUCCESS )
1566 {
1567 psa_fail_key_creation( target_slot );
1568 *target_handle = 0;
1569 }
1570 return( status );
1571}
1572
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001573
1574
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001575/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001576/* Message digests */
1577/****************************************************************/
1578
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001579static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001580{
1581 switch( alg )
1582 {
1583#if defined(MBEDTLS_MD2_C)
1584 case PSA_ALG_MD2:
1585 return( &mbedtls_md2_info );
1586#endif
1587#if defined(MBEDTLS_MD4_C)
1588 case PSA_ALG_MD4:
1589 return( &mbedtls_md4_info );
1590#endif
1591#if defined(MBEDTLS_MD5_C)
1592 case PSA_ALG_MD5:
1593 return( &mbedtls_md5_info );
1594#endif
1595#if defined(MBEDTLS_RIPEMD160_C)
1596 case PSA_ALG_RIPEMD160:
1597 return( &mbedtls_ripemd160_info );
1598#endif
1599#if defined(MBEDTLS_SHA1_C)
1600 case PSA_ALG_SHA_1:
1601 return( &mbedtls_sha1_info );
1602#endif
1603#if defined(MBEDTLS_SHA256_C)
1604 case PSA_ALG_SHA_224:
1605 return( &mbedtls_sha224_info );
1606 case PSA_ALG_SHA_256:
1607 return( &mbedtls_sha256_info );
1608#endif
1609#if defined(MBEDTLS_SHA512_C)
1610 case PSA_ALG_SHA_384:
1611 return( &mbedtls_sha384_info );
1612 case PSA_ALG_SHA_512:
1613 return( &mbedtls_sha512_info );
1614#endif
1615 default:
1616 return( NULL );
1617 }
1618}
1619
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001620psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1621{
1622 switch( operation->alg )
1623 {
Gilles Peskine81736312018-06-26 15:04:31 +02001624 case 0:
1625 /* The object has (apparently) been initialized but it is not
1626 * in use. It's ok to call abort on such an object, and there's
1627 * nothing to do. */
1628 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001629#if defined(MBEDTLS_MD2_C)
1630 case PSA_ALG_MD2:
1631 mbedtls_md2_free( &operation->ctx.md2 );
1632 break;
1633#endif
1634#if defined(MBEDTLS_MD4_C)
1635 case PSA_ALG_MD4:
1636 mbedtls_md4_free( &operation->ctx.md4 );
1637 break;
1638#endif
1639#if defined(MBEDTLS_MD5_C)
1640 case PSA_ALG_MD5:
1641 mbedtls_md5_free( &operation->ctx.md5 );
1642 break;
1643#endif
1644#if defined(MBEDTLS_RIPEMD160_C)
1645 case PSA_ALG_RIPEMD160:
1646 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1647 break;
1648#endif
1649#if defined(MBEDTLS_SHA1_C)
1650 case PSA_ALG_SHA_1:
1651 mbedtls_sha1_free( &operation->ctx.sha1 );
1652 break;
1653#endif
1654#if defined(MBEDTLS_SHA256_C)
1655 case PSA_ALG_SHA_224:
1656 case PSA_ALG_SHA_256:
1657 mbedtls_sha256_free( &operation->ctx.sha256 );
1658 break;
1659#endif
1660#if defined(MBEDTLS_SHA512_C)
1661 case PSA_ALG_SHA_384:
1662 case PSA_ALG_SHA_512:
1663 mbedtls_sha512_free( &operation->ctx.sha512 );
1664 break;
1665#endif
1666 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001667 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001668 }
1669 operation->alg = 0;
1670 return( PSA_SUCCESS );
1671}
1672
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001673psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001674 psa_algorithm_t alg )
1675{
1676 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001677
1678 /* A context must be freshly initialized before it can be set up. */
1679 if( operation->alg != 0 )
1680 {
1681 return( PSA_ERROR_BAD_STATE );
1682 }
1683
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001684 switch( alg )
1685 {
1686#if defined(MBEDTLS_MD2_C)
1687 case PSA_ALG_MD2:
1688 mbedtls_md2_init( &operation->ctx.md2 );
1689 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1690 break;
1691#endif
1692#if defined(MBEDTLS_MD4_C)
1693 case PSA_ALG_MD4:
1694 mbedtls_md4_init( &operation->ctx.md4 );
1695 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1696 break;
1697#endif
1698#if defined(MBEDTLS_MD5_C)
1699 case PSA_ALG_MD5:
1700 mbedtls_md5_init( &operation->ctx.md5 );
1701 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1702 break;
1703#endif
1704#if defined(MBEDTLS_RIPEMD160_C)
1705 case PSA_ALG_RIPEMD160:
1706 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1707 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1708 break;
1709#endif
1710#if defined(MBEDTLS_SHA1_C)
1711 case PSA_ALG_SHA_1:
1712 mbedtls_sha1_init( &operation->ctx.sha1 );
1713 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1714 break;
1715#endif
1716#if defined(MBEDTLS_SHA256_C)
1717 case PSA_ALG_SHA_224:
1718 mbedtls_sha256_init( &operation->ctx.sha256 );
1719 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1720 break;
1721 case PSA_ALG_SHA_256:
1722 mbedtls_sha256_init( &operation->ctx.sha256 );
1723 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1724 break;
1725#endif
1726#if defined(MBEDTLS_SHA512_C)
1727 case PSA_ALG_SHA_384:
1728 mbedtls_sha512_init( &operation->ctx.sha512 );
1729 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1730 break;
1731 case PSA_ALG_SHA_512:
1732 mbedtls_sha512_init( &operation->ctx.sha512 );
1733 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1734 break;
1735#endif
1736 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001737 return( PSA_ALG_IS_HASH( alg ) ?
1738 PSA_ERROR_NOT_SUPPORTED :
1739 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001740 }
1741 if( ret == 0 )
1742 operation->alg = alg;
1743 else
1744 psa_hash_abort( operation );
1745 return( mbedtls_to_psa_error( ret ) );
1746}
1747
1748psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1749 const uint8_t *input,
1750 size_t input_length )
1751{
1752 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001753
1754 /* Don't require hash implementations to behave correctly on a
1755 * zero-length input, which may have an invalid pointer. */
1756 if( input_length == 0 )
1757 return( PSA_SUCCESS );
1758
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001759 switch( operation->alg )
1760 {
1761#if defined(MBEDTLS_MD2_C)
1762 case PSA_ALG_MD2:
1763 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1764 input, input_length );
1765 break;
1766#endif
1767#if defined(MBEDTLS_MD4_C)
1768 case PSA_ALG_MD4:
1769 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1770 input, input_length );
1771 break;
1772#endif
1773#if defined(MBEDTLS_MD5_C)
1774 case PSA_ALG_MD5:
1775 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1776 input, input_length );
1777 break;
1778#endif
1779#if defined(MBEDTLS_RIPEMD160_C)
1780 case PSA_ALG_RIPEMD160:
1781 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1782 input, input_length );
1783 break;
1784#endif
1785#if defined(MBEDTLS_SHA1_C)
1786 case PSA_ALG_SHA_1:
1787 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1788 input, input_length );
1789 break;
1790#endif
1791#if defined(MBEDTLS_SHA256_C)
1792 case PSA_ALG_SHA_224:
1793 case PSA_ALG_SHA_256:
1794 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1795 input, input_length );
1796 break;
1797#endif
1798#if defined(MBEDTLS_SHA512_C)
1799 case PSA_ALG_SHA_384:
1800 case PSA_ALG_SHA_512:
1801 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1802 input, input_length );
1803 break;
1804#endif
1805 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001806 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001807 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001808
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001809 if( ret != 0 )
1810 psa_hash_abort( operation );
1811 return( mbedtls_to_psa_error( ret ) );
1812}
1813
1814psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1815 uint8_t *hash,
1816 size_t hash_size,
1817 size_t *hash_length )
1818{
itayzafrir40835d42018-08-02 13:14:17 +03001819 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001820 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001821 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001822
1823 /* Fill the output buffer with something that isn't a valid hash
1824 * (barring an attack on the hash and deliberately-crafted input),
1825 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001826 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001827 /* If hash_size is 0 then hash may be NULL and then the
1828 * call to memset would have undefined behavior. */
1829 if( hash_size != 0 )
1830 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001831
1832 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001833 {
1834 status = PSA_ERROR_BUFFER_TOO_SMALL;
1835 goto exit;
1836 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001837
1838 switch( operation->alg )
1839 {
1840#if defined(MBEDTLS_MD2_C)
1841 case PSA_ALG_MD2:
1842 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1843 break;
1844#endif
1845#if defined(MBEDTLS_MD4_C)
1846 case PSA_ALG_MD4:
1847 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1848 break;
1849#endif
1850#if defined(MBEDTLS_MD5_C)
1851 case PSA_ALG_MD5:
1852 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1853 break;
1854#endif
1855#if defined(MBEDTLS_RIPEMD160_C)
1856 case PSA_ALG_RIPEMD160:
1857 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1858 break;
1859#endif
1860#if defined(MBEDTLS_SHA1_C)
1861 case PSA_ALG_SHA_1:
1862 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1863 break;
1864#endif
1865#if defined(MBEDTLS_SHA256_C)
1866 case PSA_ALG_SHA_224:
1867 case PSA_ALG_SHA_256:
1868 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1869 break;
1870#endif
1871#if defined(MBEDTLS_SHA512_C)
1872 case PSA_ALG_SHA_384:
1873 case PSA_ALG_SHA_512:
1874 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1875 break;
1876#endif
1877 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001878 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001879 }
itayzafrir40835d42018-08-02 13:14:17 +03001880 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001881
itayzafrir40835d42018-08-02 13:14:17 +03001882exit:
1883 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001884 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001885 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001886 return( psa_hash_abort( operation ) );
1887 }
1888 else
1889 {
1890 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001891 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001892 }
1893}
1894
Gilles Peskine2d277862018-06-18 15:41:12 +02001895psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1896 const uint8_t *hash,
1897 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001898{
1899 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1900 size_t actual_hash_length;
1901 psa_status_t status = psa_hash_finish( operation,
1902 actual_hash, sizeof( actual_hash ),
1903 &actual_hash_length );
1904 if( status != PSA_SUCCESS )
1905 return( status );
1906 if( actual_hash_length != hash_length )
1907 return( PSA_ERROR_INVALID_SIGNATURE );
1908 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1909 return( PSA_ERROR_INVALID_SIGNATURE );
1910 return( PSA_SUCCESS );
1911}
1912
Gilles Peskineeb35d782019-01-22 17:56:16 +01001913psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1914 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001915{
1916 if( target_operation->alg != 0 )
1917 return( PSA_ERROR_BAD_STATE );
1918
1919 switch( source_operation->alg )
1920 {
1921 case 0:
1922 return( PSA_ERROR_BAD_STATE );
1923#if defined(MBEDTLS_MD2_C)
1924 case PSA_ALG_MD2:
1925 mbedtls_md2_clone( &target_operation->ctx.md2,
1926 &source_operation->ctx.md2 );
1927 break;
1928#endif
1929#if defined(MBEDTLS_MD4_C)
1930 case PSA_ALG_MD4:
1931 mbedtls_md4_clone( &target_operation->ctx.md4,
1932 &source_operation->ctx.md4 );
1933 break;
1934#endif
1935#if defined(MBEDTLS_MD5_C)
1936 case PSA_ALG_MD5:
1937 mbedtls_md5_clone( &target_operation->ctx.md5,
1938 &source_operation->ctx.md5 );
1939 break;
1940#endif
1941#if defined(MBEDTLS_RIPEMD160_C)
1942 case PSA_ALG_RIPEMD160:
1943 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1944 &source_operation->ctx.ripemd160 );
1945 break;
1946#endif
1947#if defined(MBEDTLS_SHA1_C)
1948 case PSA_ALG_SHA_1:
1949 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1950 &source_operation->ctx.sha1 );
1951 break;
1952#endif
1953#if defined(MBEDTLS_SHA256_C)
1954 case PSA_ALG_SHA_224:
1955 case PSA_ALG_SHA_256:
1956 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1957 &source_operation->ctx.sha256 );
1958 break;
1959#endif
1960#if defined(MBEDTLS_SHA512_C)
1961 case PSA_ALG_SHA_384:
1962 case PSA_ALG_SHA_512:
1963 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1964 &source_operation->ctx.sha512 );
1965 break;
1966#endif
1967 default:
1968 return( PSA_ERROR_NOT_SUPPORTED );
1969 }
1970
1971 target_operation->alg = source_operation->alg;
1972 return( PSA_SUCCESS );
1973}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001974
1975
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001976/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001977/* MAC */
1978/****************************************************************/
1979
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001980static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001981 psa_algorithm_t alg,
1982 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001983 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001984 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001985{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001986 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001987 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001988
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001989 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001990 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001991
Gilles Peskine8c9def32018-02-08 10:02:12 +01001992 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1993 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001994 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001995 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001996 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001997 mode = MBEDTLS_MODE_STREAM;
1998 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001999 case PSA_ALG_CTR:
2000 mode = MBEDTLS_MODE_CTR;
2001 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002002 case PSA_ALG_CFB:
2003 mode = MBEDTLS_MODE_CFB;
2004 break;
2005 case PSA_ALG_OFB:
2006 mode = MBEDTLS_MODE_OFB;
2007 break;
2008 case PSA_ALG_CBC_NO_PADDING:
2009 mode = MBEDTLS_MODE_CBC;
2010 break;
2011 case PSA_ALG_CBC_PKCS7:
2012 mode = MBEDTLS_MODE_CBC;
2013 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002014 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002015 mode = MBEDTLS_MODE_CCM;
2016 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002017 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018 mode = MBEDTLS_MODE_GCM;
2019 break;
2020 default:
2021 return( NULL );
2022 }
2023 }
2024 else if( alg == PSA_ALG_CMAC )
2025 mode = MBEDTLS_MODE_ECB;
2026 else if( alg == PSA_ALG_GMAC )
2027 mode = MBEDTLS_MODE_GCM;
2028 else
2029 return( NULL );
2030
2031 switch( key_type )
2032 {
2033 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002034 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002035 break;
2036 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002037 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2038 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002039 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002040 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002041 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002042 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002043 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2044 * but two-key Triple-DES is functionally three-key Triple-DES
2045 * with K1=K3, so that's how we present it to mbedtls. */
2046 if( key_bits == 128 )
2047 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002048 break;
2049 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002050 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002051 break;
2052 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002053 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002054 break;
2055 default:
2056 return( NULL );
2057 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002058 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002059 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002060
Jaeden Amero23bbb752018-06-26 14:16:54 +01002061 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2062 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002063}
2064
Gilles Peskinea05219c2018-11-16 16:02:56 +01002065#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002066static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002067{
Gilles Peskine2d277862018-06-18 15:41:12 +02002068 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002069 {
2070 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002071 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002072 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002073 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002074 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002075 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002076 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002077 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002078 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002079 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002080 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002081 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002082 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002083 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002084 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002085 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002086 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002087 return( 128 );
2088 default:
2089 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002090 }
2091}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002092#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002093
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002094/* Initialize the MAC operation structure. Once this function has been
2095 * called, psa_mac_abort can run and will do the right thing. */
2096static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2097 psa_algorithm_t alg )
2098{
2099 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2100
2101 operation->alg = alg;
2102 operation->key_set = 0;
2103 operation->iv_set = 0;
2104 operation->iv_required = 0;
2105 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002106 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002107
2108#if defined(MBEDTLS_CMAC_C)
2109 if( alg == PSA_ALG_CMAC )
2110 {
2111 operation->iv_required = 0;
2112 mbedtls_cipher_init( &operation->ctx.cmac );
2113 status = PSA_SUCCESS;
2114 }
2115 else
2116#endif /* MBEDTLS_CMAC_C */
2117#if defined(MBEDTLS_MD_C)
2118 if( PSA_ALG_IS_HMAC( operation->alg ) )
2119 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002120 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2121 operation->ctx.hmac.hash_ctx.alg = 0;
2122 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002123 }
2124 else
2125#endif /* MBEDTLS_MD_C */
2126 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002127 if( ! PSA_ALG_IS_MAC( alg ) )
2128 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002129 }
2130
2131 if( status != PSA_SUCCESS )
2132 memset( operation, 0, sizeof( *operation ) );
2133 return( status );
2134}
2135
Gilles Peskine01126fa2018-07-12 17:04:55 +02002136#if defined(MBEDTLS_MD_C)
2137static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2138{
Gilles Peskine3f108122018-12-07 18:14:53 +01002139 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002140 return( psa_hash_abort( &hmac->hash_ctx ) );
2141}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002142
2143static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2144{
2145 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2146 memset( hmac, 0, sizeof( *hmac ) );
2147}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002148#endif /* MBEDTLS_MD_C */
2149
Gilles Peskine8c9def32018-02-08 10:02:12 +01002150psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2151{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002152 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002153 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002154 /* The object has (apparently) been initialized but it is not
2155 * in use. It's ok to call abort on such an object, and there's
2156 * nothing to do. */
2157 return( PSA_SUCCESS );
2158 }
2159 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002160#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002161 if( operation->alg == PSA_ALG_CMAC )
2162 {
2163 mbedtls_cipher_free( &operation->ctx.cmac );
2164 }
2165 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002166#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002167#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002168 if( PSA_ALG_IS_HMAC( operation->alg ) )
2169 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002170 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002171 }
2172 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002173#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002174 {
2175 /* Sanity check (shouldn't happen: operation->alg should
2176 * always have been initialized to a valid value). */
2177 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002178 }
Moran Peker41deec42018-04-04 15:43:05 +03002179
Gilles Peskine8c9def32018-02-08 10:02:12 +01002180 operation->alg = 0;
2181 operation->key_set = 0;
2182 operation->iv_set = 0;
2183 operation->iv_required = 0;
2184 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002185 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002186
Gilles Peskine8c9def32018-02-08 10:02:12 +01002187 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002188
2189bad_state:
2190 /* If abort is called on an uninitialized object, we can't trust
2191 * anything. Wipe the object in case it contains confidential data.
2192 * This may result in a memory leak if a pointer gets overwritten,
2193 * but it's too late to do anything about this. */
2194 memset( operation, 0, sizeof( *operation ) );
2195 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002196}
2197
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002198#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002199static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002200 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002201 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002202 const mbedtls_cipher_info_t *cipher_info )
2203{
2204 int ret;
2205
2206 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002207
2208 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2209 if( ret != 0 )
2210 return( ret );
2211
2212 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2213 slot->data.raw.data,
2214 key_bits );
2215 return( ret );
2216}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002217#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002218
Gilles Peskine248051a2018-06-20 16:09:38 +02002219#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002220static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2221 const uint8_t *key,
2222 size_t key_length,
2223 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002224{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002225 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002226 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002227 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002228 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002229 psa_status_t status;
2230
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002231 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2232 * overflow below. This should never trigger if the hash algorithm
2233 * is implemented correctly. */
2234 /* The size checks against the ipad and opad buffers cannot be written
2235 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2236 * because that triggers -Wlogical-op on GCC 7.3. */
2237 if( block_size > sizeof( ipad ) )
2238 return( PSA_ERROR_NOT_SUPPORTED );
2239 if( block_size > sizeof( hmac->opad ) )
2240 return( PSA_ERROR_NOT_SUPPORTED );
2241 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002242 return( PSA_ERROR_NOT_SUPPORTED );
2243
Gilles Peskined223b522018-06-11 18:12:58 +02002244 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002245 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002246 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002247 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002248 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002249 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002250 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002251 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002252 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002253 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002254 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002255 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002256 }
Gilles Peskine96889972018-07-12 17:07:03 +02002257 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2258 * but it is permitted. It is common when HMAC is used in HKDF, for
2259 * example. Don't call `memcpy` in the 0-length because `key` could be
2260 * an invalid pointer which would make the behavior undefined. */
2261 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002262 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002263
Gilles Peskined223b522018-06-11 18:12:58 +02002264 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2265 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002266 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002267 ipad[i] ^= 0x36;
2268 memset( ipad + key_length, 0x36, block_size - key_length );
2269
2270 /* Copy the key material from ipad to opad, flipping the requisite bits,
2271 * and filling the rest of opad with the requisite constant. */
2272 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002273 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2274 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002275
Gilles Peskine01126fa2018-07-12 17:04:55 +02002276 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002277 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002278 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002279
Gilles Peskine01126fa2018-07-12 17:04:55 +02002280 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002281
2282cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002283 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002284
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002285 return( status );
2286}
Gilles Peskine248051a2018-06-20 16:09:38 +02002287#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002288
Gilles Peskine89167cb2018-07-08 20:12:23 +02002289static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002290 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002291 psa_algorithm_t alg,
2292 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002293{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002294 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002295 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002296 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002297 psa_key_usage_t usage =
2298 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002299 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002300 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002301
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002302 /* A context must be freshly initialized before it can be set up. */
2303 if( operation->alg != 0 )
2304 {
2305 return( PSA_ERROR_BAD_STATE );
2306 }
2307
Gilles Peskined911eb72018-08-14 15:18:45 +02002308 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002309 if( status != PSA_SUCCESS )
2310 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002311 if( is_sign )
2312 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002313
Gilles Peskinec5487a82018-12-03 18:08:14 +01002314 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002315 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002316 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002317 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002318
Gilles Peskine8c9def32018-02-08 10:02:12 +01002319#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002320 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002321 {
2322 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002323 mbedtls_cipher_info_from_psa( full_length_alg,
2324 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002325 int ret;
2326 if( cipher_info == NULL )
2327 {
2328 status = PSA_ERROR_NOT_SUPPORTED;
2329 goto exit;
2330 }
2331 operation->mac_size = cipher_info->block_size;
2332 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2333 status = mbedtls_to_psa_error( ret );
2334 }
2335 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002336#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002337#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002338 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002339 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002340 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002341 if( hash_alg == 0 )
2342 {
2343 status = PSA_ERROR_NOT_SUPPORTED;
2344 goto exit;
2345 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002346
2347 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2348 /* Sanity check. This shouldn't fail on a valid configuration. */
2349 if( operation->mac_size == 0 ||
2350 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2351 {
2352 status = PSA_ERROR_NOT_SUPPORTED;
2353 goto exit;
2354 }
2355
Gilles Peskine01126fa2018-07-12 17:04:55 +02002356 if( slot->type != PSA_KEY_TYPE_HMAC )
2357 {
2358 status = PSA_ERROR_INVALID_ARGUMENT;
2359 goto exit;
2360 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002361
Gilles Peskine01126fa2018-07-12 17:04:55 +02002362 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2363 slot->data.raw.data,
2364 slot->data.raw.bytes,
2365 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002366 }
2367 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002368#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002369 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002370 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002371 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002372 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002373
Gilles Peskined911eb72018-08-14 15:18:45 +02002374 if( truncated == 0 )
2375 {
2376 /* The "normal" case: untruncated algorithm. Nothing to do. */
2377 }
2378 else if( truncated < 4 )
2379 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002380 /* A very short MAC is too short for security since it can be
2381 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2382 * so we make this our minimum, even though 32 bits is still
2383 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002384 status = PSA_ERROR_NOT_SUPPORTED;
2385 }
2386 else if( truncated > operation->mac_size )
2387 {
2388 /* It's impossible to "truncate" to a larger length. */
2389 status = PSA_ERROR_INVALID_ARGUMENT;
2390 }
2391 else
2392 operation->mac_size = truncated;
2393
Gilles Peskinefbfac682018-07-08 20:51:54 +02002394exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002395 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002396 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002397 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002398 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002399 else
2400 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002401 operation->key_set = 1;
2402 }
2403 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404}
2405
Gilles Peskine89167cb2018-07-08 20:12:23 +02002406psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002407 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002408 psa_algorithm_t alg )
2409{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002410 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002411}
2412
2413psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002414 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002415 psa_algorithm_t alg )
2416{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002417 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002418}
2419
Gilles Peskine8c9def32018-02-08 10:02:12 +01002420psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2421 const uint8_t *input,
2422 size_t input_length )
2423{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002424 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002425 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002426 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002428 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002429 operation->has_input = 1;
2430
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002432 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002433 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002434 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2435 input, input_length );
2436 status = mbedtls_to_psa_error( ret );
2437 }
2438 else
2439#endif /* MBEDTLS_CMAC_C */
2440#if defined(MBEDTLS_MD_C)
2441 if( PSA_ALG_IS_HMAC( operation->alg ) )
2442 {
2443 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2444 input_length );
2445 }
2446 else
2447#endif /* MBEDTLS_MD_C */
2448 {
2449 /* This shouldn't happen if `operation` was initialized by
2450 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002451 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002452 }
2453
Gilles Peskinefbfac682018-07-08 20:51:54 +02002454 if( status != PSA_SUCCESS )
2455 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002456 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002457}
2458
Gilles Peskine01126fa2018-07-12 17:04:55 +02002459#if defined(MBEDTLS_MD_C)
2460static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2461 uint8_t *mac,
2462 size_t mac_size )
2463{
2464 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2465 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2466 size_t hash_size = 0;
2467 size_t block_size = psa_get_hash_block_size( hash_alg );
2468 psa_status_t status;
2469
Gilles Peskine01126fa2018-07-12 17:04:55 +02002470 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2471 if( status != PSA_SUCCESS )
2472 return( status );
2473 /* From here on, tmp needs to be wiped. */
2474
2475 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2476 if( status != PSA_SUCCESS )
2477 goto exit;
2478
2479 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2480 if( status != PSA_SUCCESS )
2481 goto exit;
2482
2483 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2484 if( status != PSA_SUCCESS )
2485 goto exit;
2486
Gilles Peskined911eb72018-08-14 15:18:45 +02002487 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2488 if( status != PSA_SUCCESS )
2489 goto exit;
2490
2491 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002492
2493exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002494 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002495 return( status );
2496}
2497#endif /* MBEDTLS_MD_C */
2498
mohammad16036df908f2018-04-02 08:34:15 -07002499static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002500 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002501 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002503 if( ! operation->key_set )
2504 return( PSA_ERROR_BAD_STATE );
2505 if( operation->iv_required && ! operation->iv_set )
2506 return( PSA_ERROR_BAD_STATE );
2507
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508 if( mac_size < operation->mac_size )
2509 return( PSA_ERROR_BUFFER_TOO_SMALL );
2510
Gilles Peskine8c9def32018-02-08 10:02:12 +01002511#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002512 if( operation->alg == PSA_ALG_CMAC )
2513 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002514 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2515 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2516 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002517 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002518 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002519 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002521 else
2522#endif /* MBEDTLS_CMAC_C */
2523#if defined(MBEDTLS_MD_C)
2524 if( PSA_ALG_IS_HMAC( operation->alg ) )
2525 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002526 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002527 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002528 }
2529 else
2530#endif /* MBEDTLS_MD_C */
2531 {
2532 /* This shouldn't happen if `operation` was initialized by
2533 * a setup function. */
2534 return( PSA_ERROR_BAD_STATE );
2535 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536}
2537
Gilles Peskineacd4be32018-07-08 19:56:25 +02002538psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2539 uint8_t *mac,
2540 size_t mac_size,
2541 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002542{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002543 psa_status_t status;
2544
Jaeden Amero252ef282019-02-15 14:05:35 +00002545 if( operation->alg == 0 )
2546 {
2547 return( PSA_ERROR_BAD_STATE );
2548 }
2549
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002550 /* Fill the output buffer with something that isn't a valid mac
2551 * (barring an attack on the mac and deliberately-crafted input),
2552 * in case the caller doesn't check the return status properly. */
2553 *mac_length = mac_size;
2554 /* If mac_size is 0 then mac may be NULL and then the
2555 * call to memset would have undefined behavior. */
2556 if( mac_size != 0 )
2557 memset( mac, '!', mac_size );
2558
Gilles Peskine89167cb2018-07-08 20:12:23 +02002559 if( ! operation->is_sign )
2560 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002561 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002562 }
mohammad16036df908f2018-04-02 08:34:15 -07002563
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002564 status = psa_mac_finish_internal( operation, mac, mac_size );
2565
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002566 if( status == PSA_SUCCESS )
2567 {
2568 status = psa_mac_abort( operation );
2569 if( status == PSA_SUCCESS )
2570 *mac_length = operation->mac_size;
2571 else
2572 memset( mac, '!', mac_size );
2573 }
2574 else
2575 psa_mac_abort( operation );
2576 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002577}
2578
Gilles Peskineacd4be32018-07-08 19:56:25 +02002579psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2580 const uint8_t *mac,
2581 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582{
Gilles Peskine828ed142018-06-18 23:25:51 +02002583 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002584 psa_status_t status;
2585
Jaeden Amero252ef282019-02-15 14:05:35 +00002586 if( operation->alg == 0 )
2587 {
2588 return( PSA_ERROR_BAD_STATE );
2589 }
2590
Gilles Peskine89167cb2018-07-08 20:12:23 +02002591 if( operation->is_sign )
2592 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002593 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002594 }
2595 if( operation->mac_size != mac_length )
2596 {
2597 status = PSA_ERROR_INVALID_SIGNATURE;
2598 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002599 }
mohammad16036df908f2018-04-02 08:34:15 -07002600
2601 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002602 actual_mac, sizeof( actual_mac ) );
2603
2604 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2605 status = PSA_ERROR_INVALID_SIGNATURE;
2606
2607cleanup:
2608 if( status == PSA_SUCCESS )
2609 status = psa_mac_abort( operation );
2610 else
2611 psa_mac_abort( operation );
2612
Gilles Peskine3f108122018-12-07 18:14:53 +01002613 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002614
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002615 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616}
2617
2618
Gilles Peskine20035e32018-02-03 22:44:14 +01002619
Gilles Peskine20035e32018-02-03 22:44:14 +01002620/****************************************************************/
2621/* Asymmetric cryptography */
2622/****************************************************************/
2623
Gilles Peskine2b450e32018-06-27 15:42:46 +02002624#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002625/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002626 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002627static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2628 size_t hash_length,
2629 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002630{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002631 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002632 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002633 *md_alg = mbedtls_md_get_type( md_info );
2634
2635 /* The Mbed TLS RSA module uses an unsigned int for hash length
2636 * parameters. Validate that it fits so that we don't risk an
2637 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002638#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002639 if( hash_length > UINT_MAX )
2640 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002641#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002642
2643#if defined(MBEDTLS_PKCS1_V15)
2644 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2645 * must be correct. */
2646 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2647 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002648 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002649 if( md_info == NULL )
2650 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002651 if( mbedtls_md_get_size( md_info ) != hash_length )
2652 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002653 }
2654#endif /* MBEDTLS_PKCS1_V15 */
2655
2656#if defined(MBEDTLS_PKCS1_V21)
2657 /* PSS requires a hash internally. */
2658 if( PSA_ALG_IS_RSA_PSS( alg ) )
2659 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002660 if( md_info == NULL )
2661 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002662 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002663#endif /* MBEDTLS_PKCS1_V21 */
2664
Gilles Peskine61b91d42018-06-08 16:09:36 +02002665 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002666}
2667
Gilles Peskine2b450e32018-06-27 15:42:46 +02002668static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2669 psa_algorithm_t alg,
2670 const uint8_t *hash,
2671 size_t hash_length,
2672 uint8_t *signature,
2673 size_t signature_size,
2674 size_t *signature_length )
2675{
2676 psa_status_t status;
2677 int ret;
2678 mbedtls_md_type_t md_alg;
2679
2680 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2681 if( status != PSA_SUCCESS )
2682 return( status );
2683
Gilles Peskine630a18a2018-06-29 17:49:35 +02002684 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002685 return( PSA_ERROR_BUFFER_TOO_SMALL );
2686
2687#if defined(MBEDTLS_PKCS1_V15)
2688 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2689 {
2690 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2691 MBEDTLS_MD_NONE );
2692 ret = mbedtls_rsa_pkcs1_sign( rsa,
2693 mbedtls_ctr_drbg_random,
2694 &global_data.ctr_drbg,
2695 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002696 md_alg,
2697 (unsigned int) hash_length,
2698 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002699 signature );
2700 }
2701 else
2702#endif /* MBEDTLS_PKCS1_V15 */
2703#if defined(MBEDTLS_PKCS1_V21)
2704 if( PSA_ALG_IS_RSA_PSS( alg ) )
2705 {
2706 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2707 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2708 mbedtls_ctr_drbg_random,
2709 &global_data.ctr_drbg,
2710 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002711 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002712 (unsigned int) hash_length,
2713 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002714 signature );
2715 }
2716 else
2717#endif /* MBEDTLS_PKCS1_V21 */
2718 {
2719 return( PSA_ERROR_INVALID_ARGUMENT );
2720 }
2721
2722 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002723 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002724 return( mbedtls_to_psa_error( ret ) );
2725}
2726
2727static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2728 psa_algorithm_t alg,
2729 const uint8_t *hash,
2730 size_t hash_length,
2731 const uint8_t *signature,
2732 size_t signature_length )
2733{
2734 psa_status_t status;
2735 int ret;
2736 mbedtls_md_type_t md_alg;
2737
2738 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2739 if( status != PSA_SUCCESS )
2740 return( status );
2741
Gilles Peskine630a18a2018-06-29 17:49:35 +02002742 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002743 return( PSA_ERROR_BUFFER_TOO_SMALL );
2744
2745#if defined(MBEDTLS_PKCS1_V15)
2746 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2747 {
2748 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2749 MBEDTLS_MD_NONE );
2750 ret = mbedtls_rsa_pkcs1_verify( rsa,
2751 mbedtls_ctr_drbg_random,
2752 &global_data.ctr_drbg,
2753 MBEDTLS_RSA_PUBLIC,
2754 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002755 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002756 hash,
2757 signature );
2758 }
2759 else
2760#endif /* MBEDTLS_PKCS1_V15 */
2761#if defined(MBEDTLS_PKCS1_V21)
2762 if( PSA_ALG_IS_RSA_PSS( alg ) )
2763 {
2764 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2765 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2766 mbedtls_ctr_drbg_random,
2767 &global_data.ctr_drbg,
2768 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002769 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002770 (unsigned int) hash_length,
2771 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002772 signature );
2773 }
2774 else
2775#endif /* MBEDTLS_PKCS1_V21 */
2776 {
2777 return( PSA_ERROR_INVALID_ARGUMENT );
2778 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002779
2780 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2781 * the rest of the signature is invalid". This has little use in
2782 * practice and PSA doesn't report this distinction. */
2783 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2784 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002785 return( mbedtls_to_psa_error( ret ) );
2786}
2787#endif /* MBEDTLS_RSA_C */
2788
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002789#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002790/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2791 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2792 * (even though these functions don't modify it). */
2793static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2794 psa_algorithm_t alg,
2795 const uint8_t *hash,
2796 size_t hash_length,
2797 uint8_t *signature,
2798 size_t signature_size,
2799 size_t *signature_length )
2800{
2801 int ret;
2802 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002803 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002804 mbedtls_mpi_init( &r );
2805 mbedtls_mpi_init( &s );
2806
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002807 if( signature_size < 2 * curve_bytes )
2808 {
2809 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2810 goto cleanup;
2811 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002812
Gilles Peskinea05219c2018-11-16 16:02:56 +01002813#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002814 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2815 {
2816 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2817 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2818 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2819 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2820 hash, hash_length,
2821 md_alg ) );
2822 }
2823 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002824#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002825 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002826 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002827 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2828 hash, hash_length,
2829 mbedtls_ctr_drbg_random,
2830 &global_data.ctr_drbg ) );
2831 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002832
2833 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2834 signature,
2835 curve_bytes ) );
2836 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2837 signature + curve_bytes,
2838 curve_bytes ) );
2839
2840cleanup:
2841 mbedtls_mpi_free( &r );
2842 mbedtls_mpi_free( &s );
2843 if( ret == 0 )
2844 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002845 return( mbedtls_to_psa_error( ret ) );
2846}
2847
2848static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2849 const uint8_t *hash,
2850 size_t hash_length,
2851 const uint8_t *signature,
2852 size_t signature_length )
2853{
2854 int ret;
2855 mbedtls_mpi r, s;
2856 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2857 mbedtls_mpi_init( &r );
2858 mbedtls_mpi_init( &s );
2859
2860 if( signature_length != 2 * curve_bytes )
2861 return( PSA_ERROR_INVALID_SIGNATURE );
2862
2863 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2864 signature,
2865 curve_bytes ) );
2866 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2867 signature + curve_bytes,
2868 curve_bytes ) );
2869
2870 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2871 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002872
2873cleanup:
2874 mbedtls_mpi_free( &r );
2875 mbedtls_mpi_free( &s );
2876 return( mbedtls_to_psa_error( ret ) );
2877}
2878#endif /* MBEDTLS_ECDSA_C */
2879
Gilles Peskinec5487a82018-12-03 18:08:14 +01002880psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002881 psa_algorithm_t alg,
2882 const uint8_t *hash,
2883 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002884 uint8_t *signature,
2885 size_t signature_size,
2886 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002887{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002888 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002889 psa_status_t status;
2890
2891 *signature_length = signature_size;
2892
Gilles Peskinec5487a82018-12-03 18:08:14 +01002893 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002894 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002895 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002896 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002897 {
2898 status = PSA_ERROR_INVALID_ARGUMENT;
2899 goto exit;
2900 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002901
Gilles Peskine20035e32018-02-03 22:44:14 +01002902#if defined(MBEDTLS_RSA_C)
2903 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2904 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002905 status = psa_rsa_sign( slot->data.rsa,
2906 alg,
2907 hash, hash_length,
2908 signature, signature_size,
2909 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002910 }
2911 else
2912#endif /* defined(MBEDTLS_RSA_C) */
2913#if defined(MBEDTLS_ECP_C)
2914 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2915 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002916#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002917 if(
2918#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2919 PSA_ALG_IS_ECDSA( alg )
2920#else
2921 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2922#endif
2923 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002924 status = psa_ecdsa_sign( slot->data.ecp,
2925 alg,
2926 hash, hash_length,
2927 signature, signature_size,
2928 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002929 else
2930#endif /* defined(MBEDTLS_ECDSA_C) */
2931 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002932 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002933 }
itayzafrir5c753392018-05-08 11:18:38 +03002934 }
2935 else
2936#endif /* defined(MBEDTLS_ECP_C) */
2937 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002938 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002939 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002940
2941exit:
2942 /* Fill the unused part of the output buffer (the whole buffer on error,
2943 * the trailing part on success) with something that isn't a valid mac
2944 * (barring an attack on the mac and deliberately-crafted input),
2945 * in case the caller doesn't check the return status properly. */
2946 if( status == PSA_SUCCESS )
2947 memset( signature + *signature_length, '!',
2948 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002949 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002950 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002951 /* If signature_size is 0 then we have nothing to do. We must not call
2952 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002953 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002954}
2955
Gilles Peskinec5487a82018-12-03 18:08:14 +01002956psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002957 psa_algorithm_t alg,
2958 const uint8_t *hash,
2959 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002960 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002961 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002962{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002963 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002964 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002965
Gilles Peskinec5487a82018-12-03 18:08:14 +01002966 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002967 if( status != PSA_SUCCESS )
2968 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002969
Gilles Peskine61b91d42018-06-08 16:09:36 +02002970#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002971 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002972 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002973 return( psa_rsa_verify( slot->data.rsa,
2974 alg,
2975 hash, hash_length,
2976 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002977 }
2978 else
2979#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002980#if defined(MBEDTLS_ECP_C)
2981 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2982 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002983#if defined(MBEDTLS_ECDSA_C)
2984 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002985 return( psa_ecdsa_verify( slot->data.ecp,
2986 hash, hash_length,
2987 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002988 else
2989#endif /* defined(MBEDTLS_ECDSA_C) */
2990 {
2991 return( PSA_ERROR_INVALID_ARGUMENT );
2992 }
itayzafrir5c753392018-05-08 11:18:38 +03002993 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002994 else
2995#endif /* defined(MBEDTLS_ECP_C) */
2996 {
2997 return( PSA_ERROR_NOT_SUPPORTED );
2998 }
2999}
3000
Gilles Peskine072ac562018-06-30 00:21:29 +02003001#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3002static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3003 mbedtls_rsa_context *rsa )
3004{
3005 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3006 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3007 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3008 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3009}
3010#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3011
Gilles Peskinec5487a82018-12-03 18:08:14 +01003012psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003013 psa_algorithm_t alg,
3014 const uint8_t *input,
3015 size_t input_length,
3016 const uint8_t *salt,
3017 size_t salt_length,
3018 uint8_t *output,
3019 size_t output_size,
3020 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003021{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003022 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003023 psa_status_t status;
3024
Darryl Green5cc689a2018-07-24 15:34:10 +01003025 (void) input;
3026 (void) input_length;
3027 (void) salt;
3028 (void) output;
3029 (void) output_size;
3030
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003031 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003033 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3034 return( PSA_ERROR_INVALID_ARGUMENT );
3035
Gilles Peskinec5487a82018-12-03 18:08:14 +01003036 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003037 if( status != PSA_SUCCESS )
3038 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003039 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
3040 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003041 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003042
3043#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003044 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003045 {
3046 mbedtls_rsa_context *rsa = slot->data.rsa;
3047 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003048 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003049 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003050#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003051 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003052 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003053 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3054 mbedtls_ctr_drbg_random,
3055 &global_data.ctr_drbg,
3056 MBEDTLS_RSA_PUBLIC,
3057 input_length,
3058 input,
3059 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003060 }
3061 else
3062#endif /* MBEDTLS_PKCS1_V15 */
3063#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003064 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003065 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003066 psa_rsa_oaep_set_padding_mode( alg, rsa );
3067 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3068 mbedtls_ctr_drbg_random,
3069 &global_data.ctr_drbg,
3070 MBEDTLS_RSA_PUBLIC,
3071 salt, salt_length,
3072 input_length,
3073 input,
3074 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003075 }
3076 else
3077#endif /* MBEDTLS_PKCS1_V21 */
3078 {
3079 return( PSA_ERROR_INVALID_ARGUMENT );
3080 }
3081 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003082 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003083 return( mbedtls_to_psa_error( ret ) );
3084 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003085 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003086#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003087 {
3088 return( PSA_ERROR_NOT_SUPPORTED );
3089 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003090}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003091
Gilles Peskinec5487a82018-12-03 18:08:14 +01003092psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003093 psa_algorithm_t alg,
3094 const uint8_t *input,
3095 size_t input_length,
3096 const uint8_t *salt,
3097 size_t salt_length,
3098 uint8_t *output,
3099 size_t output_size,
3100 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003101{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003102 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003103 psa_status_t status;
3104
Darryl Green5cc689a2018-07-24 15:34:10 +01003105 (void) input;
3106 (void) input_length;
3107 (void) salt;
3108 (void) output;
3109 (void) output_size;
3110
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003111 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003112
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003113 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3114 return( PSA_ERROR_INVALID_ARGUMENT );
3115
Gilles Peskinec5487a82018-12-03 18:08:14 +01003116 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003117 if( status != PSA_SUCCESS )
3118 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003119 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
3120 return( PSA_ERROR_INVALID_ARGUMENT );
3121
3122#if defined(MBEDTLS_RSA_C)
3123 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
3124 {
3125 mbedtls_rsa_context *rsa = slot->data.rsa;
3126 int ret;
3127
Gilles Peskine630a18a2018-06-29 17:49:35 +02003128 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003129 return( PSA_ERROR_INVALID_ARGUMENT );
3130
3131#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003132 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003133 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003134 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3135 mbedtls_ctr_drbg_random,
3136 &global_data.ctr_drbg,
3137 MBEDTLS_RSA_PRIVATE,
3138 output_length,
3139 input,
3140 output,
3141 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142 }
3143 else
3144#endif /* MBEDTLS_PKCS1_V15 */
3145#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003146 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003148 psa_rsa_oaep_set_padding_mode( alg, rsa );
3149 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3150 mbedtls_ctr_drbg_random,
3151 &global_data.ctr_drbg,
3152 MBEDTLS_RSA_PRIVATE,
3153 salt, salt_length,
3154 output_length,
3155 input,
3156 output,
3157 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003158 }
3159 else
3160#endif /* MBEDTLS_PKCS1_V21 */
3161 {
3162 return( PSA_ERROR_INVALID_ARGUMENT );
3163 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003164
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003165 return( mbedtls_to_psa_error( ret ) );
3166 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003167 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003168#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003169 {
3170 return( PSA_ERROR_NOT_SUPPORTED );
3171 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003172}
Gilles Peskine20035e32018-02-03 22:44:14 +01003173
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003174
3175
mohammad1603503973b2018-03-12 15:59:30 +02003176/****************************************************************/
3177/* Symmetric cryptography */
3178/****************************************************************/
3179
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003180/* Initialize the cipher operation structure. Once this function has been
3181 * called, psa_cipher_abort can run and will do the right thing. */
3182static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3183 psa_algorithm_t alg )
3184{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003185 if( ! PSA_ALG_IS_CIPHER( alg ) )
3186 {
3187 memset( operation, 0, sizeof( *operation ) );
3188 return( PSA_ERROR_INVALID_ARGUMENT );
3189 }
3190
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003191 operation->alg = alg;
3192 operation->key_set = 0;
3193 operation->iv_set = 0;
3194 operation->iv_required = 1;
3195 operation->iv_size = 0;
3196 operation->block_size = 0;
3197 mbedtls_cipher_init( &operation->ctx.cipher );
3198 return( PSA_SUCCESS );
3199}
3200
Gilles Peskinee553c652018-06-04 16:22:46 +02003201static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003202 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003203 psa_algorithm_t alg,
3204 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003205{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003206 int ret = 0;
3207 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003208 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003209 size_t key_bits;
3210 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003211 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3212 PSA_KEY_USAGE_ENCRYPT :
3213 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003214
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003215 /* A context must be freshly initialized before it can be set up. */
3216 if( operation->alg != 0 )
3217 {
3218 return( PSA_ERROR_BAD_STATE );
3219 }
3220
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003221 status = psa_cipher_init( operation, alg );
3222 if( status != PSA_SUCCESS )
3223 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003224
Gilles Peskinec5487a82018-12-03 18:08:14 +01003225 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003226 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003227 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003228 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003229
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003230 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003231 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003232 {
3233 status = PSA_ERROR_NOT_SUPPORTED;
3234 goto exit;
3235 }
mohammad1603503973b2018-03-12 15:59:30 +02003236
mohammad1603503973b2018-03-12 15:59:30 +02003237 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003238 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003239 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003240
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003241#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003242 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003243 {
3244 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3245 unsigned char keys[24];
3246 memcpy( keys, slot->data.raw.data, 16 );
3247 memcpy( keys + 16, slot->data.raw.data, 8 );
3248 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3249 keys,
3250 192, cipher_operation );
3251 }
3252 else
3253#endif
3254 {
3255 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3256 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003257 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003258 }
Moran Peker41deec42018-04-04 15:43:05 +03003259 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003260 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003261
mohammad16038481e742018-03-18 13:57:31 +02003262#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003263 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003264 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003265 case PSA_ALG_CBC_NO_PADDING:
3266 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3267 MBEDTLS_PADDING_NONE );
3268 break;
3269 case PSA_ALG_CBC_PKCS7:
3270 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3271 MBEDTLS_PADDING_PKCS7 );
3272 break;
3273 default:
3274 /* The algorithm doesn't involve padding. */
3275 ret = 0;
3276 break;
3277 }
3278 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003279 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003280#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3281
mohammad1603503973b2018-03-12 15:59:30 +02003282 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003283 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3284 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3285 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003286 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003287 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003288 }
mohammad1603503973b2018-03-12 15:59:30 +02003289
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003290exit:
3291 if( status == 0 )
3292 status = mbedtls_to_psa_error( ret );
3293 if( status != 0 )
3294 psa_cipher_abort( operation );
3295 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003296}
3297
Gilles Peskinefe119512018-07-08 21:39:34 +02003298psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003299 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003300 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003301{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003302 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003303}
3304
Gilles Peskinefe119512018-07-08 21:39:34 +02003305psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003306 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003307 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003308{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003309 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003310}
3311
Gilles Peskinefe119512018-07-08 21:39:34 +02003312psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3313 unsigned char *iv,
3314 size_t iv_size,
3315 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003316{
itayzafrir534bd7c2018-08-02 13:56:32 +03003317 psa_status_t status;
3318 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003319 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003320 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003321 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003322 }
Moran Peker41deec42018-04-04 15:43:05 +03003323 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003324 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003325 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003326 goto exit;
3327 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003328 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3329 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003330 if( ret != 0 )
3331 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003332 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003333 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003334 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003335
mohammad16038481e742018-03-18 13:57:31 +02003336 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003337 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003338
Moran Peker395db872018-05-31 14:07:14 +03003339exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003340 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003341 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003342 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003343}
3344
Gilles Peskinefe119512018-07-08 21:39:34 +02003345psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3346 const unsigned char *iv,
3347 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003348{
itayzafrir534bd7c2018-08-02 13:56:32 +03003349 psa_status_t status;
3350 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003351 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003352 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003353 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003354 }
Moran Pekera28258c2018-05-29 16:25:04 +03003355 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003356 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003357 status = PSA_ERROR_INVALID_ARGUMENT;
3358 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003359 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003360 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3361 status = mbedtls_to_psa_error( ret );
3362exit:
3363 if( status == PSA_SUCCESS )
3364 operation->iv_set = 1;
3365 else
mohammad1603503973b2018-03-12 15:59:30 +02003366 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003367 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003368}
3369
Gilles Peskinee553c652018-06-04 16:22:46 +02003370psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3371 const uint8_t *input,
3372 size_t input_length,
3373 unsigned char *output,
3374 size_t output_size,
3375 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003376{
itayzafrir534bd7c2018-08-02 13:56:32 +03003377 psa_status_t status;
3378 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003379 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003380
3381 if( operation->alg == 0 )
3382 {
3383 return( PSA_ERROR_BAD_STATE );
3384 }
3385
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003386 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003387 {
3388 /* Take the unprocessed partial block left over from previous
3389 * update calls, if any, plus the input to this call. Remove
3390 * the last partial block, if any. You get the data that will be
3391 * output in this call. */
3392 expected_output_size =
3393 ( operation->ctx.cipher.unprocessed_len + input_length )
3394 / operation->block_size * operation->block_size;
3395 }
3396 else
3397 {
3398 expected_output_size = input_length;
3399 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003400
Gilles Peskine89d789c2018-06-04 17:17:16 +02003401 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003402 {
3403 status = PSA_ERROR_BUFFER_TOO_SMALL;
3404 goto exit;
3405 }
mohammad160382759612018-03-12 18:16:40 +02003406
mohammad1603503973b2018-03-12 15:59:30 +02003407 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003408 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003409 status = mbedtls_to_psa_error( ret );
3410exit:
3411 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003412 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003413 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003414}
3415
Gilles Peskinee553c652018-06-04 16:22:46 +02003416psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3417 uint8_t *output,
3418 size_t output_size,
3419 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003420{
David Saadab4ecc272019-02-14 13:48:10 +02003421 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003422 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003423 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003424
mohammad1603503973b2018-03-12 15:59:30 +02003425 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003426 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003427 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003428 }
3429 if( operation->iv_required && ! operation->iv_set )
3430 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003431 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003432 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003433
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003434 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003435 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3436 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003437 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003438 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003439 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003440 }
3441
Janos Follath315b51c2018-07-09 16:04:51 +01003442 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3443 temp_output_buffer,
3444 output_length );
3445 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003446 {
Janos Follath315b51c2018-07-09 16:04:51 +01003447 status = mbedtls_to_psa_error( cipher_ret );
3448 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003449 }
Janos Follath315b51c2018-07-09 16:04:51 +01003450
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003451 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003452 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003453 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003454 memcpy( output, temp_output_buffer, *output_length );
3455 else
3456 {
Janos Follath315b51c2018-07-09 16:04:51 +01003457 status = PSA_ERROR_BUFFER_TOO_SMALL;
3458 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003459 }
mohammad1603503973b2018-03-12 15:59:30 +02003460
Gilles Peskine3f108122018-12-07 18:14:53 +01003461 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003462 status = psa_cipher_abort( operation );
3463
3464 return( status );
3465
3466error:
3467
3468 *output_length = 0;
3469
Gilles Peskine3f108122018-12-07 18:14:53 +01003470 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003471 (void) psa_cipher_abort( operation );
3472
3473 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003474}
3475
Gilles Peskinee553c652018-06-04 16:22:46 +02003476psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3477{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003478 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003479 {
3480 /* The object has (apparently) been initialized but it is not
3481 * in use. It's ok to call abort on such an object, and there's
3482 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003483 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003484 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003485
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003486 /* Sanity check (shouldn't happen: operation->alg should
3487 * always have been initialized to a valid value). */
3488 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3489 return( PSA_ERROR_BAD_STATE );
3490
mohammad1603503973b2018-03-12 15:59:30 +02003491 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003492
Moran Peker41deec42018-04-04 15:43:05 +03003493 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003494 operation->key_set = 0;
3495 operation->iv_set = 0;
3496 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003497 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003498 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003499
Moran Peker395db872018-05-31 14:07:14 +03003500 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003501}
3502
Gilles Peskinea0655c32018-04-30 17:06:50 +02003503
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003504
mohammad16038cc1cee2018-03-28 01:21:33 +03003505/****************************************************************/
3506/* Key Policy */
3507/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003508
mohammad160327010052018-07-03 13:16:15 +03003509#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003510void psa_key_policy_set_usage( psa_key_policy_t *policy,
3511 psa_key_usage_t usage,
3512 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003513{
mohammad16034eed7572018-03-28 05:14:59 -07003514 policy->usage = usage;
3515 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003516}
3517
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003518psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003519{
mohammad16036df908f2018-04-02 08:34:15 -07003520 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003521}
3522
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003523psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003524{
mohammad16036df908f2018-04-02 08:34:15 -07003525 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003526}
mohammad160327010052018-07-03 13:16:15 +03003527#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003528
Gilles Peskinec5487a82018-12-03 18:08:14 +01003529psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003530 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003531{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003532 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003533 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003534
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003535 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003536 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003537
Gilles Peskinec5487a82018-12-03 18:08:14 +01003538 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003539 if( status != PSA_SUCCESS )
3540 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003541
Gilles Peskine4747d192019-04-17 15:05:45 +02003542 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003543}
3544
Gilles Peskinec5487a82018-12-03 18:08:14 +01003545psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003546 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003547{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003548 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003549 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003550
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003551 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003552 return( PSA_ERROR_INVALID_ARGUMENT );
3553
Gilles Peskinec5487a82018-12-03 18:08:14 +01003554 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003555 if( status != PSA_SUCCESS )
3556 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003557
mohammad16036df908f2018-04-02 08:34:15 -07003558 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003559
3560 return( PSA_SUCCESS );
3561}
Gilles Peskine20035e32018-02-03 22:44:14 +01003562
Gilles Peskinea0655c32018-04-30 17:06:50 +02003563
3564
mohammad1603804cd712018-03-20 22:44:08 +02003565/****************************************************************/
3566/* Key Lifetime */
3567/****************************************************************/
3568
Gilles Peskine87a5e562019-04-17 12:28:25 +02003569psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003570 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003571{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003572 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003573 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003574
Gilles Peskinec5487a82018-12-03 18:08:14 +01003575 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003576 if( status != PSA_SUCCESS )
3577 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003578
mohammad1603804cd712018-03-20 22:44:08 +02003579 *lifetime = slot->lifetime;
3580
3581 return( PSA_SUCCESS );
3582}
3583
Gilles Peskine20035e32018-02-03 22:44:14 +01003584
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003585
mohammad16035955c982018-04-26 00:53:03 +03003586/****************************************************************/
3587/* AEAD */
3588/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003589
Gilles Peskineedf9a652018-08-17 18:11:56 +02003590typedef struct
3591{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003592 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003593 const mbedtls_cipher_info_t *cipher_info;
3594 union
3595 {
3596#if defined(MBEDTLS_CCM_C)
3597 mbedtls_ccm_context ccm;
3598#endif /* MBEDTLS_CCM_C */
3599#if defined(MBEDTLS_GCM_C)
3600 mbedtls_gcm_context gcm;
3601#endif /* MBEDTLS_GCM_C */
3602 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003603 psa_algorithm_t core_alg;
3604 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003605 uint8_t tag_length;
3606} aead_operation_t;
3607
Gilles Peskine30a9e412019-01-14 18:36:12 +01003608static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003609{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003610 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003611 {
3612#if defined(MBEDTLS_CCM_C)
3613 case PSA_ALG_CCM:
3614 mbedtls_ccm_free( &operation->ctx.ccm );
3615 break;
3616#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003617#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003618 case PSA_ALG_GCM:
3619 mbedtls_gcm_free( &operation->ctx.gcm );
3620 break;
3621#endif /* MBEDTLS_GCM_C */
3622 }
3623}
3624
3625static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003626 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003627 psa_key_usage_t usage,
3628 psa_algorithm_t alg )
3629{
3630 psa_status_t status;
3631 size_t key_bits;
3632 mbedtls_cipher_id_t cipher_id;
3633
Gilles Peskinec5487a82018-12-03 18:08:14 +01003634 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003635 if( status != PSA_SUCCESS )
3636 return( status );
3637
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003638 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003639
3640 operation->cipher_info =
3641 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3642 &cipher_id );
3643 if( operation->cipher_info == NULL )
3644 return( PSA_ERROR_NOT_SUPPORTED );
3645
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003646 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003647 {
3648#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003649 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3650 operation->core_alg = PSA_ALG_CCM;
3651 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003652 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3653 return( PSA_ERROR_INVALID_ARGUMENT );
3654 mbedtls_ccm_init( &operation->ctx.ccm );
3655 status = mbedtls_to_psa_error(
3656 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3657 operation->slot->data.raw.data,
3658 (unsigned int) key_bits ) );
3659 if( status != 0 )
3660 goto cleanup;
3661 break;
3662#endif /* MBEDTLS_CCM_C */
3663
3664#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003665 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3666 operation->core_alg = PSA_ALG_GCM;
3667 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003668 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3669 return( PSA_ERROR_INVALID_ARGUMENT );
3670 mbedtls_gcm_init( &operation->ctx.gcm );
3671 status = mbedtls_to_psa_error(
3672 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3673 operation->slot->data.raw.data,
3674 (unsigned int) key_bits ) );
3675 break;
3676#endif /* MBEDTLS_GCM_C */
3677
3678 default:
3679 return( PSA_ERROR_NOT_SUPPORTED );
3680 }
3681
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003682 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3683 {
3684 status = PSA_ERROR_INVALID_ARGUMENT;
3685 goto cleanup;
3686 }
3687 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3688 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3689 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3690 * In both cases, mbedtls_xxx will validate the tag length below. */
3691
Gilles Peskineedf9a652018-08-17 18:11:56 +02003692 return( PSA_SUCCESS );
3693
3694cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003695 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003696 return( status );
3697}
3698
Gilles Peskinec5487a82018-12-03 18:08:14 +01003699psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003700 psa_algorithm_t alg,
3701 const uint8_t *nonce,
3702 size_t nonce_length,
3703 const uint8_t *additional_data,
3704 size_t additional_data_length,
3705 const uint8_t *plaintext,
3706 size_t plaintext_length,
3707 uint8_t *ciphertext,
3708 size_t ciphertext_size,
3709 size_t *ciphertext_length )
3710{
mohammad16035955c982018-04-26 00:53:03 +03003711 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003712 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003713 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003714
mohammad1603f08a5502018-06-03 15:05:47 +03003715 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003716
Gilles Peskinec5487a82018-12-03 18:08:14 +01003717 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003718 if( status != PSA_SUCCESS )
3719 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003720
Gilles Peskineedf9a652018-08-17 18:11:56 +02003721 /* For all currently supported modes, the tag is at the end of the
3722 * ciphertext. */
3723 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3724 {
3725 status = PSA_ERROR_BUFFER_TOO_SMALL;
3726 goto exit;
3727 }
3728 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003729
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003730#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003731 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003732 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003733 status = mbedtls_to_psa_error(
3734 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3735 MBEDTLS_GCM_ENCRYPT,
3736 plaintext_length,
3737 nonce, nonce_length,
3738 additional_data, additional_data_length,
3739 plaintext, ciphertext,
3740 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003741 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003742 else
3743#endif /* MBEDTLS_GCM_C */
3744#if defined(MBEDTLS_CCM_C)
3745 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003746 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003747 status = mbedtls_to_psa_error(
3748 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3749 plaintext_length,
3750 nonce, nonce_length,
3751 additional_data,
3752 additional_data_length,
3753 plaintext, ciphertext,
3754 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003755 }
mohammad16035c8845f2018-05-09 05:40:09 -07003756 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003757#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003758 {
mohammad1603554faad2018-06-03 15:07:38 +03003759 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003760 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003761
Gilles Peskineedf9a652018-08-17 18:11:56 +02003762 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3763 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003764
Gilles Peskineedf9a652018-08-17 18:11:56 +02003765exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003766 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003767 if( status == PSA_SUCCESS )
3768 *ciphertext_length = plaintext_length + operation.tag_length;
3769 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003770}
3771
Gilles Peskineee652a32018-06-01 19:23:52 +02003772/* Locate the tag in a ciphertext buffer containing the encrypted data
3773 * followed by the tag. Return the length of the part preceding the tag in
3774 * *plaintext_length. This is the size of the plaintext in modes where
3775 * the encrypted data has the same size as the plaintext, such as
3776 * CCM and GCM. */
3777static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3778 const uint8_t *ciphertext,
3779 size_t ciphertext_length,
3780 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003781 const uint8_t **p_tag )
3782{
3783 size_t payload_length;
3784 if( tag_length > ciphertext_length )
3785 return( PSA_ERROR_INVALID_ARGUMENT );
3786 payload_length = ciphertext_length - tag_length;
3787 if( payload_length > plaintext_size )
3788 return( PSA_ERROR_BUFFER_TOO_SMALL );
3789 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003790 return( PSA_SUCCESS );
3791}
3792
Gilles Peskinec5487a82018-12-03 18:08:14 +01003793psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003794 psa_algorithm_t alg,
3795 const uint8_t *nonce,
3796 size_t nonce_length,
3797 const uint8_t *additional_data,
3798 size_t additional_data_length,
3799 const uint8_t *ciphertext,
3800 size_t ciphertext_length,
3801 uint8_t *plaintext,
3802 size_t plaintext_size,
3803 size_t *plaintext_length )
3804{
mohammad16035955c982018-04-26 00:53:03 +03003805 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003806 aead_operation_t operation;
3807 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003808
Gilles Peskineee652a32018-06-01 19:23:52 +02003809 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003810
Gilles Peskinec5487a82018-12-03 18:08:14 +01003811 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003812 if( status != PSA_SUCCESS )
3813 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003814
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003815#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003816 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003817 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003818 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003819 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003820 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003821 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003822 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003823
Gilles Peskineedf9a652018-08-17 18:11:56 +02003824 status = mbedtls_to_psa_error(
3825 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3826 ciphertext_length - operation.tag_length,
3827 nonce, nonce_length,
3828 additional_data,
3829 additional_data_length,
3830 tag, operation.tag_length,
3831 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003832 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003833 else
3834#endif /* MBEDTLS_GCM_C */
3835#if defined(MBEDTLS_CCM_C)
3836 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003837 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003838 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003839 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003840 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003841 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003842 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003843
Gilles Peskineedf9a652018-08-17 18:11:56 +02003844 status = mbedtls_to_psa_error(
3845 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3846 ciphertext_length - operation.tag_length,
3847 nonce, nonce_length,
3848 additional_data,
3849 additional_data_length,
3850 ciphertext, plaintext,
3851 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003852 }
mohammad160339574652018-06-01 04:39:53 -07003853 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003854#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003855 {
mohammad1603554faad2018-06-03 15:07:38 +03003856 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003857 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003858
Gilles Peskineedf9a652018-08-17 18:11:56 +02003859 if( status != PSA_SUCCESS && plaintext_size != 0 )
3860 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003861
Gilles Peskineedf9a652018-08-17 18:11:56 +02003862exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003863 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003864 if( status == PSA_SUCCESS )
3865 *plaintext_length = ciphertext_length - operation.tag_length;
3866 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003867}
3868
Gilles Peskinea0655c32018-04-30 17:06:50 +02003869
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003870
Gilles Peskine20035e32018-02-03 22:44:14 +01003871/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003872/* Generators */
3873/****************************************************************/
3874
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003875#define HKDF_STATE_INIT 0 /* no input yet */
3876#define HKDF_STATE_STARTED 1 /* got salt */
3877#define HKDF_STATE_KEYED 2 /* got key */
3878#define HKDF_STATE_OUTPUT 3 /* output started */
3879
Gilles Peskine969c5d62019-01-16 15:53:06 +01003880static psa_algorithm_t psa_generator_get_kdf_alg(
3881 const psa_crypto_generator_t *generator )
3882{
3883 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3884 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3885 else
3886 return( generator->alg );
3887}
3888
3889
Gilles Peskineeab56e42018-07-12 17:12:33 +02003890psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3891{
3892 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003893 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3894 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003895 {
3896 /* The object has (apparently) been initialized but it is not
3897 * in use. It's ok to call abort on such an object, and there's
3898 * nothing to do. */
3899 }
3900 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003901 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003902 {
3903 if( generator->ctx.buffer.data != NULL )
3904 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003905 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003906 generator->ctx.buffer.size );
3907 mbedtls_free( generator->ctx.buffer.data );
3908 }
3909 }
3910 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003911#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003912 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003913 {
3914 mbedtls_free( generator->ctx.hkdf.info );
3915 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3916 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003917 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00003918 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003919 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003920 {
3921 if( generator->ctx.tls12_prf.key != NULL )
3922 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003923 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003924 generator->ctx.tls12_prf.key_len );
3925 mbedtls_free( generator->ctx.tls12_prf.key );
3926 }
Hanno Becker580fba12018-11-13 20:50:45 +00003927
3928 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3929 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003930 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003931 generator->ctx.tls12_prf.Ai_with_seed_len );
3932 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3933 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003934 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003935 else
3936#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003937 {
3938 status = PSA_ERROR_BAD_STATE;
3939 }
3940 memset( generator, 0, sizeof( *generator ) );
3941 return( status );
3942}
3943
Gilles Peskineeab56e42018-07-12 17:12:33 +02003944psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3945 size_t *capacity)
3946{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003947 if( generator->alg == 0 )
3948 {
3949 /* This is a blank generator. */
3950 return PSA_ERROR_BAD_STATE;
3951 }
3952
Gilles Peskineeab56e42018-07-12 17:12:33 +02003953 *capacity = generator->capacity;
3954 return( PSA_SUCCESS );
3955}
3956
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003957psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
3958 size_t capacity )
3959{
3960 if( generator->alg == 0 )
3961 return( PSA_ERROR_BAD_STATE );
3962 if( capacity > generator->capacity )
3963 return( PSA_ERROR_INVALID_ARGUMENT );
3964 generator->capacity = capacity;
3965 return( PSA_SUCCESS );
3966}
3967
Gilles Peskinebef7f142018-07-12 17:22:21 +02003968#if defined(MBEDTLS_MD_C)
3969/* Read some bytes from an HKDF-based generator. This performs a chunk
3970 * of the expand phase of the HKDF algorithm. */
3971static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3972 psa_algorithm_t hash_alg,
3973 uint8_t *output,
3974 size_t output_length )
3975{
3976 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3977 psa_status_t status;
3978
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003979 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3980 return( PSA_ERROR_BAD_STATE );
3981 hkdf->state = HKDF_STATE_OUTPUT;
3982
Gilles Peskinebef7f142018-07-12 17:22:21 +02003983 while( output_length != 0 )
3984 {
3985 /* Copy what remains of the current block */
3986 uint8_t n = hash_length - hkdf->offset_in_block;
3987 if( n > output_length )
3988 n = (uint8_t) output_length;
3989 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3990 output += n;
3991 output_length -= n;
3992 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003993 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003994 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003995 /* We can't be wanting more output after block 0xff, otherwise
3996 * the capacity check in psa_generator_read() would have
3997 * prevented this call. It could happen only if the generator
3998 * object was corrupted or if this function is called directly
3999 * inside the library. */
4000 if( hkdf->block_number == 0xff )
4001 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004002
4003 /* We need a new block */
4004 ++hkdf->block_number;
4005 hkdf->offset_in_block = 0;
4006 status = psa_hmac_setup_internal( &hkdf->hmac,
4007 hkdf->prk, hash_length,
4008 hash_alg );
4009 if( status != PSA_SUCCESS )
4010 return( status );
4011 if( hkdf->block_number != 1 )
4012 {
4013 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4014 hkdf->output_block,
4015 hash_length );
4016 if( status != PSA_SUCCESS )
4017 return( status );
4018 }
4019 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4020 hkdf->info,
4021 hkdf->info_length );
4022 if( status != PSA_SUCCESS )
4023 return( status );
4024 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4025 &hkdf->block_number, 1 );
4026 if( status != PSA_SUCCESS )
4027 return( status );
4028 status = psa_hmac_finish_internal( &hkdf->hmac,
4029 hkdf->output_block,
4030 sizeof( hkdf->output_block ) );
4031 if( status != PSA_SUCCESS )
4032 return( status );
4033 }
4034
4035 return( PSA_SUCCESS );
4036}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004037
4038static psa_status_t psa_generator_tls12_prf_generate_next_block(
4039 psa_tls12_prf_generator_t *tls12_prf,
4040 psa_algorithm_t alg )
4041{
4042 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4043 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4044 psa_hmac_internal_data hmac;
4045 psa_status_t status, cleanup_status;
4046
Hanno Becker3b339e22018-11-13 20:56:14 +00004047 unsigned char *Ai;
4048 size_t Ai_len;
4049
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004050 /* We can't be wanting more output after block 0xff, otherwise
4051 * the capacity check in psa_generator_read() would have
4052 * prevented this call. It could happen only if the generator
4053 * object was corrupted or if this function is called directly
4054 * inside the library. */
4055 if( tls12_prf->block_number == 0xff )
4056 return( PSA_ERROR_BAD_STATE );
4057
4058 /* We need a new block */
4059 ++tls12_prf->block_number;
4060 tls12_prf->offset_in_block = 0;
4061
4062 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4063 *
4064 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4065 *
4066 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4067 * HMAC_hash(secret, A(2) + seed) +
4068 * HMAC_hash(secret, A(3) + seed) + ...
4069 *
4070 * A(0) = seed
4071 * A(i) = HMAC_hash( secret, A(i-1) )
4072 *
4073 * The `psa_tls12_prf_generator` structures saves the block
4074 * `HMAC_hash(secret, A(i) + seed)` from which the output
4075 * is currently extracted as `output_block`, while
4076 * `A(i) + seed` is stored in `Ai_with_seed`.
4077 *
4078 * Generating a new block means recalculating `Ai_with_seed`
4079 * from the A(i)-part of it, and afterwards recalculating
4080 * `output_block`.
4081 *
4082 * A(0) is computed at setup time.
4083 *
4084 */
4085
4086 psa_hmac_init_internal( &hmac );
4087
4088 /* We must distinguish the calculation of A(1) from those
4089 * of A(2) and higher, because A(0)=seed has a different
4090 * length than the other A(i). */
4091 if( tls12_prf->block_number == 1 )
4092 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004093 Ai = tls12_prf->Ai_with_seed + hash_length;
4094 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004095 }
4096 else
4097 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004098 Ai = tls12_prf->Ai_with_seed;
4099 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004100 }
4101
Hanno Becker3b339e22018-11-13 20:56:14 +00004102 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4103 status = psa_hmac_setup_internal( &hmac,
4104 tls12_prf->key,
4105 tls12_prf->key_len,
4106 hash_alg );
4107 if( status != PSA_SUCCESS )
4108 goto cleanup;
4109
4110 status = psa_hash_update( &hmac.hash_ctx,
4111 Ai, Ai_len );
4112 if( status != PSA_SUCCESS )
4113 goto cleanup;
4114
4115 status = psa_hmac_finish_internal( &hmac,
4116 tls12_prf->Ai_with_seed,
4117 hash_length );
4118 if( status != PSA_SUCCESS )
4119 goto cleanup;
4120
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004121 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4122 status = psa_hmac_setup_internal( &hmac,
4123 tls12_prf->key,
4124 tls12_prf->key_len,
4125 hash_alg );
4126 if( status != PSA_SUCCESS )
4127 goto cleanup;
4128
4129 status = psa_hash_update( &hmac.hash_ctx,
4130 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004131 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004132 if( status != PSA_SUCCESS )
4133 goto cleanup;
4134
4135 status = psa_hmac_finish_internal( &hmac,
4136 tls12_prf->output_block,
4137 hash_length );
4138 if( status != PSA_SUCCESS )
4139 goto cleanup;
4140
4141cleanup:
4142
4143 cleanup_status = psa_hmac_abort_internal( &hmac );
4144 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4145 status = cleanup_status;
4146
4147 return( status );
4148}
4149
4150/* Read some bytes from an TLS-1.2-PRF-based generator.
4151 * See Section 5 of RFC 5246. */
4152static psa_status_t psa_generator_tls12_prf_read(
4153 psa_tls12_prf_generator_t *tls12_prf,
4154 psa_algorithm_t alg,
4155 uint8_t *output,
4156 size_t output_length )
4157{
4158 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4159 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4160 psa_status_t status;
4161
4162 while( output_length != 0 )
4163 {
4164 /* Copy what remains of the current block */
4165 uint8_t n = hash_length - tls12_prf->offset_in_block;
4166
4167 /* Check if we have fully processed the current block. */
4168 if( n == 0 )
4169 {
4170 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
4171 alg );
4172 if( status != PSA_SUCCESS )
4173 return( status );
4174
4175 continue;
4176 }
4177
4178 if( n > output_length )
4179 n = (uint8_t) output_length;
4180 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4181 n );
4182 output += n;
4183 output_length -= n;
4184 tls12_prf->offset_in_block += n;
4185 }
4186
4187 return( PSA_SUCCESS );
4188}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004189#endif /* MBEDTLS_MD_C */
4190
Gilles Peskineeab56e42018-07-12 17:12:33 +02004191psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
4192 uint8_t *output,
4193 size_t output_length )
4194{
4195 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004196 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004197
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004198 if( generator->alg == 0 )
4199 {
4200 /* This is a blank generator. */
4201 return PSA_ERROR_BAD_STATE;
4202 }
4203
Gilles Peskineeab56e42018-07-12 17:12:33 +02004204 if( output_length > generator->capacity )
4205 {
4206 generator->capacity = 0;
4207 /* Go through the error path to wipe all confidential data now
4208 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004209 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004210 goto exit;
4211 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004212 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004213 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004214 /* Edge case: this is a finished generator, and 0 bytes
4215 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004216 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4217 * INSUFFICIENT_CAPACITY, which is right for a finished
4218 * generator, for consistency with the case when
4219 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004220 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004221 }
4222 generator->capacity -= output_length;
4223
Gilles Peskine969c5d62019-01-16 15:53:06 +01004224 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004225 {
Gilles Peskine211a4362018-10-25 22:22:31 +02004226 /* Initially, the capacity of a selection generator is always
4227 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
4228 * abbreviated in this comment as `size`. When the remaining
4229 * capacity is `c`, the next bytes to serve start `c` bytes
4230 * from the end of the buffer, i.e. `size - c` from the
4231 * beginning of the buffer. Since `generator->capacity` was just
4232 * decremented above, we need to serve the bytes from
4233 * `size - generator->capacity - output_length` to
4234 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004235 size_t offset =
4236 generator->ctx.buffer.size - generator->capacity - output_length;
4237 memcpy( output, generator->ctx.buffer.data + offset, output_length );
4238 status = PSA_SUCCESS;
4239 }
4240 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004241#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004242 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004243 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004244 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004245 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
4246 output, output_length );
4247 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004248 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4249 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004250 {
4251 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004252 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004253 output_length );
4254 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004255 else
4256#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004257 {
4258 return( PSA_ERROR_BAD_STATE );
4259 }
4260
4261exit:
4262 if( status != PSA_SUCCESS )
4263 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004264 /* Preserve the algorithm upon errors, but clear all sensitive state.
4265 * This allows us to differentiate between exhausted generators and
4266 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
4267 * generators. */
4268 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004269 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004270 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004271 memset( output, '!', output_length );
4272 }
4273 return( status );
4274}
4275
Gilles Peskine08542d82018-07-19 17:05:42 +02004276#if defined(MBEDTLS_DES_C)
4277static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4278{
4279 if( data_size >= 8 )
4280 mbedtls_des_key_set_parity( data );
4281 if( data_size >= 16 )
4282 mbedtls_des_key_set_parity( data + 8 );
4283 if( data_size >= 24 )
4284 mbedtls_des_key_set_parity( data + 16 );
4285}
4286#endif /* MBEDTLS_DES_C */
4287
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004288static psa_status_t psa_generator_import_key_internal(
4289 psa_key_slot_t *slot,
4290 size_t bits,
4291 psa_crypto_generator_t *generator )
4292{
4293 uint8_t *data = NULL;
4294 size_t bytes = PSA_BITS_TO_BYTES( bits );
4295 psa_status_t status;
4296
4297 if( ! key_type_is_raw_bytes( slot->type ) )
4298 return( PSA_ERROR_INVALID_ARGUMENT );
4299 if( bits % 8 != 0 )
4300 return( PSA_ERROR_INVALID_ARGUMENT );
4301 data = mbedtls_calloc( 1, bytes );
4302 if( data == NULL )
4303 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4304
4305 status = psa_generator_read( generator, data, bytes );
4306 if( status != PSA_SUCCESS )
4307 goto exit;
4308#if defined(MBEDTLS_DES_C)
4309 if( slot->type == PSA_KEY_TYPE_DES )
4310 psa_des_set_key_parity( data, bytes );
4311#endif /* MBEDTLS_DES_C */
4312 status = psa_import_key_into_slot( slot, data, bytes );
4313
4314exit:
4315 mbedtls_free( data );
4316 return( status );
4317}
4318
4319psa_status_t psa_generator_import_key( const psa_key_attributes_t *attributes,
4320 psa_key_handle_t *handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004321 psa_crypto_generator_t *generator )
4322{
4323 psa_status_t status;
4324 psa_key_slot_t *slot = NULL;
4325 status = psa_start_key_creation( attributes, handle, &slot );
4326 if( status == PSA_SUCCESS )
4327 {
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004328 status = psa_generator_import_key_internal( slot,
4329 attributes->bits,
4330 generator );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004331 }
4332 if( status == PSA_SUCCESS )
4333 status = psa_finish_key_creation( slot );
4334 if( status != PSA_SUCCESS )
4335 {
4336 psa_fail_key_creation( slot );
4337 *handle = 0;
4338 }
4339 return( status );
4340}
4341
Gilles Peskine87a5e562019-04-17 12:28:25 +02004342psa_status_t psa_generator_import_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004343 psa_key_type_t type,
4344 size_t bits,
4345 psa_crypto_generator_t *generator )
4346{
4347 uint8_t *data = NULL;
4348 size_t bytes = PSA_BITS_TO_BYTES( bits );
4349 psa_status_t status;
4350
4351 if( ! key_type_is_raw_bytes( type ) )
4352 return( PSA_ERROR_INVALID_ARGUMENT );
4353 if( bits % 8 != 0 )
4354 return( PSA_ERROR_INVALID_ARGUMENT );
4355 data = mbedtls_calloc( 1, bytes );
4356 if( data == NULL )
4357 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4358
4359 status = psa_generator_read( generator, data, bytes );
4360 if( status != PSA_SUCCESS )
4361 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004362#if defined(MBEDTLS_DES_C)
4363 if( type == PSA_KEY_TYPE_DES )
4364 psa_des_set_key_parity( data, bytes );
4365#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004366 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004367
4368exit:
4369 mbedtls_free( data );
4370 return( status );
4371}
4372
4373
4374
4375/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004376/* Key derivation */
4377/****************************************************************/
4378
Gilles Peskinea05219c2018-11-16 16:02:56 +01004379#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004380/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004381 * of the HKDF algorithm.
4382 *
4383 * Note that if this function fails, you must call psa_generator_abort()
4384 * to potentially free embedded data structures and wipe confidential data.
4385 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004386static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004387 const uint8_t *secret,
4388 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004389 psa_algorithm_t hash_alg,
4390 const uint8_t *salt,
4391 size_t salt_length,
4392 const uint8_t *label,
4393 size_t label_length )
4394{
4395 psa_status_t status;
4396 status = psa_hmac_setup_internal( &hkdf->hmac,
4397 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004398 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004399 if( status != PSA_SUCCESS )
4400 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004401 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004402 if( status != PSA_SUCCESS )
4403 return( status );
4404 status = psa_hmac_finish_internal( &hkdf->hmac,
4405 hkdf->prk,
4406 sizeof( hkdf->prk ) );
4407 if( status != PSA_SUCCESS )
4408 return( status );
4409 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4410 hkdf->block_number = 0;
4411 hkdf->info_length = label_length;
4412 if( label_length != 0 )
4413 {
4414 hkdf->info = mbedtls_calloc( 1, label_length );
4415 if( hkdf->info == NULL )
4416 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4417 memcpy( hkdf->info, label, label_length );
4418 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004419 hkdf->state = HKDF_STATE_KEYED;
4420 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004421 return( PSA_SUCCESS );
4422}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004423#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004424
Gilles Peskinea05219c2018-11-16 16:02:56 +01004425#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004426/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4427 *
4428 * Note that if this function fails, you must call psa_generator_abort()
4429 * to potentially free embedded data structures and wipe confidential data.
4430 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004431static psa_status_t psa_generator_tls12_prf_setup(
4432 psa_tls12_prf_generator_t *tls12_prf,
4433 const unsigned char *key,
4434 size_t key_len,
4435 psa_algorithm_t hash_alg,
4436 const uint8_t *salt,
4437 size_t salt_length,
4438 const uint8_t *label,
4439 size_t label_length )
4440{
4441 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004442 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4443 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004444
4445 tls12_prf->key = mbedtls_calloc( 1, key_len );
4446 if( tls12_prf->key == NULL )
4447 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4448 tls12_prf->key_len = key_len;
4449 memcpy( tls12_prf->key, key, key_len );
4450
Hanno Becker580fba12018-11-13 20:50:45 +00004451 overflow = ( salt_length + label_length < salt_length ) ||
4452 ( salt_length + label_length + hash_length < hash_length );
4453 if( overflow )
4454 return( PSA_ERROR_INVALID_ARGUMENT );
4455
4456 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4457 if( tls12_prf->Ai_with_seed == NULL )
4458 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4459 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4460
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004461 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4462 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004463 if( label_length != 0 )
4464 {
4465 memcpy( tls12_prf->Ai_with_seed + hash_length,
4466 label, label_length );
4467 }
4468
4469 if( salt_length != 0 )
4470 {
4471 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4472 salt, salt_length );
4473 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004474
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004475 /* The first block gets generated when
4476 * psa_generator_read() is called. */
4477 tls12_prf->block_number = 0;
4478 tls12_prf->offset_in_block = hash_length;
4479
4480 return( PSA_SUCCESS );
4481}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004482
4483/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4484static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4485 psa_tls12_prf_generator_t *tls12_prf,
4486 const unsigned char *psk,
4487 size_t psk_len,
4488 psa_algorithm_t hash_alg,
4489 const uint8_t *salt,
4490 size_t salt_length,
4491 const uint8_t *label,
4492 size_t label_length )
4493{
4494 psa_status_t status;
4495 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4496
4497 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4498 return( PSA_ERROR_INVALID_ARGUMENT );
4499
4500 /* Quoting RFC 4279, Section 2:
4501 *
4502 * The premaster secret is formed as follows: if the PSK is N octets
4503 * long, concatenate a uint16 with the value N, N zero octets, a second
4504 * uint16 with the value N, and the PSK itself.
4505 */
4506
4507 pms[0] = ( psk_len >> 8 ) & 0xff;
4508 pms[1] = ( psk_len >> 0 ) & 0xff;
4509 memset( pms + 2, 0, psk_len );
4510 pms[2 + psk_len + 0] = pms[0];
4511 pms[2 + psk_len + 1] = pms[1];
4512 memcpy( pms + 4 + psk_len, psk, psk_len );
4513
4514 status = psa_generator_tls12_prf_setup( tls12_prf,
4515 pms, 4 + 2 * psk_len,
4516 hash_alg,
4517 salt, salt_length,
4518 label, label_length );
4519
Gilles Peskine3f108122018-12-07 18:14:53 +01004520 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004521 return( status );
4522}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004523#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004524
Gilles Peskine346797d2018-11-16 16:05:06 +01004525/* Note that if this function fails, you must call psa_generator_abort()
4526 * to potentially free embedded data structures and wipe confidential data.
4527 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004528static psa_status_t psa_key_derivation_internal(
4529 psa_crypto_generator_t *generator,
4530 const uint8_t *secret, size_t secret_length,
4531 psa_algorithm_t alg,
4532 const uint8_t *salt, size_t salt_length,
4533 const uint8_t *label, size_t label_length,
4534 size_t capacity )
4535{
4536 psa_status_t status;
4537 size_t max_capacity;
4538
4539 /* Set generator->alg even on failure so that abort knows what to do. */
4540 generator->alg = alg;
4541
Gilles Peskine751d9652018-09-18 12:05:44 +02004542 if( alg == PSA_ALG_SELECT_RAW )
4543 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004544 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004545 if( salt_length != 0 )
4546 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004547 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004548 if( label_length != 0 )
4549 return( PSA_ERROR_INVALID_ARGUMENT );
4550 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4551 if( generator->ctx.buffer.data == NULL )
4552 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4553 memcpy( generator->ctx.buffer.data, secret, secret_length );
4554 generator->ctx.buffer.size = secret_length;
4555 max_capacity = secret_length;
4556 status = PSA_SUCCESS;
4557 }
4558 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004559#if defined(MBEDTLS_MD_C)
4560 if( PSA_ALG_IS_HKDF( alg ) )
4561 {
4562 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4563 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4564 if( hash_size == 0 )
4565 return( PSA_ERROR_NOT_SUPPORTED );
4566 max_capacity = 255 * hash_size;
4567 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4568 secret, secret_length,
4569 hash_alg,
4570 salt, salt_length,
4571 label, label_length );
4572 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004573 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4574 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4575 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004576 {
4577 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4578 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4579
4580 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4581 if( hash_alg != PSA_ALG_SHA_256 &&
4582 hash_alg != PSA_ALG_SHA_384 )
4583 {
4584 return( PSA_ERROR_NOT_SUPPORTED );
4585 }
4586
4587 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004588
4589 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4590 {
4591 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4592 secret, secret_length,
4593 hash_alg, salt, salt_length,
4594 label, label_length );
4595 }
4596 else
4597 {
4598 status = psa_generator_tls12_psk_to_ms_setup(
4599 &generator->ctx.tls12_prf,
4600 secret, secret_length,
4601 hash_alg, salt, salt_length,
4602 label, label_length );
4603 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004604 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004605 else
4606#endif
4607 {
4608 return( PSA_ERROR_NOT_SUPPORTED );
4609 }
4610
4611 if( status != PSA_SUCCESS )
4612 return( status );
4613
4614 if( capacity <= max_capacity )
4615 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004616 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4617 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004618 else
4619 return( PSA_ERROR_INVALID_ARGUMENT );
4620
4621 return( PSA_SUCCESS );
4622}
4623
Gilles Peskineea0fb492018-07-12 17:17:20 +02004624psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004625 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004626 psa_algorithm_t alg,
4627 const uint8_t *salt,
4628 size_t salt_length,
4629 const uint8_t *label,
4630 size_t label_length,
4631 size_t capacity )
4632{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004633 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004634 psa_status_t status;
4635
4636 if( generator->alg != 0 )
4637 return( PSA_ERROR_BAD_STATE );
4638
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004639 /* Make sure that alg is a key derivation algorithm. This prevents
4640 * key selection algorithms, which psa_key_derivation_internal
4641 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004642 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4643 return( PSA_ERROR_INVALID_ARGUMENT );
4644
Gilles Peskinec5487a82018-12-03 18:08:14 +01004645 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004646 if( status != PSA_SUCCESS )
4647 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004648
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004649 if( slot->type != PSA_KEY_TYPE_DERIVE )
4650 return( PSA_ERROR_INVALID_ARGUMENT );
4651
4652 status = psa_key_derivation_internal( generator,
4653 slot->data.raw.data,
4654 slot->data.raw.bytes,
4655 alg,
4656 salt, salt_length,
4657 label, label_length,
4658 capacity );
4659 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004660 psa_generator_abort( generator );
4661 return( status );
4662}
4663
Gilles Peskine969c5d62019-01-16 15:53:06 +01004664static psa_status_t psa_key_derivation_setup_kdf(
4665 psa_crypto_generator_t *generator,
4666 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004667{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004668 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004669#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004670 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4671 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4672 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004673 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004674 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004675 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4676 if( hash_size == 0 )
4677 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004678 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4679 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004680 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004681 {
4682 return( PSA_ERROR_NOT_SUPPORTED );
4683 }
4684 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004685 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004686 }
4687#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004688 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004689 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004690}
4691
4692psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4693 psa_algorithm_t alg )
4694{
4695 psa_status_t status;
4696
4697 if( generator->alg != 0 )
4698 return( PSA_ERROR_BAD_STATE );
4699
Gilles Peskine6843c292019-01-18 16:44:49 +01004700 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4701 return( PSA_ERROR_INVALID_ARGUMENT );
4702 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004703 {
4704 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004705 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004706 }
4707 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4708 {
4709 status = psa_key_derivation_setup_kdf( generator, alg );
4710 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004711 else
4712 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004713
4714 if( status == PSA_SUCCESS )
4715 generator->alg = alg;
4716 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004717}
4718
4719#if defined(MBEDTLS_MD_C)
4720static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4721 psa_algorithm_t hash_alg,
4722 psa_key_derivation_step_t step,
4723 const uint8_t *data,
4724 size_t data_length )
4725{
4726 psa_status_t status;
4727 switch( step )
4728 {
4729 case PSA_KDF_STEP_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004730 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004731 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004732 status = psa_hmac_setup_internal( &hkdf->hmac,
4733 data, data_length,
4734 hash_alg );
4735 if( status != PSA_SUCCESS )
4736 return( status );
4737 hkdf->state = HKDF_STATE_STARTED;
4738 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004739 case PSA_KDF_STEP_SECRET:
4740 /* If no salt was provided, use an empty salt. */
4741 if( hkdf->state == HKDF_STATE_INIT )
4742 {
4743 status = psa_hmac_setup_internal( &hkdf->hmac,
4744 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004745 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004746 if( status != PSA_SUCCESS )
4747 return( status );
4748 hkdf->state = HKDF_STATE_STARTED;
4749 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004750 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004751 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004752 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4753 data, data_length );
4754 if( status != PSA_SUCCESS )
4755 return( status );
4756 status = psa_hmac_finish_internal( &hkdf->hmac,
4757 hkdf->prk,
4758 sizeof( hkdf->prk ) );
4759 if( status != PSA_SUCCESS )
4760 return( status );
4761 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4762 hkdf->block_number = 0;
4763 hkdf->state = HKDF_STATE_KEYED;
4764 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004765 case PSA_KDF_STEP_INFO:
4766 if( hkdf->state == HKDF_STATE_OUTPUT )
4767 return( PSA_ERROR_BAD_STATE );
4768 if( hkdf->info_set )
4769 return( PSA_ERROR_BAD_STATE );
4770 hkdf->info_length = data_length;
4771 if( data_length != 0 )
4772 {
4773 hkdf->info = mbedtls_calloc( 1, data_length );
4774 if( hkdf->info == NULL )
4775 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4776 memcpy( hkdf->info, data, data_length );
4777 }
4778 hkdf->info_set = 1;
4779 return( PSA_SUCCESS );
4780 default:
4781 return( PSA_ERROR_INVALID_ARGUMENT );
4782 }
4783}
4784#endif /* MBEDTLS_MD_C */
4785
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004786static psa_status_t psa_key_derivation_input_raw(
4787 psa_crypto_generator_t *generator,
4788 psa_key_derivation_step_t step,
4789 const uint8_t *data,
4790 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004791{
4792 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004793 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004794
Gilles Peskine969c5d62019-01-16 15:53:06 +01004795 if( kdf_alg == PSA_ALG_SELECT_RAW )
4796 {
4797 if( generator->capacity != 0 )
4798 return( PSA_ERROR_INVALID_ARGUMENT );
4799 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4800 if( generator->ctx.buffer.data == NULL )
4801 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4802 memcpy( generator->ctx.buffer.data, data, data_length );
4803 generator->ctx.buffer.size = data_length;
4804 generator->capacity = data_length;
4805 status = PSA_SUCCESS;
4806 }
4807 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004808#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004809 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004810 {
4811 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004812 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004813 step, data, data_length );
4814 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004815 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004816#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004817#if defined(MBEDTLS_MD_C)
4818 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004819 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4820 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004821 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004822 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004823 status = PSA_ERROR_NOT_SUPPORTED;
4824 }
4825 else
4826#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004827 {
4828 /* This can't happen unless the generator object was not initialized */
4829 return( PSA_ERROR_BAD_STATE );
4830 }
4831
4832 if( status != PSA_SUCCESS )
4833 psa_generator_abort( generator );
4834 return( status );
4835}
4836
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004837psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4838 psa_key_derivation_step_t step,
4839 const uint8_t *data,
4840 size_t data_length )
4841{
4842 switch( step )
4843 {
4844 case PSA_KDF_STEP_LABEL:
4845 case PSA_KDF_STEP_SALT:
4846 case PSA_KDF_STEP_INFO:
4847 return( psa_key_derivation_input_raw( generator, step,
4848 data, data_length ) );
4849 default:
4850 return( PSA_ERROR_INVALID_ARGUMENT );
4851 }
4852}
4853
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004854psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4855 psa_key_derivation_step_t step,
4856 psa_key_handle_t handle )
4857{
4858 psa_key_slot_t *slot;
4859 psa_status_t status;
4860 status = psa_get_key_from_slot( handle, &slot,
4861 PSA_KEY_USAGE_DERIVE,
4862 generator->alg );
4863 if( status != PSA_SUCCESS )
4864 return( status );
4865 if( slot->type != PSA_KEY_TYPE_DERIVE )
4866 return( PSA_ERROR_INVALID_ARGUMENT );
4867 /* Don't allow a key to be used as an input that is usually public.
4868 * This is debatable. It's ok from a cryptographic perspective to
4869 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004870 * the material should be dedicated to a particular input step,
4871 * otherwise this may allow the key to be used in an unintended way
4872 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004873 if( step != PSA_KDF_STEP_SECRET )
4874 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004875 return( psa_key_derivation_input_raw( generator,
4876 step,
4877 slot->data.raw.data,
4878 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004879}
4880
Gilles Peskineea0fb492018-07-12 17:17:20 +02004881
4882
4883/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004884/* Key agreement */
4885/****************************************************************/
4886
Gilles Peskinea05219c2018-11-16 16:02:56 +01004887#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004888static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4889 size_t peer_key_length,
4890 const mbedtls_ecp_keypair *our_key,
4891 uint8_t *shared_secret,
4892 size_t shared_secret_size,
4893 size_t *shared_secret_length )
4894{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004895 mbedtls_ecp_keypair *their_key = NULL;
4896 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004897 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004898 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004899
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004900 status = psa_import_ec_public_key(
4901 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4902 peer_key, peer_key_length,
4903 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004904 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004905 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004906
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004907 status = mbedtls_to_psa_error(
4908 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4909 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004910 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004911 status = mbedtls_to_psa_error(
4912 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4913 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004914 goto exit;
4915
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004916 status = mbedtls_to_psa_error(
4917 mbedtls_ecdh_calc_secret( &ecdh,
4918 shared_secret_length,
4919 shared_secret, shared_secret_size,
4920 mbedtls_ctr_drbg_random,
4921 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004922
4923exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004924 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004925 mbedtls_ecp_keypair_free( their_key );
4926 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004927 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004928}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004929#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004930
Gilles Peskine01d718c2018-09-18 12:01:02 +02004931#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4932
Gilles Peskine0216fe12019-04-11 21:23:21 +02004933static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4934 psa_key_slot_t *private_key,
4935 const uint8_t *peer_key,
4936 size_t peer_key_length,
4937 uint8_t *shared_secret,
4938 size_t shared_secret_size,
4939 size_t *shared_secret_length )
4940{
4941 switch( alg )
4942 {
4943#if defined(MBEDTLS_ECDH_C)
4944 case PSA_ALG_ECDH:
4945 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4946 return( PSA_ERROR_INVALID_ARGUMENT );
4947 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
4948 private_key->data.ecp,
4949 shared_secret, shared_secret_size,
4950 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004951#endif /* MBEDTLS_ECDH_C */
4952 default:
4953 (void) private_key;
4954 (void) peer_key;
4955 (void) peer_key_length;
4956 (void) shared_secret;
4957 (void) shared_secret_size;
4958 (void) shared_secret_length;
4959 return( PSA_ERROR_NOT_SUPPORTED );
4960 }
4961}
4962
Gilles Peskine346797d2018-11-16 16:05:06 +01004963/* Note that if this function fails, you must call psa_generator_abort()
4964 * to potentially free embedded data structures and wipe confidential data.
4965 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004966static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004967 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004968 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004969 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004970 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004971{
4972 psa_status_t status;
4973 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4974 size_t shared_secret_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004975 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004976
4977 /* Step 1: run the secret agreement algorithm to generate the shared
4978 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004979 status = psa_key_agreement_raw_internal( ka_alg,
4980 private_key,
4981 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004982 shared_secret,
4983 sizeof( shared_secret ),
4984 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004985 if( status != PSA_SUCCESS )
4986 goto exit;
4987
4988 /* Step 2: set up the key derivation to generate key material from
4989 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004990 status = psa_key_derivation_input_raw( generator, step,
4991 shared_secret, shared_secret_length );
4992
Gilles Peskine01d718c2018-09-18 12:01:02 +02004993exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004994 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004995 return( status );
4996}
4997
4998psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004999 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005000 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005001 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005002 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005003{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005004 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005005 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005006 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005007 return( PSA_ERROR_INVALID_ARGUMENT );
5008 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005009 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005010 if( status != PSA_SUCCESS )
5011 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005012 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005013 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005014 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005015 if( status != PSA_SUCCESS )
5016 psa_generator_abort( generator );
5017 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005018}
5019
Gilles Peskine0216fe12019-04-11 21:23:21 +02005020psa_status_t psa_key_agreement_raw_shared_secret( psa_algorithm_t alg,
5021 psa_key_handle_t private_key,
5022 const uint8_t *peer_key,
5023 size_t peer_key_length,
5024 uint8_t *output,
5025 size_t output_size,
5026 size_t *output_length )
5027{
5028 psa_key_slot_t *slot;
5029 psa_status_t status;
5030
5031 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5032 {
5033 status = PSA_ERROR_INVALID_ARGUMENT;
5034 goto exit;
5035 }
5036 status = psa_get_key_from_slot( private_key, &slot,
5037 PSA_KEY_USAGE_DERIVE, alg );
5038 if( status != PSA_SUCCESS )
5039 goto exit;
5040
5041 status = psa_key_agreement_raw_internal( alg, slot,
5042 peer_key, peer_key_length,
5043 output, output_size,
5044 output_length );
5045
5046exit:
5047 if( status != PSA_SUCCESS )
5048 {
5049 /* If an error happens and is not handled properly, the output
5050 * may be used as a key to protect sensitive data. Arrange for such
5051 * a key to be random, which is likely to result in decryption or
5052 * verification errors. This is better than filling the buffer with
5053 * some constant data such as zeros, which would result in the data
5054 * being protected with a reproducible, easily knowable key.
5055 */
5056 psa_generate_random( output, output_size );
5057 *output_length = output_size;
5058 }
5059 return( status );
5060}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005061
5062
5063/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005064/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005065/****************************************************************/
5066
5067psa_status_t psa_generate_random( uint8_t *output,
5068 size_t output_size )
5069{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005070 int ret;
5071 GUARD_MODULE_INITIALIZED;
5072
5073 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005074 return( mbedtls_to_psa_error( ret ) );
5075}
5076
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005077#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5078#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005079
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005080psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5081 size_t seed_size )
5082{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005083 if( global_data.initialized )
5084 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005085
5086 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5087 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5088 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5089 return( PSA_ERROR_INVALID_ARGUMENT );
5090
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005091 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005092}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005093#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005094
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005095static psa_status_t psa_generate_key_internal( psa_key_slot_t *slot,
5096 size_t bits,
5097 const void *extra,
5098 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005099{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005100 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005101
Gilles Peskine53d991e2018-07-12 01:14:59 +02005102 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005103 return( PSA_ERROR_INVALID_ARGUMENT );
5104
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005105 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005106 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005107 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005108 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005109 if( status != PSA_SUCCESS )
5110 return( status );
5111 status = psa_generate_random( slot->data.raw.data,
5112 slot->data.raw.bytes );
5113 if( status != PSA_SUCCESS )
5114 {
5115 mbedtls_free( slot->data.raw.data );
5116 return( status );
5117 }
5118#if defined(MBEDTLS_DES_C)
5119 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005120 psa_des_set_key_parity( slot->data.raw.data,
5121 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005122#endif /* MBEDTLS_DES_C */
5123 }
5124 else
5125
5126#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5127 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
5128 {
5129 mbedtls_rsa_context *rsa;
5130 int ret;
5131 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005132 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5133 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005134 /* Accept only byte-aligned keys, for the same reasons as
5135 * in psa_import_rsa_key(). */
5136 if( bits % 8 != 0 )
5137 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005138 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005139 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02005140 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02005141 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005142 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02005143#if INT_MAX < 0xffffffff
5144 /* Check that the uint32_t value passed by the caller fits
5145 * in the range supported by this implementation. */
5146 if( p->e > INT_MAX )
5147 return( PSA_ERROR_NOT_SUPPORTED );
5148#endif
5149 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005150 }
5151 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5152 if( rsa == NULL )
5153 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5154 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5155 ret = mbedtls_rsa_gen_key( rsa,
5156 mbedtls_ctr_drbg_random,
5157 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005158 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005159 exponent );
5160 if( ret != 0 )
5161 {
5162 mbedtls_rsa_free( rsa );
5163 mbedtls_free( rsa );
5164 return( mbedtls_to_psa_error( ret ) );
5165 }
5166 slot->data.rsa = rsa;
5167 }
5168 else
5169#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5170
5171#if defined(MBEDTLS_ECP_C)
5172 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
5173 {
5174 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5175 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5176 const mbedtls_ecp_curve_info *curve_info =
5177 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5178 mbedtls_ecp_keypair *ecp;
5179 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02005180 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005181 return( PSA_ERROR_NOT_SUPPORTED );
5182 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5183 return( PSA_ERROR_NOT_SUPPORTED );
5184 if( curve_info->bit_size != bits )
5185 return( PSA_ERROR_INVALID_ARGUMENT );
5186 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5187 if( ecp == NULL )
5188 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5189 mbedtls_ecp_keypair_init( ecp );
5190 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5191 mbedtls_ctr_drbg_random,
5192 &global_data.ctr_drbg );
5193 if( ret != 0 )
5194 {
5195 mbedtls_ecp_keypair_free( ecp );
5196 mbedtls_free( ecp );
5197 return( mbedtls_to_psa_error( ret ) );
5198 }
5199 slot->data.ecp = ecp;
5200 }
5201 else
5202#endif /* MBEDTLS_ECP_C */
5203
5204 return( PSA_ERROR_NOT_SUPPORTED );
5205
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005206 return( PSA_SUCCESS );
5207}
5208
5209psa_status_t psa_generate_key_to_handle( psa_key_handle_t handle,
5210 psa_key_type_t type,
5211 size_t bits,
5212 const void *extra,
5213 size_t extra_size )
5214{
5215 psa_key_slot_t *slot;
5216 psa_status_t status;
5217
5218 status = psa_get_empty_key_slot( handle, &slot );
5219 if( status != PSA_SUCCESS )
5220 return( status );
5221
Gilles Peskine12313cd2018-06-20 00:20:32 +02005222 slot->type = type;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005223 status = psa_generate_key_internal( slot, bits, extra, extra_size );
5224 if( status != PSA_SUCCESS )
5225 slot->type = 0;
Darryl Green0c6575a2018-11-07 16:05:30 +00005226
5227#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
5228 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
5229 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01005230 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005231 }
5232#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5233
5234 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005235}
5236
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005237psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
5238 psa_key_handle_t *handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005239 const void *extra,
5240 size_t extra_size )
5241{
5242 psa_status_t status;
5243 psa_key_slot_t *slot = NULL;
5244 status = psa_start_key_creation( attributes, handle, &slot );
5245 if( status == PSA_SUCCESS )
5246 {
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005247 status = psa_generate_key_internal( slot, attributes->bits,
5248 extra, extra_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005249 }
5250 if( status == PSA_SUCCESS )
5251 status = psa_finish_key_creation( slot );
5252 if( status != PSA_SUCCESS )
5253 {
5254 psa_fail_key_creation( slot );
5255 *handle = 0;
5256 }
5257 return( status );
5258}
5259
5260
Gilles Peskine05d69892018-06-19 22:00:52 +02005261
5262/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005263/* Module setup */
5264/****************************************************************/
5265
Gilles Peskine5e769522018-11-20 21:59:56 +01005266psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5267 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5268 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5269{
5270 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5271 return( PSA_ERROR_BAD_STATE );
5272 global_data.entropy_init = entropy_init;
5273 global_data.entropy_free = entropy_free;
5274 return( PSA_SUCCESS );
5275}
5276
Gilles Peskinee59236f2018-01-27 23:32:46 +01005277void mbedtls_psa_crypto_free( void )
5278{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005279 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005280 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5281 {
5282 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005283 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005284 }
5285 /* Wipe all remaining data, including configuration.
5286 * In particular, this sets all state indicator to the value
5287 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005288 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005289}
5290
5291psa_status_t psa_crypto_init( void )
5292{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005293 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005294 const unsigned char drbg_seed[] = "PSA";
5295
Gilles Peskinec6b69072018-11-20 21:42:52 +01005296 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005297 if( global_data.initialized != 0 )
5298 return( PSA_SUCCESS );
5299
Gilles Peskine5e769522018-11-20 21:59:56 +01005300 /* Set default configuration if
5301 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5302 if( global_data.entropy_init == NULL )
5303 global_data.entropy_init = mbedtls_entropy_init;
5304 if( global_data.entropy_free == NULL )
5305 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005306
Gilles Peskinec6b69072018-11-20 21:42:52 +01005307 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005308 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005309 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005310 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005311 status = mbedtls_to_psa_error(
5312 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5313 mbedtls_entropy_func,
5314 &global_data.entropy,
5315 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5316 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005317 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005318 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005319
Gilles Peskine66fb1262018-12-10 16:29:04 +01005320 status = psa_initialize_key_slots( );
5321 if( status != PSA_SUCCESS )
5322 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005323
5324 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005325 global_data.initialized = 1;
5326
Gilles Peskinee59236f2018-01-27 23:32:46 +01005327exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005328 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005329 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005330 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005331}
5332
5333#endif /* MBEDTLS_PSA_CRYPTO_C */