blob: 768410c99ee1dd65800e1372c2b9349d3f5d46bd [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:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200205 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 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:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200314 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 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{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200591 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000592 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{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200640 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100641 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{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200771 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100772 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 Peskine96f0b3b2019-05-10 19:33:38 +0200789static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
790 psa_algorithm_t requested_alg )
791{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200792 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200793 if( requested_alg == policy_alg )
794 return( 1 );
795 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200796 * and requested_alg is the same hash-and-sign family with any hash,
797 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200798 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
799 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
800 {
801 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
802 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
803 }
804 /* If it isn't permitted, it's forbidden. */
805 return( 0 );
806}
807
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100808/** Test whether a policy permits an algorithm.
809 *
810 * The caller must test usage flags separately.
811 */
812static int psa_key_policy_permits( const psa_key_policy_t *policy,
813 psa_algorithm_t alg )
814{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200815 return( psa_key_algorithm_permits( policy->alg, alg ) ||
816 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100817}
818
Gilles Peskinef603c712019-01-19 13:40:11 +0100819/** Restrict a key policy based on a constraint.
820 *
821 * \param[in,out] policy The policy to restrict.
822 * \param[in] constraint The policy constraint to apply.
823 *
824 * \retval #PSA_SUCCESS
825 * \c *policy contains the intersection of the original value of
826 * \c *policy and \c *constraint.
827 * \retval #PSA_ERROR_INVALID_ARGUMENT
828 * \c *policy and \c *constraint are incompatible.
829 * \c *policy is unchanged.
830 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100831static psa_status_t psa_restrict_key_policy(
832 psa_key_policy_t *policy,
833 const psa_key_policy_t *constraint )
834{
835 psa_algorithm_t intersection_alg =
836 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200837 psa_algorithm_t intersection_alg2 =
838 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100839 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
840 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200841 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
842 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100843 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100844 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200845 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100846 return( PSA_SUCCESS );
847}
848
Darryl Green06fd18d2018-07-16 11:21:11 +0100849/** Retrieve a slot which must contain a key. The key must have allow all the
850 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
851 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100852static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100853 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100854 psa_key_usage_t usage,
855 psa_algorithm_t alg )
856{
857 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100858 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100859
860 *p_slot = NULL;
861
Gilles Peskinec5487a82018-12-03 18:08:14 +0100862 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100863 if( status != PSA_SUCCESS )
864 return( status );
865 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200866 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100867
868 /* Enforce that usage policy for the key slot contains all the flags
869 * required by the usage parameter. There is one exception: public
870 * keys can always be exported, so we treat public key objects as
871 * if they had the export flag. */
872 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
873 usage &= ~PSA_KEY_USAGE_EXPORT;
874 if( ( slot->policy.usage & usage ) != usage )
875 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100876
877 /* Enforce that the usage policy permits the requested algortihm. */
878 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100879 return( PSA_ERROR_NOT_PERMITTED );
880
881 *p_slot = slot;
882 return( PSA_SUCCESS );
883}
Darryl Green940d72c2018-07-13 13:18:51 +0100884
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100885/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100886static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000887{
888 if( slot->type == PSA_KEY_TYPE_NONE )
889 {
890 /* No key material to clean. */
891 }
892 else if( key_type_is_raw_bytes( slot->type ) )
893 {
894 mbedtls_free( slot->data.raw.data );
895 }
896 else
897#if defined(MBEDTLS_RSA_C)
898 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
899 {
900 mbedtls_rsa_free( slot->data.rsa );
901 mbedtls_free( slot->data.rsa );
902 }
903 else
904#endif /* defined(MBEDTLS_RSA_C) */
905#if defined(MBEDTLS_ECP_C)
906 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
907 {
908 mbedtls_ecp_keypair_free( slot->data.ecp );
909 mbedtls_free( slot->data.ecp );
910 }
911 else
912#endif /* defined(MBEDTLS_ECP_C) */
913 {
914 /* Shouldn't happen: the key type is not any type that we
915 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200916 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000917 }
918
919 return( PSA_SUCCESS );
920}
921
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100922static void psa_abort_operations_using_key( psa_key_slot_t *slot )
923{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200924 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100925 (void) slot;
926}
927
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100928/** Completely wipe a slot in memory, including its policy.
929 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100930psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100931{
932 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100933 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100934 /* At this point, key material and other type-specific content has
935 * been wiped. Clear remaining metadata. We can call memset and not
936 * zeroize because the metadata is not particularly sensitive. */
937 memset( slot, 0, sizeof( *slot ) );
938 return( status );
939}
940
Gilles Peskine87a5e562019-04-17 12:28:25 +0200941psa_status_t psa_import_key_to_handle( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100942 psa_key_type_t type,
943 const uint8_t *data,
944 size_t data_length )
945{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100946 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100947 psa_status_t status;
948
Gilles Peskinec5487a82018-12-03 18:08:14 +0100949 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100950 if( status != PSA_SUCCESS )
951 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952
953 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100954
955 status = psa_import_key_into_slot( slot, data, data_length );
956 if( status != PSA_SUCCESS )
957 {
958 slot->type = PSA_KEY_TYPE_NONE;
959 return( status );
960 }
961
Darryl Greend49a4992018-06-18 17:27:26 +0100962#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
963 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
964 {
965 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100966 status = psa_save_persistent_key( slot->persistent_storage_id,
967 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100968 data_length );
969 if( status != PSA_SUCCESS )
970 {
971 (void) psa_remove_key_data_from_memory( slot );
972 slot->type = PSA_KEY_TYPE_NONE;
973 }
974 }
975#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
976
977 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978}
979
Gilles Peskinec5487a82018-12-03 18:08:14 +0100980psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100982 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100983 psa_status_t status = PSA_SUCCESS;
984 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985
Gilles Peskinec5487a82018-12-03 18:08:14 +0100986 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200987 if( status != PSA_SUCCESS )
988 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100989#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
990 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
991 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100992 storage_status =
993 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100994 }
995#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100996 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100997 if( status != PSA_SUCCESS )
998 return( status );
999 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000}
1001
Gilles Peskineb870b182018-07-06 16:02:09 +02001002/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001003static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +02001004{
1005 if( key_type_is_raw_bytes( slot->type ) )
1006 return( slot->data.raw.bytes * 8 );
1007#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001008 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001009 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001010#endif /* defined(MBEDTLS_RSA_C) */
1011#if defined(MBEDTLS_ECP_C)
1012 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1013 return( slot->data.ecp->grp.pbits );
1014#endif /* defined(MBEDTLS_ECP_C) */
1015 /* Shouldn't happen except on an empty slot. */
1016 return( 0 );
1017}
1018
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001019void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1020{
Gilles Peskineb699f072019-04-26 16:06:02 +02001021 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001022 memset( attributes, 0, sizeof( *attributes ) );
1023}
1024
Gilles Peskineb699f072019-04-26 16:06:02 +02001025psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1026 psa_key_type_t type,
1027 const uint8_t *data,
1028 size_t data_length )
1029{
1030 uint8_t *copy = NULL;
1031
1032 if( data_length != 0 )
1033 {
1034 copy = mbedtls_calloc( 1, data_length );
1035 if( copy == NULL )
1036 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1037 memcpy( copy, data, data_length );
1038 }
1039 /* After this point, this function is guaranteed to succeed, so it
1040 * can start modifying `*attributes`. */
1041
1042 if( attributes->domain_parameters != NULL )
1043 {
1044 mbedtls_free( attributes->domain_parameters );
1045 attributes->domain_parameters = NULL;
1046 attributes->domain_parameters_size = 0;
1047 }
1048
1049 attributes->domain_parameters = copy;
1050 attributes->domain_parameters_size = data_length;
1051 attributes->type = type;
1052 return( PSA_SUCCESS );
1053}
1054
1055psa_status_t psa_get_key_domain_parameters(
1056 const psa_key_attributes_t *attributes,
1057 uint8_t *data, size_t data_size, size_t *data_length )
1058{
1059 if( attributes->domain_parameters_size > data_size )
1060 return( PSA_ERROR_BUFFER_TOO_SMALL );
1061 *data_length = attributes->domain_parameters_size;
1062 if( attributes->domain_parameters_size != 0 )
1063 memcpy( data, attributes->domain_parameters,
1064 attributes->domain_parameters_size );
1065 return( PSA_SUCCESS );
1066}
1067
1068#if defined(MBEDTLS_RSA_C)
1069static psa_status_t psa_get_rsa_public_exponent(
1070 const mbedtls_rsa_context *rsa,
1071 psa_key_attributes_t *attributes )
1072{
1073 mbedtls_mpi mpi;
1074 int ret;
1075 uint8_t *buffer = NULL;
1076 size_t buflen;
1077 mbedtls_mpi_init( &mpi );
1078
1079 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1080 if( ret != 0 )
1081 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001082 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1083 {
1084 /* It's the default value, which is reported as an empty string,
1085 * so there's nothing to do. */
1086 goto exit;
1087 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001088
1089 buflen = mbedtls_mpi_size( &mpi );
1090 buffer = mbedtls_calloc( 1, buflen );
1091 if( buffer == NULL )
1092 {
1093 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1094 goto exit;
1095 }
1096 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1097 if( ret != 0 )
1098 goto exit;
1099 attributes->domain_parameters = buffer;
1100 attributes->domain_parameters_size = buflen;
1101
1102exit:
1103 mbedtls_mpi_free( &mpi );
1104 if( ret != 0 )
1105 mbedtls_free( buffer );
1106 return( mbedtls_to_psa_error( ret ) );
1107}
1108#endif /* MBEDTLS_RSA_C */
1109
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001110psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1111 psa_key_attributes_t *attributes )
1112{
1113 psa_key_slot_t *slot;
1114 psa_status_t status;
1115
1116 psa_reset_key_attributes( attributes );
1117
1118 status = psa_get_key_slot( handle, &slot );
1119 if( status != PSA_SUCCESS )
1120 return( status );
1121
1122 attributes->id = slot->persistent_storage_id;
1123 attributes->lifetime = slot->lifetime;
1124 attributes->policy = slot->policy;
1125 attributes->type = slot->type;
1126 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001127
1128 switch( slot->type )
1129 {
1130#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001131 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001132 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1133 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1134 break;
1135#endif
1136 default:
1137 /* Nothing else to do. */
1138 break;
1139 }
1140
1141 if( status != PSA_SUCCESS )
1142 psa_reset_key_attributes( attributes );
1143 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001144}
1145
Gilles Peskinec5487a82018-12-03 18:08:14 +01001146psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001147 psa_key_type_t *type,
1148 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001149{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001150 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001151 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001152
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001153 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001154 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001155 if( bits != NULL )
1156 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001157 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001158 if( status != PSA_SUCCESS )
1159 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001160
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001161 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001162 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001163 if( type != NULL )
1164 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001165 if( bits != NULL )
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001166 *bits = psa_get_key_slot_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001167 return( PSA_SUCCESS );
1168}
1169
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001170#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001171static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1172 unsigned char *buf, size_t size )
1173{
1174 int ret;
1175 unsigned char *c;
1176 size_t len = 0;
1177
1178 c = buf + size;
1179
1180 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1181
1182 return( (int) len );
1183}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001184#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001185
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001186static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1187 uint8_t *data,
1188 size_t data_size,
1189 size_t *data_length,
1190 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001191{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001192 *data_length = 0;
1193
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001194 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001195 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001196
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001197 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001198 {
1199 if( slot->data.raw.bytes > data_size )
1200 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001201 if( data_size != 0 )
1202 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001203 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001204 memset( data + slot->data.raw.bytes, 0,
1205 data_size - slot->data.raw.bytes );
1206 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001207 *data_length = slot->data.raw.bytes;
1208 return( PSA_SUCCESS );
1209 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001210#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001211 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001212 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001213 psa_status_t status;
1214
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001215 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001216 if( bytes > data_size )
1217 return( PSA_ERROR_BUFFER_TOO_SMALL );
1218 status = mbedtls_to_psa_error(
1219 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1220 if( status != PSA_SUCCESS )
1221 return( status );
1222 memset( data + bytes, 0, data_size - bytes );
1223 *data_length = bytes;
1224 return( PSA_SUCCESS );
1225 }
1226#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001227 else
Moran Peker17e36e12018-05-02 12:55:20 +03001228 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001229#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001230 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001231 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001232 {
Moran Pekera998bc62018-04-16 18:16:20 +03001233 mbedtls_pk_context pk;
1234 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001235 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001236 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001237#if defined(MBEDTLS_RSA_C)
1238 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001239 pk.pk_info = &mbedtls_rsa_info;
1240 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001241#else
1242 return( PSA_ERROR_NOT_SUPPORTED );
1243#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001244 }
1245 else
1246 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001247#if defined(MBEDTLS_ECP_C)
1248 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001249 pk.pk_info = &mbedtls_eckey_info;
1250 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001251#else
1252 return( PSA_ERROR_NOT_SUPPORTED );
1253#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001254 }
Moran Pekerd7326592018-05-29 16:56:39 +03001255 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001256 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001257 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001258 }
Moran Peker17e36e12018-05-02 12:55:20 +03001259 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001260 {
Moran Peker17e36e12018-05-02 12:55:20 +03001261 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001262 }
Moran Peker60364322018-04-29 11:34:58 +03001263 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001264 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001265 /* If data_size is 0 then data may be NULL and then the
1266 * call to memset would have undefined behavior. */
1267 if( data_size != 0 )
1268 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001269 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001270 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001271 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1272 * Move the data to the beginning and erase remaining data
1273 * at the original location. */
1274 if( 2 * (size_t) ret <= data_size )
1275 {
1276 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001277 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001278 }
1279 else if( (size_t) ret < data_size )
1280 {
1281 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001282 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001283 }
Moran Pekera998bc62018-04-16 18:16:20 +03001284 *data_length = ret;
1285 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001286 }
1287 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001288#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001289 {
1290 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001291 it is valid for a special-purpose implementation to omit
1292 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001293 return( PSA_ERROR_NOT_SUPPORTED );
1294 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001295 }
1296}
1297
Gilles Peskinec5487a82018-12-03 18:08:14 +01001298psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001299 uint8_t *data,
1300 size_t data_size,
1301 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001302{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001303 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001304 psa_status_t status;
1305
1306 /* Set the key to empty now, so that even when there are errors, we always
1307 * set data_length to a value between 0 and data_size. On error, setting
1308 * the key to empty is a good choice because an empty key representation is
1309 * unlikely to be accepted anywhere. */
1310 *data_length = 0;
1311
1312 /* Export requires the EXPORT flag. There is an exception for public keys,
1313 * which don't require any flag, but psa_get_key_from_slot takes
1314 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001315 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001316 if( status != PSA_SUCCESS )
1317 return( status );
1318 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001319 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001320}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001321
Gilles Peskinec5487a82018-12-03 18:08:14 +01001322psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001323 uint8_t *data,
1324 size_t data_size,
1325 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001326{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001327 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001328 psa_status_t status;
1329
1330 /* Set the key to empty now, so that even when there are errors, we always
1331 * set data_length to a value between 0 and data_size. On error, setting
1332 * the key to empty is a good choice because an empty key representation is
1333 * unlikely to be accepted anywhere. */
1334 *data_length = 0;
1335
1336 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001337 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001338 if( status != PSA_SUCCESS )
1339 return( status );
1340 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001341 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001342}
1343
Darryl Green0c6575a2018-11-07 16:05:30 +00001344#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001345static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001346 size_t bits )
1347{
1348 psa_status_t status;
1349 uint8_t *data;
1350 size_t key_length;
1351 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1352 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001353 if( data == NULL )
1354 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001355 /* Get key data in export format */
1356 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1357 if( status != PSA_SUCCESS )
1358 {
1359 slot->type = PSA_KEY_TYPE_NONE;
1360 goto exit;
1361 }
1362 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001363 status = psa_save_persistent_key( slot->persistent_storage_id,
1364 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001365 data, key_length );
1366 if( status != PSA_SUCCESS )
1367 {
1368 slot->type = PSA_KEY_TYPE_NONE;
1369 }
1370exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001371 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001372 mbedtls_free( data );
1373 return( status );
1374}
1375#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1376
Gilles Peskine4747d192019-04-17 15:05:45 +02001377static psa_status_t psa_set_key_policy_internal(
1378 psa_key_slot_t *slot,
1379 const psa_key_policy_t *policy )
1380{
1381 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001382 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001383 PSA_KEY_USAGE_ENCRYPT |
1384 PSA_KEY_USAGE_DECRYPT |
1385 PSA_KEY_USAGE_SIGN |
1386 PSA_KEY_USAGE_VERIFY |
1387 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1388 return( PSA_ERROR_INVALID_ARGUMENT );
1389
1390 slot->policy = *policy;
1391 return( PSA_SUCCESS );
1392}
1393
1394/** Prepare a key slot to receive key material.
1395 *
1396 * This function allocates a key slot and sets its metadata.
1397 *
1398 * If this function fails, call psa_fail_key_creation().
1399 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001400 * This function is intended to be used as follows:
1401 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1402 * it with the specified attributes, and assign it a handle.
1403 * -# Populate the slot with the key material.
1404 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1405 * In case of failure at any step, stop the sequence and call
1406 * psa_fail_key_creation().
1407 *
Gilles Peskine4747d192019-04-17 15:05:45 +02001408 * \param attributes Key attributes for the new key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001409 * \param handle On success, a handle for the allocated slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001410 * \param p_slot On success, a pointer to the prepared slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001411 *
1412 * \retval #PSA_SUCCESS
1413 * The key slot is ready to receive key material.
1414 * \return If this function fails, the key slot is an invalid state.
1415 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001416 */
1417static psa_status_t psa_start_key_creation(
1418 const psa_key_attributes_t *attributes,
1419 psa_key_handle_t *handle,
1420 psa_key_slot_t **p_slot )
1421{
1422 psa_status_t status;
1423 psa_key_slot_t *slot;
1424
1425 status = psa_allocate_key( handle );
1426 if( status != PSA_SUCCESS )
1427 return( status );
1428 status = psa_get_key_slot( *handle, p_slot );
1429 if( status != PSA_SUCCESS )
1430 return( status );
1431 slot = *p_slot;
1432
1433 status = psa_set_key_policy_internal( slot, &attributes->policy );
1434 if( status != PSA_SUCCESS )
1435 return( status );
1436 slot->lifetime = attributes->lifetime;
1437 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001438 {
1439 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskinef9666592019-05-06 18:56:30 +02001440 attributes->id, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001441 if( status != PSA_SUCCESS )
1442 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001443 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001444 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001445 slot->type = attributes->type;
1446
1447 return( status );
1448}
1449
1450/** Finalize the creation of a key once its key material has been set.
1451 *
1452 * This entails writing the key to persistent storage.
1453 *
1454 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001455 * See the documentation of psa_start_key_creation() for the intended use
1456 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001457 *
1458 * \param slot Pointer to the slot with key material.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001459 *
1460 * \retval #PSA_SUCCESS
1461 * The key was successfully created. The handle is now valid.
1462 * \return If this function fails, the key slot is an invalid state.
1463 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001464 */
1465static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1466{
1467 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001468 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001469
1470#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1471 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1472 {
1473 uint8_t *buffer = NULL;
1474 size_t buffer_size = 0;
1475 size_t length;
1476
1477 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001478 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001479 buffer = mbedtls_calloc( 1, buffer_size );
1480 if( buffer == NULL && buffer_size != 0 )
1481 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1482 status = psa_internal_export_key( slot,
1483 buffer, buffer_size, &length,
1484 0 );
1485
1486 if( status == PSA_SUCCESS )
1487 {
1488 status = psa_save_persistent_key( slot->persistent_storage_id,
1489 slot->type, &slot->policy,
1490 buffer, length );
1491 }
1492
1493 if( buffer_size != 0 )
1494 mbedtls_platform_zeroize( buffer, buffer_size );
1495 mbedtls_free( buffer );
1496 }
1497#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1498
1499 return( status );
1500}
1501
1502/** Abort the creation of a key.
1503 *
1504 * You may call this function after calling psa_start_key_creation(),
1505 * or after psa_finish_key_creation() fails. In other circumstances, this
1506 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001507 * See the documentation of psa_start_key_creation() for the intended use
1508 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001509 *
1510 * \param slot Pointer to the slot with key material.
1511 */
1512static void psa_fail_key_creation( psa_key_slot_t *slot )
1513{
1514 if( slot == NULL )
1515 return;
1516 psa_wipe_key_slot( slot );
1517}
1518
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001519static psa_status_t psa_check_key_slot_attributes(
1520 const psa_key_slot_t *slot,
1521 const psa_key_attributes_t *attributes )
1522{
1523 if( attributes->type != 0 )
1524 {
1525 if( attributes->type != slot->type )
1526 return( PSA_ERROR_INVALID_ARGUMENT );
1527 }
1528
1529 if( attributes->domain_parameters_size != 0 )
1530 {
1531#if defined(MBEDTLS_RSA_C)
1532 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1533 {
1534 mbedtls_mpi actual, required;
1535 int ret;
1536 mbedtls_mpi_init( &actual );
1537 mbedtls_mpi_init( &required );
1538 ret = mbedtls_rsa_export( slot->data.rsa,
1539 NULL, NULL, NULL, NULL, &actual );
1540 if( ret != 0 )
1541 goto rsa_exit;
1542 ret = mbedtls_mpi_read_binary( &required,
1543 attributes->domain_parameters,
1544 attributes->domain_parameters_size );
1545 if( ret != 0 )
1546 goto rsa_exit;
1547 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1548 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1549 rsa_exit:
1550 mbedtls_mpi_free( &actual );
1551 mbedtls_mpi_free( &required );
1552 if( ret != 0)
1553 return( mbedtls_to_psa_error( ret ) );
1554 }
1555 else
1556#endif
1557 {
1558 return( PSA_ERROR_INVALID_ARGUMENT );
1559 }
1560 }
1561
1562 if( attributes->bits != 0 )
1563 {
1564 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1565 return( PSA_ERROR_INVALID_ARGUMENT );
1566 }
1567
1568 return( PSA_SUCCESS );
1569}
1570
Gilles Peskine4747d192019-04-17 15:05:45 +02001571psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001572 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001573 size_t data_length,
1574 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001575{
1576 psa_status_t status;
1577 psa_key_slot_t *slot = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001578
Gilles Peskine4747d192019-04-17 15:05:45 +02001579 status = psa_start_key_creation( attributes, handle, &slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001580 if( status != PSA_SUCCESS )
1581 goto exit;
1582
1583 status = psa_import_key_into_slot( slot, data, data_length );
1584 if( status != PSA_SUCCESS )
1585 goto exit;
1586 status = psa_check_key_slot_attributes( slot, attributes );
1587 if( status != PSA_SUCCESS )
1588 goto exit;
1589
1590 status = psa_finish_key_creation( slot );
1591exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001592 if( status != PSA_SUCCESS )
1593 {
1594 psa_fail_key_creation( slot );
1595 *handle = 0;
1596 }
1597 return( status );
1598}
1599
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001600static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001601 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001602{
1603 psa_status_t status;
1604 uint8_t *buffer = NULL;
1605 size_t buffer_size = 0;
1606 size_t length;
1607
1608 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001609 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001610 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001611 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001612 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001613 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1614 if( status != PSA_SUCCESS )
1615 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001616 target->type = source->type;
1617 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001618
1619exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001620 if( buffer_size != 0 )
1621 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001622 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001623 return( status );
1624}
1625
Gilles Peskine87a5e562019-04-17 12:28:25 +02001626psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001627 psa_key_handle_t target_handle,
1628 const psa_key_policy_t *constraint)
1629{
1630 psa_key_slot_t *source_slot = NULL;
1631 psa_key_slot_t *target_slot = NULL;
1632 psa_key_policy_t new_policy;
1633 psa_status_t status;
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001634 status = psa_get_key_from_slot( source_handle, &source_slot,
1635 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001636 if( status != PSA_SUCCESS )
1637 return( status );
1638 status = psa_get_empty_key_slot( target_handle, &target_slot );
1639 if( status != PSA_SUCCESS )
1640 return( status );
1641
1642 new_policy = target_slot->policy;
1643 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1644 if( status != PSA_SUCCESS )
1645 return( status );
1646 if( constraint != NULL )
1647 {
1648 status = psa_restrict_key_policy( &new_policy, constraint );
1649 if( status != PSA_SUCCESS )
1650 return( status );
1651 }
1652
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001653 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001654 if( status != PSA_SUCCESS )
1655 return( status );
1656
1657 target_slot->policy = new_policy;
1658 return( PSA_SUCCESS );
1659}
1660
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001661psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1662 const psa_key_attributes_t *specified_attributes,
1663 psa_key_handle_t *target_handle )
1664{
1665 psa_status_t status;
1666 psa_key_slot_t *source_slot = NULL;
1667 psa_key_slot_t *target_slot = NULL;
1668 psa_key_attributes_t actual_attributes = *specified_attributes;
1669
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001670 status = psa_get_key_from_slot( source_handle, &source_slot,
1671 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001672 if( status != PSA_SUCCESS )
1673 goto exit;
1674
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001675 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1676 if( status != PSA_SUCCESS )
1677 goto exit;
1678
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001679 status = psa_restrict_key_policy( &actual_attributes.policy,
1680 &source_slot->policy );
1681 if( status != PSA_SUCCESS )
1682 goto exit;
1683
1684 status = psa_start_key_creation( &actual_attributes,
1685 target_handle, &target_slot );
1686 if( status != PSA_SUCCESS )
1687 goto exit;
1688
1689 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001690 if( status != PSA_SUCCESS )
1691 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001692
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001693 status = psa_finish_key_creation( target_slot );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001694exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001695 if( status != PSA_SUCCESS )
1696 {
1697 psa_fail_key_creation( target_slot );
1698 *target_handle = 0;
1699 }
1700 return( status );
1701}
1702
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001703
1704
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001705/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001706/* Message digests */
1707/****************************************************************/
1708
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001709static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001710{
1711 switch( alg )
1712 {
1713#if defined(MBEDTLS_MD2_C)
1714 case PSA_ALG_MD2:
1715 return( &mbedtls_md2_info );
1716#endif
1717#if defined(MBEDTLS_MD4_C)
1718 case PSA_ALG_MD4:
1719 return( &mbedtls_md4_info );
1720#endif
1721#if defined(MBEDTLS_MD5_C)
1722 case PSA_ALG_MD5:
1723 return( &mbedtls_md5_info );
1724#endif
1725#if defined(MBEDTLS_RIPEMD160_C)
1726 case PSA_ALG_RIPEMD160:
1727 return( &mbedtls_ripemd160_info );
1728#endif
1729#if defined(MBEDTLS_SHA1_C)
1730 case PSA_ALG_SHA_1:
1731 return( &mbedtls_sha1_info );
1732#endif
1733#if defined(MBEDTLS_SHA256_C)
1734 case PSA_ALG_SHA_224:
1735 return( &mbedtls_sha224_info );
1736 case PSA_ALG_SHA_256:
1737 return( &mbedtls_sha256_info );
1738#endif
1739#if defined(MBEDTLS_SHA512_C)
1740 case PSA_ALG_SHA_384:
1741 return( &mbedtls_sha384_info );
1742 case PSA_ALG_SHA_512:
1743 return( &mbedtls_sha512_info );
1744#endif
1745 default:
1746 return( NULL );
1747 }
1748}
1749
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001750psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1751{
1752 switch( operation->alg )
1753 {
Gilles Peskine81736312018-06-26 15:04:31 +02001754 case 0:
1755 /* The object has (apparently) been initialized but it is not
1756 * in use. It's ok to call abort on such an object, and there's
1757 * nothing to do. */
1758 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001759#if defined(MBEDTLS_MD2_C)
1760 case PSA_ALG_MD2:
1761 mbedtls_md2_free( &operation->ctx.md2 );
1762 break;
1763#endif
1764#if defined(MBEDTLS_MD4_C)
1765 case PSA_ALG_MD4:
1766 mbedtls_md4_free( &operation->ctx.md4 );
1767 break;
1768#endif
1769#if defined(MBEDTLS_MD5_C)
1770 case PSA_ALG_MD5:
1771 mbedtls_md5_free( &operation->ctx.md5 );
1772 break;
1773#endif
1774#if defined(MBEDTLS_RIPEMD160_C)
1775 case PSA_ALG_RIPEMD160:
1776 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1777 break;
1778#endif
1779#if defined(MBEDTLS_SHA1_C)
1780 case PSA_ALG_SHA_1:
1781 mbedtls_sha1_free( &operation->ctx.sha1 );
1782 break;
1783#endif
1784#if defined(MBEDTLS_SHA256_C)
1785 case PSA_ALG_SHA_224:
1786 case PSA_ALG_SHA_256:
1787 mbedtls_sha256_free( &operation->ctx.sha256 );
1788 break;
1789#endif
1790#if defined(MBEDTLS_SHA512_C)
1791 case PSA_ALG_SHA_384:
1792 case PSA_ALG_SHA_512:
1793 mbedtls_sha512_free( &operation->ctx.sha512 );
1794 break;
1795#endif
1796 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001797 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001798 }
1799 operation->alg = 0;
1800 return( PSA_SUCCESS );
1801}
1802
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001803psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001804 psa_algorithm_t alg )
1805{
1806 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001807
1808 /* A context must be freshly initialized before it can be set up. */
1809 if( operation->alg != 0 )
1810 {
1811 return( PSA_ERROR_BAD_STATE );
1812 }
1813
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001814 switch( alg )
1815 {
1816#if defined(MBEDTLS_MD2_C)
1817 case PSA_ALG_MD2:
1818 mbedtls_md2_init( &operation->ctx.md2 );
1819 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1820 break;
1821#endif
1822#if defined(MBEDTLS_MD4_C)
1823 case PSA_ALG_MD4:
1824 mbedtls_md4_init( &operation->ctx.md4 );
1825 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1826 break;
1827#endif
1828#if defined(MBEDTLS_MD5_C)
1829 case PSA_ALG_MD5:
1830 mbedtls_md5_init( &operation->ctx.md5 );
1831 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1832 break;
1833#endif
1834#if defined(MBEDTLS_RIPEMD160_C)
1835 case PSA_ALG_RIPEMD160:
1836 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1837 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1838 break;
1839#endif
1840#if defined(MBEDTLS_SHA1_C)
1841 case PSA_ALG_SHA_1:
1842 mbedtls_sha1_init( &operation->ctx.sha1 );
1843 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1844 break;
1845#endif
1846#if defined(MBEDTLS_SHA256_C)
1847 case PSA_ALG_SHA_224:
1848 mbedtls_sha256_init( &operation->ctx.sha256 );
1849 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1850 break;
1851 case PSA_ALG_SHA_256:
1852 mbedtls_sha256_init( &operation->ctx.sha256 );
1853 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1854 break;
1855#endif
1856#if defined(MBEDTLS_SHA512_C)
1857 case PSA_ALG_SHA_384:
1858 mbedtls_sha512_init( &operation->ctx.sha512 );
1859 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1860 break;
1861 case PSA_ALG_SHA_512:
1862 mbedtls_sha512_init( &operation->ctx.sha512 );
1863 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1864 break;
1865#endif
1866 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001867 return( PSA_ALG_IS_HASH( alg ) ?
1868 PSA_ERROR_NOT_SUPPORTED :
1869 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001870 }
1871 if( ret == 0 )
1872 operation->alg = alg;
1873 else
1874 psa_hash_abort( operation );
1875 return( mbedtls_to_psa_error( ret ) );
1876}
1877
1878psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1879 const uint8_t *input,
1880 size_t input_length )
1881{
1882 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001883
1884 /* Don't require hash implementations to behave correctly on a
1885 * zero-length input, which may have an invalid pointer. */
1886 if( input_length == 0 )
1887 return( PSA_SUCCESS );
1888
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001889 switch( operation->alg )
1890 {
1891#if defined(MBEDTLS_MD2_C)
1892 case PSA_ALG_MD2:
1893 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1894 input, input_length );
1895 break;
1896#endif
1897#if defined(MBEDTLS_MD4_C)
1898 case PSA_ALG_MD4:
1899 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1900 input, input_length );
1901 break;
1902#endif
1903#if defined(MBEDTLS_MD5_C)
1904 case PSA_ALG_MD5:
1905 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1906 input, input_length );
1907 break;
1908#endif
1909#if defined(MBEDTLS_RIPEMD160_C)
1910 case PSA_ALG_RIPEMD160:
1911 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1912 input, input_length );
1913 break;
1914#endif
1915#if defined(MBEDTLS_SHA1_C)
1916 case PSA_ALG_SHA_1:
1917 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1918 input, input_length );
1919 break;
1920#endif
1921#if defined(MBEDTLS_SHA256_C)
1922 case PSA_ALG_SHA_224:
1923 case PSA_ALG_SHA_256:
1924 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1925 input, input_length );
1926 break;
1927#endif
1928#if defined(MBEDTLS_SHA512_C)
1929 case PSA_ALG_SHA_384:
1930 case PSA_ALG_SHA_512:
1931 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1932 input, input_length );
1933 break;
1934#endif
1935 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001936 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001937 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001938
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001939 if( ret != 0 )
1940 psa_hash_abort( operation );
1941 return( mbedtls_to_psa_error( ret ) );
1942}
1943
1944psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1945 uint8_t *hash,
1946 size_t hash_size,
1947 size_t *hash_length )
1948{
itayzafrir40835d42018-08-02 13:14:17 +03001949 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001950 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001951 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001952
1953 /* Fill the output buffer with something that isn't a valid hash
1954 * (barring an attack on the hash and deliberately-crafted input),
1955 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001956 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001957 /* If hash_size is 0 then hash may be NULL and then the
1958 * call to memset would have undefined behavior. */
1959 if( hash_size != 0 )
1960 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001961
1962 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001963 {
1964 status = PSA_ERROR_BUFFER_TOO_SMALL;
1965 goto exit;
1966 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001967
1968 switch( operation->alg )
1969 {
1970#if defined(MBEDTLS_MD2_C)
1971 case PSA_ALG_MD2:
1972 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1973 break;
1974#endif
1975#if defined(MBEDTLS_MD4_C)
1976 case PSA_ALG_MD4:
1977 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1978 break;
1979#endif
1980#if defined(MBEDTLS_MD5_C)
1981 case PSA_ALG_MD5:
1982 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1983 break;
1984#endif
1985#if defined(MBEDTLS_RIPEMD160_C)
1986 case PSA_ALG_RIPEMD160:
1987 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1988 break;
1989#endif
1990#if defined(MBEDTLS_SHA1_C)
1991 case PSA_ALG_SHA_1:
1992 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1993 break;
1994#endif
1995#if defined(MBEDTLS_SHA256_C)
1996 case PSA_ALG_SHA_224:
1997 case PSA_ALG_SHA_256:
1998 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1999 break;
2000#endif
2001#if defined(MBEDTLS_SHA512_C)
2002 case PSA_ALG_SHA_384:
2003 case PSA_ALG_SHA_512:
2004 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2005 break;
2006#endif
2007 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002008 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002009 }
itayzafrir40835d42018-08-02 13:14:17 +03002010 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002011
itayzafrir40835d42018-08-02 13:14:17 +03002012exit:
2013 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002014 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002015 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002016 return( psa_hash_abort( operation ) );
2017 }
2018 else
2019 {
2020 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002021 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002022 }
2023}
2024
Gilles Peskine2d277862018-06-18 15:41:12 +02002025psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2026 const uint8_t *hash,
2027 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002028{
2029 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2030 size_t actual_hash_length;
2031 psa_status_t status = psa_hash_finish( operation,
2032 actual_hash, sizeof( actual_hash ),
2033 &actual_hash_length );
2034 if( status != PSA_SUCCESS )
2035 return( status );
2036 if( actual_hash_length != hash_length )
2037 return( PSA_ERROR_INVALID_SIGNATURE );
2038 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2039 return( PSA_ERROR_INVALID_SIGNATURE );
2040 return( PSA_SUCCESS );
2041}
2042
Gilles Peskineeb35d782019-01-22 17:56:16 +01002043psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2044 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002045{
2046 if( target_operation->alg != 0 )
2047 return( PSA_ERROR_BAD_STATE );
2048
2049 switch( source_operation->alg )
2050 {
2051 case 0:
2052 return( PSA_ERROR_BAD_STATE );
2053#if defined(MBEDTLS_MD2_C)
2054 case PSA_ALG_MD2:
2055 mbedtls_md2_clone( &target_operation->ctx.md2,
2056 &source_operation->ctx.md2 );
2057 break;
2058#endif
2059#if defined(MBEDTLS_MD4_C)
2060 case PSA_ALG_MD4:
2061 mbedtls_md4_clone( &target_operation->ctx.md4,
2062 &source_operation->ctx.md4 );
2063 break;
2064#endif
2065#if defined(MBEDTLS_MD5_C)
2066 case PSA_ALG_MD5:
2067 mbedtls_md5_clone( &target_operation->ctx.md5,
2068 &source_operation->ctx.md5 );
2069 break;
2070#endif
2071#if defined(MBEDTLS_RIPEMD160_C)
2072 case PSA_ALG_RIPEMD160:
2073 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2074 &source_operation->ctx.ripemd160 );
2075 break;
2076#endif
2077#if defined(MBEDTLS_SHA1_C)
2078 case PSA_ALG_SHA_1:
2079 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2080 &source_operation->ctx.sha1 );
2081 break;
2082#endif
2083#if defined(MBEDTLS_SHA256_C)
2084 case PSA_ALG_SHA_224:
2085 case PSA_ALG_SHA_256:
2086 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2087 &source_operation->ctx.sha256 );
2088 break;
2089#endif
2090#if defined(MBEDTLS_SHA512_C)
2091 case PSA_ALG_SHA_384:
2092 case PSA_ALG_SHA_512:
2093 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2094 &source_operation->ctx.sha512 );
2095 break;
2096#endif
2097 default:
2098 return( PSA_ERROR_NOT_SUPPORTED );
2099 }
2100
2101 target_operation->alg = source_operation->alg;
2102 return( PSA_SUCCESS );
2103}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002104
2105
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002106/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002107/* MAC */
2108/****************************************************************/
2109
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002110static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002111 psa_algorithm_t alg,
2112 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002113 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002114 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002115{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002116 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002117 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002118
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002119 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002120 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002121
Gilles Peskine8c9def32018-02-08 10:02:12 +01002122 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2123 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002124 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002125 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002126 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002127 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128 mode = MBEDTLS_MODE_STREAM;
2129 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002130 case PSA_ALG_CTR:
2131 mode = MBEDTLS_MODE_CTR;
2132 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002133 case PSA_ALG_CFB:
2134 mode = MBEDTLS_MODE_CFB;
2135 break;
2136 case PSA_ALG_OFB:
2137 mode = MBEDTLS_MODE_OFB;
2138 break;
2139 case PSA_ALG_CBC_NO_PADDING:
2140 mode = MBEDTLS_MODE_CBC;
2141 break;
2142 case PSA_ALG_CBC_PKCS7:
2143 mode = MBEDTLS_MODE_CBC;
2144 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002145 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002146 mode = MBEDTLS_MODE_CCM;
2147 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002148 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002149 mode = MBEDTLS_MODE_GCM;
2150 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002151 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2152 mode = MBEDTLS_MODE_CHACHAPOLY;
2153 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002154 default:
2155 return( NULL );
2156 }
2157 }
2158 else if( alg == PSA_ALG_CMAC )
2159 mode = MBEDTLS_MODE_ECB;
2160 else if( alg == PSA_ALG_GMAC )
2161 mode = MBEDTLS_MODE_GCM;
2162 else
2163 return( NULL );
2164
2165 switch( key_type )
2166 {
2167 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002168 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002169 break;
2170 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002171 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2172 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002173 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002174 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002175 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002176 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002177 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2178 * but two-key Triple-DES is functionally three-key Triple-DES
2179 * with K1=K3, so that's how we present it to mbedtls. */
2180 if( key_bits == 128 )
2181 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002182 break;
2183 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002184 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002185 break;
2186 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002187 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002188 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002189 case PSA_KEY_TYPE_CHACHA20:
2190 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2191 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002192 default:
2193 return( NULL );
2194 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002195 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002196 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002197
Jaeden Amero23bbb752018-06-26 14:16:54 +01002198 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2199 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002200}
2201
Gilles Peskinea05219c2018-11-16 16:02:56 +01002202#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002203static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002204{
Gilles Peskine2d277862018-06-18 15:41:12 +02002205 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002206 {
2207 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002208 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002209 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002210 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002211 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002212 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002213 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002214 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002215 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002216 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002217 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002218 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002219 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002220 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002221 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002222 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002223 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002224 return( 128 );
2225 default:
2226 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002227 }
2228}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002229#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002230
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002231/* Initialize the MAC operation structure. Once this function has been
2232 * called, psa_mac_abort can run and will do the right thing. */
2233static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2234 psa_algorithm_t alg )
2235{
2236 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2237
2238 operation->alg = alg;
2239 operation->key_set = 0;
2240 operation->iv_set = 0;
2241 operation->iv_required = 0;
2242 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002243 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002244
2245#if defined(MBEDTLS_CMAC_C)
2246 if( alg == PSA_ALG_CMAC )
2247 {
2248 operation->iv_required = 0;
2249 mbedtls_cipher_init( &operation->ctx.cmac );
2250 status = PSA_SUCCESS;
2251 }
2252 else
2253#endif /* MBEDTLS_CMAC_C */
2254#if defined(MBEDTLS_MD_C)
2255 if( PSA_ALG_IS_HMAC( operation->alg ) )
2256 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002257 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2258 operation->ctx.hmac.hash_ctx.alg = 0;
2259 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002260 }
2261 else
2262#endif /* MBEDTLS_MD_C */
2263 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002264 if( ! PSA_ALG_IS_MAC( alg ) )
2265 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002266 }
2267
2268 if( status != PSA_SUCCESS )
2269 memset( operation, 0, sizeof( *operation ) );
2270 return( status );
2271}
2272
Gilles Peskine01126fa2018-07-12 17:04:55 +02002273#if defined(MBEDTLS_MD_C)
2274static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2275{
Gilles Peskine3f108122018-12-07 18:14:53 +01002276 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002277 return( psa_hash_abort( &hmac->hash_ctx ) );
2278}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002279
2280static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2281{
2282 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2283 memset( hmac, 0, sizeof( *hmac ) );
2284}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002285#endif /* MBEDTLS_MD_C */
2286
Gilles Peskine8c9def32018-02-08 10:02:12 +01002287psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2288{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002289 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002290 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002291 /* The object has (apparently) been initialized but it is not
2292 * in use. It's ok to call abort on such an object, and there's
2293 * nothing to do. */
2294 return( PSA_SUCCESS );
2295 }
2296 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002297#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002298 if( operation->alg == PSA_ALG_CMAC )
2299 {
2300 mbedtls_cipher_free( &operation->ctx.cmac );
2301 }
2302 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002303#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002304#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002305 if( PSA_ALG_IS_HMAC( operation->alg ) )
2306 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002307 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002308 }
2309 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002310#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002311 {
2312 /* Sanity check (shouldn't happen: operation->alg should
2313 * always have been initialized to a valid value). */
2314 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002315 }
Moran Peker41deec42018-04-04 15:43:05 +03002316
Gilles Peskine8c9def32018-02-08 10:02:12 +01002317 operation->alg = 0;
2318 operation->key_set = 0;
2319 operation->iv_set = 0;
2320 operation->iv_required = 0;
2321 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002322 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002323
Gilles Peskine8c9def32018-02-08 10:02:12 +01002324 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002325
2326bad_state:
2327 /* If abort is called on an uninitialized object, we can't trust
2328 * anything. Wipe the object in case it contains confidential data.
2329 * This may result in a memory leak if a pointer gets overwritten,
2330 * but it's too late to do anything about this. */
2331 memset( operation, 0, sizeof( *operation ) );
2332 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002333}
2334
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002335#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002336static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002337 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002338 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002339 const mbedtls_cipher_info_t *cipher_info )
2340{
2341 int ret;
2342
2343 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002344
2345 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2346 if( ret != 0 )
2347 return( ret );
2348
2349 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2350 slot->data.raw.data,
2351 key_bits );
2352 return( ret );
2353}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002354#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002355
Gilles Peskine248051a2018-06-20 16:09:38 +02002356#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002357static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2358 const uint8_t *key,
2359 size_t key_length,
2360 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002361{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002362 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002363 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002364 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002365 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002366 psa_status_t status;
2367
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002368 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2369 * overflow below. This should never trigger if the hash algorithm
2370 * is implemented correctly. */
2371 /* The size checks against the ipad and opad buffers cannot be written
2372 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2373 * because that triggers -Wlogical-op on GCC 7.3. */
2374 if( block_size > sizeof( ipad ) )
2375 return( PSA_ERROR_NOT_SUPPORTED );
2376 if( block_size > sizeof( hmac->opad ) )
2377 return( PSA_ERROR_NOT_SUPPORTED );
2378 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002379 return( PSA_ERROR_NOT_SUPPORTED );
2380
Gilles Peskined223b522018-06-11 18:12:58 +02002381 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002382 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002383 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002384 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002385 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002386 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002387 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002388 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002389 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002390 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002391 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002392 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002393 }
Gilles Peskine96889972018-07-12 17:07:03 +02002394 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2395 * but it is permitted. It is common when HMAC is used in HKDF, for
2396 * example. Don't call `memcpy` in the 0-length because `key` could be
2397 * an invalid pointer which would make the behavior undefined. */
2398 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002399 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002400
Gilles Peskined223b522018-06-11 18:12:58 +02002401 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2402 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002403 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002404 ipad[i] ^= 0x36;
2405 memset( ipad + key_length, 0x36, block_size - key_length );
2406
2407 /* Copy the key material from ipad to opad, flipping the requisite bits,
2408 * and filling the rest of opad with the requisite constant. */
2409 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002410 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2411 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002412
Gilles Peskine01126fa2018-07-12 17:04:55 +02002413 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002414 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002415 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002416
Gilles Peskine01126fa2018-07-12 17:04:55 +02002417 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002418
2419cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002420 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002421
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002422 return( status );
2423}
Gilles Peskine248051a2018-06-20 16:09:38 +02002424#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002425
Gilles Peskine89167cb2018-07-08 20:12:23 +02002426static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002427 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002428 psa_algorithm_t alg,
2429 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002432 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002434 psa_key_usage_t usage =
2435 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002436 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002437 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002438
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002439 /* A context must be freshly initialized before it can be set up. */
2440 if( operation->alg != 0 )
2441 {
2442 return( PSA_ERROR_BAD_STATE );
2443 }
2444
Gilles Peskined911eb72018-08-14 15:18:45 +02002445 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002446 if( status != PSA_SUCCESS )
2447 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002448 if( is_sign )
2449 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450
Gilles Peskinec5487a82018-12-03 18:08:14 +01002451 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002452 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002453 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002454 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002455
Gilles Peskine8c9def32018-02-08 10:02:12 +01002456#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002457 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002458 {
2459 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002460 mbedtls_cipher_info_from_psa( full_length_alg,
2461 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002462 int ret;
2463 if( cipher_info == NULL )
2464 {
2465 status = PSA_ERROR_NOT_SUPPORTED;
2466 goto exit;
2467 }
2468 operation->mac_size = cipher_info->block_size;
2469 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2470 status = mbedtls_to_psa_error( ret );
2471 }
2472 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002473#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002474#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002475 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002476 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002477 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002478 if( hash_alg == 0 )
2479 {
2480 status = PSA_ERROR_NOT_SUPPORTED;
2481 goto exit;
2482 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002483
2484 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2485 /* Sanity check. This shouldn't fail on a valid configuration. */
2486 if( operation->mac_size == 0 ||
2487 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2488 {
2489 status = PSA_ERROR_NOT_SUPPORTED;
2490 goto exit;
2491 }
2492
Gilles Peskine01126fa2018-07-12 17:04:55 +02002493 if( slot->type != PSA_KEY_TYPE_HMAC )
2494 {
2495 status = PSA_ERROR_INVALID_ARGUMENT;
2496 goto exit;
2497 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002498
Gilles Peskine01126fa2018-07-12 17:04:55 +02002499 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2500 slot->data.raw.data,
2501 slot->data.raw.bytes,
2502 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002503 }
2504 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002506 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002507 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002508 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002510
Gilles Peskined911eb72018-08-14 15:18:45 +02002511 if( truncated == 0 )
2512 {
2513 /* The "normal" case: untruncated algorithm. Nothing to do. */
2514 }
2515 else if( truncated < 4 )
2516 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002517 /* A very short MAC is too short for security since it can be
2518 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2519 * so we make this our minimum, even though 32 bits is still
2520 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002521 status = PSA_ERROR_NOT_SUPPORTED;
2522 }
2523 else if( truncated > operation->mac_size )
2524 {
2525 /* It's impossible to "truncate" to a larger length. */
2526 status = PSA_ERROR_INVALID_ARGUMENT;
2527 }
2528 else
2529 operation->mac_size = truncated;
2530
Gilles Peskinefbfac682018-07-08 20:51:54 +02002531exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002532 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002533 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002534 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002536 else
2537 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002538 operation->key_set = 1;
2539 }
2540 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002541}
2542
Gilles Peskine89167cb2018-07-08 20:12:23 +02002543psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002544 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002545 psa_algorithm_t alg )
2546{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002547 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002548}
2549
2550psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002551 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002552 psa_algorithm_t alg )
2553{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002554 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002555}
2556
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2558 const uint8_t *input,
2559 size_t input_length )
2560{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002561 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002562 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002563 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002565 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566 operation->has_input = 1;
2567
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002569 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002570 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002571 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2572 input, input_length );
2573 status = mbedtls_to_psa_error( ret );
2574 }
2575 else
2576#endif /* MBEDTLS_CMAC_C */
2577#if defined(MBEDTLS_MD_C)
2578 if( PSA_ALG_IS_HMAC( operation->alg ) )
2579 {
2580 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2581 input_length );
2582 }
2583 else
2584#endif /* MBEDTLS_MD_C */
2585 {
2586 /* This shouldn't happen if `operation` was initialized by
2587 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002588 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002589 }
2590
Gilles Peskinefbfac682018-07-08 20:51:54 +02002591 if( status != PSA_SUCCESS )
2592 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002593 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594}
2595
Gilles Peskine01126fa2018-07-12 17:04:55 +02002596#if defined(MBEDTLS_MD_C)
2597static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2598 uint8_t *mac,
2599 size_t mac_size )
2600{
2601 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2602 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2603 size_t hash_size = 0;
2604 size_t block_size = psa_get_hash_block_size( hash_alg );
2605 psa_status_t status;
2606
Gilles Peskine01126fa2018-07-12 17:04:55 +02002607 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2608 if( status != PSA_SUCCESS )
2609 return( status );
2610 /* From here on, tmp needs to be wiped. */
2611
2612 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2613 if( status != PSA_SUCCESS )
2614 goto exit;
2615
2616 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2617 if( status != PSA_SUCCESS )
2618 goto exit;
2619
2620 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2621 if( status != PSA_SUCCESS )
2622 goto exit;
2623
Gilles Peskined911eb72018-08-14 15:18:45 +02002624 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2625 if( status != PSA_SUCCESS )
2626 goto exit;
2627
2628 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002629
2630exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002631 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002632 return( status );
2633}
2634#endif /* MBEDTLS_MD_C */
2635
mohammad16036df908f2018-04-02 08:34:15 -07002636static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002637 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002638 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002640 if( ! operation->key_set )
2641 return( PSA_ERROR_BAD_STATE );
2642 if( operation->iv_required && ! operation->iv_set )
2643 return( PSA_ERROR_BAD_STATE );
2644
Gilles Peskine8c9def32018-02-08 10:02:12 +01002645 if( mac_size < operation->mac_size )
2646 return( PSA_ERROR_BUFFER_TOO_SMALL );
2647
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002649 if( operation->alg == PSA_ALG_CMAC )
2650 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002651 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2652 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2653 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002654 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002655 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002656 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002658 else
2659#endif /* MBEDTLS_CMAC_C */
2660#if defined(MBEDTLS_MD_C)
2661 if( PSA_ALG_IS_HMAC( operation->alg ) )
2662 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002663 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002664 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002665 }
2666 else
2667#endif /* MBEDTLS_MD_C */
2668 {
2669 /* This shouldn't happen if `operation` was initialized by
2670 * a setup function. */
2671 return( PSA_ERROR_BAD_STATE );
2672 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673}
2674
Gilles Peskineacd4be32018-07-08 19:56:25 +02002675psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2676 uint8_t *mac,
2677 size_t mac_size,
2678 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002679{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002680 psa_status_t status;
2681
Jaeden Amero252ef282019-02-15 14:05:35 +00002682 if( operation->alg == 0 )
2683 {
2684 return( PSA_ERROR_BAD_STATE );
2685 }
2686
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002687 /* Fill the output buffer with something that isn't a valid mac
2688 * (barring an attack on the mac and deliberately-crafted input),
2689 * in case the caller doesn't check the return status properly. */
2690 *mac_length = mac_size;
2691 /* If mac_size is 0 then mac may be NULL and then the
2692 * call to memset would have undefined behavior. */
2693 if( mac_size != 0 )
2694 memset( mac, '!', mac_size );
2695
Gilles Peskine89167cb2018-07-08 20:12:23 +02002696 if( ! operation->is_sign )
2697 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002698 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002699 }
mohammad16036df908f2018-04-02 08:34:15 -07002700
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002701 status = psa_mac_finish_internal( operation, mac, mac_size );
2702
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002703 if( status == PSA_SUCCESS )
2704 {
2705 status = psa_mac_abort( operation );
2706 if( status == PSA_SUCCESS )
2707 *mac_length = operation->mac_size;
2708 else
2709 memset( mac, '!', mac_size );
2710 }
2711 else
2712 psa_mac_abort( operation );
2713 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002714}
2715
Gilles Peskineacd4be32018-07-08 19:56:25 +02002716psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2717 const uint8_t *mac,
2718 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002719{
Gilles Peskine828ed142018-06-18 23:25:51 +02002720 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002721 psa_status_t status;
2722
Jaeden Amero252ef282019-02-15 14:05:35 +00002723 if( operation->alg == 0 )
2724 {
2725 return( PSA_ERROR_BAD_STATE );
2726 }
2727
Gilles Peskine89167cb2018-07-08 20:12:23 +02002728 if( operation->is_sign )
2729 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002730 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002731 }
2732 if( operation->mac_size != mac_length )
2733 {
2734 status = PSA_ERROR_INVALID_SIGNATURE;
2735 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002736 }
mohammad16036df908f2018-04-02 08:34:15 -07002737
2738 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002739 actual_mac, sizeof( actual_mac ) );
2740
2741 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2742 status = PSA_ERROR_INVALID_SIGNATURE;
2743
2744cleanup:
2745 if( status == PSA_SUCCESS )
2746 status = psa_mac_abort( operation );
2747 else
2748 psa_mac_abort( operation );
2749
Gilles Peskine3f108122018-12-07 18:14:53 +01002750 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002751
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002752 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753}
2754
2755
Gilles Peskine20035e32018-02-03 22:44:14 +01002756
Gilles Peskine20035e32018-02-03 22:44:14 +01002757/****************************************************************/
2758/* Asymmetric cryptography */
2759/****************************************************************/
2760
Gilles Peskine2b450e32018-06-27 15:42:46 +02002761#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002762/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002763 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002764static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2765 size_t hash_length,
2766 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002767{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002768 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002769 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002770 *md_alg = mbedtls_md_get_type( md_info );
2771
2772 /* The Mbed TLS RSA module uses an unsigned int for hash length
2773 * parameters. Validate that it fits so that we don't risk an
2774 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002775#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002776 if( hash_length > UINT_MAX )
2777 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002778#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002779
2780#if defined(MBEDTLS_PKCS1_V15)
2781 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2782 * must be correct. */
2783 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2784 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002785 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002786 if( md_info == NULL )
2787 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002788 if( mbedtls_md_get_size( md_info ) != hash_length )
2789 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002790 }
2791#endif /* MBEDTLS_PKCS1_V15 */
2792
2793#if defined(MBEDTLS_PKCS1_V21)
2794 /* PSS requires a hash internally. */
2795 if( PSA_ALG_IS_RSA_PSS( alg ) )
2796 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002797 if( md_info == NULL )
2798 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002799 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002800#endif /* MBEDTLS_PKCS1_V21 */
2801
Gilles Peskine61b91d42018-06-08 16:09:36 +02002802 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002803}
2804
Gilles Peskine2b450e32018-06-27 15:42:46 +02002805static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2806 psa_algorithm_t alg,
2807 const uint8_t *hash,
2808 size_t hash_length,
2809 uint8_t *signature,
2810 size_t signature_size,
2811 size_t *signature_length )
2812{
2813 psa_status_t status;
2814 int ret;
2815 mbedtls_md_type_t md_alg;
2816
2817 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2818 if( status != PSA_SUCCESS )
2819 return( status );
2820
Gilles Peskine630a18a2018-06-29 17:49:35 +02002821 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002822 return( PSA_ERROR_BUFFER_TOO_SMALL );
2823
2824#if defined(MBEDTLS_PKCS1_V15)
2825 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2826 {
2827 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2828 MBEDTLS_MD_NONE );
2829 ret = mbedtls_rsa_pkcs1_sign( rsa,
2830 mbedtls_ctr_drbg_random,
2831 &global_data.ctr_drbg,
2832 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002833 md_alg,
2834 (unsigned int) hash_length,
2835 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002836 signature );
2837 }
2838 else
2839#endif /* MBEDTLS_PKCS1_V15 */
2840#if defined(MBEDTLS_PKCS1_V21)
2841 if( PSA_ALG_IS_RSA_PSS( alg ) )
2842 {
2843 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2844 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2845 mbedtls_ctr_drbg_random,
2846 &global_data.ctr_drbg,
2847 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002848 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002849 (unsigned int) hash_length,
2850 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002851 signature );
2852 }
2853 else
2854#endif /* MBEDTLS_PKCS1_V21 */
2855 {
2856 return( PSA_ERROR_INVALID_ARGUMENT );
2857 }
2858
2859 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002860 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002861 return( mbedtls_to_psa_error( ret ) );
2862}
2863
2864static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2865 psa_algorithm_t alg,
2866 const uint8_t *hash,
2867 size_t hash_length,
2868 const uint8_t *signature,
2869 size_t signature_length )
2870{
2871 psa_status_t status;
2872 int ret;
2873 mbedtls_md_type_t md_alg;
2874
2875 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2876 if( status != PSA_SUCCESS )
2877 return( status );
2878
Gilles Peskine630a18a2018-06-29 17:49:35 +02002879 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002880 return( PSA_ERROR_BUFFER_TOO_SMALL );
2881
2882#if defined(MBEDTLS_PKCS1_V15)
2883 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2884 {
2885 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2886 MBEDTLS_MD_NONE );
2887 ret = mbedtls_rsa_pkcs1_verify( rsa,
2888 mbedtls_ctr_drbg_random,
2889 &global_data.ctr_drbg,
2890 MBEDTLS_RSA_PUBLIC,
2891 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002892 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002893 hash,
2894 signature );
2895 }
2896 else
2897#endif /* MBEDTLS_PKCS1_V15 */
2898#if defined(MBEDTLS_PKCS1_V21)
2899 if( PSA_ALG_IS_RSA_PSS( alg ) )
2900 {
2901 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2902 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2903 mbedtls_ctr_drbg_random,
2904 &global_data.ctr_drbg,
2905 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002906 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002907 (unsigned int) hash_length,
2908 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002909 signature );
2910 }
2911 else
2912#endif /* MBEDTLS_PKCS1_V21 */
2913 {
2914 return( PSA_ERROR_INVALID_ARGUMENT );
2915 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002916
2917 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2918 * the rest of the signature is invalid". This has little use in
2919 * practice and PSA doesn't report this distinction. */
2920 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2921 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002922 return( mbedtls_to_psa_error( ret ) );
2923}
2924#endif /* MBEDTLS_RSA_C */
2925
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002926#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002927/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2928 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2929 * (even though these functions don't modify it). */
2930static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2931 psa_algorithm_t alg,
2932 const uint8_t *hash,
2933 size_t hash_length,
2934 uint8_t *signature,
2935 size_t signature_size,
2936 size_t *signature_length )
2937{
2938 int ret;
2939 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002940 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002941 mbedtls_mpi_init( &r );
2942 mbedtls_mpi_init( &s );
2943
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002944 if( signature_size < 2 * curve_bytes )
2945 {
2946 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2947 goto cleanup;
2948 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002949
Gilles Peskinea05219c2018-11-16 16:02:56 +01002950#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002951 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2952 {
2953 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2954 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2955 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2956 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2957 hash, hash_length,
2958 md_alg ) );
2959 }
2960 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002961#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002962 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002963 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002964 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2965 hash, hash_length,
2966 mbedtls_ctr_drbg_random,
2967 &global_data.ctr_drbg ) );
2968 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002969
2970 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2971 signature,
2972 curve_bytes ) );
2973 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2974 signature + curve_bytes,
2975 curve_bytes ) );
2976
2977cleanup:
2978 mbedtls_mpi_free( &r );
2979 mbedtls_mpi_free( &s );
2980 if( ret == 0 )
2981 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002982 return( mbedtls_to_psa_error( ret ) );
2983}
2984
2985static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2986 const uint8_t *hash,
2987 size_t hash_length,
2988 const uint8_t *signature,
2989 size_t signature_length )
2990{
2991 int ret;
2992 mbedtls_mpi r, s;
2993 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2994 mbedtls_mpi_init( &r );
2995 mbedtls_mpi_init( &s );
2996
2997 if( signature_length != 2 * curve_bytes )
2998 return( PSA_ERROR_INVALID_SIGNATURE );
2999
3000 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3001 signature,
3002 curve_bytes ) );
3003 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3004 signature + curve_bytes,
3005 curve_bytes ) );
3006
3007 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3008 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003009
3010cleanup:
3011 mbedtls_mpi_free( &r );
3012 mbedtls_mpi_free( &s );
3013 return( mbedtls_to_psa_error( ret ) );
3014}
3015#endif /* MBEDTLS_ECDSA_C */
3016
Gilles Peskinec5487a82018-12-03 18:08:14 +01003017psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003018 psa_algorithm_t alg,
3019 const uint8_t *hash,
3020 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003021 uint8_t *signature,
3022 size_t signature_size,
3023 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003024{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003025 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003026 psa_status_t status;
3027
3028 *signature_length = signature_size;
3029
Gilles Peskinec5487a82018-12-03 18:08:14 +01003030 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003031 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003032 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003033 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003034 {
3035 status = PSA_ERROR_INVALID_ARGUMENT;
3036 goto exit;
3037 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003038
Gilles Peskine20035e32018-02-03 22:44:14 +01003039#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003040 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003041 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003042 status = psa_rsa_sign( slot->data.rsa,
3043 alg,
3044 hash, hash_length,
3045 signature, signature_size,
3046 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003047 }
3048 else
3049#endif /* defined(MBEDTLS_RSA_C) */
3050#if defined(MBEDTLS_ECP_C)
3051 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3052 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003053#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003054 if(
3055#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3056 PSA_ALG_IS_ECDSA( alg )
3057#else
3058 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3059#endif
3060 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003061 status = psa_ecdsa_sign( slot->data.ecp,
3062 alg,
3063 hash, hash_length,
3064 signature, signature_size,
3065 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003066 else
3067#endif /* defined(MBEDTLS_ECDSA_C) */
3068 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003069 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003070 }
itayzafrir5c753392018-05-08 11:18:38 +03003071 }
3072 else
3073#endif /* defined(MBEDTLS_ECP_C) */
3074 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003075 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003076 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003077
3078exit:
3079 /* Fill the unused part of the output buffer (the whole buffer on error,
3080 * the trailing part on success) with something that isn't a valid mac
3081 * (barring an attack on the mac and deliberately-crafted input),
3082 * in case the caller doesn't check the return status properly. */
3083 if( status == PSA_SUCCESS )
3084 memset( signature + *signature_length, '!',
3085 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003086 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003087 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003088 /* If signature_size is 0 then we have nothing to do. We must not call
3089 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003090 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003091}
3092
Gilles Peskinec5487a82018-12-03 18:08:14 +01003093psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003094 psa_algorithm_t alg,
3095 const uint8_t *hash,
3096 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003097 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003098 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003099{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003100 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003101 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003102
Gilles Peskinec5487a82018-12-03 18:08:14 +01003103 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003104 if( status != PSA_SUCCESS )
3105 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003106
Gilles Peskine61b91d42018-06-08 16:09:36 +02003107#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003108 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003109 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003110 return( psa_rsa_verify( slot->data.rsa,
3111 alg,
3112 hash, hash_length,
3113 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003114 }
3115 else
3116#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003117#if defined(MBEDTLS_ECP_C)
3118 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3119 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003120#if defined(MBEDTLS_ECDSA_C)
3121 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003122 return( psa_ecdsa_verify( slot->data.ecp,
3123 hash, hash_length,
3124 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003125 else
3126#endif /* defined(MBEDTLS_ECDSA_C) */
3127 {
3128 return( PSA_ERROR_INVALID_ARGUMENT );
3129 }
itayzafrir5c753392018-05-08 11:18:38 +03003130 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003131 else
3132#endif /* defined(MBEDTLS_ECP_C) */
3133 {
3134 return( PSA_ERROR_NOT_SUPPORTED );
3135 }
3136}
3137
Gilles Peskine072ac562018-06-30 00:21:29 +02003138#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3139static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3140 mbedtls_rsa_context *rsa )
3141{
3142 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3143 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3144 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3145 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3146}
3147#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3148
Gilles Peskinec5487a82018-12-03 18:08:14 +01003149psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003150 psa_algorithm_t alg,
3151 const uint8_t *input,
3152 size_t input_length,
3153 const uint8_t *salt,
3154 size_t salt_length,
3155 uint8_t *output,
3156 size_t output_size,
3157 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003158{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003159 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003160 psa_status_t status;
3161
Darryl Green5cc689a2018-07-24 15:34:10 +01003162 (void) input;
3163 (void) input_length;
3164 (void) salt;
3165 (void) output;
3166 (void) output_size;
3167
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003168 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003169
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003170 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3171 return( PSA_ERROR_INVALID_ARGUMENT );
3172
Gilles Peskinec5487a82018-12-03 18:08:14 +01003173 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003174 if( status != PSA_SUCCESS )
3175 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003176 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003177 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003179
3180#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003181 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003182 {
3183 mbedtls_rsa_context *rsa = slot->data.rsa;
3184 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003185 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003186 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003187#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003188 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003189 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003190 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3191 mbedtls_ctr_drbg_random,
3192 &global_data.ctr_drbg,
3193 MBEDTLS_RSA_PUBLIC,
3194 input_length,
3195 input,
3196 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003197 }
3198 else
3199#endif /* MBEDTLS_PKCS1_V15 */
3200#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003201 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003202 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003203 psa_rsa_oaep_set_padding_mode( alg, rsa );
3204 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3205 mbedtls_ctr_drbg_random,
3206 &global_data.ctr_drbg,
3207 MBEDTLS_RSA_PUBLIC,
3208 salt, salt_length,
3209 input_length,
3210 input,
3211 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212 }
3213 else
3214#endif /* MBEDTLS_PKCS1_V21 */
3215 {
3216 return( PSA_ERROR_INVALID_ARGUMENT );
3217 }
3218 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003219 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003220 return( mbedtls_to_psa_error( ret ) );
3221 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003222 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003223#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003224 {
3225 return( PSA_ERROR_NOT_SUPPORTED );
3226 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003227}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003228
Gilles Peskinec5487a82018-12-03 18:08:14 +01003229psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003230 psa_algorithm_t alg,
3231 const uint8_t *input,
3232 size_t input_length,
3233 const uint8_t *salt,
3234 size_t salt_length,
3235 uint8_t *output,
3236 size_t output_size,
3237 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003239 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003240 psa_status_t status;
3241
Darryl Green5cc689a2018-07-24 15:34:10 +01003242 (void) input;
3243 (void) input_length;
3244 (void) salt;
3245 (void) output;
3246 (void) output_size;
3247
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003248 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003249
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003250 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3251 return( PSA_ERROR_INVALID_ARGUMENT );
3252
Gilles Peskinec5487a82018-12-03 18:08:14 +01003253 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003254 if( status != PSA_SUCCESS )
3255 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003256 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003257 return( PSA_ERROR_INVALID_ARGUMENT );
3258
3259#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003260 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003261 {
3262 mbedtls_rsa_context *rsa = slot->data.rsa;
3263 int ret;
3264
Gilles Peskine630a18a2018-06-29 17:49:35 +02003265 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003266 return( PSA_ERROR_INVALID_ARGUMENT );
3267
3268#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003269 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003270 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003271 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3272 mbedtls_ctr_drbg_random,
3273 &global_data.ctr_drbg,
3274 MBEDTLS_RSA_PRIVATE,
3275 output_length,
3276 input,
3277 output,
3278 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003279 }
3280 else
3281#endif /* MBEDTLS_PKCS1_V15 */
3282#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003283 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003284 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003285 psa_rsa_oaep_set_padding_mode( alg, rsa );
3286 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3287 mbedtls_ctr_drbg_random,
3288 &global_data.ctr_drbg,
3289 MBEDTLS_RSA_PRIVATE,
3290 salt, salt_length,
3291 output_length,
3292 input,
3293 output,
3294 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003295 }
3296 else
3297#endif /* MBEDTLS_PKCS1_V21 */
3298 {
3299 return( PSA_ERROR_INVALID_ARGUMENT );
3300 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003301
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003302 return( mbedtls_to_psa_error( ret ) );
3303 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003305#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306 {
3307 return( PSA_ERROR_NOT_SUPPORTED );
3308 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003309}
Gilles Peskine20035e32018-02-03 22:44:14 +01003310
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003311
3312
mohammad1603503973b2018-03-12 15:59:30 +02003313/****************************************************************/
3314/* Symmetric cryptography */
3315/****************************************************************/
3316
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003317/* Initialize the cipher operation structure. Once this function has been
3318 * called, psa_cipher_abort can run and will do the right thing. */
3319static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3320 psa_algorithm_t alg )
3321{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003322 if( ! PSA_ALG_IS_CIPHER( alg ) )
3323 {
3324 memset( operation, 0, sizeof( *operation ) );
3325 return( PSA_ERROR_INVALID_ARGUMENT );
3326 }
3327
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003328 operation->alg = alg;
3329 operation->key_set = 0;
3330 operation->iv_set = 0;
3331 operation->iv_required = 1;
3332 operation->iv_size = 0;
3333 operation->block_size = 0;
3334 mbedtls_cipher_init( &operation->ctx.cipher );
3335 return( PSA_SUCCESS );
3336}
3337
Gilles Peskinee553c652018-06-04 16:22:46 +02003338static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003339 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003340 psa_algorithm_t alg,
3341 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003342{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003343 int ret = 0;
3344 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003345 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003346 size_t key_bits;
3347 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003348 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3349 PSA_KEY_USAGE_ENCRYPT :
3350 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003351
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003352 /* A context must be freshly initialized before it can be set up. */
3353 if( operation->alg != 0 )
3354 {
3355 return( PSA_ERROR_BAD_STATE );
3356 }
3357
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003358 status = psa_cipher_init( operation, alg );
3359 if( status != PSA_SUCCESS )
3360 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003361
Gilles Peskinec5487a82018-12-03 18:08:14 +01003362 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003363 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003364 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003365 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003366
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003367 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003368 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003369 {
3370 status = PSA_ERROR_NOT_SUPPORTED;
3371 goto exit;
3372 }
mohammad1603503973b2018-03-12 15:59:30 +02003373
mohammad1603503973b2018-03-12 15:59:30 +02003374 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003375 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003376 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003377
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003378#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003379 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003380 {
3381 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3382 unsigned char keys[24];
3383 memcpy( keys, slot->data.raw.data, 16 );
3384 memcpy( keys + 16, slot->data.raw.data, 8 );
3385 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3386 keys,
3387 192, cipher_operation );
3388 }
3389 else
3390#endif
3391 {
3392 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3393 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003394 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003395 }
Moran Peker41deec42018-04-04 15:43:05 +03003396 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003397 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003398
mohammad16038481e742018-03-18 13:57:31 +02003399#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003400 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003401 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003402 case PSA_ALG_CBC_NO_PADDING:
3403 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3404 MBEDTLS_PADDING_NONE );
3405 break;
3406 case PSA_ALG_CBC_PKCS7:
3407 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3408 MBEDTLS_PADDING_PKCS7 );
3409 break;
3410 default:
3411 /* The algorithm doesn't involve padding. */
3412 ret = 0;
3413 break;
3414 }
3415 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003416 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003417#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3418
mohammad1603503973b2018-03-12 15:59:30 +02003419 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003420 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3421 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3422 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003423 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003424 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003425 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003426#if defined(MBEDTLS_CHACHA20_C)
3427 else
3428 if( alg == PSA_ALG_CHACHA20 )
3429 operation->iv_size = 12;
3430#endif
mohammad1603503973b2018-03-12 15:59:30 +02003431
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003432exit:
3433 if( status == 0 )
3434 status = mbedtls_to_psa_error( ret );
3435 if( status != 0 )
3436 psa_cipher_abort( operation );
3437 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003438}
3439
Gilles Peskinefe119512018-07-08 21:39:34 +02003440psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003441 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003442 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003443{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003444 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003445}
3446
Gilles Peskinefe119512018-07-08 21:39:34 +02003447psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003448 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003449 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003450{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003451 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003452}
3453
Gilles Peskinefe119512018-07-08 21:39:34 +02003454psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3455 unsigned char *iv,
3456 size_t iv_size,
3457 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003458{
itayzafrir534bd7c2018-08-02 13:56:32 +03003459 psa_status_t status;
3460 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003461 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003462 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003463 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003464 }
Moran Peker41deec42018-04-04 15:43:05 +03003465 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003466 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003467 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003468 goto exit;
3469 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003470 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3471 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003472 if( ret != 0 )
3473 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003474 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003475 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003476 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003477
mohammad16038481e742018-03-18 13:57:31 +02003478 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003479 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003480
Moran Peker395db872018-05-31 14:07:14 +03003481exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003482 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003483 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003484 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003485}
3486
Gilles Peskinefe119512018-07-08 21:39:34 +02003487psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3488 const unsigned char *iv,
3489 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003490{
itayzafrir534bd7c2018-08-02 13:56:32 +03003491 psa_status_t status;
3492 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003493 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003494 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003495 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003496 }
Moran Pekera28258c2018-05-29 16:25:04 +03003497 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003498 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003499 status = PSA_ERROR_INVALID_ARGUMENT;
3500 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003501 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003502 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3503 status = mbedtls_to_psa_error( ret );
3504exit:
3505 if( status == PSA_SUCCESS )
3506 operation->iv_set = 1;
3507 else
mohammad1603503973b2018-03-12 15:59:30 +02003508 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003509 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003510}
3511
Gilles Peskinee553c652018-06-04 16:22:46 +02003512psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3513 const uint8_t *input,
3514 size_t input_length,
3515 unsigned char *output,
3516 size_t output_size,
3517 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003518{
itayzafrir534bd7c2018-08-02 13:56:32 +03003519 psa_status_t status;
3520 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003521 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003522
3523 if( operation->alg == 0 )
3524 {
3525 return( PSA_ERROR_BAD_STATE );
3526 }
3527
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003528 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003529 {
3530 /* Take the unprocessed partial block left over from previous
3531 * update calls, if any, plus the input to this call. Remove
3532 * the last partial block, if any. You get the data that will be
3533 * output in this call. */
3534 expected_output_size =
3535 ( operation->ctx.cipher.unprocessed_len + input_length )
3536 / operation->block_size * operation->block_size;
3537 }
3538 else
3539 {
3540 expected_output_size = input_length;
3541 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003542
Gilles Peskine89d789c2018-06-04 17:17:16 +02003543 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003544 {
3545 status = PSA_ERROR_BUFFER_TOO_SMALL;
3546 goto exit;
3547 }
mohammad160382759612018-03-12 18:16:40 +02003548
mohammad1603503973b2018-03-12 15:59:30 +02003549 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003550 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003551 status = mbedtls_to_psa_error( ret );
3552exit:
3553 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003554 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003555 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003556}
3557
Gilles Peskinee553c652018-06-04 16:22:46 +02003558psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3559 uint8_t *output,
3560 size_t output_size,
3561 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003562{
David Saadab4ecc272019-02-14 13:48:10 +02003563 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003564 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003565 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003566
mohammad1603503973b2018-03-12 15:59:30 +02003567 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003568 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003569 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003570 }
3571 if( operation->iv_required && ! operation->iv_set )
3572 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003573 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003574 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003575
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003576 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003577 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3578 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003579 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003580 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003581 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003582 }
3583
Janos Follath315b51c2018-07-09 16:04:51 +01003584 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3585 temp_output_buffer,
3586 output_length );
3587 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003588 {
Janos Follath315b51c2018-07-09 16:04:51 +01003589 status = mbedtls_to_psa_error( cipher_ret );
3590 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003591 }
Janos Follath315b51c2018-07-09 16:04:51 +01003592
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003593 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003594 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003595 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003596 memcpy( output, temp_output_buffer, *output_length );
3597 else
3598 {
Janos Follath315b51c2018-07-09 16:04:51 +01003599 status = PSA_ERROR_BUFFER_TOO_SMALL;
3600 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003601 }
mohammad1603503973b2018-03-12 15:59:30 +02003602
Gilles Peskine3f108122018-12-07 18:14:53 +01003603 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003604 status = psa_cipher_abort( operation );
3605
3606 return( status );
3607
3608error:
3609
3610 *output_length = 0;
3611
Gilles Peskine3f108122018-12-07 18:14:53 +01003612 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003613 (void) psa_cipher_abort( operation );
3614
3615 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003616}
3617
Gilles Peskinee553c652018-06-04 16:22:46 +02003618psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3619{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003620 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003621 {
3622 /* The object has (apparently) been initialized but it is not
3623 * in use. It's ok to call abort on such an object, and there's
3624 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003625 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003626 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003627
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003628 /* Sanity check (shouldn't happen: operation->alg should
3629 * always have been initialized to a valid value). */
3630 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3631 return( PSA_ERROR_BAD_STATE );
3632
mohammad1603503973b2018-03-12 15:59:30 +02003633 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003634
Moran Peker41deec42018-04-04 15:43:05 +03003635 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003636 operation->key_set = 0;
3637 operation->iv_set = 0;
3638 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003639 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003640 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003641
Moran Peker395db872018-05-31 14:07:14 +03003642 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003643}
3644
Gilles Peskinea0655c32018-04-30 17:06:50 +02003645
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003646
mohammad16038cc1cee2018-03-28 01:21:33 +03003647/****************************************************************/
3648/* Key Policy */
3649/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003650
mohammad160327010052018-07-03 13:16:15 +03003651#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003652void psa_key_policy_set_usage( psa_key_policy_t *policy,
3653 psa_key_usage_t usage,
3654 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003655{
mohammad16034eed7572018-03-28 05:14:59 -07003656 policy->usage = usage;
3657 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003658}
3659
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003660psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003661{
mohammad16036df908f2018-04-02 08:34:15 -07003662 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003663}
3664
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003665psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003666{
mohammad16036df908f2018-04-02 08:34:15 -07003667 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003668}
mohammad160327010052018-07-03 13:16:15 +03003669#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003670
Gilles Peskinec5487a82018-12-03 18:08:14 +01003671psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003672 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003673{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003674 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003675 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003676
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003677 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003678 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003679
Gilles Peskinec5487a82018-12-03 18:08:14 +01003680 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003681 if( status != PSA_SUCCESS )
3682 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003683
Gilles Peskine4747d192019-04-17 15:05:45 +02003684 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003685}
3686
Gilles Peskinec5487a82018-12-03 18:08:14 +01003687psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003688 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003689{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003690 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003691 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003692
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003693 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003694 return( PSA_ERROR_INVALID_ARGUMENT );
3695
Gilles Peskinec5487a82018-12-03 18:08:14 +01003696 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003697 if( status != PSA_SUCCESS )
3698 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003699
mohammad16036df908f2018-04-02 08:34:15 -07003700 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003701
3702 return( PSA_SUCCESS );
3703}
Gilles Peskine20035e32018-02-03 22:44:14 +01003704
Gilles Peskinea0655c32018-04-30 17:06:50 +02003705
3706
mohammad1603804cd712018-03-20 22:44:08 +02003707/****************************************************************/
3708/* Key Lifetime */
3709/****************************************************************/
3710
Gilles Peskine87a5e562019-04-17 12:28:25 +02003711psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003712 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003713{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003714 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003715 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003716
Gilles Peskinec5487a82018-12-03 18:08:14 +01003717 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003718 if( status != PSA_SUCCESS )
3719 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003720
mohammad1603804cd712018-03-20 22:44:08 +02003721 *lifetime = slot->lifetime;
3722
3723 return( PSA_SUCCESS );
3724}
3725
Gilles Peskine20035e32018-02-03 22:44:14 +01003726
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003727
mohammad16035955c982018-04-26 00:53:03 +03003728/****************************************************************/
3729/* AEAD */
3730/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003731
Gilles Peskineedf9a652018-08-17 18:11:56 +02003732typedef struct
3733{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003734 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003735 const mbedtls_cipher_info_t *cipher_info;
3736 union
3737 {
3738#if defined(MBEDTLS_CCM_C)
3739 mbedtls_ccm_context ccm;
3740#endif /* MBEDTLS_CCM_C */
3741#if defined(MBEDTLS_GCM_C)
3742 mbedtls_gcm_context gcm;
3743#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003744#if defined(MBEDTLS_CHACHAPOLY_C)
3745 mbedtls_chachapoly_context chachapoly;
3746#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003747 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003748 psa_algorithm_t core_alg;
3749 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003750 uint8_t tag_length;
3751} aead_operation_t;
3752
Gilles Peskine30a9e412019-01-14 18:36:12 +01003753static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003754{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003755 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003756 {
3757#if defined(MBEDTLS_CCM_C)
3758 case PSA_ALG_CCM:
3759 mbedtls_ccm_free( &operation->ctx.ccm );
3760 break;
3761#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003762#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003763 case PSA_ALG_GCM:
3764 mbedtls_gcm_free( &operation->ctx.gcm );
3765 break;
3766#endif /* MBEDTLS_GCM_C */
3767 }
3768}
3769
3770static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003771 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003772 psa_key_usage_t usage,
3773 psa_algorithm_t alg )
3774{
3775 psa_status_t status;
3776 size_t key_bits;
3777 mbedtls_cipher_id_t cipher_id;
3778
Gilles Peskinec5487a82018-12-03 18:08:14 +01003779 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003780 if( status != PSA_SUCCESS )
3781 return( status );
3782
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003783 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003784
3785 operation->cipher_info =
3786 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3787 &cipher_id );
3788 if( operation->cipher_info == NULL )
3789 return( PSA_ERROR_NOT_SUPPORTED );
3790
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003791 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003792 {
3793#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003794 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3795 operation->core_alg = PSA_ALG_CCM;
3796 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003797 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3798 * The call to mbedtls_ccm_encrypt_and_tag or
3799 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003800 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3801 return( PSA_ERROR_INVALID_ARGUMENT );
3802 mbedtls_ccm_init( &operation->ctx.ccm );
3803 status = mbedtls_to_psa_error(
3804 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3805 operation->slot->data.raw.data,
3806 (unsigned int) key_bits ) );
3807 if( status != 0 )
3808 goto cleanup;
3809 break;
3810#endif /* MBEDTLS_CCM_C */
3811
3812#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003813 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3814 operation->core_alg = PSA_ALG_GCM;
3815 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003816 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3817 * The call to mbedtls_gcm_crypt_and_tag or
3818 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003819 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3820 return( PSA_ERROR_INVALID_ARGUMENT );
3821 mbedtls_gcm_init( &operation->ctx.gcm );
3822 status = mbedtls_to_psa_error(
3823 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3824 operation->slot->data.raw.data,
3825 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003826 if( status != 0 )
3827 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003828 break;
3829#endif /* MBEDTLS_GCM_C */
3830
Gilles Peskine26869f22019-05-06 15:25:00 +02003831#if defined(MBEDTLS_CHACHAPOLY_C)
3832 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3833 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3834 operation->full_tag_length = 16;
3835 /* We only support the default tag length. */
3836 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3837 return( PSA_ERROR_NOT_SUPPORTED );
3838 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3839 status = mbedtls_to_psa_error(
3840 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3841 operation->slot->data.raw.data ) );
3842 if( status != 0 )
3843 goto cleanup;
3844 break;
3845#endif /* MBEDTLS_CHACHAPOLY_C */
3846
Gilles Peskineedf9a652018-08-17 18:11:56 +02003847 default:
3848 return( PSA_ERROR_NOT_SUPPORTED );
3849 }
3850
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003851 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3852 {
3853 status = PSA_ERROR_INVALID_ARGUMENT;
3854 goto cleanup;
3855 }
3856 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003857
Gilles Peskineedf9a652018-08-17 18:11:56 +02003858 return( PSA_SUCCESS );
3859
3860cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003861 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003862 return( status );
3863}
3864
Gilles Peskinec5487a82018-12-03 18:08:14 +01003865psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003866 psa_algorithm_t alg,
3867 const uint8_t *nonce,
3868 size_t nonce_length,
3869 const uint8_t *additional_data,
3870 size_t additional_data_length,
3871 const uint8_t *plaintext,
3872 size_t plaintext_length,
3873 uint8_t *ciphertext,
3874 size_t ciphertext_size,
3875 size_t *ciphertext_length )
3876{
mohammad16035955c982018-04-26 00:53:03 +03003877 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003878 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003879 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003880
mohammad1603f08a5502018-06-03 15:05:47 +03003881 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003882
Gilles Peskinec5487a82018-12-03 18:08:14 +01003883 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003884 if( status != PSA_SUCCESS )
3885 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003886
Gilles Peskineedf9a652018-08-17 18:11:56 +02003887 /* For all currently supported modes, the tag is at the end of the
3888 * ciphertext. */
3889 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3890 {
3891 status = PSA_ERROR_BUFFER_TOO_SMALL;
3892 goto exit;
3893 }
3894 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003895
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003896#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003897 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003898 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003899 status = mbedtls_to_psa_error(
3900 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3901 MBEDTLS_GCM_ENCRYPT,
3902 plaintext_length,
3903 nonce, nonce_length,
3904 additional_data, additional_data_length,
3905 plaintext, ciphertext,
3906 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003907 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003908 else
3909#endif /* MBEDTLS_GCM_C */
3910#if defined(MBEDTLS_CCM_C)
3911 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003912 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003913 status = mbedtls_to_psa_error(
3914 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3915 plaintext_length,
3916 nonce, nonce_length,
3917 additional_data,
3918 additional_data_length,
3919 plaintext, ciphertext,
3920 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003921 }
mohammad16035c8845f2018-05-09 05:40:09 -07003922 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003923#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003924#if defined(MBEDTLS_CHACHAPOLY_C)
3925 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3926 {
3927 if( nonce_length != 12 || operation.tag_length != 16 )
3928 {
3929 status = PSA_ERROR_NOT_SUPPORTED;
3930 goto exit;
3931 }
3932 status = mbedtls_to_psa_error(
3933 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3934 plaintext_length,
3935 nonce,
3936 additional_data,
3937 additional_data_length,
3938 plaintext,
3939 ciphertext,
3940 tag ) );
3941 }
3942 else
3943#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003944 {
mohammad1603554faad2018-06-03 15:07:38 +03003945 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003946 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003947
Gilles Peskineedf9a652018-08-17 18:11:56 +02003948 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3949 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003950
Gilles Peskineedf9a652018-08-17 18:11:56 +02003951exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003952 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003953 if( status == PSA_SUCCESS )
3954 *ciphertext_length = plaintext_length + operation.tag_length;
3955 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003956}
3957
Gilles Peskineee652a32018-06-01 19:23:52 +02003958/* Locate the tag in a ciphertext buffer containing the encrypted data
3959 * followed by the tag. Return the length of the part preceding the tag in
3960 * *plaintext_length. This is the size of the plaintext in modes where
3961 * the encrypted data has the same size as the plaintext, such as
3962 * CCM and GCM. */
3963static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3964 const uint8_t *ciphertext,
3965 size_t ciphertext_length,
3966 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003967 const uint8_t **p_tag )
3968{
3969 size_t payload_length;
3970 if( tag_length > ciphertext_length )
3971 return( PSA_ERROR_INVALID_ARGUMENT );
3972 payload_length = ciphertext_length - tag_length;
3973 if( payload_length > plaintext_size )
3974 return( PSA_ERROR_BUFFER_TOO_SMALL );
3975 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003976 return( PSA_SUCCESS );
3977}
3978
Gilles Peskinec5487a82018-12-03 18:08:14 +01003979psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003980 psa_algorithm_t alg,
3981 const uint8_t *nonce,
3982 size_t nonce_length,
3983 const uint8_t *additional_data,
3984 size_t additional_data_length,
3985 const uint8_t *ciphertext,
3986 size_t ciphertext_length,
3987 uint8_t *plaintext,
3988 size_t plaintext_size,
3989 size_t *plaintext_length )
3990{
mohammad16035955c982018-04-26 00:53:03 +03003991 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003992 aead_operation_t operation;
3993 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003994
Gilles Peskineee652a32018-06-01 19:23:52 +02003995 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003996
Gilles Peskinec5487a82018-12-03 18:08:14 +01003997 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003998 if( status != PSA_SUCCESS )
3999 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004000
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004001 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4002 ciphertext, ciphertext_length,
4003 plaintext_size, &tag );
4004 if( status != PSA_SUCCESS )
4005 goto exit;
4006
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004007#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004008 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004009 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004010 status = mbedtls_to_psa_error(
4011 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4012 ciphertext_length - operation.tag_length,
4013 nonce, nonce_length,
4014 additional_data,
4015 additional_data_length,
4016 tag, operation.tag_length,
4017 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004018 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004019 else
4020#endif /* MBEDTLS_GCM_C */
4021#if defined(MBEDTLS_CCM_C)
4022 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004023 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004024 status = mbedtls_to_psa_error(
4025 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4026 ciphertext_length - operation.tag_length,
4027 nonce, nonce_length,
4028 additional_data,
4029 additional_data_length,
4030 ciphertext, plaintext,
4031 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004032 }
mohammad160339574652018-06-01 04:39:53 -07004033 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004034#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004035#if defined(MBEDTLS_CHACHAPOLY_C)
4036 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4037 {
4038 if( nonce_length != 12 || operation.tag_length != 16 )
4039 {
4040 status = PSA_ERROR_NOT_SUPPORTED;
4041 goto exit;
4042 }
4043 status = mbedtls_to_psa_error(
4044 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4045 ciphertext_length - operation.tag_length,
4046 nonce,
4047 additional_data,
4048 additional_data_length,
4049 tag,
4050 ciphertext,
4051 plaintext ) );
4052 }
4053 else
4054#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004055 {
mohammad1603554faad2018-06-03 15:07:38 +03004056 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004057 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004058
Gilles Peskineedf9a652018-08-17 18:11:56 +02004059 if( status != PSA_SUCCESS && plaintext_size != 0 )
4060 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004061
Gilles Peskineedf9a652018-08-17 18:11:56 +02004062exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004063 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004064 if( status == PSA_SUCCESS )
4065 *plaintext_length = ciphertext_length - operation.tag_length;
4066 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004067}
4068
Gilles Peskinea0655c32018-04-30 17:06:50 +02004069
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004070
Gilles Peskine20035e32018-02-03 22:44:14 +01004071/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004072/* Generators */
4073/****************************************************************/
4074
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004075#define HKDF_STATE_INIT 0 /* no input yet */
4076#define HKDF_STATE_STARTED 1 /* got salt */
4077#define HKDF_STATE_KEYED 2 /* got key */
4078#define HKDF_STATE_OUTPUT 3 /* output started */
4079
Gilles Peskinecbe66502019-05-16 16:59:18 +02004080static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004081 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004082{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004083 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4084 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004085 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004086 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004087}
4088
4089
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004090psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004091{
4092 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004093 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004094 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004095 {
4096 /* The object has (apparently) been initialized but it is not
4097 * in use. It's ok to call abort on such an object, and there's
4098 * nothing to do. */
4099 }
4100 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01004101 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004102 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004103 if( operation->ctx.buffer.data != NULL )
Gilles Peskine751d9652018-09-18 12:05:44 +02004104 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004105 mbedtls_platform_zeroize( operation->ctx.buffer.data,
4106 operation->ctx.buffer.size );
4107 mbedtls_free( operation->ctx.buffer.data );
Gilles Peskine751d9652018-09-18 12:05:44 +02004108 }
4109 }
4110 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004111#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004112 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004113 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004114 mbedtls_free( operation->ctx.hkdf.info );
4115 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004116 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004117 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004118 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004119 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004120 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004121 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004122 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004123 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4124 operation->ctx.tls12_prf.key_len );
4125 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004126 }
Hanno Becker580fba12018-11-13 20:50:45 +00004127
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004128 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004129 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004130 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4131 operation->ctx.tls12_prf.Ai_with_seed_len );
4132 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004133 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004134 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004135 else
4136#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004137 {
4138 status = PSA_ERROR_BAD_STATE;
4139 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004140 memset( operation, 0, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004141 return( status );
4142}
4143
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004144psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004145 size_t *capacity)
4146{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004147 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004148 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004149 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004150 return PSA_ERROR_BAD_STATE;
4151 }
4152
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004153 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004154 return( PSA_SUCCESS );
4155}
4156
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004157psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004158 size_t capacity )
4159{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004160 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004161 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004162 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004163 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004164 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004165 return( PSA_SUCCESS );
4166}
4167
Gilles Peskinebef7f142018-07-12 17:22:21 +02004168#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004169/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004170 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004171static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004172 psa_algorithm_t hash_alg,
4173 uint8_t *output,
4174 size_t output_length )
4175{
4176 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4177 psa_status_t status;
4178
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004179 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4180 return( PSA_ERROR_BAD_STATE );
4181 hkdf->state = HKDF_STATE_OUTPUT;
4182
Gilles Peskinebef7f142018-07-12 17:22:21 +02004183 while( output_length != 0 )
4184 {
4185 /* Copy what remains of the current block */
4186 uint8_t n = hash_length - hkdf->offset_in_block;
4187 if( n > output_length )
4188 n = (uint8_t) output_length;
4189 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4190 output += n;
4191 output_length -= n;
4192 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004193 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004194 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004195 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004196 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004197 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004198 * object was corrupted or if this function is called directly
4199 * inside the library. */
4200 if( hkdf->block_number == 0xff )
4201 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004202
4203 /* We need a new block */
4204 ++hkdf->block_number;
4205 hkdf->offset_in_block = 0;
4206 status = psa_hmac_setup_internal( &hkdf->hmac,
4207 hkdf->prk, hash_length,
4208 hash_alg );
4209 if( status != PSA_SUCCESS )
4210 return( status );
4211 if( hkdf->block_number != 1 )
4212 {
4213 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4214 hkdf->output_block,
4215 hash_length );
4216 if( status != PSA_SUCCESS )
4217 return( status );
4218 }
4219 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4220 hkdf->info,
4221 hkdf->info_length );
4222 if( status != PSA_SUCCESS )
4223 return( status );
4224 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4225 &hkdf->block_number, 1 );
4226 if( status != PSA_SUCCESS )
4227 return( status );
4228 status = psa_hmac_finish_internal( &hkdf->hmac,
4229 hkdf->output_block,
4230 sizeof( hkdf->output_block ) );
4231 if( status != PSA_SUCCESS )
4232 return( status );
4233 }
4234
4235 return( PSA_SUCCESS );
4236}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004237
Gilles Peskinecbe66502019-05-16 16:59:18 +02004238static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4239 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004240 psa_algorithm_t alg )
4241{
4242 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4243 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4244 psa_hmac_internal_data hmac;
4245 psa_status_t status, cleanup_status;
4246
Hanno Becker3b339e22018-11-13 20:56:14 +00004247 unsigned char *Ai;
4248 size_t Ai_len;
4249
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004250 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004251 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004252 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004253 * object was corrupted or if this function is called directly
4254 * inside the library. */
4255 if( tls12_prf->block_number == 0xff )
4256 return( PSA_ERROR_BAD_STATE );
4257
4258 /* We need a new block */
4259 ++tls12_prf->block_number;
4260 tls12_prf->offset_in_block = 0;
4261
4262 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4263 *
4264 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4265 *
4266 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4267 * HMAC_hash(secret, A(2) + seed) +
4268 * HMAC_hash(secret, A(3) + seed) + ...
4269 *
4270 * A(0) = seed
4271 * A(i) = HMAC_hash( secret, A(i-1) )
4272 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004273 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004274 * `HMAC_hash(secret, A(i) + seed)` from which the output
4275 * is currently extracted as `output_block`, while
4276 * `A(i) + seed` is stored in `Ai_with_seed`.
4277 *
4278 * Generating a new block means recalculating `Ai_with_seed`
4279 * from the A(i)-part of it, and afterwards recalculating
4280 * `output_block`.
4281 *
4282 * A(0) is computed at setup time.
4283 *
4284 */
4285
4286 psa_hmac_init_internal( &hmac );
4287
4288 /* We must distinguish the calculation of A(1) from those
4289 * of A(2) and higher, because A(0)=seed has a different
4290 * length than the other A(i). */
4291 if( tls12_prf->block_number == 1 )
4292 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004293 Ai = tls12_prf->Ai_with_seed + hash_length;
4294 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004295 }
4296 else
4297 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004298 Ai = tls12_prf->Ai_with_seed;
4299 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004300 }
4301
Hanno Becker3b339e22018-11-13 20:56:14 +00004302 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4303 status = psa_hmac_setup_internal( &hmac,
4304 tls12_prf->key,
4305 tls12_prf->key_len,
4306 hash_alg );
4307 if( status != PSA_SUCCESS )
4308 goto cleanup;
4309
4310 status = psa_hash_update( &hmac.hash_ctx,
4311 Ai, Ai_len );
4312 if( status != PSA_SUCCESS )
4313 goto cleanup;
4314
4315 status = psa_hmac_finish_internal( &hmac,
4316 tls12_prf->Ai_with_seed,
4317 hash_length );
4318 if( status != PSA_SUCCESS )
4319 goto cleanup;
4320
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004321 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4322 status = psa_hmac_setup_internal( &hmac,
4323 tls12_prf->key,
4324 tls12_prf->key_len,
4325 hash_alg );
4326 if( status != PSA_SUCCESS )
4327 goto cleanup;
4328
4329 status = psa_hash_update( &hmac.hash_ctx,
4330 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004331 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004332 if( status != PSA_SUCCESS )
4333 goto cleanup;
4334
4335 status = psa_hmac_finish_internal( &hmac,
4336 tls12_prf->output_block,
4337 hash_length );
4338 if( status != PSA_SUCCESS )
4339 goto cleanup;
4340
4341cleanup:
4342
4343 cleanup_status = psa_hmac_abort_internal( &hmac );
4344 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4345 status = cleanup_status;
4346
4347 return( status );
4348}
4349
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004350/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004351 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004352static psa_status_t psa_key_derivation_tls12_prf_read(
4353 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004354 psa_algorithm_t alg,
4355 uint8_t *output,
4356 size_t output_length )
4357{
4358 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4359 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4360 psa_status_t status;
4361
4362 while( output_length != 0 )
4363 {
4364 /* Copy what remains of the current block */
4365 uint8_t n = hash_length - tls12_prf->offset_in_block;
4366
4367 /* Check if we have fully processed the current block. */
4368 if( n == 0 )
4369 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004370 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004371 alg );
4372 if( status != PSA_SUCCESS )
4373 return( status );
4374
4375 continue;
4376 }
4377
4378 if( n > output_length )
4379 n = (uint8_t) output_length;
4380 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4381 n );
4382 output += n;
4383 output_length -= n;
4384 tls12_prf->offset_in_block += n;
4385 }
4386
4387 return( PSA_SUCCESS );
4388}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004389#endif /* MBEDTLS_MD_C */
4390
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004391psa_status_t psa_key_derivation_output_bytes( psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004392 uint8_t *output,
4393 size_t output_length )
4394{
4395 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004396 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004397
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004398 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004399 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004400 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004401 return PSA_ERROR_BAD_STATE;
4402 }
4403
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004404 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004405 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004406 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004407 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004408 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004409 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004410 goto exit;
4411 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004412 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004413 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004415 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004416 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4417 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004418 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004419 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004420 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004421 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004423
Gilles Peskine969c5d62019-01-16 15:53:06 +01004424 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004425 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004426 /* Initially, the capacity of a selection operation is always
4427 * the size of the buffer, i.e. `operation->ctx.buffer.size`,
Gilles Peskine211a4362018-10-25 22:22:31 +02004428 * abbreviated in this comment as `size`. When the remaining
4429 * capacity is `c`, the next bytes to serve start `c` bytes
4430 * from the end of the buffer, i.e. `size - c` from the
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004431 * beginning of the buffer. Since `operation->capacity` was just
Gilles Peskine211a4362018-10-25 22:22:31 +02004432 * decremented above, we need to serve the bytes from
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433 * `size - operation->capacity - output_length` to
4434 * `size - operation->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004435 size_t offset =
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004436 operation->ctx.buffer.size - operation->capacity - output_length;
4437 memcpy( output, operation->ctx.buffer.data + offset, output_length );
Gilles Peskine751d9652018-09-18 12:05:44 +02004438 status = PSA_SUCCESS;
4439 }
4440 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004441#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004442 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004443 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004444 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004445 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004446 output, output_length );
4447 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004448 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4449 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004450 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004451 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004452 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004453 output_length );
4454 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004455 else
4456#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004457 {
4458 return( PSA_ERROR_BAD_STATE );
4459 }
4460
4461exit:
4462 if( status != PSA_SUCCESS )
4463 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004464 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004465 * This allows us to differentiate between exhausted operations and
4466 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4467 * operations. */
4468 psa_algorithm_t alg = operation->alg;
4469 psa_key_derivation_abort( operation );
4470 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004471 memset( output, '!', output_length );
4472 }
4473 return( status );
4474}
4475
Gilles Peskine08542d82018-07-19 17:05:42 +02004476#if defined(MBEDTLS_DES_C)
4477static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4478{
4479 if( data_size >= 8 )
4480 mbedtls_des_key_set_parity( data );
4481 if( data_size >= 16 )
4482 mbedtls_des_key_set_parity( data + 8 );
4483 if( data_size >= 24 )
4484 mbedtls_des_key_set_parity( data + 16 );
4485}
4486#endif /* MBEDTLS_DES_C */
4487
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004488static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004489 psa_key_slot_t *slot,
4490 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004491 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004492{
4493 uint8_t *data = NULL;
4494 size_t bytes = PSA_BITS_TO_BYTES( bits );
4495 psa_status_t status;
4496
4497 if( ! key_type_is_raw_bytes( slot->type ) )
4498 return( PSA_ERROR_INVALID_ARGUMENT );
4499 if( bits % 8 != 0 )
4500 return( PSA_ERROR_INVALID_ARGUMENT );
4501 data = mbedtls_calloc( 1, bytes );
4502 if( data == NULL )
4503 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4504
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004505 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004506 if( status != PSA_SUCCESS )
4507 goto exit;
4508#if defined(MBEDTLS_DES_C)
4509 if( slot->type == PSA_KEY_TYPE_DES )
4510 psa_des_set_key_parity( data, bytes );
4511#endif /* MBEDTLS_DES_C */
4512 status = psa_import_key_into_slot( slot, data, bytes );
4513
4514exit:
4515 mbedtls_free( data );
4516 return( status );
4517}
4518
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004519psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004520 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004521 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004522{
4523 psa_status_t status;
4524 psa_key_slot_t *slot = NULL;
4525 status = psa_start_key_creation( attributes, handle, &slot );
4526 if( status == PSA_SUCCESS )
4527 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004528 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004529 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004530 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004531 }
4532 if( status == PSA_SUCCESS )
4533 status = psa_finish_key_creation( slot );
4534 if( status != PSA_SUCCESS )
4535 {
4536 psa_fail_key_creation( slot );
4537 *handle = 0;
4538 }
4539 return( status );
4540}
4541
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004542psa_status_t psa_generate_derived_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004543 psa_key_type_t type,
4544 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004545 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004546{
4547 uint8_t *data = NULL;
4548 size_t bytes = PSA_BITS_TO_BYTES( bits );
4549 psa_status_t status;
4550
4551 if( ! key_type_is_raw_bytes( type ) )
4552 return( PSA_ERROR_INVALID_ARGUMENT );
4553 if( bits % 8 != 0 )
4554 return( PSA_ERROR_INVALID_ARGUMENT );
4555 data = mbedtls_calloc( 1, bytes );
4556 if( data == NULL )
4557 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4558
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004559 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004560 if( status != PSA_SUCCESS )
4561 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004562#if defined(MBEDTLS_DES_C)
4563 if( type == PSA_KEY_TYPE_DES )
4564 psa_des_set_key_parity( data, bytes );
4565#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004566 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004567
4568exit:
4569 mbedtls_free( data );
4570 return( status );
4571}
4572
4573
4574
4575/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004576/* Key derivation */
4577/****************************************************************/
4578
Gilles Peskinea05219c2018-11-16 16:02:56 +01004579#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004580/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004581 * of the HKDF algorithm.
4582 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004583 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004584 * to potentially free embedded data structures and wipe confidential data.
4585 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004586static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004587 const uint8_t *secret,
4588 size_t secret_length,
4589 psa_algorithm_t hash_alg,
4590 const uint8_t *salt,
4591 size_t salt_length,
4592 const uint8_t *label,
4593 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004594{
4595 psa_status_t status;
4596 status = psa_hmac_setup_internal( &hkdf->hmac,
4597 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004598 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004599 if( status != PSA_SUCCESS )
4600 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004601 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004602 if( status != PSA_SUCCESS )
4603 return( status );
4604 status = psa_hmac_finish_internal( &hkdf->hmac,
4605 hkdf->prk,
4606 sizeof( hkdf->prk ) );
4607 if( status != PSA_SUCCESS )
4608 return( status );
4609 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4610 hkdf->block_number = 0;
4611 hkdf->info_length = label_length;
4612 if( label_length != 0 )
4613 {
4614 hkdf->info = mbedtls_calloc( 1, label_length );
4615 if( hkdf->info == NULL )
4616 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4617 memcpy( hkdf->info, label, label_length );
4618 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004619 hkdf->state = HKDF_STATE_KEYED;
4620 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004621 return( PSA_SUCCESS );
4622}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004623#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004624
Gilles Peskinea05219c2018-11-16 16:02:56 +01004625#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004626/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004627 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004628 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004629 * to potentially free embedded data structures and wipe confidential data.
4630 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004631static psa_status_t psa_key_derivation_tls12_prf_setup(
4632 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004633 const unsigned char *key,
4634 size_t key_len,
4635 psa_algorithm_t hash_alg,
4636 const uint8_t *salt,
4637 size_t salt_length,
4638 const uint8_t *label,
4639 size_t label_length )
4640{
4641 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004642 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4643 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004644
4645 tls12_prf->key = mbedtls_calloc( 1, key_len );
4646 if( tls12_prf->key == NULL )
4647 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4648 tls12_prf->key_len = key_len;
4649 memcpy( tls12_prf->key, key, key_len );
4650
Hanno Becker580fba12018-11-13 20:50:45 +00004651 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004652 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004653 if( overflow )
4654 return( PSA_ERROR_INVALID_ARGUMENT );
4655
4656 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4657 if( tls12_prf->Ai_with_seed == NULL )
4658 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4659 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4660
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004661 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4662 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004663 if( label_length != 0 )
4664 {
4665 memcpy( tls12_prf->Ai_with_seed + hash_length,
4666 label, label_length );
4667 }
4668
4669 if( salt_length != 0 )
4670 {
4671 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4672 salt, salt_length );
4673 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004674
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004675 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004676 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004677 tls12_prf->block_number = 0;
4678 tls12_prf->offset_in_block = hash_length;
4679
4680 return( PSA_SUCCESS );
4681}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004682
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004683/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004684static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4685 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004686 const unsigned char *psk,
4687 size_t psk_len,
4688 psa_algorithm_t hash_alg,
4689 const uint8_t *salt,
4690 size_t salt_length,
4691 const uint8_t *label,
4692 size_t label_length )
4693{
4694 psa_status_t status;
4695 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4696
4697 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4698 return( PSA_ERROR_INVALID_ARGUMENT );
4699
4700 /* Quoting RFC 4279, Section 2:
4701 *
4702 * The premaster secret is formed as follows: if the PSK is N octets
4703 * long, concatenate a uint16 with the value N, N zero octets, a second
4704 * uint16 with the value N, and the PSK itself.
4705 */
4706
4707 pms[0] = ( psk_len >> 8 ) & 0xff;
4708 pms[1] = ( psk_len >> 0 ) & 0xff;
4709 memset( pms + 2, 0, psk_len );
4710 pms[2 + psk_len + 0] = pms[0];
4711 pms[2 + psk_len + 1] = pms[1];
4712 memcpy( pms + 4 + psk_len, psk, psk_len );
4713
Gilles Peskinecbe66502019-05-16 16:59:18 +02004714 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004715 pms, 4 + 2 * psk_len,
4716 hash_alg,
4717 salt, salt_length,
4718 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004719
Gilles Peskine3f108122018-12-07 18:14:53 +01004720 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004721 return( status );
4722}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004723#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004724
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004725/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004726 * to potentially free embedded data structures and wipe confidential data.
4727 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004728static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004729 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004730 const uint8_t *secret, size_t secret_length,
4731 psa_algorithm_t alg,
4732 const uint8_t *salt, size_t salt_length,
4733 const uint8_t *label, size_t label_length,
4734 size_t capacity )
4735{
4736 psa_status_t status;
4737 size_t max_capacity;
4738
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739 /* Set operation->alg even on failure so that abort knows what to do. */
4740 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004741
Gilles Peskine751d9652018-09-18 12:05:44 +02004742 if( alg == PSA_ALG_SELECT_RAW )
4743 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004744 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004745 if( salt_length != 0 )
4746 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004747 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004748 if( label_length != 0 )
4749 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 operation->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4751 if( operation->ctx.buffer.data == NULL )
Gilles Peskine751d9652018-09-18 12:05:44 +02004752 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004753 memcpy( operation->ctx.buffer.data, secret, secret_length );
4754 operation->ctx.buffer.size = secret_length;
Gilles Peskine751d9652018-09-18 12:05:44 +02004755 max_capacity = secret_length;
4756 status = PSA_SUCCESS;
4757 }
4758 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004759#if defined(MBEDTLS_MD_C)
4760 if( PSA_ALG_IS_HKDF( alg ) )
4761 {
4762 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4763 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4764 if( hash_size == 0 )
4765 return( PSA_ERROR_NOT_SUPPORTED );
4766 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004767 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004768 secret, secret_length,
4769 hash_alg,
4770 salt, salt_length,
4771 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004772 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004773 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4774 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4775 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004776 {
4777 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4778 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4779
4780 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4781 if( hash_alg != PSA_ALG_SHA_256 &&
4782 hash_alg != PSA_ALG_SHA_384 )
4783 {
4784 return( PSA_ERROR_NOT_SUPPORTED );
4785 }
4786
4787 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004788
4789 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4790 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004791 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004792 secret, secret_length,
4793 hash_alg, salt, salt_length,
4794 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004795 }
4796 else
4797 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004798 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004800 secret, secret_length,
4801 hash_alg, salt, salt_length,
4802 label, label_length );
4803 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004804 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004805 else
4806#endif
4807 {
4808 return( PSA_ERROR_NOT_SUPPORTED );
4809 }
4810
4811 if( status != PSA_SUCCESS )
4812 return( status );
4813
4814 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004815 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004816 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004817 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004818 else
4819 return( PSA_ERROR_INVALID_ARGUMENT );
4820
4821 return( PSA_SUCCESS );
4822}
4823
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004825 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004826 psa_algorithm_t alg,
4827 const uint8_t *salt,
4828 size_t salt_length,
4829 const uint8_t *label,
4830 size_t label_length,
4831 size_t capacity )
4832{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004833 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004834 psa_status_t status;
4835
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004837 return( PSA_ERROR_BAD_STATE );
4838
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004839 /* Make sure that alg is a key derivation algorithm. This prevents
4840 * key selection algorithms, which psa_key_derivation_internal
4841 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004842 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4843 return( PSA_ERROR_INVALID_ARGUMENT );
4844
Gilles Peskinec5487a82018-12-03 18:08:14 +01004845 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004846 if( status != PSA_SUCCESS )
4847 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004848
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004849 if( slot->type != PSA_KEY_TYPE_DERIVE )
4850 return( PSA_ERROR_INVALID_ARGUMENT );
4851
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004853 slot->data.raw.data,
4854 slot->data.raw.bytes,
4855 alg,
4856 salt, salt_length,
4857 label, label_length,
4858 capacity );
4859 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004860 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004861 return( status );
4862}
4863
Gilles Peskine969c5d62019-01-16 15:53:06 +01004864static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004866 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004867{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004868 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004869#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004870 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4871 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4872 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004873 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004874 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004875 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4876 if( hash_size == 0 )
4877 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004878 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4879 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004880 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004881 {
4882 return( PSA_ERROR_NOT_SUPPORTED );
4883 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004884 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004885 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004886 }
4887#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004888 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004889 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004890}
4891
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004892psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004893 psa_algorithm_t alg )
4894{
4895 psa_status_t status;
4896
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004897 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004898 return( PSA_ERROR_BAD_STATE );
4899
Gilles Peskine6843c292019-01-18 16:44:49 +01004900 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4901 return( PSA_ERROR_INVALID_ARGUMENT );
4902 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004903 {
4904 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004905 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004906 }
4907 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4908 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004909 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004910 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004911 else
4912 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004913
4914 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004915 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004916 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004917}
4918
4919#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004920static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004921 psa_algorithm_t hash_alg,
4922 psa_key_derivation_step_t step,
4923 const uint8_t *data,
4924 size_t data_length )
4925{
4926 psa_status_t status;
4927 switch( step )
4928 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004929 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004930 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004931 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004932 status = psa_hmac_setup_internal( &hkdf->hmac,
4933 data, data_length,
4934 hash_alg );
4935 if( status != PSA_SUCCESS )
4936 return( status );
4937 hkdf->state = HKDF_STATE_STARTED;
4938 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004939 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004940 /* If no salt was provided, use an empty salt. */
4941 if( hkdf->state == HKDF_STATE_INIT )
4942 {
4943 status = psa_hmac_setup_internal( &hkdf->hmac,
4944 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004945 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004946 if( status != PSA_SUCCESS )
4947 return( status );
4948 hkdf->state = HKDF_STATE_STARTED;
4949 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004950 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004951 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004952 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4953 data, data_length );
4954 if( status != PSA_SUCCESS )
4955 return( status );
4956 status = psa_hmac_finish_internal( &hkdf->hmac,
4957 hkdf->prk,
4958 sizeof( hkdf->prk ) );
4959 if( status != PSA_SUCCESS )
4960 return( status );
4961 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4962 hkdf->block_number = 0;
4963 hkdf->state = HKDF_STATE_KEYED;
4964 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004965 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004966 if( hkdf->state == HKDF_STATE_OUTPUT )
4967 return( PSA_ERROR_BAD_STATE );
4968 if( hkdf->info_set )
4969 return( PSA_ERROR_BAD_STATE );
4970 hkdf->info_length = data_length;
4971 if( data_length != 0 )
4972 {
4973 hkdf->info = mbedtls_calloc( 1, data_length );
4974 if( hkdf->info == NULL )
4975 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4976 memcpy( hkdf->info, data, data_length );
4977 }
4978 hkdf->info_set = 1;
4979 return( PSA_SUCCESS );
4980 default:
4981 return( PSA_ERROR_INVALID_ARGUMENT );
4982 }
4983}
4984#endif /* MBEDTLS_MD_C */
4985
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004986static psa_status_t psa_key_derivation_input_raw(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004987 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004988 psa_key_derivation_step_t step,
4989 const uint8_t *data,
4990 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004991{
4992 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004993 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004994
Gilles Peskine969c5d62019-01-16 15:53:06 +01004995 if( kdf_alg == PSA_ALG_SELECT_RAW )
4996 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004997 if( operation->capacity != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004998 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004999 operation->ctx.buffer.data = mbedtls_calloc( 1, data_length );
5000 if( operation->ctx.buffer.data == NULL )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005001 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005002 memcpy( operation->ctx.buffer.data, data, data_length );
5003 operation->ctx.buffer.size = data_length;
5004 operation->capacity = data_length;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005005 status = PSA_SUCCESS;
5006 }
5007 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005008#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005009 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005010 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005011 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005012 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005013 step, data, data_length );
5014 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005015 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005016#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005017#if defined(MBEDTLS_MD_C)
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005018 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005019 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005020 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005021 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02005022 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005023 status = PSA_ERROR_NOT_SUPPORTED;
5024 }
5025 else
5026#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005027 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005028 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005029 return( PSA_ERROR_BAD_STATE );
5030 }
5031
5032 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005033 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005034 return( status );
5035}
5036
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005037psa_status_t psa_key_derivation_input_bytes( psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005038 psa_key_derivation_step_t step,
5039 const uint8_t *data,
5040 size_t data_length )
5041{
5042 switch( step )
5043 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005044 case PSA_KEY_DERIVATION_INPUT_LABEL:
5045 case PSA_KEY_DERIVATION_INPUT_SALT:
5046 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005047 return( psa_key_derivation_input_raw( operation, step,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005048 data, data_length ) );
5049 default:
5050 return( PSA_ERROR_INVALID_ARGUMENT );
5051 }
5052}
5053
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005054psa_status_t psa_key_derivation_input_key( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005055 psa_key_derivation_step_t step,
5056 psa_key_handle_t handle )
5057{
5058 psa_key_slot_t *slot;
5059 psa_status_t status;
5060 status = psa_get_key_from_slot( handle, &slot,
5061 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005063 if( status != PSA_SUCCESS )
5064 return( status );
5065 if( slot->type != PSA_KEY_TYPE_DERIVE )
5066 return( PSA_ERROR_INVALID_ARGUMENT );
5067 /* Don't allow a key to be used as an input that is usually public.
5068 * This is debatable. It's ok from a cryptographic perspective to
5069 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005070 * the material should be dedicated to a particular input step,
5071 * otherwise this may allow the key to be used in an unintended way
5072 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005073 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005074 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005075 return( psa_key_derivation_input_raw( operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005076 step,
5077 slot->data.raw.data,
5078 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005079}
5080
Gilles Peskineea0fb492018-07-12 17:17:20 +02005081
5082
5083/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005084/* Key agreement */
5085/****************************************************************/
5086
Gilles Peskinea05219c2018-11-16 16:02:56 +01005087#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005088static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5089 size_t peer_key_length,
5090 const mbedtls_ecp_keypair *our_key,
5091 uint8_t *shared_secret,
5092 size_t shared_secret_size,
5093 size_t *shared_secret_length )
5094{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005095 mbedtls_ecp_keypair *their_key = NULL;
5096 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005097 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005098 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005099
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005100 status = psa_import_ec_public_key(
5101 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5102 peer_key, peer_key_length,
5103 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005104 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005105 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005106
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005107 status = mbedtls_to_psa_error(
5108 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5109 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005110 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005111 status = mbedtls_to_psa_error(
5112 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5113 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005114 goto exit;
5115
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005116 status = mbedtls_to_psa_error(
5117 mbedtls_ecdh_calc_secret( &ecdh,
5118 shared_secret_length,
5119 shared_secret, shared_secret_size,
5120 mbedtls_ctr_drbg_random,
5121 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005122
5123exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005124 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005125 mbedtls_ecp_keypair_free( their_key );
5126 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005127 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005128}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005129#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005130
Gilles Peskine01d718c2018-09-18 12:01:02 +02005131#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5132
Gilles Peskine0216fe12019-04-11 21:23:21 +02005133static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5134 psa_key_slot_t *private_key,
5135 const uint8_t *peer_key,
5136 size_t peer_key_length,
5137 uint8_t *shared_secret,
5138 size_t shared_secret_size,
5139 size_t *shared_secret_length )
5140{
5141 switch( alg )
5142 {
5143#if defined(MBEDTLS_ECDH_C)
5144 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005145 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005146 return( PSA_ERROR_INVALID_ARGUMENT );
5147 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5148 private_key->data.ecp,
5149 shared_secret, shared_secret_size,
5150 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005151#endif /* MBEDTLS_ECDH_C */
5152 default:
5153 (void) private_key;
5154 (void) peer_key;
5155 (void) peer_key_length;
5156 (void) shared_secret;
5157 (void) shared_secret_size;
5158 (void) shared_secret_length;
5159 return( PSA_ERROR_NOT_SUPPORTED );
5160 }
5161}
5162
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005163/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005164 * to potentially free embedded data structures and wipe confidential data.
5165 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005166static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005167 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005168 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005169 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005170 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005171{
5172 psa_status_t status;
5173 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5174 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005175 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005176
5177 /* Step 1: run the secret agreement algorithm to generate the shared
5178 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005179 status = psa_key_agreement_raw_internal( ka_alg,
5180 private_key,
5181 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005182 shared_secret,
5183 sizeof( shared_secret ),
5184 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005185 if( status != PSA_SUCCESS )
5186 goto exit;
5187
5188 /* Step 2: set up the key derivation to generate key material from
5189 * the shared secret. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190 status = psa_key_derivation_input_raw( operation, step,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005191 shared_secret, shared_secret_length );
5192
Gilles Peskine01d718c2018-09-18 12:01:02 +02005193exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005194 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005195 return( status );
5196}
5197
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005198psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005199 psa_key_derivation_step_t step,
5200 psa_key_handle_t private_key,
5201 const uint8_t *peer_key,
5202 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005203{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005204 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005205 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005206 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005207 return( PSA_ERROR_INVALID_ARGUMENT );
5208 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005210 if( status != PSA_SUCCESS )
5211 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005213 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005214 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005215 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005217 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005218}
5219
Gilles Peskinebe697d82019-05-16 18:00:41 +02005220psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5221 psa_key_handle_t private_key,
5222 const uint8_t *peer_key,
5223 size_t peer_key_length,
5224 uint8_t *output,
5225 size_t output_size,
5226 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005227{
5228 psa_key_slot_t *slot;
5229 psa_status_t status;
5230
5231 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5232 {
5233 status = PSA_ERROR_INVALID_ARGUMENT;
5234 goto exit;
5235 }
5236 status = psa_get_key_from_slot( private_key, &slot,
5237 PSA_KEY_USAGE_DERIVE, alg );
5238 if( status != PSA_SUCCESS )
5239 goto exit;
5240
5241 status = psa_key_agreement_raw_internal( alg, slot,
5242 peer_key, peer_key_length,
5243 output, output_size,
5244 output_length );
5245
5246exit:
5247 if( status != PSA_SUCCESS )
5248 {
5249 /* If an error happens and is not handled properly, the output
5250 * may be used as a key to protect sensitive data. Arrange for such
5251 * a key to be random, which is likely to result in decryption or
5252 * verification errors. This is better than filling the buffer with
5253 * some constant data such as zeros, which would result in the data
5254 * being protected with a reproducible, easily knowable key.
5255 */
5256 psa_generate_random( output, output_size );
5257 *output_length = output_size;
5258 }
5259 return( status );
5260}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005261
5262
5263/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005264/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005265/****************************************************************/
5266
5267psa_status_t psa_generate_random( uint8_t *output,
5268 size_t output_size )
5269{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005270 int ret;
5271 GUARD_MODULE_INITIALIZED;
5272
5273 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005274 return( mbedtls_to_psa_error( ret ) );
5275}
5276
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005277#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5278#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005279
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005280psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5281 size_t seed_size )
5282{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005283 if( global_data.initialized )
5284 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005285
5286 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5287 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5288 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5289 return( PSA_ERROR_INVALID_ARGUMENT );
5290
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005291 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005292}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005293#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005294
Gilles Peskinee56e8782019-04-26 17:34:02 +02005295#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5296static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5297 size_t domain_parameters_size,
5298 int *exponent )
5299{
5300 size_t i;
5301 uint32_t acc = 0;
5302
5303 if( domain_parameters_size == 0 )
5304 {
5305 *exponent = 65537;
5306 return( PSA_SUCCESS );
5307 }
5308
5309 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5310 * support values that fit in a 32-bit integer, which is larger than
5311 * int on just about every platform anyway. */
5312 if( domain_parameters_size > sizeof( acc ) )
5313 return( PSA_ERROR_NOT_SUPPORTED );
5314 for( i = 0; i < domain_parameters_size; i++ )
5315 acc = ( acc << 8 ) | domain_parameters[i];
5316 if( acc > INT_MAX )
5317 return( PSA_ERROR_NOT_SUPPORTED );
5318 *exponent = acc;
5319 return( PSA_SUCCESS );
5320}
5321#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5322
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005323static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005324 psa_key_slot_t *slot, size_t bits,
5325 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005326{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005327 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005328
Gilles Peskinee56e8782019-04-26 17:34:02 +02005329 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005330 return( PSA_ERROR_INVALID_ARGUMENT );
5331
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005332 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005333 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005334 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005335 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005336 if( status != PSA_SUCCESS )
5337 return( status );
5338 status = psa_generate_random( slot->data.raw.data,
5339 slot->data.raw.bytes );
5340 if( status != PSA_SUCCESS )
5341 {
5342 mbedtls_free( slot->data.raw.data );
5343 return( status );
5344 }
5345#if defined(MBEDTLS_DES_C)
5346 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005347 psa_des_set_key_parity( slot->data.raw.data,
5348 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005349#endif /* MBEDTLS_DES_C */
5350 }
5351 else
5352
5353#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005354 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005355 {
5356 mbedtls_rsa_context *rsa;
5357 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005358 int exponent;
5359 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005360 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5361 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005362 /* Accept only byte-aligned keys, for the same reasons as
5363 * in psa_import_rsa_key(). */
5364 if( bits % 8 != 0 )
5365 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005366 status = psa_read_rsa_exponent( domain_parameters,
5367 domain_parameters_size,
5368 &exponent );
5369 if( status != PSA_SUCCESS )
5370 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005371 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5372 if( rsa == NULL )
5373 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5374 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5375 ret = mbedtls_rsa_gen_key( rsa,
5376 mbedtls_ctr_drbg_random,
5377 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005378 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005379 exponent );
5380 if( ret != 0 )
5381 {
5382 mbedtls_rsa_free( rsa );
5383 mbedtls_free( rsa );
5384 return( mbedtls_to_psa_error( ret ) );
5385 }
5386 slot->data.rsa = rsa;
5387 }
5388 else
5389#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5390
5391#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005392 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005393 {
5394 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5395 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5396 const mbedtls_ecp_curve_info *curve_info =
5397 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5398 mbedtls_ecp_keypair *ecp;
5399 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005400 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005401 return( PSA_ERROR_NOT_SUPPORTED );
5402 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5403 return( PSA_ERROR_NOT_SUPPORTED );
5404 if( curve_info->bit_size != bits )
5405 return( PSA_ERROR_INVALID_ARGUMENT );
5406 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5407 if( ecp == NULL )
5408 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5409 mbedtls_ecp_keypair_init( ecp );
5410 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5411 mbedtls_ctr_drbg_random,
5412 &global_data.ctr_drbg );
5413 if( ret != 0 )
5414 {
5415 mbedtls_ecp_keypair_free( ecp );
5416 mbedtls_free( ecp );
5417 return( mbedtls_to_psa_error( ret ) );
5418 }
5419 slot->data.ecp = ecp;
5420 }
5421 else
5422#endif /* MBEDTLS_ECP_C */
5423
5424 return( PSA_ERROR_NOT_SUPPORTED );
5425
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005426 return( PSA_SUCCESS );
5427}
5428
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005429psa_status_t psa_generate_key_to_handle( psa_key_handle_t handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005430 psa_key_type_t type,
5431 size_t bits,
5432 const void *extra,
5433 size_t extra_size )
5434{
5435 psa_key_slot_t *slot;
5436 psa_status_t status;
5437
Gilles Peskinee56e8782019-04-26 17:34:02 +02005438#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5439 /* The old public exponent encoding is no longer supported. */
5440 if( extra_size != 0 )
5441 return( PSA_ERROR_NOT_SUPPORTED );
5442#endif
5443
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005444 status = psa_get_empty_key_slot( handle, &slot );
5445 if( status != PSA_SUCCESS )
5446 return( status );
5447
Gilles Peskine12313cd2018-06-20 00:20:32 +02005448 slot->type = type;
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005449 status = psa_generate_key_internal( slot, bits, extra, extra_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005450 if( status != PSA_SUCCESS )
5451 slot->type = 0;
Darryl Green0c6575a2018-11-07 16:05:30 +00005452
5453#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
5454 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
5455 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01005456 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005457 }
5458#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5459
5460 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005461}
5462
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005463psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005464 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005465{
5466 psa_status_t status;
5467 psa_key_slot_t *slot = NULL;
5468 status = psa_start_key_creation( attributes, handle, &slot );
5469 if( status == PSA_SUCCESS )
5470 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005471 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005472 slot, attributes->bits,
5473 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005474 }
5475 if( status == PSA_SUCCESS )
5476 status = psa_finish_key_creation( slot );
5477 if( status != PSA_SUCCESS )
5478 {
5479 psa_fail_key_creation( slot );
5480 *handle = 0;
5481 }
5482 return( status );
5483}
5484
5485
Gilles Peskine05d69892018-06-19 22:00:52 +02005486
5487/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005488/* Module setup */
5489/****************************************************************/
5490
Gilles Peskine5e769522018-11-20 21:59:56 +01005491psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5492 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5493 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5494{
5495 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5496 return( PSA_ERROR_BAD_STATE );
5497 global_data.entropy_init = entropy_init;
5498 global_data.entropy_free = entropy_free;
5499 return( PSA_SUCCESS );
5500}
5501
Gilles Peskinee59236f2018-01-27 23:32:46 +01005502void mbedtls_psa_crypto_free( void )
5503{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005504 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005505 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5506 {
5507 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005508 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005509 }
5510 /* Wipe all remaining data, including configuration.
5511 * In particular, this sets all state indicator to the value
5512 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005513 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005514}
5515
5516psa_status_t psa_crypto_init( void )
5517{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005518 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005519 const unsigned char drbg_seed[] = "PSA";
5520
Gilles Peskinec6b69072018-11-20 21:42:52 +01005521 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005522 if( global_data.initialized != 0 )
5523 return( PSA_SUCCESS );
5524
Gilles Peskine5e769522018-11-20 21:59:56 +01005525 /* Set default configuration if
5526 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5527 if( global_data.entropy_init == NULL )
5528 global_data.entropy_init = mbedtls_entropy_init;
5529 if( global_data.entropy_free == NULL )
5530 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005531
Gilles Peskinec6b69072018-11-20 21:42:52 +01005532 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005533 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005534 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005535 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005536 status = mbedtls_to_psa_error(
5537 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5538 mbedtls_entropy_func,
5539 &global_data.entropy,
5540 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5541 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005542 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005543 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005544
Gilles Peskine66fb1262018-12-10 16:29:04 +01005545 status = psa_initialize_key_slots( );
5546 if( status != PSA_SUCCESS )
5547 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005548
5549 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005550 global_data.initialized = 1;
5551
Gilles Peskinee59236f2018-01-27 23:32:46 +01005552exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005553 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005554 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005555 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005556}
5557
5558#endif /* MBEDTLS_PSA_CRYPTO_C */