blob: 45ce313d355648dd934bc61973fe64be3cb6015e [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>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020043#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#define mbedtls_calloc calloc
45#define mbedtls_free free
46#endif
47
Gilles Peskinea5905292018-02-07 20:59:33 +010048#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020049#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000050#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020051#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/blowfish.h"
53#include "mbedtls/camellia.h"
54#include "mbedtls/cipher.h"
55#include "mbedtls/ccm.h"
56#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010057#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020059#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010060#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010061#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/error.h"
63#include "mbedtls/gcm.h"
64#include "mbedtls/md2.h"
65#include "mbedtls/md4.h"
66#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010067#include "mbedtls/md.h"
68#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010069#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010071#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010073#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/sha1.h"
75#include "mbedtls/sha256.h"
76#include "mbedtls/sha512.h"
77#include "mbedtls/xtea.h"
78
Gilles Peskine996deb12018-08-01 15:45:45 +020079#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
80
Gilles Peskine9ef733f2018-02-07 21:05:37 +010081/* constant-time buffer comparison */
82static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
83{
84 size_t i;
85 unsigned char diff = 0;
86
87 for( i = 0; i < n; i++ )
88 diff |= a[i] ^ b[i];
89
90 return( diff );
91}
92
93
94
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010095/****************************************************************/
96/* Global data, support functions and library management */
97/****************************************************************/
98
Gilles Peskine48c0ea12018-06-21 14:15:31 +020099static int key_type_is_raw_bytes( psa_key_type_t type )
100{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200101 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102}
103
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100104/* Values for psa_global_data_t::rng_state */
105#define RNG_NOT_INITIALIZED 0
106#define RNG_INITIALIZED 1
107#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108
Gilles Peskine2d277862018-06-18 15:41:12 +0200109typedef struct
110{
Gilles Peskine5e769522018-11-20 21:59:56 +0100111 void (* entropy_init )( mbedtls_entropy_context *ctx );
112 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100113 mbedtls_entropy_context entropy;
114 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100115 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100116 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100117} psa_global_data_t;
118
119static psa_global_data_t global_data;
120
itayzafrir0adf0fc2018-09-06 16:24:41 +0300121#define GUARD_MODULE_INITIALIZED \
122 if( global_data.initialized == 0 ) \
123 return( PSA_ERROR_BAD_STATE );
124
Gilles Peskinee59236f2018-01-27 23:32:46 +0100125static psa_status_t mbedtls_to_psa_error( int ret )
126{
Gilles Peskinea5905292018-02-07 20:59:33 +0100127 /* If there's both a high-level code and low-level code, dispatch on
128 * the high-level code. */
129 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130 {
131 case 0:
132 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100133
134 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
135 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
136 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
137 return( PSA_ERROR_NOT_SUPPORTED );
138 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
139 return( PSA_ERROR_HARDWARE_FAILURE );
140
141 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
142 return( PSA_ERROR_HARDWARE_FAILURE );
143
Gilles Peskine9a944802018-06-21 09:35:35 +0200144 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
145 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
146 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
147 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
148 case MBEDTLS_ERR_ASN1_INVALID_DATA:
149 return( PSA_ERROR_INVALID_ARGUMENT );
150 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
151 return( PSA_ERROR_INSUFFICIENT_MEMORY );
152 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
153 return( PSA_ERROR_BUFFER_TOO_SMALL );
154
Jaeden Amero93e21112019-02-20 13:57:28 +0000155#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000156 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000157#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100158 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100160 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
161 return( PSA_ERROR_NOT_SUPPORTED );
162 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
163 return( PSA_ERROR_HARDWARE_FAILURE );
164
Jaeden Amero93e21112019-02-20 13:57:28 +0000165#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000166 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000167#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100168 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000169#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100170 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
171 return( PSA_ERROR_NOT_SUPPORTED );
172 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
173 return( PSA_ERROR_HARDWARE_FAILURE );
174
175 case MBEDTLS_ERR_CCM_BAD_INPUT:
176 return( PSA_ERROR_INVALID_ARGUMENT );
177 case MBEDTLS_ERR_CCM_AUTH_FAILED:
178 return( PSA_ERROR_INVALID_SIGNATURE );
179 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
182 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
183 return( PSA_ERROR_NOT_SUPPORTED );
184 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
185 return( PSA_ERROR_INVALID_ARGUMENT );
186 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
187 return( PSA_ERROR_INSUFFICIENT_MEMORY );
188 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
189 return( PSA_ERROR_INVALID_PADDING );
190 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
191 return( PSA_ERROR_BAD_STATE );
192 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
195 return( PSA_ERROR_TAMPERING_DETECTED );
196 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
197 return( PSA_ERROR_HARDWARE_FAILURE );
198
199 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
200 return( PSA_ERROR_HARDWARE_FAILURE );
201
202 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
203 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
204 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
205 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
206 return( PSA_ERROR_NOT_SUPPORTED );
207 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
208 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
209
210 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
211 return( PSA_ERROR_NOT_SUPPORTED );
212 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
213 return( PSA_ERROR_HARDWARE_FAILURE );
214
Gilles Peskinee59236f2018-01-27 23:32:46 +0100215 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
216 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
217 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100219
220 case MBEDTLS_ERR_GCM_AUTH_FAILED:
221 return( PSA_ERROR_INVALID_SIGNATURE );
222 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200223 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100224 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
225 return( PSA_ERROR_HARDWARE_FAILURE );
226
227 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
228 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
229 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
230 return( PSA_ERROR_HARDWARE_FAILURE );
231
232 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
233 return( PSA_ERROR_NOT_SUPPORTED );
234 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
235 return( PSA_ERROR_INVALID_ARGUMENT );
236 case MBEDTLS_ERR_MD_ALLOC_FAILED:
237 return( PSA_ERROR_INSUFFICIENT_MEMORY );
238 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
239 return( PSA_ERROR_STORAGE_FAILURE );
240 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
Gilles Peskinef76aa772018-10-29 19:24:33 +0100243 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
244 return( PSA_ERROR_STORAGE_FAILURE );
245 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
250 return( PSA_ERROR_BUFFER_TOO_SMALL );
251 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
252 return( PSA_ERROR_INVALID_ARGUMENT );
253 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
254 return( PSA_ERROR_INVALID_ARGUMENT );
255 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
256 return( PSA_ERROR_INVALID_ARGUMENT );
257 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
258 return( PSA_ERROR_INSUFFICIENT_MEMORY );
259
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100260 case MBEDTLS_ERR_PK_ALLOC_FAILED:
261 return( PSA_ERROR_INSUFFICIENT_MEMORY );
262 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
263 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100267 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
268 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
271 return( PSA_ERROR_NOT_SUPPORTED );
272 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
273 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
274 return( PSA_ERROR_NOT_PERMITTED );
275 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_PK_INVALID_ALG:
278 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
279 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
280 return( PSA_ERROR_NOT_SUPPORTED );
281 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
282 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100283 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
284 return( PSA_ERROR_HARDWARE_FAILURE );
285
Gilles Peskineff2d2002019-05-06 15:26:23 +0200286 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
287 return( PSA_ERROR_HARDWARE_FAILURE );
288 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
289 return( PSA_ERROR_NOT_SUPPORTED );
290
Gilles Peskinea5905292018-02-07 20:59:33 +0100291 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
292 return( PSA_ERROR_HARDWARE_FAILURE );
293
294 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_RSA_INVALID_PADDING:
297 return( PSA_ERROR_INVALID_PADDING );
298 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
301 return( PSA_ERROR_INVALID_ARGUMENT );
302 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
303 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
304 return( PSA_ERROR_TAMPERING_DETECTED );
305 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
306 return( PSA_ERROR_INVALID_SIGNATURE );
307 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
308 return( PSA_ERROR_BUFFER_TOO_SMALL );
309 case MBEDTLS_ERR_RSA_RNG_FAILED:
310 return( PSA_ERROR_INSUFFICIENT_MEMORY );
311 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
312 return( PSA_ERROR_NOT_SUPPORTED );
313 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315
316 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
317 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
318 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
319 return( PSA_ERROR_HARDWARE_FAILURE );
320
321 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
322 return( PSA_ERROR_INVALID_ARGUMENT );
323 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325
itayzafrir5c753392018-05-08 11:18:38 +0300326 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300328 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300329 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
330 return( PSA_ERROR_BUFFER_TOO_SMALL );
331 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
332 return( PSA_ERROR_NOT_SUPPORTED );
333 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
334 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
335 return( PSA_ERROR_INVALID_SIGNATURE );
336 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
337 return( PSA_ERROR_INSUFFICIENT_MEMORY );
338 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300340
Gilles Peskinee59236f2018-01-27 23:32:46 +0100341 default:
David Saadab4ecc272019-02-14 13:48:10 +0200342 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100343 }
344}
345
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200346
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200347
348
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100349/****************************************************************/
350/* Key management */
351/****************************************************************/
352
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100353#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200354static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
355{
356 switch( grpid )
357 {
358 case MBEDTLS_ECP_DP_SECP192R1:
359 return( PSA_ECC_CURVE_SECP192R1 );
360 case MBEDTLS_ECP_DP_SECP224R1:
361 return( PSA_ECC_CURVE_SECP224R1 );
362 case MBEDTLS_ECP_DP_SECP256R1:
363 return( PSA_ECC_CURVE_SECP256R1 );
364 case MBEDTLS_ECP_DP_SECP384R1:
365 return( PSA_ECC_CURVE_SECP384R1 );
366 case MBEDTLS_ECP_DP_SECP521R1:
367 return( PSA_ECC_CURVE_SECP521R1 );
368 case MBEDTLS_ECP_DP_BP256R1:
369 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
370 case MBEDTLS_ECP_DP_BP384R1:
371 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
372 case MBEDTLS_ECP_DP_BP512R1:
373 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
374 case MBEDTLS_ECP_DP_CURVE25519:
375 return( PSA_ECC_CURVE_CURVE25519 );
376 case MBEDTLS_ECP_DP_SECP192K1:
377 return( PSA_ECC_CURVE_SECP192K1 );
378 case MBEDTLS_ECP_DP_SECP224K1:
379 return( PSA_ECC_CURVE_SECP224K1 );
380 case MBEDTLS_ECP_DP_SECP256K1:
381 return( PSA_ECC_CURVE_SECP256K1 );
382 case MBEDTLS_ECP_DP_CURVE448:
383 return( PSA_ECC_CURVE_CURVE448 );
384 default:
385 return( 0 );
386 }
387}
388
Gilles Peskine12313cd2018-06-20 00:20:32 +0200389static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
390{
391 switch( curve )
392 {
393 case PSA_ECC_CURVE_SECP192R1:
394 return( MBEDTLS_ECP_DP_SECP192R1 );
395 case PSA_ECC_CURVE_SECP224R1:
396 return( MBEDTLS_ECP_DP_SECP224R1 );
397 case PSA_ECC_CURVE_SECP256R1:
398 return( MBEDTLS_ECP_DP_SECP256R1 );
399 case PSA_ECC_CURVE_SECP384R1:
400 return( MBEDTLS_ECP_DP_SECP384R1 );
401 case PSA_ECC_CURVE_SECP521R1:
402 return( MBEDTLS_ECP_DP_SECP521R1 );
403 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
404 return( MBEDTLS_ECP_DP_BP256R1 );
405 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
406 return( MBEDTLS_ECP_DP_BP384R1 );
407 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
408 return( MBEDTLS_ECP_DP_BP512R1 );
409 case PSA_ECC_CURVE_CURVE25519:
410 return( MBEDTLS_ECP_DP_CURVE25519 );
411 case PSA_ECC_CURVE_SECP192K1:
412 return( MBEDTLS_ECP_DP_SECP192K1 );
413 case PSA_ECC_CURVE_SECP224K1:
414 return( MBEDTLS_ECP_DP_SECP224K1 );
415 case PSA_ECC_CURVE_SECP256K1:
416 return( MBEDTLS_ECP_DP_SECP256K1 );
417 case PSA_ECC_CURVE_CURVE448:
418 return( MBEDTLS_ECP_DP_CURVE448 );
419 default:
420 return( MBEDTLS_ECP_DP_NONE );
421 }
422}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100423#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200424
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200425static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
426 size_t bits,
427 struct raw_data *raw )
428{
429 /* Check that the bit size is acceptable for the key type */
430 switch( type )
431 {
432 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200433 if( bits == 0 )
434 {
435 raw->bytes = 0;
436 raw->data = NULL;
437 return( PSA_SUCCESS );
438 }
439 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200440#if defined(MBEDTLS_MD_C)
441 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200442#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200443 case PSA_KEY_TYPE_DERIVE:
444 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445#if defined(MBEDTLS_AES_C)
446 case PSA_KEY_TYPE_AES:
447 if( bits != 128 && bits != 192 && bits != 256 )
448 return( PSA_ERROR_INVALID_ARGUMENT );
449 break;
450#endif
451#if defined(MBEDTLS_CAMELLIA_C)
452 case PSA_KEY_TYPE_CAMELLIA:
453 if( bits != 128 && bits != 192 && bits != 256 )
454 return( PSA_ERROR_INVALID_ARGUMENT );
455 break;
456#endif
457#if defined(MBEDTLS_DES_C)
458 case PSA_KEY_TYPE_DES:
459 if( bits != 64 && bits != 128 && bits != 192 )
460 return( PSA_ERROR_INVALID_ARGUMENT );
461 break;
462#endif
463#if defined(MBEDTLS_ARC4_C)
464 case PSA_KEY_TYPE_ARC4:
465 if( bits < 8 || bits > 2048 )
466 return( PSA_ERROR_INVALID_ARGUMENT );
467 break;
468#endif
469 default:
470 return( PSA_ERROR_NOT_SUPPORTED );
471 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200472 if( bits % 8 != 0 )
473 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200474
475 /* Allocate memory for the key */
476 raw->bytes = PSA_BITS_TO_BYTES( bits );
477 raw->data = mbedtls_calloc( 1, raw->bytes );
478 if( raw->data == NULL )
479 {
480 raw->bytes = 0;
481 return( PSA_ERROR_INSUFFICIENT_MEMORY );
482 }
483 return( PSA_SUCCESS );
484}
485
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200486#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100487/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
488 * that are not a multiple of 8) well. For example, there is only
489 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
490 * way to return the exact bit size of a key.
491 * To keep things simple, reject non-byte-aligned key sizes. */
492static psa_status_t psa_check_rsa_key_byte_aligned(
493 const mbedtls_rsa_context *rsa )
494{
495 mbedtls_mpi n;
496 psa_status_t status;
497 mbedtls_mpi_init( &n );
498 status = mbedtls_to_psa_error(
499 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
500 if( status == PSA_SUCCESS )
501 {
502 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
503 status = PSA_ERROR_NOT_SUPPORTED;
504 }
505 mbedtls_mpi_free( &n );
506 return( status );
507}
508
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000509static psa_status_t psa_import_rsa_key( psa_key_type_t type,
510 const uint8_t *data,
511 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200512 mbedtls_rsa_context **p_rsa )
513{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000514 psa_status_t status;
515 mbedtls_pk_context pk;
516 mbedtls_rsa_context *rsa;
517 size_t bits;
518
519 mbedtls_pk_init( &pk );
520
521 /* Parse the data. */
522 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
523 status = mbedtls_to_psa_error(
524 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200525 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000526 status = mbedtls_to_psa_error(
527 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
528 if( status != PSA_SUCCESS )
529 goto exit;
530
531 /* We have something that the pkparse module recognizes. If it is a
532 * valid RSA key, store it. */
533 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000535 status = PSA_ERROR_INVALID_ARGUMENT;
536 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200537 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000538
539 rsa = mbedtls_pk_rsa( pk );
540 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
541 * supports non-byte-aligned key sizes, but not well. For example,
542 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
543 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
544 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
545 {
546 status = PSA_ERROR_NOT_SUPPORTED;
547 goto exit;
548 }
549 status = psa_check_rsa_key_byte_aligned( rsa );
550
551exit:
552 /* Free the content of the pk object only on error. */
553 if( status != PSA_SUCCESS )
554 {
555 mbedtls_pk_free( &pk );
556 return( status );
557 }
558
559 /* On success, store the content of the object in the RSA context. */
560 *p_rsa = rsa;
561
562 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563}
564#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
565
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000566#if defined(MBEDTLS_ECP_C)
567
568/* Import a public key given as the uncompressed representation defined by SEC1
569 * 2.3.3 as the content of an ECPoint. */
570static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
571 const uint8_t *data,
572 size_t data_length,
573 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200574{
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000575 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
576 mbedtls_ecp_keypair *ecp = NULL;
577 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
578
579 *p_ecp = NULL;
580 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
581 if( ecp == NULL )
582 return( PSA_ERROR_INSUFFICIENT_MEMORY );
583 mbedtls_ecp_keypair_init( ecp );
584
585 /* Load the group. */
586 status = mbedtls_to_psa_error(
587 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
588 if( status != PSA_SUCCESS )
589 goto exit;
590 /* Load the public value. */
591 status = mbedtls_to_psa_error(
592 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
593 data, data_length ) );
594 if( status != PSA_SUCCESS )
595 goto exit;
596
597 /* Check that the point is on the curve. */
598 status = mbedtls_to_psa_error(
599 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
600 if( status != PSA_SUCCESS )
601 goto exit;
602
603 *p_ecp = ecp;
604 return( PSA_SUCCESS );
605
606exit:
607 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200608 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000609 mbedtls_ecp_keypair_free( ecp );
610 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200611 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000612 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200613}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000614#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200615
Gilles Peskinef76aa772018-10-29 19:24:33 +0100616#if defined(MBEDTLS_ECP_C)
617/* Import a private key given as a byte string which is the private value
618 * in big-endian order. */
619static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
620 const uint8_t *data,
621 size_t data_length,
622 mbedtls_ecp_keypair **p_ecp )
623{
624 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
625 mbedtls_ecp_keypair *ecp = NULL;
626 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
627
628 *p_ecp = NULL;
629 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
630 if( ecp == NULL )
631 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000632 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100633
634 /* Load the group. */
635 status = mbedtls_to_psa_error(
636 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
637 if( status != PSA_SUCCESS )
638 goto exit;
639 /* Load the secret value. */
640 status = mbedtls_to_psa_error(
641 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
642 if( status != PSA_SUCCESS )
643 goto exit;
644 /* Validate the private key. */
645 status = mbedtls_to_psa_error(
646 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
647 if( status != PSA_SUCCESS )
648 goto exit;
649 /* Calculate the public key from the private key. */
650 status = mbedtls_to_psa_error(
651 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
652 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
653 if( status != PSA_SUCCESS )
654 goto exit;
655
656 *p_ecp = ecp;
657 return( PSA_SUCCESS );
658
659exit:
660 if( ecp != NULL )
661 {
662 mbedtls_ecp_keypair_free( ecp );
663 mbedtls_free( ecp );
664 }
665 return( status );
666}
667#endif /* defined(MBEDTLS_ECP_C) */
668
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100669/** Import key data into a slot. `slot->type` must have been set
670 * previously. This function assumes that the slot does not contain
671 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100672psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
673 const uint8_t *data,
674 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200676 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677
Darryl Green940d72c2018-07-13 13:18:51 +0100678 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100680 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100681 if( data_length > SIZE_MAX / 8 )
682 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100683 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200684 PSA_BYTES_TO_BITS( data_length ),
685 &slot->data.raw );
686 if( status != PSA_SUCCESS )
687 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200688 if( data_length != 0 )
689 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690 }
691 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100692#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100693 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100694 {
Darryl Green940d72c2018-07-13 13:18:51 +0100695 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100696 data, data_length,
697 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100698 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000699 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
700 {
701 status = psa_import_ec_public_key(
702 PSA_KEY_TYPE_GET_CURVE( slot->type ),
703 data, data_length,
704 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000705 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100706 else
707#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000708#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
709 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000711 status = psa_import_rsa_key( slot->type,
712 data, data_length,
713 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100714 }
715 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000716#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100717 {
718 return( PSA_ERROR_NOT_SUPPORTED );
719 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000720 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100721}
722
Darryl Green06fd18d2018-07-16 11:21:11 +0100723/* Retrieve an empty key slot (slot with no key data, but possibly
Jaeden Amero283dfd12019-01-11 12:06:22 +0000724 * with some metadata such as a policy or domain parameters). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100725static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100726 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100727{
728 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100729 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100730
731 *p_slot = NULL;
732
Gilles Peskinec5487a82018-12-03 18:08:14 +0100733 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100734 if( status != PSA_SUCCESS )
735 return( status );
736
737 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200738 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100739
740 *p_slot = slot;
741 return( status );
742}
743
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100744/** Calculate the intersection of two algorithm usage policies.
745 *
746 * Return 0 (which allows no operation) on incompatibility.
747 */
748static psa_algorithm_t psa_key_policy_algorithm_intersection(
749 psa_algorithm_t alg1,
750 psa_algorithm_t alg2 )
751{
752 /* Common case: the policy only allows alg. */
753 if( alg1 == alg2 )
754 return( alg1 );
755 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100756 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100757 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
758 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
759 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
760 {
761 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
762 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100763 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100764 return( alg1 );
765 }
766 /* If the policies are incompatible, allow nothing. */
767 return( 0 );
768}
769
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100770/** Test whether a policy permits an algorithm.
771 *
772 * The caller must test usage flags separately.
773 */
774static int psa_key_policy_permits( const psa_key_policy_t *policy,
775 psa_algorithm_t alg )
776{
777 /* Common case: the policy only allows alg. */
778 if( alg == policy->alg )
779 return( 1 );
780 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
781 * and alg is the same hash-and-sign family with any hash,
782 * then alg is compliant with policy->alg. */
783 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
784 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
785 {
786 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
787 ( alg & ~PSA_ALG_HASH_MASK ) );
788 }
789 /* If it isn't permitted, it's forbidden. */
790 return( 0 );
791}
792
Gilles Peskinef603c712019-01-19 13:40:11 +0100793/** Restrict a key policy based on a constraint.
794 *
795 * \param[in,out] policy The policy to restrict.
796 * \param[in] constraint The policy constraint to apply.
797 *
798 * \retval #PSA_SUCCESS
799 * \c *policy contains the intersection of the original value of
800 * \c *policy and \c *constraint.
801 * \retval #PSA_ERROR_INVALID_ARGUMENT
802 * \c *policy and \c *constraint are incompatible.
803 * \c *policy is unchanged.
804 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100805static psa_status_t psa_restrict_key_policy(
806 psa_key_policy_t *policy,
807 const psa_key_policy_t *constraint )
808{
809 psa_algorithm_t intersection_alg =
810 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
811 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
812 return( PSA_ERROR_INVALID_ARGUMENT );
813 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100814 policy->alg = intersection_alg;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100815 return( PSA_SUCCESS );
816}
817
Darryl Green06fd18d2018-07-16 11:21:11 +0100818/** Retrieve a slot which must contain a key. The key must have allow all the
819 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
820 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100821static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100822 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100823 psa_key_usage_t usage,
824 psa_algorithm_t alg )
825{
826 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100827 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100828
829 *p_slot = NULL;
830
Gilles Peskinec5487a82018-12-03 18:08:14 +0100831 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100832 if( status != PSA_SUCCESS )
833 return( status );
834 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200835 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100836
837 /* Enforce that usage policy for the key slot contains all the flags
838 * required by the usage parameter. There is one exception: public
839 * keys can always be exported, so we treat public key objects as
840 * if they had the export flag. */
841 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
842 usage &= ~PSA_KEY_USAGE_EXPORT;
843 if( ( slot->policy.usage & usage ) != usage )
844 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100845
846 /* Enforce that the usage policy permits the requested algortihm. */
847 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100848 return( PSA_ERROR_NOT_PERMITTED );
849
850 *p_slot = slot;
851 return( PSA_SUCCESS );
852}
Darryl Green940d72c2018-07-13 13:18:51 +0100853
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100854/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100855static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000856{
857 if( slot->type == PSA_KEY_TYPE_NONE )
858 {
859 /* No key material to clean. */
860 }
861 else if( key_type_is_raw_bytes( slot->type ) )
862 {
863 mbedtls_free( slot->data.raw.data );
864 }
865 else
866#if defined(MBEDTLS_RSA_C)
867 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
868 {
869 mbedtls_rsa_free( slot->data.rsa );
870 mbedtls_free( slot->data.rsa );
871 }
872 else
873#endif /* defined(MBEDTLS_RSA_C) */
874#if defined(MBEDTLS_ECP_C)
875 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
876 {
877 mbedtls_ecp_keypair_free( slot->data.ecp );
878 mbedtls_free( slot->data.ecp );
879 }
880 else
881#endif /* defined(MBEDTLS_ECP_C) */
882 {
883 /* Shouldn't happen: the key type is not any type that we
884 * put in. */
885 return( PSA_ERROR_TAMPERING_DETECTED );
886 }
887
888 return( PSA_SUCCESS );
889}
890
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100891static void psa_abort_operations_using_key( psa_key_slot_t *slot )
892{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200893 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100894 (void) slot;
895}
896
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100897/** Completely wipe a slot in memory, including its policy.
898 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100899psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100900{
901 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100902 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100903 /* At this point, key material and other type-specific content has
904 * been wiped. Clear remaining metadata. We can call memset and not
905 * zeroize because the metadata is not particularly sensitive. */
906 memset( slot, 0, sizeof( *slot ) );
907 return( status );
908}
909
Gilles Peskine87a5e562019-04-17 12:28:25 +0200910psa_status_t psa_import_key_to_handle( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100911 psa_key_type_t type,
912 const uint8_t *data,
913 size_t data_length )
914{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100915 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100916 psa_status_t status;
917
Gilles Peskinec5487a82018-12-03 18:08:14 +0100918 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100919 if( status != PSA_SUCCESS )
920 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100921
922 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100923
924 status = psa_import_key_into_slot( slot, data, data_length );
925 if( status != PSA_SUCCESS )
926 {
927 slot->type = PSA_KEY_TYPE_NONE;
928 return( status );
929 }
930
Darryl Greend49a4992018-06-18 17:27:26 +0100931#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
932 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
933 {
934 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100935 status = psa_save_persistent_key( slot->persistent_storage_id,
936 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100937 data_length );
938 if( status != PSA_SUCCESS )
939 {
940 (void) psa_remove_key_data_from_memory( slot );
941 slot->type = PSA_KEY_TYPE_NONE;
942 }
943 }
944#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
945
946 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947}
948
Gilles Peskinec5487a82018-12-03 18:08:14 +0100949psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100951 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100952 psa_status_t status = PSA_SUCCESS;
953 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100954
Gilles Peskinec5487a82018-12-03 18:08:14 +0100955 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200956 if( status != PSA_SUCCESS )
957 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100958#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
959 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
960 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100961 storage_status =
962 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100963 }
964#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100965 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100966 if( status != PSA_SUCCESS )
967 return( status );
968 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969}
970
Gilles Peskineb870b182018-07-06 16:02:09 +0200971/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200972static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200973{
974 if( key_type_is_raw_bytes( slot->type ) )
975 return( slot->data.raw.bytes * 8 );
976#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200977 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100978 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200979#endif /* defined(MBEDTLS_RSA_C) */
980#if defined(MBEDTLS_ECP_C)
981 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
982 return( slot->data.ecp->grp.pbits );
983#endif /* defined(MBEDTLS_ECP_C) */
984 /* Shouldn't happen except on an empty slot. */
985 return( 0 );
986}
987
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200988void psa_reset_key_attributes( psa_key_attributes_t *attributes )
989{
Gilles Peskineb699f072019-04-26 16:06:02 +0200990 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200991 memset( attributes, 0, sizeof( *attributes ) );
992}
993
Gilles Peskineb699f072019-04-26 16:06:02 +0200994psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
995 psa_key_type_t type,
996 const uint8_t *data,
997 size_t data_length )
998{
999 uint8_t *copy = NULL;
1000
1001 if( data_length != 0 )
1002 {
1003 copy = mbedtls_calloc( 1, data_length );
1004 if( copy == NULL )
1005 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1006 memcpy( copy, data, data_length );
1007 }
1008 /* After this point, this function is guaranteed to succeed, so it
1009 * can start modifying `*attributes`. */
1010
1011 if( attributes->domain_parameters != NULL )
1012 {
1013 mbedtls_free( attributes->domain_parameters );
1014 attributes->domain_parameters = NULL;
1015 attributes->domain_parameters_size = 0;
1016 }
1017
1018 attributes->domain_parameters = copy;
1019 attributes->domain_parameters_size = data_length;
1020 attributes->type = type;
1021 return( PSA_SUCCESS );
1022}
1023
1024psa_status_t psa_get_key_domain_parameters(
1025 const psa_key_attributes_t *attributes,
1026 uint8_t *data, size_t data_size, size_t *data_length )
1027{
1028 if( attributes->domain_parameters_size > data_size )
1029 return( PSA_ERROR_BUFFER_TOO_SMALL );
1030 *data_length = attributes->domain_parameters_size;
1031 if( attributes->domain_parameters_size != 0 )
1032 memcpy( data, attributes->domain_parameters,
1033 attributes->domain_parameters_size );
1034 return( PSA_SUCCESS );
1035}
1036
1037#if defined(MBEDTLS_RSA_C)
1038static psa_status_t psa_get_rsa_public_exponent(
1039 const mbedtls_rsa_context *rsa,
1040 psa_key_attributes_t *attributes )
1041{
1042 mbedtls_mpi mpi;
1043 int ret;
1044 uint8_t *buffer = NULL;
1045 size_t buflen;
1046 mbedtls_mpi_init( &mpi );
1047
1048 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1049 if( ret != 0 )
1050 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001051 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1052 {
1053 /* It's the default value, which is reported as an empty string,
1054 * so there's nothing to do. */
1055 goto exit;
1056 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001057
1058 buflen = mbedtls_mpi_size( &mpi );
1059 buffer = mbedtls_calloc( 1, buflen );
1060 if( buffer == NULL )
1061 {
1062 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1063 goto exit;
1064 }
1065 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1066 if( ret != 0 )
1067 goto exit;
1068 attributes->domain_parameters = buffer;
1069 attributes->domain_parameters_size = buflen;
1070
1071exit:
1072 mbedtls_mpi_free( &mpi );
1073 if( ret != 0 )
1074 mbedtls_free( buffer );
1075 return( mbedtls_to_psa_error( ret ) );
1076}
1077#endif /* MBEDTLS_RSA_C */
1078
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001079psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1080 psa_key_attributes_t *attributes )
1081{
1082 psa_key_slot_t *slot;
1083 psa_status_t status;
1084
1085 psa_reset_key_attributes( attributes );
1086
1087 status = psa_get_key_slot( handle, &slot );
1088 if( status != PSA_SUCCESS )
1089 return( status );
1090
1091 attributes->id = slot->persistent_storage_id;
1092 attributes->lifetime = slot->lifetime;
1093 attributes->policy = slot->policy;
1094 attributes->type = slot->type;
1095 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001096
1097 switch( slot->type )
1098 {
1099#if defined(MBEDTLS_RSA_C)
1100 case PSA_KEY_TYPE_RSA_KEYPAIR:
1101 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1102 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1103 break;
1104#endif
1105 default:
1106 /* Nothing else to do. */
1107 break;
1108 }
1109
1110 if( status != PSA_SUCCESS )
1111 psa_reset_key_attributes( attributes );
1112 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001113}
1114
Gilles Peskinec5487a82018-12-03 18:08:14 +01001115psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001116 psa_key_type_t *type,
1117 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001118{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001119 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001120 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001121
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001122 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001123 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001124 if( bits != NULL )
1125 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001126 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001127 if( status != PSA_SUCCESS )
1128 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001129
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001130 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001131 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001132 if( type != NULL )
1133 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001134 if( bits != NULL )
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001135 *bits = psa_get_key_slot_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001136 return( PSA_SUCCESS );
1137}
1138
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001139#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001140static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1141 unsigned char *buf, size_t size )
1142{
1143 int ret;
1144 unsigned char *c;
1145 size_t len = 0;
1146
1147 c = buf + size;
1148
1149 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1150
1151 return( (int) len );
1152}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001153#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001154
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001155static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1156 uint8_t *data,
1157 size_t data_size,
1158 size_t *data_length,
1159 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001160{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001161 *data_length = 0;
1162
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001163 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001164 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001165
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001166 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001167 {
1168 if( slot->data.raw.bytes > data_size )
1169 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001170 if( data_size != 0 )
1171 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001172 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001173 memset( data + slot->data.raw.bytes, 0,
1174 data_size - slot->data.raw.bytes );
1175 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001176 *data_length = slot->data.raw.bytes;
1177 return( PSA_SUCCESS );
1178 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001179#if defined(MBEDTLS_ECP_C)
1180 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1181 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001182 psa_status_t status;
1183
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001184 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001185 if( bytes > data_size )
1186 return( PSA_ERROR_BUFFER_TOO_SMALL );
1187 status = mbedtls_to_psa_error(
1188 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1189 if( status != PSA_SUCCESS )
1190 return( status );
1191 memset( data + bytes, 0, data_size - bytes );
1192 *data_length = bytes;
1193 return( PSA_SUCCESS );
1194 }
1195#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001196 else
Moran Peker17e36e12018-05-02 12:55:20 +03001197 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001198#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001199 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001200 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001201 {
Moran Pekera998bc62018-04-16 18:16:20 +03001202 mbedtls_pk_context pk;
1203 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001204 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001205 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001206#if defined(MBEDTLS_RSA_C)
1207 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001208 pk.pk_info = &mbedtls_rsa_info;
1209 pk.pk_ctx = slot->data.rsa;
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 }
1214 else
1215 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001216#if defined(MBEDTLS_ECP_C)
1217 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001218 pk.pk_info = &mbedtls_eckey_info;
1219 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001220#else
1221 return( PSA_ERROR_NOT_SUPPORTED );
1222#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001223 }
Moran Pekerd7326592018-05-29 16:56:39 +03001224 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001225 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001226 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001227 }
Moran Peker17e36e12018-05-02 12:55:20 +03001228 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001229 {
Moran Peker17e36e12018-05-02 12:55:20 +03001230 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001231 }
Moran Peker60364322018-04-29 11:34:58 +03001232 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001233 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001234 /* If data_size is 0 then data may be NULL and then the
1235 * call to memset would have undefined behavior. */
1236 if( data_size != 0 )
1237 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001238 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001239 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001240 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1241 * Move the data to the beginning and erase remaining data
1242 * at the original location. */
1243 if( 2 * (size_t) ret <= data_size )
1244 {
1245 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001246 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001247 }
1248 else if( (size_t) ret < data_size )
1249 {
1250 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001251 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001252 }
Moran Pekera998bc62018-04-16 18:16:20 +03001253 *data_length = ret;
1254 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001255 }
1256 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001257#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001258 {
1259 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001260 it is valid for a special-purpose implementation to omit
1261 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001262 return( PSA_ERROR_NOT_SUPPORTED );
1263 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001264 }
1265}
1266
Gilles Peskinec5487a82018-12-03 18:08:14 +01001267psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001268 uint8_t *data,
1269 size_t data_size,
1270 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001271{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001272 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001273 psa_status_t status;
1274
1275 /* Set the key to empty now, so that even when there are errors, we always
1276 * set data_length to a value between 0 and data_size. On error, setting
1277 * the key to empty is a good choice because an empty key representation is
1278 * unlikely to be accepted anywhere. */
1279 *data_length = 0;
1280
1281 /* Export requires the EXPORT flag. There is an exception for public keys,
1282 * which don't require any flag, but psa_get_key_from_slot takes
1283 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001284 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001285 if( status != PSA_SUCCESS )
1286 return( status );
1287 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001288 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001289}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001290
Gilles Peskinec5487a82018-12-03 18:08:14 +01001291psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001292 uint8_t *data,
1293 size_t data_size,
1294 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001295{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001296 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001297 psa_status_t status;
1298
1299 /* Set the key to empty now, so that even when there are errors, we always
1300 * set data_length to a value between 0 and data_size. On error, setting
1301 * the key to empty is a good choice because an empty key representation is
1302 * unlikely to be accepted anywhere. */
1303 *data_length = 0;
1304
1305 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001306 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001307 if( status != PSA_SUCCESS )
1308 return( status );
1309 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001310 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001311}
1312
Darryl Green0c6575a2018-11-07 16:05:30 +00001313#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001314static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001315 size_t bits )
1316{
1317 psa_status_t status;
1318 uint8_t *data;
1319 size_t key_length;
1320 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1321 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001322 if( data == NULL )
1323 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001324 /* Get key data in export format */
1325 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1326 if( status != PSA_SUCCESS )
1327 {
1328 slot->type = PSA_KEY_TYPE_NONE;
1329 goto exit;
1330 }
1331 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001332 status = psa_save_persistent_key( slot->persistent_storage_id,
1333 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001334 data, key_length );
1335 if( status != PSA_SUCCESS )
1336 {
1337 slot->type = PSA_KEY_TYPE_NONE;
1338 }
1339exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001340 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001341 mbedtls_free( data );
1342 return( status );
1343}
1344#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1345
Gilles Peskine4747d192019-04-17 15:05:45 +02001346static psa_status_t psa_set_key_policy_internal(
1347 psa_key_slot_t *slot,
1348 const psa_key_policy_t *policy )
1349{
1350 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
1351 PSA_KEY_USAGE_ENCRYPT |
1352 PSA_KEY_USAGE_DECRYPT |
1353 PSA_KEY_USAGE_SIGN |
1354 PSA_KEY_USAGE_VERIFY |
1355 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1356 return( PSA_ERROR_INVALID_ARGUMENT );
1357
1358 slot->policy = *policy;
1359 return( PSA_SUCCESS );
1360}
1361
1362/** Prepare a key slot to receive key material.
1363 *
1364 * This function allocates a key slot and sets its metadata.
1365 *
1366 * If this function fails, call psa_fail_key_creation().
1367 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001368 * This function is intended to be used as follows:
1369 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1370 * it with the specified attributes, and assign it a handle.
1371 * -# Populate the slot with the key material.
1372 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1373 * In case of failure at any step, stop the sequence and call
1374 * psa_fail_key_creation().
1375 *
Gilles Peskine4747d192019-04-17 15:05:45 +02001376 * \param attributes Key attributes for the new key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001377 * \param handle On success, a handle for the allocated slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001378 * \param p_slot On success, a pointer to the prepared slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001379 *
1380 * \retval #PSA_SUCCESS
1381 * The key slot is ready to receive key material.
1382 * \return If this function fails, the key slot is an invalid state.
1383 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001384 */
1385static psa_status_t psa_start_key_creation(
1386 const psa_key_attributes_t *attributes,
1387 psa_key_handle_t *handle,
1388 psa_key_slot_t **p_slot )
1389{
1390 psa_status_t status;
1391 psa_key_slot_t *slot;
1392
1393 status = psa_allocate_key( handle );
1394 if( status != PSA_SUCCESS )
1395 return( status );
1396 status = psa_get_key_slot( *handle, p_slot );
1397 if( status != PSA_SUCCESS )
1398 return( status );
1399 slot = *p_slot;
1400
1401 status = psa_set_key_policy_internal( slot, &attributes->policy );
1402 if( status != PSA_SUCCESS )
1403 return( status );
1404 slot->lifetime = attributes->lifetime;
1405 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001406 {
1407 status = psa_validate_persistent_key_parameters( attributes->lifetime,
1408 attributes->id );
1409 if( status != PSA_SUCCESS )
1410 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001411 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001412 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001413 slot->type = attributes->type;
1414
1415 return( status );
1416}
1417
1418/** Finalize the creation of a key once its key material has been set.
1419 *
1420 * This entails writing the key to persistent storage.
1421 *
1422 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001423 * See the documentation of psa_start_key_creation() for the intended use
1424 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001425 *
1426 * \param slot Pointer to the slot with key material.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001427 *
1428 * \retval #PSA_SUCCESS
1429 * The key was successfully created. The handle is now valid.
1430 * \return If this function fails, the key slot is an invalid state.
1431 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001432 */
1433static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1434{
1435 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001436 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001437
1438#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1439 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1440 {
1441 uint8_t *buffer = NULL;
1442 size_t buffer_size = 0;
1443 size_t length;
1444
1445 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001446 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001447 buffer = mbedtls_calloc( 1, buffer_size );
1448 if( buffer == NULL && buffer_size != 0 )
1449 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1450 status = psa_internal_export_key( slot,
1451 buffer, buffer_size, &length,
1452 0 );
1453
1454 if( status == PSA_SUCCESS )
1455 {
1456 status = psa_save_persistent_key( slot->persistent_storage_id,
1457 slot->type, &slot->policy,
1458 buffer, length );
1459 }
1460
1461 if( buffer_size != 0 )
1462 mbedtls_platform_zeroize( buffer, buffer_size );
1463 mbedtls_free( buffer );
1464 }
1465#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1466
1467 return( status );
1468}
1469
1470/** Abort the creation of a key.
1471 *
1472 * You may call this function after calling psa_start_key_creation(),
1473 * or after psa_finish_key_creation() fails. In other circumstances, this
1474 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001475 * See the documentation of psa_start_key_creation() for the intended use
1476 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001477 *
1478 * \param slot Pointer to the slot with key material.
1479 */
1480static void psa_fail_key_creation( psa_key_slot_t *slot )
1481{
1482 if( slot == NULL )
1483 return;
1484 psa_wipe_key_slot( slot );
1485}
1486
1487psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
1488 psa_key_handle_t *handle,
1489 const uint8_t *data,
1490 size_t data_length )
1491{
1492 psa_status_t status;
1493 psa_key_slot_t *slot = NULL;
1494 status = psa_start_key_creation( attributes, handle, &slot );
1495 if( status == PSA_SUCCESS )
1496 {
1497 status = psa_import_key_into_slot( slot, data, data_length );
1498 }
1499 if( status == PSA_SUCCESS )
1500 status = psa_finish_key_creation( slot );
1501 if( status != PSA_SUCCESS )
1502 {
1503 psa_fail_key_creation( slot );
1504 *handle = 0;
1505 }
1506 return( status );
1507}
1508
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001509static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001510 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001511{
1512 psa_status_t status;
1513 uint8_t *buffer = NULL;
1514 size_t buffer_size = 0;
1515 size_t length;
1516
1517 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001518 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001519 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001520 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001521 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001522 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1523 if( status != PSA_SUCCESS )
1524 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001525 target->type = source->type;
1526 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001527
1528exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001529 if( buffer_size != 0 )
1530 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001531 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001532 return( status );
1533}
1534
Gilles Peskine87a5e562019-04-17 12:28:25 +02001535psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001536 psa_key_handle_t target_handle,
1537 const psa_key_policy_t *constraint)
1538{
1539 psa_key_slot_t *source_slot = NULL;
1540 psa_key_slot_t *target_slot = NULL;
1541 psa_key_policy_t new_policy;
1542 psa_status_t status;
1543 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1544 if( status != PSA_SUCCESS )
1545 return( status );
1546 status = psa_get_empty_key_slot( target_handle, &target_slot );
1547 if( status != PSA_SUCCESS )
1548 return( status );
1549
1550 new_policy = target_slot->policy;
1551 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1552 if( status != PSA_SUCCESS )
1553 return( status );
1554 if( constraint != NULL )
1555 {
1556 status = psa_restrict_key_policy( &new_policy, constraint );
1557 if( status != PSA_SUCCESS )
1558 return( status );
1559 }
1560
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001561 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001562 if( status != PSA_SUCCESS )
1563 return( status );
1564
1565 target_slot->policy = new_policy;
1566 return( PSA_SUCCESS );
1567}
1568
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001569psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1570 const psa_key_attributes_t *specified_attributes,
1571 psa_key_handle_t *target_handle )
1572{
1573 psa_status_t status;
1574 psa_key_slot_t *source_slot = NULL;
1575 psa_key_slot_t *target_slot = NULL;
1576 psa_key_attributes_t actual_attributes = *specified_attributes;
1577
1578 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1579 if( status != PSA_SUCCESS )
1580 goto exit;
1581
1582 status = psa_restrict_key_policy( &actual_attributes.policy,
1583 &source_slot->policy );
1584 if( status != PSA_SUCCESS )
1585 goto exit;
1586
1587 status = psa_start_key_creation( &actual_attributes,
1588 target_handle, &target_slot );
1589 if( status != PSA_SUCCESS )
1590 goto exit;
1591
1592 status = psa_copy_key_material( source_slot, target_slot );
1593
1594exit:
1595 if( status == PSA_SUCCESS )
1596 status = psa_finish_key_creation( target_slot );
1597 if( status != PSA_SUCCESS )
1598 {
1599 psa_fail_key_creation( target_slot );
1600 *target_handle = 0;
1601 }
1602 return( status );
1603}
1604
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001605
1606
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001607/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001608/* Message digests */
1609/****************************************************************/
1610
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001611static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001612{
1613 switch( alg )
1614 {
1615#if defined(MBEDTLS_MD2_C)
1616 case PSA_ALG_MD2:
1617 return( &mbedtls_md2_info );
1618#endif
1619#if defined(MBEDTLS_MD4_C)
1620 case PSA_ALG_MD4:
1621 return( &mbedtls_md4_info );
1622#endif
1623#if defined(MBEDTLS_MD5_C)
1624 case PSA_ALG_MD5:
1625 return( &mbedtls_md5_info );
1626#endif
1627#if defined(MBEDTLS_RIPEMD160_C)
1628 case PSA_ALG_RIPEMD160:
1629 return( &mbedtls_ripemd160_info );
1630#endif
1631#if defined(MBEDTLS_SHA1_C)
1632 case PSA_ALG_SHA_1:
1633 return( &mbedtls_sha1_info );
1634#endif
1635#if defined(MBEDTLS_SHA256_C)
1636 case PSA_ALG_SHA_224:
1637 return( &mbedtls_sha224_info );
1638 case PSA_ALG_SHA_256:
1639 return( &mbedtls_sha256_info );
1640#endif
1641#if defined(MBEDTLS_SHA512_C)
1642 case PSA_ALG_SHA_384:
1643 return( &mbedtls_sha384_info );
1644 case PSA_ALG_SHA_512:
1645 return( &mbedtls_sha512_info );
1646#endif
1647 default:
1648 return( NULL );
1649 }
1650}
1651
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001652psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1653{
1654 switch( operation->alg )
1655 {
Gilles Peskine81736312018-06-26 15:04:31 +02001656 case 0:
1657 /* The object has (apparently) been initialized but it is not
1658 * in use. It's ok to call abort on such an object, and there's
1659 * nothing to do. */
1660 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001661#if defined(MBEDTLS_MD2_C)
1662 case PSA_ALG_MD2:
1663 mbedtls_md2_free( &operation->ctx.md2 );
1664 break;
1665#endif
1666#if defined(MBEDTLS_MD4_C)
1667 case PSA_ALG_MD4:
1668 mbedtls_md4_free( &operation->ctx.md4 );
1669 break;
1670#endif
1671#if defined(MBEDTLS_MD5_C)
1672 case PSA_ALG_MD5:
1673 mbedtls_md5_free( &operation->ctx.md5 );
1674 break;
1675#endif
1676#if defined(MBEDTLS_RIPEMD160_C)
1677 case PSA_ALG_RIPEMD160:
1678 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1679 break;
1680#endif
1681#if defined(MBEDTLS_SHA1_C)
1682 case PSA_ALG_SHA_1:
1683 mbedtls_sha1_free( &operation->ctx.sha1 );
1684 break;
1685#endif
1686#if defined(MBEDTLS_SHA256_C)
1687 case PSA_ALG_SHA_224:
1688 case PSA_ALG_SHA_256:
1689 mbedtls_sha256_free( &operation->ctx.sha256 );
1690 break;
1691#endif
1692#if defined(MBEDTLS_SHA512_C)
1693 case PSA_ALG_SHA_384:
1694 case PSA_ALG_SHA_512:
1695 mbedtls_sha512_free( &operation->ctx.sha512 );
1696 break;
1697#endif
1698 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001699 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001700 }
1701 operation->alg = 0;
1702 return( PSA_SUCCESS );
1703}
1704
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001705psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001706 psa_algorithm_t alg )
1707{
1708 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001709
1710 /* A context must be freshly initialized before it can be set up. */
1711 if( operation->alg != 0 )
1712 {
1713 return( PSA_ERROR_BAD_STATE );
1714 }
1715
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001716 switch( alg )
1717 {
1718#if defined(MBEDTLS_MD2_C)
1719 case PSA_ALG_MD2:
1720 mbedtls_md2_init( &operation->ctx.md2 );
1721 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1722 break;
1723#endif
1724#if defined(MBEDTLS_MD4_C)
1725 case PSA_ALG_MD4:
1726 mbedtls_md4_init( &operation->ctx.md4 );
1727 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1728 break;
1729#endif
1730#if defined(MBEDTLS_MD5_C)
1731 case PSA_ALG_MD5:
1732 mbedtls_md5_init( &operation->ctx.md5 );
1733 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1734 break;
1735#endif
1736#if defined(MBEDTLS_RIPEMD160_C)
1737 case PSA_ALG_RIPEMD160:
1738 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1739 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1740 break;
1741#endif
1742#if defined(MBEDTLS_SHA1_C)
1743 case PSA_ALG_SHA_1:
1744 mbedtls_sha1_init( &operation->ctx.sha1 );
1745 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1746 break;
1747#endif
1748#if defined(MBEDTLS_SHA256_C)
1749 case PSA_ALG_SHA_224:
1750 mbedtls_sha256_init( &operation->ctx.sha256 );
1751 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1752 break;
1753 case PSA_ALG_SHA_256:
1754 mbedtls_sha256_init( &operation->ctx.sha256 );
1755 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1756 break;
1757#endif
1758#if defined(MBEDTLS_SHA512_C)
1759 case PSA_ALG_SHA_384:
1760 mbedtls_sha512_init( &operation->ctx.sha512 );
1761 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1762 break;
1763 case PSA_ALG_SHA_512:
1764 mbedtls_sha512_init( &operation->ctx.sha512 );
1765 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1766 break;
1767#endif
1768 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001769 return( PSA_ALG_IS_HASH( alg ) ?
1770 PSA_ERROR_NOT_SUPPORTED :
1771 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001772 }
1773 if( ret == 0 )
1774 operation->alg = alg;
1775 else
1776 psa_hash_abort( operation );
1777 return( mbedtls_to_psa_error( ret ) );
1778}
1779
1780psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1781 const uint8_t *input,
1782 size_t input_length )
1783{
1784 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001785
1786 /* Don't require hash implementations to behave correctly on a
1787 * zero-length input, which may have an invalid pointer. */
1788 if( input_length == 0 )
1789 return( PSA_SUCCESS );
1790
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001791 switch( operation->alg )
1792 {
1793#if defined(MBEDTLS_MD2_C)
1794 case PSA_ALG_MD2:
1795 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1796 input, input_length );
1797 break;
1798#endif
1799#if defined(MBEDTLS_MD4_C)
1800 case PSA_ALG_MD4:
1801 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1802 input, input_length );
1803 break;
1804#endif
1805#if defined(MBEDTLS_MD5_C)
1806 case PSA_ALG_MD5:
1807 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1808 input, input_length );
1809 break;
1810#endif
1811#if defined(MBEDTLS_RIPEMD160_C)
1812 case PSA_ALG_RIPEMD160:
1813 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1814 input, input_length );
1815 break;
1816#endif
1817#if defined(MBEDTLS_SHA1_C)
1818 case PSA_ALG_SHA_1:
1819 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1820 input, input_length );
1821 break;
1822#endif
1823#if defined(MBEDTLS_SHA256_C)
1824 case PSA_ALG_SHA_224:
1825 case PSA_ALG_SHA_256:
1826 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1827 input, input_length );
1828 break;
1829#endif
1830#if defined(MBEDTLS_SHA512_C)
1831 case PSA_ALG_SHA_384:
1832 case PSA_ALG_SHA_512:
1833 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1834 input, input_length );
1835 break;
1836#endif
1837 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001838 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001839 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001840
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001841 if( ret != 0 )
1842 psa_hash_abort( operation );
1843 return( mbedtls_to_psa_error( ret ) );
1844}
1845
1846psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1847 uint8_t *hash,
1848 size_t hash_size,
1849 size_t *hash_length )
1850{
itayzafrir40835d42018-08-02 13:14:17 +03001851 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001852 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001853 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001854
1855 /* Fill the output buffer with something that isn't a valid hash
1856 * (barring an attack on the hash and deliberately-crafted input),
1857 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001858 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001859 /* If hash_size is 0 then hash may be NULL and then the
1860 * call to memset would have undefined behavior. */
1861 if( hash_size != 0 )
1862 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001863
1864 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001865 {
1866 status = PSA_ERROR_BUFFER_TOO_SMALL;
1867 goto exit;
1868 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001869
1870 switch( operation->alg )
1871 {
1872#if defined(MBEDTLS_MD2_C)
1873 case PSA_ALG_MD2:
1874 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1875 break;
1876#endif
1877#if defined(MBEDTLS_MD4_C)
1878 case PSA_ALG_MD4:
1879 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1880 break;
1881#endif
1882#if defined(MBEDTLS_MD5_C)
1883 case PSA_ALG_MD5:
1884 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1885 break;
1886#endif
1887#if defined(MBEDTLS_RIPEMD160_C)
1888 case PSA_ALG_RIPEMD160:
1889 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1890 break;
1891#endif
1892#if defined(MBEDTLS_SHA1_C)
1893 case PSA_ALG_SHA_1:
1894 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1895 break;
1896#endif
1897#if defined(MBEDTLS_SHA256_C)
1898 case PSA_ALG_SHA_224:
1899 case PSA_ALG_SHA_256:
1900 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1901 break;
1902#endif
1903#if defined(MBEDTLS_SHA512_C)
1904 case PSA_ALG_SHA_384:
1905 case PSA_ALG_SHA_512:
1906 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1907 break;
1908#endif
1909 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001910 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001911 }
itayzafrir40835d42018-08-02 13:14:17 +03001912 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001913
itayzafrir40835d42018-08-02 13:14:17 +03001914exit:
1915 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001916 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001917 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001918 return( psa_hash_abort( operation ) );
1919 }
1920 else
1921 {
1922 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001923 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001924 }
1925}
1926
Gilles Peskine2d277862018-06-18 15:41:12 +02001927psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1928 const uint8_t *hash,
1929 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001930{
1931 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1932 size_t actual_hash_length;
1933 psa_status_t status = psa_hash_finish( operation,
1934 actual_hash, sizeof( actual_hash ),
1935 &actual_hash_length );
1936 if( status != PSA_SUCCESS )
1937 return( status );
1938 if( actual_hash_length != hash_length )
1939 return( PSA_ERROR_INVALID_SIGNATURE );
1940 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1941 return( PSA_ERROR_INVALID_SIGNATURE );
1942 return( PSA_SUCCESS );
1943}
1944
Gilles Peskineeb35d782019-01-22 17:56:16 +01001945psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1946 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001947{
1948 if( target_operation->alg != 0 )
1949 return( PSA_ERROR_BAD_STATE );
1950
1951 switch( source_operation->alg )
1952 {
1953 case 0:
1954 return( PSA_ERROR_BAD_STATE );
1955#if defined(MBEDTLS_MD2_C)
1956 case PSA_ALG_MD2:
1957 mbedtls_md2_clone( &target_operation->ctx.md2,
1958 &source_operation->ctx.md2 );
1959 break;
1960#endif
1961#if defined(MBEDTLS_MD4_C)
1962 case PSA_ALG_MD4:
1963 mbedtls_md4_clone( &target_operation->ctx.md4,
1964 &source_operation->ctx.md4 );
1965 break;
1966#endif
1967#if defined(MBEDTLS_MD5_C)
1968 case PSA_ALG_MD5:
1969 mbedtls_md5_clone( &target_operation->ctx.md5,
1970 &source_operation->ctx.md5 );
1971 break;
1972#endif
1973#if defined(MBEDTLS_RIPEMD160_C)
1974 case PSA_ALG_RIPEMD160:
1975 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1976 &source_operation->ctx.ripemd160 );
1977 break;
1978#endif
1979#if defined(MBEDTLS_SHA1_C)
1980 case PSA_ALG_SHA_1:
1981 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1982 &source_operation->ctx.sha1 );
1983 break;
1984#endif
1985#if defined(MBEDTLS_SHA256_C)
1986 case PSA_ALG_SHA_224:
1987 case PSA_ALG_SHA_256:
1988 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1989 &source_operation->ctx.sha256 );
1990 break;
1991#endif
1992#if defined(MBEDTLS_SHA512_C)
1993 case PSA_ALG_SHA_384:
1994 case PSA_ALG_SHA_512:
1995 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1996 &source_operation->ctx.sha512 );
1997 break;
1998#endif
1999 default:
2000 return( PSA_ERROR_NOT_SUPPORTED );
2001 }
2002
2003 target_operation->alg = source_operation->alg;
2004 return( PSA_SUCCESS );
2005}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002006
2007
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002008/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002009/* MAC */
2010/****************************************************************/
2011
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002012static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002013 psa_algorithm_t alg,
2014 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002015 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002016 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002017{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002019 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002020
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002021 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002022 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002023
Gilles Peskine8c9def32018-02-08 10:02:12 +01002024 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2025 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002026 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002027 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002028 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002029 mode = MBEDTLS_MODE_STREAM;
2030 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031 case PSA_ALG_CTR:
2032 mode = MBEDTLS_MODE_CTR;
2033 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002034 case PSA_ALG_CFB:
2035 mode = MBEDTLS_MODE_CFB;
2036 break;
2037 case PSA_ALG_OFB:
2038 mode = MBEDTLS_MODE_OFB;
2039 break;
2040 case PSA_ALG_CBC_NO_PADDING:
2041 mode = MBEDTLS_MODE_CBC;
2042 break;
2043 case PSA_ALG_CBC_PKCS7:
2044 mode = MBEDTLS_MODE_CBC;
2045 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002046 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002047 mode = MBEDTLS_MODE_CCM;
2048 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002049 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002050 mode = MBEDTLS_MODE_GCM;
2051 break;
2052 default:
2053 return( NULL );
2054 }
2055 }
2056 else if( alg == PSA_ALG_CMAC )
2057 mode = MBEDTLS_MODE_ECB;
2058 else if( alg == PSA_ALG_GMAC )
2059 mode = MBEDTLS_MODE_GCM;
2060 else
2061 return( NULL );
2062
2063 switch( key_type )
2064 {
2065 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002066 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002067 break;
2068 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002069 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2070 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002071 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002072 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002073 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002074 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002075 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2076 * but two-key Triple-DES is functionally three-key Triple-DES
2077 * with K1=K3, so that's how we present it to mbedtls. */
2078 if( key_bits == 128 )
2079 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002080 break;
2081 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002082 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002083 break;
2084 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002085 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002086 break;
2087 default:
2088 return( NULL );
2089 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002090 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002091 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002092
Jaeden Amero23bbb752018-06-26 14:16:54 +01002093 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2094 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002095}
2096
Gilles Peskinea05219c2018-11-16 16:02:56 +01002097#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002098static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002099{
Gilles Peskine2d277862018-06-18 15:41:12 +02002100 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002101 {
2102 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002103 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002104 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002105 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002106 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002107 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002108 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002109 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002110 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002111 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002112 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002113 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002114 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002115 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002116 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002117 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002118 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002119 return( 128 );
2120 default:
2121 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002122 }
2123}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002124#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002125
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002126/* Initialize the MAC operation structure. Once this function has been
2127 * called, psa_mac_abort can run and will do the right thing. */
2128static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2129 psa_algorithm_t alg )
2130{
2131 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2132
2133 operation->alg = alg;
2134 operation->key_set = 0;
2135 operation->iv_set = 0;
2136 operation->iv_required = 0;
2137 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002138 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002139
2140#if defined(MBEDTLS_CMAC_C)
2141 if( alg == PSA_ALG_CMAC )
2142 {
2143 operation->iv_required = 0;
2144 mbedtls_cipher_init( &operation->ctx.cmac );
2145 status = PSA_SUCCESS;
2146 }
2147 else
2148#endif /* MBEDTLS_CMAC_C */
2149#if defined(MBEDTLS_MD_C)
2150 if( PSA_ALG_IS_HMAC( operation->alg ) )
2151 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002152 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2153 operation->ctx.hmac.hash_ctx.alg = 0;
2154 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002155 }
2156 else
2157#endif /* MBEDTLS_MD_C */
2158 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002159 if( ! PSA_ALG_IS_MAC( alg ) )
2160 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002161 }
2162
2163 if( status != PSA_SUCCESS )
2164 memset( operation, 0, sizeof( *operation ) );
2165 return( status );
2166}
2167
Gilles Peskine01126fa2018-07-12 17:04:55 +02002168#if defined(MBEDTLS_MD_C)
2169static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2170{
Gilles Peskine3f108122018-12-07 18:14:53 +01002171 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002172 return( psa_hash_abort( &hmac->hash_ctx ) );
2173}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002174
2175static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2176{
2177 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2178 memset( hmac, 0, sizeof( *hmac ) );
2179}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002180#endif /* MBEDTLS_MD_C */
2181
Gilles Peskine8c9def32018-02-08 10:02:12 +01002182psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2183{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002184 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002185 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002186 /* The object has (apparently) been initialized but it is not
2187 * in use. It's ok to call abort on such an object, and there's
2188 * nothing to do. */
2189 return( PSA_SUCCESS );
2190 }
2191 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002192#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002193 if( operation->alg == PSA_ALG_CMAC )
2194 {
2195 mbedtls_cipher_free( &operation->ctx.cmac );
2196 }
2197 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002198#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002199#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002200 if( PSA_ALG_IS_HMAC( operation->alg ) )
2201 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002202 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002203 }
2204 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002205#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002206 {
2207 /* Sanity check (shouldn't happen: operation->alg should
2208 * always have been initialized to a valid value). */
2209 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002210 }
Moran Peker41deec42018-04-04 15:43:05 +03002211
Gilles Peskine8c9def32018-02-08 10:02:12 +01002212 operation->alg = 0;
2213 operation->key_set = 0;
2214 operation->iv_set = 0;
2215 operation->iv_required = 0;
2216 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002217 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002218
Gilles Peskine8c9def32018-02-08 10:02:12 +01002219 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002220
2221bad_state:
2222 /* If abort is called on an uninitialized object, we can't trust
2223 * anything. Wipe the object in case it contains confidential data.
2224 * This may result in a memory leak if a pointer gets overwritten,
2225 * but it's too late to do anything about this. */
2226 memset( operation, 0, sizeof( *operation ) );
2227 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002228}
2229
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002230#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002231static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002232 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002233 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002234 const mbedtls_cipher_info_t *cipher_info )
2235{
2236 int ret;
2237
2238 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002239
2240 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2241 if( ret != 0 )
2242 return( ret );
2243
2244 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2245 slot->data.raw.data,
2246 key_bits );
2247 return( ret );
2248}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002249#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002250
Gilles Peskine248051a2018-06-20 16:09:38 +02002251#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002252static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2253 const uint8_t *key,
2254 size_t key_length,
2255 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002256{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002257 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002258 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002259 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002260 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002261 psa_status_t status;
2262
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002263 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2264 * overflow below. This should never trigger if the hash algorithm
2265 * is implemented correctly. */
2266 /* The size checks against the ipad and opad buffers cannot be written
2267 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2268 * because that triggers -Wlogical-op on GCC 7.3. */
2269 if( block_size > sizeof( ipad ) )
2270 return( PSA_ERROR_NOT_SUPPORTED );
2271 if( block_size > sizeof( hmac->opad ) )
2272 return( PSA_ERROR_NOT_SUPPORTED );
2273 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002274 return( PSA_ERROR_NOT_SUPPORTED );
2275
Gilles Peskined223b522018-06-11 18:12:58 +02002276 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002277 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002278 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002279 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002280 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002281 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002282 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002283 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002284 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002285 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002286 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002287 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002288 }
Gilles Peskine96889972018-07-12 17:07:03 +02002289 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2290 * but it is permitted. It is common when HMAC is used in HKDF, for
2291 * example. Don't call `memcpy` in the 0-length because `key` could be
2292 * an invalid pointer which would make the behavior undefined. */
2293 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002294 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002295
Gilles Peskined223b522018-06-11 18:12:58 +02002296 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2297 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002298 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002299 ipad[i] ^= 0x36;
2300 memset( ipad + key_length, 0x36, block_size - key_length );
2301
2302 /* Copy the key material from ipad to opad, flipping the requisite bits,
2303 * and filling the rest of opad with the requisite constant. */
2304 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002305 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2306 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002307
Gilles Peskine01126fa2018-07-12 17:04:55 +02002308 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002309 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002310 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002311
Gilles Peskine01126fa2018-07-12 17:04:55 +02002312 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002313
2314cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002315 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002316
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002317 return( status );
2318}
Gilles Peskine248051a2018-06-20 16:09:38 +02002319#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002320
Gilles Peskine89167cb2018-07-08 20:12:23 +02002321static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002322 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002323 psa_algorithm_t alg,
2324 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002325{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002326 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002327 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002328 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002329 psa_key_usage_t usage =
2330 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002331 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002332 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002333
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002334 /* A context must be freshly initialized before it can be set up. */
2335 if( operation->alg != 0 )
2336 {
2337 return( PSA_ERROR_BAD_STATE );
2338 }
2339
Gilles Peskined911eb72018-08-14 15:18:45 +02002340 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002341 if( status != PSA_SUCCESS )
2342 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002343 if( is_sign )
2344 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002345
Gilles Peskinec5487a82018-12-03 18:08:14 +01002346 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002347 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002348 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002349 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002350
Gilles Peskine8c9def32018-02-08 10:02:12 +01002351#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002352 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002353 {
2354 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002355 mbedtls_cipher_info_from_psa( full_length_alg,
2356 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002357 int ret;
2358 if( cipher_info == NULL )
2359 {
2360 status = PSA_ERROR_NOT_SUPPORTED;
2361 goto exit;
2362 }
2363 operation->mac_size = cipher_info->block_size;
2364 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2365 status = mbedtls_to_psa_error( ret );
2366 }
2367 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002368#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002369#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002370 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002371 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002372 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002373 if( hash_alg == 0 )
2374 {
2375 status = PSA_ERROR_NOT_SUPPORTED;
2376 goto exit;
2377 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002378
2379 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2380 /* Sanity check. This shouldn't fail on a valid configuration. */
2381 if( operation->mac_size == 0 ||
2382 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2383 {
2384 status = PSA_ERROR_NOT_SUPPORTED;
2385 goto exit;
2386 }
2387
Gilles Peskine01126fa2018-07-12 17:04:55 +02002388 if( slot->type != PSA_KEY_TYPE_HMAC )
2389 {
2390 status = PSA_ERROR_INVALID_ARGUMENT;
2391 goto exit;
2392 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002393
Gilles Peskine01126fa2018-07-12 17:04:55 +02002394 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2395 slot->data.raw.data,
2396 slot->data.raw.bytes,
2397 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002398 }
2399 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002401 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002402 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002403 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002405
Gilles Peskined911eb72018-08-14 15:18:45 +02002406 if( truncated == 0 )
2407 {
2408 /* The "normal" case: untruncated algorithm. Nothing to do. */
2409 }
2410 else if( truncated < 4 )
2411 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002412 /* A very short MAC is too short for security since it can be
2413 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2414 * so we make this our minimum, even though 32 bits is still
2415 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002416 status = PSA_ERROR_NOT_SUPPORTED;
2417 }
2418 else if( truncated > operation->mac_size )
2419 {
2420 /* It's impossible to "truncate" to a larger length. */
2421 status = PSA_ERROR_INVALID_ARGUMENT;
2422 }
2423 else
2424 operation->mac_size = truncated;
2425
Gilles Peskinefbfac682018-07-08 20:51:54 +02002426exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002427 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002428 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002429 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002431 else
2432 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002433 operation->key_set = 1;
2434 }
2435 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002436}
2437
Gilles Peskine89167cb2018-07-08 20:12:23 +02002438psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002439 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002440 psa_algorithm_t alg )
2441{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002442 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002443}
2444
2445psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002446 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002447 psa_algorithm_t alg )
2448{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002449 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002450}
2451
Gilles Peskine8c9def32018-02-08 10:02:12 +01002452psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2453 const uint8_t *input,
2454 size_t input_length )
2455{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002456 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002457 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002458 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002460 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002461 operation->has_input = 1;
2462
Gilles Peskine8c9def32018-02-08 10:02:12 +01002463#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002464 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002465 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002466 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2467 input, input_length );
2468 status = mbedtls_to_psa_error( ret );
2469 }
2470 else
2471#endif /* MBEDTLS_CMAC_C */
2472#if defined(MBEDTLS_MD_C)
2473 if( PSA_ALG_IS_HMAC( operation->alg ) )
2474 {
2475 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2476 input_length );
2477 }
2478 else
2479#endif /* MBEDTLS_MD_C */
2480 {
2481 /* This shouldn't happen if `operation` was initialized by
2482 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002483 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002484 }
2485
Gilles Peskinefbfac682018-07-08 20:51:54 +02002486 if( status != PSA_SUCCESS )
2487 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002488 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489}
2490
Gilles Peskine01126fa2018-07-12 17:04:55 +02002491#if defined(MBEDTLS_MD_C)
2492static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2493 uint8_t *mac,
2494 size_t mac_size )
2495{
2496 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2497 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2498 size_t hash_size = 0;
2499 size_t block_size = psa_get_hash_block_size( hash_alg );
2500 psa_status_t status;
2501
Gilles Peskine01126fa2018-07-12 17:04:55 +02002502 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2503 if( status != PSA_SUCCESS )
2504 return( status );
2505 /* From here on, tmp needs to be wiped. */
2506
2507 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2508 if( status != PSA_SUCCESS )
2509 goto exit;
2510
2511 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2512 if( status != PSA_SUCCESS )
2513 goto exit;
2514
2515 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2516 if( status != PSA_SUCCESS )
2517 goto exit;
2518
Gilles Peskined911eb72018-08-14 15:18:45 +02002519 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2520 if( status != PSA_SUCCESS )
2521 goto exit;
2522
2523 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002524
2525exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002526 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002527 return( status );
2528}
2529#endif /* MBEDTLS_MD_C */
2530
mohammad16036df908f2018-04-02 08:34:15 -07002531static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002532 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002533 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002535 if( ! operation->key_set )
2536 return( PSA_ERROR_BAD_STATE );
2537 if( operation->iv_required && ! operation->iv_set )
2538 return( PSA_ERROR_BAD_STATE );
2539
Gilles Peskine8c9def32018-02-08 10:02:12 +01002540 if( mac_size < operation->mac_size )
2541 return( PSA_ERROR_BUFFER_TOO_SMALL );
2542
Gilles Peskine8c9def32018-02-08 10:02:12 +01002543#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002544 if( operation->alg == PSA_ALG_CMAC )
2545 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002546 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2547 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2548 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002549 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002550 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002551 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002553 else
2554#endif /* MBEDTLS_CMAC_C */
2555#if defined(MBEDTLS_MD_C)
2556 if( PSA_ALG_IS_HMAC( operation->alg ) )
2557 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002558 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002559 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002560 }
2561 else
2562#endif /* MBEDTLS_MD_C */
2563 {
2564 /* This shouldn't happen if `operation` was initialized by
2565 * a setup function. */
2566 return( PSA_ERROR_BAD_STATE );
2567 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568}
2569
Gilles Peskineacd4be32018-07-08 19:56:25 +02002570psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2571 uint8_t *mac,
2572 size_t mac_size,
2573 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002574{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002575 psa_status_t status;
2576
Jaeden Amero252ef282019-02-15 14:05:35 +00002577 if( operation->alg == 0 )
2578 {
2579 return( PSA_ERROR_BAD_STATE );
2580 }
2581
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002582 /* Fill the output buffer with something that isn't a valid mac
2583 * (barring an attack on the mac and deliberately-crafted input),
2584 * in case the caller doesn't check the return status properly. */
2585 *mac_length = mac_size;
2586 /* If mac_size is 0 then mac may be NULL and then the
2587 * call to memset would have undefined behavior. */
2588 if( mac_size != 0 )
2589 memset( mac, '!', mac_size );
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 Peskine89167cb2018-07-08 20:12:23 +02002594 }
mohammad16036df908f2018-04-02 08:34:15 -07002595
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002596 status = psa_mac_finish_internal( operation, mac, mac_size );
2597
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002598 if( status == PSA_SUCCESS )
2599 {
2600 status = psa_mac_abort( operation );
2601 if( status == PSA_SUCCESS )
2602 *mac_length = operation->mac_size;
2603 else
2604 memset( mac, '!', mac_size );
2605 }
2606 else
2607 psa_mac_abort( operation );
2608 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002609}
2610
Gilles Peskineacd4be32018-07-08 19:56:25 +02002611psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2612 const uint8_t *mac,
2613 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614{
Gilles Peskine828ed142018-06-18 23:25:51 +02002615 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002616 psa_status_t status;
2617
Jaeden Amero252ef282019-02-15 14:05:35 +00002618 if( operation->alg == 0 )
2619 {
2620 return( PSA_ERROR_BAD_STATE );
2621 }
2622
Gilles Peskine89167cb2018-07-08 20:12:23 +02002623 if( operation->is_sign )
2624 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002625 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002626 }
2627 if( operation->mac_size != mac_length )
2628 {
2629 status = PSA_ERROR_INVALID_SIGNATURE;
2630 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002631 }
mohammad16036df908f2018-04-02 08:34:15 -07002632
2633 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002634 actual_mac, sizeof( actual_mac ) );
2635
2636 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2637 status = PSA_ERROR_INVALID_SIGNATURE;
2638
2639cleanup:
2640 if( status == PSA_SUCCESS )
2641 status = psa_mac_abort( operation );
2642 else
2643 psa_mac_abort( operation );
2644
Gilles Peskine3f108122018-12-07 18:14:53 +01002645 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002646
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002647 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648}
2649
2650
Gilles Peskine20035e32018-02-03 22:44:14 +01002651
Gilles Peskine20035e32018-02-03 22:44:14 +01002652/****************************************************************/
2653/* Asymmetric cryptography */
2654/****************************************************************/
2655
Gilles Peskine2b450e32018-06-27 15:42:46 +02002656#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002657/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002658 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002659static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2660 size_t hash_length,
2661 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002662{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002663 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002664 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002665 *md_alg = mbedtls_md_get_type( md_info );
2666
2667 /* The Mbed TLS RSA module uses an unsigned int for hash length
2668 * parameters. Validate that it fits so that we don't risk an
2669 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002670#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002671 if( hash_length > UINT_MAX )
2672 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002673#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002674
2675#if defined(MBEDTLS_PKCS1_V15)
2676 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2677 * must be correct. */
2678 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2679 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002680 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002681 if( md_info == NULL )
2682 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002683 if( mbedtls_md_get_size( md_info ) != hash_length )
2684 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002685 }
2686#endif /* MBEDTLS_PKCS1_V15 */
2687
2688#if defined(MBEDTLS_PKCS1_V21)
2689 /* PSS requires a hash internally. */
2690 if( PSA_ALG_IS_RSA_PSS( alg ) )
2691 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002692 if( md_info == NULL )
2693 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002694 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002695#endif /* MBEDTLS_PKCS1_V21 */
2696
Gilles Peskine61b91d42018-06-08 16:09:36 +02002697 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002698}
2699
Gilles Peskine2b450e32018-06-27 15:42:46 +02002700static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2701 psa_algorithm_t alg,
2702 const uint8_t *hash,
2703 size_t hash_length,
2704 uint8_t *signature,
2705 size_t signature_size,
2706 size_t *signature_length )
2707{
2708 psa_status_t status;
2709 int ret;
2710 mbedtls_md_type_t md_alg;
2711
2712 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2713 if( status != PSA_SUCCESS )
2714 return( status );
2715
Gilles Peskine630a18a2018-06-29 17:49:35 +02002716 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002717 return( PSA_ERROR_BUFFER_TOO_SMALL );
2718
2719#if defined(MBEDTLS_PKCS1_V15)
2720 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2721 {
2722 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2723 MBEDTLS_MD_NONE );
2724 ret = mbedtls_rsa_pkcs1_sign( rsa,
2725 mbedtls_ctr_drbg_random,
2726 &global_data.ctr_drbg,
2727 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002728 md_alg,
2729 (unsigned int) hash_length,
2730 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002731 signature );
2732 }
2733 else
2734#endif /* MBEDTLS_PKCS1_V15 */
2735#if defined(MBEDTLS_PKCS1_V21)
2736 if( PSA_ALG_IS_RSA_PSS( alg ) )
2737 {
2738 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2739 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2740 mbedtls_ctr_drbg_random,
2741 &global_data.ctr_drbg,
2742 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002743 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002744 (unsigned int) hash_length,
2745 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002746 signature );
2747 }
2748 else
2749#endif /* MBEDTLS_PKCS1_V21 */
2750 {
2751 return( PSA_ERROR_INVALID_ARGUMENT );
2752 }
2753
2754 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002755 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002756 return( mbedtls_to_psa_error( ret ) );
2757}
2758
2759static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2760 psa_algorithm_t alg,
2761 const uint8_t *hash,
2762 size_t hash_length,
2763 const uint8_t *signature,
2764 size_t signature_length )
2765{
2766 psa_status_t status;
2767 int ret;
2768 mbedtls_md_type_t md_alg;
2769
2770 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2771 if( status != PSA_SUCCESS )
2772 return( status );
2773
Gilles Peskine630a18a2018-06-29 17:49:35 +02002774 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002775 return( PSA_ERROR_BUFFER_TOO_SMALL );
2776
2777#if defined(MBEDTLS_PKCS1_V15)
2778 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2779 {
2780 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2781 MBEDTLS_MD_NONE );
2782 ret = mbedtls_rsa_pkcs1_verify( rsa,
2783 mbedtls_ctr_drbg_random,
2784 &global_data.ctr_drbg,
2785 MBEDTLS_RSA_PUBLIC,
2786 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002787 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002788 hash,
2789 signature );
2790 }
2791 else
2792#endif /* MBEDTLS_PKCS1_V15 */
2793#if defined(MBEDTLS_PKCS1_V21)
2794 if( PSA_ALG_IS_RSA_PSS( alg ) )
2795 {
2796 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2797 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2798 mbedtls_ctr_drbg_random,
2799 &global_data.ctr_drbg,
2800 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002801 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002802 (unsigned int) hash_length,
2803 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002804 signature );
2805 }
2806 else
2807#endif /* MBEDTLS_PKCS1_V21 */
2808 {
2809 return( PSA_ERROR_INVALID_ARGUMENT );
2810 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002811
2812 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2813 * the rest of the signature is invalid". This has little use in
2814 * practice and PSA doesn't report this distinction. */
2815 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2816 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002817 return( mbedtls_to_psa_error( ret ) );
2818}
2819#endif /* MBEDTLS_RSA_C */
2820
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002821#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002822/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2823 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2824 * (even though these functions don't modify it). */
2825static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2826 psa_algorithm_t alg,
2827 const uint8_t *hash,
2828 size_t hash_length,
2829 uint8_t *signature,
2830 size_t signature_size,
2831 size_t *signature_length )
2832{
2833 int ret;
2834 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002835 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002836 mbedtls_mpi_init( &r );
2837 mbedtls_mpi_init( &s );
2838
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002839 if( signature_size < 2 * curve_bytes )
2840 {
2841 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2842 goto cleanup;
2843 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002844
Gilles Peskinea05219c2018-11-16 16:02:56 +01002845#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002846 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2847 {
2848 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2849 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2850 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2851 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2852 hash, hash_length,
2853 md_alg ) );
2854 }
2855 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002856#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002857 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002858 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002859 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2860 hash, hash_length,
2861 mbedtls_ctr_drbg_random,
2862 &global_data.ctr_drbg ) );
2863 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002864
2865 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2866 signature,
2867 curve_bytes ) );
2868 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2869 signature + curve_bytes,
2870 curve_bytes ) );
2871
2872cleanup:
2873 mbedtls_mpi_free( &r );
2874 mbedtls_mpi_free( &s );
2875 if( ret == 0 )
2876 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002877 return( mbedtls_to_psa_error( ret ) );
2878}
2879
2880static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2881 const uint8_t *hash,
2882 size_t hash_length,
2883 const uint8_t *signature,
2884 size_t signature_length )
2885{
2886 int ret;
2887 mbedtls_mpi r, s;
2888 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2889 mbedtls_mpi_init( &r );
2890 mbedtls_mpi_init( &s );
2891
2892 if( signature_length != 2 * curve_bytes )
2893 return( PSA_ERROR_INVALID_SIGNATURE );
2894
2895 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2896 signature,
2897 curve_bytes ) );
2898 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2899 signature + curve_bytes,
2900 curve_bytes ) );
2901
2902 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2903 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002904
2905cleanup:
2906 mbedtls_mpi_free( &r );
2907 mbedtls_mpi_free( &s );
2908 return( mbedtls_to_psa_error( ret ) );
2909}
2910#endif /* MBEDTLS_ECDSA_C */
2911
Gilles Peskinec5487a82018-12-03 18:08:14 +01002912psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002913 psa_algorithm_t alg,
2914 const uint8_t *hash,
2915 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002916 uint8_t *signature,
2917 size_t signature_size,
2918 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002919{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002920 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002921 psa_status_t status;
2922
2923 *signature_length = signature_size;
2924
Gilles Peskinec5487a82018-12-03 18:08:14 +01002925 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002926 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002927 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002928 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002929 {
2930 status = PSA_ERROR_INVALID_ARGUMENT;
2931 goto exit;
2932 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002933
Gilles Peskine20035e32018-02-03 22:44:14 +01002934#if defined(MBEDTLS_RSA_C)
2935 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2936 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002937 status = psa_rsa_sign( slot->data.rsa,
2938 alg,
2939 hash, hash_length,
2940 signature, signature_size,
2941 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002942 }
2943 else
2944#endif /* defined(MBEDTLS_RSA_C) */
2945#if defined(MBEDTLS_ECP_C)
2946 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2947 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002948#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002949 if(
2950#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2951 PSA_ALG_IS_ECDSA( alg )
2952#else
2953 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2954#endif
2955 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002956 status = psa_ecdsa_sign( slot->data.ecp,
2957 alg,
2958 hash, hash_length,
2959 signature, signature_size,
2960 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002961 else
2962#endif /* defined(MBEDTLS_ECDSA_C) */
2963 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002964 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002965 }
itayzafrir5c753392018-05-08 11:18:38 +03002966 }
2967 else
2968#endif /* defined(MBEDTLS_ECP_C) */
2969 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002970 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002971 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002972
2973exit:
2974 /* Fill the unused part of the output buffer (the whole buffer on error,
2975 * the trailing part on success) with something that isn't a valid mac
2976 * (barring an attack on the mac and deliberately-crafted input),
2977 * in case the caller doesn't check the return status properly. */
2978 if( status == PSA_SUCCESS )
2979 memset( signature + *signature_length, '!',
2980 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002981 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002982 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002983 /* If signature_size is 0 then we have nothing to do. We must not call
2984 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002985 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002986}
2987
Gilles Peskinec5487a82018-12-03 18:08:14 +01002988psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002989 psa_algorithm_t alg,
2990 const uint8_t *hash,
2991 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002992 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002993 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002994{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002995 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002996 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002997
Gilles Peskinec5487a82018-12-03 18:08:14 +01002998 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002999 if( status != PSA_SUCCESS )
3000 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003001
Gilles Peskine61b91d42018-06-08 16:09:36 +02003002#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003003 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003004 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003005 return( psa_rsa_verify( slot->data.rsa,
3006 alg,
3007 hash, hash_length,
3008 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003009 }
3010 else
3011#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003012#if defined(MBEDTLS_ECP_C)
3013 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3014 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003015#if defined(MBEDTLS_ECDSA_C)
3016 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003017 return( psa_ecdsa_verify( slot->data.ecp,
3018 hash, hash_length,
3019 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003020 else
3021#endif /* defined(MBEDTLS_ECDSA_C) */
3022 {
3023 return( PSA_ERROR_INVALID_ARGUMENT );
3024 }
itayzafrir5c753392018-05-08 11:18:38 +03003025 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003026 else
3027#endif /* defined(MBEDTLS_ECP_C) */
3028 {
3029 return( PSA_ERROR_NOT_SUPPORTED );
3030 }
3031}
3032
Gilles Peskine072ac562018-06-30 00:21:29 +02003033#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3034static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3035 mbedtls_rsa_context *rsa )
3036{
3037 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3038 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3039 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3040 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3041}
3042#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3043
Gilles Peskinec5487a82018-12-03 18:08:14 +01003044psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003045 psa_algorithm_t alg,
3046 const uint8_t *input,
3047 size_t input_length,
3048 const uint8_t *salt,
3049 size_t salt_length,
3050 uint8_t *output,
3051 size_t output_size,
3052 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003053{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003054 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003055 psa_status_t status;
3056
Darryl Green5cc689a2018-07-24 15:34:10 +01003057 (void) input;
3058 (void) input_length;
3059 (void) salt;
3060 (void) output;
3061 (void) output_size;
3062
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003063 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003064
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003065 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3066 return( PSA_ERROR_INVALID_ARGUMENT );
3067
Gilles Peskinec5487a82018-12-03 18:08:14 +01003068 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003069 if( status != PSA_SUCCESS )
3070 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003071 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
3072 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003073 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003074
3075#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003076 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003077 {
3078 mbedtls_rsa_context *rsa = slot->data.rsa;
3079 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003080 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003081 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003082#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003083 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003084 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003085 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3086 mbedtls_ctr_drbg_random,
3087 &global_data.ctr_drbg,
3088 MBEDTLS_RSA_PUBLIC,
3089 input_length,
3090 input,
3091 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003092 }
3093 else
3094#endif /* MBEDTLS_PKCS1_V15 */
3095#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003096 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003097 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003098 psa_rsa_oaep_set_padding_mode( alg, rsa );
3099 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3100 mbedtls_ctr_drbg_random,
3101 &global_data.ctr_drbg,
3102 MBEDTLS_RSA_PUBLIC,
3103 salt, salt_length,
3104 input_length,
3105 input,
3106 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003107 }
3108 else
3109#endif /* MBEDTLS_PKCS1_V21 */
3110 {
3111 return( PSA_ERROR_INVALID_ARGUMENT );
3112 }
3113 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003114 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115 return( mbedtls_to_psa_error( ret ) );
3116 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003117 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003118#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003119 {
3120 return( PSA_ERROR_NOT_SUPPORTED );
3121 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003122}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003123
Gilles Peskinec5487a82018-12-03 18:08:14 +01003124psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003125 psa_algorithm_t alg,
3126 const uint8_t *input,
3127 size_t input_length,
3128 const uint8_t *salt,
3129 size_t salt_length,
3130 uint8_t *output,
3131 size_t output_size,
3132 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003133{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003134 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003135 psa_status_t status;
3136
Darryl Green5cc689a2018-07-24 15:34:10 +01003137 (void) input;
3138 (void) input_length;
3139 (void) salt;
3140 (void) output;
3141 (void) output_size;
3142
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003143 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003144
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003145 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3146 return( PSA_ERROR_INVALID_ARGUMENT );
3147
Gilles Peskinec5487a82018-12-03 18:08:14 +01003148 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003149 if( status != PSA_SUCCESS )
3150 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003151 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
3152 return( PSA_ERROR_INVALID_ARGUMENT );
3153
3154#if defined(MBEDTLS_RSA_C)
3155 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
3156 {
3157 mbedtls_rsa_context *rsa = slot->data.rsa;
3158 int ret;
3159
Gilles Peskine630a18a2018-06-29 17:49:35 +02003160 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003161 return( PSA_ERROR_INVALID_ARGUMENT );
3162
3163#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003164 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003165 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003166 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3167 mbedtls_ctr_drbg_random,
3168 &global_data.ctr_drbg,
3169 MBEDTLS_RSA_PRIVATE,
3170 output_length,
3171 input,
3172 output,
3173 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003174 }
3175 else
3176#endif /* MBEDTLS_PKCS1_V15 */
3177#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003178 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003179 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003180 psa_rsa_oaep_set_padding_mode( alg, rsa );
3181 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3182 mbedtls_ctr_drbg_random,
3183 &global_data.ctr_drbg,
3184 MBEDTLS_RSA_PRIVATE,
3185 salt, salt_length,
3186 output_length,
3187 input,
3188 output,
3189 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190 }
3191 else
3192#endif /* MBEDTLS_PKCS1_V21 */
3193 {
3194 return( PSA_ERROR_INVALID_ARGUMENT );
3195 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003196
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003197 return( mbedtls_to_psa_error( ret ) );
3198 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003199 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003200#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201 {
3202 return( PSA_ERROR_NOT_SUPPORTED );
3203 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003204}
Gilles Peskine20035e32018-02-03 22:44:14 +01003205
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003206
3207
mohammad1603503973b2018-03-12 15:59:30 +02003208/****************************************************************/
3209/* Symmetric cryptography */
3210/****************************************************************/
3211
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003212/* Initialize the cipher operation structure. Once this function has been
3213 * called, psa_cipher_abort can run and will do the right thing. */
3214static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3215 psa_algorithm_t alg )
3216{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003217 if( ! PSA_ALG_IS_CIPHER( alg ) )
3218 {
3219 memset( operation, 0, sizeof( *operation ) );
3220 return( PSA_ERROR_INVALID_ARGUMENT );
3221 }
3222
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003223 operation->alg = alg;
3224 operation->key_set = 0;
3225 operation->iv_set = 0;
3226 operation->iv_required = 1;
3227 operation->iv_size = 0;
3228 operation->block_size = 0;
3229 mbedtls_cipher_init( &operation->ctx.cipher );
3230 return( PSA_SUCCESS );
3231}
3232
Gilles Peskinee553c652018-06-04 16:22:46 +02003233static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003234 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003235 psa_algorithm_t alg,
3236 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003237{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003238 int ret = 0;
3239 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003240 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003241 size_t key_bits;
3242 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003243 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3244 PSA_KEY_USAGE_ENCRYPT :
3245 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003246
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003247 /* A context must be freshly initialized before it can be set up. */
3248 if( operation->alg != 0 )
3249 {
3250 return( PSA_ERROR_BAD_STATE );
3251 }
3252
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003253 status = psa_cipher_init( operation, alg );
3254 if( status != PSA_SUCCESS )
3255 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003256
Gilles Peskinec5487a82018-12-03 18:08:14 +01003257 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003258 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003259 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003260 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003261
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003262 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003263 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003264 {
3265 status = PSA_ERROR_NOT_SUPPORTED;
3266 goto exit;
3267 }
mohammad1603503973b2018-03-12 15:59:30 +02003268
mohammad1603503973b2018-03-12 15:59:30 +02003269 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003270 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003271 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003272
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003273#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003274 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003275 {
3276 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3277 unsigned char keys[24];
3278 memcpy( keys, slot->data.raw.data, 16 );
3279 memcpy( keys + 16, slot->data.raw.data, 8 );
3280 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3281 keys,
3282 192, cipher_operation );
3283 }
3284 else
3285#endif
3286 {
3287 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3288 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003289 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003290 }
Moran Peker41deec42018-04-04 15:43:05 +03003291 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003292 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003293
mohammad16038481e742018-03-18 13:57:31 +02003294#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003295 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003296 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003297 case PSA_ALG_CBC_NO_PADDING:
3298 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3299 MBEDTLS_PADDING_NONE );
3300 break;
3301 case PSA_ALG_CBC_PKCS7:
3302 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3303 MBEDTLS_PADDING_PKCS7 );
3304 break;
3305 default:
3306 /* The algorithm doesn't involve padding. */
3307 ret = 0;
3308 break;
3309 }
3310 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003311 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003312#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3313
mohammad1603503973b2018-03-12 15:59:30 +02003314 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003315 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3316 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3317 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003318 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003319 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003320 }
mohammad1603503973b2018-03-12 15:59:30 +02003321
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003322exit:
3323 if( status == 0 )
3324 status = mbedtls_to_psa_error( ret );
3325 if( status != 0 )
3326 psa_cipher_abort( operation );
3327 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003328}
3329
Gilles Peskinefe119512018-07-08 21:39:34 +02003330psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003331 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003332 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003333{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003334 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003335}
3336
Gilles Peskinefe119512018-07-08 21:39:34 +02003337psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003338 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003339 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003340{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003341 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003342}
3343
Gilles Peskinefe119512018-07-08 21:39:34 +02003344psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3345 unsigned char *iv,
3346 size_t iv_size,
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 Peker41deec42018-04-04 15:43:05 +03003355 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003356 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003357 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003358 goto exit;
3359 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003360 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3361 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003362 if( ret != 0 )
3363 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003364 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003365 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003366 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003367
mohammad16038481e742018-03-18 13:57:31 +02003368 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003369 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003370
Moran Peker395db872018-05-31 14:07:14 +03003371exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003372 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003373 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003374 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003375}
3376
Gilles Peskinefe119512018-07-08 21:39:34 +02003377psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3378 const unsigned char *iv,
3379 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003380{
itayzafrir534bd7c2018-08-02 13:56:32 +03003381 psa_status_t status;
3382 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003383 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003384 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003385 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003386 }
Moran Pekera28258c2018-05-29 16:25:04 +03003387 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003388 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003389 status = PSA_ERROR_INVALID_ARGUMENT;
3390 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003391 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003392 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3393 status = mbedtls_to_psa_error( ret );
3394exit:
3395 if( status == PSA_SUCCESS )
3396 operation->iv_set = 1;
3397 else
mohammad1603503973b2018-03-12 15:59:30 +02003398 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003399 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003400}
3401
Gilles Peskinee553c652018-06-04 16:22:46 +02003402psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3403 const uint8_t *input,
3404 size_t input_length,
3405 unsigned char *output,
3406 size_t output_size,
3407 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003408{
itayzafrir534bd7c2018-08-02 13:56:32 +03003409 psa_status_t status;
3410 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003411 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003412
3413 if( operation->alg == 0 )
3414 {
3415 return( PSA_ERROR_BAD_STATE );
3416 }
3417
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003418 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003419 {
3420 /* Take the unprocessed partial block left over from previous
3421 * update calls, if any, plus the input to this call. Remove
3422 * the last partial block, if any. You get the data that will be
3423 * output in this call. */
3424 expected_output_size =
3425 ( operation->ctx.cipher.unprocessed_len + input_length )
3426 / operation->block_size * operation->block_size;
3427 }
3428 else
3429 {
3430 expected_output_size = input_length;
3431 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003432
Gilles Peskine89d789c2018-06-04 17:17:16 +02003433 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003434 {
3435 status = PSA_ERROR_BUFFER_TOO_SMALL;
3436 goto exit;
3437 }
mohammad160382759612018-03-12 18:16:40 +02003438
mohammad1603503973b2018-03-12 15:59:30 +02003439 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003440 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003441 status = mbedtls_to_psa_error( ret );
3442exit:
3443 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003444 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003445 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003446}
3447
Gilles Peskinee553c652018-06-04 16:22:46 +02003448psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3449 uint8_t *output,
3450 size_t output_size,
3451 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003452{
David Saadab4ecc272019-02-14 13:48:10 +02003453 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003454 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003455 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003456
mohammad1603503973b2018-03-12 15:59:30 +02003457 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003458 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003459 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003460 }
3461 if( operation->iv_required && ! operation->iv_set )
3462 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003463 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003464 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003465
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003466 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003467 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3468 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003469 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003470 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003471 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003472 }
3473
Janos Follath315b51c2018-07-09 16:04:51 +01003474 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3475 temp_output_buffer,
3476 output_length );
3477 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003478 {
Janos Follath315b51c2018-07-09 16:04:51 +01003479 status = mbedtls_to_psa_error( cipher_ret );
3480 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003481 }
Janos Follath315b51c2018-07-09 16:04:51 +01003482
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003483 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003484 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003485 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003486 memcpy( output, temp_output_buffer, *output_length );
3487 else
3488 {
Janos Follath315b51c2018-07-09 16:04:51 +01003489 status = PSA_ERROR_BUFFER_TOO_SMALL;
3490 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003491 }
mohammad1603503973b2018-03-12 15:59:30 +02003492
Gilles Peskine3f108122018-12-07 18:14:53 +01003493 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003494 status = psa_cipher_abort( operation );
3495
3496 return( status );
3497
3498error:
3499
3500 *output_length = 0;
3501
Gilles Peskine3f108122018-12-07 18:14:53 +01003502 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003503 (void) psa_cipher_abort( operation );
3504
3505 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003506}
3507
Gilles Peskinee553c652018-06-04 16:22:46 +02003508psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3509{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003510 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003511 {
3512 /* The object has (apparently) been initialized but it is not
3513 * in use. It's ok to call abort on such an object, and there's
3514 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003515 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003516 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003517
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003518 /* Sanity check (shouldn't happen: operation->alg should
3519 * always have been initialized to a valid value). */
3520 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3521 return( PSA_ERROR_BAD_STATE );
3522
mohammad1603503973b2018-03-12 15:59:30 +02003523 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003524
Moran Peker41deec42018-04-04 15:43:05 +03003525 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003526 operation->key_set = 0;
3527 operation->iv_set = 0;
3528 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003529 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003530 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003531
Moran Peker395db872018-05-31 14:07:14 +03003532 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003533}
3534
Gilles Peskinea0655c32018-04-30 17:06:50 +02003535
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003536
mohammad16038cc1cee2018-03-28 01:21:33 +03003537/****************************************************************/
3538/* Key Policy */
3539/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003540
mohammad160327010052018-07-03 13:16:15 +03003541#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003542void psa_key_policy_set_usage( psa_key_policy_t *policy,
3543 psa_key_usage_t usage,
3544 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003545{
mohammad16034eed7572018-03-28 05:14:59 -07003546 policy->usage = usage;
3547 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003548}
3549
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003550psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003551{
mohammad16036df908f2018-04-02 08:34:15 -07003552 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003553}
3554
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003555psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003556{
mohammad16036df908f2018-04-02 08:34:15 -07003557 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003558}
mohammad160327010052018-07-03 13:16:15 +03003559#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003560
Gilles Peskinec5487a82018-12-03 18:08:14 +01003561psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003562 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003563{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003564 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003565 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003566
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003567 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003568 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003569
Gilles Peskinec5487a82018-12-03 18:08:14 +01003570 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003571 if( status != PSA_SUCCESS )
3572 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003573
Gilles Peskine4747d192019-04-17 15:05:45 +02003574 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003575}
3576
Gilles Peskinec5487a82018-12-03 18:08:14 +01003577psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003578 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003579{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003580 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003581 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003582
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003583 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003584 return( PSA_ERROR_INVALID_ARGUMENT );
3585
Gilles Peskinec5487a82018-12-03 18:08:14 +01003586 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003587 if( status != PSA_SUCCESS )
3588 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003589
mohammad16036df908f2018-04-02 08:34:15 -07003590 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003591
3592 return( PSA_SUCCESS );
3593}
Gilles Peskine20035e32018-02-03 22:44:14 +01003594
Gilles Peskinea0655c32018-04-30 17:06:50 +02003595
3596
mohammad1603804cd712018-03-20 22:44:08 +02003597/****************************************************************/
3598/* Key Lifetime */
3599/****************************************************************/
3600
Gilles Peskine87a5e562019-04-17 12:28:25 +02003601psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003602 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003603{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003604 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003605 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003606
Gilles Peskinec5487a82018-12-03 18:08:14 +01003607 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003608 if( status != PSA_SUCCESS )
3609 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003610
mohammad1603804cd712018-03-20 22:44:08 +02003611 *lifetime = slot->lifetime;
3612
3613 return( PSA_SUCCESS );
3614}
3615
Gilles Peskine20035e32018-02-03 22:44:14 +01003616
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003617
mohammad16035955c982018-04-26 00:53:03 +03003618/****************************************************************/
3619/* AEAD */
3620/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003621
Gilles Peskineedf9a652018-08-17 18:11:56 +02003622typedef struct
3623{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003624 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003625 const mbedtls_cipher_info_t *cipher_info;
3626 union
3627 {
3628#if defined(MBEDTLS_CCM_C)
3629 mbedtls_ccm_context ccm;
3630#endif /* MBEDTLS_CCM_C */
3631#if defined(MBEDTLS_GCM_C)
3632 mbedtls_gcm_context gcm;
3633#endif /* MBEDTLS_GCM_C */
3634 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003635 psa_algorithm_t core_alg;
3636 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003637 uint8_t tag_length;
3638} aead_operation_t;
3639
Gilles Peskine30a9e412019-01-14 18:36:12 +01003640static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003641{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003642 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003643 {
3644#if defined(MBEDTLS_CCM_C)
3645 case PSA_ALG_CCM:
3646 mbedtls_ccm_free( &operation->ctx.ccm );
3647 break;
3648#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003649#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003650 case PSA_ALG_GCM:
3651 mbedtls_gcm_free( &operation->ctx.gcm );
3652 break;
3653#endif /* MBEDTLS_GCM_C */
3654 }
3655}
3656
3657static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003658 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003659 psa_key_usage_t usage,
3660 psa_algorithm_t alg )
3661{
3662 psa_status_t status;
3663 size_t key_bits;
3664 mbedtls_cipher_id_t cipher_id;
3665
Gilles Peskinec5487a82018-12-03 18:08:14 +01003666 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003667 if( status != PSA_SUCCESS )
3668 return( status );
3669
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003670 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003671
3672 operation->cipher_info =
3673 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3674 &cipher_id );
3675 if( operation->cipher_info == NULL )
3676 return( PSA_ERROR_NOT_SUPPORTED );
3677
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003678 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003679 {
3680#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003681 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3682 operation->core_alg = PSA_ALG_CCM;
3683 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003684 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3685 return( PSA_ERROR_INVALID_ARGUMENT );
3686 mbedtls_ccm_init( &operation->ctx.ccm );
3687 status = mbedtls_to_psa_error(
3688 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3689 operation->slot->data.raw.data,
3690 (unsigned int) key_bits ) );
3691 if( status != 0 )
3692 goto cleanup;
3693 break;
3694#endif /* MBEDTLS_CCM_C */
3695
3696#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003697 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3698 operation->core_alg = PSA_ALG_GCM;
3699 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003700 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3701 return( PSA_ERROR_INVALID_ARGUMENT );
3702 mbedtls_gcm_init( &operation->ctx.gcm );
3703 status = mbedtls_to_psa_error(
3704 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3705 operation->slot->data.raw.data,
3706 (unsigned int) key_bits ) );
3707 break;
3708#endif /* MBEDTLS_GCM_C */
3709
3710 default:
3711 return( PSA_ERROR_NOT_SUPPORTED );
3712 }
3713
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003714 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3715 {
3716 status = PSA_ERROR_INVALID_ARGUMENT;
3717 goto cleanup;
3718 }
3719 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3720 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3721 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3722 * In both cases, mbedtls_xxx will validate the tag length below. */
3723
Gilles Peskineedf9a652018-08-17 18:11:56 +02003724 return( PSA_SUCCESS );
3725
3726cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003727 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003728 return( status );
3729}
3730
Gilles Peskinec5487a82018-12-03 18:08:14 +01003731psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003732 psa_algorithm_t alg,
3733 const uint8_t *nonce,
3734 size_t nonce_length,
3735 const uint8_t *additional_data,
3736 size_t additional_data_length,
3737 const uint8_t *plaintext,
3738 size_t plaintext_length,
3739 uint8_t *ciphertext,
3740 size_t ciphertext_size,
3741 size_t *ciphertext_length )
3742{
mohammad16035955c982018-04-26 00:53:03 +03003743 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003744 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003745 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003746
mohammad1603f08a5502018-06-03 15:05:47 +03003747 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003748
Gilles Peskinec5487a82018-12-03 18:08:14 +01003749 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003750 if( status != PSA_SUCCESS )
3751 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003752
Gilles Peskineedf9a652018-08-17 18:11:56 +02003753 /* For all currently supported modes, the tag is at the end of the
3754 * ciphertext. */
3755 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3756 {
3757 status = PSA_ERROR_BUFFER_TOO_SMALL;
3758 goto exit;
3759 }
3760 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003761
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003762#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003763 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003764 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003765 status = mbedtls_to_psa_error(
3766 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3767 MBEDTLS_GCM_ENCRYPT,
3768 plaintext_length,
3769 nonce, nonce_length,
3770 additional_data, additional_data_length,
3771 plaintext, ciphertext,
3772 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003773 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003774 else
3775#endif /* MBEDTLS_GCM_C */
3776#if defined(MBEDTLS_CCM_C)
3777 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003778 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003779 status = mbedtls_to_psa_error(
3780 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3781 plaintext_length,
3782 nonce, nonce_length,
3783 additional_data,
3784 additional_data_length,
3785 plaintext, ciphertext,
3786 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003787 }
mohammad16035c8845f2018-05-09 05:40:09 -07003788 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003789#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003790 {
mohammad1603554faad2018-06-03 15:07:38 +03003791 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003792 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003793
Gilles Peskineedf9a652018-08-17 18:11:56 +02003794 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3795 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003796
Gilles Peskineedf9a652018-08-17 18:11:56 +02003797exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003798 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003799 if( status == PSA_SUCCESS )
3800 *ciphertext_length = plaintext_length + operation.tag_length;
3801 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003802}
3803
Gilles Peskineee652a32018-06-01 19:23:52 +02003804/* Locate the tag in a ciphertext buffer containing the encrypted data
3805 * followed by the tag. Return the length of the part preceding the tag in
3806 * *plaintext_length. This is the size of the plaintext in modes where
3807 * the encrypted data has the same size as the plaintext, such as
3808 * CCM and GCM. */
3809static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3810 const uint8_t *ciphertext,
3811 size_t ciphertext_length,
3812 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003813 const uint8_t **p_tag )
3814{
3815 size_t payload_length;
3816 if( tag_length > ciphertext_length )
3817 return( PSA_ERROR_INVALID_ARGUMENT );
3818 payload_length = ciphertext_length - tag_length;
3819 if( payload_length > plaintext_size )
3820 return( PSA_ERROR_BUFFER_TOO_SMALL );
3821 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003822 return( PSA_SUCCESS );
3823}
3824
Gilles Peskinec5487a82018-12-03 18:08:14 +01003825psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003826 psa_algorithm_t alg,
3827 const uint8_t *nonce,
3828 size_t nonce_length,
3829 const uint8_t *additional_data,
3830 size_t additional_data_length,
3831 const uint8_t *ciphertext,
3832 size_t ciphertext_length,
3833 uint8_t *plaintext,
3834 size_t plaintext_size,
3835 size_t *plaintext_length )
3836{
mohammad16035955c982018-04-26 00:53:03 +03003837 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003838 aead_operation_t operation;
3839 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003840
Gilles Peskineee652a32018-06-01 19:23:52 +02003841 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003842
Gilles Peskinec5487a82018-12-03 18:08:14 +01003843 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003844 if( status != PSA_SUCCESS )
3845 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003846
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003847#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003848 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003849 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003850 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003851 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003852 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003853 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003854 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003855
Gilles Peskineedf9a652018-08-17 18:11:56 +02003856 status = mbedtls_to_psa_error(
3857 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3858 ciphertext_length - operation.tag_length,
3859 nonce, nonce_length,
3860 additional_data,
3861 additional_data_length,
3862 tag, operation.tag_length,
3863 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003864 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003865 else
3866#endif /* MBEDTLS_GCM_C */
3867#if defined(MBEDTLS_CCM_C)
3868 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003869 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003870 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003871 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003872 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003873 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003874 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003875
Gilles Peskineedf9a652018-08-17 18:11:56 +02003876 status = mbedtls_to_psa_error(
3877 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3878 ciphertext_length - operation.tag_length,
3879 nonce, nonce_length,
3880 additional_data,
3881 additional_data_length,
3882 ciphertext, plaintext,
3883 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003884 }
mohammad160339574652018-06-01 04:39:53 -07003885 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003886#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003887 {
mohammad1603554faad2018-06-03 15:07:38 +03003888 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003889 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003890
Gilles Peskineedf9a652018-08-17 18:11:56 +02003891 if( status != PSA_SUCCESS && plaintext_size != 0 )
3892 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003893
Gilles Peskineedf9a652018-08-17 18:11:56 +02003894exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003895 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003896 if( status == PSA_SUCCESS )
3897 *plaintext_length = ciphertext_length - operation.tag_length;
3898 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003899}
3900
Gilles Peskinea0655c32018-04-30 17:06:50 +02003901
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003902
Gilles Peskine20035e32018-02-03 22:44:14 +01003903/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003904/* Generators */
3905/****************************************************************/
3906
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003907#define HKDF_STATE_INIT 0 /* no input yet */
3908#define HKDF_STATE_STARTED 1 /* got salt */
3909#define HKDF_STATE_KEYED 2 /* got key */
3910#define HKDF_STATE_OUTPUT 3 /* output started */
3911
Gilles Peskine969c5d62019-01-16 15:53:06 +01003912static psa_algorithm_t psa_generator_get_kdf_alg(
3913 const psa_crypto_generator_t *generator )
3914{
3915 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3916 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3917 else
3918 return( generator->alg );
3919}
3920
3921
Gilles Peskineeab56e42018-07-12 17:12:33 +02003922psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3923{
3924 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003925 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3926 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003927 {
3928 /* The object has (apparently) been initialized but it is not
3929 * in use. It's ok to call abort on such an object, and there's
3930 * nothing to do. */
3931 }
3932 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003933 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003934 {
3935 if( generator->ctx.buffer.data != NULL )
3936 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003937 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003938 generator->ctx.buffer.size );
3939 mbedtls_free( generator->ctx.buffer.data );
3940 }
3941 }
3942 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003943#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003944 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003945 {
3946 mbedtls_free( generator->ctx.hkdf.info );
3947 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3948 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003949 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00003950 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003951 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003952 {
3953 if( generator->ctx.tls12_prf.key != NULL )
3954 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003955 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003956 generator->ctx.tls12_prf.key_len );
3957 mbedtls_free( generator->ctx.tls12_prf.key );
3958 }
Hanno Becker580fba12018-11-13 20:50:45 +00003959
3960 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3961 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003962 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003963 generator->ctx.tls12_prf.Ai_with_seed_len );
3964 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3965 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003966 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003967 else
3968#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003969 {
3970 status = PSA_ERROR_BAD_STATE;
3971 }
3972 memset( generator, 0, sizeof( *generator ) );
3973 return( status );
3974}
3975
Gilles Peskineeab56e42018-07-12 17:12:33 +02003976psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3977 size_t *capacity)
3978{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003979 if( generator->alg == 0 )
3980 {
3981 /* This is a blank generator. */
3982 return PSA_ERROR_BAD_STATE;
3983 }
3984
Gilles Peskineeab56e42018-07-12 17:12:33 +02003985 *capacity = generator->capacity;
3986 return( PSA_SUCCESS );
3987}
3988
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003989psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
3990 size_t capacity )
3991{
3992 if( generator->alg == 0 )
3993 return( PSA_ERROR_BAD_STATE );
3994 if( capacity > generator->capacity )
3995 return( PSA_ERROR_INVALID_ARGUMENT );
3996 generator->capacity = capacity;
3997 return( PSA_SUCCESS );
3998}
3999
Gilles Peskinebef7f142018-07-12 17:22:21 +02004000#if defined(MBEDTLS_MD_C)
4001/* Read some bytes from an HKDF-based generator. This performs a chunk
4002 * of the expand phase of the HKDF algorithm. */
4003static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
4004 psa_algorithm_t hash_alg,
4005 uint8_t *output,
4006 size_t output_length )
4007{
4008 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4009 psa_status_t status;
4010
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004011 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4012 return( PSA_ERROR_BAD_STATE );
4013 hkdf->state = HKDF_STATE_OUTPUT;
4014
Gilles Peskinebef7f142018-07-12 17:22:21 +02004015 while( output_length != 0 )
4016 {
4017 /* Copy what remains of the current block */
4018 uint8_t n = hash_length - hkdf->offset_in_block;
4019 if( n > output_length )
4020 n = (uint8_t) output_length;
4021 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4022 output += n;
4023 output_length -= n;
4024 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004025 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004026 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004027 /* We can't be wanting more output after block 0xff, otherwise
4028 * the capacity check in psa_generator_read() would have
4029 * prevented this call. It could happen only if the generator
4030 * object was corrupted or if this function is called directly
4031 * inside the library. */
4032 if( hkdf->block_number == 0xff )
4033 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004034
4035 /* We need a new block */
4036 ++hkdf->block_number;
4037 hkdf->offset_in_block = 0;
4038 status = psa_hmac_setup_internal( &hkdf->hmac,
4039 hkdf->prk, hash_length,
4040 hash_alg );
4041 if( status != PSA_SUCCESS )
4042 return( status );
4043 if( hkdf->block_number != 1 )
4044 {
4045 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4046 hkdf->output_block,
4047 hash_length );
4048 if( status != PSA_SUCCESS )
4049 return( status );
4050 }
4051 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4052 hkdf->info,
4053 hkdf->info_length );
4054 if( status != PSA_SUCCESS )
4055 return( status );
4056 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4057 &hkdf->block_number, 1 );
4058 if( status != PSA_SUCCESS )
4059 return( status );
4060 status = psa_hmac_finish_internal( &hkdf->hmac,
4061 hkdf->output_block,
4062 sizeof( hkdf->output_block ) );
4063 if( status != PSA_SUCCESS )
4064 return( status );
4065 }
4066
4067 return( PSA_SUCCESS );
4068}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004069
4070static psa_status_t psa_generator_tls12_prf_generate_next_block(
4071 psa_tls12_prf_generator_t *tls12_prf,
4072 psa_algorithm_t alg )
4073{
4074 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4075 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4076 psa_hmac_internal_data hmac;
4077 psa_status_t status, cleanup_status;
4078
Hanno Becker3b339e22018-11-13 20:56:14 +00004079 unsigned char *Ai;
4080 size_t Ai_len;
4081
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004082 /* We can't be wanting more output after block 0xff, otherwise
4083 * the capacity check in psa_generator_read() would have
4084 * prevented this call. It could happen only if the generator
4085 * object was corrupted or if this function is called directly
4086 * inside the library. */
4087 if( tls12_prf->block_number == 0xff )
4088 return( PSA_ERROR_BAD_STATE );
4089
4090 /* We need a new block */
4091 ++tls12_prf->block_number;
4092 tls12_prf->offset_in_block = 0;
4093
4094 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4095 *
4096 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4097 *
4098 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4099 * HMAC_hash(secret, A(2) + seed) +
4100 * HMAC_hash(secret, A(3) + seed) + ...
4101 *
4102 * A(0) = seed
4103 * A(i) = HMAC_hash( secret, A(i-1) )
4104 *
4105 * The `psa_tls12_prf_generator` structures saves the block
4106 * `HMAC_hash(secret, A(i) + seed)` from which the output
4107 * is currently extracted as `output_block`, while
4108 * `A(i) + seed` is stored in `Ai_with_seed`.
4109 *
4110 * Generating a new block means recalculating `Ai_with_seed`
4111 * from the A(i)-part of it, and afterwards recalculating
4112 * `output_block`.
4113 *
4114 * A(0) is computed at setup time.
4115 *
4116 */
4117
4118 psa_hmac_init_internal( &hmac );
4119
4120 /* We must distinguish the calculation of A(1) from those
4121 * of A(2) and higher, because A(0)=seed has a different
4122 * length than the other A(i). */
4123 if( tls12_prf->block_number == 1 )
4124 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004125 Ai = tls12_prf->Ai_with_seed + hash_length;
4126 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004127 }
4128 else
4129 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004130 Ai = tls12_prf->Ai_with_seed;
4131 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004132 }
4133
Hanno Becker3b339e22018-11-13 20:56:14 +00004134 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4135 status = psa_hmac_setup_internal( &hmac,
4136 tls12_prf->key,
4137 tls12_prf->key_len,
4138 hash_alg );
4139 if( status != PSA_SUCCESS )
4140 goto cleanup;
4141
4142 status = psa_hash_update( &hmac.hash_ctx,
4143 Ai, Ai_len );
4144 if( status != PSA_SUCCESS )
4145 goto cleanup;
4146
4147 status = psa_hmac_finish_internal( &hmac,
4148 tls12_prf->Ai_with_seed,
4149 hash_length );
4150 if( status != PSA_SUCCESS )
4151 goto cleanup;
4152
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004153 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4154 status = psa_hmac_setup_internal( &hmac,
4155 tls12_prf->key,
4156 tls12_prf->key_len,
4157 hash_alg );
4158 if( status != PSA_SUCCESS )
4159 goto cleanup;
4160
4161 status = psa_hash_update( &hmac.hash_ctx,
4162 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004163 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004164 if( status != PSA_SUCCESS )
4165 goto cleanup;
4166
4167 status = psa_hmac_finish_internal( &hmac,
4168 tls12_prf->output_block,
4169 hash_length );
4170 if( status != PSA_SUCCESS )
4171 goto cleanup;
4172
4173cleanup:
4174
4175 cleanup_status = psa_hmac_abort_internal( &hmac );
4176 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4177 status = cleanup_status;
4178
4179 return( status );
4180}
4181
4182/* Read some bytes from an TLS-1.2-PRF-based generator.
4183 * See Section 5 of RFC 5246. */
4184static psa_status_t psa_generator_tls12_prf_read(
4185 psa_tls12_prf_generator_t *tls12_prf,
4186 psa_algorithm_t alg,
4187 uint8_t *output,
4188 size_t output_length )
4189{
4190 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4191 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4192 psa_status_t status;
4193
4194 while( output_length != 0 )
4195 {
4196 /* Copy what remains of the current block */
4197 uint8_t n = hash_length - tls12_prf->offset_in_block;
4198
4199 /* Check if we have fully processed the current block. */
4200 if( n == 0 )
4201 {
4202 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
4203 alg );
4204 if( status != PSA_SUCCESS )
4205 return( status );
4206
4207 continue;
4208 }
4209
4210 if( n > output_length )
4211 n = (uint8_t) output_length;
4212 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4213 n );
4214 output += n;
4215 output_length -= n;
4216 tls12_prf->offset_in_block += n;
4217 }
4218
4219 return( PSA_SUCCESS );
4220}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004221#endif /* MBEDTLS_MD_C */
4222
Gilles Peskineeab56e42018-07-12 17:12:33 +02004223psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
4224 uint8_t *output,
4225 size_t output_length )
4226{
4227 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004228 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004229
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004230 if( generator->alg == 0 )
4231 {
4232 /* This is a blank generator. */
4233 return PSA_ERROR_BAD_STATE;
4234 }
4235
Gilles Peskineeab56e42018-07-12 17:12:33 +02004236 if( output_length > generator->capacity )
4237 {
4238 generator->capacity = 0;
4239 /* Go through the error path to wipe all confidential data now
4240 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004241 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004242 goto exit;
4243 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004244 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004245 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004246 /* Edge case: this is a finished generator, and 0 bytes
4247 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004248 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4249 * INSUFFICIENT_CAPACITY, which is right for a finished
4250 * generator, for consistency with the case when
4251 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004252 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004253 }
4254 generator->capacity -= output_length;
4255
Gilles Peskine969c5d62019-01-16 15:53:06 +01004256 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004257 {
Gilles Peskine211a4362018-10-25 22:22:31 +02004258 /* Initially, the capacity of a selection generator is always
4259 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
4260 * abbreviated in this comment as `size`. When the remaining
4261 * capacity is `c`, the next bytes to serve start `c` bytes
4262 * from the end of the buffer, i.e. `size - c` from the
4263 * beginning of the buffer. Since `generator->capacity` was just
4264 * decremented above, we need to serve the bytes from
4265 * `size - generator->capacity - output_length` to
4266 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004267 size_t offset =
4268 generator->ctx.buffer.size - generator->capacity - output_length;
4269 memcpy( output, generator->ctx.buffer.data + offset, output_length );
4270 status = PSA_SUCCESS;
4271 }
4272 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004273#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004274 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004275 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004276 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004277 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
4278 output, output_length );
4279 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004280 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4281 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004282 {
4283 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004284 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004285 output_length );
4286 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004287 else
4288#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004289 {
4290 return( PSA_ERROR_BAD_STATE );
4291 }
4292
4293exit:
4294 if( status != PSA_SUCCESS )
4295 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004296 /* Preserve the algorithm upon errors, but clear all sensitive state.
4297 * This allows us to differentiate between exhausted generators and
4298 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
4299 * generators. */
4300 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004301 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004302 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004303 memset( output, '!', output_length );
4304 }
4305 return( status );
4306}
4307
Gilles Peskine08542d82018-07-19 17:05:42 +02004308#if defined(MBEDTLS_DES_C)
4309static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4310{
4311 if( data_size >= 8 )
4312 mbedtls_des_key_set_parity( data );
4313 if( data_size >= 16 )
4314 mbedtls_des_key_set_parity( data + 8 );
4315 if( data_size >= 24 )
4316 mbedtls_des_key_set_parity( data + 16 );
4317}
4318#endif /* MBEDTLS_DES_C */
4319
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004320static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004321 psa_key_slot_t *slot,
4322 size_t bits,
4323 psa_crypto_generator_t *generator )
4324{
4325 uint8_t *data = NULL;
4326 size_t bytes = PSA_BITS_TO_BYTES( bits );
4327 psa_status_t status;
4328
4329 if( ! key_type_is_raw_bytes( slot->type ) )
4330 return( PSA_ERROR_INVALID_ARGUMENT );
4331 if( bits % 8 != 0 )
4332 return( PSA_ERROR_INVALID_ARGUMENT );
4333 data = mbedtls_calloc( 1, bytes );
4334 if( data == NULL )
4335 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4336
4337 status = psa_generator_read( generator, data, bytes );
4338 if( status != PSA_SUCCESS )
4339 goto exit;
4340#if defined(MBEDTLS_DES_C)
4341 if( slot->type == PSA_KEY_TYPE_DES )
4342 psa_des_set_key_parity( data, bytes );
4343#endif /* MBEDTLS_DES_C */
4344 status = psa_import_key_into_slot( slot, data, bytes );
4345
4346exit:
4347 mbedtls_free( data );
4348 return( status );
4349}
4350
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004351psa_status_t psa_generate_derived_key( const psa_key_attributes_t *attributes,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004352 psa_key_handle_t *handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004353 psa_crypto_generator_t *generator )
4354{
4355 psa_status_t status;
4356 psa_key_slot_t *slot = NULL;
4357 status = psa_start_key_creation( attributes, handle, &slot );
4358 if( status == PSA_SUCCESS )
4359 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004360 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004361 attributes->bits,
4362 generator );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004363 }
4364 if( status == PSA_SUCCESS )
4365 status = psa_finish_key_creation( slot );
4366 if( status != PSA_SUCCESS )
4367 {
4368 psa_fail_key_creation( slot );
4369 *handle = 0;
4370 }
4371 return( status );
4372}
4373
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004374psa_status_t psa_generate_derived_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004375 psa_key_type_t type,
4376 size_t bits,
4377 psa_crypto_generator_t *generator )
4378{
4379 uint8_t *data = NULL;
4380 size_t bytes = PSA_BITS_TO_BYTES( bits );
4381 psa_status_t status;
4382
4383 if( ! key_type_is_raw_bytes( type ) )
4384 return( PSA_ERROR_INVALID_ARGUMENT );
4385 if( bits % 8 != 0 )
4386 return( PSA_ERROR_INVALID_ARGUMENT );
4387 data = mbedtls_calloc( 1, bytes );
4388 if( data == NULL )
4389 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4390
4391 status = psa_generator_read( generator, data, bytes );
4392 if( status != PSA_SUCCESS )
4393 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004394#if defined(MBEDTLS_DES_C)
4395 if( type == PSA_KEY_TYPE_DES )
4396 psa_des_set_key_parity( data, bytes );
4397#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004398 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004399
4400exit:
4401 mbedtls_free( data );
4402 return( status );
4403}
4404
4405
4406
4407/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004408/* Key derivation */
4409/****************************************************************/
4410
Gilles Peskinea05219c2018-11-16 16:02:56 +01004411#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004412/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004413 * of the HKDF algorithm.
4414 *
4415 * Note that if this function fails, you must call psa_generator_abort()
4416 * to potentially free embedded data structures and wipe confidential data.
4417 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004418static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004419 const uint8_t *secret,
4420 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004421 psa_algorithm_t hash_alg,
4422 const uint8_t *salt,
4423 size_t salt_length,
4424 const uint8_t *label,
4425 size_t label_length )
4426{
4427 psa_status_t status;
4428 status = psa_hmac_setup_internal( &hkdf->hmac,
4429 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004430 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004431 if( status != PSA_SUCCESS )
4432 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004433 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004434 if( status != PSA_SUCCESS )
4435 return( status );
4436 status = psa_hmac_finish_internal( &hkdf->hmac,
4437 hkdf->prk,
4438 sizeof( hkdf->prk ) );
4439 if( status != PSA_SUCCESS )
4440 return( status );
4441 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4442 hkdf->block_number = 0;
4443 hkdf->info_length = label_length;
4444 if( label_length != 0 )
4445 {
4446 hkdf->info = mbedtls_calloc( 1, label_length );
4447 if( hkdf->info == NULL )
4448 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4449 memcpy( hkdf->info, label, label_length );
4450 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004451 hkdf->state = HKDF_STATE_KEYED;
4452 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004453 return( PSA_SUCCESS );
4454}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004455#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004456
Gilles Peskinea05219c2018-11-16 16:02:56 +01004457#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004458/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4459 *
4460 * Note that if this function fails, you must call psa_generator_abort()
4461 * to potentially free embedded data structures and wipe confidential data.
4462 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004463static psa_status_t psa_generator_tls12_prf_setup(
4464 psa_tls12_prf_generator_t *tls12_prf,
4465 const unsigned char *key,
4466 size_t key_len,
4467 psa_algorithm_t hash_alg,
4468 const uint8_t *salt,
4469 size_t salt_length,
4470 const uint8_t *label,
4471 size_t label_length )
4472{
4473 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004474 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4475 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004476
4477 tls12_prf->key = mbedtls_calloc( 1, key_len );
4478 if( tls12_prf->key == NULL )
4479 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4480 tls12_prf->key_len = key_len;
4481 memcpy( tls12_prf->key, key, key_len );
4482
Hanno Becker580fba12018-11-13 20:50:45 +00004483 overflow = ( salt_length + label_length < salt_length ) ||
4484 ( salt_length + label_length + hash_length < hash_length );
4485 if( overflow )
4486 return( PSA_ERROR_INVALID_ARGUMENT );
4487
4488 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4489 if( tls12_prf->Ai_with_seed == NULL )
4490 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4491 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4492
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004493 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4494 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004495 if( label_length != 0 )
4496 {
4497 memcpy( tls12_prf->Ai_with_seed + hash_length,
4498 label, label_length );
4499 }
4500
4501 if( salt_length != 0 )
4502 {
4503 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4504 salt, salt_length );
4505 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004506
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004507 /* The first block gets generated when
4508 * psa_generator_read() is called. */
4509 tls12_prf->block_number = 0;
4510 tls12_prf->offset_in_block = hash_length;
4511
4512 return( PSA_SUCCESS );
4513}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004514
4515/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4516static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4517 psa_tls12_prf_generator_t *tls12_prf,
4518 const unsigned char *psk,
4519 size_t psk_len,
4520 psa_algorithm_t hash_alg,
4521 const uint8_t *salt,
4522 size_t salt_length,
4523 const uint8_t *label,
4524 size_t label_length )
4525{
4526 psa_status_t status;
4527 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4528
4529 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4530 return( PSA_ERROR_INVALID_ARGUMENT );
4531
4532 /* Quoting RFC 4279, Section 2:
4533 *
4534 * The premaster secret is formed as follows: if the PSK is N octets
4535 * long, concatenate a uint16 with the value N, N zero octets, a second
4536 * uint16 with the value N, and the PSK itself.
4537 */
4538
4539 pms[0] = ( psk_len >> 8 ) & 0xff;
4540 pms[1] = ( psk_len >> 0 ) & 0xff;
4541 memset( pms + 2, 0, psk_len );
4542 pms[2 + psk_len + 0] = pms[0];
4543 pms[2 + psk_len + 1] = pms[1];
4544 memcpy( pms + 4 + psk_len, psk, psk_len );
4545
4546 status = psa_generator_tls12_prf_setup( tls12_prf,
4547 pms, 4 + 2 * psk_len,
4548 hash_alg,
4549 salt, salt_length,
4550 label, label_length );
4551
Gilles Peskine3f108122018-12-07 18:14:53 +01004552 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004553 return( status );
4554}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004555#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004556
Gilles Peskine346797d2018-11-16 16:05:06 +01004557/* Note that if this function fails, you must call psa_generator_abort()
4558 * to potentially free embedded data structures and wipe confidential data.
4559 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004560static psa_status_t psa_key_derivation_internal(
4561 psa_crypto_generator_t *generator,
4562 const uint8_t *secret, size_t secret_length,
4563 psa_algorithm_t alg,
4564 const uint8_t *salt, size_t salt_length,
4565 const uint8_t *label, size_t label_length,
4566 size_t capacity )
4567{
4568 psa_status_t status;
4569 size_t max_capacity;
4570
4571 /* Set generator->alg even on failure so that abort knows what to do. */
4572 generator->alg = alg;
4573
Gilles Peskine751d9652018-09-18 12:05:44 +02004574 if( alg == PSA_ALG_SELECT_RAW )
4575 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004576 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004577 if( salt_length != 0 )
4578 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004579 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004580 if( label_length != 0 )
4581 return( PSA_ERROR_INVALID_ARGUMENT );
4582 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4583 if( generator->ctx.buffer.data == NULL )
4584 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4585 memcpy( generator->ctx.buffer.data, secret, secret_length );
4586 generator->ctx.buffer.size = secret_length;
4587 max_capacity = secret_length;
4588 status = PSA_SUCCESS;
4589 }
4590 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004591#if defined(MBEDTLS_MD_C)
4592 if( PSA_ALG_IS_HKDF( alg ) )
4593 {
4594 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4595 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4596 if( hash_size == 0 )
4597 return( PSA_ERROR_NOT_SUPPORTED );
4598 max_capacity = 255 * hash_size;
4599 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4600 secret, secret_length,
4601 hash_alg,
4602 salt, salt_length,
4603 label, label_length );
4604 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004605 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4606 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4607 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004608 {
4609 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4610 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4611
4612 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4613 if( hash_alg != PSA_ALG_SHA_256 &&
4614 hash_alg != PSA_ALG_SHA_384 )
4615 {
4616 return( PSA_ERROR_NOT_SUPPORTED );
4617 }
4618
4619 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004620
4621 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4622 {
4623 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4624 secret, secret_length,
4625 hash_alg, salt, salt_length,
4626 label, label_length );
4627 }
4628 else
4629 {
4630 status = psa_generator_tls12_psk_to_ms_setup(
4631 &generator->ctx.tls12_prf,
4632 secret, secret_length,
4633 hash_alg, salt, salt_length,
4634 label, label_length );
4635 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004636 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004637 else
4638#endif
4639 {
4640 return( PSA_ERROR_NOT_SUPPORTED );
4641 }
4642
4643 if( status != PSA_SUCCESS )
4644 return( status );
4645
4646 if( capacity <= max_capacity )
4647 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004648 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4649 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004650 else
4651 return( PSA_ERROR_INVALID_ARGUMENT );
4652
4653 return( PSA_SUCCESS );
4654}
4655
Gilles Peskineea0fb492018-07-12 17:17:20 +02004656psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004657 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004658 psa_algorithm_t alg,
4659 const uint8_t *salt,
4660 size_t salt_length,
4661 const uint8_t *label,
4662 size_t label_length,
4663 size_t capacity )
4664{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004665 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004666 psa_status_t status;
4667
4668 if( generator->alg != 0 )
4669 return( PSA_ERROR_BAD_STATE );
4670
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004671 /* Make sure that alg is a key derivation algorithm. This prevents
4672 * key selection algorithms, which psa_key_derivation_internal
4673 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004674 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4675 return( PSA_ERROR_INVALID_ARGUMENT );
4676
Gilles Peskinec5487a82018-12-03 18:08:14 +01004677 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004678 if( status != PSA_SUCCESS )
4679 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004680
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004681 if( slot->type != PSA_KEY_TYPE_DERIVE )
4682 return( PSA_ERROR_INVALID_ARGUMENT );
4683
4684 status = psa_key_derivation_internal( generator,
4685 slot->data.raw.data,
4686 slot->data.raw.bytes,
4687 alg,
4688 salt, salt_length,
4689 label, label_length,
4690 capacity );
4691 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004692 psa_generator_abort( generator );
4693 return( status );
4694}
4695
Gilles Peskine969c5d62019-01-16 15:53:06 +01004696static psa_status_t psa_key_derivation_setup_kdf(
4697 psa_crypto_generator_t *generator,
4698 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004699{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004700 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004701#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004702 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4703 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4704 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004705 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004706 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004707 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4708 if( hash_size == 0 )
4709 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004710 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4711 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004712 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004713 {
4714 return( PSA_ERROR_NOT_SUPPORTED );
4715 }
4716 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004717 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004718 }
4719#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004720 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004721 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004722}
4723
4724psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4725 psa_algorithm_t alg )
4726{
4727 psa_status_t status;
4728
4729 if( generator->alg != 0 )
4730 return( PSA_ERROR_BAD_STATE );
4731
Gilles Peskine6843c292019-01-18 16:44:49 +01004732 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4733 return( PSA_ERROR_INVALID_ARGUMENT );
4734 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004735 {
4736 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004737 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004738 }
4739 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4740 {
4741 status = psa_key_derivation_setup_kdf( generator, alg );
4742 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004743 else
4744 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004745
4746 if( status == PSA_SUCCESS )
4747 generator->alg = alg;
4748 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004749}
4750
4751#if defined(MBEDTLS_MD_C)
4752static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4753 psa_algorithm_t hash_alg,
4754 psa_key_derivation_step_t step,
4755 const uint8_t *data,
4756 size_t data_length )
4757{
4758 psa_status_t status;
4759 switch( step )
4760 {
4761 case PSA_KDF_STEP_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004762 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004763 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004764 status = psa_hmac_setup_internal( &hkdf->hmac,
4765 data, data_length,
4766 hash_alg );
4767 if( status != PSA_SUCCESS )
4768 return( status );
4769 hkdf->state = HKDF_STATE_STARTED;
4770 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004771 case PSA_KDF_STEP_SECRET:
4772 /* If no salt was provided, use an empty salt. */
4773 if( hkdf->state == HKDF_STATE_INIT )
4774 {
4775 status = psa_hmac_setup_internal( &hkdf->hmac,
4776 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004777 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004778 if( status != PSA_SUCCESS )
4779 return( status );
4780 hkdf->state = HKDF_STATE_STARTED;
4781 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004782 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004783 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004784 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4785 data, data_length );
4786 if( status != PSA_SUCCESS )
4787 return( status );
4788 status = psa_hmac_finish_internal( &hkdf->hmac,
4789 hkdf->prk,
4790 sizeof( hkdf->prk ) );
4791 if( status != PSA_SUCCESS )
4792 return( status );
4793 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4794 hkdf->block_number = 0;
4795 hkdf->state = HKDF_STATE_KEYED;
4796 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004797 case PSA_KDF_STEP_INFO:
4798 if( hkdf->state == HKDF_STATE_OUTPUT )
4799 return( PSA_ERROR_BAD_STATE );
4800 if( hkdf->info_set )
4801 return( PSA_ERROR_BAD_STATE );
4802 hkdf->info_length = data_length;
4803 if( data_length != 0 )
4804 {
4805 hkdf->info = mbedtls_calloc( 1, data_length );
4806 if( hkdf->info == NULL )
4807 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4808 memcpy( hkdf->info, data, data_length );
4809 }
4810 hkdf->info_set = 1;
4811 return( PSA_SUCCESS );
4812 default:
4813 return( PSA_ERROR_INVALID_ARGUMENT );
4814 }
4815}
4816#endif /* MBEDTLS_MD_C */
4817
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004818static psa_status_t psa_key_derivation_input_raw(
4819 psa_crypto_generator_t *generator,
4820 psa_key_derivation_step_t step,
4821 const uint8_t *data,
4822 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004823{
4824 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004825 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004826
Gilles Peskine969c5d62019-01-16 15:53:06 +01004827 if( kdf_alg == PSA_ALG_SELECT_RAW )
4828 {
4829 if( generator->capacity != 0 )
4830 return( PSA_ERROR_INVALID_ARGUMENT );
4831 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4832 if( generator->ctx.buffer.data == NULL )
4833 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4834 memcpy( generator->ctx.buffer.data, data, data_length );
4835 generator->ctx.buffer.size = data_length;
4836 generator->capacity = data_length;
4837 status = PSA_SUCCESS;
4838 }
4839 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004840#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004841 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004842 {
4843 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004844 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004845 step, data, data_length );
4846 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004847 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004848#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004849#if defined(MBEDTLS_MD_C)
4850 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004851 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4852 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004853 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004854 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004855 status = PSA_ERROR_NOT_SUPPORTED;
4856 }
4857 else
4858#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004859 {
4860 /* This can't happen unless the generator object was not initialized */
4861 return( PSA_ERROR_BAD_STATE );
4862 }
4863
4864 if( status != PSA_SUCCESS )
4865 psa_generator_abort( generator );
4866 return( status );
4867}
4868
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004869psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4870 psa_key_derivation_step_t step,
4871 const uint8_t *data,
4872 size_t data_length )
4873{
4874 switch( step )
4875 {
4876 case PSA_KDF_STEP_LABEL:
4877 case PSA_KDF_STEP_SALT:
4878 case PSA_KDF_STEP_INFO:
4879 return( psa_key_derivation_input_raw( generator, step,
4880 data, data_length ) );
4881 default:
4882 return( PSA_ERROR_INVALID_ARGUMENT );
4883 }
4884}
4885
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004886psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4887 psa_key_derivation_step_t step,
4888 psa_key_handle_t handle )
4889{
4890 psa_key_slot_t *slot;
4891 psa_status_t status;
4892 status = psa_get_key_from_slot( handle, &slot,
4893 PSA_KEY_USAGE_DERIVE,
4894 generator->alg );
4895 if( status != PSA_SUCCESS )
4896 return( status );
4897 if( slot->type != PSA_KEY_TYPE_DERIVE )
4898 return( PSA_ERROR_INVALID_ARGUMENT );
4899 /* Don't allow a key to be used as an input that is usually public.
4900 * This is debatable. It's ok from a cryptographic perspective to
4901 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004902 * the material should be dedicated to a particular input step,
4903 * otherwise this may allow the key to be used in an unintended way
4904 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004905 if( step != PSA_KDF_STEP_SECRET )
4906 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004907 return( psa_key_derivation_input_raw( generator,
4908 step,
4909 slot->data.raw.data,
4910 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004911}
4912
Gilles Peskineea0fb492018-07-12 17:17:20 +02004913
4914
4915/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004916/* Key agreement */
4917/****************************************************************/
4918
Gilles Peskinea05219c2018-11-16 16:02:56 +01004919#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004920static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4921 size_t peer_key_length,
4922 const mbedtls_ecp_keypair *our_key,
4923 uint8_t *shared_secret,
4924 size_t shared_secret_size,
4925 size_t *shared_secret_length )
4926{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004927 mbedtls_ecp_keypair *their_key = NULL;
4928 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004929 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004930 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004931
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004932 status = psa_import_ec_public_key(
4933 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4934 peer_key, peer_key_length,
4935 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004936 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004937 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004938
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004939 status = mbedtls_to_psa_error(
4940 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4941 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004942 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004943 status = mbedtls_to_psa_error(
4944 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4945 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004946 goto exit;
4947
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004948 status = mbedtls_to_psa_error(
4949 mbedtls_ecdh_calc_secret( &ecdh,
4950 shared_secret_length,
4951 shared_secret, shared_secret_size,
4952 mbedtls_ctr_drbg_random,
4953 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004954
4955exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004956 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004957 mbedtls_ecp_keypair_free( their_key );
4958 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004959 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004960}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004961#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004962
Gilles Peskine01d718c2018-09-18 12:01:02 +02004963#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4964
Gilles Peskine0216fe12019-04-11 21:23:21 +02004965static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4966 psa_key_slot_t *private_key,
4967 const uint8_t *peer_key,
4968 size_t peer_key_length,
4969 uint8_t *shared_secret,
4970 size_t shared_secret_size,
4971 size_t *shared_secret_length )
4972{
4973 switch( alg )
4974 {
4975#if defined(MBEDTLS_ECDH_C)
4976 case PSA_ALG_ECDH:
4977 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4978 return( PSA_ERROR_INVALID_ARGUMENT );
4979 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
4980 private_key->data.ecp,
4981 shared_secret, shared_secret_size,
4982 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004983#endif /* MBEDTLS_ECDH_C */
4984 default:
4985 (void) private_key;
4986 (void) peer_key;
4987 (void) peer_key_length;
4988 (void) shared_secret;
4989 (void) shared_secret_size;
4990 (void) shared_secret_length;
4991 return( PSA_ERROR_NOT_SUPPORTED );
4992 }
4993}
4994
Gilles Peskine346797d2018-11-16 16:05:06 +01004995/* Note that if this function fails, you must call psa_generator_abort()
4996 * to potentially free embedded data structures and wipe confidential data.
4997 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004998static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004999 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005000 psa_key_slot_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{
5004 psa_status_t status;
5005 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5006 size_t shared_secret_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005007 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005008
5009 /* Step 1: run the secret agreement algorithm to generate the shared
5010 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005011 status = psa_key_agreement_raw_internal( ka_alg,
5012 private_key,
5013 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005014 shared_secret,
5015 sizeof( shared_secret ),
5016 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005017 if( status != PSA_SUCCESS )
5018 goto exit;
5019
5020 /* Step 2: set up the key derivation to generate key material from
5021 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005022 status = psa_key_derivation_input_raw( generator, step,
5023 shared_secret, shared_secret_length );
5024
Gilles Peskine01d718c2018-09-18 12:01:02 +02005025exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005026 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005027 return( status );
5028}
5029
5030psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005031 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005032 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005033 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005034 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005035{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005036 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005037 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005038 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005039 return( PSA_ERROR_INVALID_ARGUMENT );
5040 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005041 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005042 if( status != PSA_SUCCESS )
5043 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005044 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005045 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005046 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005047 if( status != PSA_SUCCESS )
5048 psa_generator_abort( generator );
5049 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005050}
5051
Gilles Peskine0216fe12019-04-11 21:23:21 +02005052psa_status_t psa_key_agreement_raw_shared_secret( psa_algorithm_t alg,
5053 psa_key_handle_t private_key,
5054 const uint8_t *peer_key,
5055 size_t peer_key_length,
5056 uint8_t *output,
5057 size_t output_size,
5058 size_t *output_length )
5059{
5060 psa_key_slot_t *slot;
5061 psa_status_t status;
5062
5063 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5064 {
5065 status = PSA_ERROR_INVALID_ARGUMENT;
5066 goto exit;
5067 }
5068 status = psa_get_key_from_slot( private_key, &slot,
5069 PSA_KEY_USAGE_DERIVE, alg );
5070 if( status != PSA_SUCCESS )
5071 goto exit;
5072
5073 status = psa_key_agreement_raw_internal( alg, slot,
5074 peer_key, peer_key_length,
5075 output, output_size,
5076 output_length );
5077
5078exit:
5079 if( status != PSA_SUCCESS )
5080 {
5081 /* If an error happens and is not handled properly, the output
5082 * may be used as a key to protect sensitive data. Arrange for such
5083 * a key to be random, which is likely to result in decryption or
5084 * verification errors. This is better than filling the buffer with
5085 * some constant data such as zeros, which would result in the data
5086 * being protected with a reproducible, easily knowable key.
5087 */
5088 psa_generate_random( output, output_size );
5089 *output_length = output_size;
5090 }
5091 return( status );
5092}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005093
5094
5095/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005096/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005097/****************************************************************/
5098
5099psa_status_t psa_generate_random( uint8_t *output,
5100 size_t output_size )
5101{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005102 int ret;
5103 GUARD_MODULE_INITIALIZED;
5104
5105 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005106 return( mbedtls_to_psa_error( ret ) );
5107}
5108
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005109#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5110#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005111
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005112psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5113 size_t seed_size )
5114{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005115 if( global_data.initialized )
5116 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005117
5118 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5119 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5120 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5121 return( PSA_ERROR_INVALID_ARGUMENT );
5122
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005123 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005124}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005125#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005126
Gilles Peskinee56e8782019-04-26 17:34:02 +02005127#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5128static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5129 size_t domain_parameters_size,
5130 int *exponent )
5131{
5132 size_t i;
5133 uint32_t acc = 0;
5134
5135 if( domain_parameters_size == 0 )
5136 {
5137 *exponent = 65537;
5138 return( PSA_SUCCESS );
5139 }
5140
5141 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5142 * support values that fit in a 32-bit integer, which is larger than
5143 * int on just about every platform anyway. */
5144 if( domain_parameters_size > sizeof( acc ) )
5145 return( PSA_ERROR_NOT_SUPPORTED );
5146 for( i = 0; i < domain_parameters_size; i++ )
5147 acc = ( acc << 8 ) | domain_parameters[i];
5148 if( acc > INT_MAX )
5149 return( PSA_ERROR_NOT_SUPPORTED );
5150 *exponent = acc;
5151 return( PSA_SUCCESS );
5152}
5153#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5154
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005155static psa_status_t psa_generate_random_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005156 psa_key_slot_t *slot, size_t bits,
5157 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005158{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005159 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005160
Gilles Peskinee56e8782019-04-26 17:34:02 +02005161 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005162 return( PSA_ERROR_INVALID_ARGUMENT );
5163
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005164 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005165 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005166 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005167 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005168 if( status != PSA_SUCCESS )
5169 return( status );
5170 status = psa_generate_random( slot->data.raw.data,
5171 slot->data.raw.bytes );
5172 if( status != PSA_SUCCESS )
5173 {
5174 mbedtls_free( slot->data.raw.data );
5175 return( status );
5176 }
5177#if defined(MBEDTLS_DES_C)
5178 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005179 psa_des_set_key_parity( slot->data.raw.data,
5180 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005181#endif /* MBEDTLS_DES_C */
5182 }
5183 else
5184
5185#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5186 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
5187 {
5188 mbedtls_rsa_context *rsa;
5189 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005190 int exponent;
5191 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005192 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5193 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005194 /* Accept only byte-aligned keys, for the same reasons as
5195 * in psa_import_rsa_key(). */
5196 if( bits % 8 != 0 )
5197 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005198 status = psa_read_rsa_exponent( domain_parameters,
5199 domain_parameters_size,
5200 &exponent );
5201 if( status != PSA_SUCCESS )
5202 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005203 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5204 if( rsa == NULL )
5205 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5206 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5207 ret = mbedtls_rsa_gen_key( rsa,
5208 mbedtls_ctr_drbg_random,
5209 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005210 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005211 exponent );
5212 if( ret != 0 )
5213 {
5214 mbedtls_rsa_free( rsa );
5215 mbedtls_free( rsa );
5216 return( mbedtls_to_psa_error( ret ) );
5217 }
5218 slot->data.rsa = rsa;
5219 }
5220 else
5221#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5222
5223#if defined(MBEDTLS_ECP_C)
5224 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
5225 {
5226 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5227 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5228 const mbedtls_ecp_curve_info *curve_info =
5229 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5230 mbedtls_ecp_keypair *ecp;
5231 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005232 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005233 return( PSA_ERROR_NOT_SUPPORTED );
5234 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5235 return( PSA_ERROR_NOT_SUPPORTED );
5236 if( curve_info->bit_size != bits )
5237 return( PSA_ERROR_INVALID_ARGUMENT );
5238 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5239 if( ecp == NULL )
5240 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5241 mbedtls_ecp_keypair_init( ecp );
5242 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5243 mbedtls_ctr_drbg_random,
5244 &global_data.ctr_drbg );
5245 if( ret != 0 )
5246 {
5247 mbedtls_ecp_keypair_free( ecp );
5248 mbedtls_free( ecp );
5249 return( mbedtls_to_psa_error( ret ) );
5250 }
5251 slot->data.ecp = ecp;
5252 }
5253 else
5254#endif /* MBEDTLS_ECP_C */
5255
5256 return( PSA_ERROR_NOT_SUPPORTED );
5257
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005258 return( PSA_SUCCESS );
5259}
5260
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005261psa_status_t psa_generate_random_key_to_handle( psa_key_handle_t handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005262 psa_key_type_t type,
5263 size_t bits,
5264 const void *extra,
5265 size_t extra_size )
5266{
5267 psa_key_slot_t *slot;
5268 psa_status_t status;
5269
Gilles Peskinee56e8782019-04-26 17:34:02 +02005270#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5271 /* The old public exponent encoding is no longer supported. */
5272 if( extra_size != 0 )
5273 return( PSA_ERROR_NOT_SUPPORTED );
5274#endif
5275
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005276 status = psa_get_empty_key_slot( handle, &slot );
5277 if( status != PSA_SUCCESS )
5278 return( status );
5279
Gilles Peskine12313cd2018-06-20 00:20:32 +02005280 slot->type = type;
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005281 status = psa_generate_random_key_internal( slot, bits, extra, extra_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005282 if( status != PSA_SUCCESS )
5283 slot->type = 0;
Darryl Green0c6575a2018-11-07 16:05:30 +00005284
5285#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
5286 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
5287 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01005288 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005289 }
5290#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5291
5292 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005293}
5294
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005295psa_status_t psa_generate_random_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005296 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005297{
5298 psa_status_t status;
5299 psa_key_slot_t *slot = NULL;
5300 status = psa_start_key_creation( attributes, handle, &slot );
5301 if( status == PSA_SUCCESS )
5302 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005303 status = psa_generate_random_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005304 slot, attributes->bits,
5305 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005306 }
5307 if( status == PSA_SUCCESS )
5308 status = psa_finish_key_creation( slot );
5309 if( status != PSA_SUCCESS )
5310 {
5311 psa_fail_key_creation( slot );
5312 *handle = 0;
5313 }
5314 return( status );
5315}
5316
5317
Gilles Peskine05d69892018-06-19 22:00:52 +02005318
5319/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005320/* Module setup */
5321/****************************************************************/
5322
Gilles Peskine5e769522018-11-20 21:59:56 +01005323psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5324 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5325 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5326{
5327 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5328 return( PSA_ERROR_BAD_STATE );
5329 global_data.entropy_init = entropy_init;
5330 global_data.entropy_free = entropy_free;
5331 return( PSA_SUCCESS );
5332}
5333
Gilles Peskinee59236f2018-01-27 23:32:46 +01005334void mbedtls_psa_crypto_free( void )
5335{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005336 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005337 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5338 {
5339 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005340 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005341 }
5342 /* Wipe all remaining data, including configuration.
5343 * In particular, this sets all state indicator to the value
5344 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005345 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005346}
5347
5348psa_status_t psa_crypto_init( void )
5349{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005350 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005351 const unsigned char drbg_seed[] = "PSA";
5352
Gilles Peskinec6b69072018-11-20 21:42:52 +01005353 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005354 if( global_data.initialized != 0 )
5355 return( PSA_SUCCESS );
5356
Gilles Peskine5e769522018-11-20 21:59:56 +01005357 /* Set default configuration if
5358 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5359 if( global_data.entropy_init == NULL )
5360 global_data.entropy_init = mbedtls_entropy_init;
5361 if( global_data.entropy_free == NULL )
5362 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005363
Gilles Peskinec6b69072018-11-20 21:42:52 +01005364 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005365 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005366 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005367 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005368 status = mbedtls_to_psa_error(
5369 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5370 mbedtls_entropy_func,
5371 &global_data.entropy,
5372 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5373 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005374 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005375 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005376
Gilles Peskine66fb1262018-12-10 16:29:04 +01005377 status = psa_initialize_key_slots( );
5378 if( status != PSA_SUCCESS )
5379 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005380
5381 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005382 global_data.initialized = 1;
5383
Gilles Peskinee59236f2018-01-27 23:32:46 +01005384exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005385 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005386 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005387 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005388}
5389
5390#endif /* MBEDTLS_PSA_CRYPTO_C */