blob: ae93e8b8f367a50dc6856624366d23735cc9855a [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"
Gilles Peskine26869f22019-05-06 15:25:00 +020054#include "mbedtls/chacha20.h"
55#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/cipher.h"
57#include "mbedtls/ccm.h"
58#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010059#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020061#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010062#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/error.h"
65#include "mbedtls/gcm.h"
66#include "mbedtls/md2.h"
67#include "mbedtls/md4.h"
68#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010069#include "mbedtls/md.h"
70#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010071#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010073#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/sha1.h"
77#include "mbedtls/sha256.h"
78#include "mbedtls/sha512.h"
79#include "mbedtls/xtea.h"
80
Gilles Peskine996deb12018-08-01 15:45:45 +020081#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
82
Gilles Peskine9ef733f2018-02-07 21:05:37 +010083/* constant-time buffer comparison */
84static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
85{
86 size_t i;
87 unsigned char diff = 0;
88
89 for( i = 0; i < n; i++ )
90 diff |= a[i] ^ b[i];
91
92 return( diff );
93}
94
95
96
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010097/****************************************************************/
98/* Global data, support functions and library management */
99/****************************************************************/
100
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200101static int key_type_is_raw_bytes( psa_key_type_t type )
102{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200103 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104}
105
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100106/* Values for psa_global_data_t::rng_state */
107#define RNG_NOT_INITIALIZED 0
108#define RNG_INITIALIZED 1
109#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100110
Gilles Peskine2d277862018-06-18 15:41:12 +0200111typedef struct
112{
Gilles Peskine5e769522018-11-20 21:59:56 +0100113 void (* entropy_init )( mbedtls_entropy_context *ctx );
114 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100115 mbedtls_entropy_context entropy;
116 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100117 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100118 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119} psa_global_data_t;
120
121static psa_global_data_t global_data;
122
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123#define GUARD_MODULE_INITIALIZED \
124 if( global_data.initialized == 0 ) \
125 return( PSA_ERROR_BAD_STATE );
126
Gilles Peskinee59236f2018-01-27 23:32:46 +0100127static psa_status_t mbedtls_to_psa_error( int ret )
128{
Gilles Peskinea5905292018-02-07 20:59:33 +0100129 /* If there's both a high-level code and low-level code, dispatch on
130 * the high-level code. */
131 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132 {
133 case 0:
134 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100135
136 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
137 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
138 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
139 return( PSA_ERROR_NOT_SUPPORTED );
140 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
141 return( PSA_ERROR_HARDWARE_FAILURE );
142
143 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
Gilles Peskine9a944802018-06-21 09:35:35 +0200146 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
147 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
148 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
149 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
150 case MBEDTLS_ERR_ASN1_INVALID_DATA:
151 return( PSA_ERROR_INVALID_ARGUMENT );
152 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
153 return( PSA_ERROR_INSUFFICIENT_MEMORY );
154 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
155 return( PSA_ERROR_BUFFER_TOO_SMALL );
156
Jaeden Amero93e21112019-02-20 13:57:28 +0000157#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000158 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100160 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100162 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
163 return( PSA_ERROR_NOT_SUPPORTED );
164 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
165 return( PSA_ERROR_HARDWARE_FAILURE );
166
Jaeden Amero93e21112019-02-20 13:57:28 +0000167#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000168 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000169#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100170 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100172 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
173 return( PSA_ERROR_NOT_SUPPORTED );
174 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
175 return( PSA_ERROR_HARDWARE_FAILURE );
176
177 case MBEDTLS_ERR_CCM_BAD_INPUT:
178 return( PSA_ERROR_INVALID_ARGUMENT );
179 case MBEDTLS_ERR_CCM_AUTH_FAILED:
180 return( PSA_ERROR_INVALID_SIGNATURE );
181 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
182 return( PSA_ERROR_HARDWARE_FAILURE );
183
Gilles Peskine26869f22019-05-06 15:25:00 +0200184 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
185 return( PSA_ERROR_INVALID_ARGUMENT );
186
187 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
188 return( PSA_ERROR_BAD_STATE );
189 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
190 return( PSA_ERROR_INVALID_SIGNATURE );
191
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
193 return( PSA_ERROR_NOT_SUPPORTED );
194 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
195 return( PSA_ERROR_INVALID_ARGUMENT );
196 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
197 return( PSA_ERROR_INSUFFICIENT_MEMORY );
198 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
199 return( PSA_ERROR_INVALID_PADDING );
200 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
201 return( PSA_ERROR_BAD_STATE );
202 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
203 return( PSA_ERROR_INVALID_SIGNATURE );
204 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
205 return( PSA_ERROR_TAMPERING_DETECTED );
206 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
207 return( PSA_ERROR_HARDWARE_FAILURE );
208
209 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
213 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
214 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
215 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
216 return( PSA_ERROR_NOT_SUPPORTED );
217 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219
220 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
223 return( PSA_ERROR_HARDWARE_FAILURE );
224
Gilles Peskinee59236f2018-01-27 23:32:46 +0100225 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
226 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
227 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
228 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
230 case MBEDTLS_ERR_GCM_AUTH_FAILED:
231 return( PSA_ERROR_INVALID_SIGNATURE );
232 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200233 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
235 return( PSA_ERROR_HARDWARE_FAILURE );
236
237 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
238 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
243 return( PSA_ERROR_NOT_SUPPORTED );
244 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
245 return( PSA_ERROR_INVALID_ARGUMENT );
246 case MBEDTLS_ERR_MD_ALLOC_FAILED:
247 return( PSA_ERROR_INSUFFICIENT_MEMORY );
248 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
249 return( PSA_ERROR_STORAGE_FAILURE );
250 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
251 return( PSA_ERROR_HARDWARE_FAILURE );
252
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
256 return( PSA_ERROR_INVALID_ARGUMENT );
257 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
260 return( PSA_ERROR_BUFFER_TOO_SMALL );
261 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
268 return( PSA_ERROR_INSUFFICIENT_MEMORY );
269
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
273 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100276 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
278 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
283 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
284 return( PSA_ERROR_NOT_PERMITTED );
285 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_PK_INVALID_ALG:
288 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
289 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
290 return( PSA_ERROR_NOT_SUPPORTED );
291 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
292 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
294 return( PSA_ERROR_HARDWARE_FAILURE );
295
Gilles Peskineff2d2002019-05-06 15:26:23 +0200296 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
299 return( PSA_ERROR_NOT_SUPPORTED );
300
Gilles Peskinea5905292018-02-07 20:59:33 +0100301 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
302 return( PSA_ERROR_HARDWARE_FAILURE );
303
304 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
305 return( PSA_ERROR_INVALID_ARGUMENT );
306 case MBEDTLS_ERR_RSA_INVALID_PADDING:
307 return( PSA_ERROR_INVALID_PADDING );
308 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
309 return( PSA_ERROR_HARDWARE_FAILURE );
310 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
311 return( PSA_ERROR_INVALID_ARGUMENT );
312 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
313 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
314 return( PSA_ERROR_TAMPERING_DETECTED );
315 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
316 return( PSA_ERROR_INVALID_SIGNATURE );
317 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
318 return( PSA_ERROR_BUFFER_TOO_SMALL );
319 case MBEDTLS_ERR_RSA_RNG_FAILED:
320 return( PSA_ERROR_INSUFFICIENT_MEMORY );
321 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
322 return( PSA_ERROR_NOT_SUPPORTED );
323 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325
326 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
327 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
332 return( PSA_ERROR_INVALID_ARGUMENT );
333 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
itayzafrir5c753392018-05-08 11:18:38 +0300336 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300337 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300338 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300339 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
340 return( PSA_ERROR_BUFFER_TOO_SMALL );
341 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
342 return( PSA_ERROR_NOT_SUPPORTED );
343 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
344 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
345 return( PSA_ERROR_INVALID_SIGNATURE );
346 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
347 return( PSA_ERROR_INSUFFICIENT_MEMORY );
348 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
349 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300350
Gilles Peskinee59236f2018-01-27 23:32:46 +0100351 default:
David Saadab4ecc272019-02-14 13:48:10 +0200352 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100353 }
354}
355
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200356
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200357
358
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359/****************************************************************/
360/* Key management */
361/****************************************************************/
362
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100363#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200364static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
365{
366 switch( grpid )
367 {
368 case MBEDTLS_ECP_DP_SECP192R1:
369 return( PSA_ECC_CURVE_SECP192R1 );
370 case MBEDTLS_ECP_DP_SECP224R1:
371 return( PSA_ECC_CURVE_SECP224R1 );
372 case MBEDTLS_ECP_DP_SECP256R1:
373 return( PSA_ECC_CURVE_SECP256R1 );
374 case MBEDTLS_ECP_DP_SECP384R1:
375 return( PSA_ECC_CURVE_SECP384R1 );
376 case MBEDTLS_ECP_DP_SECP521R1:
377 return( PSA_ECC_CURVE_SECP521R1 );
378 case MBEDTLS_ECP_DP_BP256R1:
379 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
380 case MBEDTLS_ECP_DP_BP384R1:
381 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
382 case MBEDTLS_ECP_DP_BP512R1:
383 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
384 case MBEDTLS_ECP_DP_CURVE25519:
385 return( PSA_ECC_CURVE_CURVE25519 );
386 case MBEDTLS_ECP_DP_SECP192K1:
387 return( PSA_ECC_CURVE_SECP192K1 );
388 case MBEDTLS_ECP_DP_SECP224K1:
389 return( PSA_ECC_CURVE_SECP224K1 );
390 case MBEDTLS_ECP_DP_SECP256K1:
391 return( PSA_ECC_CURVE_SECP256K1 );
392 case MBEDTLS_ECP_DP_CURVE448:
393 return( PSA_ECC_CURVE_CURVE448 );
394 default:
395 return( 0 );
396 }
397}
398
Gilles Peskine12313cd2018-06-20 00:20:32 +0200399static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
400{
401 switch( curve )
402 {
403 case PSA_ECC_CURVE_SECP192R1:
404 return( MBEDTLS_ECP_DP_SECP192R1 );
405 case PSA_ECC_CURVE_SECP224R1:
406 return( MBEDTLS_ECP_DP_SECP224R1 );
407 case PSA_ECC_CURVE_SECP256R1:
408 return( MBEDTLS_ECP_DP_SECP256R1 );
409 case PSA_ECC_CURVE_SECP384R1:
410 return( MBEDTLS_ECP_DP_SECP384R1 );
411 case PSA_ECC_CURVE_SECP521R1:
412 return( MBEDTLS_ECP_DP_SECP521R1 );
413 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
414 return( MBEDTLS_ECP_DP_BP256R1 );
415 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
416 return( MBEDTLS_ECP_DP_BP384R1 );
417 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
418 return( MBEDTLS_ECP_DP_BP512R1 );
419 case PSA_ECC_CURVE_CURVE25519:
420 return( MBEDTLS_ECP_DP_CURVE25519 );
421 case PSA_ECC_CURVE_SECP192K1:
422 return( MBEDTLS_ECP_DP_SECP192K1 );
423 case PSA_ECC_CURVE_SECP224K1:
424 return( MBEDTLS_ECP_DP_SECP224K1 );
425 case PSA_ECC_CURVE_SECP256K1:
426 return( MBEDTLS_ECP_DP_SECP256K1 );
427 case PSA_ECC_CURVE_CURVE448:
428 return( MBEDTLS_ECP_DP_CURVE448 );
429 default:
430 return( MBEDTLS_ECP_DP_NONE );
431 }
432}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100433#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200434
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200435static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
436 size_t bits,
437 struct raw_data *raw )
438{
439 /* Check that the bit size is acceptable for the key type */
440 switch( type )
441 {
442 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200443 if( bits == 0 )
444 {
445 raw->bytes = 0;
446 raw->data = NULL;
447 return( PSA_SUCCESS );
448 }
449 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450#if defined(MBEDTLS_MD_C)
451 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200452#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200453 case PSA_KEY_TYPE_DERIVE:
454 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200455#if defined(MBEDTLS_AES_C)
456 case PSA_KEY_TYPE_AES:
457 if( bits != 128 && bits != 192 && bits != 256 )
458 return( PSA_ERROR_INVALID_ARGUMENT );
459 break;
460#endif
461#if defined(MBEDTLS_CAMELLIA_C)
462 case PSA_KEY_TYPE_CAMELLIA:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
467#if defined(MBEDTLS_DES_C)
468 case PSA_KEY_TYPE_DES:
469 if( bits != 64 && bits != 128 && bits != 192 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_ARC4_C)
474 case PSA_KEY_TYPE_ARC4:
475 if( bits < 8 || bits > 2048 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200479#if defined(MBEDTLS_CHACHA20_C)
480 case PSA_KEY_TYPE_CHACHA20:
481 if( bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485 default:
486 return( PSA_ERROR_NOT_SUPPORTED );
487 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200488 if( bits % 8 != 0 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490
491 /* Allocate memory for the key */
492 raw->bytes = PSA_BITS_TO_BYTES( bits );
493 raw->data = mbedtls_calloc( 1, raw->bytes );
494 if( raw->data == NULL )
495 {
496 raw->bytes = 0;
497 return( PSA_ERROR_INSUFFICIENT_MEMORY );
498 }
499 return( PSA_SUCCESS );
500}
501
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200502#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100503/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
504 * that are not a multiple of 8) well. For example, there is only
505 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
506 * way to return the exact bit size of a key.
507 * To keep things simple, reject non-byte-aligned key sizes. */
508static psa_status_t psa_check_rsa_key_byte_aligned(
509 const mbedtls_rsa_context *rsa )
510{
511 mbedtls_mpi n;
512 psa_status_t status;
513 mbedtls_mpi_init( &n );
514 status = mbedtls_to_psa_error(
515 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
516 if( status == PSA_SUCCESS )
517 {
518 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
519 status = PSA_ERROR_NOT_SUPPORTED;
520 }
521 mbedtls_mpi_free( &n );
522 return( status );
523}
524
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000525static psa_status_t psa_import_rsa_key( psa_key_type_t type,
526 const uint8_t *data,
527 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200528 mbedtls_rsa_context **p_rsa )
529{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000530 psa_status_t status;
531 mbedtls_pk_context pk;
532 mbedtls_rsa_context *rsa;
533 size_t bits;
534
535 mbedtls_pk_init( &pk );
536
537 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200538 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000539 status = mbedtls_to_psa_error(
540 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200541 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000542 status = mbedtls_to_psa_error(
543 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
544 if( status != PSA_SUCCESS )
545 goto exit;
546
547 /* We have something that the pkparse module recognizes. If it is a
548 * valid RSA key, store it. */
549 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200550 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000551 status = PSA_ERROR_INVALID_ARGUMENT;
552 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200553 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000554
555 rsa = mbedtls_pk_rsa( pk );
556 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
557 * supports non-byte-aligned key sizes, but not well. For example,
558 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
559 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
560 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
561 {
562 status = PSA_ERROR_NOT_SUPPORTED;
563 goto exit;
564 }
565 status = psa_check_rsa_key_byte_aligned( rsa );
566
567exit:
568 /* Free the content of the pk object only on error. */
569 if( status != PSA_SUCCESS )
570 {
571 mbedtls_pk_free( &pk );
572 return( status );
573 }
574
575 /* On success, store the content of the object in the RSA context. */
576 *p_rsa = rsa;
577
578 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200579}
580#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
581
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000582#if defined(MBEDTLS_ECP_C)
583
584/* Import a public key given as the uncompressed representation defined by SEC1
585 * 2.3.3 as the content of an ECPoint. */
586static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
587 const uint8_t *data,
588 size_t data_length,
589 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200590{
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000591 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
592 mbedtls_ecp_keypair *ecp = NULL;
593 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
594
595 *p_ecp = NULL;
596 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
597 if( ecp == NULL )
598 return( PSA_ERROR_INSUFFICIENT_MEMORY );
599 mbedtls_ecp_keypair_init( ecp );
600
601 /* Load the group. */
602 status = mbedtls_to_psa_error(
603 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
604 if( status != PSA_SUCCESS )
605 goto exit;
606 /* Load the public value. */
607 status = mbedtls_to_psa_error(
608 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
609 data, data_length ) );
610 if( status != PSA_SUCCESS )
611 goto exit;
612
613 /* Check that the point is on the curve. */
614 status = mbedtls_to_psa_error(
615 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
616 if( status != PSA_SUCCESS )
617 goto exit;
618
619 *p_ecp = ecp;
620 return( PSA_SUCCESS );
621
622exit:
623 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200624 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000625 mbedtls_ecp_keypair_free( ecp );
626 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200627 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000628 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200629}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000630#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631
Gilles Peskinef76aa772018-10-29 19:24:33 +0100632#if defined(MBEDTLS_ECP_C)
633/* Import a private key given as a byte string which is the private value
634 * in big-endian order. */
635static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
636 const uint8_t *data,
637 size_t data_length,
638 mbedtls_ecp_keypair **p_ecp )
639{
640 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
641 mbedtls_ecp_keypair *ecp = NULL;
642 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
643
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200644 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
645 return( PSA_ERROR_INVALID_ARGUMENT );
646
Gilles Peskinef76aa772018-10-29 19:24:33 +0100647 *p_ecp = NULL;
648 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
649 if( ecp == NULL )
650 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000651 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652
653 /* Load the group. */
654 status = mbedtls_to_psa_error(
655 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
656 if( status != PSA_SUCCESS )
657 goto exit;
658 /* Load the secret value. */
659 status = mbedtls_to_psa_error(
660 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
661 if( status != PSA_SUCCESS )
662 goto exit;
663 /* Validate the private key. */
664 status = mbedtls_to_psa_error(
665 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
666 if( status != PSA_SUCCESS )
667 goto exit;
668 /* Calculate the public key from the private key. */
669 status = mbedtls_to_psa_error(
670 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
671 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
672 if( status != PSA_SUCCESS )
673 goto exit;
674
675 *p_ecp = ecp;
676 return( PSA_SUCCESS );
677
678exit:
679 if( ecp != NULL )
680 {
681 mbedtls_ecp_keypair_free( ecp );
682 mbedtls_free( ecp );
683 }
684 return( status );
685}
686#endif /* defined(MBEDTLS_ECP_C) */
687
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100688/** Import key data into a slot. `slot->type` must have been set
689 * previously. This function assumes that the slot does not contain
690 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100691psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
692 const uint8_t *data,
693 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100694{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200695 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100696
Darryl Green940d72c2018-07-13 13:18:51 +0100697 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100699 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100700 if( data_length > SIZE_MAX / 8 )
701 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100702 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200703 PSA_BYTES_TO_BITS( data_length ),
704 &slot->data.raw );
705 if( status != PSA_SUCCESS )
706 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200707 if( data_length != 0 )
708 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100709 }
710 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100711#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200712 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100713 {
Darryl Green940d72c2018-07-13 13:18:51 +0100714 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100715 data, data_length,
716 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100717 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000718 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
719 {
720 status = psa_import_ec_public_key(
721 PSA_KEY_TYPE_GET_CURVE( slot->type ),
722 data, data_length,
723 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000724 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100725 else
726#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000727#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
728 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100729 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000730 status = psa_import_rsa_key( slot->type,
731 data, data_length,
732 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100733 }
734 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000735#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736 {
737 return( PSA_ERROR_NOT_SUPPORTED );
738 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000739 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100740}
741
Darryl Green06fd18d2018-07-16 11:21:11 +0100742/* Retrieve an empty key slot (slot with no key data, but possibly
Jaeden Amero283dfd12019-01-11 12:06:22 +0000743 * with some metadata such as a policy or domain parameters). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100744static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100745 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100746{
747 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100748 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100749
750 *p_slot = NULL;
751
Gilles Peskinec5487a82018-12-03 18:08:14 +0100752 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100753 if( status != PSA_SUCCESS )
754 return( status );
755
756 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200757 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100758
759 *p_slot = slot;
760 return( status );
761}
762
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100763/** Calculate the intersection of two algorithm usage policies.
764 *
765 * Return 0 (which allows no operation) on incompatibility.
766 */
767static psa_algorithm_t psa_key_policy_algorithm_intersection(
768 psa_algorithm_t alg1,
769 psa_algorithm_t alg2 )
770{
771 /* Common case: the policy only allows alg. */
772 if( alg1 == alg2 )
773 return( alg1 );
774 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100775 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100776 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
777 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
778 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
779 {
780 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
781 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100782 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100783 return( alg1 );
784 }
785 /* If the policies are incompatible, allow nothing. */
786 return( 0 );
787}
788
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100789/** Test whether a policy permits an algorithm.
790 *
791 * The caller must test usage flags separately.
792 */
793static int psa_key_policy_permits( const psa_key_policy_t *policy,
794 psa_algorithm_t alg )
795{
796 /* Common case: the policy only allows alg. */
797 if( alg == policy->alg )
798 return( 1 );
799 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
800 * and alg is the same hash-and-sign family with any hash,
801 * then alg is compliant with policy->alg. */
802 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
803 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
804 {
805 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
806 ( alg & ~PSA_ALG_HASH_MASK ) );
807 }
808 /* If it isn't permitted, it's forbidden. */
809 return( 0 );
810}
811
Gilles Peskinef603c712019-01-19 13:40:11 +0100812/** Restrict a key policy based on a constraint.
813 *
814 * \param[in,out] policy The policy to restrict.
815 * \param[in] constraint The policy constraint to apply.
816 *
817 * \retval #PSA_SUCCESS
818 * \c *policy contains the intersection of the original value of
819 * \c *policy and \c *constraint.
820 * \retval #PSA_ERROR_INVALID_ARGUMENT
821 * \c *policy and \c *constraint are incompatible.
822 * \c *policy is unchanged.
823 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100824static psa_status_t psa_restrict_key_policy(
825 psa_key_policy_t *policy,
826 const psa_key_policy_t *constraint )
827{
828 psa_algorithm_t intersection_alg =
829 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
830 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
831 return( PSA_ERROR_INVALID_ARGUMENT );
832 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100833 policy->alg = intersection_alg;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100834 return( PSA_SUCCESS );
835}
836
Darryl Green06fd18d2018-07-16 11:21:11 +0100837/** Retrieve a slot which must contain a key. The key must have allow all the
838 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
839 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100840static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100841 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100842 psa_key_usage_t usage,
843 psa_algorithm_t alg )
844{
845 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100846 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100847
848 *p_slot = NULL;
849
Gilles Peskinec5487a82018-12-03 18:08:14 +0100850 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100851 if( status != PSA_SUCCESS )
852 return( status );
853 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200854 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100855
856 /* Enforce that usage policy for the key slot contains all the flags
857 * required by the usage parameter. There is one exception: public
858 * keys can always be exported, so we treat public key objects as
859 * if they had the export flag. */
860 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
861 usage &= ~PSA_KEY_USAGE_EXPORT;
862 if( ( slot->policy.usage & usage ) != usage )
863 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100864
865 /* Enforce that the usage policy permits the requested algortihm. */
866 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100867 return( PSA_ERROR_NOT_PERMITTED );
868
869 *p_slot = slot;
870 return( PSA_SUCCESS );
871}
Darryl Green940d72c2018-07-13 13:18:51 +0100872
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100873/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100874static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000875{
876 if( slot->type == PSA_KEY_TYPE_NONE )
877 {
878 /* No key material to clean. */
879 }
880 else if( key_type_is_raw_bytes( slot->type ) )
881 {
882 mbedtls_free( slot->data.raw.data );
883 }
884 else
885#if defined(MBEDTLS_RSA_C)
886 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
887 {
888 mbedtls_rsa_free( slot->data.rsa );
889 mbedtls_free( slot->data.rsa );
890 }
891 else
892#endif /* defined(MBEDTLS_RSA_C) */
893#if defined(MBEDTLS_ECP_C)
894 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
895 {
896 mbedtls_ecp_keypair_free( slot->data.ecp );
897 mbedtls_free( slot->data.ecp );
898 }
899 else
900#endif /* defined(MBEDTLS_ECP_C) */
901 {
902 /* Shouldn't happen: the key type is not any type that we
903 * put in. */
904 return( PSA_ERROR_TAMPERING_DETECTED );
905 }
906
907 return( PSA_SUCCESS );
908}
909
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100910static void psa_abort_operations_using_key( psa_key_slot_t *slot )
911{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200912 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100913 (void) slot;
914}
915
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100916/** Completely wipe a slot in memory, including its policy.
917 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100918psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100919{
920 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100921 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100922 /* At this point, key material and other type-specific content has
923 * been wiped. Clear remaining metadata. We can call memset and not
924 * zeroize because the metadata is not particularly sensitive. */
925 memset( slot, 0, sizeof( *slot ) );
926 return( status );
927}
928
Gilles Peskine87a5e562019-04-17 12:28:25 +0200929psa_status_t psa_import_key_to_handle( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100930 psa_key_type_t type,
931 const uint8_t *data,
932 size_t data_length )
933{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100934 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100935 psa_status_t status;
936
Gilles Peskinec5487a82018-12-03 18:08:14 +0100937 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100938 if( status != PSA_SUCCESS )
939 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100940
941 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100942
943 status = psa_import_key_into_slot( slot, data, data_length );
944 if( status != PSA_SUCCESS )
945 {
946 slot->type = PSA_KEY_TYPE_NONE;
947 return( status );
948 }
949
Darryl Greend49a4992018-06-18 17:27:26 +0100950#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
951 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
952 {
953 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100954 status = psa_save_persistent_key( slot->persistent_storage_id,
955 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100956 data_length );
957 if( status != PSA_SUCCESS )
958 {
959 (void) psa_remove_key_data_from_memory( slot );
960 slot->type = PSA_KEY_TYPE_NONE;
961 }
962 }
963#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
964
965 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966}
967
Gilles Peskinec5487a82018-12-03 18:08:14 +0100968psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100970 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100971 psa_status_t status = PSA_SUCCESS;
972 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100973
Gilles Peskinec5487a82018-12-03 18:08:14 +0100974 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200975 if( status != PSA_SUCCESS )
976 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100977#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
978 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
979 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100980 storage_status =
981 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100982 }
983#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100984 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100985 if( status != PSA_SUCCESS )
986 return( status );
987 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988}
989
Gilles Peskineb870b182018-07-06 16:02:09 +0200990/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200991static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200992{
993 if( key_type_is_raw_bytes( slot->type ) )
994 return( slot->data.raw.bytes * 8 );
995#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200996 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100997 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200998#endif /* defined(MBEDTLS_RSA_C) */
999#if defined(MBEDTLS_ECP_C)
1000 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1001 return( slot->data.ecp->grp.pbits );
1002#endif /* defined(MBEDTLS_ECP_C) */
1003 /* Shouldn't happen except on an empty slot. */
1004 return( 0 );
1005}
1006
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001007void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1008{
Gilles Peskineb699f072019-04-26 16:06:02 +02001009 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001010 memset( attributes, 0, sizeof( *attributes ) );
1011}
1012
Gilles Peskineb699f072019-04-26 16:06:02 +02001013psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1014 psa_key_type_t type,
1015 const uint8_t *data,
1016 size_t data_length )
1017{
1018 uint8_t *copy = NULL;
1019
1020 if( data_length != 0 )
1021 {
1022 copy = mbedtls_calloc( 1, data_length );
1023 if( copy == NULL )
1024 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1025 memcpy( copy, data, data_length );
1026 }
1027 /* After this point, this function is guaranteed to succeed, so it
1028 * can start modifying `*attributes`. */
1029
1030 if( attributes->domain_parameters != NULL )
1031 {
1032 mbedtls_free( attributes->domain_parameters );
1033 attributes->domain_parameters = NULL;
1034 attributes->domain_parameters_size = 0;
1035 }
1036
1037 attributes->domain_parameters = copy;
1038 attributes->domain_parameters_size = data_length;
1039 attributes->type = type;
1040 return( PSA_SUCCESS );
1041}
1042
1043psa_status_t psa_get_key_domain_parameters(
1044 const psa_key_attributes_t *attributes,
1045 uint8_t *data, size_t data_size, size_t *data_length )
1046{
1047 if( attributes->domain_parameters_size > data_size )
1048 return( PSA_ERROR_BUFFER_TOO_SMALL );
1049 *data_length = attributes->domain_parameters_size;
1050 if( attributes->domain_parameters_size != 0 )
1051 memcpy( data, attributes->domain_parameters,
1052 attributes->domain_parameters_size );
1053 return( PSA_SUCCESS );
1054}
1055
1056#if defined(MBEDTLS_RSA_C)
1057static psa_status_t psa_get_rsa_public_exponent(
1058 const mbedtls_rsa_context *rsa,
1059 psa_key_attributes_t *attributes )
1060{
1061 mbedtls_mpi mpi;
1062 int ret;
1063 uint8_t *buffer = NULL;
1064 size_t buflen;
1065 mbedtls_mpi_init( &mpi );
1066
1067 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1068 if( ret != 0 )
1069 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001070 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1071 {
1072 /* It's the default value, which is reported as an empty string,
1073 * so there's nothing to do. */
1074 goto exit;
1075 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001076
1077 buflen = mbedtls_mpi_size( &mpi );
1078 buffer = mbedtls_calloc( 1, buflen );
1079 if( buffer == NULL )
1080 {
1081 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1082 goto exit;
1083 }
1084 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1085 if( ret != 0 )
1086 goto exit;
1087 attributes->domain_parameters = buffer;
1088 attributes->domain_parameters_size = buflen;
1089
1090exit:
1091 mbedtls_mpi_free( &mpi );
1092 if( ret != 0 )
1093 mbedtls_free( buffer );
1094 return( mbedtls_to_psa_error( ret ) );
1095}
1096#endif /* MBEDTLS_RSA_C */
1097
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001098psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1099 psa_key_attributes_t *attributes )
1100{
1101 psa_key_slot_t *slot;
1102 psa_status_t status;
1103
1104 psa_reset_key_attributes( attributes );
1105
1106 status = psa_get_key_slot( handle, &slot );
1107 if( status != PSA_SUCCESS )
1108 return( status );
1109
1110 attributes->id = slot->persistent_storage_id;
1111 attributes->lifetime = slot->lifetime;
1112 attributes->policy = slot->policy;
1113 attributes->type = slot->type;
1114 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001115
1116 switch( slot->type )
1117 {
1118#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001119 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001120 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1121 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1122 break;
1123#endif
1124 default:
1125 /* Nothing else to do. */
1126 break;
1127 }
1128
1129 if( status != PSA_SUCCESS )
1130 psa_reset_key_attributes( attributes );
1131 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001132}
1133
Gilles Peskinec5487a82018-12-03 18:08:14 +01001134psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001135 psa_key_type_t *type,
1136 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001137{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001138 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001139 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001140
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001141 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001142 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001143 if( bits != NULL )
1144 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001145 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001146 if( status != PSA_SUCCESS )
1147 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001148
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001149 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001150 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001151 if( type != NULL )
1152 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001153 if( bits != NULL )
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001154 *bits = psa_get_key_slot_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001155 return( PSA_SUCCESS );
1156}
1157
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001158#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001159static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1160 unsigned char *buf, size_t size )
1161{
1162 int ret;
1163 unsigned char *c;
1164 size_t len = 0;
1165
1166 c = buf + size;
1167
1168 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1169
1170 return( (int) len );
1171}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001172#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001173
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001174static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1175 uint8_t *data,
1176 size_t data_size,
1177 size_t *data_length,
1178 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001179{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001180 *data_length = 0;
1181
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001182 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001183 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001184
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001185 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001186 {
1187 if( slot->data.raw.bytes > data_size )
1188 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001189 if( data_size != 0 )
1190 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001191 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001192 memset( data + slot->data.raw.bytes, 0,
1193 data_size - slot->data.raw.bytes );
1194 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001195 *data_length = slot->data.raw.bytes;
1196 return( PSA_SUCCESS );
1197 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001198#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001199 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001200 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001201 psa_status_t status;
1202
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001203 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001204 if( bytes > data_size )
1205 return( PSA_ERROR_BUFFER_TOO_SMALL );
1206 status = mbedtls_to_psa_error(
1207 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1208 if( status != PSA_SUCCESS )
1209 return( status );
1210 memset( data + bytes, 0, data_size - bytes );
1211 *data_length = bytes;
1212 return( PSA_SUCCESS );
1213 }
1214#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001215 else
Moran Peker17e36e12018-05-02 12:55:20 +03001216 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001217#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001218 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001219 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001220 {
Moran Pekera998bc62018-04-16 18:16:20 +03001221 mbedtls_pk_context pk;
1222 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001223 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001224 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001225#if defined(MBEDTLS_RSA_C)
1226 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001227 pk.pk_info = &mbedtls_rsa_info;
1228 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001229#else
1230 return( PSA_ERROR_NOT_SUPPORTED );
1231#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001232 }
1233 else
1234 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001235#if defined(MBEDTLS_ECP_C)
1236 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001237 pk.pk_info = &mbedtls_eckey_info;
1238 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001239#else
1240 return( PSA_ERROR_NOT_SUPPORTED );
1241#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001242 }
Moran Pekerd7326592018-05-29 16:56:39 +03001243 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001244 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001245 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001246 }
Moran Peker17e36e12018-05-02 12:55:20 +03001247 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001248 {
Moran Peker17e36e12018-05-02 12:55:20 +03001249 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001250 }
Moran Peker60364322018-04-29 11:34:58 +03001251 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001252 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001253 /* If data_size is 0 then data may be NULL and then the
1254 * call to memset would have undefined behavior. */
1255 if( data_size != 0 )
1256 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001257 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001258 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001259 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1260 * Move the data to the beginning and erase remaining data
1261 * at the original location. */
1262 if( 2 * (size_t) ret <= data_size )
1263 {
1264 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001265 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001266 }
1267 else if( (size_t) ret < data_size )
1268 {
1269 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001270 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001271 }
Moran Pekera998bc62018-04-16 18:16:20 +03001272 *data_length = ret;
1273 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001274 }
1275 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001276#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001277 {
1278 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001279 it is valid for a special-purpose implementation to omit
1280 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001281 return( PSA_ERROR_NOT_SUPPORTED );
1282 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001283 }
1284}
1285
Gilles Peskinec5487a82018-12-03 18:08:14 +01001286psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001287 uint8_t *data,
1288 size_t data_size,
1289 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001290{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001291 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001292 psa_status_t status;
1293
1294 /* Set the key to empty now, so that even when there are errors, we always
1295 * set data_length to a value between 0 and data_size. On error, setting
1296 * the key to empty is a good choice because an empty key representation is
1297 * unlikely to be accepted anywhere. */
1298 *data_length = 0;
1299
1300 /* Export requires the EXPORT flag. There is an exception for public keys,
1301 * which don't require any flag, but psa_get_key_from_slot takes
1302 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001303 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001304 if( status != PSA_SUCCESS )
1305 return( status );
1306 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001307 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001308}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001309
Gilles Peskinec5487a82018-12-03 18:08:14 +01001310psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001311 uint8_t *data,
1312 size_t data_size,
1313 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001314{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001315 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001316 psa_status_t status;
1317
1318 /* Set the key to empty now, so that even when there are errors, we always
1319 * set data_length to a value between 0 and data_size. On error, setting
1320 * the key to empty is a good choice because an empty key representation is
1321 * unlikely to be accepted anywhere. */
1322 *data_length = 0;
1323
1324 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001325 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001326 if( status != PSA_SUCCESS )
1327 return( status );
1328 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001329 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001330}
1331
Darryl Green0c6575a2018-11-07 16:05:30 +00001332#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001333static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001334 size_t bits )
1335{
1336 psa_status_t status;
1337 uint8_t *data;
1338 size_t key_length;
1339 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1340 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001341 if( data == NULL )
1342 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001343 /* Get key data in export format */
1344 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1345 if( status != PSA_SUCCESS )
1346 {
1347 slot->type = PSA_KEY_TYPE_NONE;
1348 goto exit;
1349 }
1350 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001351 status = psa_save_persistent_key( slot->persistent_storage_id,
1352 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001353 data, key_length );
1354 if( status != PSA_SUCCESS )
1355 {
1356 slot->type = PSA_KEY_TYPE_NONE;
1357 }
1358exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001359 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001360 mbedtls_free( data );
1361 return( status );
1362}
1363#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1364
Gilles Peskine4747d192019-04-17 15:05:45 +02001365static psa_status_t psa_set_key_policy_internal(
1366 psa_key_slot_t *slot,
1367 const psa_key_policy_t *policy )
1368{
1369 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001370 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001371 PSA_KEY_USAGE_ENCRYPT |
1372 PSA_KEY_USAGE_DECRYPT |
1373 PSA_KEY_USAGE_SIGN |
1374 PSA_KEY_USAGE_VERIFY |
1375 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1376 return( PSA_ERROR_INVALID_ARGUMENT );
1377
1378 slot->policy = *policy;
1379 return( PSA_SUCCESS );
1380}
1381
1382/** Prepare a key slot to receive key material.
1383 *
1384 * This function allocates a key slot and sets its metadata.
1385 *
1386 * If this function fails, call psa_fail_key_creation().
1387 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001388 * This function is intended to be used as follows:
1389 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1390 * it with the specified attributes, and assign it a handle.
1391 * -# Populate the slot with the key material.
1392 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1393 * In case of failure at any step, stop the sequence and call
1394 * psa_fail_key_creation().
1395 *
Gilles Peskine4747d192019-04-17 15:05:45 +02001396 * \param attributes Key attributes for the new key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001397 * \param handle On success, a handle for the allocated slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001398 * \param p_slot On success, a pointer to the prepared slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001399 *
1400 * \retval #PSA_SUCCESS
1401 * The key slot is ready to receive key material.
1402 * \return If this function fails, the key slot is an invalid state.
1403 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001404 */
1405static psa_status_t psa_start_key_creation(
1406 const psa_key_attributes_t *attributes,
1407 psa_key_handle_t *handle,
1408 psa_key_slot_t **p_slot )
1409{
1410 psa_status_t status;
1411 psa_key_slot_t *slot;
1412
1413 status = psa_allocate_key( handle );
1414 if( status != PSA_SUCCESS )
1415 return( status );
1416 status = psa_get_key_slot( *handle, p_slot );
1417 if( status != PSA_SUCCESS )
1418 return( status );
1419 slot = *p_slot;
1420
1421 status = psa_set_key_policy_internal( slot, &attributes->policy );
1422 if( status != PSA_SUCCESS )
1423 return( status );
1424 slot->lifetime = attributes->lifetime;
1425 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001426 {
1427 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskinef9666592019-05-06 18:56:30 +02001428 attributes->id, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001429 if( status != PSA_SUCCESS )
1430 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001431 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001432 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001433 slot->type = attributes->type;
1434
1435 return( status );
1436}
1437
1438/** Finalize the creation of a key once its key material has been set.
1439 *
1440 * This entails writing the key to persistent storage.
1441 *
1442 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001443 * See the documentation of psa_start_key_creation() for the intended use
1444 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001445 *
1446 * \param slot Pointer to the slot with key material.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001447 *
1448 * \retval #PSA_SUCCESS
1449 * The key was successfully created. The handle is now valid.
1450 * \return If this function fails, the key slot is an invalid state.
1451 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001452 */
1453static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1454{
1455 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001456 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001457
1458#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1459 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1460 {
1461 uint8_t *buffer = NULL;
1462 size_t buffer_size = 0;
1463 size_t length;
1464
1465 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001466 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001467 buffer = mbedtls_calloc( 1, buffer_size );
1468 if( buffer == NULL && buffer_size != 0 )
1469 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1470 status = psa_internal_export_key( slot,
1471 buffer, buffer_size, &length,
1472 0 );
1473
1474 if( status == PSA_SUCCESS )
1475 {
1476 status = psa_save_persistent_key( slot->persistent_storage_id,
1477 slot->type, &slot->policy,
1478 buffer, length );
1479 }
1480
1481 if( buffer_size != 0 )
1482 mbedtls_platform_zeroize( buffer, buffer_size );
1483 mbedtls_free( buffer );
1484 }
1485#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1486
1487 return( status );
1488}
1489
1490/** Abort the creation of a key.
1491 *
1492 * You may call this function after calling psa_start_key_creation(),
1493 * or after psa_finish_key_creation() fails. In other circumstances, this
1494 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001495 * See the documentation of psa_start_key_creation() for the intended use
1496 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001497 *
1498 * \param slot Pointer to the slot with key material.
1499 */
1500static void psa_fail_key_creation( psa_key_slot_t *slot )
1501{
1502 if( slot == NULL )
1503 return;
1504 psa_wipe_key_slot( slot );
1505}
1506
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001507static psa_status_t psa_check_key_slot_attributes(
1508 const psa_key_slot_t *slot,
1509 const psa_key_attributes_t *attributes )
1510{
1511 if( attributes->type != 0 )
1512 {
1513 if( attributes->type != slot->type )
1514 return( PSA_ERROR_INVALID_ARGUMENT );
1515 }
1516
1517 if( attributes->domain_parameters_size != 0 )
1518 {
1519#if defined(MBEDTLS_RSA_C)
1520 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1521 {
1522 mbedtls_mpi actual, required;
1523 int ret;
1524 mbedtls_mpi_init( &actual );
1525 mbedtls_mpi_init( &required );
1526 ret = mbedtls_rsa_export( slot->data.rsa,
1527 NULL, NULL, NULL, NULL, &actual );
1528 if( ret != 0 )
1529 goto rsa_exit;
1530 ret = mbedtls_mpi_read_binary( &required,
1531 attributes->domain_parameters,
1532 attributes->domain_parameters_size );
1533 if( ret != 0 )
1534 goto rsa_exit;
1535 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1536 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1537 rsa_exit:
1538 mbedtls_mpi_free( &actual );
1539 mbedtls_mpi_free( &required );
1540 if( ret != 0)
1541 return( mbedtls_to_psa_error( ret ) );
1542 }
1543 else
1544#endif
1545 {
1546 return( PSA_ERROR_INVALID_ARGUMENT );
1547 }
1548 }
1549
1550 if( attributes->bits != 0 )
1551 {
1552 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1553 return( PSA_ERROR_INVALID_ARGUMENT );
1554 }
1555
1556 return( PSA_SUCCESS );
1557}
1558
Gilles Peskine4747d192019-04-17 15:05:45 +02001559psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001560 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001561 size_t data_length,
1562 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001563{
1564 psa_status_t status;
1565 psa_key_slot_t *slot = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001566
Gilles Peskine4747d192019-04-17 15:05:45 +02001567 status = psa_start_key_creation( attributes, handle, &slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001568 if( status != PSA_SUCCESS )
1569 goto exit;
1570
1571 status = psa_import_key_into_slot( slot, data, data_length );
1572 if( status != PSA_SUCCESS )
1573 goto exit;
1574 status = psa_check_key_slot_attributes( slot, attributes );
1575 if( status != PSA_SUCCESS )
1576 goto exit;
1577
1578 status = psa_finish_key_creation( slot );
1579exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001580 if( status != PSA_SUCCESS )
1581 {
1582 psa_fail_key_creation( slot );
1583 *handle = 0;
1584 }
1585 return( status );
1586}
1587
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001588static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001589 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001590{
1591 psa_status_t status;
1592 uint8_t *buffer = NULL;
1593 size_t buffer_size = 0;
1594 size_t length;
1595
1596 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001597 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001598 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001599 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001600 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001601 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1602 if( status != PSA_SUCCESS )
1603 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001604 target->type = source->type;
1605 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001606
1607exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001608 if( buffer_size != 0 )
1609 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001610 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001611 return( status );
1612}
1613
Gilles Peskine87a5e562019-04-17 12:28:25 +02001614psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001615 psa_key_handle_t target_handle,
1616 const psa_key_policy_t *constraint)
1617{
1618 psa_key_slot_t *source_slot = NULL;
1619 psa_key_slot_t *target_slot = NULL;
1620 psa_key_policy_t new_policy;
1621 psa_status_t status;
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001622 status = psa_get_key_from_slot( source_handle, &source_slot,
1623 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001624 if( status != PSA_SUCCESS )
1625 return( status );
1626 status = psa_get_empty_key_slot( target_handle, &target_slot );
1627 if( status != PSA_SUCCESS )
1628 return( status );
1629
1630 new_policy = target_slot->policy;
1631 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1632 if( status != PSA_SUCCESS )
1633 return( status );
1634 if( constraint != NULL )
1635 {
1636 status = psa_restrict_key_policy( &new_policy, constraint );
1637 if( status != PSA_SUCCESS )
1638 return( status );
1639 }
1640
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001641 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001642 if( status != PSA_SUCCESS )
1643 return( status );
1644
1645 target_slot->policy = new_policy;
1646 return( PSA_SUCCESS );
1647}
1648
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001649psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1650 const psa_key_attributes_t *specified_attributes,
1651 psa_key_handle_t *target_handle )
1652{
1653 psa_status_t status;
1654 psa_key_slot_t *source_slot = NULL;
1655 psa_key_slot_t *target_slot = NULL;
1656 psa_key_attributes_t actual_attributes = *specified_attributes;
1657
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001658 status = psa_get_key_from_slot( source_handle, &source_slot,
1659 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001660 if( status != PSA_SUCCESS )
1661 goto exit;
1662
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001663 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1664 if( status != PSA_SUCCESS )
1665 goto exit;
1666
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001667 status = psa_restrict_key_policy( &actual_attributes.policy,
1668 &source_slot->policy );
1669 if( status != PSA_SUCCESS )
1670 goto exit;
1671
1672 status = psa_start_key_creation( &actual_attributes,
1673 target_handle, &target_slot );
1674 if( status != PSA_SUCCESS )
1675 goto exit;
1676
1677 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001678 if( status != PSA_SUCCESS )
1679 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001680
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001681 status = psa_finish_key_creation( target_slot );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001682exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001683 if( status != PSA_SUCCESS )
1684 {
1685 psa_fail_key_creation( target_slot );
1686 *target_handle = 0;
1687 }
1688 return( status );
1689}
1690
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001691
1692
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001693/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001694/* Message digests */
1695/****************************************************************/
1696
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001697static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001698{
1699 switch( alg )
1700 {
1701#if defined(MBEDTLS_MD2_C)
1702 case PSA_ALG_MD2:
1703 return( &mbedtls_md2_info );
1704#endif
1705#if defined(MBEDTLS_MD4_C)
1706 case PSA_ALG_MD4:
1707 return( &mbedtls_md4_info );
1708#endif
1709#if defined(MBEDTLS_MD5_C)
1710 case PSA_ALG_MD5:
1711 return( &mbedtls_md5_info );
1712#endif
1713#if defined(MBEDTLS_RIPEMD160_C)
1714 case PSA_ALG_RIPEMD160:
1715 return( &mbedtls_ripemd160_info );
1716#endif
1717#if defined(MBEDTLS_SHA1_C)
1718 case PSA_ALG_SHA_1:
1719 return( &mbedtls_sha1_info );
1720#endif
1721#if defined(MBEDTLS_SHA256_C)
1722 case PSA_ALG_SHA_224:
1723 return( &mbedtls_sha224_info );
1724 case PSA_ALG_SHA_256:
1725 return( &mbedtls_sha256_info );
1726#endif
1727#if defined(MBEDTLS_SHA512_C)
1728 case PSA_ALG_SHA_384:
1729 return( &mbedtls_sha384_info );
1730 case PSA_ALG_SHA_512:
1731 return( &mbedtls_sha512_info );
1732#endif
1733 default:
1734 return( NULL );
1735 }
1736}
1737
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001738psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1739{
1740 switch( operation->alg )
1741 {
Gilles Peskine81736312018-06-26 15:04:31 +02001742 case 0:
1743 /* The object has (apparently) been initialized but it is not
1744 * in use. It's ok to call abort on such an object, and there's
1745 * nothing to do. */
1746 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001747#if defined(MBEDTLS_MD2_C)
1748 case PSA_ALG_MD2:
1749 mbedtls_md2_free( &operation->ctx.md2 );
1750 break;
1751#endif
1752#if defined(MBEDTLS_MD4_C)
1753 case PSA_ALG_MD4:
1754 mbedtls_md4_free( &operation->ctx.md4 );
1755 break;
1756#endif
1757#if defined(MBEDTLS_MD5_C)
1758 case PSA_ALG_MD5:
1759 mbedtls_md5_free( &operation->ctx.md5 );
1760 break;
1761#endif
1762#if defined(MBEDTLS_RIPEMD160_C)
1763 case PSA_ALG_RIPEMD160:
1764 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1765 break;
1766#endif
1767#if defined(MBEDTLS_SHA1_C)
1768 case PSA_ALG_SHA_1:
1769 mbedtls_sha1_free( &operation->ctx.sha1 );
1770 break;
1771#endif
1772#if defined(MBEDTLS_SHA256_C)
1773 case PSA_ALG_SHA_224:
1774 case PSA_ALG_SHA_256:
1775 mbedtls_sha256_free( &operation->ctx.sha256 );
1776 break;
1777#endif
1778#if defined(MBEDTLS_SHA512_C)
1779 case PSA_ALG_SHA_384:
1780 case PSA_ALG_SHA_512:
1781 mbedtls_sha512_free( &operation->ctx.sha512 );
1782 break;
1783#endif
1784 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001785 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001786 }
1787 operation->alg = 0;
1788 return( PSA_SUCCESS );
1789}
1790
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001791psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001792 psa_algorithm_t alg )
1793{
1794 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001795
1796 /* A context must be freshly initialized before it can be set up. */
1797 if( operation->alg != 0 )
1798 {
1799 return( PSA_ERROR_BAD_STATE );
1800 }
1801
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001802 switch( alg )
1803 {
1804#if defined(MBEDTLS_MD2_C)
1805 case PSA_ALG_MD2:
1806 mbedtls_md2_init( &operation->ctx.md2 );
1807 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1808 break;
1809#endif
1810#if defined(MBEDTLS_MD4_C)
1811 case PSA_ALG_MD4:
1812 mbedtls_md4_init( &operation->ctx.md4 );
1813 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1814 break;
1815#endif
1816#if defined(MBEDTLS_MD5_C)
1817 case PSA_ALG_MD5:
1818 mbedtls_md5_init( &operation->ctx.md5 );
1819 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1820 break;
1821#endif
1822#if defined(MBEDTLS_RIPEMD160_C)
1823 case PSA_ALG_RIPEMD160:
1824 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1825 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1826 break;
1827#endif
1828#if defined(MBEDTLS_SHA1_C)
1829 case PSA_ALG_SHA_1:
1830 mbedtls_sha1_init( &operation->ctx.sha1 );
1831 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1832 break;
1833#endif
1834#if defined(MBEDTLS_SHA256_C)
1835 case PSA_ALG_SHA_224:
1836 mbedtls_sha256_init( &operation->ctx.sha256 );
1837 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1838 break;
1839 case PSA_ALG_SHA_256:
1840 mbedtls_sha256_init( &operation->ctx.sha256 );
1841 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1842 break;
1843#endif
1844#if defined(MBEDTLS_SHA512_C)
1845 case PSA_ALG_SHA_384:
1846 mbedtls_sha512_init( &operation->ctx.sha512 );
1847 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1848 break;
1849 case PSA_ALG_SHA_512:
1850 mbedtls_sha512_init( &operation->ctx.sha512 );
1851 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1852 break;
1853#endif
1854 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001855 return( PSA_ALG_IS_HASH( alg ) ?
1856 PSA_ERROR_NOT_SUPPORTED :
1857 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001858 }
1859 if( ret == 0 )
1860 operation->alg = alg;
1861 else
1862 psa_hash_abort( operation );
1863 return( mbedtls_to_psa_error( ret ) );
1864}
1865
1866psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1867 const uint8_t *input,
1868 size_t input_length )
1869{
1870 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001871
1872 /* Don't require hash implementations to behave correctly on a
1873 * zero-length input, which may have an invalid pointer. */
1874 if( input_length == 0 )
1875 return( PSA_SUCCESS );
1876
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001877 switch( operation->alg )
1878 {
1879#if defined(MBEDTLS_MD2_C)
1880 case PSA_ALG_MD2:
1881 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1882 input, input_length );
1883 break;
1884#endif
1885#if defined(MBEDTLS_MD4_C)
1886 case PSA_ALG_MD4:
1887 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1888 input, input_length );
1889 break;
1890#endif
1891#if defined(MBEDTLS_MD5_C)
1892 case PSA_ALG_MD5:
1893 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1894 input, input_length );
1895 break;
1896#endif
1897#if defined(MBEDTLS_RIPEMD160_C)
1898 case PSA_ALG_RIPEMD160:
1899 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1900 input, input_length );
1901 break;
1902#endif
1903#if defined(MBEDTLS_SHA1_C)
1904 case PSA_ALG_SHA_1:
1905 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1906 input, input_length );
1907 break;
1908#endif
1909#if defined(MBEDTLS_SHA256_C)
1910 case PSA_ALG_SHA_224:
1911 case PSA_ALG_SHA_256:
1912 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1913 input, input_length );
1914 break;
1915#endif
1916#if defined(MBEDTLS_SHA512_C)
1917 case PSA_ALG_SHA_384:
1918 case PSA_ALG_SHA_512:
1919 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1920 input, input_length );
1921 break;
1922#endif
1923 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001924 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001925 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001926
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001927 if( ret != 0 )
1928 psa_hash_abort( operation );
1929 return( mbedtls_to_psa_error( ret ) );
1930}
1931
1932psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1933 uint8_t *hash,
1934 size_t hash_size,
1935 size_t *hash_length )
1936{
itayzafrir40835d42018-08-02 13:14:17 +03001937 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001938 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001939 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001940
1941 /* Fill the output buffer with something that isn't a valid hash
1942 * (barring an attack on the hash and deliberately-crafted input),
1943 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001944 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001945 /* If hash_size is 0 then hash may be NULL and then the
1946 * call to memset would have undefined behavior. */
1947 if( hash_size != 0 )
1948 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001949
1950 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001951 {
1952 status = PSA_ERROR_BUFFER_TOO_SMALL;
1953 goto exit;
1954 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001955
1956 switch( operation->alg )
1957 {
1958#if defined(MBEDTLS_MD2_C)
1959 case PSA_ALG_MD2:
1960 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1961 break;
1962#endif
1963#if defined(MBEDTLS_MD4_C)
1964 case PSA_ALG_MD4:
1965 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1966 break;
1967#endif
1968#if defined(MBEDTLS_MD5_C)
1969 case PSA_ALG_MD5:
1970 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1971 break;
1972#endif
1973#if defined(MBEDTLS_RIPEMD160_C)
1974 case PSA_ALG_RIPEMD160:
1975 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1976 break;
1977#endif
1978#if defined(MBEDTLS_SHA1_C)
1979 case PSA_ALG_SHA_1:
1980 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1981 break;
1982#endif
1983#if defined(MBEDTLS_SHA256_C)
1984 case PSA_ALG_SHA_224:
1985 case PSA_ALG_SHA_256:
1986 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1987 break;
1988#endif
1989#if defined(MBEDTLS_SHA512_C)
1990 case PSA_ALG_SHA_384:
1991 case PSA_ALG_SHA_512:
1992 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1993 break;
1994#endif
1995 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001996 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001997 }
itayzafrir40835d42018-08-02 13:14:17 +03001998 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001999
itayzafrir40835d42018-08-02 13:14:17 +03002000exit:
2001 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002002 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002003 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002004 return( psa_hash_abort( operation ) );
2005 }
2006 else
2007 {
2008 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002009 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002010 }
2011}
2012
Gilles Peskine2d277862018-06-18 15:41:12 +02002013psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2014 const uint8_t *hash,
2015 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002016{
2017 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2018 size_t actual_hash_length;
2019 psa_status_t status = psa_hash_finish( operation,
2020 actual_hash, sizeof( actual_hash ),
2021 &actual_hash_length );
2022 if( status != PSA_SUCCESS )
2023 return( status );
2024 if( actual_hash_length != hash_length )
2025 return( PSA_ERROR_INVALID_SIGNATURE );
2026 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2027 return( PSA_ERROR_INVALID_SIGNATURE );
2028 return( PSA_SUCCESS );
2029}
2030
Gilles Peskineeb35d782019-01-22 17:56:16 +01002031psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2032 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002033{
2034 if( target_operation->alg != 0 )
2035 return( PSA_ERROR_BAD_STATE );
2036
2037 switch( source_operation->alg )
2038 {
2039 case 0:
2040 return( PSA_ERROR_BAD_STATE );
2041#if defined(MBEDTLS_MD2_C)
2042 case PSA_ALG_MD2:
2043 mbedtls_md2_clone( &target_operation->ctx.md2,
2044 &source_operation->ctx.md2 );
2045 break;
2046#endif
2047#if defined(MBEDTLS_MD4_C)
2048 case PSA_ALG_MD4:
2049 mbedtls_md4_clone( &target_operation->ctx.md4,
2050 &source_operation->ctx.md4 );
2051 break;
2052#endif
2053#if defined(MBEDTLS_MD5_C)
2054 case PSA_ALG_MD5:
2055 mbedtls_md5_clone( &target_operation->ctx.md5,
2056 &source_operation->ctx.md5 );
2057 break;
2058#endif
2059#if defined(MBEDTLS_RIPEMD160_C)
2060 case PSA_ALG_RIPEMD160:
2061 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2062 &source_operation->ctx.ripemd160 );
2063 break;
2064#endif
2065#if defined(MBEDTLS_SHA1_C)
2066 case PSA_ALG_SHA_1:
2067 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2068 &source_operation->ctx.sha1 );
2069 break;
2070#endif
2071#if defined(MBEDTLS_SHA256_C)
2072 case PSA_ALG_SHA_224:
2073 case PSA_ALG_SHA_256:
2074 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2075 &source_operation->ctx.sha256 );
2076 break;
2077#endif
2078#if defined(MBEDTLS_SHA512_C)
2079 case PSA_ALG_SHA_384:
2080 case PSA_ALG_SHA_512:
2081 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2082 &source_operation->ctx.sha512 );
2083 break;
2084#endif
2085 default:
2086 return( PSA_ERROR_NOT_SUPPORTED );
2087 }
2088
2089 target_operation->alg = source_operation->alg;
2090 return( PSA_SUCCESS );
2091}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002092
2093
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002094/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002095/* MAC */
2096/****************************************************************/
2097
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002098static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002099 psa_algorithm_t alg,
2100 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002101 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002102 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002103{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002104 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002105 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002106
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002107 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002108 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002109
Gilles Peskine8c9def32018-02-08 10:02:12 +01002110 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2111 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002112 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002113 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002114 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002115 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002116 mode = MBEDTLS_MODE_STREAM;
2117 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002118 case PSA_ALG_CTR:
2119 mode = MBEDTLS_MODE_CTR;
2120 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002121 case PSA_ALG_CFB:
2122 mode = MBEDTLS_MODE_CFB;
2123 break;
2124 case PSA_ALG_OFB:
2125 mode = MBEDTLS_MODE_OFB;
2126 break;
2127 case PSA_ALG_CBC_NO_PADDING:
2128 mode = MBEDTLS_MODE_CBC;
2129 break;
2130 case PSA_ALG_CBC_PKCS7:
2131 mode = MBEDTLS_MODE_CBC;
2132 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002133 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002134 mode = MBEDTLS_MODE_CCM;
2135 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002136 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137 mode = MBEDTLS_MODE_GCM;
2138 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002139 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2140 mode = MBEDTLS_MODE_CHACHAPOLY;
2141 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002142 default:
2143 return( NULL );
2144 }
2145 }
2146 else if( alg == PSA_ALG_CMAC )
2147 mode = MBEDTLS_MODE_ECB;
2148 else if( alg == PSA_ALG_GMAC )
2149 mode = MBEDTLS_MODE_GCM;
2150 else
2151 return( NULL );
2152
2153 switch( key_type )
2154 {
2155 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002156 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002157 break;
2158 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002159 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2160 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002161 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002162 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002163 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002164 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002165 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2166 * but two-key Triple-DES is functionally three-key Triple-DES
2167 * with K1=K3, so that's how we present it to mbedtls. */
2168 if( key_bits == 128 )
2169 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002170 break;
2171 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002172 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002173 break;
2174 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002175 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002176 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002177 case PSA_KEY_TYPE_CHACHA20:
2178 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2179 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002180 default:
2181 return( NULL );
2182 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002183 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002184 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002185
Jaeden Amero23bbb752018-06-26 14:16:54 +01002186 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2187 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002188}
2189
Gilles Peskinea05219c2018-11-16 16:02:56 +01002190#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002191static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002192{
Gilles Peskine2d277862018-06-18 15:41:12 +02002193 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002194 {
2195 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002196 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002197 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002198 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002199 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002200 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002201 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002202 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002203 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002204 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002205 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002206 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002207 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002208 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002209 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002210 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002211 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002212 return( 128 );
2213 default:
2214 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002215 }
2216}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002217#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002218
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002219/* Initialize the MAC operation structure. Once this function has been
2220 * called, psa_mac_abort can run and will do the right thing. */
2221static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2222 psa_algorithm_t alg )
2223{
2224 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2225
2226 operation->alg = alg;
2227 operation->key_set = 0;
2228 operation->iv_set = 0;
2229 operation->iv_required = 0;
2230 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002231 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002232
2233#if defined(MBEDTLS_CMAC_C)
2234 if( alg == PSA_ALG_CMAC )
2235 {
2236 operation->iv_required = 0;
2237 mbedtls_cipher_init( &operation->ctx.cmac );
2238 status = PSA_SUCCESS;
2239 }
2240 else
2241#endif /* MBEDTLS_CMAC_C */
2242#if defined(MBEDTLS_MD_C)
2243 if( PSA_ALG_IS_HMAC( operation->alg ) )
2244 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002245 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2246 operation->ctx.hmac.hash_ctx.alg = 0;
2247 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002248 }
2249 else
2250#endif /* MBEDTLS_MD_C */
2251 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002252 if( ! PSA_ALG_IS_MAC( alg ) )
2253 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002254 }
2255
2256 if( status != PSA_SUCCESS )
2257 memset( operation, 0, sizeof( *operation ) );
2258 return( status );
2259}
2260
Gilles Peskine01126fa2018-07-12 17:04:55 +02002261#if defined(MBEDTLS_MD_C)
2262static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2263{
Gilles Peskine3f108122018-12-07 18:14:53 +01002264 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002265 return( psa_hash_abort( &hmac->hash_ctx ) );
2266}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002267
2268static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2269{
2270 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2271 memset( hmac, 0, sizeof( *hmac ) );
2272}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002273#endif /* MBEDTLS_MD_C */
2274
Gilles Peskine8c9def32018-02-08 10:02:12 +01002275psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2276{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002277 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002278 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002279 /* The object has (apparently) been initialized but it is not
2280 * in use. It's ok to call abort on such an object, and there's
2281 * nothing to do. */
2282 return( PSA_SUCCESS );
2283 }
2284 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002285#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002286 if( operation->alg == PSA_ALG_CMAC )
2287 {
2288 mbedtls_cipher_free( &operation->ctx.cmac );
2289 }
2290 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002291#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002292#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002293 if( PSA_ALG_IS_HMAC( operation->alg ) )
2294 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002295 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002296 }
2297 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002298#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002299 {
2300 /* Sanity check (shouldn't happen: operation->alg should
2301 * always have been initialized to a valid value). */
2302 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002303 }
Moran Peker41deec42018-04-04 15:43:05 +03002304
Gilles Peskine8c9def32018-02-08 10:02:12 +01002305 operation->alg = 0;
2306 operation->key_set = 0;
2307 operation->iv_set = 0;
2308 operation->iv_required = 0;
2309 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002310 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002311
Gilles Peskine8c9def32018-02-08 10:02:12 +01002312 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002313
2314bad_state:
2315 /* If abort is called on an uninitialized object, we can't trust
2316 * anything. Wipe the object in case it contains confidential data.
2317 * This may result in a memory leak if a pointer gets overwritten,
2318 * but it's too late to do anything about this. */
2319 memset( operation, 0, sizeof( *operation ) );
2320 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002321}
2322
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002323#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002324static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002325 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002326 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002327 const mbedtls_cipher_info_t *cipher_info )
2328{
2329 int ret;
2330
2331 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002332
2333 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2334 if( ret != 0 )
2335 return( ret );
2336
2337 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2338 slot->data.raw.data,
2339 key_bits );
2340 return( ret );
2341}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002342#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002343
Gilles Peskine248051a2018-06-20 16:09:38 +02002344#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002345static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2346 const uint8_t *key,
2347 size_t key_length,
2348 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002349{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002350 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002351 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002352 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002353 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002354 psa_status_t status;
2355
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002356 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2357 * overflow below. This should never trigger if the hash algorithm
2358 * is implemented correctly. */
2359 /* The size checks against the ipad and opad buffers cannot be written
2360 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2361 * because that triggers -Wlogical-op on GCC 7.3. */
2362 if( block_size > sizeof( ipad ) )
2363 return( PSA_ERROR_NOT_SUPPORTED );
2364 if( block_size > sizeof( hmac->opad ) )
2365 return( PSA_ERROR_NOT_SUPPORTED );
2366 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002367 return( PSA_ERROR_NOT_SUPPORTED );
2368
Gilles Peskined223b522018-06-11 18:12:58 +02002369 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002370 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002371 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002372 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002373 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002374 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002375 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002376 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002377 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002378 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002379 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002380 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002381 }
Gilles Peskine96889972018-07-12 17:07:03 +02002382 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2383 * but it is permitted. It is common when HMAC is used in HKDF, for
2384 * example. Don't call `memcpy` in the 0-length because `key` could be
2385 * an invalid pointer which would make the behavior undefined. */
2386 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002387 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002388
Gilles Peskined223b522018-06-11 18:12:58 +02002389 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2390 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002391 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002392 ipad[i] ^= 0x36;
2393 memset( ipad + key_length, 0x36, block_size - key_length );
2394
2395 /* Copy the key material from ipad to opad, flipping the requisite bits,
2396 * and filling the rest of opad with the requisite constant. */
2397 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002398 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2399 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002400
Gilles Peskine01126fa2018-07-12 17:04:55 +02002401 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002402 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002403 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002404
Gilles Peskine01126fa2018-07-12 17:04:55 +02002405 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002406
2407cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002408 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002409
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002410 return( status );
2411}
Gilles Peskine248051a2018-06-20 16:09:38 +02002412#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002413
Gilles Peskine89167cb2018-07-08 20:12:23 +02002414static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002415 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002416 psa_algorithm_t alg,
2417 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002419 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002420 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002421 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002422 psa_key_usage_t usage =
2423 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002424 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002425 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002427 /* A context must be freshly initialized before it can be set up. */
2428 if( operation->alg != 0 )
2429 {
2430 return( PSA_ERROR_BAD_STATE );
2431 }
2432
Gilles Peskined911eb72018-08-14 15:18:45 +02002433 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002434 if( status != PSA_SUCCESS )
2435 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002436 if( is_sign )
2437 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002438
Gilles Peskinec5487a82018-12-03 18:08:14 +01002439 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002440 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002441 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002442 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002443
Gilles Peskine8c9def32018-02-08 10:02:12 +01002444#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002445 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002446 {
2447 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002448 mbedtls_cipher_info_from_psa( full_length_alg,
2449 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002450 int ret;
2451 if( cipher_info == NULL )
2452 {
2453 status = PSA_ERROR_NOT_SUPPORTED;
2454 goto exit;
2455 }
2456 operation->mac_size = cipher_info->block_size;
2457 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2458 status = mbedtls_to_psa_error( ret );
2459 }
2460 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002461#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002462#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002463 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002464 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002465 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002466 if( hash_alg == 0 )
2467 {
2468 status = PSA_ERROR_NOT_SUPPORTED;
2469 goto exit;
2470 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002471
2472 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2473 /* Sanity check. This shouldn't fail on a valid configuration. */
2474 if( operation->mac_size == 0 ||
2475 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2476 {
2477 status = PSA_ERROR_NOT_SUPPORTED;
2478 goto exit;
2479 }
2480
Gilles Peskine01126fa2018-07-12 17:04:55 +02002481 if( slot->type != PSA_KEY_TYPE_HMAC )
2482 {
2483 status = PSA_ERROR_INVALID_ARGUMENT;
2484 goto exit;
2485 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002486
Gilles Peskine01126fa2018-07-12 17:04:55 +02002487 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2488 slot->data.raw.data,
2489 slot->data.raw.bytes,
2490 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002491 }
2492 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002494 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002495 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002496 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002498
Gilles Peskined911eb72018-08-14 15:18:45 +02002499 if( truncated == 0 )
2500 {
2501 /* The "normal" case: untruncated algorithm. Nothing to do. */
2502 }
2503 else if( truncated < 4 )
2504 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002505 /* A very short MAC is too short for security since it can be
2506 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2507 * so we make this our minimum, even though 32 bits is still
2508 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002509 status = PSA_ERROR_NOT_SUPPORTED;
2510 }
2511 else if( truncated > operation->mac_size )
2512 {
2513 /* It's impossible to "truncate" to a larger length. */
2514 status = PSA_ERROR_INVALID_ARGUMENT;
2515 }
2516 else
2517 operation->mac_size = truncated;
2518
Gilles Peskinefbfac682018-07-08 20:51:54 +02002519exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002520 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002521 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002522 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002523 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002524 else
2525 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002526 operation->key_set = 1;
2527 }
2528 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529}
2530
Gilles Peskine89167cb2018-07-08 20:12:23 +02002531psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002532 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002533 psa_algorithm_t alg )
2534{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002535 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002536}
2537
2538psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002539 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002540 psa_algorithm_t alg )
2541{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002542 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002543}
2544
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2546 const uint8_t *input,
2547 size_t input_length )
2548{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002549 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002551 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002553 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002554 operation->has_input = 1;
2555
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002557 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002558 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002559 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2560 input, input_length );
2561 status = mbedtls_to_psa_error( ret );
2562 }
2563 else
2564#endif /* MBEDTLS_CMAC_C */
2565#if defined(MBEDTLS_MD_C)
2566 if( PSA_ALG_IS_HMAC( operation->alg ) )
2567 {
2568 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2569 input_length );
2570 }
2571 else
2572#endif /* MBEDTLS_MD_C */
2573 {
2574 /* This shouldn't happen if `operation` was initialized by
2575 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002576 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002577 }
2578
Gilles Peskinefbfac682018-07-08 20:51:54 +02002579 if( status != PSA_SUCCESS )
2580 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002581 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582}
2583
Gilles Peskine01126fa2018-07-12 17:04:55 +02002584#if defined(MBEDTLS_MD_C)
2585static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2586 uint8_t *mac,
2587 size_t mac_size )
2588{
2589 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2590 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2591 size_t hash_size = 0;
2592 size_t block_size = psa_get_hash_block_size( hash_alg );
2593 psa_status_t status;
2594
Gilles Peskine01126fa2018-07-12 17:04:55 +02002595 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2596 if( status != PSA_SUCCESS )
2597 return( status );
2598 /* From here on, tmp needs to be wiped. */
2599
2600 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2601 if( status != PSA_SUCCESS )
2602 goto exit;
2603
2604 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2605 if( status != PSA_SUCCESS )
2606 goto exit;
2607
2608 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2609 if( status != PSA_SUCCESS )
2610 goto exit;
2611
Gilles Peskined911eb72018-08-14 15:18:45 +02002612 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2613 if( status != PSA_SUCCESS )
2614 goto exit;
2615
2616 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002617
2618exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002619 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002620 return( status );
2621}
2622#endif /* MBEDTLS_MD_C */
2623
mohammad16036df908f2018-04-02 08:34:15 -07002624static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002625 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002626 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002627{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002628 if( ! operation->key_set )
2629 return( PSA_ERROR_BAD_STATE );
2630 if( operation->iv_required && ! operation->iv_set )
2631 return( PSA_ERROR_BAD_STATE );
2632
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633 if( mac_size < operation->mac_size )
2634 return( PSA_ERROR_BUFFER_TOO_SMALL );
2635
Gilles Peskine8c9def32018-02-08 10:02:12 +01002636#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002637 if( operation->alg == PSA_ALG_CMAC )
2638 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002639 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2640 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2641 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002642 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002643 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002644 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002645 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002646 else
2647#endif /* MBEDTLS_CMAC_C */
2648#if defined(MBEDTLS_MD_C)
2649 if( PSA_ALG_IS_HMAC( operation->alg ) )
2650 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002651 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002652 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002653 }
2654 else
2655#endif /* MBEDTLS_MD_C */
2656 {
2657 /* This shouldn't happen if `operation` was initialized by
2658 * a setup function. */
2659 return( PSA_ERROR_BAD_STATE );
2660 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002661}
2662
Gilles Peskineacd4be32018-07-08 19:56:25 +02002663psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2664 uint8_t *mac,
2665 size_t mac_size,
2666 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002667{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002668 psa_status_t status;
2669
Jaeden Amero252ef282019-02-15 14:05:35 +00002670 if( operation->alg == 0 )
2671 {
2672 return( PSA_ERROR_BAD_STATE );
2673 }
2674
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002675 /* Fill the output buffer with something that isn't a valid mac
2676 * (barring an attack on the mac and deliberately-crafted input),
2677 * in case the caller doesn't check the return status properly. */
2678 *mac_length = mac_size;
2679 /* If mac_size is 0 then mac may be NULL and then the
2680 * call to memset would have undefined behavior. */
2681 if( mac_size != 0 )
2682 memset( mac, '!', mac_size );
2683
Gilles Peskine89167cb2018-07-08 20:12:23 +02002684 if( ! operation->is_sign )
2685 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002686 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002687 }
mohammad16036df908f2018-04-02 08:34:15 -07002688
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002689 status = psa_mac_finish_internal( operation, mac, mac_size );
2690
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002691 if( status == PSA_SUCCESS )
2692 {
2693 status = psa_mac_abort( operation );
2694 if( status == PSA_SUCCESS )
2695 *mac_length = operation->mac_size;
2696 else
2697 memset( mac, '!', mac_size );
2698 }
2699 else
2700 psa_mac_abort( operation );
2701 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002702}
2703
Gilles Peskineacd4be32018-07-08 19:56:25 +02002704psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2705 const uint8_t *mac,
2706 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707{
Gilles Peskine828ed142018-06-18 23:25:51 +02002708 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002709 psa_status_t status;
2710
Jaeden Amero252ef282019-02-15 14:05:35 +00002711 if( operation->alg == 0 )
2712 {
2713 return( PSA_ERROR_BAD_STATE );
2714 }
2715
Gilles Peskine89167cb2018-07-08 20:12:23 +02002716 if( operation->is_sign )
2717 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002718 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002719 }
2720 if( operation->mac_size != mac_length )
2721 {
2722 status = PSA_ERROR_INVALID_SIGNATURE;
2723 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002724 }
mohammad16036df908f2018-04-02 08:34:15 -07002725
2726 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002727 actual_mac, sizeof( actual_mac ) );
2728
2729 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2730 status = PSA_ERROR_INVALID_SIGNATURE;
2731
2732cleanup:
2733 if( status == PSA_SUCCESS )
2734 status = psa_mac_abort( operation );
2735 else
2736 psa_mac_abort( operation );
2737
Gilles Peskine3f108122018-12-07 18:14:53 +01002738 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002739
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002740 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741}
2742
2743
Gilles Peskine20035e32018-02-03 22:44:14 +01002744
Gilles Peskine20035e32018-02-03 22:44:14 +01002745/****************************************************************/
2746/* Asymmetric cryptography */
2747/****************************************************************/
2748
Gilles Peskine2b450e32018-06-27 15:42:46 +02002749#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002750/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002751 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002752static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2753 size_t hash_length,
2754 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002755{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002756 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002757 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002758 *md_alg = mbedtls_md_get_type( md_info );
2759
2760 /* The Mbed TLS RSA module uses an unsigned int for hash length
2761 * parameters. Validate that it fits so that we don't risk an
2762 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002763#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002764 if( hash_length > UINT_MAX )
2765 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002766#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002767
2768#if defined(MBEDTLS_PKCS1_V15)
2769 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2770 * must be correct. */
2771 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2772 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002773 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002774 if( md_info == NULL )
2775 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002776 if( mbedtls_md_get_size( md_info ) != hash_length )
2777 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002778 }
2779#endif /* MBEDTLS_PKCS1_V15 */
2780
2781#if defined(MBEDTLS_PKCS1_V21)
2782 /* PSS requires a hash internally. */
2783 if( PSA_ALG_IS_RSA_PSS( alg ) )
2784 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002785 if( md_info == NULL )
2786 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002787 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002788#endif /* MBEDTLS_PKCS1_V21 */
2789
Gilles Peskine61b91d42018-06-08 16:09:36 +02002790 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002791}
2792
Gilles Peskine2b450e32018-06-27 15:42:46 +02002793static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2794 psa_algorithm_t alg,
2795 const uint8_t *hash,
2796 size_t hash_length,
2797 uint8_t *signature,
2798 size_t signature_size,
2799 size_t *signature_length )
2800{
2801 psa_status_t status;
2802 int ret;
2803 mbedtls_md_type_t md_alg;
2804
2805 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2806 if( status != PSA_SUCCESS )
2807 return( status );
2808
Gilles Peskine630a18a2018-06-29 17:49:35 +02002809 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002810 return( PSA_ERROR_BUFFER_TOO_SMALL );
2811
2812#if defined(MBEDTLS_PKCS1_V15)
2813 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2814 {
2815 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2816 MBEDTLS_MD_NONE );
2817 ret = mbedtls_rsa_pkcs1_sign( rsa,
2818 mbedtls_ctr_drbg_random,
2819 &global_data.ctr_drbg,
2820 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002821 md_alg,
2822 (unsigned int) hash_length,
2823 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002824 signature );
2825 }
2826 else
2827#endif /* MBEDTLS_PKCS1_V15 */
2828#if defined(MBEDTLS_PKCS1_V21)
2829 if( PSA_ALG_IS_RSA_PSS( alg ) )
2830 {
2831 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2832 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2833 mbedtls_ctr_drbg_random,
2834 &global_data.ctr_drbg,
2835 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002836 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002837 (unsigned int) hash_length,
2838 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002839 signature );
2840 }
2841 else
2842#endif /* MBEDTLS_PKCS1_V21 */
2843 {
2844 return( PSA_ERROR_INVALID_ARGUMENT );
2845 }
2846
2847 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002848 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002849 return( mbedtls_to_psa_error( ret ) );
2850}
2851
2852static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2853 psa_algorithm_t alg,
2854 const uint8_t *hash,
2855 size_t hash_length,
2856 const uint8_t *signature,
2857 size_t signature_length )
2858{
2859 psa_status_t status;
2860 int ret;
2861 mbedtls_md_type_t md_alg;
2862
2863 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2864 if( status != PSA_SUCCESS )
2865 return( status );
2866
Gilles Peskine630a18a2018-06-29 17:49:35 +02002867 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002868 return( PSA_ERROR_BUFFER_TOO_SMALL );
2869
2870#if defined(MBEDTLS_PKCS1_V15)
2871 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2872 {
2873 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2874 MBEDTLS_MD_NONE );
2875 ret = mbedtls_rsa_pkcs1_verify( rsa,
2876 mbedtls_ctr_drbg_random,
2877 &global_data.ctr_drbg,
2878 MBEDTLS_RSA_PUBLIC,
2879 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002880 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002881 hash,
2882 signature );
2883 }
2884 else
2885#endif /* MBEDTLS_PKCS1_V15 */
2886#if defined(MBEDTLS_PKCS1_V21)
2887 if( PSA_ALG_IS_RSA_PSS( alg ) )
2888 {
2889 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2890 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2891 mbedtls_ctr_drbg_random,
2892 &global_data.ctr_drbg,
2893 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002894 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002895 (unsigned int) hash_length,
2896 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002897 signature );
2898 }
2899 else
2900#endif /* MBEDTLS_PKCS1_V21 */
2901 {
2902 return( PSA_ERROR_INVALID_ARGUMENT );
2903 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002904
2905 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2906 * the rest of the signature is invalid". This has little use in
2907 * practice and PSA doesn't report this distinction. */
2908 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2909 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002910 return( mbedtls_to_psa_error( ret ) );
2911}
2912#endif /* MBEDTLS_RSA_C */
2913
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002914#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002915/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2916 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2917 * (even though these functions don't modify it). */
2918static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2919 psa_algorithm_t alg,
2920 const uint8_t *hash,
2921 size_t hash_length,
2922 uint8_t *signature,
2923 size_t signature_size,
2924 size_t *signature_length )
2925{
2926 int ret;
2927 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002928 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002929 mbedtls_mpi_init( &r );
2930 mbedtls_mpi_init( &s );
2931
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002932 if( signature_size < 2 * curve_bytes )
2933 {
2934 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2935 goto cleanup;
2936 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002937
Gilles Peskinea05219c2018-11-16 16:02:56 +01002938#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002939 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2940 {
2941 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2942 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2943 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2944 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2945 hash, hash_length,
2946 md_alg ) );
2947 }
2948 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002949#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002950 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002951 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002952 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2953 hash, hash_length,
2954 mbedtls_ctr_drbg_random,
2955 &global_data.ctr_drbg ) );
2956 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002957
2958 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2959 signature,
2960 curve_bytes ) );
2961 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2962 signature + curve_bytes,
2963 curve_bytes ) );
2964
2965cleanup:
2966 mbedtls_mpi_free( &r );
2967 mbedtls_mpi_free( &s );
2968 if( ret == 0 )
2969 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002970 return( mbedtls_to_psa_error( ret ) );
2971}
2972
2973static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2974 const uint8_t *hash,
2975 size_t hash_length,
2976 const uint8_t *signature,
2977 size_t signature_length )
2978{
2979 int ret;
2980 mbedtls_mpi r, s;
2981 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2982 mbedtls_mpi_init( &r );
2983 mbedtls_mpi_init( &s );
2984
2985 if( signature_length != 2 * curve_bytes )
2986 return( PSA_ERROR_INVALID_SIGNATURE );
2987
2988 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2989 signature,
2990 curve_bytes ) );
2991 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2992 signature + curve_bytes,
2993 curve_bytes ) );
2994
2995 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2996 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002997
2998cleanup:
2999 mbedtls_mpi_free( &r );
3000 mbedtls_mpi_free( &s );
3001 return( mbedtls_to_psa_error( ret ) );
3002}
3003#endif /* MBEDTLS_ECDSA_C */
3004
Gilles Peskinec5487a82018-12-03 18:08:14 +01003005psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003006 psa_algorithm_t alg,
3007 const uint8_t *hash,
3008 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003009 uint8_t *signature,
3010 size_t signature_size,
3011 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003012{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003013 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003014 psa_status_t status;
3015
3016 *signature_length = signature_size;
3017
Gilles Peskinec5487a82018-12-03 18:08:14 +01003018 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003019 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003020 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003021 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003022 {
3023 status = PSA_ERROR_INVALID_ARGUMENT;
3024 goto exit;
3025 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003026
Gilles Peskine20035e32018-02-03 22:44:14 +01003027#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003028 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003029 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003030 status = psa_rsa_sign( slot->data.rsa,
3031 alg,
3032 hash, hash_length,
3033 signature, signature_size,
3034 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003035 }
3036 else
3037#endif /* defined(MBEDTLS_RSA_C) */
3038#if defined(MBEDTLS_ECP_C)
3039 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3040 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003041#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003042 if(
3043#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3044 PSA_ALG_IS_ECDSA( alg )
3045#else
3046 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3047#endif
3048 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003049 status = psa_ecdsa_sign( slot->data.ecp,
3050 alg,
3051 hash, hash_length,
3052 signature, signature_size,
3053 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003054 else
3055#endif /* defined(MBEDTLS_ECDSA_C) */
3056 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003057 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003058 }
itayzafrir5c753392018-05-08 11:18:38 +03003059 }
3060 else
3061#endif /* defined(MBEDTLS_ECP_C) */
3062 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003063 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003064 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003065
3066exit:
3067 /* Fill the unused part of the output buffer (the whole buffer on error,
3068 * the trailing part on success) with something that isn't a valid mac
3069 * (barring an attack on the mac and deliberately-crafted input),
3070 * in case the caller doesn't check the return status properly. */
3071 if( status == PSA_SUCCESS )
3072 memset( signature + *signature_length, '!',
3073 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003074 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003075 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003076 /* If signature_size is 0 then we have nothing to do. We must not call
3077 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003078 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003079}
3080
Gilles Peskinec5487a82018-12-03 18:08:14 +01003081psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003082 psa_algorithm_t alg,
3083 const uint8_t *hash,
3084 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003085 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003086 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003087{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003088 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003089 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003090
Gilles Peskinec5487a82018-12-03 18:08:14 +01003091 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003092 if( status != PSA_SUCCESS )
3093 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003094
Gilles Peskine61b91d42018-06-08 16:09:36 +02003095#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003096 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003097 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003098 return( psa_rsa_verify( slot->data.rsa,
3099 alg,
3100 hash, hash_length,
3101 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003102 }
3103 else
3104#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003105#if defined(MBEDTLS_ECP_C)
3106 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3107 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003108#if defined(MBEDTLS_ECDSA_C)
3109 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003110 return( psa_ecdsa_verify( slot->data.ecp,
3111 hash, hash_length,
3112 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003113 else
3114#endif /* defined(MBEDTLS_ECDSA_C) */
3115 {
3116 return( PSA_ERROR_INVALID_ARGUMENT );
3117 }
itayzafrir5c753392018-05-08 11:18:38 +03003118 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003119 else
3120#endif /* defined(MBEDTLS_ECP_C) */
3121 {
3122 return( PSA_ERROR_NOT_SUPPORTED );
3123 }
3124}
3125
Gilles Peskine072ac562018-06-30 00:21:29 +02003126#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3127static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3128 mbedtls_rsa_context *rsa )
3129{
3130 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3131 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3132 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3133 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3134}
3135#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3136
Gilles Peskinec5487a82018-12-03 18:08:14 +01003137psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003138 psa_algorithm_t alg,
3139 const uint8_t *input,
3140 size_t input_length,
3141 const uint8_t *salt,
3142 size_t salt_length,
3143 uint8_t *output,
3144 size_t output_size,
3145 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003146{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003147 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003148 psa_status_t status;
3149
Darryl Green5cc689a2018-07-24 15:34:10 +01003150 (void) input;
3151 (void) input_length;
3152 (void) salt;
3153 (void) output;
3154 (void) output_size;
3155
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003156 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003157
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003158 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3159 return( PSA_ERROR_INVALID_ARGUMENT );
3160
Gilles Peskinec5487a82018-12-03 18:08:14 +01003161 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003162 if( status != PSA_SUCCESS )
3163 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003164 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003165 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003167
3168#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003169 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003170 {
3171 mbedtls_rsa_context *rsa = slot->data.rsa;
3172 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003173 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003174 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003175#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003176 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003178 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3179 mbedtls_ctr_drbg_random,
3180 &global_data.ctr_drbg,
3181 MBEDTLS_RSA_PUBLIC,
3182 input_length,
3183 input,
3184 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003185 }
3186 else
3187#endif /* MBEDTLS_PKCS1_V15 */
3188#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003189 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003191 psa_rsa_oaep_set_padding_mode( alg, rsa );
3192 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3193 mbedtls_ctr_drbg_random,
3194 &global_data.ctr_drbg,
3195 MBEDTLS_RSA_PUBLIC,
3196 salt, salt_length,
3197 input_length,
3198 input,
3199 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003200 }
3201 else
3202#endif /* MBEDTLS_PKCS1_V21 */
3203 {
3204 return( PSA_ERROR_INVALID_ARGUMENT );
3205 }
3206 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003207 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003208 return( mbedtls_to_psa_error( ret ) );
3209 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003210 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003211#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212 {
3213 return( PSA_ERROR_NOT_SUPPORTED );
3214 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003215}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003216
Gilles Peskinec5487a82018-12-03 18:08:14 +01003217psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003218 psa_algorithm_t alg,
3219 const uint8_t *input,
3220 size_t input_length,
3221 const uint8_t *salt,
3222 size_t salt_length,
3223 uint8_t *output,
3224 size_t output_size,
3225 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003227 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003228 psa_status_t status;
3229
Darryl Green5cc689a2018-07-24 15:34:10 +01003230 (void) input;
3231 (void) input_length;
3232 (void) salt;
3233 (void) output;
3234 (void) output_size;
3235
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003236 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003238 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3239 return( PSA_ERROR_INVALID_ARGUMENT );
3240
Gilles Peskinec5487a82018-12-03 18:08:14 +01003241 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003242 if( status != PSA_SUCCESS )
3243 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003244 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003245 return( PSA_ERROR_INVALID_ARGUMENT );
3246
3247#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003248 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003249 {
3250 mbedtls_rsa_context *rsa = slot->data.rsa;
3251 int ret;
3252
Gilles Peskine630a18a2018-06-29 17:49:35 +02003253 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003254 return( PSA_ERROR_INVALID_ARGUMENT );
3255
3256#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003257 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003258 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003259 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3260 mbedtls_ctr_drbg_random,
3261 &global_data.ctr_drbg,
3262 MBEDTLS_RSA_PRIVATE,
3263 output_length,
3264 input,
3265 output,
3266 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267 }
3268 else
3269#endif /* MBEDTLS_PKCS1_V15 */
3270#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003271 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003272 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003273 psa_rsa_oaep_set_padding_mode( alg, rsa );
3274 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3275 mbedtls_ctr_drbg_random,
3276 &global_data.ctr_drbg,
3277 MBEDTLS_RSA_PRIVATE,
3278 salt, salt_length,
3279 output_length,
3280 input,
3281 output,
3282 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003283 }
3284 else
3285#endif /* MBEDTLS_PKCS1_V21 */
3286 {
3287 return( PSA_ERROR_INVALID_ARGUMENT );
3288 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003289
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003290 return( mbedtls_to_psa_error( ret ) );
3291 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003292 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003293#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003294 {
3295 return( PSA_ERROR_NOT_SUPPORTED );
3296 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003297}
Gilles Peskine20035e32018-02-03 22:44:14 +01003298
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003299
3300
mohammad1603503973b2018-03-12 15:59:30 +02003301/****************************************************************/
3302/* Symmetric cryptography */
3303/****************************************************************/
3304
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003305/* Initialize the cipher operation structure. Once this function has been
3306 * called, psa_cipher_abort can run and will do the right thing. */
3307static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3308 psa_algorithm_t alg )
3309{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003310 if( ! PSA_ALG_IS_CIPHER( alg ) )
3311 {
3312 memset( operation, 0, sizeof( *operation ) );
3313 return( PSA_ERROR_INVALID_ARGUMENT );
3314 }
3315
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003316 operation->alg = alg;
3317 operation->key_set = 0;
3318 operation->iv_set = 0;
3319 operation->iv_required = 1;
3320 operation->iv_size = 0;
3321 operation->block_size = 0;
3322 mbedtls_cipher_init( &operation->ctx.cipher );
3323 return( PSA_SUCCESS );
3324}
3325
Gilles Peskinee553c652018-06-04 16:22:46 +02003326static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003327 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003328 psa_algorithm_t alg,
3329 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003330{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003331 int ret = 0;
3332 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003333 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003334 size_t key_bits;
3335 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003336 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3337 PSA_KEY_USAGE_ENCRYPT :
3338 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003339
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003340 /* A context must be freshly initialized before it can be set up. */
3341 if( operation->alg != 0 )
3342 {
3343 return( PSA_ERROR_BAD_STATE );
3344 }
3345
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003346 status = psa_cipher_init( operation, alg );
3347 if( status != PSA_SUCCESS )
3348 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003349
Gilles Peskinec5487a82018-12-03 18:08:14 +01003350 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003351 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003352 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003353 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003354
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003355 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003356 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003357 {
3358 status = PSA_ERROR_NOT_SUPPORTED;
3359 goto exit;
3360 }
mohammad1603503973b2018-03-12 15:59:30 +02003361
mohammad1603503973b2018-03-12 15:59:30 +02003362 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003363 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003364 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003365
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003366#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003367 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003368 {
3369 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3370 unsigned char keys[24];
3371 memcpy( keys, slot->data.raw.data, 16 );
3372 memcpy( keys + 16, slot->data.raw.data, 8 );
3373 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3374 keys,
3375 192, cipher_operation );
3376 }
3377 else
3378#endif
3379 {
3380 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3381 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003382 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003383 }
Moran Peker41deec42018-04-04 15:43:05 +03003384 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003385 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003386
mohammad16038481e742018-03-18 13:57:31 +02003387#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003388 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003389 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003390 case PSA_ALG_CBC_NO_PADDING:
3391 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3392 MBEDTLS_PADDING_NONE );
3393 break;
3394 case PSA_ALG_CBC_PKCS7:
3395 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3396 MBEDTLS_PADDING_PKCS7 );
3397 break;
3398 default:
3399 /* The algorithm doesn't involve padding. */
3400 ret = 0;
3401 break;
3402 }
3403 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003404 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003405#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3406
mohammad1603503973b2018-03-12 15:59:30 +02003407 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003408 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3409 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3410 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003411 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003412 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003413 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003414#if defined(MBEDTLS_CHACHA20_C)
3415 else
3416 if( alg == PSA_ALG_CHACHA20 )
3417 operation->iv_size = 12;
3418#endif
mohammad1603503973b2018-03-12 15:59:30 +02003419
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003420exit:
3421 if( status == 0 )
3422 status = mbedtls_to_psa_error( ret );
3423 if( status != 0 )
3424 psa_cipher_abort( operation );
3425 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003426}
3427
Gilles Peskinefe119512018-07-08 21:39:34 +02003428psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003429 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003430 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003431{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003432 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003433}
3434
Gilles Peskinefe119512018-07-08 21:39:34 +02003435psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003436 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003437 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003438{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003439 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003440}
3441
Gilles Peskinefe119512018-07-08 21:39:34 +02003442psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3443 unsigned char *iv,
3444 size_t iv_size,
3445 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003446{
itayzafrir534bd7c2018-08-02 13:56:32 +03003447 psa_status_t status;
3448 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003449 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003450 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003451 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003452 }
Moran Peker41deec42018-04-04 15:43:05 +03003453 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003454 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003455 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003456 goto exit;
3457 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003458 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3459 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003460 if( ret != 0 )
3461 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003462 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003463 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003464 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003465
mohammad16038481e742018-03-18 13:57:31 +02003466 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003467 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003468
Moran Peker395db872018-05-31 14:07:14 +03003469exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003470 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003471 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003472 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003473}
3474
Gilles Peskinefe119512018-07-08 21:39:34 +02003475psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3476 const unsigned char *iv,
3477 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003478{
itayzafrir534bd7c2018-08-02 13:56:32 +03003479 psa_status_t status;
3480 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003481 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003482 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003483 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003484 }
Moran Pekera28258c2018-05-29 16:25:04 +03003485 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003486 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003487 status = PSA_ERROR_INVALID_ARGUMENT;
3488 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003489 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003490 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3491 status = mbedtls_to_psa_error( ret );
3492exit:
3493 if( status == PSA_SUCCESS )
3494 operation->iv_set = 1;
3495 else
mohammad1603503973b2018-03-12 15:59:30 +02003496 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003497 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003498}
3499
Gilles Peskinee553c652018-06-04 16:22:46 +02003500psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3501 const uint8_t *input,
3502 size_t input_length,
3503 unsigned char *output,
3504 size_t output_size,
3505 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003506{
itayzafrir534bd7c2018-08-02 13:56:32 +03003507 psa_status_t status;
3508 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003509 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003510
3511 if( operation->alg == 0 )
3512 {
3513 return( PSA_ERROR_BAD_STATE );
3514 }
3515
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003516 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003517 {
3518 /* Take the unprocessed partial block left over from previous
3519 * update calls, if any, plus the input to this call. Remove
3520 * the last partial block, if any. You get the data that will be
3521 * output in this call. */
3522 expected_output_size =
3523 ( operation->ctx.cipher.unprocessed_len + input_length )
3524 / operation->block_size * operation->block_size;
3525 }
3526 else
3527 {
3528 expected_output_size = input_length;
3529 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003530
Gilles Peskine89d789c2018-06-04 17:17:16 +02003531 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003532 {
3533 status = PSA_ERROR_BUFFER_TOO_SMALL;
3534 goto exit;
3535 }
mohammad160382759612018-03-12 18:16:40 +02003536
mohammad1603503973b2018-03-12 15:59:30 +02003537 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003538 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003539 status = mbedtls_to_psa_error( ret );
3540exit:
3541 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003542 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003543 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003544}
3545
Gilles Peskinee553c652018-06-04 16:22:46 +02003546psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3547 uint8_t *output,
3548 size_t output_size,
3549 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003550{
David Saadab4ecc272019-02-14 13:48:10 +02003551 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003552 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003553 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003554
mohammad1603503973b2018-03-12 15:59:30 +02003555 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003556 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003557 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003558 }
3559 if( operation->iv_required && ! operation->iv_set )
3560 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003561 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003562 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003563
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003564 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003565 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3566 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003567 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003568 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003569 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003570 }
3571
Janos Follath315b51c2018-07-09 16:04:51 +01003572 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3573 temp_output_buffer,
3574 output_length );
3575 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003576 {
Janos Follath315b51c2018-07-09 16:04:51 +01003577 status = mbedtls_to_psa_error( cipher_ret );
3578 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003579 }
Janos Follath315b51c2018-07-09 16:04:51 +01003580
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003581 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003582 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003583 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003584 memcpy( output, temp_output_buffer, *output_length );
3585 else
3586 {
Janos Follath315b51c2018-07-09 16:04:51 +01003587 status = PSA_ERROR_BUFFER_TOO_SMALL;
3588 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003589 }
mohammad1603503973b2018-03-12 15:59:30 +02003590
Gilles Peskine3f108122018-12-07 18:14:53 +01003591 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003592 status = psa_cipher_abort( operation );
3593
3594 return( status );
3595
3596error:
3597
3598 *output_length = 0;
3599
Gilles Peskine3f108122018-12-07 18:14:53 +01003600 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003601 (void) psa_cipher_abort( operation );
3602
3603 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003604}
3605
Gilles Peskinee553c652018-06-04 16:22:46 +02003606psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3607{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003608 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003609 {
3610 /* The object has (apparently) been initialized but it is not
3611 * in use. It's ok to call abort on such an object, and there's
3612 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003613 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003614 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003615
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003616 /* Sanity check (shouldn't happen: operation->alg should
3617 * always have been initialized to a valid value). */
3618 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3619 return( PSA_ERROR_BAD_STATE );
3620
mohammad1603503973b2018-03-12 15:59:30 +02003621 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003622
Moran Peker41deec42018-04-04 15:43:05 +03003623 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003624 operation->key_set = 0;
3625 operation->iv_set = 0;
3626 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003627 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003628 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003629
Moran Peker395db872018-05-31 14:07:14 +03003630 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003631}
3632
Gilles Peskinea0655c32018-04-30 17:06:50 +02003633
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003634
mohammad16038cc1cee2018-03-28 01:21:33 +03003635/****************************************************************/
3636/* Key Policy */
3637/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003638
mohammad160327010052018-07-03 13:16:15 +03003639#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003640void psa_key_policy_set_usage( psa_key_policy_t *policy,
3641 psa_key_usage_t usage,
3642 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003643{
mohammad16034eed7572018-03-28 05:14:59 -07003644 policy->usage = usage;
3645 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003646}
3647
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003648psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003649{
mohammad16036df908f2018-04-02 08:34:15 -07003650 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003651}
3652
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003653psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003654{
mohammad16036df908f2018-04-02 08:34:15 -07003655 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003656}
mohammad160327010052018-07-03 13:16:15 +03003657#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003658
Gilles Peskinec5487a82018-12-03 18:08:14 +01003659psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003660 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003661{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003662 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003663 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003664
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003665 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003666 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003667
Gilles Peskinec5487a82018-12-03 18:08:14 +01003668 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003669 if( status != PSA_SUCCESS )
3670 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003671
Gilles Peskine4747d192019-04-17 15:05:45 +02003672 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003673}
3674
Gilles Peskinec5487a82018-12-03 18:08:14 +01003675psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003676 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003677{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003678 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003679 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003680
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003681 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003682 return( PSA_ERROR_INVALID_ARGUMENT );
3683
Gilles Peskinec5487a82018-12-03 18:08:14 +01003684 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003685 if( status != PSA_SUCCESS )
3686 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003687
mohammad16036df908f2018-04-02 08:34:15 -07003688 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003689
3690 return( PSA_SUCCESS );
3691}
Gilles Peskine20035e32018-02-03 22:44:14 +01003692
Gilles Peskinea0655c32018-04-30 17:06:50 +02003693
3694
mohammad1603804cd712018-03-20 22:44:08 +02003695/****************************************************************/
3696/* Key Lifetime */
3697/****************************************************************/
3698
Gilles Peskine87a5e562019-04-17 12:28:25 +02003699psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003700 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003701{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003702 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003703 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003704
Gilles Peskinec5487a82018-12-03 18:08:14 +01003705 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003706 if( status != PSA_SUCCESS )
3707 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003708
mohammad1603804cd712018-03-20 22:44:08 +02003709 *lifetime = slot->lifetime;
3710
3711 return( PSA_SUCCESS );
3712}
3713
Gilles Peskine20035e32018-02-03 22:44:14 +01003714
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003715
mohammad16035955c982018-04-26 00:53:03 +03003716/****************************************************************/
3717/* AEAD */
3718/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003719
Gilles Peskineedf9a652018-08-17 18:11:56 +02003720typedef struct
3721{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003722 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003723 const mbedtls_cipher_info_t *cipher_info;
3724 union
3725 {
3726#if defined(MBEDTLS_CCM_C)
3727 mbedtls_ccm_context ccm;
3728#endif /* MBEDTLS_CCM_C */
3729#if defined(MBEDTLS_GCM_C)
3730 mbedtls_gcm_context gcm;
3731#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003732#if defined(MBEDTLS_CHACHAPOLY_C)
3733 mbedtls_chachapoly_context chachapoly;
3734#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003735 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003736 psa_algorithm_t core_alg;
3737 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003738 uint8_t tag_length;
3739} aead_operation_t;
3740
Gilles Peskine30a9e412019-01-14 18:36:12 +01003741static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003742{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003743 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003744 {
3745#if defined(MBEDTLS_CCM_C)
3746 case PSA_ALG_CCM:
3747 mbedtls_ccm_free( &operation->ctx.ccm );
3748 break;
3749#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003750#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003751 case PSA_ALG_GCM:
3752 mbedtls_gcm_free( &operation->ctx.gcm );
3753 break;
3754#endif /* MBEDTLS_GCM_C */
3755 }
3756}
3757
3758static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003759 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003760 psa_key_usage_t usage,
3761 psa_algorithm_t alg )
3762{
3763 psa_status_t status;
3764 size_t key_bits;
3765 mbedtls_cipher_id_t cipher_id;
3766
Gilles Peskinec5487a82018-12-03 18:08:14 +01003767 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003768 if( status != PSA_SUCCESS )
3769 return( status );
3770
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003771 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003772
3773 operation->cipher_info =
3774 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3775 &cipher_id );
3776 if( operation->cipher_info == NULL )
3777 return( PSA_ERROR_NOT_SUPPORTED );
3778
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003779 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003780 {
3781#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003782 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3783 operation->core_alg = PSA_ALG_CCM;
3784 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003785 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3786 * The call to mbedtls_ccm_encrypt_and_tag or
3787 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003788 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3789 return( PSA_ERROR_INVALID_ARGUMENT );
3790 mbedtls_ccm_init( &operation->ctx.ccm );
3791 status = mbedtls_to_psa_error(
3792 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3793 operation->slot->data.raw.data,
3794 (unsigned int) key_bits ) );
3795 if( status != 0 )
3796 goto cleanup;
3797 break;
3798#endif /* MBEDTLS_CCM_C */
3799
3800#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003801 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3802 operation->core_alg = PSA_ALG_GCM;
3803 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003804 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3805 * The call to mbedtls_gcm_crypt_and_tag or
3806 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003807 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3808 return( PSA_ERROR_INVALID_ARGUMENT );
3809 mbedtls_gcm_init( &operation->ctx.gcm );
3810 status = mbedtls_to_psa_error(
3811 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3812 operation->slot->data.raw.data,
3813 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003814 if( status != 0 )
3815 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003816 break;
3817#endif /* MBEDTLS_GCM_C */
3818
Gilles Peskine26869f22019-05-06 15:25:00 +02003819#if defined(MBEDTLS_CHACHAPOLY_C)
3820 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3821 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3822 operation->full_tag_length = 16;
3823 /* We only support the default tag length. */
3824 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3825 return( PSA_ERROR_NOT_SUPPORTED );
3826 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3827 status = mbedtls_to_psa_error(
3828 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3829 operation->slot->data.raw.data ) );
3830 if( status != 0 )
3831 goto cleanup;
3832 break;
3833#endif /* MBEDTLS_CHACHAPOLY_C */
3834
Gilles Peskineedf9a652018-08-17 18:11:56 +02003835 default:
3836 return( PSA_ERROR_NOT_SUPPORTED );
3837 }
3838
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003839 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3840 {
3841 status = PSA_ERROR_INVALID_ARGUMENT;
3842 goto cleanup;
3843 }
3844 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003845
Gilles Peskineedf9a652018-08-17 18:11:56 +02003846 return( PSA_SUCCESS );
3847
3848cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003849 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003850 return( status );
3851}
3852
Gilles Peskinec5487a82018-12-03 18:08:14 +01003853psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003854 psa_algorithm_t alg,
3855 const uint8_t *nonce,
3856 size_t nonce_length,
3857 const uint8_t *additional_data,
3858 size_t additional_data_length,
3859 const uint8_t *plaintext,
3860 size_t plaintext_length,
3861 uint8_t *ciphertext,
3862 size_t ciphertext_size,
3863 size_t *ciphertext_length )
3864{
mohammad16035955c982018-04-26 00:53:03 +03003865 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003866 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003867 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003868
mohammad1603f08a5502018-06-03 15:05:47 +03003869 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003870
Gilles Peskinec5487a82018-12-03 18:08:14 +01003871 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003872 if( status != PSA_SUCCESS )
3873 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003874
Gilles Peskineedf9a652018-08-17 18:11:56 +02003875 /* For all currently supported modes, the tag is at the end of the
3876 * ciphertext. */
3877 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3878 {
3879 status = PSA_ERROR_BUFFER_TOO_SMALL;
3880 goto exit;
3881 }
3882 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003883
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003884#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003885 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003886 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003887 status = mbedtls_to_psa_error(
3888 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3889 MBEDTLS_GCM_ENCRYPT,
3890 plaintext_length,
3891 nonce, nonce_length,
3892 additional_data, additional_data_length,
3893 plaintext, ciphertext,
3894 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003895 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003896 else
3897#endif /* MBEDTLS_GCM_C */
3898#if defined(MBEDTLS_CCM_C)
3899 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003900 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003901 status = mbedtls_to_psa_error(
3902 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3903 plaintext_length,
3904 nonce, nonce_length,
3905 additional_data,
3906 additional_data_length,
3907 plaintext, ciphertext,
3908 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003909 }
mohammad16035c8845f2018-05-09 05:40:09 -07003910 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003911#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003912#if defined(MBEDTLS_CHACHAPOLY_C)
3913 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3914 {
3915 if( nonce_length != 12 || operation.tag_length != 16 )
3916 {
3917 status = PSA_ERROR_NOT_SUPPORTED;
3918 goto exit;
3919 }
3920 status = mbedtls_to_psa_error(
3921 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3922 plaintext_length,
3923 nonce,
3924 additional_data,
3925 additional_data_length,
3926 plaintext,
3927 ciphertext,
3928 tag ) );
3929 }
3930 else
3931#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003932 {
mohammad1603554faad2018-06-03 15:07:38 +03003933 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003934 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003935
Gilles Peskineedf9a652018-08-17 18:11:56 +02003936 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3937 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003938
Gilles Peskineedf9a652018-08-17 18:11:56 +02003939exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003940 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003941 if( status == PSA_SUCCESS )
3942 *ciphertext_length = plaintext_length + operation.tag_length;
3943 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003944}
3945
Gilles Peskineee652a32018-06-01 19:23:52 +02003946/* Locate the tag in a ciphertext buffer containing the encrypted data
3947 * followed by the tag. Return the length of the part preceding the tag in
3948 * *plaintext_length. This is the size of the plaintext in modes where
3949 * the encrypted data has the same size as the plaintext, such as
3950 * CCM and GCM. */
3951static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3952 const uint8_t *ciphertext,
3953 size_t ciphertext_length,
3954 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003955 const uint8_t **p_tag )
3956{
3957 size_t payload_length;
3958 if( tag_length > ciphertext_length )
3959 return( PSA_ERROR_INVALID_ARGUMENT );
3960 payload_length = ciphertext_length - tag_length;
3961 if( payload_length > plaintext_size )
3962 return( PSA_ERROR_BUFFER_TOO_SMALL );
3963 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003964 return( PSA_SUCCESS );
3965}
3966
Gilles Peskinec5487a82018-12-03 18:08:14 +01003967psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003968 psa_algorithm_t alg,
3969 const uint8_t *nonce,
3970 size_t nonce_length,
3971 const uint8_t *additional_data,
3972 size_t additional_data_length,
3973 const uint8_t *ciphertext,
3974 size_t ciphertext_length,
3975 uint8_t *plaintext,
3976 size_t plaintext_size,
3977 size_t *plaintext_length )
3978{
mohammad16035955c982018-04-26 00:53:03 +03003979 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003980 aead_operation_t operation;
3981 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003982
Gilles Peskineee652a32018-06-01 19:23:52 +02003983 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003984
Gilles Peskinec5487a82018-12-03 18:08:14 +01003985 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003986 if( status != PSA_SUCCESS )
3987 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003988
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003989 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3990 ciphertext, ciphertext_length,
3991 plaintext_size, &tag );
3992 if( status != PSA_SUCCESS )
3993 goto exit;
3994
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003995#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003996 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003997 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003998 status = mbedtls_to_psa_error(
3999 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4000 ciphertext_length - operation.tag_length,
4001 nonce, nonce_length,
4002 additional_data,
4003 additional_data_length,
4004 tag, operation.tag_length,
4005 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004006 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004007 else
4008#endif /* MBEDTLS_GCM_C */
4009#if defined(MBEDTLS_CCM_C)
4010 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004011 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004012 status = mbedtls_to_psa_error(
4013 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4014 ciphertext_length - operation.tag_length,
4015 nonce, nonce_length,
4016 additional_data,
4017 additional_data_length,
4018 ciphertext, plaintext,
4019 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004020 }
mohammad160339574652018-06-01 04:39:53 -07004021 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004022#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004023#if defined(MBEDTLS_CHACHAPOLY_C)
4024 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4025 {
4026 if( nonce_length != 12 || operation.tag_length != 16 )
4027 {
4028 status = PSA_ERROR_NOT_SUPPORTED;
4029 goto exit;
4030 }
4031 status = mbedtls_to_psa_error(
4032 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4033 ciphertext_length - operation.tag_length,
4034 nonce,
4035 additional_data,
4036 additional_data_length,
4037 tag,
4038 ciphertext,
4039 plaintext ) );
4040 }
4041 else
4042#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004043 {
mohammad1603554faad2018-06-03 15:07:38 +03004044 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004045 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004046
Gilles Peskineedf9a652018-08-17 18:11:56 +02004047 if( status != PSA_SUCCESS && plaintext_size != 0 )
4048 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004049
Gilles Peskineedf9a652018-08-17 18:11:56 +02004050exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004051 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004052 if( status == PSA_SUCCESS )
4053 *plaintext_length = ciphertext_length - operation.tag_length;
4054 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004055}
4056
Gilles Peskinea0655c32018-04-30 17:06:50 +02004057
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004058
Gilles Peskine20035e32018-02-03 22:44:14 +01004059/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004060/* Generators */
4061/****************************************************************/
4062
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004063#define HKDF_STATE_INIT 0 /* no input yet */
4064#define HKDF_STATE_STARTED 1 /* got salt */
4065#define HKDF_STATE_KEYED 2 /* got key */
4066#define HKDF_STATE_OUTPUT 3 /* output started */
4067
Gilles Peskinecbe66502019-05-16 16:59:18 +02004068static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004069 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004070{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004071 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4072 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004073 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004074 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004075}
4076
4077
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004078psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004079{
4080 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004081 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004082 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004083 {
4084 /* The object has (apparently) been initialized but it is not
4085 * in use. It's ok to call abort on such an object, and there's
4086 * nothing to do. */
4087 }
4088 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01004089 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004090 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004091 if( operation->ctx.buffer.data != NULL )
Gilles Peskine751d9652018-09-18 12:05:44 +02004092 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004093 mbedtls_platform_zeroize( operation->ctx.buffer.data,
4094 operation->ctx.buffer.size );
4095 mbedtls_free( operation->ctx.buffer.data );
Gilles Peskine751d9652018-09-18 12:05:44 +02004096 }
4097 }
4098 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004099#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004100 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004101 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004102 mbedtls_free( operation->ctx.hkdf.info );
4103 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004104 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004105 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004106 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004107 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004108 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004109 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004110 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004111 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4112 operation->ctx.tls12_prf.key_len );
4113 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004114 }
Hanno Becker580fba12018-11-13 20:50:45 +00004115
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004116 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004117 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004118 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4119 operation->ctx.tls12_prf.Ai_with_seed_len );
4120 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004121 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004122 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004123 else
4124#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004125 {
4126 status = PSA_ERROR_BAD_STATE;
4127 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004128 memset( operation, 0, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004129 return( status );
4130}
4131
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004132psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004133 size_t *capacity)
4134{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004135 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004136 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004137 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004138 return PSA_ERROR_BAD_STATE;
4139 }
4140
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004141 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004142 return( PSA_SUCCESS );
4143}
4144
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004145psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004146 size_t capacity )
4147{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004148 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004149 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004150 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004151 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004152 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004153 return( PSA_SUCCESS );
4154}
4155
Gilles Peskinebef7f142018-07-12 17:22:21 +02004156#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004157/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004158 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004159static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004160 psa_algorithm_t hash_alg,
4161 uint8_t *output,
4162 size_t output_length )
4163{
4164 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4165 psa_status_t status;
4166
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004167 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4168 return( PSA_ERROR_BAD_STATE );
4169 hkdf->state = HKDF_STATE_OUTPUT;
4170
Gilles Peskinebef7f142018-07-12 17:22:21 +02004171 while( output_length != 0 )
4172 {
4173 /* Copy what remains of the current block */
4174 uint8_t n = hash_length - hkdf->offset_in_block;
4175 if( n > output_length )
4176 n = (uint8_t) output_length;
4177 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4178 output += n;
4179 output_length -= n;
4180 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004181 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004182 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004183 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004184 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004185 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004186 * object was corrupted or if this function is called directly
4187 * inside the library. */
4188 if( hkdf->block_number == 0xff )
4189 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004190
4191 /* We need a new block */
4192 ++hkdf->block_number;
4193 hkdf->offset_in_block = 0;
4194 status = psa_hmac_setup_internal( &hkdf->hmac,
4195 hkdf->prk, hash_length,
4196 hash_alg );
4197 if( status != PSA_SUCCESS )
4198 return( status );
4199 if( hkdf->block_number != 1 )
4200 {
4201 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4202 hkdf->output_block,
4203 hash_length );
4204 if( status != PSA_SUCCESS )
4205 return( status );
4206 }
4207 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4208 hkdf->info,
4209 hkdf->info_length );
4210 if( status != PSA_SUCCESS )
4211 return( status );
4212 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4213 &hkdf->block_number, 1 );
4214 if( status != PSA_SUCCESS )
4215 return( status );
4216 status = psa_hmac_finish_internal( &hkdf->hmac,
4217 hkdf->output_block,
4218 sizeof( hkdf->output_block ) );
4219 if( status != PSA_SUCCESS )
4220 return( status );
4221 }
4222
4223 return( PSA_SUCCESS );
4224}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004225
Gilles Peskinecbe66502019-05-16 16:59:18 +02004226static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4227 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004228 psa_algorithm_t alg )
4229{
4230 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4231 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4232 psa_hmac_internal_data hmac;
4233 psa_status_t status, cleanup_status;
4234
Hanno Becker3b339e22018-11-13 20:56:14 +00004235 unsigned char *Ai;
4236 size_t Ai_len;
4237
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004238 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004239 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004240 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004241 * object was corrupted or if this function is called directly
4242 * inside the library. */
4243 if( tls12_prf->block_number == 0xff )
4244 return( PSA_ERROR_BAD_STATE );
4245
4246 /* We need a new block */
4247 ++tls12_prf->block_number;
4248 tls12_prf->offset_in_block = 0;
4249
4250 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4251 *
4252 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4253 *
4254 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4255 * HMAC_hash(secret, A(2) + seed) +
4256 * HMAC_hash(secret, A(3) + seed) + ...
4257 *
4258 * A(0) = seed
4259 * A(i) = HMAC_hash( secret, A(i-1) )
4260 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004261 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004262 * `HMAC_hash(secret, A(i) + seed)` from which the output
4263 * is currently extracted as `output_block`, while
4264 * `A(i) + seed` is stored in `Ai_with_seed`.
4265 *
4266 * Generating a new block means recalculating `Ai_with_seed`
4267 * from the A(i)-part of it, and afterwards recalculating
4268 * `output_block`.
4269 *
4270 * A(0) is computed at setup time.
4271 *
4272 */
4273
4274 psa_hmac_init_internal( &hmac );
4275
4276 /* We must distinguish the calculation of A(1) from those
4277 * of A(2) and higher, because A(0)=seed has a different
4278 * length than the other A(i). */
4279 if( tls12_prf->block_number == 1 )
4280 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004281 Ai = tls12_prf->Ai_with_seed + hash_length;
4282 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004283 }
4284 else
4285 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004286 Ai = tls12_prf->Ai_with_seed;
4287 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004288 }
4289
Hanno Becker3b339e22018-11-13 20:56:14 +00004290 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4291 status = psa_hmac_setup_internal( &hmac,
4292 tls12_prf->key,
4293 tls12_prf->key_len,
4294 hash_alg );
4295 if( status != PSA_SUCCESS )
4296 goto cleanup;
4297
4298 status = psa_hash_update( &hmac.hash_ctx,
4299 Ai, Ai_len );
4300 if( status != PSA_SUCCESS )
4301 goto cleanup;
4302
4303 status = psa_hmac_finish_internal( &hmac,
4304 tls12_prf->Ai_with_seed,
4305 hash_length );
4306 if( status != PSA_SUCCESS )
4307 goto cleanup;
4308
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004309 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4310 status = psa_hmac_setup_internal( &hmac,
4311 tls12_prf->key,
4312 tls12_prf->key_len,
4313 hash_alg );
4314 if( status != PSA_SUCCESS )
4315 goto cleanup;
4316
4317 status = psa_hash_update( &hmac.hash_ctx,
4318 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004319 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004320 if( status != PSA_SUCCESS )
4321 goto cleanup;
4322
4323 status = psa_hmac_finish_internal( &hmac,
4324 tls12_prf->output_block,
4325 hash_length );
4326 if( status != PSA_SUCCESS )
4327 goto cleanup;
4328
4329cleanup:
4330
4331 cleanup_status = psa_hmac_abort_internal( &hmac );
4332 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4333 status = cleanup_status;
4334
4335 return( status );
4336}
4337
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004338/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004339 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004340static psa_status_t psa_key_derivation_tls12_prf_read(
4341 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004342 psa_algorithm_t alg,
4343 uint8_t *output,
4344 size_t output_length )
4345{
4346 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4347 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4348 psa_status_t status;
4349
4350 while( output_length != 0 )
4351 {
4352 /* Copy what remains of the current block */
4353 uint8_t n = hash_length - tls12_prf->offset_in_block;
4354
4355 /* Check if we have fully processed the current block. */
4356 if( n == 0 )
4357 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004358 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004359 alg );
4360 if( status != PSA_SUCCESS )
4361 return( status );
4362
4363 continue;
4364 }
4365
4366 if( n > output_length )
4367 n = (uint8_t) output_length;
4368 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4369 n );
4370 output += n;
4371 output_length -= n;
4372 tls12_prf->offset_in_block += n;
4373 }
4374
4375 return( PSA_SUCCESS );
4376}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004377#endif /* MBEDTLS_MD_C */
4378
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004379psa_status_t psa_key_derivation_output_bytes( psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004380 uint8_t *output,
4381 size_t output_length )
4382{
4383 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004384 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004385
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004386 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004387 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004388 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004389 return PSA_ERROR_BAD_STATE;
4390 }
4391
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004392 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004393 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004394 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004395 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004396 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004397 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004398 goto exit;
4399 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004400 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004401 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004402 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004403 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004404 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4405 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004406 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004407 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004408 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004409 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004410 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004411
Gilles Peskine969c5d62019-01-16 15:53:06 +01004412 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004413 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 /* Initially, the capacity of a selection operation is always
4415 * the size of the buffer, i.e. `operation->ctx.buffer.size`,
Gilles Peskine211a4362018-10-25 22:22:31 +02004416 * abbreviated in this comment as `size`. When the remaining
4417 * capacity is `c`, the next bytes to serve start `c` bytes
4418 * from the end of the buffer, i.e. `size - c` from the
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 * beginning of the buffer. Since `operation->capacity` was just
Gilles Peskine211a4362018-10-25 22:22:31 +02004420 * decremented above, we need to serve the bytes from
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421 * `size - operation->capacity - output_length` to
4422 * `size - operation->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004423 size_t offset =
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 operation->ctx.buffer.size - operation->capacity - output_length;
4425 memcpy( output, operation->ctx.buffer.data + offset, output_length );
Gilles Peskine751d9652018-09-18 12:05:44 +02004426 status = PSA_SUCCESS;
4427 }
4428 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004429#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004430 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004431 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004432 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004434 output, output_length );
4435 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004436 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4437 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004438 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004441 output_length );
4442 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004443 else
4444#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004445 {
4446 return( PSA_ERROR_BAD_STATE );
4447 }
4448
4449exit:
4450 if( status != PSA_SUCCESS )
4451 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004452 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004453 * This allows us to differentiate between exhausted operations and
4454 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4455 * operations. */
4456 psa_algorithm_t alg = operation->alg;
4457 psa_key_derivation_abort( operation );
4458 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004459 memset( output, '!', output_length );
4460 }
4461 return( status );
4462}
4463
Gilles Peskine08542d82018-07-19 17:05:42 +02004464#if defined(MBEDTLS_DES_C)
4465static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4466{
4467 if( data_size >= 8 )
4468 mbedtls_des_key_set_parity( data );
4469 if( data_size >= 16 )
4470 mbedtls_des_key_set_parity( data + 8 );
4471 if( data_size >= 24 )
4472 mbedtls_des_key_set_parity( data + 16 );
4473}
4474#endif /* MBEDTLS_DES_C */
4475
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004476static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004477 psa_key_slot_t *slot,
4478 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004479 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004480{
4481 uint8_t *data = NULL;
4482 size_t bytes = PSA_BITS_TO_BYTES( bits );
4483 psa_status_t status;
4484
4485 if( ! key_type_is_raw_bytes( slot->type ) )
4486 return( PSA_ERROR_INVALID_ARGUMENT );
4487 if( bits % 8 != 0 )
4488 return( PSA_ERROR_INVALID_ARGUMENT );
4489 data = mbedtls_calloc( 1, bytes );
4490 if( data == NULL )
4491 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4492
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004494 if( status != PSA_SUCCESS )
4495 goto exit;
4496#if defined(MBEDTLS_DES_C)
4497 if( slot->type == PSA_KEY_TYPE_DES )
4498 psa_des_set_key_parity( data, bytes );
4499#endif /* MBEDTLS_DES_C */
4500 status = psa_import_key_into_slot( slot, data, bytes );
4501
4502exit:
4503 mbedtls_free( data );
4504 return( status );
4505}
4506
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004507psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004508 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004509 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004510{
4511 psa_status_t status;
4512 psa_key_slot_t *slot = NULL;
4513 status = psa_start_key_creation( attributes, handle, &slot );
4514 if( status == PSA_SUCCESS )
4515 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004516 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004517 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004518 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004519 }
4520 if( status == PSA_SUCCESS )
4521 status = psa_finish_key_creation( slot );
4522 if( status != PSA_SUCCESS )
4523 {
4524 psa_fail_key_creation( slot );
4525 *handle = 0;
4526 }
4527 return( status );
4528}
4529
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004530psa_status_t psa_generate_derived_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004531 psa_key_type_t type,
4532 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004533 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004534{
4535 uint8_t *data = NULL;
4536 size_t bytes = PSA_BITS_TO_BYTES( bits );
4537 psa_status_t status;
4538
4539 if( ! key_type_is_raw_bytes( type ) )
4540 return( PSA_ERROR_INVALID_ARGUMENT );
4541 if( bits % 8 != 0 )
4542 return( PSA_ERROR_INVALID_ARGUMENT );
4543 data = mbedtls_calloc( 1, bytes );
4544 if( data == NULL )
4545 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4546
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004547 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004548 if( status != PSA_SUCCESS )
4549 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004550#if defined(MBEDTLS_DES_C)
4551 if( type == PSA_KEY_TYPE_DES )
4552 psa_des_set_key_parity( data, bytes );
4553#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004554 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004555
4556exit:
4557 mbedtls_free( data );
4558 return( status );
4559}
4560
4561
4562
4563/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004564/* Key derivation */
4565/****************************************************************/
4566
Gilles Peskinea05219c2018-11-16 16:02:56 +01004567#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004568/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004569 * of the HKDF algorithm.
4570 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004571 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004572 * to potentially free embedded data structures and wipe confidential data.
4573 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004574static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004575 const uint8_t *secret,
4576 size_t secret_length,
4577 psa_algorithm_t hash_alg,
4578 const uint8_t *salt,
4579 size_t salt_length,
4580 const uint8_t *label,
4581 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004582{
4583 psa_status_t status;
4584 status = psa_hmac_setup_internal( &hkdf->hmac,
4585 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004586 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004587 if( status != PSA_SUCCESS )
4588 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004589 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004590 if( status != PSA_SUCCESS )
4591 return( status );
4592 status = psa_hmac_finish_internal( &hkdf->hmac,
4593 hkdf->prk,
4594 sizeof( hkdf->prk ) );
4595 if( status != PSA_SUCCESS )
4596 return( status );
4597 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4598 hkdf->block_number = 0;
4599 hkdf->info_length = label_length;
4600 if( label_length != 0 )
4601 {
4602 hkdf->info = mbedtls_calloc( 1, label_length );
4603 if( hkdf->info == NULL )
4604 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4605 memcpy( hkdf->info, label, label_length );
4606 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004607 hkdf->state = HKDF_STATE_KEYED;
4608 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004609 return( PSA_SUCCESS );
4610}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004611#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004612
Gilles Peskinea05219c2018-11-16 16:02:56 +01004613#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004614/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004615 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004616 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004617 * to potentially free embedded data structures and wipe confidential data.
4618 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004619static psa_status_t psa_key_derivation_tls12_prf_setup(
4620 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004621 const unsigned char *key,
4622 size_t key_len,
4623 psa_algorithm_t hash_alg,
4624 const uint8_t *salt,
4625 size_t salt_length,
4626 const uint8_t *label,
4627 size_t label_length )
4628{
4629 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004630 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4631 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004632
4633 tls12_prf->key = mbedtls_calloc( 1, key_len );
4634 if( tls12_prf->key == NULL )
4635 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4636 tls12_prf->key_len = key_len;
4637 memcpy( tls12_prf->key, key, key_len );
4638
Hanno Becker580fba12018-11-13 20:50:45 +00004639 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004640 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004641 if( overflow )
4642 return( PSA_ERROR_INVALID_ARGUMENT );
4643
4644 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4645 if( tls12_prf->Ai_with_seed == NULL )
4646 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4647 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4648
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004649 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4650 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004651 if( label_length != 0 )
4652 {
4653 memcpy( tls12_prf->Ai_with_seed + hash_length,
4654 label, label_length );
4655 }
4656
4657 if( salt_length != 0 )
4658 {
4659 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4660 salt, salt_length );
4661 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004662
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004663 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004664 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004665 tls12_prf->block_number = 0;
4666 tls12_prf->offset_in_block = hash_length;
4667
4668 return( PSA_SUCCESS );
4669}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004670
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004671/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004672static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4673 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004674 const unsigned char *psk,
4675 size_t psk_len,
4676 psa_algorithm_t hash_alg,
4677 const uint8_t *salt,
4678 size_t salt_length,
4679 const uint8_t *label,
4680 size_t label_length )
4681{
4682 psa_status_t status;
4683 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4684
4685 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4686 return( PSA_ERROR_INVALID_ARGUMENT );
4687
4688 /* Quoting RFC 4279, Section 2:
4689 *
4690 * The premaster secret is formed as follows: if the PSK is N octets
4691 * long, concatenate a uint16 with the value N, N zero octets, a second
4692 * uint16 with the value N, and the PSK itself.
4693 */
4694
4695 pms[0] = ( psk_len >> 8 ) & 0xff;
4696 pms[1] = ( psk_len >> 0 ) & 0xff;
4697 memset( pms + 2, 0, psk_len );
4698 pms[2 + psk_len + 0] = pms[0];
4699 pms[2 + psk_len + 1] = pms[1];
4700 memcpy( pms + 4 + psk_len, psk, psk_len );
4701
Gilles Peskinecbe66502019-05-16 16:59:18 +02004702 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004703 pms, 4 + 2 * psk_len,
4704 hash_alg,
4705 salt, salt_length,
4706 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004707
Gilles Peskine3f108122018-12-07 18:14:53 +01004708 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004709 return( status );
4710}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004711#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004712
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004713/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004714 * to potentially free embedded data structures and wipe confidential data.
4715 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004716static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004717 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004718 const uint8_t *secret, size_t secret_length,
4719 psa_algorithm_t alg,
4720 const uint8_t *salt, size_t salt_length,
4721 const uint8_t *label, size_t label_length,
4722 size_t capacity )
4723{
4724 psa_status_t status;
4725 size_t max_capacity;
4726
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004727 /* Set operation->alg even on failure so that abort knows what to do. */
4728 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004729
Gilles Peskine751d9652018-09-18 12:05:44 +02004730 if( alg == PSA_ALG_SELECT_RAW )
4731 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004732 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004733 if( salt_length != 0 )
4734 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004735 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004736 if( label_length != 0 )
4737 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 operation->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4739 if( operation->ctx.buffer.data == NULL )
Gilles Peskine751d9652018-09-18 12:05:44 +02004740 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 memcpy( operation->ctx.buffer.data, secret, secret_length );
4742 operation->ctx.buffer.size = secret_length;
Gilles Peskine751d9652018-09-18 12:05:44 +02004743 max_capacity = secret_length;
4744 status = PSA_SUCCESS;
4745 }
4746 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004747#if defined(MBEDTLS_MD_C)
4748 if( PSA_ALG_IS_HKDF( alg ) )
4749 {
4750 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4751 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4752 if( hash_size == 0 )
4753 return( PSA_ERROR_NOT_SUPPORTED );
4754 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004756 secret, secret_length,
4757 hash_alg,
4758 salt, salt_length,
4759 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004760 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004761 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4762 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4763 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004764 {
4765 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4766 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4767
4768 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4769 if( hash_alg != PSA_ALG_SHA_256 &&
4770 hash_alg != PSA_ALG_SHA_384 )
4771 {
4772 return( PSA_ERROR_NOT_SUPPORTED );
4773 }
4774
4775 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004776
4777 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4778 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004779 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004780 secret, secret_length,
4781 hash_alg, salt, salt_length,
4782 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004783 }
4784 else
4785 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004786 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004787 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004788 secret, secret_length,
4789 hash_alg, salt, salt_length,
4790 label, label_length );
4791 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004792 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004793 else
4794#endif
4795 {
4796 return( PSA_ERROR_NOT_SUPPORTED );
4797 }
4798
4799 if( status != PSA_SUCCESS )
4800 return( status );
4801
4802 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004803 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004804 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004806 else
4807 return( PSA_ERROR_INVALID_ARGUMENT );
4808
4809 return( PSA_SUCCESS );
4810}
4811
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004812psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004813 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004814 psa_algorithm_t alg,
4815 const uint8_t *salt,
4816 size_t salt_length,
4817 const uint8_t *label,
4818 size_t label_length,
4819 size_t capacity )
4820{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004821 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004822 psa_status_t status;
4823
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004825 return( PSA_ERROR_BAD_STATE );
4826
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004827 /* Make sure that alg is a key derivation algorithm. This prevents
4828 * key selection algorithms, which psa_key_derivation_internal
4829 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004830 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4831 return( PSA_ERROR_INVALID_ARGUMENT );
4832
Gilles Peskinec5487a82018-12-03 18:08:14 +01004833 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004834 if( status != PSA_SUCCESS )
4835 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004836
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004837 if( slot->type != PSA_KEY_TYPE_DERIVE )
4838 return( PSA_ERROR_INVALID_ARGUMENT );
4839
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004840 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004841 slot->data.raw.data,
4842 slot->data.raw.bytes,
4843 alg,
4844 salt, salt_length,
4845 label, label_length,
4846 capacity );
4847 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004849 return( status );
4850}
4851
Gilles Peskine969c5d62019-01-16 15:53:06 +01004852static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004854 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004855{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004856 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004857#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004858 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4859 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4860 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004861 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004862 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004863 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4864 if( hash_size == 0 )
4865 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004866 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4867 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004868 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004869 {
4870 return( PSA_ERROR_NOT_SUPPORTED );
4871 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004872 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004873 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004874 }
4875#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004876 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004877 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004878}
4879
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004880psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004881 psa_algorithm_t alg )
4882{
4883 psa_status_t status;
4884
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004885 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004886 return( PSA_ERROR_BAD_STATE );
4887
Gilles Peskine6843c292019-01-18 16:44:49 +01004888 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4889 return( PSA_ERROR_INVALID_ARGUMENT );
4890 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004891 {
4892 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004893 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004894 }
4895 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4896 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004897 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004898 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004899 else
4900 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004901
4902 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004904 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004905}
4906
4907#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004908static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004909 psa_algorithm_t hash_alg,
4910 psa_key_derivation_step_t step,
4911 const uint8_t *data,
4912 size_t data_length )
4913{
4914 psa_status_t status;
4915 switch( step )
4916 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004917 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004918 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004919 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004920 status = psa_hmac_setup_internal( &hkdf->hmac,
4921 data, data_length,
4922 hash_alg );
4923 if( status != PSA_SUCCESS )
4924 return( status );
4925 hkdf->state = HKDF_STATE_STARTED;
4926 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004927 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004928 /* If no salt was provided, use an empty salt. */
4929 if( hkdf->state == HKDF_STATE_INIT )
4930 {
4931 status = psa_hmac_setup_internal( &hkdf->hmac,
4932 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004933 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004934 if( status != PSA_SUCCESS )
4935 return( status );
4936 hkdf->state = HKDF_STATE_STARTED;
4937 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004938 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004939 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004940 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4941 data, data_length );
4942 if( status != PSA_SUCCESS )
4943 return( status );
4944 status = psa_hmac_finish_internal( &hkdf->hmac,
4945 hkdf->prk,
4946 sizeof( hkdf->prk ) );
4947 if( status != PSA_SUCCESS )
4948 return( status );
4949 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4950 hkdf->block_number = 0;
4951 hkdf->state = HKDF_STATE_KEYED;
4952 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004953 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004954 if( hkdf->state == HKDF_STATE_OUTPUT )
4955 return( PSA_ERROR_BAD_STATE );
4956 if( hkdf->info_set )
4957 return( PSA_ERROR_BAD_STATE );
4958 hkdf->info_length = data_length;
4959 if( data_length != 0 )
4960 {
4961 hkdf->info = mbedtls_calloc( 1, data_length );
4962 if( hkdf->info == NULL )
4963 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4964 memcpy( hkdf->info, data, data_length );
4965 }
4966 hkdf->info_set = 1;
4967 return( PSA_SUCCESS );
4968 default:
4969 return( PSA_ERROR_INVALID_ARGUMENT );
4970 }
4971}
4972#endif /* MBEDTLS_MD_C */
4973
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004974static psa_status_t psa_key_derivation_input_raw(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004976 psa_key_derivation_step_t step,
4977 const uint8_t *data,
4978 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004979{
4980 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004982
Gilles Peskine969c5d62019-01-16 15:53:06 +01004983 if( kdf_alg == PSA_ALG_SELECT_RAW )
4984 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004985 if( operation->capacity != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004986 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004987 operation->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4988 if( operation->ctx.buffer.data == NULL )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004989 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004990 memcpy( operation->ctx.buffer.data, data, data_length );
4991 operation->ctx.buffer.size = data_length;
4992 operation->capacity = data_length;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004993 status = PSA_SUCCESS;
4994 }
4995 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004996#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004997 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004998 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004999 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005000 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005001 step, data, data_length );
5002 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005003 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005004#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005005#if defined(MBEDTLS_MD_C)
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005006 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005007 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005008 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005009 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02005010 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005011 status = PSA_ERROR_NOT_SUPPORTED;
5012 }
5013 else
5014#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005015 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005016 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005017 return( PSA_ERROR_BAD_STATE );
5018 }
5019
5020 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005022 return( status );
5023}
5024
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005025psa_status_t psa_key_derivation_input_bytes( psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005026 psa_key_derivation_step_t step,
5027 const uint8_t *data,
5028 size_t data_length )
5029{
5030 switch( step )
5031 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005032 case PSA_KEY_DERIVATION_INPUT_LABEL:
5033 case PSA_KEY_DERIVATION_INPUT_SALT:
5034 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005035 return( psa_key_derivation_input_raw( operation, step,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005036 data, data_length ) );
5037 default:
5038 return( PSA_ERROR_INVALID_ARGUMENT );
5039 }
5040}
5041
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005042psa_status_t psa_key_derivation_input_key( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005043 psa_key_derivation_step_t step,
5044 psa_key_handle_t handle )
5045{
5046 psa_key_slot_t *slot;
5047 psa_status_t status;
5048 status = psa_get_key_from_slot( handle, &slot,
5049 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005050 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005051 if( status != PSA_SUCCESS )
5052 return( status );
5053 if( slot->type != PSA_KEY_TYPE_DERIVE )
5054 return( PSA_ERROR_INVALID_ARGUMENT );
5055 /* Don't allow a key to be used as an input that is usually public.
5056 * This is debatable. It's ok from a cryptographic perspective to
5057 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005058 * the material should be dedicated to a particular input step,
5059 * otherwise this may allow the key to be used in an unintended way
5060 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005061 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005062 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 return( psa_key_derivation_input_raw( operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005064 step,
5065 slot->data.raw.data,
5066 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005067}
5068
Gilles Peskineea0fb492018-07-12 17:17:20 +02005069
5070
5071/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005072/* Key agreement */
5073/****************************************************************/
5074
Gilles Peskinea05219c2018-11-16 16:02:56 +01005075#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005076static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5077 size_t peer_key_length,
5078 const mbedtls_ecp_keypair *our_key,
5079 uint8_t *shared_secret,
5080 size_t shared_secret_size,
5081 size_t *shared_secret_length )
5082{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005083 mbedtls_ecp_keypair *their_key = NULL;
5084 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005085 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005086 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005087
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005088 status = psa_import_ec_public_key(
5089 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5090 peer_key, peer_key_length,
5091 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005092 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005093 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005094
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005095 status = mbedtls_to_psa_error(
5096 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5097 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005098 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005099 status = mbedtls_to_psa_error(
5100 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5101 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005102 goto exit;
5103
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005104 status = mbedtls_to_psa_error(
5105 mbedtls_ecdh_calc_secret( &ecdh,
5106 shared_secret_length,
5107 shared_secret, shared_secret_size,
5108 mbedtls_ctr_drbg_random,
5109 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005110
5111exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005112 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005113 mbedtls_ecp_keypair_free( their_key );
5114 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005115 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005116}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005117#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005118
Gilles Peskine01d718c2018-09-18 12:01:02 +02005119#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5120
Gilles Peskine0216fe12019-04-11 21:23:21 +02005121static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5122 psa_key_slot_t *private_key,
5123 const uint8_t *peer_key,
5124 size_t peer_key_length,
5125 uint8_t *shared_secret,
5126 size_t shared_secret_size,
5127 size_t *shared_secret_length )
5128{
5129 switch( alg )
5130 {
5131#if defined(MBEDTLS_ECDH_C)
5132 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005133 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005134 return( PSA_ERROR_INVALID_ARGUMENT );
5135 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5136 private_key->data.ecp,
5137 shared_secret, shared_secret_size,
5138 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005139#endif /* MBEDTLS_ECDH_C */
5140 default:
5141 (void) private_key;
5142 (void) peer_key;
5143 (void) peer_key_length;
5144 (void) shared_secret;
5145 (void) shared_secret_size;
5146 (void) shared_secret_length;
5147 return( PSA_ERROR_NOT_SUPPORTED );
5148 }
5149}
5150
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005151/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005152 * to potentially free embedded data structures and wipe confidential data.
5153 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005154static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005155 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005156 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005157 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005158 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005159{
5160 psa_status_t status;
5161 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5162 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005163 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005164
5165 /* Step 1: run the secret agreement algorithm to generate the shared
5166 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005167 status = psa_key_agreement_raw_internal( ka_alg,
5168 private_key,
5169 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005170 shared_secret,
5171 sizeof( shared_secret ),
5172 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005173 if( status != PSA_SUCCESS )
5174 goto exit;
5175
5176 /* Step 2: set up the key derivation to generate key material from
5177 * the shared secret. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005178 status = psa_key_derivation_input_raw( operation, step,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005179 shared_secret, shared_secret_length );
5180
Gilles Peskine01d718c2018-09-18 12:01:02 +02005181exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005182 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005183 return( status );
5184}
5185
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005186psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005187 psa_key_derivation_step_t step,
5188 psa_key_handle_t private_key,
5189 const uint8_t *peer_key,
5190 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005191{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005192 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005193 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005194 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005195 return( PSA_ERROR_INVALID_ARGUMENT );
5196 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005197 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005198 if( status != PSA_SUCCESS )
5199 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005200 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005201 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005202 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005203 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005204 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005205 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005206}
5207
Gilles Peskinebe697d82019-05-16 18:00:41 +02005208psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5209 psa_key_handle_t private_key,
5210 const uint8_t *peer_key,
5211 size_t peer_key_length,
5212 uint8_t *output,
5213 size_t output_size,
5214 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005215{
5216 psa_key_slot_t *slot;
5217 psa_status_t status;
5218
5219 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5220 {
5221 status = PSA_ERROR_INVALID_ARGUMENT;
5222 goto exit;
5223 }
5224 status = psa_get_key_from_slot( private_key, &slot,
5225 PSA_KEY_USAGE_DERIVE, alg );
5226 if( status != PSA_SUCCESS )
5227 goto exit;
5228
5229 status = psa_key_agreement_raw_internal( alg, slot,
5230 peer_key, peer_key_length,
5231 output, output_size,
5232 output_length );
5233
5234exit:
5235 if( status != PSA_SUCCESS )
5236 {
5237 /* If an error happens and is not handled properly, the output
5238 * may be used as a key to protect sensitive data. Arrange for such
5239 * a key to be random, which is likely to result in decryption or
5240 * verification errors. This is better than filling the buffer with
5241 * some constant data such as zeros, which would result in the data
5242 * being protected with a reproducible, easily knowable key.
5243 */
5244 psa_generate_random( output, output_size );
5245 *output_length = output_size;
5246 }
5247 return( status );
5248}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005249
5250
5251/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005252/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005253/****************************************************************/
5254
5255psa_status_t psa_generate_random( uint8_t *output,
5256 size_t output_size )
5257{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005258 int ret;
5259 GUARD_MODULE_INITIALIZED;
5260
5261 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005262 return( mbedtls_to_psa_error( ret ) );
5263}
5264
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005265#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5266#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005267
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005268psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5269 size_t seed_size )
5270{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005271 if( global_data.initialized )
5272 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005273
5274 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5275 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5276 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5277 return( PSA_ERROR_INVALID_ARGUMENT );
5278
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005279 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005280}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005281#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005282
Gilles Peskinee56e8782019-04-26 17:34:02 +02005283#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5284static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5285 size_t domain_parameters_size,
5286 int *exponent )
5287{
5288 size_t i;
5289 uint32_t acc = 0;
5290
5291 if( domain_parameters_size == 0 )
5292 {
5293 *exponent = 65537;
5294 return( PSA_SUCCESS );
5295 }
5296
5297 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5298 * support values that fit in a 32-bit integer, which is larger than
5299 * int on just about every platform anyway. */
5300 if( domain_parameters_size > sizeof( acc ) )
5301 return( PSA_ERROR_NOT_SUPPORTED );
5302 for( i = 0; i < domain_parameters_size; i++ )
5303 acc = ( acc << 8 ) | domain_parameters[i];
5304 if( acc > INT_MAX )
5305 return( PSA_ERROR_NOT_SUPPORTED );
5306 *exponent = acc;
5307 return( PSA_SUCCESS );
5308}
5309#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5310
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005311static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005312 psa_key_slot_t *slot, size_t bits,
5313 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005314{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005315 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005316
Gilles Peskinee56e8782019-04-26 17:34:02 +02005317 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005318 return( PSA_ERROR_INVALID_ARGUMENT );
5319
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005320 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005321 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005322 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005323 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005324 if( status != PSA_SUCCESS )
5325 return( status );
5326 status = psa_generate_random( slot->data.raw.data,
5327 slot->data.raw.bytes );
5328 if( status != PSA_SUCCESS )
5329 {
5330 mbedtls_free( slot->data.raw.data );
5331 return( status );
5332 }
5333#if defined(MBEDTLS_DES_C)
5334 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005335 psa_des_set_key_parity( slot->data.raw.data,
5336 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005337#endif /* MBEDTLS_DES_C */
5338 }
5339 else
5340
5341#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005342 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005343 {
5344 mbedtls_rsa_context *rsa;
5345 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005346 int exponent;
5347 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005348 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5349 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005350 /* Accept only byte-aligned keys, for the same reasons as
5351 * in psa_import_rsa_key(). */
5352 if( bits % 8 != 0 )
5353 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005354 status = psa_read_rsa_exponent( domain_parameters,
5355 domain_parameters_size,
5356 &exponent );
5357 if( status != PSA_SUCCESS )
5358 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005359 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5360 if( rsa == NULL )
5361 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5362 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5363 ret = mbedtls_rsa_gen_key( rsa,
5364 mbedtls_ctr_drbg_random,
5365 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005366 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005367 exponent );
5368 if( ret != 0 )
5369 {
5370 mbedtls_rsa_free( rsa );
5371 mbedtls_free( rsa );
5372 return( mbedtls_to_psa_error( ret ) );
5373 }
5374 slot->data.rsa = rsa;
5375 }
5376 else
5377#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5378
5379#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005380 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005381 {
5382 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5383 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5384 const mbedtls_ecp_curve_info *curve_info =
5385 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5386 mbedtls_ecp_keypair *ecp;
5387 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005388 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005389 return( PSA_ERROR_NOT_SUPPORTED );
5390 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5391 return( PSA_ERROR_NOT_SUPPORTED );
5392 if( curve_info->bit_size != bits )
5393 return( PSA_ERROR_INVALID_ARGUMENT );
5394 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5395 if( ecp == NULL )
5396 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5397 mbedtls_ecp_keypair_init( ecp );
5398 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5399 mbedtls_ctr_drbg_random,
5400 &global_data.ctr_drbg );
5401 if( ret != 0 )
5402 {
5403 mbedtls_ecp_keypair_free( ecp );
5404 mbedtls_free( ecp );
5405 return( mbedtls_to_psa_error( ret ) );
5406 }
5407 slot->data.ecp = ecp;
5408 }
5409 else
5410#endif /* MBEDTLS_ECP_C */
5411
5412 return( PSA_ERROR_NOT_SUPPORTED );
5413
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005414 return( PSA_SUCCESS );
5415}
5416
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005417psa_status_t psa_generate_key_to_handle( psa_key_handle_t handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005418 psa_key_type_t type,
5419 size_t bits,
5420 const void *extra,
5421 size_t extra_size )
5422{
5423 psa_key_slot_t *slot;
5424 psa_status_t status;
5425
Gilles Peskinee56e8782019-04-26 17:34:02 +02005426#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5427 /* The old public exponent encoding is no longer supported. */
5428 if( extra_size != 0 )
5429 return( PSA_ERROR_NOT_SUPPORTED );
5430#endif
5431
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005432 status = psa_get_empty_key_slot( handle, &slot );
5433 if( status != PSA_SUCCESS )
5434 return( status );
5435
Gilles Peskine12313cd2018-06-20 00:20:32 +02005436 slot->type = type;
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005437 status = psa_generate_key_internal( slot, bits, extra, extra_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005438 if( status != PSA_SUCCESS )
5439 slot->type = 0;
Darryl Green0c6575a2018-11-07 16:05:30 +00005440
5441#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
5442 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
5443 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01005444 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005445 }
5446#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5447
5448 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005449}
5450
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005451psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005452 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005453{
5454 psa_status_t status;
5455 psa_key_slot_t *slot = NULL;
5456 status = psa_start_key_creation( attributes, handle, &slot );
5457 if( status == PSA_SUCCESS )
5458 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005459 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005460 slot, attributes->bits,
5461 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005462 }
5463 if( status == PSA_SUCCESS )
5464 status = psa_finish_key_creation( slot );
5465 if( status != PSA_SUCCESS )
5466 {
5467 psa_fail_key_creation( slot );
5468 *handle = 0;
5469 }
5470 return( status );
5471}
5472
5473
Gilles Peskine05d69892018-06-19 22:00:52 +02005474
5475/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005476/* Module setup */
5477/****************************************************************/
5478
Gilles Peskine5e769522018-11-20 21:59:56 +01005479psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5480 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5481 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5482{
5483 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5484 return( PSA_ERROR_BAD_STATE );
5485 global_data.entropy_init = entropy_init;
5486 global_data.entropy_free = entropy_free;
5487 return( PSA_SUCCESS );
5488}
5489
Gilles Peskinee59236f2018-01-27 23:32:46 +01005490void mbedtls_psa_crypto_free( void )
5491{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005492 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005493 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5494 {
5495 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005496 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005497 }
5498 /* Wipe all remaining data, including configuration.
5499 * In particular, this sets all state indicator to the value
5500 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005501 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005502}
5503
5504psa_status_t psa_crypto_init( void )
5505{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005506 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005507 const unsigned char drbg_seed[] = "PSA";
5508
Gilles Peskinec6b69072018-11-20 21:42:52 +01005509 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005510 if( global_data.initialized != 0 )
5511 return( PSA_SUCCESS );
5512
Gilles Peskine5e769522018-11-20 21:59:56 +01005513 /* Set default configuration if
5514 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5515 if( global_data.entropy_init == NULL )
5516 global_data.entropy_init = mbedtls_entropy_init;
5517 if( global_data.entropy_free == NULL )
5518 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005519
Gilles Peskinec6b69072018-11-20 21:42:52 +01005520 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005521 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005522 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005523 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005524 status = mbedtls_to_psa_error(
5525 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5526 mbedtls_entropy_func,
5527 &global_data.entropy,
5528 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5529 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005530 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005531 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005532
Gilles Peskine66fb1262018-12-10 16:29:04 +01005533 status = psa_initialize_key_slots( );
5534 if( status != PSA_SUCCESS )
5535 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005536
5537 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005538 global_data.initialized = 1;
5539
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005541 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005542 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005543 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005544}
5545
5546#endif /* MBEDTLS_PSA_CRYPTO_C */