blob: 6f374b12b8f87adf0e29eb1838d1dd467722e4cb [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
5 * Copyright (C) 2018, ARM Limited, All Rights Reserved
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * This file is part of mbed TLS (https://tls.mbed.org)
21 */
22
Gilles Peskinedb09ef62020-06-03 01:43:33 +020023#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010024
25#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030026
itayzafrir7723ab12019-02-14 10:28:02 +020027#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010028#include "psa/crypto.h"
29
Gilles Peskine039b90c2018-12-07 18:24:41 +010030#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010031#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
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 Peskineb46bef22019-07-30 21:32:04 +020040#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041#include <stdlib.h>
42#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020044#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020050#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000051#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020052#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/blowfish.h"
54#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020055#include "mbedtls/chacha20.h"
56#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/cipher.h"
58#include "mbedtls/ccm.h"
59#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020062#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010063#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010064#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/error.h"
66#include "mbedtls/gcm.h"
67#include "mbedtls/md2.h"
68#include "mbedtls/md4.h"
69#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010070#include "mbedtls/md.h"
71#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010072#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010073#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010074#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000075#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010077#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/sha1.h"
79#include "mbedtls/sha256.h"
80#include "mbedtls/sha512.h"
81#include "mbedtls/xtea.h"
82
Gilles Peskine996deb12018-08-01 15:45:45 +020083#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
84
Gilles Peskine9ef733f2018-02-07 21:05:37 +010085/* constant-time buffer comparison */
86static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
87{
88 size_t i;
89 unsigned char diff = 0;
90
91 for( i = 0; i < n; i++ )
92 diff |= a[i] ^ b[i];
93
94 return( diff );
95}
96
97
98
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010099/****************************************************************/
100/* Global data, support functions and library management */
101/****************************************************************/
102
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200103static int key_type_is_raw_bytes( psa_key_type_t type )
104{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200105 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106}
107
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100108/* Values for psa_global_data_t::rng_state */
109#define RNG_NOT_INITIALIZED 0
110#define RNG_INITIALIZED 1
111#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112
Gilles Peskine2d277862018-06-18 15:41:12 +0200113typedef struct
114{
Gilles Peskine5e769522018-11-20 21:59:56 +0100115 void (* entropy_init )( mbedtls_entropy_context *ctx );
116 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100117 mbedtls_entropy_context entropy;
118 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100119 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100120 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100121} psa_global_data_t;
122
123static psa_global_data_t global_data;
124
itayzafrir0adf0fc2018-09-06 16:24:41 +0300125#define GUARD_MODULE_INITIALIZED \
126 if( global_data.initialized == 0 ) \
127 return( PSA_ERROR_BAD_STATE );
128
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129static psa_status_t mbedtls_to_psa_error( int ret )
130{
Gilles Peskinea5905292018-02-07 20:59:33 +0100131 /* If there's both a high-level code and low-level code, dispatch on
132 * the high-level code. */
133 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100134 {
135 case 0:
136 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100137
138 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
139 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
140 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
141 return( PSA_ERROR_NOT_SUPPORTED );
142 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
145 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
Gilles Peskine9a944802018-06-21 09:35:35 +0200148 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
149 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
150 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
151 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
152 case MBEDTLS_ERR_ASN1_INVALID_DATA:
153 return( PSA_ERROR_INVALID_ARGUMENT );
154 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
155 return( PSA_ERROR_INSUFFICIENT_MEMORY );
156 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
157 return( PSA_ERROR_BUFFER_TOO_SMALL );
158
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000160 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
162 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
163#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
165 return( PSA_ERROR_NOT_SUPPORTED );
166 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
167 return( PSA_ERROR_HARDWARE_FAILURE );
168
Jaeden Amero93e21112019-02-20 13:57:28 +0000169#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000170 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
172 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
173#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
175 return( PSA_ERROR_NOT_SUPPORTED );
176 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
179 case MBEDTLS_ERR_CCM_BAD_INPUT:
180 return( PSA_ERROR_INVALID_ARGUMENT );
181 case MBEDTLS_ERR_CCM_AUTH_FAILED:
182 return( PSA_ERROR_INVALID_SIGNATURE );
183 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
187 return( PSA_ERROR_INVALID_ARGUMENT );
188
189 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
190 return( PSA_ERROR_BAD_STATE );
191 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
192 return( PSA_ERROR_INVALID_SIGNATURE );
193
Gilles Peskinea5905292018-02-07 20:59:33 +0100194 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
195 return( PSA_ERROR_NOT_SUPPORTED );
196 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
197 return( PSA_ERROR_INVALID_ARGUMENT );
198 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
199 return( PSA_ERROR_INSUFFICIENT_MEMORY );
200 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
201 return( PSA_ERROR_INVALID_PADDING );
202 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
203 return( PSA_ERROR_BAD_STATE );
204 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200207 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
211 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
215 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
216 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
217 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
218 return( PSA_ERROR_NOT_SUPPORTED );
219 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
220 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
221
222 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
223 return( PSA_ERROR_NOT_SUPPORTED );
224 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
225 return( PSA_ERROR_HARDWARE_FAILURE );
226
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
228 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
229 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
232 case MBEDTLS_ERR_GCM_AUTH_FAILED:
233 return( PSA_ERROR_INVALID_SIGNATURE );
234 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200235 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
237 return( PSA_ERROR_HARDWARE_FAILURE );
238
239 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
244 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MD_ALLOC_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
250 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
251 return( PSA_ERROR_STORAGE_FAILURE );
252 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
256 return( PSA_ERROR_STORAGE_FAILURE );
257 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
262 return( PSA_ERROR_BUFFER_TOO_SMALL );
263 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
270 return( PSA_ERROR_INSUFFICIENT_MEMORY );
271
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
275 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100278 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100279 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
280 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
283 return( PSA_ERROR_NOT_SUPPORTED );
284 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
285 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
286 return( PSA_ERROR_NOT_PERMITTED );
287 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_PK_INVALID_ALG:
290 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
291 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
292 return( PSA_ERROR_NOT_SUPPORTED );
293 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
294 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
296 return( PSA_ERROR_HARDWARE_FAILURE );
297
Gilles Peskineff2d2002019-05-06 15:26:23 +0200298 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
301 return( PSA_ERROR_NOT_SUPPORTED );
302
Gilles Peskinea5905292018-02-07 20:59:33 +0100303 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
304 return( PSA_ERROR_HARDWARE_FAILURE );
305
306 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
307 return( PSA_ERROR_INVALID_ARGUMENT );
308 case MBEDTLS_ERR_RSA_INVALID_PADDING:
309 return( PSA_ERROR_INVALID_PADDING );
310 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
311 return( PSA_ERROR_HARDWARE_FAILURE );
312 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
313 return( PSA_ERROR_INVALID_ARGUMENT );
314 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
315 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200316 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
318 return( PSA_ERROR_INVALID_SIGNATURE );
319 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
320 return( PSA_ERROR_BUFFER_TOO_SMALL );
321 case MBEDTLS_ERR_RSA_RNG_FAILED:
322 return( PSA_ERROR_INSUFFICIENT_MEMORY );
323 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
324 return( PSA_ERROR_NOT_SUPPORTED );
325 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
333 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
334 return( PSA_ERROR_INVALID_ARGUMENT );
335 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
337
itayzafrir5c753392018-05-08 11:18:38 +0300338 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300339 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300340 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
342 return( PSA_ERROR_BUFFER_TOO_SMALL );
343 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
344 return( PSA_ERROR_NOT_SUPPORTED );
345 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
346 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
347 return( PSA_ERROR_INVALID_SIGNATURE );
348 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
349 return( PSA_ERROR_INSUFFICIENT_MEMORY );
350 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
351 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000352 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
353 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100375mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100376 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200377{
378 switch( curve )
379 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100380 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100381 switch( byte_length )
382 {
383 case PSA_BITS_TO_BYTES( 192 ):
384 return( MBEDTLS_ECP_DP_SECP192R1 );
385 case PSA_BITS_TO_BYTES( 224 ):
386 return( MBEDTLS_ECP_DP_SECP224R1 );
387 case PSA_BITS_TO_BYTES( 256 ):
388 return( MBEDTLS_ECP_DP_SECP256R1 );
389 case PSA_BITS_TO_BYTES( 384 ):
390 return( MBEDTLS_ECP_DP_SECP384R1 );
391 case PSA_BITS_TO_BYTES( 521 ):
392 return( MBEDTLS_ECP_DP_SECP521R1 );
393 default:
394 return( MBEDTLS_ECP_DP_NONE );
395 }
396 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100397
Paul Elliott8ff510a2020-06-02 17:19:28 +0100398 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100399 switch( byte_length )
400 {
401 case PSA_BITS_TO_BYTES( 256 ):
402 return( MBEDTLS_ECP_DP_BP256R1 );
403 case PSA_BITS_TO_BYTES( 384 ):
404 return( MBEDTLS_ECP_DP_BP384R1 );
405 case PSA_BITS_TO_BYTES( 512 ):
406 return( MBEDTLS_ECP_DP_BP512R1 );
407 default:
408 return( MBEDTLS_ECP_DP_NONE );
409 }
410 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100411
Paul Elliott8ff510a2020-06-02 17:19:28 +0100412 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100413 switch( byte_length )
414 {
415 case PSA_BITS_TO_BYTES( 255 ):
416 return( MBEDTLS_ECP_DP_CURVE25519 );
417 case PSA_BITS_TO_BYTES( 448 ):
418 return( MBEDTLS_ECP_DP_CURVE448 );
419 default:
420 return( MBEDTLS_ECP_DP_NONE );
421 }
422 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100423
Paul Elliott8ff510a2020-06-02 17:19:28 +0100424 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100425 switch( byte_length )
426 {
427 case PSA_BITS_TO_BYTES( 192 ):
428 return( MBEDTLS_ECP_DP_SECP192K1 );
429 case PSA_BITS_TO_BYTES( 224 ):
430 return( MBEDTLS_ECP_DP_SECP224K1 );
431 case PSA_BITS_TO_BYTES( 256 ):
432 return( MBEDTLS_ECP_DP_SECP256K1 );
433 default:
434 return( MBEDTLS_ECP_DP_NONE );
435 }
436 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100437
Gilles Peskine12313cd2018-06-20 00:20:32 +0200438 default:
439 return( MBEDTLS_ECP_DP_NONE );
440 }
441}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100442#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200443
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200444static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
445 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446{
447 /* Check that the bit size is acceptable for the key type */
448 switch( type )
449 {
450 case PSA_KEY_TYPE_RAW_DATA:
451#if defined(MBEDTLS_MD_C)
452 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200454 case PSA_KEY_TYPE_DERIVE:
455 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200456#if defined(MBEDTLS_AES_C)
457 case PSA_KEY_TYPE_AES:
458 if( bits != 128 && bits != 192 && bits != 256 )
459 return( PSA_ERROR_INVALID_ARGUMENT );
460 break;
461#endif
462#if defined(MBEDTLS_CAMELLIA_C)
463 case PSA_KEY_TYPE_CAMELLIA:
464 if( bits != 128 && bits != 192 && bits != 256 )
465 return( PSA_ERROR_INVALID_ARGUMENT );
466 break;
467#endif
468#if defined(MBEDTLS_DES_C)
469 case PSA_KEY_TYPE_DES:
470 if( bits != 64 && bits != 128 && bits != 192 )
471 return( PSA_ERROR_INVALID_ARGUMENT );
472 break;
473#endif
474#if defined(MBEDTLS_ARC4_C)
475 case PSA_KEY_TYPE_ARC4:
476 if( bits < 8 || bits > 2048 )
477 return( PSA_ERROR_INVALID_ARGUMENT );
478 break;
479#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200480#if defined(MBEDTLS_CHACHA20_C)
481 case PSA_KEY_TYPE_CHACHA20:
482 if( bits != 256 )
483 return( PSA_ERROR_INVALID_ARGUMENT );
484 break;
485#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200486 default:
487 return( PSA_ERROR_NOT_SUPPORTED );
488 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200489 if( bits % 8 != 0 )
490 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 return( PSA_SUCCESS );
493}
494
Steven Cooremana01795d2020-07-24 22:48:15 +0200495#if defined(MBEDTLS_RSA_C)
496
497#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100498/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
499 * that are not a multiple of 8) well. For example, there is only
500 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
501 * way to return the exact bit size of a key.
502 * To keep things simple, reject non-byte-aligned key sizes. */
503static psa_status_t psa_check_rsa_key_byte_aligned(
504 const mbedtls_rsa_context *rsa )
505{
506 mbedtls_mpi n;
507 psa_status_t status;
508 mbedtls_mpi_init( &n );
509 status = mbedtls_to_psa_error(
510 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
511 if( status == PSA_SUCCESS )
512 {
513 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
514 status = PSA_ERROR_NOT_SUPPORTED;
515 }
516 mbedtls_mpi_free( &n );
517 return( status );
518}
Steven Cooremana01795d2020-07-24 22:48:15 +0200519#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100520
Steven Cooremana01795d2020-07-24 22:48:15 +0200521/** Load the contents of a key slot into an internal RSA representation
522 *
523 * \param[in] slot The slot from which to load the representation
524 * \param[out] rsa The internal RSA representation to hold the key. Must be
525 * allocated and initialized. If it already holds a
526 * different key, it will be overwritten and cause a memory
527 * leak.
528 */
529static psa_status_t psa_load_rsa_representation( const psa_key_slot_t *slot,
530 mbedtls_rsa_context *rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200531{
Steven Cooremana01795d2020-07-24 22:48:15 +0200532#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000533 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200534 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000535 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200536 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000537
538 /* Parse the data. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200539 if( PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000540 status = mbedtls_to_psa_error(
Steven Cooremana01795d2020-07-24 22:48:15 +0200541 mbedtls_pk_parse_key( &ctx, slot->data.key.data, slot->data.key.bytes, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200542 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
Steven Cooremana01795d2020-07-24 22:48:15 +0200544 mbedtls_pk_parse_public_key( &ctx, slot->data.key.data, slot->data.key.bytes ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000545 if( status != PSA_SUCCESS )
546 goto exit;
547
548 /* We have something that the pkparse module recognizes. If it is a
549 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200550 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000552 status = PSA_ERROR_INVALID_ARGUMENT;
553 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556 /* 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. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200559 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000560 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
561 {
562 status = PSA_ERROR_NOT_SUPPORTED;
563 goto exit;
564 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200565 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
566
567 if( status != PSA_SUCCESS )
568 goto exit;
569
570 /* Copy the PK-contained RSA context to the one provided as function input */
571 status = mbedtls_to_psa_error(
572 mbedtls_rsa_copy( rsa, mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000573
574exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200575 mbedtls_pk_free( &ctx );
576 return( status );
577#else
578 (void) slot;
579 (void) rsa;
580 return( PSA_ERROR_NOT_SUPPORTED );
581#endif /* MBEDTLS_PK_PARSE_C */
582}
583
584/** Export an RSA key to export representation
585 *
586 * \param[in] type The type of key (public/private) to export
587 * \param[in] rsa The internal RSA representation from which to export
588 * \param[out] data The buffer to export to
589 * \param[in] data_size The length of the buffer to export to
590 * \param[out] data_length The amount of bytes written to \p data
591 */
592static psa_status_t psa_export_rsa_key( psa_key_type_t type,
593 mbedtls_rsa_context *rsa,
594 uint8_t *data,
595 size_t data_size,
596 size_t *data_length )
597{
598#if defined(MBEDTLS_PK_WRITE_C)
599 int ret;
600 mbedtls_pk_context pk;
601 uint8_t *pos = data + data_size;
602
603 mbedtls_pk_init( &pk );
604 pk.pk_info = &mbedtls_rsa_info;
605 pk.pk_ctx = rsa;
606
607 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
608 * representation of respectively the non-encrypted PKCS#1 RSAPrivateKey
609 * or the RFC3279 RSAPublicKey for a private key or a public key. */
610 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
611 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
612 else
613 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
614
615 if( ret < 0 )
616 return mbedtls_to_psa_error( ret );
617
618 /* The mbedtls_pk_xxx functions write to the end of the buffer.
619 * Move the data to the beginning and erase remaining data
620 * at the original location. */
621 if( 2 * (size_t) ret <= data_size )
622 {
623 memcpy( data, data + data_size - ret, ret );
624 memset( data + data_size - ret, 0, ret );
625 }
626 else if( (size_t) ret < data_size )
627 {
628 memmove( data, data + data_size - ret, ret );
629 memset( data + ret, 0, data_size - ret );
630 }
631
632 *data_length = ret;
633 return( PSA_SUCCESS );
634#else
635 (void) type;
636 (void) rsa;
637 (void) data;
638 (void) data_size;
639 (void) data_length;
640 return( PSA_ERROR_NOT_SUPPORTED );
641#endif /* MBEDTLS_PK_WRITE_C */
642}
643
644/** Import an RSA key from import representation to a slot
645 *
646 * \param[in,out] slot The slot where to store the export representation to
647 * \param[in] data The buffer containing the import representation
648 * \param[in] data_length The amount of bytes in \p data
649 */
650static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
651 const uint8_t *data,
652 size_t data_length )
653{
654 psa_status_t status;
655 uint8_t* output = NULL;
656 mbedtls_rsa_context rsa;
657 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
658
659 /* Temporarily load input into slot. The cast here is safe since it'll
660 * only be used for load_rsa_representation, which doesn't modify the
661 * buffer. */
662 slot->data.key.data = (uint8_t *)data;
663 slot->data.key.bytes = data_length;
664
665 /* Parse input */
666 status = psa_load_rsa_representation( slot, &rsa );
667 if( status != PSA_SUCCESS )
668 goto exit;
669
670 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
671 mbedtls_rsa_get_len( &rsa ) );
672
673 /* Re-export the data to PSA export format, which in case of RSA is the
674 * smallest representation we can parse. */
675 output = mbedtls_calloc( 1, data_length );
676
677 if( output == NULL )
678 {
679 status = PSA_ERROR_INSUFFICIENT_MEMORY;
680 goto exit;
681 }
682
683 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
684 * representation of respectively the non-encrypted PKCS#1 RSAPrivateKey
685 * or the RFC3279 RSAPublicKey for a private key or a public key. That
686 * means we have no other choice then to run an import to verify the key
687 * size. */
688 status = psa_export_rsa_key( slot->attr.type,
689 &rsa,
690 output,
691 data_length,
692 &data_length);
693
694exit:
695 /* Always free the RSA object */
696 mbedtls_rsa_free( &rsa );
697
698 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000699 if( status != PSA_SUCCESS )
700 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200701 mbedtls_free( output );
702 slot->data.key.data = NULL;
703 slot->data.key.bytes = 0;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000704 return( status );
705 }
706
Steven Cooremana01795d2020-07-24 22:48:15 +0200707 /* On success, store the allocated export-formatted key. */
708 slot->data.key.data = output;
709 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000710
711 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200712}
Steven Cooremana01795d2020-07-24 22:48:15 +0200713#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200714
Jaeden Ameroccdce902019-01-10 11:42:27 +0000715#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100716static psa_status_t psa_prepare_import_ec_key( psa_ecc_family_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100717 size_t data_length,
718 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100719 mbedtls_ecp_keypair **p_ecp )
720{
721 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
722 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
723 if( *p_ecp == NULL )
724 return( PSA_ERROR_INSUFFICIENT_MEMORY );
725 mbedtls_ecp_keypair_init( *p_ecp );
726
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100727 if( is_public )
728 {
729 /* A public key is represented as:
730 * - The byte 0x04;
731 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
732 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
733 * So its data length is 2m+1 where n is the key size in bits.
734 */
735 if( ( data_length & 1 ) == 0 )
736 return( PSA_ERROR_INVALID_ARGUMENT );
737 data_length = data_length / 2;
738 }
739
Gilles Peskine4cd32772019-12-02 20:49:42 +0100740 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100741 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
742 if( grp_id == MBEDTLS_ECP_DP_NONE )
743 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100744 return( mbedtls_to_psa_error(
745 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
746}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000747
748/* Import a public key given as the uncompressed representation defined by SEC1
749 * 2.3.3 as the content of an ECPoint. */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100750static psa_status_t psa_import_ec_public_key( psa_ecc_family_t curve,
Jaeden Ameroccdce902019-01-10 11:42:27 +0000751 const uint8_t *data,
752 size_t data_length,
753 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200754{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200755 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000756 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000757
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100758 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000759 if( status != PSA_SUCCESS )
760 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100761
Jaeden Ameroccdce902019-01-10 11:42:27 +0000762 /* Load the public value. */
763 status = mbedtls_to_psa_error(
764 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
765 data, data_length ) );
766 if( status != PSA_SUCCESS )
767 goto exit;
768
769 /* Check that the point is on the curve. */
770 status = mbedtls_to_psa_error(
771 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
772 if( status != PSA_SUCCESS )
773 goto exit;
774
775 *p_ecp = ecp;
776 return( PSA_SUCCESS );
777
778exit:
779 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200780 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000781 mbedtls_ecp_keypair_free( ecp );
782 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200783 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000784 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200785}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200786
Gilles Peskinef76aa772018-10-29 19:24:33 +0100787/* Import a private key given as a byte string which is the private value
788 * in big-endian order. */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100789static psa_status_t psa_import_ec_private_key( psa_ecc_family_t curve,
Gilles Peskinef76aa772018-10-29 19:24:33 +0100790 const uint8_t *data,
791 size_t data_length,
792 mbedtls_ecp_keypair **p_ecp )
793{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200794 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100795 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100796
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100797 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100798 if( status != PSA_SUCCESS )
799 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100800
Steven Cooremane3fd3922020-06-11 16:50:36 +0200801 /* Load and validate the secret key */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100802 status = mbedtls_to_psa_error(
Steven Cooremane3fd3922020-06-11 16:50:36 +0200803 mbedtls_ecp_read_key( ecp->grp.id, ecp, data, data_length ) );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100804 if( status != PSA_SUCCESS )
805 goto exit;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200806
Gilles Peskinef76aa772018-10-29 19:24:33 +0100807 /* Calculate the public key from the private key. */
808 status = mbedtls_to_psa_error(
809 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
810 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
811 if( status != PSA_SUCCESS )
812 goto exit;
813
814 *p_ecp = ecp;
815 return( PSA_SUCCESS );
816
817exit:
818 if( ecp != NULL )
819 {
820 mbedtls_ecp_keypair_free( ecp );
821 mbedtls_free( ecp );
822 }
823 return( status );
824}
825#endif /* defined(MBEDTLS_ECP_C) */
826
Gilles Peskineb46bef22019-07-30 21:32:04 +0200827
828/** Return the size of the key in the given slot, in bits.
829 *
830 * \param[in] slot A key slot.
831 *
832 * \return The key size in bits, read from the metadata in the slot.
833 */
834static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
835{
836 return( slot->attr.bits );
837}
838
839/** Calculate the size of the key in the given slot, in bits.
840 *
841 * \param[in] slot A key slot containing a transparent key.
842 *
843 * \return The key size in bits, calculated from the key data.
844 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200845static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200846{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200847 size_t bits = 0; /* return 0 on an empty slot */
848
Gilles Peskineb46bef22019-07-30 21:32:04 +0200849 if( key_type_is_raw_bytes( slot->attr.type ) )
Steven Cooreman71fd80d2020-07-07 21:12:27 +0200850 bits = PSA_BYTES_TO_BITS( slot->data.key.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200851#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200852 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
853 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200854#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200855
856 /* We know that the size fits in psa_key_bits_t thanks to checks
857 * when the key was created. */
858 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200859}
860
Gilles Peskine8e338702019-07-30 20:06:31 +0200861/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100862 * previously. This function assumes that the slot does not contain
863 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100864psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
865 const uint8_t *data,
866 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200868 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100869
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200870 /* zero-length keys are never supported. */
871 if( data_length == 0 )
872 return( PSA_ERROR_NOT_SUPPORTED );
873
874 /* Ensure that the bytes-to-bit conversion never overflows. */
875 if( data_length > SIZE_MAX / 8 )
876 return( PSA_ERROR_NOT_SUPPORTED );
877
Gilles Peskine8e338702019-07-30 20:06:31 +0200878 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100879 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200880 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200881
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200882 /* Enforce a size limit, and in particular ensure that the bit
883 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200884 if( bit_size > PSA_MAX_KEY_BITS )
885 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200886
887 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200888 if( status != PSA_SUCCESS )
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200889 return status;
890
891 /* Allocate memory for the key */
892 slot->data.key.data = mbedtls_calloc( 1, data_length );
893 if( slot->data.key.data == NULL )
894 {
895 return( PSA_ERROR_INSUFFICIENT_MEMORY );
896 }
897 slot->data.key.bytes = data_length;
898
899 /* copy key into allocated buffer */
900 memcpy(slot->data.key.data, data, data_length);
901
902 /* Write the actual key size to the slot.
903 * psa_start_key_creation() wrote the size declared by the
904 * caller, which may be 0 (meaning unspecified) or wrong. */
905 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100906 }
907 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100908#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200909 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100910 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100911 status = psa_import_ec_private_key( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100912 data, data_length,
913 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100914 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200915 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100916 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000917 status = psa_import_ec_public_key(
Paul Elliott8ff510a2020-06-02 17:19:28 +0100918 PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000919 data, data_length,
920 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100921 }
922 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100923#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000924#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200925 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100926 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200927 status = psa_import_rsa_key( slot, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100928 }
929 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000930#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100931 {
932 return( PSA_ERROR_NOT_SUPPORTED );
933 }
Darryl Green940d72c2018-07-13 13:18:51 +0100934
Gilles Peskineb46bef22019-07-30 21:32:04 +0200935 if( status == PSA_SUCCESS )
936 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200937 if( !PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
938 {
939 /* Write the actual key size to the slot.
940 * psa_start_key_creation() wrote the size declared by the
941 * caller, which may be 0 (meaning unspecified) or wrong. */
942 slot->attr.bits = psa_calculate_key_bits( slot );
943 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200944 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100945 return( status );
946}
947
Gilles Peskinef603c712019-01-19 13:40:11 +0100948/** Calculate the intersection of two algorithm usage policies.
949 *
950 * Return 0 (which allows no operation) on incompatibility.
951 */
952static psa_algorithm_t psa_key_policy_algorithm_intersection(
953 psa_algorithm_t alg1,
954 psa_algorithm_t alg2 )
955{
Gilles Peskine549ea862019-05-22 11:45:59 +0200956 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100957 if( alg1 == alg2 )
958 return( alg1 );
959 /* If the policies are from the same hash-and-sign family, check
960 * if one is a wildcard. If so the other has the specific algorithm. */
961 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
962 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
963 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
964 {
965 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
966 return( alg2 );
967 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
968 return( alg1 );
969 }
970 /* If the policies are incompatible, allow nothing. */
971 return( 0 );
972}
973
Gilles Peskined6f371b2019-05-10 19:33:38 +0200974static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
975 psa_algorithm_t requested_alg )
976{
Gilles Peskine549ea862019-05-22 11:45:59 +0200977 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200978 if( requested_alg == policy_alg )
979 return( 1 );
980 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200981 * and requested_alg is the same hash-and-sign family with any hash,
982 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200983 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
984 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
985 {
986 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
987 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
988 }
989 /* If it isn't permitted, it's forbidden. */
990 return( 0 );
991}
992
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100993/** Test whether a policy permits an algorithm.
994 *
995 * The caller must test usage flags separately.
996 */
997static int psa_key_policy_permits( const psa_key_policy_t *policy,
998 psa_algorithm_t alg )
999{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001000 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1001 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001002}
1003
Gilles Peskinef603c712019-01-19 13:40:11 +01001004/** Restrict a key policy based on a constraint.
1005 *
1006 * \param[in,out] policy The policy to restrict.
1007 * \param[in] constraint The policy constraint to apply.
1008 *
1009 * \retval #PSA_SUCCESS
1010 * \c *policy contains the intersection of the original value of
1011 * \c *policy and \c *constraint.
1012 * \retval #PSA_ERROR_INVALID_ARGUMENT
1013 * \c *policy and \c *constraint are incompatible.
1014 * \c *policy is unchanged.
1015 */
1016static psa_status_t psa_restrict_key_policy(
1017 psa_key_policy_t *policy,
1018 const psa_key_policy_t *constraint )
1019{
1020 psa_algorithm_t intersection_alg =
1021 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001022 psa_algorithm_t intersection_alg2 =
1023 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001024 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1025 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001026 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1027 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001028 policy->usage &= constraint->usage;
1029 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001030 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001031 return( PSA_SUCCESS );
1032}
1033
Darryl Green06fd18d2018-07-16 11:21:11 +01001034/** Retrieve a slot which must contain a key. The key must have allow all the
1035 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1036 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001037static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001038 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001039 psa_key_usage_t usage,
1040 psa_algorithm_t alg )
1041{
1042 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001043 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001044
1045 *p_slot = NULL;
1046
Gilles Peskinec5487a82018-12-03 18:08:14 +01001047 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001048 if( status != PSA_SUCCESS )
1049 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001050
1051 /* Enforce that usage policy for the key slot contains all the flags
1052 * required by the usage parameter. There is one exception: public
1053 * keys can always be exported, so we treat public key objects as
1054 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001055 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001056 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001057 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001058 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001059
1060 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001061 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001062 return( PSA_ERROR_NOT_PERMITTED );
1063
1064 *p_slot = slot;
1065 return( PSA_SUCCESS );
1066}
Darryl Green940d72c2018-07-13 13:18:51 +01001067
Gilles Peskine28f8f302019-07-24 13:30:31 +02001068/** Retrieve a slot which must contain a transparent key.
1069 *
1070 * A transparent key is a key for which the key material is directly
1071 * available, as opposed to a key in a secure element.
1072 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001073 * This is a temporary function to use instead of psa_get_key_from_slot()
1074 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001075 */
1076#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1077static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1078 psa_key_slot_t **p_slot,
1079 psa_key_usage_t usage,
1080 psa_algorithm_t alg )
1081{
1082 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1083 if( status != PSA_SUCCESS )
1084 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001085 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001086 {
1087 *p_slot = NULL;
1088 return( PSA_ERROR_NOT_SUPPORTED );
1089 }
1090 return( PSA_SUCCESS );
1091}
1092#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1093/* With no secure element support, all keys are transparent. */
1094#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1095 psa_get_key_from_slot( handle, p_slot, usage, alg )
1096#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1097
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001098/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001099static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001100{
Gilles Peskine73167e12019-07-12 23:44:37 +02001101#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1102 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001103 {
1104 /* No key material to clean. */
1105 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001106 else
1107#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001108 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001109 {
1110 /* No key material to clean. */
1111 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001112 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001113 {
Steven Cooreman71fd80d2020-07-07 21:12:27 +02001114 mbedtls_free( slot->data.key.data );
Darryl Green40225ba2018-11-15 14:48:15 +00001115 }
1116 else
1117#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001118 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001119 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001120 mbedtls_free( slot->data.key.data );
1121 slot->data.key.data = NULL;
1122 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001123 }
1124 else
1125#endif /* defined(MBEDTLS_RSA_C) */
1126#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001127 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001128 {
1129 mbedtls_ecp_keypair_free( slot->data.ecp );
1130 mbedtls_free( slot->data.ecp );
1131 }
1132 else
1133#endif /* defined(MBEDTLS_ECP_C) */
1134 {
1135 /* Shouldn't happen: the key type is not any type that we
1136 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001137 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001138 }
1139
1140 return( PSA_SUCCESS );
1141}
1142
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001143/** Completely wipe a slot in memory, including its policy.
1144 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001145psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001146{
1147 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001148 /* Multipart operations may still be using the key. This is safe
1149 * because all multipart operation objects are independent from
1150 * the key slot: if they need to access the key after the setup
1151 * phase, they have a copy of the key. Note that this means that
1152 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001153 /* At this point, key material and other type-specific content has
1154 * been wiped. Clear remaining metadata. We can call memset and not
1155 * zeroize because the metadata is not particularly sensitive. */
1156 memset( slot, 0, sizeof( *slot ) );
1157 return( status );
1158}
1159
Gilles Peskinec5487a82018-12-03 18:08:14 +01001160psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001161{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001162 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001163 psa_status_t status; /* status of the last operation */
1164 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001165#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1166 psa_se_drv_table_entry_t *driver;
1167#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001168
Gilles Peskine1841cf42019-10-08 15:48:25 +02001169 if( handle == 0 )
1170 return( PSA_SUCCESS );
1171
Gilles Peskinec5487a82018-12-03 18:08:14 +01001172 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001173 if( status != PSA_SUCCESS )
1174 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001175
1176#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001177 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001178 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001179 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001180 /* For a key in a secure element, we need to do three things:
1181 * remove the key file in internal storage, destroy the
1182 * key inside the secure element, and update the driver's
1183 * persistent data. Start a transaction that will encompass these
1184 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001185 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001186 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001187 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001188 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001189 status = psa_crypto_save_transaction( );
1190 if( status != PSA_SUCCESS )
1191 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001192 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001193 /* We should still try to destroy the key in the secure
1194 * element and the key metadata in storage. This is especially
1195 * important if the error is that the storage is full.
1196 * But how to do it exactly without risking an inconsistent
1197 * state after a reset?
1198 * https://github.com/ARMmbed/mbed-crypto/issues/215
1199 */
1200 overall_status = status;
1201 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001202 }
1203
Gilles Peskine354f7672019-07-12 23:46:38 +02001204 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001205 if( overall_status == PSA_SUCCESS )
1206 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001207 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001208#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1209
Darryl Greend49a4992018-06-18 17:27:26 +01001210#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001211 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001212 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001213 status = psa_destroy_persistent_key( slot->attr.id );
1214 if( overall_status == PSA_SUCCESS )
1215 overall_status = status;
1216
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001217 /* TODO: other slots may have a copy of the same key. We should
1218 * invalidate them.
1219 * https://github.com/ARMmbed/mbed-crypto/issues/214
1220 */
Darryl Greend49a4992018-06-18 17:27:26 +01001221 }
1222#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001223
Gilles Peskinefc762652019-07-22 19:30:34 +02001224#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1225 if( driver != NULL )
1226 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001227 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001228 if( overall_status == PSA_SUCCESS )
1229 overall_status = status;
1230 status = psa_crypto_stop_transaction( );
1231 if( overall_status == PSA_SUCCESS )
1232 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001233 }
1234#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1235
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001236#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1237exit:
1238#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001239 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001240 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1241 if( overall_status == PSA_SUCCESS )
1242 overall_status = status;
1243 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001244}
1245
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001246void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001247{
Gilles Peskineb699f072019-04-26 16:06:02 +02001248 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001249 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001250}
1251
Gilles Peskineb699f072019-04-26 16:06:02 +02001252psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1253 psa_key_type_t type,
1254 const uint8_t *data,
1255 size_t data_length )
1256{
1257 uint8_t *copy = NULL;
1258
1259 if( data_length != 0 )
1260 {
1261 copy = mbedtls_calloc( 1, data_length );
1262 if( copy == NULL )
1263 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1264 memcpy( copy, data, data_length );
1265 }
1266 /* After this point, this function is guaranteed to succeed, so it
1267 * can start modifying `*attributes`. */
1268
1269 if( attributes->domain_parameters != NULL )
1270 {
1271 mbedtls_free( attributes->domain_parameters );
1272 attributes->domain_parameters = NULL;
1273 attributes->domain_parameters_size = 0;
1274 }
1275
1276 attributes->domain_parameters = copy;
1277 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001278 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001279 return( PSA_SUCCESS );
1280}
1281
1282psa_status_t psa_get_key_domain_parameters(
1283 const psa_key_attributes_t *attributes,
1284 uint8_t *data, size_t data_size, size_t *data_length )
1285{
1286 if( attributes->domain_parameters_size > data_size )
1287 return( PSA_ERROR_BUFFER_TOO_SMALL );
1288 *data_length = attributes->domain_parameters_size;
1289 if( attributes->domain_parameters_size != 0 )
1290 memcpy( data, attributes->domain_parameters,
1291 attributes->domain_parameters_size );
1292 return( PSA_SUCCESS );
1293}
1294
1295#if defined(MBEDTLS_RSA_C)
1296static psa_status_t psa_get_rsa_public_exponent(
1297 const mbedtls_rsa_context *rsa,
1298 psa_key_attributes_t *attributes )
1299{
1300 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001302 uint8_t *buffer = NULL;
1303 size_t buflen;
1304 mbedtls_mpi_init( &mpi );
1305
1306 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1307 if( ret != 0 )
1308 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001309 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1310 {
1311 /* It's the default value, which is reported as an empty string,
1312 * so there's nothing to do. */
1313 goto exit;
1314 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001315
1316 buflen = mbedtls_mpi_size( &mpi );
1317 buffer = mbedtls_calloc( 1, buflen );
1318 if( buffer == NULL )
1319 {
1320 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1321 goto exit;
1322 }
1323 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1324 if( ret != 0 )
1325 goto exit;
1326 attributes->domain_parameters = buffer;
1327 attributes->domain_parameters_size = buflen;
1328
1329exit:
1330 mbedtls_mpi_free( &mpi );
1331 if( ret != 0 )
1332 mbedtls_free( buffer );
1333 return( mbedtls_to_psa_error( ret ) );
1334}
1335#endif /* MBEDTLS_RSA_C */
1336
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001337/** Retrieve all the publicly-accessible attributes of a key.
1338 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001339psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1340 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001341{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001342 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001343 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001344
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001345 psa_reset_key_attributes( attributes );
1346
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001347 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001348 if( status != PSA_SUCCESS )
1349 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001350
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001351 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001352 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1353 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1354
1355#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1356 if( psa_key_slot_is_external( slot ) )
1357 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1358#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001359
Gilles Peskine8e338702019-07-30 20:06:31 +02001360 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001361 {
1362#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001363 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001364 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001365#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001366 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001367 * is not yet implemented.
1368 * https://github.com/ARMmbed/mbed-crypto/issues/216
1369 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001370 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001371 break;
1372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001373 {
1374 mbedtls_rsa_context rsa;
1375 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
1376
1377 status = psa_load_rsa_representation( slot, &rsa );
1378 if( status != PSA_SUCCESS )
1379 break;
1380
1381 status = psa_get_rsa_public_exponent( &rsa,
1382 attributes );
1383 mbedtls_rsa_free( &rsa );
1384 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001385 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001386#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001387 default:
1388 /* Nothing else to do. */
1389 break;
1390 }
1391
1392 if( status != PSA_SUCCESS )
1393 psa_reset_key_attributes( attributes );
1394 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001395}
1396
Gilles Peskinec8000c02019-08-02 20:15:51 +02001397#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1398psa_status_t psa_get_key_slot_number(
1399 const psa_key_attributes_t *attributes,
1400 psa_key_slot_number_t *slot_number )
1401{
1402 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1403 {
1404 *slot_number = attributes->slot_number;
1405 return( PSA_SUCCESS );
1406 }
1407 else
1408 return( PSA_ERROR_INVALID_ARGUMENT );
1409}
1410#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1411
Jaeden Ameroccdce902019-01-10 11:42:27 +00001412#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001413static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1414 unsigned char *buf, size_t size )
1415{
Janos Follath24eed8d2019-11-22 13:21:35 +00001416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001417 unsigned char *c;
1418 size_t len = 0;
1419
1420 c = buf + size;
1421
1422 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1423
1424 return( (int) len );
1425}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001426#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001427
Steven Cooremana01795d2020-07-24 22:48:15 +02001428static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1429 uint8_t *data,
1430 size_t data_size,
1431 size_t *data_length )
1432{
1433 if( slot->data.key.bytes > data_size )
1434 return( PSA_ERROR_BUFFER_TOO_SMALL );
1435 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1436 memset( data + slot->data.key.bytes, 0,
1437 data_size - slot->data.key.bytes );
1438 *data_length = slot->data.key.bytes;
1439 return( PSA_SUCCESS );
1440}
1441
Gilles Peskinef603c712019-01-19 13:40:11 +01001442static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1443 uint8_t *data,
1444 size_t data_size,
1445 size_t *data_length,
1446 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001447{
Gilles Peskine5d309672019-07-12 23:47:28 +02001448#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1449 const psa_drv_se_t *drv;
1450 psa_drv_se_context_t *drv_context;
1451#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1452
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001453 *data_length = 0;
1454
Gilles Peskine8e338702019-07-30 20:06:31 +02001455 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001456 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001457
Gilles Peskinef9168942019-09-12 19:20:29 +02001458 /* Reject a zero-length output buffer now, since this can never be a
1459 * valid key representation. This way we know that data must be a valid
1460 * pointer and we can do things like memset(data, ..., data_size). */
1461 if( data_size == 0 )
1462 return( PSA_ERROR_BUFFER_TOO_SMALL );
1463
Gilles Peskine5d309672019-07-12 23:47:28 +02001464#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001465 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001466 {
1467 psa_drv_se_export_key_t method;
1468 if( drv->key_management == NULL )
1469 return( PSA_ERROR_NOT_SUPPORTED );
1470 method = ( export_public_key ?
1471 drv->key_management->p_export_public :
1472 drv->key_management->p_export );
1473 if( method == NULL )
1474 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001475 return( method( drv_context,
1476 slot->data.se.slot_number,
1477 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001478 }
1479#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1480
Gilles Peskine8e338702019-07-30 20:06:31 +02001481 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001482 {
Steven Cooreman71fd80d2020-07-07 21:12:27 +02001483 if( slot->data.key.bytes > data_size )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001484 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooreman71fd80d2020-07-07 21:12:27 +02001485 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1486 memset( data + slot->data.key.bytes, 0,
1487 data_size - slot->data.key.bytes );
1488 *data_length = slot->data.key.bytes;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001489 return( PSA_SUCCESS );
1490 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001491#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001492 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001493 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001494 psa_status_t status;
1495
Gilles Peskineb46bef22019-07-30 21:32:04 +02001496 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001497 if( bytes > data_size )
1498 return( PSA_ERROR_BUFFER_TOO_SMALL );
1499 status = mbedtls_to_psa_error(
Steven Cooreman0024df62020-07-13 10:59:40 +02001500 mbedtls_ecp_write_key( slot->data.ecp,
1501 data, bytes ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001502 if( status != PSA_SUCCESS )
1503 return( status );
1504 memset( data + bytes, 0, data_size - bytes );
1505 *data_length = bytes;
1506 return( PSA_SUCCESS );
1507 }
1508#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001509 else
Moran Peker17e36e12018-05-02 12:55:20 +03001510 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001511#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001512 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1513 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001514 {
Moran Pekera998bc62018-04-16 18:16:20 +03001515 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001517 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001518 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001519#if defined(MBEDTLS_RSA_C)
Steven Cooremana01795d2020-07-24 22:48:15 +02001520 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
1521 {
1522 /* Exporting public -> public */
1523 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1524 }
1525 else if( !export_public_key )
1526 {
1527 /* Exporting private -> private */
1528 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1529 }
1530
1531 /* Exporting private -> public */
1532 mbedtls_rsa_context rsa;
1533 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
1534
1535 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
1536 if( status != PSA_SUCCESS )
1537 return status;
1538
1539 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
1540 &rsa,
1541 data,
1542 data_size,
1543 data_length );
1544
1545 mbedtls_rsa_free( &rsa );
1546
1547 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001548#else
Steven Cooremana01795d2020-07-24 22:48:15 +02001549 /* We don't know how to convert a private RSA key to public. */
Darryl Green9e2d7a02018-07-24 16:33:30 +01001550 return( PSA_ERROR_NOT_SUPPORTED );
1551#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001552 }
1553 else
1554 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001555#if defined(MBEDTLS_ECP_C)
1556 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001557 pk.pk_info = &mbedtls_eckey_info;
1558 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001559#else
1560 return( PSA_ERROR_NOT_SUPPORTED );
1561#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001562 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001563 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001564 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001565 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001566 }
Moran Peker17e36e12018-05-02 12:55:20 +03001567 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001568 {
Moran Peker17e36e12018-05-02 12:55:20 +03001569 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001570 }
Moran Peker60364322018-04-29 11:34:58 +03001571 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001572 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001573 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001574 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001575 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001576 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1577 * Move the data to the beginning and erase remaining data
1578 * at the original location. */
1579 if( 2 * (size_t) ret <= data_size )
1580 {
1581 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001582 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001583 }
1584 else if( (size_t) ret < data_size )
1585 {
1586 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001587 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001588 }
Moran Pekera998bc62018-04-16 18:16:20 +03001589 *data_length = ret;
1590 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001591 }
1592 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001593#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001594 {
1595 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001596 it is valid for a special-purpose implementation to omit
1597 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001598 return( PSA_ERROR_NOT_SUPPORTED );
1599 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001600 }
1601}
1602
Gilles Peskinec5487a82018-12-03 18:08:14 +01001603psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001604 uint8_t *data,
1605 size_t data_size,
1606 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001607{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001608 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001609 psa_status_t status;
1610
1611 /* Set the key to empty now, so that even when there are errors, we always
1612 * set data_length to a value between 0 and data_size. On error, setting
1613 * the key to empty is a good choice because an empty key representation is
1614 * unlikely to be accepted anywhere. */
1615 *data_length = 0;
1616
1617 /* Export requires the EXPORT flag. There is an exception for public keys,
1618 * which don't require any flag, but psa_get_key_from_slot takes
1619 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001620 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001621 if( status != PSA_SUCCESS )
1622 return( status );
1623 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001624 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001625}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001626
Gilles Peskinec5487a82018-12-03 18:08:14 +01001627psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001628 uint8_t *data,
1629 size_t data_size,
1630 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001631{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001632 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001633 psa_status_t status;
1634
1635 /* Set the key to empty now, so that even when there are errors, we always
1636 * set data_length to a value between 0 and data_size. On error, setting
1637 * the key to empty is a good choice because an empty key representation is
1638 * unlikely to be accepted anywhere. */
1639 *data_length = 0;
1640
1641 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001642 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001643 if( status != PSA_SUCCESS )
1644 return( status );
1645 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001646 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001647}
1648
Gilles Peskine91e8c332019-08-02 19:19:39 +02001649#if defined(static_assert)
1650static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1651 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001652static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001653 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001654static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001655 "One or more key attribute flag is listed as both internal-only and external-only" );
1656#endif
1657
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001658/** Validate that a key policy is internally well-formed.
1659 *
1660 * This function only rejects invalid policies. It does not validate the
1661 * consistency of the policy with respect to other attributes of the key
1662 * such as the key type.
1663 */
1664static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001665{
1666 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001667 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001668 PSA_KEY_USAGE_ENCRYPT |
1669 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001670 PSA_KEY_USAGE_SIGN_HASH |
1671 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001672 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1673 return( PSA_ERROR_INVALID_ARGUMENT );
1674
Gilles Peskine4747d192019-04-17 15:05:45 +02001675 return( PSA_SUCCESS );
1676}
1677
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001678/** Validate the internal consistency of key attributes.
1679 *
1680 * This function only rejects invalid attribute values. If does not
1681 * validate the consistency of the attributes with any key data that may
1682 * be involved in the creation of the key.
1683 *
1684 * Call this function early in the key creation process.
1685 *
1686 * \param[in] attributes Key attributes for the new key.
1687 * \param[out] p_drv On any return, the driver for the key, if any.
1688 * NULL for a transparent key.
1689 *
1690 */
1691static psa_status_t psa_validate_key_attributes(
1692 const psa_key_attributes_t *attributes,
1693 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001694{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001695 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001696
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001697 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1698 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001699 if( status != PSA_SUCCESS )
1700 return( status );
1701
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001702 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1703 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001704 if( status != PSA_SUCCESS )
1705 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001706
1707 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001708 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001709 return( status );
1710
1711 /* Refuse to create overly large keys.
1712 * Note that this doesn't trigger on import if the attributes don't
1713 * explicitly specify a size (so psa_get_key_bits returns 0), so
1714 * psa_import_key() needs its own checks. */
1715 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1716 return( PSA_ERROR_NOT_SUPPORTED );
1717
Gilles Peskine91e8c332019-08-02 19:19:39 +02001718 /* Reject invalid flags. These should not be reachable through the API. */
1719 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1720 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1721 return( PSA_ERROR_INVALID_ARGUMENT );
1722
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001723 return( PSA_SUCCESS );
1724}
1725
Gilles Peskine4747d192019-04-17 15:05:45 +02001726/** Prepare a key slot to receive key material.
1727 *
1728 * This function allocates a key slot and sets its metadata.
1729 *
1730 * If this function fails, call psa_fail_key_creation().
1731 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001732 * This function is intended to be used as follows:
1733 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1734 * it with the specified attributes, and assign it a handle.
1735 * -# Populate the slot with the key material.
1736 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1737 * In case of failure at any step, stop the sequence and call
1738 * psa_fail_key_creation().
1739 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001740 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001741 * \param[in] attributes Key attributes for the new key.
1742 * \param[out] handle On success, a handle for the allocated slot.
1743 * \param[out] p_slot On success, a pointer to the prepared slot.
1744 * \param[out] p_drv On any return, the driver for the key, if any.
1745 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001746 *
1747 * \retval #PSA_SUCCESS
1748 * The key slot is ready to receive key material.
1749 * \return If this function fails, the key slot is an invalid state.
1750 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001751 */
1752static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001753 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001754 const psa_key_attributes_t *attributes,
1755 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001756 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001757 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001758{
1759 psa_status_t status;
1760 psa_key_slot_t *slot;
1761
Gilles Peskinedf179142019-07-15 22:02:14 +02001762 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001763 *p_drv = NULL;
1764
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001765 status = psa_validate_key_attributes( attributes, p_drv );
1766 if( status != PSA_SUCCESS )
1767 return( status );
1768
Gilles Peskineedbed562019-08-07 18:19:59 +02001769 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 if( status != PSA_SUCCESS )
1771 return( status );
1772 slot = *p_slot;
1773
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001774 /* We're storing the declared bit-size of the key. It's up to each
1775 * creation mechanism to verify that this information is correct.
1776 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001777 * an input (generate, device) but not for those where the bit-size
1778 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001779
1780 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001781
Gilles Peskine91e8c332019-08-02 19:19:39 +02001782 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001783 * external-only flags, query `attributes`. Thanks to the check
1784 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001785 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001786 * may have set. */
1787 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001788
Gilles Peskinecbaff462019-07-12 23:46:04 +02001789#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001790 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001791 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001792 * create the key file in internal storage, create the
1793 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001794 * persistent data. This is done by starting a transaction that will
1795 * encompass these three actions.
1796 * For registering a volatile key, we just need to find an appropriate
1797 * slot number inside the SE. Since the key is designated volatile, creating
1798 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001799 /* The first thing to do is to find a slot number for the new key.
1800 * We save the slot number in persistent storage as part of the
1801 * transaction data. It will be needed to recover if the power
1802 * fails during the key creation process, to clean up on the secure
1803 * element side after restarting. Obtaining a slot number from the
1804 * secure element driver updates its persistent state, but we do not yet
1805 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001806 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001807 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001808 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001809 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001810 &slot->data.se.slot_number );
1811 if( status != PSA_SUCCESS )
1812 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001813
1814 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001815 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001816 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1817 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1818 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1819 psa_crypto_transaction.key.id = slot->attr.id;
1820 status = psa_crypto_save_transaction( );
1821 if( status != PSA_SUCCESS )
1822 {
1823 (void) psa_crypto_stop_transaction( );
1824 return( status );
1825 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001826 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001827 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001828
1829 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1830 {
1831 /* Key registration only makes sense with a secure element. */
1832 return( PSA_ERROR_INVALID_ARGUMENT );
1833 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001834#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1835
Darryl Green0c6575a2018-11-07 16:05:30 +00001836 return( status );
1837}
Gilles Peskine4747d192019-04-17 15:05:45 +02001838
1839/** Finalize the creation of a key once its key material has been set.
1840 *
1841 * This entails writing the key to persistent storage.
1842 *
1843 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001844 * See the documentation of psa_start_key_creation() for the intended use
1845 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001846 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001847 * \param[in,out] slot Pointer to the slot with key material.
1848 * \param[in] driver The secure element driver for the key,
1849 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001850 *
1851 * \retval #PSA_SUCCESS
1852 * The key was successfully created. The handle is now valid.
1853 * \return If this function fails, the key slot is an invalid state.
1854 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001855 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001856static psa_status_t psa_finish_key_creation(
1857 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001858 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001859{
1860 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001861 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001862 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001863
1864#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001865 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001866 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001867#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1868 if( driver != NULL )
1869 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001870 psa_se_key_data_storage_t data;
1871#if defined(static_assert)
1872 static_assert( sizeof( slot->data.se.slot_number ) ==
1873 sizeof( data.slot_number ),
1874 "Slot number size does not match psa_se_key_data_storage_t" );
1875 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1876 "Bit-size size does not match psa_se_key_data_storage_t" );
1877#endif
1878 memcpy( &data.slot_number, &slot->data.se.slot_number,
1879 sizeof( slot->data.se.slot_number ) );
1880 memcpy( &data.bits, &slot->attr.bits,
1881 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001882 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001883 (uint8_t*) &data,
1884 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001885 }
1886 else
1887#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1888 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001889 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001890 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001891 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001892 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1893 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001894 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001895 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1896 status = psa_internal_export_key( slot,
1897 buffer, buffer_size, &length,
1898 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001899 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001900 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001901 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001902
Gilles Peskinef9168942019-09-12 19:20:29 +02001903 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001904 mbedtls_free( buffer );
1905 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001906 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001907#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1908
Gilles Peskinecbaff462019-07-12 23:46:04 +02001909#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001910 /* Finish the transaction for a key creation. This does not
1911 * happen when registering an existing key. Detect this case
1912 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001913 * creation of a persistent key in a secure element requires a transaction,
1914 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001915 if( driver != NULL &&
1916 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001917 {
1918 status = psa_save_se_persistent_data( driver );
1919 if( status != PSA_SUCCESS )
1920 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001921 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001922 return( status );
1923 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001924 status = psa_crypto_stop_transaction( );
1925 if( status != PSA_SUCCESS )
1926 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001927 }
1928#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1929
Gilles Peskine4747d192019-04-17 15:05:45 +02001930 return( status );
1931}
1932
1933/** Abort the creation of a key.
1934 *
1935 * You may call this function after calling psa_start_key_creation(),
1936 * or after psa_finish_key_creation() fails. In other circumstances, this
1937 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001938 * See the documentation of psa_start_key_creation() for the intended use
1939 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001940 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001941 * \param[in,out] slot Pointer to the slot with key material.
1942 * \param[in] driver The secure element driver for the key,
1943 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001944 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001945static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001946 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001947{
Gilles Peskine011e4282019-06-26 18:34:38 +02001948 (void) driver;
1949
Gilles Peskine4747d192019-04-17 15:05:45 +02001950 if( slot == NULL )
1951 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001952
1953#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001954 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001955 * element, and the failure happened later (when saving metadata
1956 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001957 * element.
1958 * https://github.com/ARMmbed/mbed-crypto/issues/217
1959 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001960
Gilles Peskined7729582019-08-05 15:55:54 +02001961 /* Abort the ongoing transaction if any (there may not be one if
1962 * the creation process failed before starting one, or if the
1963 * key creation is a registration of a key in a secure element).
1964 * Earlier functions must already have done what it takes to undo any
1965 * partial creation. All that's left is to update the transaction data
1966 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001967 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001968#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1969
Gilles Peskine4747d192019-04-17 15:05:45 +02001970 psa_wipe_key_slot( slot );
1971}
1972
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001973/** Validate optional attributes during key creation.
1974 *
1975 * Some key attributes are optional during key creation. If they are
1976 * specified in the attributes structure, check that they are consistent
1977 * with the data in the slot.
1978 *
1979 * This function should be called near the end of key creation, after
1980 * the slot in memory is fully populated but before saving persistent data.
1981 */
1982static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001983 const psa_key_slot_t *slot,
1984 const psa_key_attributes_t *attributes )
1985{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001986 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001987 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001988 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001989 return( PSA_ERROR_INVALID_ARGUMENT );
1990 }
1991
1992 if( attributes->domain_parameters_size != 0 )
1993 {
1994#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001995 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001996 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001997 mbedtls_rsa_context rsa;
1998 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
1999
2000 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
2001 if( status != PSA_SUCCESS )
2002 return status;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002003 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00002004 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002005 mbedtls_mpi_init( &actual );
2006 mbedtls_mpi_init( &required );
Steven Cooremana01795d2020-07-24 22:48:15 +02002007 ret = mbedtls_rsa_export( &rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana01795d2020-07-24 22:48:15 +02002009 mbedtls_rsa_free( &rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002010 if( ret != 0 )
2011 goto rsa_exit;
2012 ret = mbedtls_mpi_read_binary( &required,
2013 attributes->domain_parameters,
2014 attributes->domain_parameters_size );
2015 if( ret != 0 )
2016 goto rsa_exit;
2017 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2018 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2019 rsa_exit:
2020 mbedtls_mpi_free( &actual );
2021 mbedtls_mpi_free( &required );
2022 if( ret != 0)
2023 return( mbedtls_to_psa_error( ret ) );
2024 }
2025 else
2026#endif
2027 {
2028 return( PSA_ERROR_INVALID_ARGUMENT );
2029 }
2030 }
2031
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002032 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002033 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002034 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002035 return( PSA_ERROR_INVALID_ARGUMENT );
2036 }
2037
2038 return( PSA_SUCCESS );
2039}
2040
Gilles Peskine4747d192019-04-17 15:05:45 +02002041psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002042 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002043 size_t data_length,
2044 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002045{
2046 psa_status_t status;
2047 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002048 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049
Gilles Peskine0f84d622019-09-12 19:03:13 +02002050 /* Reject zero-length symmetric keys (including raw data key objects).
2051 * This also rejects any key which might be encoded as an empty string,
2052 * which is never valid. */
2053 if( data_length == 0 )
2054 return( PSA_ERROR_INVALID_ARGUMENT );
2055
Gilles Peskinedf179142019-07-15 22:02:14 +02002056 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2057 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002058 if( status != PSA_SUCCESS )
2059 goto exit;
2060
Gilles Peskine5d309672019-07-12 23:47:28 +02002061#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2062 if( driver != NULL )
2063 {
2064 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002065 /* The driver should set the number of key bits, however in
2066 * case it doesn't, we initialize bits to an invalid value. */
2067 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002068 if( drv->key_management == NULL ||
2069 drv->key_management->p_import == NULL )
2070 {
2071 status = PSA_ERROR_NOT_SUPPORTED;
2072 goto exit;
2073 }
2074 status = drv->key_management->p_import(
2075 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002076 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002077 &bits );
2078 if( status != PSA_SUCCESS )
2079 goto exit;
2080 if( bits > PSA_MAX_KEY_BITS )
2081 {
2082 status = PSA_ERROR_NOT_SUPPORTED;
2083 goto exit;
2084 }
2085 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002086 }
2087 else
2088#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2089 {
2090 status = psa_import_key_into_slot( slot, data, data_length );
2091 if( status != PSA_SUCCESS )
2092 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002093 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002094 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002095 if( status != PSA_SUCCESS )
2096 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002097
Gilles Peskine011e4282019-06-26 18:34:38 +02002098 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002099exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002100 if( status != PSA_SUCCESS )
2101 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002102 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002103 *handle = 0;
2104 }
2105 return( status );
2106}
2107
Gilles Peskined7729582019-08-05 15:55:54 +02002108#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2109psa_status_t mbedtls_psa_register_se_key(
2110 const psa_key_attributes_t *attributes )
2111{
2112 psa_status_t status;
2113 psa_key_slot_t *slot = NULL;
2114 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002115 psa_key_handle_t handle = 0;
2116
2117 /* Leaving attributes unspecified is not currently supported.
2118 * It could make sense to query the key type and size from the
2119 * secure element, but not all secure elements support this
2120 * and the driver HAL doesn't currently support it. */
2121 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2122 return( PSA_ERROR_NOT_SUPPORTED );
2123 if( psa_get_key_bits( attributes ) == 0 )
2124 return( PSA_ERROR_NOT_SUPPORTED );
2125
2126 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2127 &handle, &slot, &driver );
2128 if( status != PSA_SUCCESS )
2129 goto exit;
2130
Gilles Peskined7729582019-08-05 15:55:54 +02002131 status = psa_finish_key_creation( slot, driver );
2132
2133exit:
2134 if( status != PSA_SUCCESS )
2135 {
2136 psa_fail_key_creation( slot, driver );
2137 }
2138 /* Registration doesn't keep the key in RAM. */
2139 psa_close_key( handle );
2140 return( status );
2141}
2142#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2143
Gilles Peskinef603c712019-01-19 13:40:11 +01002144static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002145 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002146{
2147 psa_status_t status;
2148 uint8_t *buffer = NULL;
2149 size_t buffer_size = 0;
2150 size_t length;
2151
Gilles Peskine8e338702019-07-30 20:06:31 +02002152 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002153 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002154 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002155 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002156 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002157 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2158 if( status != PSA_SUCCESS )
2159 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002160 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002161 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002162
2163exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002164 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002165 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002166 return( status );
2167}
2168
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002169psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2170 const psa_key_attributes_t *specified_attributes,
2171 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002172{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002173 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002174 psa_key_slot_t *source_slot = NULL;
2175 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002176 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002177 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002178
Gilles Peskine28f8f302019-07-24 13:30:31 +02002179 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002180 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002181 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002182 goto exit;
2183
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002184 status = psa_validate_optional_attributes( source_slot,
2185 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002186 if( status != PSA_SUCCESS )
2187 goto exit;
2188
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002189 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002190 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002191 if( status != PSA_SUCCESS )
2192 goto exit;
2193
Gilles Peskinedf179142019-07-15 22:02:14 +02002194 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2195 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002196 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002197 if( status != PSA_SUCCESS )
2198 goto exit;
2199
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002200#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2201 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002202 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002203 /* Copying to a secure element is not implemented yet. */
2204 status = PSA_ERROR_NOT_SUPPORTED;
2205 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002206 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002207#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002208
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002209 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002210 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002211 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002212
Gilles Peskine011e4282019-06-26 18:34:38 +02002213 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002214exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002215 if( status != PSA_SUCCESS )
2216 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002217 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002218 *target_handle = 0;
2219 }
2220 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002221}
2222
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002223
2224
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002225/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002226/* Message digests */
2227/****************************************************************/
2228
Gilles Peskineb16841e2019-10-10 20:36:12 +02002229#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002230static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002231{
2232 switch( alg )
2233 {
2234#if defined(MBEDTLS_MD2_C)
2235 case PSA_ALG_MD2:
2236 return( &mbedtls_md2_info );
2237#endif
2238#if defined(MBEDTLS_MD4_C)
2239 case PSA_ALG_MD4:
2240 return( &mbedtls_md4_info );
2241#endif
2242#if defined(MBEDTLS_MD5_C)
2243 case PSA_ALG_MD5:
2244 return( &mbedtls_md5_info );
2245#endif
2246#if defined(MBEDTLS_RIPEMD160_C)
2247 case PSA_ALG_RIPEMD160:
2248 return( &mbedtls_ripemd160_info );
2249#endif
2250#if defined(MBEDTLS_SHA1_C)
2251 case PSA_ALG_SHA_1:
2252 return( &mbedtls_sha1_info );
2253#endif
2254#if defined(MBEDTLS_SHA256_C)
2255 case PSA_ALG_SHA_224:
2256 return( &mbedtls_sha224_info );
2257 case PSA_ALG_SHA_256:
2258 return( &mbedtls_sha256_info );
2259#endif
2260#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002261#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002262 case PSA_ALG_SHA_384:
2263 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002264#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002265 case PSA_ALG_SHA_512:
2266 return( &mbedtls_sha512_info );
2267#endif
2268 default:
2269 return( NULL );
2270 }
2271}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002272#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002273
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002274psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2275{
2276 switch( operation->alg )
2277 {
Gilles Peskine81736312018-06-26 15:04:31 +02002278 case 0:
2279 /* The object has (apparently) been initialized but it is not
2280 * in use. It's ok to call abort on such an object, and there's
2281 * nothing to do. */
2282 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002283#if defined(MBEDTLS_MD2_C)
2284 case PSA_ALG_MD2:
2285 mbedtls_md2_free( &operation->ctx.md2 );
2286 break;
2287#endif
2288#if defined(MBEDTLS_MD4_C)
2289 case PSA_ALG_MD4:
2290 mbedtls_md4_free( &operation->ctx.md4 );
2291 break;
2292#endif
2293#if defined(MBEDTLS_MD5_C)
2294 case PSA_ALG_MD5:
2295 mbedtls_md5_free( &operation->ctx.md5 );
2296 break;
2297#endif
2298#if defined(MBEDTLS_RIPEMD160_C)
2299 case PSA_ALG_RIPEMD160:
2300 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2301 break;
2302#endif
2303#if defined(MBEDTLS_SHA1_C)
2304 case PSA_ALG_SHA_1:
2305 mbedtls_sha1_free( &operation->ctx.sha1 );
2306 break;
2307#endif
2308#if defined(MBEDTLS_SHA256_C)
2309 case PSA_ALG_SHA_224:
2310 case PSA_ALG_SHA_256:
2311 mbedtls_sha256_free( &operation->ctx.sha256 );
2312 break;
2313#endif
2314#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002315#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002317#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318 case PSA_ALG_SHA_512:
2319 mbedtls_sha512_free( &operation->ctx.sha512 );
2320 break;
2321#endif
2322 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002323 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324 }
2325 operation->alg = 0;
2326 return( PSA_SUCCESS );
2327}
2328
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002329psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330 psa_algorithm_t alg )
2331{
Janos Follath24eed8d2019-11-22 13:21:35 +00002332 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002333
2334 /* A context must be freshly initialized before it can be set up. */
2335 if( operation->alg != 0 )
2336 {
2337 return( PSA_ERROR_BAD_STATE );
2338 }
2339
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340 switch( alg )
2341 {
2342#if defined(MBEDTLS_MD2_C)
2343 case PSA_ALG_MD2:
2344 mbedtls_md2_init( &operation->ctx.md2 );
2345 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2346 break;
2347#endif
2348#if defined(MBEDTLS_MD4_C)
2349 case PSA_ALG_MD4:
2350 mbedtls_md4_init( &operation->ctx.md4 );
2351 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2352 break;
2353#endif
2354#if defined(MBEDTLS_MD5_C)
2355 case PSA_ALG_MD5:
2356 mbedtls_md5_init( &operation->ctx.md5 );
2357 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2358 break;
2359#endif
2360#if defined(MBEDTLS_RIPEMD160_C)
2361 case PSA_ALG_RIPEMD160:
2362 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2363 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2364 break;
2365#endif
2366#if defined(MBEDTLS_SHA1_C)
2367 case PSA_ALG_SHA_1:
2368 mbedtls_sha1_init( &operation->ctx.sha1 );
2369 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2370 break;
2371#endif
2372#if defined(MBEDTLS_SHA256_C)
2373 case PSA_ALG_SHA_224:
2374 mbedtls_sha256_init( &operation->ctx.sha256 );
2375 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2376 break;
2377 case PSA_ALG_SHA_256:
2378 mbedtls_sha256_init( &operation->ctx.sha256 );
2379 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2380 break;
2381#endif
2382#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002383#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002384 case PSA_ALG_SHA_384:
2385 mbedtls_sha512_init( &operation->ctx.sha512 );
2386 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2387 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002388#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389 case PSA_ALG_SHA_512:
2390 mbedtls_sha512_init( &operation->ctx.sha512 );
2391 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2392 break;
2393#endif
2394 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002395 return( PSA_ALG_IS_HASH( alg ) ?
2396 PSA_ERROR_NOT_SUPPORTED :
2397 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002398 }
2399 if( ret == 0 )
2400 operation->alg = alg;
2401 else
2402 psa_hash_abort( operation );
2403 return( mbedtls_to_psa_error( ret ) );
2404}
2405
2406psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2407 const uint8_t *input,
2408 size_t input_length )
2409{
Janos Follath24eed8d2019-11-22 13:21:35 +00002410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002411
2412 /* Don't require hash implementations to behave correctly on a
2413 * zero-length input, which may have an invalid pointer. */
2414 if( input_length == 0 )
2415 return( PSA_SUCCESS );
2416
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002417 switch( operation->alg )
2418 {
2419#if defined(MBEDTLS_MD2_C)
2420 case PSA_ALG_MD2:
2421 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2422 input, input_length );
2423 break;
2424#endif
2425#if defined(MBEDTLS_MD4_C)
2426 case PSA_ALG_MD4:
2427 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2428 input, input_length );
2429 break;
2430#endif
2431#if defined(MBEDTLS_MD5_C)
2432 case PSA_ALG_MD5:
2433 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2434 input, input_length );
2435 break;
2436#endif
2437#if defined(MBEDTLS_RIPEMD160_C)
2438 case PSA_ALG_RIPEMD160:
2439 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2440 input, input_length );
2441 break;
2442#endif
2443#if defined(MBEDTLS_SHA1_C)
2444 case PSA_ALG_SHA_1:
2445 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2446 input, input_length );
2447 break;
2448#endif
2449#if defined(MBEDTLS_SHA256_C)
2450 case PSA_ALG_SHA_224:
2451 case PSA_ALG_SHA_256:
2452 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2453 input, input_length );
2454 break;
2455#endif
2456#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002457#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002458 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002459#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002460 case PSA_ALG_SHA_512:
2461 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2462 input, input_length );
2463 break;
2464#endif
2465 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002466 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002467 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002468
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002469 if( ret != 0 )
2470 psa_hash_abort( operation );
2471 return( mbedtls_to_psa_error( ret ) );
2472}
2473
2474psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2475 uint8_t *hash,
2476 size_t hash_size,
2477 size_t *hash_length )
2478{
itayzafrir40835d42018-08-02 13:14:17 +03002479 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002481 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002482
2483 /* Fill the output buffer with something that isn't a valid hash
2484 * (barring an attack on the hash and deliberately-crafted input),
2485 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002486 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002487 /* If hash_size is 0 then hash may be NULL and then the
2488 * call to memset would have undefined behavior. */
2489 if( hash_size != 0 )
2490 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002491
2492 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002493 {
2494 status = PSA_ERROR_BUFFER_TOO_SMALL;
2495 goto exit;
2496 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002497
2498 switch( operation->alg )
2499 {
2500#if defined(MBEDTLS_MD2_C)
2501 case PSA_ALG_MD2:
2502 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2503 break;
2504#endif
2505#if defined(MBEDTLS_MD4_C)
2506 case PSA_ALG_MD4:
2507 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2508 break;
2509#endif
2510#if defined(MBEDTLS_MD5_C)
2511 case PSA_ALG_MD5:
2512 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2513 break;
2514#endif
2515#if defined(MBEDTLS_RIPEMD160_C)
2516 case PSA_ALG_RIPEMD160:
2517 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2518 break;
2519#endif
2520#if defined(MBEDTLS_SHA1_C)
2521 case PSA_ALG_SHA_1:
2522 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2523 break;
2524#endif
2525#if defined(MBEDTLS_SHA256_C)
2526 case PSA_ALG_SHA_224:
2527 case PSA_ALG_SHA_256:
2528 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2529 break;
2530#endif
2531#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002532#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002533 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002534#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002535 case PSA_ALG_SHA_512:
2536 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2537 break;
2538#endif
2539 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002540 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002541 }
itayzafrir40835d42018-08-02 13:14:17 +03002542 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002543
itayzafrir40835d42018-08-02 13:14:17 +03002544exit:
2545 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002546 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002547 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002548 return( psa_hash_abort( operation ) );
2549 }
2550 else
2551 {
2552 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002553 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002554 }
2555}
2556
Gilles Peskine2d277862018-06-18 15:41:12 +02002557psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2558 const uint8_t *hash,
2559 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002560{
2561 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2562 size_t actual_hash_length;
2563 psa_status_t status = psa_hash_finish( operation,
2564 actual_hash, sizeof( actual_hash ),
2565 &actual_hash_length );
2566 if( status != PSA_SUCCESS )
2567 return( status );
2568 if( actual_hash_length != hash_length )
2569 return( PSA_ERROR_INVALID_SIGNATURE );
2570 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2571 return( PSA_ERROR_INVALID_SIGNATURE );
2572 return( PSA_SUCCESS );
2573}
2574
Gilles Peskine0a749c82019-11-28 19:33:58 +01002575psa_status_t psa_hash_compute( psa_algorithm_t alg,
2576 const uint8_t *input, size_t input_length,
2577 uint8_t *hash, size_t hash_size,
2578 size_t *hash_length )
2579{
2580 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2581 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2582
2583 *hash_length = hash_size;
2584 status = psa_hash_setup( &operation, alg );
2585 if( status != PSA_SUCCESS )
2586 goto exit;
2587 status = psa_hash_update( &operation, input, input_length );
2588 if( status != PSA_SUCCESS )
2589 goto exit;
2590 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2591 if( status != PSA_SUCCESS )
2592 goto exit;
2593
2594exit:
2595 if( status == PSA_SUCCESS )
2596 status = psa_hash_abort( &operation );
2597 else
2598 psa_hash_abort( &operation );
2599 return( status );
2600}
2601
2602psa_status_t psa_hash_compare( psa_algorithm_t alg,
2603 const uint8_t *input, size_t input_length,
2604 const uint8_t *hash, size_t hash_length )
2605{
2606 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2607 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2608
2609 status = psa_hash_setup( &operation, alg );
2610 if( status != PSA_SUCCESS )
2611 goto exit;
2612 status = psa_hash_update( &operation, input, input_length );
2613 if( status != PSA_SUCCESS )
2614 goto exit;
2615 status = psa_hash_verify( &operation, hash, hash_length );
2616 if( status != PSA_SUCCESS )
2617 goto exit;
2618
2619exit:
2620 if( status == PSA_SUCCESS )
2621 status = psa_hash_abort( &operation );
2622 else
2623 psa_hash_abort( &operation );
2624 return( status );
2625}
2626
Gilles Peskineeb35d782019-01-22 17:56:16 +01002627psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2628 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002629{
2630 if( target_operation->alg != 0 )
2631 return( PSA_ERROR_BAD_STATE );
2632
2633 switch( source_operation->alg )
2634 {
2635 case 0:
2636 return( PSA_ERROR_BAD_STATE );
2637#if defined(MBEDTLS_MD2_C)
2638 case PSA_ALG_MD2:
2639 mbedtls_md2_clone( &target_operation->ctx.md2,
2640 &source_operation->ctx.md2 );
2641 break;
2642#endif
2643#if defined(MBEDTLS_MD4_C)
2644 case PSA_ALG_MD4:
2645 mbedtls_md4_clone( &target_operation->ctx.md4,
2646 &source_operation->ctx.md4 );
2647 break;
2648#endif
2649#if defined(MBEDTLS_MD5_C)
2650 case PSA_ALG_MD5:
2651 mbedtls_md5_clone( &target_operation->ctx.md5,
2652 &source_operation->ctx.md5 );
2653 break;
2654#endif
2655#if defined(MBEDTLS_RIPEMD160_C)
2656 case PSA_ALG_RIPEMD160:
2657 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2658 &source_operation->ctx.ripemd160 );
2659 break;
2660#endif
2661#if defined(MBEDTLS_SHA1_C)
2662 case PSA_ALG_SHA_1:
2663 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2664 &source_operation->ctx.sha1 );
2665 break;
2666#endif
2667#if defined(MBEDTLS_SHA256_C)
2668 case PSA_ALG_SHA_224:
2669 case PSA_ALG_SHA_256:
2670 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2671 &source_operation->ctx.sha256 );
2672 break;
2673#endif
2674#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002675#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002676 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002677#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002678 case PSA_ALG_SHA_512:
2679 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2680 &source_operation->ctx.sha512 );
2681 break;
2682#endif
2683 default:
2684 return( PSA_ERROR_NOT_SUPPORTED );
2685 }
2686
2687 target_operation->alg = source_operation->alg;
2688 return( PSA_SUCCESS );
2689}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002690
2691
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002692/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002693/* MAC */
2694/****************************************************************/
2695
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002696static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002697 psa_algorithm_t alg,
2698 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002699 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002700 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002701{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002702 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002703 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002705 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002706 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002707
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2709 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002710 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002711 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002712 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002713 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002714 mode = MBEDTLS_MODE_STREAM;
2715 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002716 case PSA_ALG_CTR:
2717 mode = MBEDTLS_MODE_CTR;
2718 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002719 case PSA_ALG_CFB:
2720 mode = MBEDTLS_MODE_CFB;
2721 break;
2722 case PSA_ALG_OFB:
2723 mode = MBEDTLS_MODE_OFB;
2724 break;
2725 case PSA_ALG_CBC_NO_PADDING:
2726 mode = MBEDTLS_MODE_CBC;
2727 break;
2728 case PSA_ALG_CBC_PKCS7:
2729 mode = MBEDTLS_MODE_CBC;
2730 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002731 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002732 mode = MBEDTLS_MODE_CCM;
2733 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002734 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002735 mode = MBEDTLS_MODE_GCM;
2736 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002737 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2738 mode = MBEDTLS_MODE_CHACHAPOLY;
2739 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740 default:
2741 return( NULL );
2742 }
2743 }
2744 else if( alg == PSA_ALG_CMAC )
2745 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746 else
2747 return( NULL );
2748
2749 switch( key_type )
2750 {
2751 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002752 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753 break;
2754 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002755 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2756 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002757 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002758 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002759 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002760 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002761 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2762 * but two-key Triple-DES is functionally three-key Triple-DES
2763 * with K1=K3, so that's how we present it to mbedtls. */
2764 if( key_bits == 128 )
2765 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002766 break;
2767 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002768 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002769 break;
2770 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002771 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002773 case PSA_KEY_TYPE_CHACHA20:
2774 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2775 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002776 default:
2777 return( NULL );
2778 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002779 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002780 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002781
Jaeden Amero23bbb752018-06-26 14:16:54 +01002782 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2783 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002784}
2785
Gilles Peskinea05219c2018-11-16 16:02:56 +01002786#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002787static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002788{
Gilles Peskine2d277862018-06-18 15:41:12 +02002789 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002790 {
2791 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002792 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002793 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002794 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002795 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002796 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002797 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002798 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002799 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002800 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002801 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002802 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002803 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002804 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002805 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002806 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002807 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002808 return( 128 );
2809 default:
2810 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002811 }
2812}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002813#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002814
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002815/* Initialize the MAC operation structure. Once this function has been
2816 * called, psa_mac_abort can run and will do the right thing. */
2817static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2818 psa_algorithm_t alg )
2819{
2820 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2821
2822 operation->alg = alg;
2823 operation->key_set = 0;
2824 operation->iv_set = 0;
2825 operation->iv_required = 0;
2826 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002827 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002828
2829#if defined(MBEDTLS_CMAC_C)
2830 if( alg == PSA_ALG_CMAC )
2831 {
2832 operation->iv_required = 0;
2833 mbedtls_cipher_init( &operation->ctx.cmac );
2834 status = PSA_SUCCESS;
2835 }
2836 else
2837#endif /* MBEDTLS_CMAC_C */
2838#if defined(MBEDTLS_MD_C)
2839 if( PSA_ALG_IS_HMAC( operation->alg ) )
2840 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002841 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2842 operation->ctx.hmac.hash_ctx.alg = 0;
2843 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002844 }
2845 else
2846#endif /* MBEDTLS_MD_C */
2847 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002848 if( ! PSA_ALG_IS_MAC( alg ) )
2849 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002850 }
2851
2852 if( status != PSA_SUCCESS )
2853 memset( operation, 0, sizeof( *operation ) );
2854 return( status );
2855}
2856
Gilles Peskine01126fa2018-07-12 17:04:55 +02002857#if defined(MBEDTLS_MD_C)
2858static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2859{
Gilles Peskine3f108122018-12-07 18:14:53 +01002860 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002861 return( psa_hash_abort( &hmac->hash_ctx ) );
2862}
2863#endif /* MBEDTLS_MD_C */
2864
Gilles Peskine8c9def32018-02-08 10:02:12 +01002865psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2866{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002867 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002868 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002869 /* The object has (apparently) been initialized but it is not
2870 * in use. It's ok to call abort on such an object, and there's
2871 * nothing to do. */
2872 return( PSA_SUCCESS );
2873 }
2874 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002876 if( operation->alg == PSA_ALG_CMAC )
2877 {
2878 mbedtls_cipher_free( &operation->ctx.cmac );
2879 }
2880 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002881#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002882#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002883 if( PSA_ALG_IS_HMAC( operation->alg ) )
2884 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002885 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002886 }
2887 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002888#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002889 {
2890 /* Sanity check (shouldn't happen: operation->alg should
2891 * always have been initialized to a valid value). */
2892 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002893 }
Moran Peker41deec42018-04-04 15:43:05 +03002894
Gilles Peskine8c9def32018-02-08 10:02:12 +01002895 operation->alg = 0;
2896 operation->key_set = 0;
2897 operation->iv_set = 0;
2898 operation->iv_required = 0;
2899 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002900 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002901
Gilles Peskine8c9def32018-02-08 10:02:12 +01002902 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002903
2904bad_state:
2905 /* If abort is called on an uninitialized object, we can't trust
2906 * anything. Wipe the object in case it contains confidential data.
2907 * This may result in a memory leak if a pointer gets overwritten,
2908 * but it's too late to do anything about this. */
2909 memset( operation, 0, sizeof( *operation ) );
2910 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002911}
2912
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002913#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002914static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002915 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002916 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002917 const mbedtls_cipher_info_t *cipher_info )
2918{
Janos Follath24eed8d2019-11-22 13:21:35 +00002919 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002920
2921 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002922
2923 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2924 if( ret != 0 )
2925 return( ret );
2926
2927 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002928 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002929 key_bits );
2930 return( ret );
2931}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002932#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002933
Gilles Peskine248051a2018-06-20 16:09:38 +02002934#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002935static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2936 const uint8_t *key,
2937 size_t key_length,
2938 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002939{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002940 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002941 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002942 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002943 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002944 psa_status_t status;
2945
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002946 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2947 * overflow below. This should never trigger if the hash algorithm
2948 * is implemented correctly. */
2949 /* The size checks against the ipad and opad buffers cannot be written
2950 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2951 * because that triggers -Wlogical-op on GCC 7.3. */
2952 if( block_size > sizeof( ipad ) )
2953 return( PSA_ERROR_NOT_SUPPORTED );
2954 if( block_size > sizeof( hmac->opad ) )
2955 return( PSA_ERROR_NOT_SUPPORTED );
2956 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002957 return( PSA_ERROR_NOT_SUPPORTED );
2958
Gilles Peskined223b522018-06-11 18:12:58 +02002959 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002960 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002961 status = psa_hash_compute( hash_alg, key, key_length,
2962 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002963 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002964 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002965 }
Gilles Peskine96889972018-07-12 17:07:03 +02002966 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2967 * but it is permitted. It is common when HMAC is used in HKDF, for
2968 * example. Don't call `memcpy` in the 0-length because `key` could be
2969 * an invalid pointer which would make the behavior undefined. */
2970 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002971 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002972
Gilles Peskined223b522018-06-11 18:12:58 +02002973 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2974 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002975 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002976 ipad[i] ^= 0x36;
2977 memset( ipad + key_length, 0x36, block_size - key_length );
2978
2979 /* Copy the key material from ipad to opad, flipping the requisite bits,
2980 * and filling the rest of opad with the requisite constant. */
2981 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002982 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2983 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002984
Gilles Peskine01126fa2018-07-12 17:04:55 +02002985 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002986 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002987 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002988
Gilles Peskine01126fa2018-07-12 17:04:55 +02002989 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002990
2991cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002992 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002993
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002994 return( status );
2995}
Gilles Peskine248051a2018-06-20 16:09:38 +02002996#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002997
Gilles Peskine89167cb2018-07-08 20:12:23 +02002998static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002999 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003000 psa_algorithm_t alg,
3001 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003002{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003003 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003004 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003005 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003006 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003007 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003008 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003009 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003011 /* A context must be freshly initialized before it can be set up. */
3012 if( operation->alg != 0 )
3013 {
3014 return( PSA_ERROR_BAD_STATE );
3015 }
3016
Gilles Peskined911eb72018-08-14 15:18:45 +02003017 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003018 if( status != PSA_SUCCESS )
3019 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003020 if( is_sign )
3021 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003022
Gilles Peskine28f8f302019-07-24 13:30:31 +02003023 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003024 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003025 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003026 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003027
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003029 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003030 {
3031 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003032 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003033 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003035 if( cipher_info == NULL )
3036 {
3037 status = PSA_ERROR_NOT_SUPPORTED;
3038 goto exit;
3039 }
3040 operation->mac_size = cipher_info->block_size;
3041 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3042 status = mbedtls_to_psa_error( ret );
3043 }
3044 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003045#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003046#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003047 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003048 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003049 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003050 if( hash_alg == 0 )
3051 {
3052 status = PSA_ERROR_NOT_SUPPORTED;
3053 goto exit;
3054 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003055
3056 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3057 /* Sanity check. This shouldn't fail on a valid configuration. */
3058 if( operation->mac_size == 0 ||
3059 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3060 {
3061 status = PSA_ERROR_NOT_SUPPORTED;
3062 goto exit;
3063 }
3064
Gilles Peskine8e338702019-07-30 20:06:31 +02003065 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003066 {
3067 status = PSA_ERROR_INVALID_ARGUMENT;
3068 goto exit;
3069 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003070
Gilles Peskine01126fa2018-07-12 17:04:55 +02003071 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003072 slot->data.key.data,
3073 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003074 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003075 }
3076 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003077#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003078 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003079 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003080 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003081 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003082
Gilles Peskined911eb72018-08-14 15:18:45 +02003083 if( truncated == 0 )
3084 {
3085 /* The "normal" case: untruncated algorithm. Nothing to do. */
3086 }
3087 else if( truncated < 4 )
3088 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003089 /* A very short MAC is too short for security since it can be
3090 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3091 * so we make this our minimum, even though 32 bits is still
3092 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003093 status = PSA_ERROR_NOT_SUPPORTED;
3094 }
3095 else if( truncated > operation->mac_size )
3096 {
3097 /* It's impossible to "truncate" to a larger length. */
3098 status = PSA_ERROR_INVALID_ARGUMENT;
3099 }
3100 else
3101 operation->mac_size = truncated;
3102
Gilles Peskinefbfac682018-07-08 20:51:54 +02003103exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003104 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003105 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003106 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003107 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003108 else
3109 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003110 operation->key_set = 1;
3111 }
3112 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003113}
3114
Gilles Peskine89167cb2018-07-08 20:12:23 +02003115psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003116 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003117 psa_algorithm_t alg )
3118{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003119 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003120}
3121
3122psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003123 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003124 psa_algorithm_t alg )
3125{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003126 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003127}
3128
Gilles Peskine8c9def32018-02-08 10:02:12 +01003129psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3130 const uint8_t *input,
3131 size_t input_length )
3132{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003133 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003134 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003135 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003136 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003137 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003138 operation->has_input = 1;
3139
Gilles Peskine8c9def32018-02-08 10:02:12 +01003140#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003141 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003142 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003143 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3144 input, input_length );
3145 status = mbedtls_to_psa_error( ret );
3146 }
3147 else
3148#endif /* MBEDTLS_CMAC_C */
3149#if defined(MBEDTLS_MD_C)
3150 if( PSA_ALG_IS_HMAC( operation->alg ) )
3151 {
3152 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3153 input_length );
3154 }
3155 else
3156#endif /* MBEDTLS_MD_C */
3157 {
3158 /* This shouldn't happen if `operation` was initialized by
3159 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003160 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003161 }
3162
Gilles Peskinefbfac682018-07-08 20:51:54 +02003163 if( status != PSA_SUCCESS )
3164 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003165 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003166}
3167
Gilles Peskine01126fa2018-07-12 17:04:55 +02003168#if defined(MBEDTLS_MD_C)
3169static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3170 uint8_t *mac,
3171 size_t mac_size )
3172{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003173 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003174 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3175 size_t hash_size = 0;
3176 size_t block_size = psa_get_hash_block_size( hash_alg );
3177 psa_status_t status;
3178
Gilles Peskine01126fa2018-07-12 17:04:55 +02003179 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3180 if( status != PSA_SUCCESS )
3181 return( status );
3182 /* From here on, tmp needs to be wiped. */
3183
3184 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3185 if( status != PSA_SUCCESS )
3186 goto exit;
3187
3188 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3189 if( status != PSA_SUCCESS )
3190 goto exit;
3191
3192 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3193 if( status != PSA_SUCCESS )
3194 goto exit;
3195
Gilles Peskined911eb72018-08-14 15:18:45 +02003196 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3197 if( status != PSA_SUCCESS )
3198 goto exit;
3199
3200 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003201
3202exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003203 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003204 return( status );
3205}
3206#endif /* MBEDTLS_MD_C */
3207
mohammad16036df908f2018-04-02 08:34:15 -07003208static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003209 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003210 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003211{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003212 if( ! operation->key_set )
3213 return( PSA_ERROR_BAD_STATE );
3214 if( operation->iv_required && ! operation->iv_set )
3215 return( PSA_ERROR_BAD_STATE );
3216
Gilles Peskine8c9def32018-02-08 10:02:12 +01003217 if( mac_size < operation->mac_size )
3218 return( PSA_ERROR_BUFFER_TOO_SMALL );
3219
Gilles Peskine8c9def32018-02-08 10:02:12 +01003220#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003221 if( operation->alg == PSA_ALG_CMAC )
3222 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003223 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3224 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3225 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003226 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003227 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003228 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003229 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003230 else
3231#endif /* MBEDTLS_CMAC_C */
3232#if defined(MBEDTLS_MD_C)
3233 if( PSA_ALG_IS_HMAC( operation->alg ) )
3234 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003235 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003236 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003237 }
3238 else
3239#endif /* MBEDTLS_MD_C */
3240 {
3241 /* This shouldn't happen if `operation` was initialized by
3242 * a setup function. */
3243 return( PSA_ERROR_BAD_STATE );
3244 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003245}
3246
Gilles Peskineacd4be32018-07-08 19:56:25 +02003247psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3248 uint8_t *mac,
3249 size_t mac_size,
3250 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003251{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003252 psa_status_t status;
3253
Jaeden Amero252ef282019-02-15 14:05:35 +00003254 if( operation->alg == 0 )
3255 {
3256 return( PSA_ERROR_BAD_STATE );
3257 }
3258
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003259 /* Fill the output buffer with something that isn't a valid mac
3260 * (barring an attack on the mac and deliberately-crafted input),
3261 * in case the caller doesn't check the return status properly. */
3262 *mac_length = mac_size;
3263 /* If mac_size is 0 then mac may be NULL and then the
3264 * call to memset would have undefined behavior. */
3265 if( mac_size != 0 )
3266 memset( mac, '!', mac_size );
3267
Gilles Peskine89167cb2018-07-08 20:12:23 +02003268 if( ! operation->is_sign )
3269 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003270 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003271 }
mohammad16036df908f2018-04-02 08:34:15 -07003272
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003273 status = psa_mac_finish_internal( operation, mac, mac_size );
3274
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003275 if( status == PSA_SUCCESS )
3276 {
3277 status = psa_mac_abort( operation );
3278 if( status == PSA_SUCCESS )
3279 *mac_length = operation->mac_size;
3280 else
3281 memset( mac, '!', mac_size );
3282 }
3283 else
3284 psa_mac_abort( operation );
3285 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003286}
3287
Gilles Peskineacd4be32018-07-08 19:56:25 +02003288psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3289 const uint8_t *mac,
3290 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003291{
Gilles Peskine828ed142018-06-18 23:25:51 +02003292 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003293 psa_status_t status;
3294
Jaeden Amero252ef282019-02-15 14:05:35 +00003295 if( operation->alg == 0 )
3296 {
3297 return( PSA_ERROR_BAD_STATE );
3298 }
3299
Gilles Peskine89167cb2018-07-08 20:12:23 +02003300 if( operation->is_sign )
3301 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003302 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003303 }
3304 if( operation->mac_size != mac_length )
3305 {
3306 status = PSA_ERROR_INVALID_SIGNATURE;
3307 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003308 }
mohammad16036df908f2018-04-02 08:34:15 -07003309
3310 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003311 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003312 if( status != PSA_SUCCESS )
3313 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003314
3315 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3316 status = PSA_ERROR_INVALID_SIGNATURE;
3317
3318cleanup:
3319 if( status == PSA_SUCCESS )
3320 status = psa_mac_abort( operation );
3321 else
3322 psa_mac_abort( operation );
3323
Gilles Peskine3f108122018-12-07 18:14:53 +01003324 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003325
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003326 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003327}
3328
3329
Gilles Peskine20035e32018-02-03 22:44:14 +01003330
Gilles Peskine20035e32018-02-03 22:44:14 +01003331/****************************************************************/
3332/* Asymmetric cryptography */
3333/****************************************************************/
3334
Gilles Peskine2b450e32018-06-27 15:42:46 +02003335#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003336/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003337 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003338static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3339 size_t hash_length,
3340 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003341{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003342 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003343 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003344 *md_alg = mbedtls_md_get_type( md_info );
3345
3346 /* The Mbed TLS RSA module uses an unsigned int for hash length
3347 * parameters. Validate that it fits so that we don't risk an
3348 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003349#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003350 if( hash_length > UINT_MAX )
3351 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003352#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003353
3354#if defined(MBEDTLS_PKCS1_V15)
3355 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3356 * must be correct. */
3357 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3358 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003359 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003360 if( md_info == NULL )
3361 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003362 if( mbedtls_md_get_size( md_info ) != hash_length )
3363 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003364 }
3365#endif /* MBEDTLS_PKCS1_V15 */
3366
3367#if defined(MBEDTLS_PKCS1_V21)
3368 /* PSS requires a hash internally. */
3369 if( PSA_ALG_IS_RSA_PSS( alg ) )
3370 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003371 if( md_info == NULL )
3372 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003373 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003374#endif /* MBEDTLS_PKCS1_V21 */
3375
Gilles Peskine61b91d42018-06-08 16:09:36 +02003376 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003377}
3378
Gilles Peskine2b450e32018-06-27 15:42:46 +02003379static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3380 psa_algorithm_t alg,
3381 const uint8_t *hash,
3382 size_t hash_length,
3383 uint8_t *signature,
3384 size_t signature_size,
3385 size_t *signature_length )
3386{
3387 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003389 mbedtls_md_type_t md_alg;
3390
3391 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3392 if( status != PSA_SUCCESS )
3393 return( status );
3394
Gilles Peskine630a18a2018-06-29 17:49:35 +02003395 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003396 return( PSA_ERROR_BUFFER_TOO_SMALL );
3397
3398#if defined(MBEDTLS_PKCS1_V15)
3399 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3400 {
3401 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3402 MBEDTLS_MD_NONE );
3403 ret = mbedtls_rsa_pkcs1_sign( rsa,
3404 mbedtls_ctr_drbg_random,
3405 &global_data.ctr_drbg,
3406 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003407 md_alg,
3408 (unsigned int) hash_length,
3409 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003410 signature );
3411 }
3412 else
3413#endif /* MBEDTLS_PKCS1_V15 */
3414#if defined(MBEDTLS_PKCS1_V21)
3415 if( PSA_ALG_IS_RSA_PSS( alg ) )
3416 {
3417 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3418 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3419 mbedtls_ctr_drbg_random,
3420 &global_data.ctr_drbg,
3421 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003422 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003423 (unsigned int) hash_length,
3424 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003425 signature );
3426 }
3427 else
3428#endif /* MBEDTLS_PKCS1_V21 */
3429 {
3430 return( PSA_ERROR_INVALID_ARGUMENT );
3431 }
3432
3433 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003434 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003435 return( mbedtls_to_psa_error( ret ) );
3436}
3437
3438static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3439 psa_algorithm_t alg,
3440 const uint8_t *hash,
3441 size_t hash_length,
3442 const uint8_t *signature,
3443 size_t signature_length )
3444{
3445 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003447 mbedtls_md_type_t md_alg;
3448
3449 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3450 if( status != PSA_SUCCESS )
3451 return( status );
3452
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003453 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3454 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003455
3456#if defined(MBEDTLS_PKCS1_V15)
3457 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3458 {
3459 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3460 MBEDTLS_MD_NONE );
3461 ret = mbedtls_rsa_pkcs1_verify( rsa,
3462 mbedtls_ctr_drbg_random,
3463 &global_data.ctr_drbg,
3464 MBEDTLS_RSA_PUBLIC,
3465 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003466 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003467 hash,
3468 signature );
3469 }
3470 else
3471#endif /* MBEDTLS_PKCS1_V15 */
3472#if defined(MBEDTLS_PKCS1_V21)
3473 if( PSA_ALG_IS_RSA_PSS( alg ) )
3474 {
3475 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3476 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3477 mbedtls_ctr_drbg_random,
3478 &global_data.ctr_drbg,
3479 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003480 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003481 (unsigned int) hash_length,
3482 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003483 signature );
3484 }
3485 else
3486#endif /* MBEDTLS_PKCS1_V21 */
3487 {
3488 return( PSA_ERROR_INVALID_ARGUMENT );
3489 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003490
3491 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3492 * the rest of the signature is invalid". This has little use in
3493 * practice and PSA doesn't report this distinction. */
3494 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3495 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003496 return( mbedtls_to_psa_error( ret ) );
3497}
3498#endif /* MBEDTLS_RSA_C */
3499
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003500#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003501/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3502 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3503 * (even though these functions don't modify it). */
3504static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3505 psa_algorithm_t alg,
3506 const uint8_t *hash,
3507 size_t hash_length,
3508 uint8_t *signature,
3509 size_t signature_size,
3510 size_t *signature_length )
3511{
Janos Follath24eed8d2019-11-22 13:21:35 +00003512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003513 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003514 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003515 mbedtls_mpi_init( &r );
3516 mbedtls_mpi_init( &s );
3517
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003518 if( signature_size < 2 * curve_bytes )
3519 {
3520 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3521 goto cleanup;
3522 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003523
Gilles Peskinea05219c2018-11-16 16:02:56 +01003524#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003525 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3526 {
3527 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3528 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3529 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003530 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3531 &ecp->d, hash,
3532 hash_length, md_alg,
3533 mbedtls_ctr_drbg_random,
3534 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003535 }
3536 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003537#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003538 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003539 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003540 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3541 hash, hash_length,
3542 mbedtls_ctr_drbg_random,
3543 &global_data.ctr_drbg ) );
3544 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003545
3546 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3547 signature,
3548 curve_bytes ) );
3549 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3550 signature + curve_bytes,
3551 curve_bytes ) );
3552
3553cleanup:
3554 mbedtls_mpi_free( &r );
3555 mbedtls_mpi_free( &s );
3556 if( ret == 0 )
3557 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003558 return( mbedtls_to_psa_error( ret ) );
3559}
3560
3561static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3562 const uint8_t *hash,
3563 size_t hash_length,
3564 const uint8_t *signature,
3565 size_t signature_length )
3566{
Janos Follath24eed8d2019-11-22 13:21:35 +00003567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003568 mbedtls_mpi r, s;
3569 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3570 mbedtls_mpi_init( &r );
3571 mbedtls_mpi_init( &s );
3572
3573 if( signature_length != 2 * curve_bytes )
3574 return( PSA_ERROR_INVALID_SIGNATURE );
3575
3576 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3577 signature,
3578 curve_bytes ) );
3579 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3580 signature + curve_bytes,
3581 curve_bytes ) );
3582
3583 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3584 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003585
3586cleanup:
3587 mbedtls_mpi_free( &r );
3588 mbedtls_mpi_free( &s );
3589 return( mbedtls_to_psa_error( ret ) );
3590}
3591#endif /* MBEDTLS_ECDSA_C */
3592
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003593psa_status_t psa_sign_hash( psa_key_handle_t handle,
3594 psa_algorithm_t alg,
3595 const uint8_t *hash,
3596 size_t hash_length,
3597 uint8_t *signature,
3598 size_t signature_size,
3599 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003600{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003601 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003602 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003603#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3604 const psa_drv_se_t *drv;
3605 psa_drv_se_context_t *drv_context;
3606#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003607
3608 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003609 /* Immediately reject a zero-length signature buffer. This guarantees
3610 * that signature must be a valid pointer. (On the other hand, the hash
3611 * buffer can in principle be empty since it doesn't actually have
3612 * to be a hash.) */
3613 if( signature_size == 0 )
3614 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003615
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003616 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003617 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003618 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003619 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003620 {
3621 status = PSA_ERROR_INVALID_ARGUMENT;
3622 goto exit;
3623 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003624
Gilles Peskineedc64242019-08-07 21:05:07 +02003625#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3626 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3627 {
3628 if( drv->asymmetric == NULL ||
3629 drv->asymmetric->p_sign == NULL )
3630 {
3631 status = PSA_ERROR_NOT_SUPPORTED;
3632 goto exit;
3633 }
3634 status = drv->asymmetric->p_sign( drv_context,
3635 slot->data.se.slot_number,
3636 alg,
3637 hash, hash_length,
3638 signature, signature_size,
3639 signature_length );
3640 }
3641 else
3642#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003643#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003644 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003645 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003646 mbedtls_rsa_context rsa;
3647 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3648
3649 status = psa_load_rsa_representation( slot,
3650 &rsa );
3651 if( status != PSA_SUCCESS )
3652 goto exit;
3653
3654 status = psa_rsa_sign( &rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003655 alg,
3656 hash, hash_length,
3657 signature, signature_size,
3658 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003659
3660 mbedtls_rsa_free( &rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003661 }
3662 else
3663#endif /* defined(MBEDTLS_RSA_C) */
3664#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003665 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003666 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003667#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003668 if(
3669#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3670 PSA_ALG_IS_ECDSA( alg )
3671#else
3672 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3673#endif
3674 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003675 status = psa_ecdsa_sign( slot->data.ecp,
3676 alg,
3677 hash, hash_length,
3678 signature, signature_size,
3679 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003680 else
3681#endif /* defined(MBEDTLS_ECDSA_C) */
3682 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003683 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003684 }
itayzafrir5c753392018-05-08 11:18:38 +03003685 }
3686 else
3687#endif /* defined(MBEDTLS_ECP_C) */
3688 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003689 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003690 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003691
3692exit:
3693 /* Fill the unused part of the output buffer (the whole buffer on error,
3694 * the trailing part on success) with something that isn't a valid mac
3695 * (barring an attack on the mac and deliberately-crafted input),
3696 * in case the caller doesn't check the return status properly. */
3697 if( status == PSA_SUCCESS )
3698 memset( signature + *signature_length, '!',
3699 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003700 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003701 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003702 /* If signature_size is 0 then we have nothing to do. We must not call
3703 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003704 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003705}
3706
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003707psa_status_t psa_verify_hash( psa_key_handle_t handle,
3708 psa_algorithm_t alg,
3709 const uint8_t *hash,
3710 size_t hash_length,
3711 const uint8_t *signature,
3712 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003713{
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;
Gilles Peskineedc64242019-08-07 21:05:07 +02003716#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3717 const psa_drv_se_t *drv;
3718 psa_drv_se_context_t *drv_context;
3719#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003720
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003721 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003722 if( status != PSA_SUCCESS )
3723 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003724
Gilles Peskineedc64242019-08-07 21:05:07 +02003725#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3726 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3727 {
3728 if( drv->asymmetric == NULL ||
3729 drv->asymmetric->p_verify == NULL )
3730 return( PSA_ERROR_NOT_SUPPORTED );
3731 return( drv->asymmetric->p_verify( drv_context,
3732 slot->data.se.slot_number,
3733 alg,
3734 hash, hash_length,
3735 signature, signature_length ) );
3736 }
3737 else
3738#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003739#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003740 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003741 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003742 mbedtls_rsa_context rsa;
3743 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3744
3745 status = psa_load_rsa_representation( slot, &rsa );
3746 if( status != PSA_SUCCESS )
3747 return status;
3748
3749 status = psa_rsa_verify( &rsa,
3750 alg,
3751 hash, hash_length,
3752 signature, signature_length );
3753 mbedtls_rsa_free( &rsa );
3754 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003755 }
3756 else
3757#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003758#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003759 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003760 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003761#if defined(MBEDTLS_ECDSA_C)
3762 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003763 return( psa_ecdsa_verify( slot->data.ecp,
3764 hash, hash_length,
3765 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003766 else
3767#endif /* defined(MBEDTLS_ECDSA_C) */
3768 {
3769 return( PSA_ERROR_INVALID_ARGUMENT );
3770 }
itayzafrir5c753392018-05-08 11:18:38 +03003771 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003772 else
3773#endif /* defined(MBEDTLS_ECP_C) */
3774 {
3775 return( PSA_ERROR_NOT_SUPPORTED );
3776 }
3777}
3778
Gilles Peskine072ac562018-06-30 00:21:29 +02003779#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3780static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3781 mbedtls_rsa_context *rsa )
3782{
3783 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3784 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3785 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3786 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3787}
3788#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3789
Gilles Peskinec5487a82018-12-03 18:08:14 +01003790psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003791 psa_algorithm_t alg,
3792 const uint8_t *input,
3793 size_t input_length,
3794 const uint8_t *salt,
3795 size_t salt_length,
3796 uint8_t *output,
3797 size_t output_size,
3798 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003799{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003800 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003801 psa_status_t status;
3802
Darryl Green5cc689a2018-07-24 15:34:10 +01003803 (void) input;
3804 (void) input_length;
3805 (void) salt;
3806 (void) output;
3807 (void) output_size;
3808
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003809 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003810
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003811 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3812 return( PSA_ERROR_INVALID_ARGUMENT );
3813
Gilles Peskine28f8f302019-07-24 13:30:31 +02003814 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003815 if( status != PSA_SUCCESS )
3816 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003817 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3818 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003819 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003820
3821#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003822 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003823 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003824 mbedtls_rsa_context rsa;
3825 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3826
3827 status = psa_load_rsa_representation( slot, &rsa );
3828 if( status != PSA_SUCCESS )
3829 return status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003830 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003831 if( output_size < mbedtls_rsa_get_len( &rsa ) )
3832 {
3833 mbedtls_rsa_free( &rsa );
Vikas Katariya21599b62019-08-02 12:26:29 +01003834 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooremana01795d2020-07-24 22:48:15 +02003835 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003836#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003837 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003838 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003839 ret = mbedtls_rsa_pkcs1_encrypt( &rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003840 mbedtls_ctr_drbg_random,
3841 &global_data.ctr_drbg,
3842 MBEDTLS_RSA_PUBLIC,
3843 input_length,
3844 input,
3845 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003846 }
3847 else
3848#endif /* MBEDTLS_PKCS1_V15 */
3849#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003850 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003851 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003852 psa_rsa_oaep_set_padding_mode( alg, &rsa );
3853 ret = mbedtls_rsa_rsaes_oaep_encrypt( &rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003854 mbedtls_ctr_drbg_random,
3855 &global_data.ctr_drbg,
3856 MBEDTLS_RSA_PUBLIC,
3857 salt, salt_length,
3858 input_length,
3859 input,
3860 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003861 }
3862 else
3863#endif /* MBEDTLS_PKCS1_V21 */
3864 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003865 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003866 return( PSA_ERROR_INVALID_ARGUMENT );
3867 }
3868 if( ret == 0 )
Steven Cooremana01795d2020-07-24 22:48:15 +02003869 *output_length = mbedtls_rsa_get_len( &rsa );
3870
3871 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003872 return( mbedtls_to_psa_error( ret ) );
3873 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003874 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003875#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876 {
3877 return( PSA_ERROR_NOT_SUPPORTED );
3878 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003879}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003880
Gilles Peskinec5487a82018-12-03 18:08:14 +01003881psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003882 psa_algorithm_t alg,
3883 const uint8_t *input,
3884 size_t input_length,
3885 const uint8_t *salt,
3886 size_t salt_length,
3887 uint8_t *output,
3888 size_t output_size,
3889 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003890{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003891 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003892 psa_status_t status;
3893
Darryl Green5cc689a2018-07-24 15:34:10 +01003894 (void) input;
3895 (void) input_length;
3896 (void) salt;
3897 (void) output;
3898 (void) output_size;
3899
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003900 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003902 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3903 return( PSA_ERROR_INVALID_ARGUMENT );
3904
Gilles Peskine28f8f302019-07-24 13:30:31 +02003905 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003906 if( status != PSA_SUCCESS )
3907 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003908 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003909 return( PSA_ERROR_INVALID_ARGUMENT );
3910
3911#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003912 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003913 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003914 mbedtls_rsa_context rsa;
3915 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3916
3917 status = psa_load_rsa_representation( slot, &rsa );
3918 if( status != PSA_SUCCESS )
3919 return status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003920 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003921
Steven Cooremana01795d2020-07-24 22:48:15 +02003922 if( input_length != mbedtls_rsa_get_len( &rsa ) )
3923 {
3924 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003925 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana01795d2020-07-24 22:48:15 +02003926 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003927
3928#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003929 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003930 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003931 ret = mbedtls_rsa_pkcs1_decrypt( &rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003932 mbedtls_ctr_drbg_random,
3933 &global_data.ctr_drbg,
3934 MBEDTLS_RSA_PRIVATE,
3935 output_length,
3936 input,
3937 output,
3938 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003939 }
3940 else
3941#endif /* MBEDTLS_PKCS1_V15 */
3942#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003943 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003945 psa_rsa_oaep_set_padding_mode( alg, &rsa );
3946 ret = mbedtls_rsa_rsaes_oaep_decrypt( &rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003947 mbedtls_ctr_drbg_random,
3948 &global_data.ctr_drbg,
3949 MBEDTLS_RSA_PRIVATE,
3950 salt, salt_length,
3951 output_length,
3952 input,
3953 output,
3954 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003955 }
3956 else
3957#endif /* MBEDTLS_PKCS1_V21 */
3958 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003959 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003960 return( PSA_ERROR_INVALID_ARGUMENT );
3961 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003962
Steven Cooremana01795d2020-07-24 22:48:15 +02003963 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003964 return( mbedtls_to_psa_error( ret ) );
3965 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003966 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003967#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968 {
3969 return( PSA_ERROR_NOT_SUPPORTED );
3970 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003971}
Gilles Peskine20035e32018-02-03 22:44:14 +01003972
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003973
3974
mohammad1603503973b2018-03-12 15:59:30 +02003975/****************************************************************/
3976/* Symmetric cryptography */
3977/****************************************************************/
3978
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003979/* Initialize the cipher operation structure. Once this function has been
3980 * called, psa_cipher_abort can run and will do the right thing. */
3981static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3982 psa_algorithm_t alg )
3983{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003984 if( ! PSA_ALG_IS_CIPHER( alg ) )
3985 {
3986 memset( operation, 0, sizeof( *operation ) );
3987 return( PSA_ERROR_INVALID_ARGUMENT );
3988 }
3989
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003990 operation->alg = alg;
3991 operation->key_set = 0;
3992 operation->iv_set = 0;
3993 operation->iv_required = 1;
3994 operation->iv_size = 0;
3995 operation->block_size = 0;
3996 mbedtls_cipher_init( &operation->ctx.cipher );
3997 return( PSA_SUCCESS );
3998}
3999
Gilles Peskinee553c652018-06-04 16:22:46 +02004000static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004001 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004002 psa_algorithm_t alg,
4003 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004004{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004005 int ret = 0;
4006 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004007 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004008 size_t key_bits;
4009 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004010 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4011 PSA_KEY_USAGE_ENCRYPT :
4012 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004013
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004014 /* A context must be freshly initialized before it can be set up. */
4015 if( operation->alg != 0 )
4016 {
4017 return( PSA_ERROR_BAD_STATE );
4018 }
4019
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004020 status = psa_cipher_init( operation, alg );
4021 if( status != PSA_SUCCESS )
4022 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004023
Gilles Peskine28f8f302019-07-24 13:30:31 +02004024 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004025 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004026 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004027 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004028
Gilles Peskine8e338702019-07-30 20:06:31 +02004029 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004030 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004031 {
4032 status = PSA_ERROR_NOT_SUPPORTED;
4033 goto exit;
4034 }
mohammad1603503973b2018-03-12 15:59:30 +02004035
mohammad1603503973b2018-03-12 15:59:30 +02004036 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004037 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004038 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004039
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004040#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004041 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004042 {
4043 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004044 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004045 memcpy( keys, slot->data.key.data, 16 );
4046 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004047 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4048 keys,
4049 192, cipher_operation );
4050 }
4051 else
4052#endif
4053 {
4054 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004055 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004056 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004057 }
Moran Peker41deec42018-04-04 15:43:05 +03004058 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004059 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004060
mohammad16038481e742018-03-18 13:57:31 +02004061#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004062 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004063 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004064 case PSA_ALG_CBC_NO_PADDING:
4065 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4066 MBEDTLS_PADDING_NONE );
4067 break;
4068 case PSA_ALG_CBC_PKCS7:
4069 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4070 MBEDTLS_PADDING_PKCS7 );
4071 break;
4072 default:
4073 /* The algorithm doesn't involve padding. */
4074 ret = 0;
4075 break;
4076 }
4077 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004078 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004079#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4080
mohammad1603503973b2018-03-12 15:59:30 +02004081 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004082 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004083 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004084 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004085 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004086 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004087 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004088#if defined(MBEDTLS_CHACHA20_C)
4089 else
4090 if( alg == PSA_ALG_CHACHA20 )
4091 operation->iv_size = 12;
4092#endif
mohammad1603503973b2018-03-12 15:59:30 +02004093
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004094exit:
4095 if( status == 0 )
4096 status = mbedtls_to_psa_error( ret );
4097 if( status != 0 )
4098 psa_cipher_abort( operation );
4099 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004100}
4101
Gilles Peskinefe119512018-07-08 21:39:34 +02004102psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004103 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004104 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004105{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004106 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004107}
4108
Gilles Peskinefe119512018-07-08 21:39:34 +02004109psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004110 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004111 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004112{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004113 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004114}
4115
Gilles Peskinefe119512018-07-08 21:39:34 +02004116psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004117 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004118 size_t iv_size,
4119 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004120{
itayzafrir534bd7c2018-08-02 13:56:32 +03004121 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004123 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004124 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004125 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004126 }
Moran Peker41deec42018-04-04 15:43:05 +03004127 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004128 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004129 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004130 goto exit;
4131 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004132 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4133 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004134 if( ret != 0 )
4135 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004136 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004137 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004138 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004139
mohammad16038481e742018-03-18 13:57:31 +02004140 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004141 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004142
Moran Peker395db872018-05-31 14:07:14 +03004143exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004144 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004145 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004146 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004147}
4148
Gilles Peskinefe119512018-07-08 21:39:34 +02004149psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004150 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004151 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004152{
itayzafrir534bd7c2018-08-02 13:56:32 +03004153 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004154 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004155 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004156 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004157 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004158 }
Moran Pekera28258c2018-05-29 16:25:04 +03004159 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004160 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004161 status = PSA_ERROR_INVALID_ARGUMENT;
4162 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004163 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004164 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4165 status = mbedtls_to_psa_error( ret );
4166exit:
4167 if( status == PSA_SUCCESS )
4168 operation->iv_set = 1;
4169 else
mohammad1603503973b2018-03-12 15:59:30 +02004170 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004171 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004172}
4173
Gilles Peskinee553c652018-06-04 16:22:46 +02004174psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4175 const uint8_t *input,
4176 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004177 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004178 size_t output_size,
4179 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004180{
itayzafrir534bd7c2018-08-02 13:56:32 +03004181 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004182 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004183 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004184
4185 if( operation->alg == 0 )
4186 {
4187 return( PSA_ERROR_BAD_STATE );
4188 }
4189
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004190 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004191 {
4192 /* Take the unprocessed partial block left over from previous
4193 * update calls, if any, plus the input to this call. Remove
4194 * the last partial block, if any. You get the data that will be
4195 * output in this call. */
4196 expected_output_size =
4197 ( operation->ctx.cipher.unprocessed_len + input_length )
4198 / operation->block_size * operation->block_size;
4199 }
4200 else
4201 {
4202 expected_output_size = input_length;
4203 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004204
Gilles Peskine89d789c2018-06-04 17:17:16 +02004205 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004206 {
4207 status = PSA_ERROR_BUFFER_TOO_SMALL;
4208 goto exit;
4209 }
mohammad160382759612018-03-12 18:16:40 +02004210
mohammad1603503973b2018-03-12 15:59:30 +02004211 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004212 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004213 status = mbedtls_to_psa_error( ret );
4214exit:
4215 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004216 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004217 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004218}
4219
Gilles Peskinee553c652018-06-04 16:22:46 +02004220psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4221 uint8_t *output,
4222 size_t output_size,
4223 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004224{
David Saadab4ecc272019-02-14 13:48:10 +02004225 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004226 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004227 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004228
mohammad1603503973b2018-03-12 15:59:30 +02004229 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004230 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004231 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004232 }
4233 if( operation->iv_required && ! operation->iv_set )
4234 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004235 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004236 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004237
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004238 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004239 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4240 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004241 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004242 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004243 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004244 }
4245
Janos Follath315b51c2018-07-09 16:04:51 +01004246 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4247 temp_output_buffer,
4248 output_length );
4249 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004250 {
Janos Follath315b51c2018-07-09 16:04:51 +01004251 status = mbedtls_to_psa_error( cipher_ret );
4252 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004253 }
Janos Follath315b51c2018-07-09 16:04:51 +01004254
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004255 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004256 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004257 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004258 memcpy( output, temp_output_buffer, *output_length );
4259 else
4260 {
Janos Follath315b51c2018-07-09 16:04:51 +01004261 status = PSA_ERROR_BUFFER_TOO_SMALL;
4262 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004263 }
mohammad1603503973b2018-03-12 15:59:30 +02004264
Gilles Peskine3f108122018-12-07 18:14:53 +01004265 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004266 status = psa_cipher_abort( operation );
4267
4268 return( status );
4269
4270error:
4271
4272 *output_length = 0;
4273
Gilles Peskine3f108122018-12-07 18:14:53 +01004274 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004275 (void) psa_cipher_abort( operation );
4276
4277 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004278}
4279
Gilles Peskinee553c652018-06-04 16:22:46 +02004280psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4281{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004282 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004283 {
4284 /* The object has (apparently) been initialized but it is not
4285 * in use. It's ok to call abort on such an object, and there's
4286 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004287 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004288 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004289
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004290 /* Sanity check (shouldn't happen: operation->alg should
4291 * always have been initialized to a valid value). */
4292 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4293 return( PSA_ERROR_BAD_STATE );
4294
mohammad1603503973b2018-03-12 15:59:30 +02004295 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004296
Moran Peker41deec42018-04-04 15:43:05 +03004297 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004298 operation->key_set = 0;
4299 operation->iv_set = 0;
4300 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004301 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004302 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004303
Moran Peker395db872018-05-31 14:07:14 +03004304 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004305}
4306
Gilles Peskinea0655c32018-04-30 17:06:50 +02004307
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004308
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004309
mohammad16035955c982018-04-26 00:53:03 +03004310/****************************************************************/
4311/* AEAD */
4312/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004313
Gilles Peskineedf9a652018-08-17 18:11:56 +02004314typedef struct
4315{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004316 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004317 const mbedtls_cipher_info_t *cipher_info;
4318 union
4319 {
4320#if defined(MBEDTLS_CCM_C)
4321 mbedtls_ccm_context ccm;
4322#endif /* MBEDTLS_CCM_C */
4323#if defined(MBEDTLS_GCM_C)
4324 mbedtls_gcm_context gcm;
4325#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004326#if defined(MBEDTLS_CHACHAPOLY_C)
4327 mbedtls_chachapoly_context chachapoly;
4328#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004329 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004330 psa_algorithm_t core_alg;
4331 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004332 uint8_t tag_length;
4333} aead_operation_t;
4334
Gilles Peskine30a9e412019-01-14 18:36:12 +01004335static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004336{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004337 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004338 {
4339#if defined(MBEDTLS_CCM_C)
4340 case PSA_ALG_CCM:
4341 mbedtls_ccm_free( &operation->ctx.ccm );
4342 break;
4343#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004344#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004345 case PSA_ALG_GCM:
4346 mbedtls_gcm_free( &operation->ctx.gcm );
4347 break;
4348#endif /* MBEDTLS_GCM_C */
4349 }
4350}
4351
4352static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004353 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004354 psa_key_usage_t usage,
4355 psa_algorithm_t alg )
4356{
4357 psa_status_t status;
4358 size_t key_bits;
4359 mbedtls_cipher_id_t cipher_id;
4360
Gilles Peskine28f8f302019-07-24 13:30:31 +02004361 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004362 if( status != PSA_SUCCESS )
4363 return( status );
4364
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004365 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004366
4367 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004368 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004369 &cipher_id );
4370 if( operation->cipher_info == NULL )
4371 return( PSA_ERROR_NOT_SUPPORTED );
4372
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004373 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004374 {
4375#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004376 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4377 operation->core_alg = PSA_ALG_CCM;
4378 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004379 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4380 * The call to mbedtls_ccm_encrypt_and_tag or
4381 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004382 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004383 return( PSA_ERROR_INVALID_ARGUMENT );
4384 mbedtls_ccm_init( &operation->ctx.ccm );
4385 status = mbedtls_to_psa_error(
4386 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004387 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004388 (unsigned int) key_bits ) );
4389 if( status != 0 )
4390 goto cleanup;
4391 break;
4392#endif /* MBEDTLS_CCM_C */
4393
4394#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004395 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4396 operation->core_alg = PSA_ALG_GCM;
4397 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004398 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4399 * The call to mbedtls_gcm_crypt_and_tag or
4400 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004401 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004402 return( PSA_ERROR_INVALID_ARGUMENT );
4403 mbedtls_gcm_init( &operation->ctx.gcm );
4404 status = mbedtls_to_psa_error(
4405 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004406 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004407 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004408 if( status != 0 )
4409 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004410 break;
4411#endif /* MBEDTLS_GCM_C */
4412
Gilles Peskine26869f22019-05-06 15:25:00 +02004413#if defined(MBEDTLS_CHACHAPOLY_C)
4414 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4415 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4416 operation->full_tag_length = 16;
4417 /* We only support the default tag length. */
4418 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4419 return( PSA_ERROR_NOT_SUPPORTED );
4420 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4421 status = mbedtls_to_psa_error(
4422 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004423 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004424 if( status != 0 )
4425 goto cleanup;
4426 break;
4427#endif /* MBEDTLS_CHACHAPOLY_C */
4428
Gilles Peskineedf9a652018-08-17 18:11:56 +02004429 default:
4430 return( PSA_ERROR_NOT_SUPPORTED );
4431 }
4432
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004433 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4434 {
4435 status = PSA_ERROR_INVALID_ARGUMENT;
4436 goto cleanup;
4437 }
4438 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004439
Gilles Peskineedf9a652018-08-17 18:11:56 +02004440 return( PSA_SUCCESS );
4441
4442cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004443 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004444 return( status );
4445}
4446
Gilles Peskinec5487a82018-12-03 18:08:14 +01004447psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004448 psa_algorithm_t alg,
4449 const uint8_t *nonce,
4450 size_t nonce_length,
4451 const uint8_t *additional_data,
4452 size_t additional_data_length,
4453 const uint8_t *plaintext,
4454 size_t plaintext_length,
4455 uint8_t *ciphertext,
4456 size_t ciphertext_size,
4457 size_t *ciphertext_length )
4458{
mohammad16035955c982018-04-26 00:53:03 +03004459 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004460 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004461 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004462
mohammad1603f08a5502018-06-03 15:05:47 +03004463 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004464
Gilles Peskinec5487a82018-12-03 18:08:14 +01004465 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004466 if( status != PSA_SUCCESS )
4467 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004468
Gilles Peskineedf9a652018-08-17 18:11:56 +02004469 /* For all currently supported modes, the tag is at the end of the
4470 * ciphertext. */
4471 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4472 {
4473 status = PSA_ERROR_BUFFER_TOO_SMALL;
4474 goto exit;
4475 }
4476 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004477
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004478#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004479 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004480 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004481 status = mbedtls_to_psa_error(
4482 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4483 MBEDTLS_GCM_ENCRYPT,
4484 plaintext_length,
4485 nonce, nonce_length,
4486 additional_data, additional_data_length,
4487 plaintext, ciphertext,
4488 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004489 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004490 else
4491#endif /* MBEDTLS_GCM_C */
4492#if defined(MBEDTLS_CCM_C)
4493 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004494 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004495 status = mbedtls_to_psa_error(
4496 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4497 plaintext_length,
4498 nonce, nonce_length,
4499 additional_data,
4500 additional_data_length,
4501 plaintext, ciphertext,
4502 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004503 }
mohammad16035c8845f2018-05-09 05:40:09 -07004504 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004505#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004506#if defined(MBEDTLS_CHACHAPOLY_C)
4507 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4508 {
4509 if( nonce_length != 12 || operation.tag_length != 16 )
4510 {
4511 status = PSA_ERROR_NOT_SUPPORTED;
4512 goto exit;
4513 }
4514 status = mbedtls_to_psa_error(
4515 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4516 plaintext_length,
4517 nonce,
4518 additional_data,
4519 additional_data_length,
4520 plaintext,
4521 ciphertext,
4522 tag ) );
4523 }
4524 else
4525#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004526 {
mohammad1603554faad2018-06-03 15:07:38 +03004527 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004528 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004529
Gilles Peskineedf9a652018-08-17 18:11:56 +02004530 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4531 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004532
Gilles Peskineedf9a652018-08-17 18:11:56 +02004533exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004534 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004535 if( status == PSA_SUCCESS )
4536 *ciphertext_length = plaintext_length + operation.tag_length;
4537 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004538}
4539
Gilles Peskineee652a32018-06-01 19:23:52 +02004540/* Locate the tag in a ciphertext buffer containing the encrypted data
4541 * followed by the tag. Return the length of the part preceding the tag in
4542 * *plaintext_length. This is the size of the plaintext in modes where
4543 * the encrypted data has the same size as the plaintext, such as
4544 * CCM and GCM. */
4545static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4546 const uint8_t *ciphertext,
4547 size_t ciphertext_length,
4548 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004549 const uint8_t **p_tag )
4550{
4551 size_t payload_length;
4552 if( tag_length > ciphertext_length )
4553 return( PSA_ERROR_INVALID_ARGUMENT );
4554 payload_length = ciphertext_length - tag_length;
4555 if( payload_length > plaintext_size )
4556 return( PSA_ERROR_BUFFER_TOO_SMALL );
4557 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004558 return( PSA_SUCCESS );
4559}
4560
Gilles Peskinec5487a82018-12-03 18:08:14 +01004561psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004562 psa_algorithm_t alg,
4563 const uint8_t *nonce,
4564 size_t nonce_length,
4565 const uint8_t *additional_data,
4566 size_t additional_data_length,
4567 const uint8_t *ciphertext,
4568 size_t ciphertext_length,
4569 uint8_t *plaintext,
4570 size_t plaintext_size,
4571 size_t *plaintext_length )
4572{
mohammad16035955c982018-04-26 00:53:03 +03004573 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004574 aead_operation_t operation;
4575 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004576
Gilles Peskineee652a32018-06-01 19:23:52 +02004577 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004578
Gilles Peskinec5487a82018-12-03 18:08:14 +01004579 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004580 if( status != PSA_SUCCESS )
4581 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004582
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004583 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4584 ciphertext, ciphertext_length,
4585 plaintext_size, &tag );
4586 if( status != PSA_SUCCESS )
4587 goto exit;
4588
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004589#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004590 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004591 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004592 status = mbedtls_to_psa_error(
4593 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4594 ciphertext_length - operation.tag_length,
4595 nonce, nonce_length,
4596 additional_data,
4597 additional_data_length,
4598 tag, operation.tag_length,
4599 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004600 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004601 else
4602#endif /* MBEDTLS_GCM_C */
4603#if defined(MBEDTLS_CCM_C)
4604 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004605 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004606 status = mbedtls_to_psa_error(
4607 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4608 ciphertext_length - operation.tag_length,
4609 nonce, nonce_length,
4610 additional_data,
4611 additional_data_length,
4612 ciphertext, plaintext,
4613 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004614 }
mohammad160339574652018-06-01 04:39:53 -07004615 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004616#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004617#if defined(MBEDTLS_CHACHAPOLY_C)
4618 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4619 {
4620 if( nonce_length != 12 || operation.tag_length != 16 )
4621 {
4622 status = PSA_ERROR_NOT_SUPPORTED;
4623 goto exit;
4624 }
4625 status = mbedtls_to_psa_error(
4626 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4627 ciphertext_length - operation.tag_length,
4628 nonce,
4629 additional_data,
4630 additional_data_length,
4631 tag,
4632 ciphertext,
4633 plaintext ) );
4634 }
4635 else
4636#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004637 {
mohammad1603554faad2018-06-03 15:07:38 +03004638 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004639 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004640
Gilles Peskineedf9a652018-08-17 18:11:56 +02004641 if( status != PSA_SUCCESS && plaintext_size != 0 )
4642 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004643
Gilles Peskineedf9a652018-08-17 18:11:56 +02004644exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004645 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004646 if( status == PSA_SUCCESS )
4647 *plaintext_length = ciphertext_length - operation.tag_length;
4648 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004649}
4650
Gilles Peskinea0655c32018-04-30 17:06:50 +02004651
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004652
Gilles Peskine20035e32018-02-03 22:44:14 +01004653/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004654/* Generators */
4655/****************************************************************/
4656
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004657#define HKDF_STATE_INIT 0 /* no input yet */
4658#define HKDF_STATE_STARTED 1 /* got salt */
4659#define HKDF_STATE_KEYED 2 /* got key */
4660#define HKDF_STATE_OUTPUT 3 /* output started */
4661
Gilles Peskinecbe66502019-05-16 16:59:18 +02004662static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004664{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004665 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4666 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004667 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004669}
4670
4671
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004672psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004673{
4674 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004676 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004677 {
4678 /* The object has (apparently) been initialized but it is not
4679 * in use. It's ok to call abort on such an object, and there's
4680 * nothing to do. */
4681 }
4682 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004683#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004684 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004685 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004686 mbedtls_free( operation->ctx.hkdf.info );
4687 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004688 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004689 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004690 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004691 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004692 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004693 if( operation->ctx.tls12_prf.seed != NULL )
4694 {
4695 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4696 operation->ctx.tls12_prf.seed_length );
4697 mbedtls_free( operation->ctx.tls12_prf.seed );
4698 }
4699
4700 if( operation->ctx.tls12_prf.label != NULL )
4701 {
4702 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4703 operation->ctx.tls12_prf.label_length );
4704 mbedtls_free( operation->ctx.tls12_prf.label );
4705 }
4706
4707 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4708
4709 /* We leave the fields Ai and output_block to be erased safely by the
4710 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004711 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004712 else
4713#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004714 {
4715 status = PSA_ERROR_BAD_STATE;
4716 }
Janos Follath083036a2019-06-11 10:22:26 +01004717 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004718 return( status );
4719}
4720
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004721psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004722 size_t *capacity)
4723{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004725 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004727 return PSA_ERROR_BAD_STATE;
4728 }
4729
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004731 return( PSA_SUCCESS );
4732}
4733
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004735 size_t capacity )
4736{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004738 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004740 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004742 return( PSA_SUCCESS );
4743}
4744
Gilles Peskinebef7f142018-07-12 17:22:21 +02004745#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004746/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004747 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004748static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004749 psa_algorithm_t hash_alg,
4750 uint8_t *output,
4751 size_t output_length )
4752{
4753 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4754 psa_status_t status;
4755
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004756 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4757 return( PSA_ERROR_BAD_STATE );
4758 hkdf->state = HKDF_STATE_OUTPUT;
4759
Gilles Peskinebef7f142018-07-12 17:22:21 +02004760 while( output_length != 0 )
4761 {
4762 /* Copy what remains of the current block */
4763 uint8_t n = hash_length - hkdf->offset_in_block;
4764 if( n > output_length )
4765 n = (uint8_t) output_length;
4766 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4767 output += n;
4768 output_length -= n;
4769 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004770 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004771 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004772 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004773 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004774 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004775 * object was corrupted or if this function is called directly
4776 * inside the library. */
4777 if( hkdf->block_number == 0xff )
4778 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004779
4780 /* We need a new block */
4781 ++hkdf->block_number;
4782 hkdf->offset_in_block = 0;
4783 status = psa_hmac_setup_internal( &hkdf->hmac,
4784 hkdf->prk, hash_length,
4785 hash_alg );
4786 if( status != PSA_SUCCESS )
4787 return( status );
4788 if( hkdf->block_number != 1 )
4789 {
4790 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4791 hkdf->output_block,
4792 hash_length );
4793 if( status != PSA_SUCCESS )
4794 return( status );
4795 }
4796 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4797 hkdf->info,
4798 hkdf->info_length );
4799 if( status != PSA_SUCCESS )
4800 return( status );
4801 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4802 &hkdf->block_number, 1 );
4803 if( status != PSA_SUCCESS )
4804 return( status );
4805 status = psa_hmac_finish_internal( &hkdf->hmac,
4806 hkdf->output_block,
4807 sizeof( hkdf->output_block ) );
4808 if( status != PSA_SUCCESS )
4809 return( status );
4810 }
4811
4812 return( PSA_SUCCESS );
4813}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004814
Janos Follath7742fee2019-06-17 12:58:10 +01004815static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4816 psa_tls12_prf_key_derivation_t *tls12_prf,
4817 psa_algorithm_t alg )
4818{
4819 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4820 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004821 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4822 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004823
Janos Follath7742fee2019-06-17 12:58:10 +01004824 /* We can't be wanting more output after block 0xff, otherwise
4825 * the capacity check in psa_key_derivation_output_bytes() would have
4826 * prevented this call. It could happen only if the operation
4827 * object was corrupted or if this function is called directly
4828 * inside the library. */
4829 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004830 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004831
4832 /* We need a new block */
4833 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004834 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004835
4836 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4837 *
4838 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4839 *
4840 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4841 * HMAC_hash(secret, A(2) + seed) +
4842 * HMAC_hash(secret, A(3) + seed) + ...
4843 *
4844 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004845 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004846 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004847 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004848 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004849 * is currently extracted as `output_block` and where i is
4850 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004851 */
4852
Janos Follathea29bfb2019-06-19 12:21:20 +01004853 /* Save the hash context before using it, to preserve the hash state with
4854 * only the inner padding in it. We need this, because inner padding depends
4855 * on the key (secret in the RFC's terminology). */
4856 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4857 if( status != PSA_SUCCESS )
4858 goto cleanup;
4859
4860 /* Calculate A(i) where i = tls12_prf->block_number. */
4861 if( tls12_prf->block_number == 1 )
4862 {
4863 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4864 * the variable seed and in this instance means it in the context of the
4865 * P_hash function, where seed = label + seed.) */
4866 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4867 tls12_prf->label, tls12_prf->label_length );
4868 if( status != PSA_SUCCESS )
4869 goto cleanup;
4870 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4871 tls12_prf->seed, tls12_prf->seed_length );
4872 if( status != PSA_SUCCESS )
4873 goto cleanup;
4874 }
4875 else
4876 {
4877 /* A(i) = HMAC_hash(secret, A(i-1)) */
4878 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4879 tls12_prf->Ai, hash_length );
4880 if( status != PSA_SUCCESS )
4881 goto cleanup;
4882 }
4883
4884 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4885 tls12_prf->Ai, hash_length );
4886 if( status != PSA_SUCCESS )
4887 goto cleanup;
4888 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4889 if( status != PSA_SUCCESS )
4890 goto cleanup;
4891
4892 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4893 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4894 tls12_prf->Ai, hash_length );
4895 if( status != PSA_SUCCESS )
4896 goto cleanup;
4897 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4898 tls12_prf->label, tls12_prf->label_length );
4899 if( status != PSA_SUCCESS )
4900 goto cleanup;
4901 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4902 tls12_prf->seed, tls12_prf->seed_length );
4903 if( status != PSA_SUCCESS )
4904 goto cleanup;
4905 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4906 tls12_prf->output_block, hash_length );
4907 if( status != PSA_SUCCESS )
4908 goto cleanup;
4909 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4910 if( status != PSA_SUCCESS )
4911 goto cleanup;
4912
Janos Follath7742fee2019-06-17 12:58:10 +01004913
4914cleanup:
4915
Janos Follathea29bfb2019-06-19 12:21:20 +01004916 cleanup_status = psa_hash_abort( &backup );
4917 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4918 status = cleanup_status;
4919
4920 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004921}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004922
Janos Follath844eb0e2019-06-19 12:10:49 +01004923static psa_status_t psa_key_derivation_tls12_prf_read(
4924 psa_tls12_prf_key_derivation_t *tls12_prf,
4925 psa_algorithm_t alg,
4926 uint8_t *output,
4927 size_t output_length )
4928{
4929 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4930 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4931 psa_status_t status;
4932 uint8_t offset, length;
4933
4934 while( output_length != 0 )
4935 {
4936 /* Check if we have fully processed the current block. */
4937 if( tls12_prf->left_in_block == 0 )
4938 {
4939 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4940 alg );
4941 if( status != PSA_SUCCESS )
4942 return( status );
4943
4944 continue;
4945 }
4946
4947 if( tls12_prf->left_in_block > output_length )
4948 length = (uint8_t) output_length;
4949 else
4950 length = tls12_prf->left_in_block;
4951
4952 offset = hash_length - tls12_prf->left_in_block;
4953 memcpy( output, tls12_prf->output_block + offset, length );
4954 output += length;
4955 output_length -= length;
4956 tls12_prf->left_in_block -= length;
4957 }
4958
4959 return( PSA_SUCCESS );
4960}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004961#endif /* MBEDTLS_MD_C */
4962
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004963psa_status_t psa_key_derivation_output_bytes(
4964 psa_key_derivation_operation_t *operation,
4965 uint8_t *output,
4966 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004967{
4968 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004969 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004970
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004972 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004974 return PSA_ERROR_BAD_STATE;
4975 }
4976
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004978 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004980 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004982 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004983 goto exit;
4984 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004985 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004986 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004987 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004988 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004989 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4990 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004991 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004992 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004993 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004994 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004995 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004996
Gilles Peskinebef7f142018-07-12 17:22:21 +02004997#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004998 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004999 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005000 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005001 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005002 output, output_length );
5003 }
Janos Follathadbec812019-06-14 11:05:39 +01005004 else
Janos Follathadbec812019-06-14 11:05:39 +01005005 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005006 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005007 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005008 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005009 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005010 output_length );
5011 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005012 else
5013#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005014 {
5015 return( PSA_ERROR_BAD_STATE );
5016 }
5017
5018exit:
5019 if( status != PSA_SUCCESS )
5020 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005021 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005022 * This allows us to differentiate between exhausted operations and
5023 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5024 * operations. */
5025 psa_algorithm_t alg = operation->alg;
5026 psa_key_derivation_abort( operation );
5027 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005028 memset( output, '!', output_length );
5029 }
5030 return( status );
5031}
5032
Gilles Peskine08542d82018-07-19 17:05:42 +02005033#if defined(MBEDTLS_DES_C)
5034static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5035{
5036 if( data_size >= 8 )
5037 mbedtls_des_key_set_parity( data );
5038 if( data_size >= 16 )
5039 mbedtls_des_key_set_parity( data + 8 );
5040 if( data_size >= 24 )
5041 mbedtls_des_key_set_parity( data + 16 );
5042}
5043#endif /* MBEDTLS_DES_C */
5044
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005045static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005046 psa_key_slot_t *slot,
5047 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005048 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005049{
5050 uint8_t *data = NULL;
5051 size_t bytes = PSA_BITS_TO_BYTES( bits );
5052 psa_status_t status;
5053
Gilles Peskine8e338702019-07-30 20:06:31 +02005054 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005055 return( PSA_ERROR_INVALID_ARGUMENT );
5056 if( bits % 8 != 0 )
5057 return( PSA_ERROR_INVALID_ARGUMENT );
5058 data = mbedtls_calloc( 1, bytes );
5059 if( data == NULL )
5060 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5061
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005063 if( status != PSA_SUCCESS )
5064 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005065#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005066 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005067 psa_des_set_key_parity( data, bytes );
5068#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005069 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005070
5071exit:
5072 mbedtls_free( data );
5073 return( status );
5074}
5075
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005076psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005077 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005078 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005079{
5080 psa_status_t status;
5081 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005082 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005083
5084 /* Reject any attempt to create a zero-length key so that we don't
5085 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5086 if( psa_get_key_bits( attributes ) == 0 )
5087 return( PSA_ERROR_INVALID_ARGUMENT );
5088
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005089 if( ! operation->can_output_key )
5090 return( PSA_ERROR_NOT_PERMITTED );
5091
Gilles Peskinedf179142019-07-15 22:02:14 +02005092 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5093 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005094#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5095 if( driver != NULL )
5096 {
5097 /* Deriving a key in a secure element is not implemented yet. */
5098 status = PSA_ERROR_NOT_SUPPORTED;
5099 }
5100#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005101 if( status == PSA_SUCCESS )
5102 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005103 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005104 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005105 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005106 }
5107 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005108 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005109 if( status != PSA_SUCCESS )
5110 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005111 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005112 *handle = 0;
5113 }
5114 return( status );
5115}
5116
Gilles Peskineeab56e42018-07-12 17:12:33 +02005117
5118
5119/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005120/* Key derivation */
5121/****************************************************************/
5122
Gilles Peskine969c5d62019-01-16 15:53:06 +01005123static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005124 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005125 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005126{
Janos Follath5fe19732019-06-20 15:09:30 +01005127 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5128 * initialisers for this union leave some bytes unspecified.) */
5129 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5130
Gilles Peskine969c5d62019-01-16 15:53:06 +01005131 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005132#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005133 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5134 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5135 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005136 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005137 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005138 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5139 if( hash_size == 0 )
5140 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005141 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5142 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005143 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005144 {
5145 return( PSA_ERROR_NOT_SUPPORTED );
5146 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005147 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005148 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005149 }
5150#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005151 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005152 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005153}
5154
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005156 psa_algorithm_t alg )
5157{
5158 psa_status_t status;
5159
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005160 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005161 return( PSA_ERROR_BAD_STATE );
5162
Gilles Peskine6843c292019-01-18 16:44:49 +01005163 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5164 return( PSA_ERROR_INVALID_ARGUMENT );
5165 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005166 {
5167 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005168 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005169 }
5170 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5171 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005172 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005173 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005174 else
5175 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005176
5177 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005178 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005179 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180}
5181
5182#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005183static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005184 psa_algorithm_t hash_alg,
5185 psa_key_derivation_step_t step,
5186 const uint8_t *data,
5187 size_t data_length )
5188{
5189 psa_status_t status;
5190 switch( step )
5191 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005192 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005193 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005195 status = psa_hmac_setup_internal( &hkdf->hmac,
5196 data, data_length,
5197 hash_alg );
5198 if( status != PSA_SUCCESS )
5199 return( status );
5200 hkdf->state = HKDF_STATE_STARTED;
5201 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005202 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005203 /* If no salt was provided, use an empty salt. */
5204 if( hkdf->state == HKDF_STATE_INIT )
5205 {
5206 status = psa_hmac_setup_internal( &hkdf->hmac,
5207 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005208 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005209 if( status != PSA_SUCCESS )
5210 return( status );
5211 hkdf->state = HKDF_STATE_STARTED;
5212 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005213 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005214 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005215 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5216 data, data_length );
5217 if( status != PSA_SUCCESS )
5218 return( status );
5219 status = psa_hmac_finish_internal( &hkdf->hmac,
5220 hkdf->prk,
5221 sizeof( hkdf->prk ) );
5222 if( status != PSA_SUCCESS )
5223 return( status );
5224 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5225 hkdf->block_number = 0;
5226 hkdf->state = HKDF_STATE_KEYED;
5227 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005228 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005229 if( hkdf->state == HKDF_STATE_OUTPUT )
5230 return( PSA_ERROR_BAD_STATE );
5231 if( hkdf->info_set )
5232 return( PSA_ERROR_BAD_STATE );
5233 hkdf->info_length = data_length;
5234 if( data_length != 0 )
5235 {
5236 hkdf->info = mbedtls_calloc( 1, data_length );
5237 if( hkdf->info == NULL )
5238 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5239 memcpy( hkdf->info, data, data_length );
5240 }
5241 hkdf->info_set = 1;
5242 return( PSA_SUCCESS );
5243 default:
5244 return( PSA_ERROR_INVALID_ARGUMENT );
5245 }
5246}
Janos Follathb03233e2019-06-11 15:30:30 +01005247
Janos Follathf08e2652019-06-13 09:05:41 +01005248static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5249 const uint8_t *data,
5250 size_t data_length )
5251{
5252 if( prf->state != TLS12_PRF_STATE_INIT )
5253 return( PSA_ERROR_BAD_STATE );
5254
Janos Follathd6dce9f2019-07-04 09:11:38 +01005255 if( data_length != 0 )
5256 {
5257 prf->seed = mbedtls_calloc( 1, data_length );
5258 if( prf->seed == NULL )
5259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005260
Janos Follathd6dce9f2019-07-04 09:11:38 +01005261 memcpy( prf->seed, data, data_length );
5262 prf->seed_length = data_length;
5263 }
Janos Follathf08e2652019-06-13 09:05:41 +01005264
5265 prf->state = TLS12_PRF_STATE_SEED_SET;
5266
5267 return( PSA_SUCCESS );
5268}
5269
Janos Follath81550542019-06-13 14:26:34 +01005270static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5271 psa_algorithm_t hash_alg,
5272 const uint8_t *data,
5273 size_t data_length )
5274{
5275 psa_status_t status;
5276 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5277 return( PSA_ERROR_BAD_STATE );
5278
5279 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5280 if( status != PSA_SUCCESS )
5281 return( status );
5282
5283 prf->state = TLS12_PRF_STATE_KEY_SET;
5284
5285 return( PSA_SUCCESS );
5286}
5287
Janos Follath6660f0e2019-06-17 08:44:03 +01005288static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5289 psa_tls12_prf_key_derivation_t *prf,
5290 psa_algorithm_t hash_alg,
5291 const uint8_t *data,
5292 size_t data_length )
5293{
5294 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005295 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5296 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005297
5298 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5299 return( PSA_ERROR_INVALID_ARGUMENT );
5300
5301 /* Quoting RFC 4279, Section 2:
5302 *
5303 * The premaster secret is formed as follows: if the PSK is N octets
5304 * long, concatenate a uint16 with the value N, N zero octets, a second
5305 * uint16 with the value N, and the PSK itself.
5306 */
5307
Janos Follath40e13932019-06-26 13:22:29 +01005308 *cur++ = ( data_length >> 8 ) & 0xff;
5309 *cur++ = ( data_length >> 0 ) & 0xff;
5310 memset( cur, 0, data_length );
5311 cur += data_length;
5312 *cur++ = pms[0];
5313 *cur++ = pms[1];
5314 memcpy( cur, data, data_length );
5315 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005316
Janos Follath40e13932019-06-26 13:22:29 +01005317 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005318
5319 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5320 return( status );
5321}
5322
Janos Follath63028dd2019-06-13 09:15:47 +01005323static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5324 const uint8_t *data,
5325 size_t data_length )
5326{
5327 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5328 return( PSA_ERROR_BAD_STATE );
5329
Janos Follathd6dce9f2019-07-04 09:11:38 +01005330 if( data_length != 0 )
5331 {
5332 prf->label = mbedtls_calloc( 1, data_length );
5333 if( prf->label == NULL )
5334 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005335
Janos Follathd6dce9f2019-07-04 09:11:38 +01005336 memcpy( prf->label, data, data_length );
5337 prf->label_length = data_length;
5338 }
Janos Follath63028dd2019-06-13 09:15:47 +01005339
5340 prf->state = TLS12_PRF_STATE_LABEL_SET;
5341
5342 return( PSA_SUCCESS );
5343}
5344
Janos Follathb03233e2019-06-11 15:30:30 +01005345static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5346 psa_algorithm_t hash_alg,
5347 psa_key_derivation_step_t step,
5348 const uint8_t *data,
5349 size_t data_length )
5350{
Janos Follathb03233e2019-06-11 15:30:30 +01005351 switch( step )
5352 {
Janos Follathf08e2652019-06-13 09:05:41 +01005353 case PSA_KEY_DERIVATION_INPUT_SEED:
5354 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005355 case PSA_KEY_DERIVATION_INPUT_SECRET:
5356 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005357 case PSA_KEY_DERIVATION_INPUT_LABEL:
5358 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005359 default:
5360 return( PSA_ERROR_INVALID_ARGUMENT );
5361 }
5362}
Janos Follath6660f0e2019-06-17 08:44:03 +01005363
5364static psa_status_t psa_tls12_prf_psk_to_ms_input(
5365 psa_tls12_prf_key_derivation_t *prf,
5366 psa_algorithm_t hash_alg,
5367 psa_key_derivation_step_t step,
5368 const uint8_t *data,
5369 size_t data_length )
5370{
5371 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005372 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005373 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5374 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005375 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005376
5377 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5378}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005379#endif /* MBEDTLS_MD_C */
5380
Gilles Peskineb8965192019-09-24 16:21:10 +02005381/** Check whether the given key type is acceptable for the given
5382 * input step of a key derivation.
5383 *
5384 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5385 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5386 * Both secret and non-secret inputs can alternatively have the type
5387 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5388 * that the input was passed as a buffer rather than via a key object.
5389 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005390static int psa_key_derivation_check_input_type(
5391 psa_key_derivation_step_t step,
5392 psa_key_type_t key_type )
5393{
5394 switch( step )
5395 {
5396 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005397 if( key_type == PSA_KEY_TYPE_DERIVE )
5398 return( PSA_SUCCESS );
5399 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005400 return( PSA_SUCCESS );
5401 break;
5402 case PSA_KEY_DERIVATION_INPUT_LABEL:
5403 case PSA_KEY_DERIVATION_INPUT_SALT:
5404 case PSA_KEY_DERIVATION_INPUT_INFO:
5405 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005406 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5407 return( PSA_SUCCESS );
5408 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005409 return( PSA_SUCCESS );
5410 break;
5411 }
5412 return( PSA_ERROR_INVALID_ARGUMENT );
5413}
5414
Janos Follathb80a94e2019-06-12 15:54:46 +01005415static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005416 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005417 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005418 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005419 const uint8_t *data,
5420 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005421{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005422 psa_status_t status;
5423 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5424
5425 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005426 if( status != PSA_SUCCESS )
5427 goto exit;
5428
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005429#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005430 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005431 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005432 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005433 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005434 step, data, data_length );
5435 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005436 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005437 {
Janos Follathb03233e2019-06-11 15:30:30 +01005438 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5439 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5440 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005441 }
5442 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5443 {
5444 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5445 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5446 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005447 }
5448 else
5449#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005450 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005451 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005452 return( PSA_ERROR_BAD_STATE );
5453 }
5454
Gilles Peskine224b0d62019-09-23 18:13:17 +02005455exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005456 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005457 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005458 return( status );
5459}
5460
Janos Follath51f4a0f2019-06-14 11:35:55 +01005461psa_status_t psa_key_derivation_input_bytes(
5462 psa_key_derivation_operation_t *operation,
5463 psa_key_derivation_step_t step,
5464 const uint8_t *data,
5465 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005466{
Gilles Peskineb8965192019-09-24 16:21:10 +02005467 return( psa_key_derivation_input_internal( operation, step,
5468 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005469 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005470}
5471
Janos Follath51f4a0f2019-06-14 11:35:55 +01005472psa_status_t psa_key_derivation_input_key(
5473 psa_key_derivation_operation_t *operation,
5474 psa_key_derivation_step_t step,
5475 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005476{
5477 psa_key_slot_t *slot;
5478 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005479
Gilles Peskine28f8f302019-07-24 13:30:31 +02005480 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005481 PSA_KEY_USAGE_DERIVE,
5482 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005483 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005484 {
5485 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005486 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005487 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005488
5489 /* Passing a key object as a SECRET input unlocks the permission
5490 * to output to a key object. */
5491 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5492 operation->can_output_key = 1;
5493
Janos Follathb80a94e2019-06-12 15:54:46 +01005494 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005495 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005496 slot->data.key.data,
5497 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005498}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005499
5500
5501
5502/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005503/* Key agreement */
5504/****************************************************************/
5505
Gilles Peskinea05219c2018-11-16 16:02:56 +01005506#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005507static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5508 size_t peer_key_length,
5509 const mbedtls_ecp_keypair *our_key,
5510 uint8_t *shared_secret,
5511 size_t shared_secret_size,
5512 size_t *shared_secret_length )
5513{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005514 mbedtls_ecp_keypair *their_key = NULL;
5515 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005516 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005517 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005518 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005519 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005520
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005521 status = psa_import_ec_public_key( curve,
5522 peer_key, peer_key_length,
5523 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005524 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005525 goto exit;
5526
Jaeden Amero97271b32019-01-10 19:38:51 +00005527 status = mbedtls_to_psa_error(
5528 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5529 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005530 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005531 status = mbedtls_to_psa_error(
5532 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5533 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005534 goto exit;
5535
Jaeden Amero97271b32019-01-10 19:38:51 +00005536 status = mbedtls_to_psa_error(
5537 mbedtls_ecdh_calc_secret( &ecdh,
5538 shared_secret_length,
5539 shared_secret, shared_secret_size,
5540 mbedtls_ctr_drbg_random,
5541 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005542 if( status != PSA_SUCCESS )
5543 goto exit;
5544 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5545 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005546
5547exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005548 if( status != PSA_SUCCESS )
5549 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005550 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005551 mbedtls_ecp_keypair_free( their_key );
5552 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005553 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005554}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005555#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005556
Gilles Peskine01d718c2018-09-18 12:01:02 +02005557#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5558
Gilles Peskine0216fe12019-04-11 21:23:21 +02005559static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5560 psa_key_slot_t *private_key,
5561 const uint8_t *peer_key,
5562 size_t peer_key_length,
5563 uint8_t *shared_secret,
5564 size_t shared_secret_size,
5565 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005566{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005567 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005568 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005569#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005570 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005571 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005572 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005573 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5574 private_key->data.ecp,
5575 shared_secret, shared_secret_size,
5576 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005577#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005578 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005579 (void) private_key;
5580 (void) peer_key;
5581 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005582 (void) shared_secret;
5583 (void) shared_secret_size;
5584 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005585 return( PSA_ERROR_NOT_SUPPORTED );
5586 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005587}
5588
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005589/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005590 * to potentially free embedded data structures and wipe confidential data.
5591 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005592static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005593 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005594 psa_key_slot_t *private_key,
5595 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005596 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005597{
5598 psa_status_t status;
5599 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5600 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005601 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005602
5603 /* Step 1: run the secret agreement algorithm to generate the shared
5604 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005605 status = psa_key_agreement_raw_internal( ka_alg,
5606 private_key,
5607 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005608 shared_secret,
5609 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005610 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005611 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005612 goto exit;
5613
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005614 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005615 * the shared secret. A shared secret is permitted wherever a key
5616 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005617 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005618 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005619 shared_secret,
5620 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005621
Gilles Peskine12313cd2018-06-20 00:20:32 +02005622exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005623 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005624 return( status );
5625}
5626
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005627psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005628 psa_key_derivation_step_t step,
5629 psa_key_handle_t private_key,
5630 const uint8_t *peer_key,
5631 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005632{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005633 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005634 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005635 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005636 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005637 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005638 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005639 if( status != PSA_SUCCESS )
5640 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005641 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005642 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005643 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005644 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005645 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005646 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005647}
5648
Gilles Peskinebe697d82019-05-16 18:00:41 +02005649psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5650 psa_key_handle_t private_key,
5651 const uint8_t *peer_key,
5652 size_t peer_key_length,
5653 uint8_t *output,
5654 size_t output_size,
5655 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005656{
5657 psa_key_slot_t *slot;
5658 psa_status_t status;
5659
5660 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5661 {
5662 status = PSA_ERROR_INVALID_ARGUMENT;
5663 goto exit;
5664 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005665 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005666 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005667 if( status != PSA_SUCCESS )
5668 goto exit;
5669
5670 status = psa_key_agreement_raw_internal( alg, slot,
5671 peer_key, peer_key_length,
5672 output, output_size,
5673 output_length );
5674
5675exit:
5676 if( status != PSA_SUCCESS )
5677 {
5678 /* If an error happens and is not handled properly, the output
5679 * may be used as a key to protect sensitive data. Arrange for such
5680 * a key to be random, which is likely to result in decryption or
5681 * verification errors. This is better than filling the buffer with
5682 * some constant data such as zeros, which would result in the data
5683 * being protected with a reproducible, easily knowable key.
5684 */
5685 psa_generate_random( output, output_size );
5686 *output_length = output_size;
5687 }
5688 return( status );
5689}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005690
5691
5692/****************************************************************/
5693/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005694/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005695
Gilles Peskine4c317f42018-07-12 01:24:09 +02005696psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005697 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005698{
Janos Follath24eed8d2019-11-22 13:21:35 +00005699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005700 GUARD_MODULE_INITIALIZED;
5701
Gilles Peskinef181eca2019-08-07 13:49:00 +02005702 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5703 {
5704 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5705 output,
5706 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5707 if( ret != 0 )
5708 return( mbedtls_to_psa_error( ret ) );
5709 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5710 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5711 }
5712
Gilles Peskinee59236f2018-01-27 23:32:46 +01005713 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5714 return( mbedtls_to_psa_error( ret ) );
5715}
5716
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005717#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5718#include "mbedtls/entropy_poll.h"
5719
Gilles Peskine7228da22019-07-15 11:06:15 +02005720psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005721 size_t seed_size )
5722{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005723 if( global_data.initialized )
5724 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005725
5726 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5727 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5728 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5729 return( PSA_ERROR_INVALID_ARGUMENT );
5730
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005731 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005732}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005733#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005734
Gilles Peskinee56e8782019-04-26 17:34:02 +02005735#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5736static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5737 size_t domain_parameters_size,
5738 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005739{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005740 size_t i;
5741 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005742
Gilles Peskinee56e8782019-04-26 17:34:02 +02005743 if( domain_parameters_size == 0 )
5744 {
5745 *exponent = 65537;
5746 return( PSA_SUCCESS );
5747 }
5748
5749 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5750 * support values that fit in a 32-bit integer, which is larger than
5751 * int on just about every platform anyway. */
5752 if( domain_parameters_size > sizeof( acc ) )
5753 return( PSA_ERROR_NOT_SUPPORTED );
5754 for( i = 0; i < domain_parameters_size; i++ )
5755 acc = ( acc << 8 ) | domain_parameters[i];
5756 if( acc > INT_MAX )
5757 return( PSA_ERROR_NOT_SUPPORTED );
5758 *exponent = acc;
5759 return( PSA_SUCCESS );
5760}
5761#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5762
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005763static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005764 psa_key_slot_t *slot, size_t bits,
5765 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005766{
Gilles Peskine8e338702019-07-30 20:06:31 +02005767 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005768
Gilles Peskinee56e8782019-04-26 17:34:02 +02005769 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005770 return( PSA_ERROR_INVALID_ARGUMENT );
5771
Gilles Peskinee59236f2018-01-27 23:32:46 +01005772 if( key_type_is_raw_bytes( type ) )
5773 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005774 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005775
5776 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005777 if( status != PSA_SUCCESS )
5778 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005779
5780 /* Allocate memory for the key */
5781 slot->data.key.bytes = PSA_BITS_TO_BYTES( bits );
5782 slot->data.key.data = mbedtls_calloc( 1, slot->data.key.bytes );
5783 if( slot->data.key.data == NULL )
5784 {
5785 slot->data.key.bytes = 0;
5786 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5787 }
5788
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005789 status = psa_generate_random( slot->data.key.data,
5790 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005791 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005792 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005793
5794 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005795#if defined(MBEDTLS_DES_C)
5796 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005797 psa_des_set_key_parity( slot->data.key.data,
5798 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005799#endif /* MBEDTLS_DES_C */
5800 }
5801 else
5802
5803#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005804 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005805 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005806 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005808 int exponent;
5809 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005810 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5811 return( PSA_ERROR_NOT_SUPPORTED );
5812 /* Accept only byte-aligned keys, for the same reasons as
5813 * in psa_import_rsa_key(). */
5814 if( bits % 8 != 0 )
5815 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005816 status = psa_read_rsa_exponent( domain_parameters,
5817 domain_parameters_size,
5818 &exponent );
5819 if( status != PSA_SUCCESS )
5820 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005821 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5822 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005823 mbedtls_ctr_drbg_random,
5824 &global_data.ctr_drbg,
5825 (unsigned int) bits,
5826 exponent );
5827 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005828 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005829
5830 /* Make sure to always have an export representation available */
5831 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5832
5833 slot->data.key.data = mbedtls_calloc( 1, bytes );
5834 if( slot->data.key.data == NULL )
5835 {
5836 mbedtls_rsa_free( &rsa );
5837 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005838 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005839
5840 status = psa_export_rsa_key( type,
5841 &rsa,
5842 slot->data.key.data,
5843 bytes,
5844 &slot->data.key.bytes );
5845 mbedtls_rsa_free( &rsa );
5846 if( status != PSA_SUCCESS )
5847 {
5848 psa_remove_key_data_from_memory( slot );
5849 return( status );
5850 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005851 }
5852 else
5853#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5854
5855#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005856 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005857 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005858 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005859 mbedtls_ecp_group_id grp_id =
5860 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005861 const mbedtls_ecp_curve_info *curve_info =
5862 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5863 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005865 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005866 return( PSA_ERROR_NOT_SUPPORTED );
5867 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5868 return( PSA_ERROR_NOT_SUPPORTED );
5869 if( curve_info->bit_size != bits )
5870 return( PSA_ERROR_INVALID_ARGUMENT );
5871 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5872 if( ecp == NULL )
5873 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5874 mbedtls_ecp_keypair_init( ecp );
5875 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5876 mbedtls_ctr_drbg_random,
5877 &global_data.ctr_drbg );
5878 if( ret != 0 )
5879 {
5880 mbedtls_ecp_keypair_free( ecp );
5881 mbedtls_free( ecp );
5882 return( mbedtls_to_psa_error( ret ) );
5883 }
5884 slot->data.ecp = ecp;
5885 }
5886 else
5887#endif /* MBEDTLS_ECP_C */
5888
5889 return( PSA_ERROR_NOT_SUPPORTED );
5890
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005891 return( PSA_SUCCESS );
5892}
Darryl Green0c6575a2018-11-07 16:05:30 +00005893
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005894psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005895 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005896{
5897 psa_status_t status;
5898 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005899 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005900
Gilles Peskine0f84d622019-09-12 19:03:13 +02005901 /* Reject any attempt to create a zero-length key so that we don't
5902 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5903 if( psa_get_key_bits( attributes ) == 0 )
5904 return( PSA_ERROR_INVALID_ARGUMENT );
5905
Gilles Peskinedf179142019-07-15 22:02:14 +02005906 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5907 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005908 if( status != PSA_SUCCESS )
5909 goto exit;
5910
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005911#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5912 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005913 {
Gilles Peskine11792082019-08-06 18:36:36 +02005914 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5915 size_t pubkey_length = 0; /* We don't support this feature yet */
5916 if( drv->key_management == NULL ||
5917 drv->key_management->p_generate == NULL )
5918 {
5919 status = PSA_ERROR_NOT_SUPPORTED;
5920 goto exit;
5921 }
5922 status = drv->key_management->p_generate(
5923 psa_get_se_driver_context( driver ),
5924 slot->data.se.slot_number, attributes,
5925 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005926 }
Gilles Peskine11792082019-08-06 18:36:36 +02005927 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005928#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005929 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005930 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005931 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005932 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005933 }
Gilles Peskine11792082019-08-06 18:36:36 +02005934
5935exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005936 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005937 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005938 if( status != PSA_SUCCESS )
5939 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005940 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005941 *handle = 0;
5942 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005943 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005944}
5945
5946
Gilles Peskinee59236f2018-01-27 23:32:46 +01005947
5948/****************************************************************/
5949/* Module setup */
5950/****************************************************************/
5951
Gilles Peskine5e769522018-11-20 21:59:56 +01005952psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5953 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5954 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5955{
5956 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5957 return( PSA_ERROR_BAD_STATE );
5958 global_data.entropy_init = entropy_init;
5959 global_data.entropy_free = entropy_free;
5960 return( PSA_SUCCESS );
5961}
5962
Gilles Peskinee59236f2018-01-27 23:32:46 +01005963void mbedtls_psa_crypto_free( void )
5964{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005965 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005966 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5967 {
5968 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005969 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005970 }
5971 /* Wipe all remaining data, including configuration.
5972 * In particular, this sets all state indicator to the value
5973 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005974 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005975#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005976 /* Unregister all secure element drivers, so that we restart from
5977 * a pristine state. */
5978 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005979#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005980}
5981
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005982#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5983/** Recover a transaction that was interrupted by a power failure.
5984 *
5985 * This function is called during initialization, before psa_crypto_init()
5986 * returns. If this function returns a failure status, the initialization
5987 * fails.
5988 */
5989static psa_status_t psa_crypto_recover_transaction(
5990 const psa_crypto_transaction_t *transaction )
5991{
5992 switch( transaction->unknown.type )
5993 {
5994 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5995 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005996 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005997 * is implemented.
5998 * https://github.com/ARMmbed/mbed-crypto/issues/218
5999 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006000 default:
6001 /* We found an unsupported transaction in the storage.
6002 * We don't know what state the storage is in. Give up. */
6003 return( PSA_ERROR_STORAGE_FAILURE );
6004 }
6005}
6006#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6007
Gilles Peskinee59236f2018-01-27 23:32:46 +01006008psa_status_t psa_crypto_init( void )
6009{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006010 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006011 const unsigned char drbg_seed[] = "PSA";
6012
Gilles Peskinec6b69072018-11-20 21:42:52 +01006013 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006014 if( global_data.initialized != 0 )
6015 return( PSA_SUCCESS );
6016
Gilles Peskine5e769522018-11-20 21:59:56 +01006017 /* Set default configuration if
6018 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6019 if( global_data.entropy_init == NULL )
6020 global_data.entropy_init = mbedtls_entropy_init;
6021 if( global_data.entropy_free == NULL )
6022 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006023
Gilles Peskinec6b69072018-11-20 21:42:52 +01006024 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006025 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006026#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6027 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6028 /* The PSA entropy injection feature depends on using NV seed as an entropy
6029 * source. Add NV seed as an entropy source for PSA entropy injection. */
6030 mbedtls_entropy_add_source( &global_data.entropy,
6031 mbedtls_nv_seed_poll, NULL,
6032 MBEDTLS_ENTROPY_BLOCK_SIZE,
6033 MBEDTLS_ENTROPY_SOURCE_STRONG );
6034#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006035 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006036 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006037 status = mbedtls_to_psa_error(
6038 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6039 mbedtls_entropy_func,
6040 &global_data.entropy,
6041 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6042 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006043 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006044 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006045
Gilles Peskine66fb1262018-12-10 16:29:04 +01006046 status = psa_initialize_key_slots( );
6047 if( status != PSA_SUCCESS )
6048 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006049
Gilles Peskined9348f22019-10-01 15:22:29 +02006050#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6051 status = psa_init_all_se_drivers( );
6052 if( status != PSA_SUCCESS )
6053 goto exit;
6054#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6055
Gilles Peskine4b734222019-07-24 15:56:31 +02006056#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006057 status = psa_crypto_load_transaction( );
6058 if( status == PSA_SUCCESS )
6059 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006060 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6061 if( status != PSA_SUCCESS )
6062 goto exit;
6063 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006064 }
6065 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6066 {
6067 /* There's no transaction to complete. It's all good. */
6068 status = PSA_SUCCESS;
6069 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006070#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006071
Gilles Peskinec6b69072018-11-20 21:42:52 +01006072 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006073 global_data.initialized = 1;
6074
6075exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006076 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006077 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006078 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006079}
6080
6081#endif /* MBEDTLS_PSA_CRYPTO_C */