blob: de5e8588d53f64663fd097ee8358b208f8ba915c [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)
Steven Cooremanacda8342020-07-24 23:09:52 +0200716/* Load the key slot contents into an mbedTLS internal representation object.
717 * Note: caller is responsible for freeing the object properly */
718static psa_status_t psa_load_ecp_representation( const psa_key_slot_t *slot,
719 mbedtls_ecp_keypair *ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100720{
721 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200722 size_t data_length = slot->data.key.bytes;
723 psa_status_t status;
724 mbedtls_ecp_keypair_init( ecp );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100725
Steven Cooremanacda8342020-07-24 23:09:52 +0200726 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100727 {
728 /* A public key is represented as:
729 * - The byte 0x04;
730 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
731 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
732 * So its data length is 2m+1 where n is the key size in bits.
733 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200734 if( ( slot->data.key.bytes & 1 ) == 0 )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100735 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +0200736 data_length = slot->data.key.bytes / 2;
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100737 }
738
Gilles Peskine4cd32772019-12-02 20:49:42 +0100739 /* Load the group. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200740 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type),
741 data_length );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100742 if( grp_id == MBEDTLS_ECP_DP_NONE )
743 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +0200744 status = mbedtls_to_psa_error(
745 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
746 if( status != PSA_SUCCESS )
747 return( status );
748
749 /* Load the key material */
750 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
751 {
752 /* Load the public value. */
753 status = mbedtls_to_psa_error(
754 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
755 slot->data.key.data,
756 slot->data.key.bytes ) );
757 if( status != PSA_SUCCESS )
758 goto exit;
759
760 /* Check that the point is on the curve. */
761 status = mbedtls_to_psa_error(
762 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
763 if( status != PSA_SUCCESS )
764 goto exit;
765 }
766 else
767 {
768 /* Load the secret value. */
769 status = mbedtls_to_psa_error(
770 mbedtls_ecp_read_key( ecp->grp.id,
771 ecp,
772 slot->data.key.data,
773 slot->data.key.bytes ) );
774
775 if( status != PSA_SUCCESS )
776 goto exit;
777 /* Validate the private key. */
778 status = mbedtls_to_psa_error(
779 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
780 if( status != PSA_SUCCESS )
781 goto exit;
782 }
783exit:
784 if( status != PSA_SUCCESS )
785 mbedtls_ecp_keypair_free( ecp );
786 return status;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100787}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000788
Steven Cooremanacda8342020-07-24 23:09:52 +0200789static psa_status_t psa_export_ecp_key( psa_key_type_t type,
790 mbedtls_ecp_keypair *ecp,
791 uint8_t *data,
792 size_t data_size,
793 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200794{
Steven Cooremanacda8342020-07-24 23:09:52 +0200795 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000796
Steven Cooremanacda8342020-07-24 23:09:52 +0200797 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200798 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200799 /* Check whether the public part is loaded */
800 if( mbedtls_ecp_is_zero( &ecp->Q ) )
801 {
802 /* Calculate the public key */
803 status = mbedtls_to_psa_error(
804 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
805 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
806 if( status != PSA_SUCCESS )
807 return status;
808 }
809
810 return( mbedtls_to_psa_error(
811 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
812 MBEDTLS_ECP_PF_UNCOMPRESSED,
813 data_length,
814 data,
815 data_size ) ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200816 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200817 else
818 {
819 if( data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits) )
820 return( PSA_ERROR_BUFFER_TOO_SMALL );
821
822 status = mbedtls_to_psa_error(
823 mbedtls_ecp_write_key( ecp,
824 data,
825 PSA_BITS_TO_BYTES(ecp->grp.nbits) ) );
826 if( status == PSA_SUCCESS )
827 {
828 *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits);
829 }
830
831 return( status );
832 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200833}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200834
Steven Cooremanacda8342020-07-24 23:09:52 +0200835static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
836 const uint8_t *data,
837 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100838{
Steven Cooremanacda8342020-07-24 23:09:52 +0200839 psa_status_t status;
840 uint8_t* output = NULL;
841 mbedtls_ecp_keypair ecp;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100842
Steven Cooremanacda8342020-07-24 23:09:52 +0200843 /* Temporarily load input into slot. The cast here is safe since it'll
844 * only be used for load_ecp_representation, which doesn't modify the
845 * buffer. */
846 slot->data.key.data = (uint8_t *)data;
847 slot->data.key.bytes = data_length;
848
849 /* Parse input */
850 status = psa_load_ecp_representation( slot, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100851 if( status != PSA_SUCCESS )
852 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100853
Steven Cooremanacda8342020-07-24 23:09:52 +0200854 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
855 slot->attr.bits = (psa_key_bits_t) ecp.grp.nbits + 1;
856 else
857 slot->attr.bits = (psa_key_bits_t) ecp.grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200858
Steven Cooremanacda8342020-07-24 23:09:52 +0200859 /* Re-export the data to PSA export format. There is currently no support
860 * for other input formats then the export format, so this is a 1-1
861 * copy operation. */
862 output = mbedtls_calloc( 1, data_length );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100863
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 if( output == NULL )
865 {
866 status = PSA_ERROR_INSUFFICIENT_MEMORY;
867 goto exit;
868 }
869
870 status = psa_export_ecp_key( slot->attr.type,
871 &ecp,
872 output,
873 data_length,
874 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100875
876exit:
Steven Cooremanacda8342020-07-24 23:09:52 +0200877 /* Always free the PK object (will also free contained RSA context) */
878 mbedtls_ecp_keypair_free( &ecp );
879
880 /* Free the allocated buffer only on error. */
881 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100882 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200883 mbedtls_free( output );
884 slot->data.key.data = NULL;
885 slot->data.key.bytes = 0;
886 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100887 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200888
889 /* On success, store the allocated export-formatted key. */
890 slot->data.key.data = output;
891 slot->data.key.bytes = data_length;
892
893 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100894}
895#endif /* defined(MBEDTLS_ECP_C) */
896
Gilles Peskineb46bef22019-07-30 21:32:04 +0200897/** Return the size of the key in the given slot, in bits.
898 *
899 * \param[in] slot A key slot.
900 *
901 * \return The key size in bits, read from the metadata in the slot.
902 */
903static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
904{
905 return( slot->attr.bits );
906}
907
Gilles Peskine8e338702019-07-30 20:06:31 +0200908/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100909 * previously. This function assumes that the slot does not contain
910 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100911psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
912 const uint8_t *data,
913 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100914{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200915 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100916
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200917 /* zero-length keys are never supported. */
918 if( data_length == 0 )
919 return( PSA_ERROR_NOT_SUPPORTED );
920
921 /* Ensure that the bytes-to-bit conversion never overflows. */
922 if( data_length > SIZE_MAX / 8 )
923 return( PSA_ERROR_NOT_SUPPORTED );
924
Gilles Peskine8e338702019-07-30 20:06:31 +0200925 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100926 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200927 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200928
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200929 /* Enforce a size limit, and in particular ensure that the bit
930 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200931 if( bit_size > PSA_MAX_KEY_BITS )
932 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200933
934 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200935 if( status != PSA_SUCCESS )
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200936 return status;
937
938 /* Allocate memory for the key */
939 slot->data.key.data = mbedtls_calloc( 1, data_length );
940 if( slot->data.key.data == NULL )
941 {
942 return( PSA_ERROR_INSUFFICIENT_MEMORY );
943 }
944 slot->data.key.bytes = data_length;
945
946 /* copy key into allocated buffer */
947 memcpy(slot->data.key.data, data, data_length);
948
949 /* Write the actual key size to the slot.
950 * psa_start_key_creation() wrote the size declared by the
951 * caller, which may be 0 (meaning unspecified) or wrong. */
952 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953 }
954 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100955#if defined(MBEDTLS_ECP_C)
Steven Cooremanacda8342020-07-24 23:09:52 +0200956 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100957 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200958 status = psa_import_ecp_key( slot, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100959 }
960 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100961#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000962#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100964 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200965 status = psa_import_rsa_key( slot, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 }
967 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000968#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969 {
970 return( PSA_ERROR_NOT_SUPPORTED );
971 }
Darryl Green940d72c2018-07-13 13:18:51 +0100972
Darryl Green06fd18d2018-07-16 11:21:11 +0100973 return( status );
974}
975
Gilles Peskinef603c712019-01-19 13:40:11 +0100976/** Calculate the intersection of two algorithm usage policies.
977 *
978 * Return 0 (which allows no operation) on incompatibility.
979 */
980static psa_algorithm_t psa_key_policy_algorithm_intersection(
981 psa_algorithm_t alg1,
982 psa_algorithm_t alg2 )
983{
Gilles Peskine549ea862019-05-22 11:45:59 +0200984 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100985 if( alg1 == alg2 )
986 return( alg1 );
987 /* If the policies are from the same hash-and-sign family, check
988 * if one is a wildcard. If so the other has the specific algorithm. */
989 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
990 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
991 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
992 {
993 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
994 return( alg2 );
995 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
996 return( alg1 );
997 }
998 /* If the policies are incompatible, allow nothing. */
999 return( 0 );
1000}
1001
Gilles Peskined6f371b2019-05-10 19:33:38 +02001002static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1003 psa_algorithm_t requested_alg )
1004{
Gilles Peskine549ea862019-05-22 11:45:59 +02001005 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001006 if( requested_alg == policy_alg )
1007 return( 1 );
1008 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001009 * and requested_alg is the same hash-and-sign family with any hash,
1010 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001011 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1012 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1013 {
1014 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1015 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1016 }
1017 /* If it isn't permitted, it's forbidden. */
1018 return( 0 );
1019}
1020
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001021/** Test whether a policy permits an algorithm.
1022 *
1023 * The caller must test usage flags separately.
1024 */
1025static int psa_key_policy_permits( const psa_key_policy_t *policy,
1026 psa_algorithm_t alg )
1027{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001028 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1029 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001030}
1031
Gilles Peskinef603c712019-01-19 13:40:11 +01001032/** Restrict a key policy based on a constraint.
1033 *
1034 * \param[in,out] policy The policy to restrict.
1035 * \param[in] constraint The policy constraint to apply.
1036 *
1037 * \retval #PSA_SUCCESS
1038 * \c *policy contains the intersection of the original value of
1039 * \c *policy and \c *constraint.
1040 * \retval #PSA_ERROR_INVALID_ARGUMENT
1041 * \c *policy and \c *constraint are incompatible.
1042 * \c *policy is unchanged.
1043 */
1044static psa_status_t psa_restrict_key_policy(
1045 psa_key_policy_t *policy,
1046 const psa_key_policy_t *constraint )
1047{
1048 psa_algorithm_t intersection_alg =
1049 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001050 psa_algorithm_t intersection_alg2 =
1051 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001052 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1053 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001054 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1055 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001056 policy->usage &= constraint->usage;
1057 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001058 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001059 return( PSA_SUCCESS );
1060}
1061
Darryl Green06fd18d2018-07-16 11:21:11 +01001062/** Retrieve a slot which must contain a key. The key must have allow all the
1063 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1064 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001065static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001066 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001067 psa_key_usage_t usage,
1068 psa_algorithm_t alg )
1069{
1070 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001071 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001072
1073 *p_slot = NULL;
1074
Gilles Peskinec5487a82018-12-03 18:08:14 +01001075 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001076 if( status != PSA_SUCCESS )
1077 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001078
1079 /* Enforce that usage policy for the key slot contains all the flags
1080 * required by the usage parameter. There is one exception: public
1081 * keys can always be exported, so we treat public key objects as
1082 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001083 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001084 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001085 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001086 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001087
1088 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001089 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001090 return( PSA_ERROR_NOT_PERMITTED );
1091
1092 *p_slot = slot;
1093 return( PSA_SUCCESS );
1094}
Darryl Green940d72c2018-07-13 13:18:51 +01001095
Gilles Peskine28f8f302019-07-24 13:30:31 +02001096/** Retrieve a slot which must contain a transparent key.
1097 *
1098 * A transparent key is a key for which the key material is directly
1099 * available, as opposed to a key in a secure element.
1100 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001101 * This is a temporary function to use instead of psa_get_key_from_slot()
1102 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001103 */
1104#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1105static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1106 psa_key_slot_t **p_slot,
1107 psa_key_usage_t usage,
1108 psa_algorithm_t alg )
1109{
1110 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1111 if( status != PSA_SUCCESS )
1112 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001113 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001114 {
1115 *p_slot = NULL;
1116 return( PSA_ERROR_NOT_SUPPORTED );
1117 }
1118 return( PSA_SUCCESS );
1119}
1120#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1121/* With no secure element support, all keys are transparent. */
1122#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1123 psa_get_key_from_slot( handle, p_slot, usage, alg )
1124#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1125
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001126/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001127static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001128{
Gilles Peskine73167e12019-07-12 23:44:37 +02001129#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1130 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001131 {
1132 /* No key material to clean. */
1133 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001134 else
1135#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001136 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001137 {
1138 /* No key material to clean. */
1139 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001140 else if( key_type_is_raw_bytes( slot->attr.type ) ||
1141 PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1142 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001143 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001144 mbedtls_free( slot->data.key.data );
1145 slot->data.key.data = NULL;
1146 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001147 }
1148 else
Darryl Green40225ba2018-11-15 14:48:15 +00001149 {
1150 /* Shouldn't happen: the key type is not any type that we
1151 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001152 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001153 }
1154
1155 return( PSA_SUCCESS );
1156}
1157
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001158/** Completely wipe a slot in memory, including its policy.
1159 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001160psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001161{
1162 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001163 /* Multipart operations may still be using the key. This is safe
1164 * because all multipart operation objects are independent from
1165 * the key slot: if they need to access the key after the setup
1166 * phase, they have a copy of the key. Note that this means that
1167 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001168 /* At this point, key material and other type-specific content has
1169 * been wiped. Clear remaining metadata. We can call memset and not
1170 * zeroize because the metadata is not particularly sensitive. */
1171 memset( slot, 0, sizeof( *slot ) );
1172 return( status );
1173}
1174
Gilles Peskinec5487a82018-12-03 18:08:14 +01001175psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001176{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001177 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001178 psa_status_t status; /* status of the last operation */
1179 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001180#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1181 psa_se_drv_table_entry_t *driver;
1182#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001183
Gilles Peskine1841cf42019-10-08 15:48:25 +02001184 if( handle == 0 )
1185 return( PSA_SUCCESS );
1186
Gilles Peskinec5487a82018-12-03 18:08:14 +01001187 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001188 if( status != PSA_SUCCESS )
1189 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001190
1191#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001192 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001193 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001194 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001195 /* For a key in a secure element, we need to do three things:
1196 * remove the key file in internal storage, destroy the
1197 * key inside the secure element, and update the driver's
1198 * persistent data. Start a transaction that will encompass these
1199 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001200 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001201 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001202 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001203 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001204 status = psa_crypto_save_transaction( );
1205 if( status != PSA_SUCCESS )
1206 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001207 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001208 /* We should still try to destroy the key in the secure
1209 * element and the key metadata in storage. This is especially
1210 * important if the error is that the storage is full.
1211 * But how to do it exactly without risking an inconsistent
1212 * state after a reset?
1213 * https://github.com/ARMmbed/mbed-crypto/issues/215
1214 */
1215 overall_status = status;
1216 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001217 }
1218
Gilles Peskine354f7672019-07-12 23:46:38 +02001219 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001220 if( overall_status == PSA_SUCCESS )
1221 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001222 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001223#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1224
Darryl Greend49a4992018-06-18 17:27:26 +01001225#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001226 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001227 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001228 status = psa_destroy_persistent_key( slot->attr.id );
1229 if( overall_status == PSA_SUCCESS )
1230 overall_status = status;
1231
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001232 /* TODO: other slots may have a copy of the same key. We should
1233 * invalidate them.
1234 * https://github.com/ARMmbed/mbed-crypto/issues/214
1235 */
Darryl Greend49a4992018-06-18 17:27:26 +01001236 }
1237#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001238
Gilles Peskinefc762652019-07-22 19:30:34 +02001239#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1240 if( driver != NULL )
1241 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001242 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001243 if( overall_status == PSA_SUCCESS )
1244 overall_status = status;
1245 status = psa_crypto_stop_transaction( );
1246 if( overall_status == PSA_SUCCESS )
1247 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001248 }
1249#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1250
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001251#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1252exit:
1253#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001254 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001255 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1256 if( overall_status == PSA_SUCCESS )
1257 overall_status = status;
1258 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001259}
1260
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001261void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001262{
Gilles Peskineb699f072019-04-26 16:06:02 +02001263 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001264 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001265}
1266
Gilles Peskineb699f072019-04-26 16:06:02 +02001267psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1268 psa_key_type_t type,
1269 const uint8_t *data,
1270 size_t data_length )
1271{
1272 uint8_t *copy = NULL;
1273
1274 if( data_length != 0 )
1275 {
1276 copy = mbedtls_calloc( 1, data_length );
1277 if( copy == NULL )
1278 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1279 memcpy( copy, data, data_length );
1280 }
1281 /* After this point, this function is guaranteed to succeed, so it
1282 * can start modifying `*attributes`. */
1283
1284 if( attributes->domain_parameters != NULL )
1285 {
1286 mbedtls_free( attributes->domain_parameters );
1287 attributes->domain_parameters = NULL;
1288 attributes->domain_parameters_size = 0;
1289 }
1290
1291 attributes->domain_parameters = copy;
1292 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001293 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001294 return( PSA_SUCCESS );
1295}
1296
1297psa_status_t psa_get_key_domain_parameters(
1298 const psa_key_attributes_t *attributes,
1299 uint8_t *data, size_t data_size, size_t *data_length )
1300{
1301 if( attributes->domain_parameters_size > data_size )
1302 return( PSA_ERROR_BUFFER_TOO_SMALL );
1303 *data_length = attributes->domain_parameters_size;
1304 if( attributes->domain_parameters_size != 0 )
1305 memcpy( data, attributes->domain_parameters,
1306 attributes->domain_parameters_size );
1307 return( PSA_SUCCESS );
1308}
1309
1310#if defined(MBEDTLS_RSA_C)
1311static psa_status_t psa_get_rsa_public_exponent(
1312 const mbedtls_rsa_context *rsa,
1313 psa_key_attributes_t *attributes )
1314{
1315 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001317 uint8_t *buffer = NULL;
1318 size_t buflen;
1319 mbedtls_mpi_init( &mpi );
1320
1321 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1322 if( ret != 0 )
1323 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001324 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1325 {
1326 /* It's the default value, which is reported as an empty string,
1327 * so there's nothing to do. */
1328 goto exit;
1329 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001330
1331 buflen = mbedtls_mpi_size( &mpi );
1332 buffer = mbedtls_calloc( 1, buflen );
1333 if( buffer == NULL )
1334 {
1335 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1336 goto exit;
1337 }
1338 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1339 if( ret != 0 )
1340 goto exit;
1341 attributes->domain_parameters = buffer;
1342 attributes->domain_parameters_size = buflen;
1343
1344exit:
1345 mbedtls_mpi_free( &mpi );
1346 if( ret != 0 )
1347 mbedtls_free( buffer );
1348 return( mbedtls_to_psa_error( ret ) );
1349}
1350#endif /* MBEDTLS_RSA_C */
1351
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001352/** Retrieve all the publicly-accessible attributes of a key.
1353 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001354psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1355 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001356{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001357 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001358 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001359
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001360 psa_reset_key_attributes( attributes );
1361
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001362 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001363 if( status != PSA_SUCCESS )
1364 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001365
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001366 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001367 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1368 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1369
1370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1371 if( psa_key_slot_is_external( slot ) )
1372 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1373#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001374
Gilles Peskine8e338702019-07-30 20:06:31 +02001375 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001376 {
1377#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001378 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001379 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001380#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001381 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001382 * is not yet implemented.
1383 * https://github.com/ARMmbed/mbed-crypto/issues/216
1384 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001385 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001386 break;
1387#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001388 {
1389 mbedtls_rsa_context rsa;
1390 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
1391
1392 status = psa_load_rsa_representation( slot, &rsa );
1393 if( status != PSA_SUCCESS )
1394 break;
1395
1396 status = psa_get_rsa_public_exponent( &rsa,
1397 attributes );
1398 mbedtls_rsa_free( &rsa );
1399 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001400 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001401#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001402 default:
1403 /* Nothing else to do. */
1404 break;
1405 }
1406
1407 if( status != PSA_SUCCESS )
1408 psa_reset_key_attributes( attributes );
1409 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001410}
1411
Gilles Peskinec8000c02019-08-02 20:15:51 +02001412#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1413psa_status_t psa_get_key_slot_number(
1414 const psa_key_attributes_t *attributes,
1415 psa_key_slot_number_t *slot_number )
1416{
1417 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1418 {
1419 *slot_number = attributes->slot_number;
1420 return( PSA_SUCCESS );
1421 }
1422 else
1423 return( PSA_ERROR_INVALID_ARGUMENT );
1424}
1425#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1426
Steven Cooremana01795d2020-07-24 22:48:15 +02001427static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1428 uint8_t *data,
1429 size_t data_size,
1430 size_t *data_length )
1431{
1432 if( slot->data.key.bytes > data_size )
1433 return( PSA_ERROR_BUFFER_TOO_SMALL );
1434 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1435 memset( data + slot->data.key.bytes, 0,
1436 data_size - slot->data.key.bytes );
1437 *data_length = slot->data.key.bytes;
1438 return( PSA_SUCCESS );
1439}
1440
Gilles Peskinef603c712019-01-19 13:40:11 +01001441static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1442 uint8_t *data,
1443 size_t data_size,
1444 size_t *data_length,
1445 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001446{
Gilles Peskine5d309672019-07-12 23:47:28 +02001447#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1448 const psa_drv_se_t *drv;
1449 psa_drv_se_context_t *drv_context;
1450#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1451
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001452 *data_length = 0;
1453
Gilles Peskine8e338702019-07-30 20:06:31 +02001454 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001455 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001456
Gilles Peskinef9168942019-09-12 19:20:29 +02001457 /* Reject a zero-length output buffer now, since this can never be a
1458 * valid key representation. This way we know that data must be a valid
1459 * pointer and we can do things like memset(data, ..., data_size). */
1460 if( data_size == 0 )
1461 return( PSA_ERROR_BUFFER_TOO_SMALL );
1462
Gilles Peskine5d309672019-07-12 23:47:28 +02001463#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001464 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001465 {
1466 psa_drv_se_export_key_t method;
1467 if( drv->key_management == NULL )
1468 return( PSA_ERROR_NOT_SUPPORTED );
1469 method = ( export_public_key ?
1470 drv->key_management->p_export_public :
1471 drv->key_management->p_export );
1472 if( method == NULL )
1473 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001474 return( method( drv_context,
1475 slot->data.se.slot_number,
1476 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001477 }
1478#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1479
Gilles Peskine8e338702019-07-30 20:06:31 +02001480 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001481 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001482 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001483 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001484 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1485 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001486 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001487 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001488 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001489 /* Exporting public -> public */
1490 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1491 }
1492 else if( !export_public_key )
1493 {
1494 /* Exporting private -> private */
1495 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1496 }
1497 /* Need to export the public part of a private key,
1498 * so conversion is needed */
1499 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1500 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001501#if defined(MBEDTLS_RSA_C)
Steven Cooreman560c28a2020-07-24 23:20:24 +02001502 mbedtls_rsa_context rsa;
1503 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
Steven Cooremana01795d2020-07-24 22:48:15 +02001504
Steven Cooreman560c28a2020-07-24 23:20:24 +02001505 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
1506 if( status != PSA_SUCCESS )
1507 return status;
Steven Cooremana01795d2020-07-24 22:48:15 +02001508
Steven Cooreman560c28a2020-07-24 23:20:24 +02001509 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
1510 &rsa,
1511 data,
1512 data_size,
1513 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001514
Steven Cooreman560c28a2020-07-24 23:20:24 +02001515 mbedtls_rsa_free( &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001516
Steven Cooreman560c28a2020-07-24 23:20:24 +02001517 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001518#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001519 /* We don't know how to convert a private RSA key to public. */
1520 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001521#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001522 }
1523 else
Moran Pekera998bc62018-04-16 18:16:20 +03001524 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001525#if defined(MBEDTLS_ECP_C)
1526 mbedtls_ecp_keypair ecp;
1527 psa_status_t status = psa_load_ecp_representation( slot, &ecp );
1528 if( status != PSA_SUCCESS )
1529 return status;
1530
1531 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1532 PSA_KEY_TYPE_ECC_GET_FAMILY(
1533 slot->attr.type ) ),
1534 &ecp,
1535 data,
1536 data_size,
1537 data_length );
1538
1539 mbedtls_ecp_keypair_free( &ecp );
1540 return( status );
1541#else
1542 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001543 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001544#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001545 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001546 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001547 else
1548 {
1549 /* This shouldn't happen in the reference implementation, but
1550 it is valid for a special-purpose implementation to omit
1551 support for exporting certain key types. */
1552 return( PSA_ERROR_NOT_SUPPORTED );
1553 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001554}
1555
Gilles Peskinec5487a82018-12-03 18:08:14 +01001556psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001557 uint8_t *data,
1558 size_t data_size,
1559 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001560{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001561 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001562 psa_status_t status;
1563
1564 /* Set the key to empty now, so that even when there are errors, we always
1565 * set data_length to a value between 0 and data_size. On error, setting
1566 * the key to empty is a good choice because an empty key representation is
1567 * unlikely to be accepted anywhere. */
1568 *data_length = 0;
1569
1570 /* Export requires the EXPORT flag. There is an exception for public keys,
1571 * which don't require any flag, but psa_get_key_from_slot takes
1572 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001573 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001574 if( status != PSA_SUCCESS )
1575 return( status );
1576 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001577 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001578}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001579
Gilles Peskinec5487a82018-12-03 18:08:14 +01001580psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001581 uint8_t *data,
1582 size_t data_size,
1583 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001584{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001585 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001586 psa_status_t status;
1587
1588 /* Set the key to empty now, so that even when there are errors, we always
1589 * set data_length to a value between 0 and data_size. On error, setting
1590 * the key to empty is a good choice because an empty key representation is
1591 * unlikely to be accepted anywhere. */
1592 *data_length = 0;
1593
1594 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001595 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001596 if( status != PSA_SUCCESS )
1597 return( status );
1598 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001599 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001600}
1601
Gilles Peskine91e8c332019-08-02 19:19:39 +02001602#if defined(static_assert)
1603static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1604 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001605static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001606 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001607static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001608 "One or more key attribute flag is listed as both internal-only and external-only" );
1609#endif
1610
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001611/** Validate that a key policy is internally well-formed.
1612 *
1613 * This function only rejects invalid policies. It does not validate the
1614 * consistency of the policy with respect to other attributes of the key
1615 * such as the key type.
1616 */
1617static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001618{
1619 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001620 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001621 PSA_KEY_USAGE_ENCRYPT |
1622 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001623 PSA_KEY_USAGE_SIGN_HASH |
1624 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001625 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1626 return( PSA_ERROR_INVALID_ARGUMENT );
1627
Gilles Peskine4747d192019-04-17 15:05:45 +02001628 return( PSA_SUCCESS );
1629}
1630
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001631/** Validate the internal consistency of key attributes.
1632 *
1633 * This function only rejects invalid attribute values. If does not
1634 * validate the consistency of the attributes with any key data that may
1635 * be involved in the creation of the key.
1636 *
1637 * Call this function early in the key creation process.
1638 *
1639 * \param[in] attributes Key attributes for the new key.
1640 * \param[out] p_drv On any return, the driver for the key, if any.
1641 * NULL for a transparent key.
1642 *
1643 */
1644static psa_status_t psa_validate_key_attributes(
1645 const psa_key_attributes_t *attributes,
1646 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001647{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001648 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001649
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001650 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1651 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001652 if( status != PSA_SUCCESS )
1653 return( status );
1654
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001655 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1656 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001657 if( status != PSA_SUCCESS )
1658 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001659
1660 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001661 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001662 return( status );
1663
1664 /* Refuse to create overly large keys.
1665 * Note that this doesn't trigger on import if the attributes don't
1666 * explicitly specify a size (so psa_get_key_bits returns 0), so
1667 * psa_import_key() needs its own checks. */
1668 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1669 return( PSA_ERROR_NOT_SUPPORTED );
1670
Gilles Peskine91e8c332019-08-02 19:19:39 +02001671 /* Reject invalid flags. These should not be reachable through the API. */
1672 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1673 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1674 return( PSA_ERROR_INVALID_ARGUMENT );
1675
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001676 return( PSA_SUCCESS );
1677}
1678
Gilles Peskine4747d192019-04-17 15:05:45 +02001679/** Prepare a key slot to receive key material.
1680 *
1681 * This function allocates a key slot and sets its metadata.
1682 *
1683 * If this function fails, call psa_fail_key_creation().
1684 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001685 * This function is intended to be used as follows:
1686 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1687 * it with the specified attributes, and assign it a handle.
1688 * -# Populate the slot with the key material.
1689 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1690 * In case of failure at any step, stop the sequence and call
1691 * psa_fail_key_creation().
1692 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001693 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001694 * \param[in] attributes Key attributes for the new key.
1695 * \param[out] handle On success, a handle for the allocated slot.
1696 * \param[out] p_slot On success, a pointer to the prepared slot.
1697 * \param[out] p_drv On any return, the driver for the key, if any.
1698 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001699 *
1700 * \retval #PSA_SUCCESS
1701 * The key slot is ready to receive key material.
1702 * \return If this function fails, the key slot is an invalid state.
1703 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001704 */
1705static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001706 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001707 const psa_key_attributes_t *attributes,
1708 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001709 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001710 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001711{
1712 psa_status_t status;
1713 psa_key_slot_t *slot;
1714
Gilles Peskinedf179142019-07-15 22:02:14 +02001715 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001716 *p_drv = NULL;
1717
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001718 status = psa_validate_key_attributes( attributes, p_drv );
1719 if( status != PSA_SUCCESS )
1720 return( status );
1721
Gilles Peskineedbed562019-08-07 18:19:59 +02001722 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001723 if( status != PSA_SUCCESS )
1724 return( status );
1725 slot = *p_slot;
1726
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001727 /* We're storing the declared bit-size of the key. It's up to each
1728 * creation mechanism to verify that this information is correct.
1729 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001730 * an input (generate, device) but not for those where the bit-size
1731 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001732
1733 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001734
Gilles Peskine91e8c332019-08-02 19:19:39 +02001735 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001736 * external-only flags, query `attributes`. Thanks to the check
1737 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001738 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001739 * may have set. */
1740 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001741
Gilles Peskinecbaff462019-07-12 23:46:04 +02001742#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001743 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001744 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001745 * create the key file in internal storage, create the
1746 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001747 * persistent data. This is done by starting a transaction that will
1748 * encompass these three actions.
1749 * For registering a volatile key, we just need to find an appropriate
1750 * slot number inside the SE. Since the key is designated volatile, creating
1751 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001752 /* The first thing to do is to find a slot number for the new key.
1753 * We save the slot number in persistent storage as part of the
1754 * transaction data. It will be needed to recover if the power
1755 * fails during the key creation process, to clean up on the secure
1756 * element side after restarting. Obtaining a slot number from the
1757 * secure element driver updates its persistent state, but we do not yet
1758 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001759 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001760 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001761 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001762 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001763 &slot->data.se.slot_number );
1764 if( status != PSA_SUCCESS )
1765 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001766
1767 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001768 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001769 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1770 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1771 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1772 psa_crypto_transaction.key.id = slot->attr.id;
1773 status = psa_crypto_save_transaction( );
1774 if( status != PSA_SUCCESS )
1775 {
1776 (void) psa_crypto_stop_transaction( );
1777 return( status );
1778 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001779 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001780 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001781
1782 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1783 {
1784 /* Key registration only makes sense with a secure element. */
1785 return( PSA_ERROR_INVALID_ARGUMENT );
1786 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001787#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1788
Darryl Green0c6575a2018-11-07 16:05:30 +00001789 return( status );
1790}
Gilles Peskine4747d192019-04-17 15:05:45 +02001791
1792/** Finalize the creation of a key once its key material has been set.
1793 *
1794 * This entails writing the key to persistent storage.
1795 *
1796 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001797 * See the documentation of psa_start_key_creation() for the intended use
1798 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001799 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001800 * \param[in,out] slot Pointer to the slot with key material.
1801 * \param[in] driver The secure element driver for the key,
1802 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001803 *
1804 * \retval #PSA_SUCCESS
1805 * The key was successfully created. The handle is now valid.
1806 * \return If this function fails, the key slot is an invalid state.
1807 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001808 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001809static psa_status_t psa_finish_key_creation(
1810 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001811 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001812{
1813 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001814 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001815 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001816
1817#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001818 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001819 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001820#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1821 if( driver != NULL )
1822 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001823 psa_se_key_data_storage_t data;
1824#if defined(static_assert)
1825 static_assert( sizeof( slot->data.se.slot_number ) ==
1826 sizeof( data.slot_number ),
1827 "Slot number size does not match psa_se_key_data_storage_t" );
1828 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1829 "Bit-size size does not match psa_se_key_data_storage_t" );
1830#endif
1831 memcpy( &data.slot_number, &slot->data.se.slot_number,
1832 sizeof( slot->data.se.slot_number ) );
1833 memcpy( &data.bits, &slot->attr.bits,
1834 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001835 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001836 (uint8_t*) &data,
1837 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001838 }
1839 else
1840#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1841 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001842 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001843 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001844 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001845 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1846 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001847 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001848 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1849 status = psa_internal_export_key( slot,
1850 buffer, buffer_size, &length,
1851 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001852 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001853 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001854 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001855
Gilles Peskinef9168942019-09-12 19:20:29 +02001856 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001857 mbedtls_free( buffer );
1858 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001859 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001860#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1861
Gilles Peskinecbaff462019-07-12 23:46:04 +02001862#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001863 /* Finish the transaction for a key creation. This does not
1864 * happen when registering an existing key. Detect this case
1865 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001866 * creation of a persistent key in a secure element requires a transaction,
1867 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001868 if( driver != NULL &&
1869 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001870 {
1871 status = psa_save_se_persistent_data( driver );
1872 if( status != PSA_SUCCESS )
1873 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001874 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001875 return( status );
1876 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001877 status = psa_crypto_stop_transaction( );
1878 if( status != PSA_SUCCESS )
1879 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001880 }
1881#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1882
Gilles Peskine4747d192019-04-17 15:05:45 +02001883 return( status );
1884}
1885
1886/** Abort the creation of a key.
1887 *
1888 * You may call this function after calling psa_start_key_creation(),
1889 * or after psa_finish_key_creation() fails. In other circumstances, this
1890 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001891 * See the documentation of psa_start_key_creation() for the intended use
1892 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001893 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001894 * \param[in,out] slot Pointer to the slot with key material.
1895 * \param[in] driver The secure element driver for the key,
1896 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001897 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001898static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001899 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001900{
Gilles Peskine011e4282019-06-26 18:34:38 +02001901 (void) driver;
1902
Gilles Peskine4747d192019-04-17 15:05:45 +02001903 if( slot == NULL )
1904 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001905
1906#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001907 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001908 * element, and the failure happened later (when saving metadata
1909 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001910 * element.
1911 * https://github.com/ARMmbed/mbed-crypto/issues/217
1912 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001913
Gilles Peskined7729582019-08-05 15:55:54 +02001914 /* Abort the ongoing transaction if any (there may not be one if
1915 * the creation process failed before starting one, or if the
1916 * key creation is a registration of a key in a secure element).
1917 * Earlier functions must already have done what it takes to undo any
1918 * partial creation. All that's left is to update the transaction data
1919 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001920 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001921#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1922
Gilles Peskine4747d192019-04-17 15:05:45 +02001923 psa_wipe_key_slot( slot );
1924}
1925
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001926/** Validate optional attributes during key creation.
1927 *
1928 * Some key attributes are optional during key creation. If they are
1929 * specified in the attributes structure, check that they are consistent
1930 * with the data in the slot.
1931 *
1932 * This function should be called near the end of key creation, after
1933 * the slot in memory is fully populated but before saving persistent data.
1934 */
1935static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001936 const psa_key_slot_t *slot,
1937 const psa_key_attributes_t *attributes )
1938{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001939 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001941 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001942 return( PSA_ERROR_INVALID_ARGUMENT );
1943 }
1944
1945 if( attributes->domain_parameters_size != 0 )
1946 {
1947#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001948 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001950 mbedtls_rsa_context rsa;
1951 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
1952
1953 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
1954 if( status != PSA_SUCCESS )
1955 return status;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001956 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001957 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001958 mbedtls_mpi_init( &actual );
1959 mbedtls_mpi_init( &required );
Steven Cooremana01795d2020-07-24 22:48:15 +02001960 ret = mbedtls_rsa_export( &rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001961 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana01795d2020-07-24 22:48:15 +02001962 mbedtls_rsa_free( &rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963 if( ret != 0 )
1964 goto rsa_exit;
1965 ret = mbedtls_mpi_read_binary( &required,
1966 attributes->domain_parameters,
1967 attributes->domain_parameters_size );
1968 if( ret != 0 )
1969 goto rsa_exit;
1970 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1971 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1972 rsa_exit:
1973 mbedtls_mpi_free( &actual );
1974 mbedtls_mpi_free( &required );
1975 if( ret != 0)
1976 return( mbedtls_to_psa_error( ret ) );
1977 }
1978 else
1979#endif
1980 {
1981 return( PSA_ERROR_INVALID_ARGUMENT );
1982 }
1983 }
1984
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001985 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001986 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001987 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001988 return( PSA_ERROR_INVALID_ARGUMENT );
1989 }
1990
1991 return( PSA_SUCCESS );
1992}
1993
Gilles Peskine4747d192019-04-17 15:05:45 +02001994psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001995 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001996 size_t data_length,
1997 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001998{
1999 psa_status_t status;
2000 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002001 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002002
Gilles Peskine0f84d622019-09-12 19:03:13 +02002003 /* Reject zero-length symmetric keys (including raw data key objects).
2004 * This also rejects any key which might be encoded as an empty string,
2005 * which is never valid. */
2006 if( data_length == 0 )
2007 return( PSA_ERROR_INVALID_ARGUMENT );
2008
Gilles Peskinedf179142019-07-15 22:02:14 +02002009 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2010 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002011 if( status != PSA_SUCCESS )
2012 goto exit;
2013
Gilles Peskine5d309672019-07-12 23:47:28 +02002014#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2015 if( driver != NULL )
2016 {
2017 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002018 /* The driver should set the number of key bits, however in
2019 * case it doesn't, we initialize bits to an invalid value. */
2020 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002021 if( drv->key_management == NULL ||
2022 drv->key_management->p_import == NULL )
2023 {
2024 status = PSA_ERROR_NOT_SUPPORTED;
2025 goto exit;
2026 }
2027 status = drv->key_management->p_import(
2028 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002029 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002030 &bits );
2031 if( status != PSA_SUCCESS )
2032 goto exit;
2033 if( bits > PSA_MAX_KEY_BITS )
2034 {
2035 status = PSA_ERROR_NOT_SUPPORTED;
2036 goto exit;
2037 }
2038 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002039 }
2040 else
2041#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2042 {
2043 status = psa_import_key_into_slot( slot, data, data_length );
2044 if( status != PSA_SUCCESS )
2045 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002046 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002047 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002048 if( status != PSA_SUCCESS )
2049 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002050
Gilles Peskine011e4282019-06-26 18:34:38 +02002051 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002052exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002053 if( status != PSA_SUCCESS )
2054 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002055 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002056 *handle = 0;
2057 }
2058 return( status );
2059}
2060
Gilles Peskined7729582019-08-05 15:55:54 +02002061#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2062psa_status_t mbedtls_psa_register_se_key(
2063 const psa_key_attributes_t *attributes )
2064{
2065 psa_status_t status;
2066 psa_key_slot_t *slot = NULL;
2067 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002068 psa_key_handle_t handle = 0;
2069
2070 /* Leaving attributes unspecified is not currently supported.
2071 * It could make sense to query the key type and size from the
2072 * secure element, but not all secure elements support this
2073 * and the driver HAL doesn't currently support it. */
2074 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2075 return( PSA_ERROR_NOT_SUPPORTED );
2076 if( psa_get_key_bits( attributes ) == 0 )
2077 return( PSA_ERROR_NOT_SUPPORTED );
2078
2079 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2080 &handle, &slot, &driver );
2081 if( status != PSA_SUCCESS )
2082 goto exit;
2083
Gilles Peskined7729582019-08-05 15:55:54 +02002084 status = psa_finish_key_creation( slot, driver );
2085
2086exit:
2087 if( status != PSA_SUCCESS )
2088 {
2089 psa_fail_key_creation( slot, driver );
2090 }
2091 /* Registration doesn't keep the key in RAM. */
2092 psa_close_key( handle );
2093 return( status );
2094}
2095#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2096
Gilles Peskinef603c712019-01-19 13:40:11 +01002097static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002098 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002099{
2100 psa_status_t status;
2101 uint8_t *buffer = NULL;
2102 size_t buffer_size = 0;
2103 size_t length;
2104
Gilles Peskine8e338702019-07-30 20:06:31 +02002105 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002106 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002107 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002108 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002109 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002110 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2111 if( status != PSA_SUCCESS )
2112 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002113 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002114 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002115
2116exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002117 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002118 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002119 return( status );
2120}
2121
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002122psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2123 const psa_key_attributes_t *specified_attributes,
2124 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002125{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002126 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002127 psa_key_slot_t *source_slot = NULL;
2128 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002129 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002130 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002131
Gilles Peskine28f8f302019-07-24 13:30:31 +02002132 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002133 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002134 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002135 goto exit;
2136
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002137 status = psa_validate_optional_attributes( source_slot,
2138 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002139 if( status != PSA_SUCCESS )
2140 goto exit;
2141
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002142 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002143 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002144 if( status != PSA_SUCCESS )
2145 goto exit;
2146
Gilles Peskinedf179142019-07-15 22:02:14 +02002147 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2148 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002149 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002150 if( status != PSA_SUCCESS )
2151 goto exit;
2152
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002153#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2154 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002155 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002156 /* Copying to a secure element is not implemented yet. */
2157 status = PSA_ERROR_NOT_SUPPORTED;
2158 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002159 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002160#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002161
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002162 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002163 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002164 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002165
Gilles Peskine011e4282019-06-26 18:34:38 +02002166 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002167exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002168 if( status != PSA_SUCCESS )
2169 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002170 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002171 *target_handle = 0;
2172 }
2173 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002174}
2175
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002176
2177
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002178/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002179/* Message digests */
2180/****************************************************************/
2181
Gilles Peskineb16841e2019-10-10 20:36:12 +02002182#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002183static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002184{
2185 switch( alg )
2186 {
2187#if defined(MBEDTLS_MD2_C)
2188 case PSA_ALG_MD2:
2189 return( &mbedtls_md2_info );
2190#endif
2191#if defined(MBEDTLS_MD4_C)
2192 case PSA_ALG_MD4:
2193 return( &mbedtls_md4_info );
2194#endif
2195#if defined(MBEDTLS_MD5_C)
2196 case PSA_ALG_MD5:
2197 return( &mbedtls_md5_info );
2198#endif
2199#if defined(MBEDTLS_RIPEMD160_C)
2200 case PSA_ALG_RIPEMD160:
2201 return( &mbedtls_ripemd160_info );
2202#endif
2203#if defined(MBEDTLS_SHA1_C)
2204 case PSA_ALG_SHA_1:
2205 return( &mbedtls_sha1_info );
2206#endif
2207#if defined(MBEDTLS_SHA256_C)
2208 case PSA_ALG_SHA_224:
2209 return( &mbedtls_sha224_info );
2210 case PSA_ALG_SHA_256:
2211 return( &mbedtls_sha256_info );
2212#endif
2213#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002214#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002215 case PSA_ALG_SHA_384:
2216 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002217#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002218 case PSA_ALG_SHA_512:
2219 return( &mbedtls_sha512_info );
2220#endif
2221 default:
2222 return( NULL );
2223 }
2224}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002225#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002226
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002227psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2228{
2229 switch( operation->alg )
2230 {
Gilles Peskine81736312018-06-26 15:04:31 +02002231 case 0:
2232 /* The object has (apparently) been initialized but it is not
2233 * in use. It's ok to call abort on such an object, and there's
2234 * nothing to do. */
2235 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002236#if defined(MBEDTLS_MD2_C)
2237 case PSA_ALG_MD2:
2238 mbedtls_md2_free( &operation->ctx.md2 );
2239 break;
2240#endif
2241#if defined(MBEDTLS_MD4_C)
2242 case PSA_ALG_MD4:
2243 mbedtls_md4_free( &operation->ctx.md4 );
2244 break;
2245#endif
2246#if defined(MBEDTLS_MD5_C)
2247 case PSA_ALG_MD5:
2248 mbedtls_md5_free( &operation->ctx.md5 );
2249 break;
2250#endif
2251#if defined(MBEDTLS_RIPEMD160_C)
2252 case PSA_ALG_RIPEMD160:
2253 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2254 break;
2255#endif
2256#if defined(MBEDTLS_SHA1_C)
2257 case PSA_ALG_SHA_1:
2258 mbedtls_sha1_free( &operation->ctx.sha1 );
2259 break;
2260#endif
2261#if defined(MBEDTLS_SHA256_C)
2262 case PSA_ALG_SHA_224:
2263 case PSA_ALG_SHA_256:
2264 mbedtls_sha256_free( &operation->ctx.sha256 );
2265 break;
2266#endif
2267#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002268#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002270#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271 case PSA_ALG_SHA_512:
2272 mbedtls_sha512_free( &operation->ctx.sha512 );
2273 break;
2274#endif
2275 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002276 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002277 }
2278 operation->alg = 0;
2279 return( PSA_SUCCESS );
2280}
2281
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002282psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002283 psa_algorithm_t alg )
2284{
Janos Follath24eed8d2019-11-22 13:21:35 +00002285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002286
2287 /* A context must be freshly initialized before it can be set up. */
2288 if( operation->alg != 0 )
2289 {
2290 return( PSA_ERROR_BAD_STATE );
2291 }
2292
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002293 switch( alg )
2294 {
2295#if defined(MBEDTLS_MD2_C)
2296 case PSA_ALG_MD2:
2297 mbedtls_md2_init( &operation->ctx.md2 );
2298 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2299 break;
2300#endif
2301#if defined(MBEDTLS_MD4_C)
2302 case PSA_ALG_MD4:
2303 mbedtls_md4_init( &operation->ctx.md4 );
2304 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2305 break;
2306#endif
2307#if defined(MBEDTLS_MD5_C)
2308 case PSA_ALG_MD5:
2309 mbedtls_md5_init( &operation->ctx.md5 );
2310 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2311 break;
2312#endif
2313#if defined(MBEDTLS_RIPEMD160_C)
2314 case PSA_ALG_RIPEMD160:
2315 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2316 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2317 break;
2318#endif
2319#if defined(MBEDTLS_SHA1_C)
2320 case PSA_ALG_SHA_1:
2321 mbedtls_sha1_init( &operation->ctx.sha1 );
2322 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2323 break;
2324#endif
2325#if defined(MBEDTLS_SHA256_C)
2326 case PSA_ALG_SHA_224:
2327 mbedtls_sha256_init( &operation->ctx.sha256 );
2328 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2329 break;
2330 case PSA_ALG_SHA_256:
2331 mbedtls_sha256_init( &operation->ctx.sha256 );
2332 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2333 break;
2334#endif
2335#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002336#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337 case PSA_ALG_SHA_384:
2338 mbedtls_sha512_init( &operation->ctx.sha512 );
2339 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2340 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002341#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342 case PSA_ALG_SHA_512:
2343 mbedtls_sha512_init( &operation->ctx.sha512 );
2344 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2345 break;
2346#endif
2347 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002348 return( PSA_ALG_IS_HASH( alg ) ?
2349 PSA_ERROR_NOT_SUPPORTED :
2350 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351 }
2352 if( ret == 0 )
2353 operation->alg = alg;
2354 else
2355 psa_hash_abort( operation );
2356 return( mbedtls_to_psa_error( ret ) );
2357}
2358
2359psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2360 const uint8_t *input,
2361 size_t input_length )
2362{
Janos Follath24eed8d2019-11-22 13:21:35 +00002363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002364
2365 /* Don't require hash implementations to behave correctly on a
2366 * zero-length input, which may have an invalid pointer. */
2367 if( input_length == 0 )
2368 return( PSA_SUCCESS );
2369
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002370 switch( operation->alg )
2371 {
2372#if defined(MBEDTLS_MD2_C)
2373 case PSA_ALG_MD2:
2374 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2375 input, input_length );
2376 break;
2377#endif
2378#if defined(MBEDTLS_MD4_C)
2379 case PSA_ALG_MD4:
2380 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2381 input, input_length );
2382 break;
2383#endif
2384#if defined(MBEDTLS_MD5_C)
2385 case PSA_ALG_MD5:
2386 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2387 input, input_length );
2388 break;
2389#endif
2390#if defined(MBEDTLS_RIPEMD160_C)
2391 case PSA_ALG_RIPEMD160:
2392 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2393 input, input_length );
2394 break;
2395#endif
2396#if defined(MBEDTLS_SHA1_C)
2397 case PSA_ALG_SHA_1:
2398 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2399 input, input_length );
2400 break;
2401#endif
2402#if defined(MBEDTLS_SHA256_C)
2403 case PSA_ALG_SHA_224:
2404 case PSA_ALG_SHA_256:
2405 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2406 input, input_length );
2407 break;
2408#endif
2409#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002410#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002411 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002412#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002413 case PSA_ALG_SHA_512:
2414 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2415 input, input_length );
2416 break;
2417#endif
2418 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002419 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002420 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002421
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002422 if( ret != 0 )
2423 psa_hash_abort( operation );
2424 return( mbedtls_to_psa_error( ret ) );
2425}
2426
2427psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2428 uint8_t *hash,
2429 size_t hash_size,
2430 size_t *hash_length )
2431{
itayzafrir40835d42018-08-02 13:14:17 +03002432 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002434 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002435
2436 /* Fill the output buffer with something that isn't a valid hash
2437 * (barring an attack on the hash and deliberately-crafted input),
2438 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002439 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002440 /* If hash_size is 0 then hash may be NULL and then the
2441 * call to memset would have undefined behavior. */
2442 if( hash_size != 0 )
2443 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002444
2445 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002446 {
2447 status = PSA_ERROR_BUFFER_TOO_SMALL;
2448 goto exit;
2449 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002450
2451 switch( operation->alg )
2452 {
2453#if defined(MBEDTLS_MD2_C)
2454 case PSA_ALG_MD2:
2455 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2456 break;
2457#endif
2458#if defined(MBEDTLS_MD4_C)
2459 case PSA_ALG_MD4:
2460 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2461 break;
2462#endif
2463#if defined(MBEDTLS_MD5_C)
2464 case PSA_ALG_MD5:
2465 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2466 break;
2467#endif
2468#if defined(MBEDTLS_RIPEMD160_C)
2469 case PSA_ALG_RIPEMD160:
2470 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2471 break;
2472#endif
2473#if defined(MBEDTLS_SHA1_C)
2474 case PSA_ALG_SHA_1:
2475 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2476 break;
2477#endif
2478#if defined(MBEDTLS_SHA256_C)
2479 case PSA_ALG_SHA_224:
2480 case PSA_ALG_SHA_256:
2481 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2482 break;
2483#endif
2484#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002485#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002486 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002487#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488 case PSA_ALG_SHA_512:
2489 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2490 break;
2491#endif
2492 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002493 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494 }
itayzafrir40835d42018-08-02 13:14:17 +03002495 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002496
itayzafrir40835d42018-08-02 13:14:17 +03002497exit:
2498 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002499 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002500 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002501 return( psa_hash_abort( operation ) );
2502 }
2503 else
2504 {
2505 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002506 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002507 }
2508}
2509
Gilles Peskine2d277862018-06-18 15:41:12 +02002510psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2511 const uint8_t *hash,
2512 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002513{
2514 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2515 size_t actual_hash_length;
2516 psa_status_t status = psa_hash_finish( operation,
2517 actual_hash, sizeof( actual_hash ),
2518 &actual_hash_length );
2519 if( status != PSA_SUCCESS )
2520 return( status );
2521 if( actual_hash_length != hash_length )
2522 return( PSA_ERROR_INVALID_SIGNATURE );
2523 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2524 return( PSA_ERROR_INVALID_SIGNATURE );
2525 return( PSA_SUCCESS );
2526}
2527
Gilles Peskine0a749c82019-11-28 19:33:58 +01002528psa_status_t psa_hash_compute( psa_algorithm_t alg,
2529 const uint8_t *input, size_t input_length,
2530 uint8_t *hash, size_t hash_size,
2531 size_t *hash_length )
2532{
2533 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2534 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2535
2536 *hash_length = hash_size;
2537 status = psa_hash_setup( &operation, alg );
2538 if( status != PSA_SUCCESS )
2539 goto exit;
2540 status = psa_hash_update( &operation, input, input_length );
2541 if( status != PSA_SUCCESS )
2542 goto exit;
2543 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2544 if( status != PSA_SUCCESS )
2545 goto exit;
2546
2547exit:
2548 if( status == PSA_SUCCESS )
2549 status = psa_hash_abort( &operation );
2550 else
2551 psa_hash_abort( &operation );
2552 return( status );
2553}
2554
2555psa_status_t psa_hash_compare( psa_algorithm_t alg,
2556 const uint8_t *input, size_t input_length,
2557 const uint8_t *hash, size_t hash_length )
2558{
2559 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2560 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2561
2562 status = psa_hash_setup( &operation, alg );
2563 if( status != PSA_SUCCESS )
2564 goto exit;
2565 status = psa_hash_update( &operation, input, input_length );
2566 if( status != PSA_SUCCESS )
2567 goto exit;
2568 status = psa_hash_verify( &operation, hash, hash_length );
2569 if( status != PSA_SUCCESS )
2570 goto exit;
2571
2572exit:
2573 if( status == PSA_SUCCESS )
2574 status = psa_hash_abort( &operation );
2575 else
2576 psa_hash_abort( &operation );
2577 return( status );
2578}
2579
Gilles Peskineeb35d782019-01-22 17:56:16 +01002580psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2581 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002582{
2583 if( target_operation->alg != 0 )
2584 return( PSA_ERROR_BAD_STATE );
2585
2586 switch( source_operation->alg )
2587 {
2588 case 0:
2589 return( PSA_ERROR_BAD_STATE );
2590#if defined(MBEDTLS_MD2_C)
2591 case PSA_ALG_MD2:
2592 mbedtls_md2_clone( &target_operation->ctx.md2,
2593 &source_operation->ctx.md2 );
2594 break;
2595#endif
2596#if defined(MBEDTLS_MD4_C)
2597 case PSA_ALG_MD4:
2598 mbedtls_md4_clone( &target_operation->ctx.md4,
2599 &source_operation->ctx.md4 );
2600 break;
2601#endif
2602#if defined(MBEDTLS_MD5_C)
2603 case PSA_ALG_MD5:
2604 mbedtls_md5_clone( &target_operation->ctx.md5,
2605 &source_operation->ctx.md5 );
2606 break;
2607#endif
2608#if defined(MBEDTLS_RIPEMD160_C)
2609 case PSA_ALG_RIPEMD160:
2610 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2611 &source_operation->ctx.ripemd160 );
2612 break;
2613#endif
2614#if defined(MBEDTLS_SHA1_C)
2615 case PSA_ALG_SHA_1:
2616 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2617 &source_operation->ctx.sha1 );
2618 break;
2619#endif
2620#if defined(MBEDTLS_SHA256_C)
2621 case PSA_ALG_SHA_224:
2622 case PSA_ALG_SHA_256:
2623 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2624 &source_operation->ctx.sha256 );
2625 break;
2626#endif
2627#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002628#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002629 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002630#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002631 case PSA_ALG_SHA_512:
2632 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2633 &source_operation->ctx.sha512 );
2634 break;
2635#endif
2636 default:
2637 return( PSA_ERROR_NOT_SUPPORTED );
2638 }
2639
2640 target_operation->alg = source_operation->alg;
2641 return( PSA_SUCCESS );
2642}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002643
2644
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002645/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002646/* MAC */
2647/****************************************************************/
2648
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002649static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002650 psa_algorithm_t alg,
2651 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002652 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002653 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002654{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002655 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002656 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002658 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002659 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002660
Gilles Peskine8c9def32018-02-08 10:02:12 +01002661 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2662 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002663 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002665 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002666 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002667 mode = MBEDTLS_MODE_STREAM;
2668 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002669 case PSA_ALG_CTR:
2670 mode = MBEDTLS_MODE_CTR;
2671 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002672 case PSA_ALG_CFB:
2673 mode = MBEDTLS_MODE_CFB;
2674 break;
2675 case PSA_ALG_OFB:
2676 mode = MBEDTLS_MODE_OFB;
2677 break;
2678 case PSA_ALG_CBC_NO_PADDING:
2679 mode = MBEDTLS_MODE_CBC;
2680 break;
2681 case PSA_ALG_CBC_PKCS7:
2682 mode = MBEDTLS_MODE_CBC;
2683 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002684 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002685 mode = MBEDTLS_MODE_CCM;
2686 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002687 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688 mode = MBEDTLS_MODE_GCM;
2689 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002690 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2691 mode = MBEDTLS_MODE_CHACHAPOLY;
2692 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002693 default:
2694 return( NULL );
2695 }
2696 }
2697 else if( alg == PSA_ALG_CMAC )
2698 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002699 else
2700 return( NULL );
2701
2702 switch( key_type )
2703 {
2704 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002705 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002706 break;
2707 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002708 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2709 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002711 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002712 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002713 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002714 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2715 * but two-key Triple-DES is functionally three-key Triple-DES
2716 * with K1=K3, so that's how we present it to mbedtls. */
2717 if( key_bits == 128 )
2718 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002719 break;
2720 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002721 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722 break;
2723 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002724 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002725 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002726 case PSA_KEY_TYPE_CHACHA20:
2727 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2728 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002729 default:
2730 return( NULL );
2731 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002732 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002733 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002734
Jaeden Amero23bbb752018-06-26 14:16:54 +01002735 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2736 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002737}
2738
Gilles Peskinea05219c2018-11-16 16:02:56 +01002739#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002740static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002741{
Gilles Peskine2d277862018-06-18 15:41:12 +02002742 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002743 {
2744 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002745 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002746 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002747 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002748 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002749 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002750 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002751 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002752 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002753 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002754 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002755 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002756 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002757 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002758 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002759 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002760 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002761 return( 128 );
2762 default:
2763 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002764 }
2765}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002766#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002767
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002768/* Initialize the MAC operation structure. Once this function has been
2769 * called, psa_mac_abort can run and will do the right thing. */
2770static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2771 psa_algorithm_t alg )
2772{
2773 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2774
2775 operation->alg = alg;
2776 operation->key_set = 0;
2777 operation->iv_set = 0;
2778 operation->iv_required = 0;
2779 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002780 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002781
2782#if defined(MBEDTLS_CMAC_C)
2783 if( alg == PSA_ALG_CMAC )
2784 {
2785 operation->iv_required = 0;
2786 mbedtls_cipher_init( &operation->ctx.cmac );
2787 status = PSA_SUCCESS;
2788 }
2789 else
2790#endif /* MBEDTLS_CMAC_C */
2791#if defined(MBEDTLS_MD_C)
2792 if( PSA_ALG_IS_HMAC( operation->alg ) )
2793 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002794 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2795 operation->ctx.hmac.hash_ctx.alg = 0;
2796 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002797 }
2798 else
2799#endif /* MBEDTLS_MD_C */
2800 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002801 if( ! PSA_ALG_IS_MAC( alg ) )
2802 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002803 }
2804
2805 if( status != PSA_SUCCESS )
2806 memset( operation, 0, sizeof( *operation ) );
2807 return( status );
2808}
2809
Gilles Peskine01126fa2018-07-12 17:04:55 +02002810#if defined(MBEDTLS_MD_C)
2811static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2812{
Gilles Peskine3f108122018-12-07 18:14:53 +01002813 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002814 return( psa_hash_abort( &hmac->hash_ctx ) );
2815}
2816#endif /* MBEDTLS_MD_C */
2817
Gilles Peskine8c9def32018-02-08 10:02:12 +01002818psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2819{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002820 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002821 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002822 /* The object has (apparently) been initialized but it is not
2823 * in use. It's ok to call abort on such an object, and there's
2824 * nothing to do. */
2825 return( PSA_SUCCESS );
2826 }
2827 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002828#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002829 if( operation->alg == PSA_ALG_CMAC )
2830 {
2831 mbedtls_cipher_free( &operation->ctx.cmac );
2832 }
2833 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002834#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002835#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002836 if( PSA_ALG_IS_HMAC( operation->alg ) )
2837 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002838 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002839 }
2840 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002841#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002842 {
2843 /* Sanity check (shouldn't happen: operation->alg should
2844 * always have been initialized to a valid value). */
2845 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002846 }
Moran Peker41deec42018-04-04 15:43:05 +03002847
Gilles Peskine8c9def32018-02-08 10:02:12 +01002848 operation->alg = 0;
2849 operation->key_set = 0;
2850 operation->iv_set = 0;
2851 operation->iv_required = 0;
2852 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002853 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002854
Gilles Peskine8c9def32018-02-08 10:02:12 +01002855 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002856
2857bad_state:
2858 /* If abort is called on an uninitialized object, we can't trust
2859 * anything. Wipe the object in case it contains confidential data.
2860 * This may result in a memory leak if a pointer gets overwritten,
2861 * but it's too late to do anything about this. */
2862 memset( operation, 0, sizeof( *operation ) );
2863 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002864}
2865
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002866#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002867static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002868 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002869 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002870 const mbedtls_cipher_info_t *cipher_info )
2871{
Janos Follath24eed8d2019-11-22 13:21:35 +00002872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002873
2874 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002875
2876 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2877 if( ret != 0 )
2878 return( ret );
2879
2880 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002881 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002882 key_bits );
2883 return( ret );
2884}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002885#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002886
Gilles Peskine248051a2018-06-20 16:09:38 +02002887#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002888static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2889 const uint8_t *key,
2890 size_t key_length,
2891 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002892{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002893 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002894 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002895 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002896 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002897 psa_status_t status;
2898
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002899 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2900 * overflow below. This should never trigger if the hash algorithm
2901 * is implemented correctly. */
2902 /* The size checks against the ipad and opad buffers cannot be written
2903 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2904 * because that triggers -Wlogical-op on GCC 7.3. */
2905 if( block_size > sizeof( ipad ) )
2906 return( PSA_ERROR_NOT_SUPPORTED );
2907 if( block_size > sizeof( hmac->opad ) )
2908 return( PSA_ERROR_NOT_SUPPORTED );
2909 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002910 return( PSA_ERROR_NOT_SUPPORTED );
2911
Gilles Peskined223b522018-06-11 18:12:58 +02002912 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002913 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002914 status = psa_hash_compute( hash_alg, key, key_length,
2915 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002916 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002917 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002918 }
Gilles Peskine96889972018-07-12 17:07:03 +02002919 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2920 * but it is permitted. It is common when HMAC is used in HKDF, for
2921 * example. Don't call `memcpy` in the 0-length because `key` could be
2922 * an invalid pointer which would make the behavior undefined. */
2923 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002924 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002925
Gilles Peskined223b522018-06-11 18:12:58 +02002926 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2927 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002928 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002929 ipad[i] ^= 0x36;
2930 memset( ipad + key_length, 0x36, block_size - key_length );
2931
2932 /* Copy the key material from ipad to opad, flipping the requisite bits,
2933 * and filling the rest of opad with the requisite constant. */
2934 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002935 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2936 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002937
Gilles Peskine01126fa2018-07-12 17:04:55 +02002938 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002939 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002940 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002941
Gilles Peskine01126fa2018-07-12 17:04:55 +02002942 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002943
2944cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002945 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002946
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002947 return( status );
2948}
Gilles Peskine248051a2018-06-20 16:09:38 +02002949#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002950
Gilles Peskine89167cb2018-07-08 20:12:23 +02002951static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002952 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002953 psa_algorithm_t alg,
2954 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002955{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002956 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002957 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002958 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002959 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002960 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002961 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002962 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002963
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002964 /* A context must be freshly initialized before it can be set up. */
2965 if( operation->alg != 0 )
2966 {
2967 return( PSA_ERROR_BAD_STATE );
2968 }
2969
Gilles Peskined911eb72018-08-14 15:18:45 +02002970 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002971 if( status != PSA_SUCCESS )
2972 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002973 if( is_sign )
2974 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002975
Gilles Peskine28f8f302019-07-24 13:30:31 +02002976 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002977 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002978 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002979 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002980
Gilles Peskine8c9def32018-02-08 10:02:12 +01002981#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002982 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002983 {
2984 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002985 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002986 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002987 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002988 if( cipher_info == NULL )
2989 {
2990 status = PSA_ERROR_NOT_SUPPORTED;
2991 goto exit;
2992 }
2993 operation->mac_size = cipher_info->block_size;
2994 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2995 status = mbedtls_to_psa_error( ret );
2996 }
2997 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002998#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002999#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003000 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003001 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003002 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003003 if( hash_alg == 0 )
3004 {
3005 status = PSA_ERROR_NOT_SUPPORTED;
3006 goto exit;
3007 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003008
3009 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3010 /* Sanity check. This shouldn't fail on a valid configuration. */
3011 if( operation->mac_size == 0 ||
3012 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3013 {
3014 status = PSA_ERROR_NOT_SUPPORTED;
3015 goto exit;
3016 }
3017
Gilles Peskine8e338702019-07-30 20:06:31 +02003018 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003019 {
3020 status = PSA_ERROR_INVALID_ARGUMENT;
3021 goto exit;
3022 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003023
Gilles Peskine01126fa2018-07-12 17:04:55 +02003024 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003025 slot->data.key.data,
3026 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003027 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003028 }
3029 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003031 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003032 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003033 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003035
Gilles Peskined911eb72018-08-14 15:18:45 +02003036 if( truncated == 0 )
3037 {
3038 /* The "normal" case: untruncated algorithm. Nothing to do. */
3039 }
3040 else if( truncated < 4 )
3041 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003042 /* A very short MAC is too short for security since it can be
3043 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3044 * so we make this our minimum, even though 32 bits is still
3045 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003046 status = PSA_ERROR_NOT_SUPPORTED;
3047 }
3048 else if( truncated > operation->mac_size )
3049 {
3050 /* It's impossible to "truncate" to a larger length. */
3051 status = PSA_ERROR_INVALID_ARGUMENT;
3052 }
3053 else
3054 operation->mac_size = truncated;
3055
Gilles Peskinefbfac682018-07-08 20:51:54 +02003056exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003057 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003058 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003059 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003060 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003061 else
3062 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003063 operation->key_set = 1;
3064 }
3065 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003066}
3067
Gilles Peskine89167cb2018-07-08 20:12:23 +02003068psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003069 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003070 psa_algorithm_t alg )
3071{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003072 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003073}
3074
3075psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003076 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003077 psa_algorithm_t alg )
3078{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003079 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003080}
3081
Gilles Peskine8c9def32018-02-08 10:02:12 +01003082psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3083 const uint8_t *input,
3084 size_t input_length )
3085{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003086 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003087 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003088 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003090 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091 operation->has_input = 1;
3092
Gilles Peskine8c9def32018-02-08 10:02:12 +01003093#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003094 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003095 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003096 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3097 input, input_length );
3098 status = mbedtls_to_psa_error( ret );
3099 }
3100 else
3101#endif /* MBEDTLS_CMAC_C */
3102#if defined(MBEDTLS_MD_C)
3103 if( PSA_ALG_IS_HMAC( operation->alg ) )
3104 {
3105 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3106 input_length );
3107 }
3108 else
3109#endif /* MBEDTLS_MD_C */
3110 {
3111 /* This shouldn't happen if `operation` was initialized by
3112 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003113 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003114 }
3115
Gilles Peskinefbfac682018-07-08 20:51:54 +02003116 if( status != PSA_SUCCESS )
3117 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003118 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003119}
3120
Gilles Peskine01126fa2018-07-12 17:04:55 +02003121#if defined(MBEDTLS_MD_C)
3122static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3123 uint8_t *mac,
3124 size_t mac_size )
3125{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003126 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003127 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3128 size_t hash_size = 0;
3129 size_t block_size = psa_get_hash_block_size( hash_alg );
3130 psa_status_t status;
3131
Gilles Peskine01126fa2018-07-12 17:04:55 +02003132 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3133 if( status != PSA_SUCCESS )
3134 return( status );
3135 /* From here on, tmp needs to be wiped. */
3136
3137 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3138 if( status != PSA_SUCCESS )
3139 goto exit;
3140
3141 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3142 if( status != PSA_SUCCESS )
3143 goto exit;
3144
3145 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3146 if( status != PSA_SUCCESS )
3147 goto exit;
3148
Gilles Peskined911eb72018-08-14 15:18:45 +02003149 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3150 if( status != PSA_SUCCESS )
3151 goto exit;
3152
3153 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003154
3155exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003156 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003157 return( status );
3158}
3159#endif /* MBEDTLS_MD_C */
3160
mohammad16036df908f2018-04-02 08:34:15 -07003161static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003162 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003163 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003164{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003165 if( ! operation->key_set )
3166 return( PSA_ERROR_BAD_STATE );
3167 if( operation->iv_required && ! operation->iv_set )
3168 return( PSA_ERROR_BAD_STATE );
3169
Gilles Peskine8c9def32018-02-08 10:02:12 +01003170 if( mac_size < operation->mac_size )
3171 return( PSA_ERROR_BUFFER_TOO_SMALL );
3172
Gilles Peskine8c9def32018-02-08 10:02:12 +01003173#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003174 if( operation->alg == PSA_ALG_CMAC )
3175 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003176 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3177 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3178 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003179 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003180 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003181 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003182 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003183 else
3184#endif /* MBEDTLS_CMAC_C */
3185#if defined(MBEDTLS_MD_C)
3186 if( PSA_ALG_IS_HMAC( operation->alg ) )
3187 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003188 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003189 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003190 }
3191 else
3192#endif /* MBEDTLS_MD_C */
3193 {
3194 /* This shouldn't happen if `operation` was initialized by
3195 * a setup function. */
3196 return( PSA_ERROR_BAD_STATE );
3197 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003198}
3199
Gilles Peskineacd4be32018-07-08 19:56:25 +02003200psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3201 uint8_t *mac,
3202 size_t mac_size,
3203 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003204{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003205 psa_status_t status;
3206
Jaeden Amero252ef282019-02-15 14:05:35 +00003207 if( operation->alg == 0 )
3208 {
3209 return( PSA_ERROR_BAD_STATE );
3210 }
3211
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003212 /* Fill the output buffer with something that isn't a valid mac
3213 * (barring an attack on the mac and deliberately-crafted input),
3214 * in case the caller doesn't check the return status properly. */
3215 *mac_length = mac_size;
3216 /* If mac_size is 0 then mac may be NULL and then the
3217 * call to memset would have undefined behavior. */
3218 if( mac_size != 0 )
3219 memset( mac, '!', mac_size );
3220
Gilles Peskine89167cb2018-07-08 20:12:23 +02003221 if( ! operation->is_sign )
3222 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003223 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003224 }
mohammad16036df908f2018-04-02 08:34:15 -07003225
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003226 status = psa_mac_finish_internal( operation, mac, mac_size );
3227
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003228 if( status == PSA_SUCCESS )
3229 {
3230 status = psa_mac_abort( operation );
3231 if( status == PSA_SUCCESS )
3232 *mac_length = operation->mac_size;
3233 else
3234 memset( mac, '!', mac_size );
3235 }
3236 else
3237 psa_mac_abort( operation );
3238 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003239}
3240
Gilles Peskineacd4be32018-07-08 19:56:25 +02003241psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3242 const uint8_t *mac,
3243 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003244{
Gilles Peskine828ed142018-06-18 23:25:51 +02003245 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003246 psa_status_t status;
3247
Jaeden Amero252ef282019-02-15 14:05:35 +00003248 if( operation->alg == 0 )
3249 {
3250 return( PSA_ERROR_BAD_STATE );
3251 }
3252
Gilles Peskine89167cb2018-07-08 20:12:23 +02003253 if( operation->is_sign )
3254 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003255 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003256 }
3257 if( operation->mac_size != mac_length )
3258 {
3259 status = PSA_ERROR_INVALID_SIGNATURE;
3260 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003261 }
mohammad16036df908f2018-04-02 08:34:15 -07003262
3263 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003264 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003265 if( status != PSA_SUCCESS )
3266 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003267
3268 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3269 status = PSA_ERROR_INVALID_SIGNATURE;
3270
3271cleanup:
3272 if( status == PSA_SUCCESS )
3273 status = psa_mac_abort( operation );
3274 else
3275 psa_mac_abort( operation );
3276
Gilles Peskine3f108122018-12-07 18:14:53 +01003277 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003278
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003279 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003280}
3281
3282
Gilles Peskine20035e32018-02-03 22:44:14 +01003283
Gilles Peskine20035e32018-02-03 22:44:14 +01003284/****************************************************************/
3285/* Asymmetric cryptography */
3286/****************************************************************/
3287
Gilles Peskine2b450e32018-06-27 15:42:46 +02003288#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003289/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003290 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003291static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3292 size_t hash_length,
3293 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003294{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003295 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003296 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003297 *md_alg = mbedtls_md_get_type( md_info );
3298
3299 /* The Mbed TLS RSA module uses an unsigned int for hash length
3300 * parameters. Validate that it fits so that we don't risk an
3301 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003302#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003303 if( hash_length > UINT_MAX )
3304 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003305#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003306
3307#if defined(MBEDTLS_PKCS1_V15)
3308 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3309 * must be correct. */
3310 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3311 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003312 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003313 if( md_info == NULL )
3314 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003315 if( mbedtls_md_get_size( md_info ) != hash_length )
3316 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003317 }
3318#endif /* MBEDTLS_PKCS1_V15 */
3319
3320#if defined(MBEDTLS_PKCS1_V21)
3321 /* PSS requires a hash internally. */
3322 if( PSA_ALG_IS_RSA_PSS( alg ) )
3323 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003324 if( md_info == NULL )
3325 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003326 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003327#endif /* MBEDTLS_PKCS1_V21 */
3328
Gilles Peskine61b91d42018-06-08 16:09:36 +02003329 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003330}
3331
Gilles Peskine2b450e32018-06-27 15:42:46 +02003332static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3333 psa_algorithm_t alg,
3334 const uint8_t *hash,
3335 size_t hash_length,
3336 uint8_t *signature,
3337 size_t signature_size,
3338 size_t *signature_length )
3339{
3340 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003342 mbedtls_md_type_t md_alg;
3343
3344 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3345 if( status != PSA_SUCCESS )
3346 return( status );
3347
Gilles Peskine630a18a2018-06-29 17:49:35 +02003348 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003349 return( PSA_ERROR_BUFFER_TOO_SMALL );
3350
3351#if defined(MBEDTLS_PKCS1_V15)
3352 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3353 {
3354 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3355 MBEDTLS_MD_NONE );
3356 ret = mbedtls_rsa_pkcs1_sign( rsa,
3357 mbedtls_ctr_drbg_random,
3358 &global_data.ctr_drbg,
3359 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003360 md_alg,
3361 (unsigned int) hash_length,
3362 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003363 signature );
3364 }
3365 else
3366#endif /* MBEDTLS_PKCS1_V15 */
3367#if defined(MBEDTLS_PKCS1_V21)
3368 if( PSA_ALG_IS_RSA_PSS( alg ) )
3369 {
3370 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3371 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3372 mbedtls_ctr_drbg_random,
3373 &global_data.ctr_drbg,
3374 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003375 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003376 (unsigned int) hash_length,
3377 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003378 signature );
3379 }
3380 else
3381#endif /* MBEDTLS_PKCS1_V21 */
3382 {
3383 return( PSA_ERROR_INVALID_ARGUMENT );
3384 }
3385
3386 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003387 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003388 return( mbedtls_to_psa_error( ret ) );
3389}
3390
3391static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3392 psa_algorithm_t alg,
3393 const uint8_t *hash,
3394 size_t hash_length,
3395 const uint8_t *signature,
3396 size_t signature_length )
3397{
3398 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003400 mbedtls_md_type_t md_alg;
3401
3402 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3403 if( status != PSA_SUCCESS )
3404 return( status );
3405
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003406 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3407 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003408
3409#if defined(MBEDTLS_PKCS1_V15)
3410 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3411 {
3412 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3413 MBEDTLS_MD_NONE );
3414 ret = mbedtls_rsa_pkcs1_verify( rsa,
3415 mbedtls_ctr_drbg_random,
3416 &global_data.ctr_drbg,
3417 MBEDTLS_RSA_PUBLIC,
3418 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003419 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003420 hash,
3421 signature );
3422 }
3423 else
3424#endif /* MBEDTLS_PKCS1_V15 */
3425#if defined(MBEDTLS_PKCS1_V21)
3426 if( PSA_ALG_IS_RSA_PSS( alg ) )
3427 {
3428 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3429 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3430 mbedtls_ctr_drbg_random,
3431 &global_data.ctr_drbg,
3432 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003433 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003434 (unsigned int) hash_length,
3435 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003436 signature );
3437 }
3438 else
3439#endif /* MBEDTLS_PKCS1_V21 */
3440 {
3441 return( PSA_ERROR_INVALID_ARGUMENT );
3442 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003443
3444 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3445 * the rest of the signature is invalid". This has little use in
3446 * practice and PSA doesn't report this distinction. */
3447 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3448 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003449 return( mbedtls_to_psa_error( ret ) );
3450}
3451#endif /* MBEDTLS_RSA_C */
3452
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003453#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003454/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3455 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3456 * (even though these functions don't modify it). */
3457static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3458 psa_algorithm_t alg,
3459 const uint8_t *hash,
3460 size_t hash_length,
3461 uint8_t *signature,
3462 size_t signature_size,
3463 size_t *signature_length )
3464{
Janos Follath24eed8d2019-11-22 13:21:35 +00003465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003466 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003467 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003468 mbedtls_mpi_init( &r );
3469 mbedtls_mpi_init( &s );
3470
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003471 if( signature_size < 2 * curve_bytes )
3472 {
3473 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3474 goto cleanup;
3475 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003476
Gilles Peskinea05219c2018-11-16 16:02:56 +01003477#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003478 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3479 {
3480 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3481 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3482 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003483 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3484 &ecp->d, hash,
3485 hash_length, md_alg,
3486 mbedtls_ctr_drbg_random,
3487 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003488 }
3489 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003490#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003491 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003492 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003493 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3494 hash, hash_length,
3495 mbedtls_ctr_drbg_random,
3496 &global_data.ctr_drbg ) );
3497 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003498
3499 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3500 signature,
3501 curve_bytes ) );
3502 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3503 signature + curve_bytes,
3504 curve_bytes ) );
3505
3506cleanup:
3507 mbedtls_mpi_free( &r );
3508 mbedtls_mpi_free( &s );
3509 if( ret == 0 )
3510 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003511 return( mbedtls_to_psa_error( ret ) );
3512}
3513
3514static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3515 const uint8_t *hash,
3516 size_t hash_length,
3517 const uint8_t *signature,
3518 size_t signature_length )
3519{
Janos Follath24eed8d2019-11-22 13:21:35 +00003520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003521 mbedtls_mpi r, s;
3522 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3523 mbedtls_mpi_init( &r );
3524 mbedtls_mpi_init( &s );
3525
3526 if( signature_length != 2 * curve_bytes )
3527 return( PSA_ERROR_INVALID_SIGNATURE );
3528
3529 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3530 signature,
3531 curve_bytes ) );
3532 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3533 signature + curve_bytes,
3534 curve_bytes ) );
3535
Steven Cooremanacda8342020-07-24 23:09:52 +02003536 /* Check whether the public part is loaded. If not, load it. */
3537 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3538 {
3539 MBEDTLS_MPI_CHK(
3540 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3541 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3542 }
3543
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003544 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3545 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003546
3547cleanup:
3548 mbedtls_mpi_free( &r );
3549 mbedtls_mpi_free( &s );
3550 return( mbedtls_to_psa_error( ret ) );
3551}
3552#endif /* MBEDTLS_ECDSA_C */
3553
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003554psa_status_t psa_sign_hash( psa_key_handle_t handle,
3555 psa_algorithm_t alg,
3556 const uint8_t *hash,
3557 size_t hash_length,
3558 uint8_t *signature,
3559 size_t signature_size,
3560 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003561{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003562 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003563 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003564#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3565 const psa_drv_se_t *drv;
3566 psa_drv_se_context_t *drv_context;
3567#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003568
3569 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003570 /* Immediately reject a zero-length signature buffer. This guarantees
3571 * that signature must be a valid pointer. (On the other hand, the hash
3572 * buffer can in principle be empty since it doesn't actually have
3573 * to be a hash.) */
3574 if( signature_size == 0 )
3575 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003576
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003577 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003578 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003579 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003580 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003581 {
3582 status = PSA_ERROR_INVALID_ARGUMENT;
3583 goto exit;
3584 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003585
Gilles Peskineedc64242019-08-07 21:05:07 +02003586#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3587 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3588 {
3589 if( drv->asymmetric == NULL ||
3590 drv->asymmetric->p_sign == NULL )
3591 {
3592 status = PSA_ERROR_NOT_SUPPORTED;
3593 goto exit;
3594 }
3595 status = drv->asymmetric->p_sign( drv_context,
3596 slot->data.se.slot_number,
3597 alg,
3598 hash, hash_length,
3599 signature, signature_size,
3600 signature_length );
3601 }
3602 else
3603#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003604#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003605 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003606 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003607 mbedtls_rsa_context rsa;
3608 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3609
3610 status = psa_load_rsa_representation( slot,
3611 &rsa );
3612 if( status != PSA_SUCCESS )
3613 goto exit;
3614
3615 status = psa_rsa_sign( &rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003616 alg,
3617 hash, hash_length,
3618 signature, signature_size,
3619 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003620
3621 mbedtls_rsa_free( &rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003622 }
3623 else
3624#endif /* defined(MBEDTLS_RSA_C) */
3625#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003626 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003627 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003628#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003629 if(
3630#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3631 PSA_ALG_IS_ECDSA( alg )
3632#else
3633 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3634#endif
3635 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003636 {
3637 mbedtls_ecp_keypair ecp;
3638 status = psa_load_ecp_representation( slot, &ecp );
3639 if( status != PSA_SUCCESS )
3640 goto exit;
3641 status = psa_ecdsa_sign( &ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003642 alg,
3643 hash, hash_length,
3644 signature, signature_size,
3645 signature_length );
Steven Cooremanacda8342020-07-24 23:09:52 +02003646 mbedtls_ecp_keypair_free( &ecp );
3647 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003648 else
3649#endif /* defined(MBEDTLS_ECDSA_C) */
3650 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003651 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003652 }
itayzafrir5c753392018-05-08 11:18:38 +03003653 }
3654 else
3655#endif /* defined(MBEDTLS_ECP_C) */
3656 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003657 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003658 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003659
3660exit:
3661 /* Fill the unused part of the output buffer (the whole buffer on error,
3662 * the trailing part on success) with something that isn't a valid mac
3663 * (barring an attack on the mac and deliberately-crafted input),
3664 * in case the caller doesn't check the return status properly. */
3665 if( status == PSA_SUCCESS )
3666 memset( signature + *signature_length, '!',
3667 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003668 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003669 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003670 /* If signature_size is 0 then we have nothing to do. We must not call
3671 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003672 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003673}
3674
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003675psa_status_t psa_verify_hash( psa_key_handle_t handle,
3676 psa_algorithm_t alg,
3677 const uint8_t *hash,
3678 size_t hash_length,
3679 const uint8_t *signature,
3680 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003681{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003682 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003683 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003684#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3685 const psa_drv_se_t *drv;
3686 psa_drv_se_context_t *drv_context;
3687#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003688
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003689 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003690 if( status != PSA_SUCCESS )
3691 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003692
Gilles Peskineedc64242019-08-07 21:05:07 +02003693#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3694 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3695 {
3696 if( drv->asymmetric == NULL ||
3697 drv->asymmetric->p_verify == NULL )
3698 return( PSA_ERROR_NOT_SUPPORTED );
3699 return( drv->asymmetric->p_verify( drv_context,
3700 slot->data.se.slot_number,
3701 alg,
3702 hash, hash_length,
3703 signature, signature_length ) );
3704 }
3705 else
3706#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003707#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003708 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003709 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003710 mbedtls_rsa_context rsa;
3711 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3712
3713 status = psa_load_rsa_representation( slot, &rsa );
3714 if( status != PSA_SUCCESS )
3715 return status;
3716
3717 status = psa_rsa_verify( &rsa,
3718 alg,
3719 hash, hash_length,
3720 signature, signature_length );
3721 mbedtls_rsa_free( &rsa );
3722 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003723 }
3724 else
3725#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003726#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003727 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003728 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003729#if defined(MBEDTLS_ECDSA_C)
3730 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003731 {
3732 mbedtls_ecp_keypair ecp;
3733 status = psa_load_ecp_representation( slot, &ecp );
3734 if( status != PSA_SUCCESS )
3735 return status;
3736 status = psa_ecdsa_verify( &ecp,
3737 hash, hash_length,
3738 signature, signature_length );
3739 mbedtls_ecp_keypair_free( &ecp );
3740 return status;
3741 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003742 else
3743#endif /* defined(MBEDTLS_ECDSA_C) */
3744 {
3745 return( PSA_ERROR_INVALID_ARGUMENT );
3746 }
itayzafrir5c753392018-05-08 11:18:38 +03003747 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003748 else
3749#endif /* defined(MBEDTLS_ECP_C) */
3750 {
3751 return( PSA_ERROR_NOT_SUPPORTED );
3752 }
3753}
3754
Gilles Peskine072ac562018-06-30 00:21:29 +02003755#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3756static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3757 mbedtls_rsa_context *rsa )
3758{
3759 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3760 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3761 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3762 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3763}
3764#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3765
Gilles Peskinec5487a82018-12-03 18:08:14 +01003766psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003767 psa_algorithm_t alg,
3768 const uint8_t *input,
3769 size_t input_length,
3770 const uint8_t *salt,
3771 size_t salt_length,
3772 uint8_t *output,
3773 size_t output_size,
3774 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003775{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003776 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003777 psa_status_t status;
3778
Darryl Green5cc689a2018-07-24 15:34:10 +01003779 (void) input;
3780 (void) input_length;
3781 (void) salt;
3782 (void) output;
3783 (void) output_size;
3784
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003785 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003786
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003787 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3788 return( PSA_ERROR_INVALID_ARGUMENT );
3789
Gilles Peskine28f8f302019-07-24 13:30:31 +02003790 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003791 if( status != PSA_SUCCESS )
3792 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003793 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3794 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003795 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003796
3797#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003798 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003799 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003800 mbedtls_rsa_context rsa;
3801 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3802
3803 status = psa_load_rsa_representation( slot, &rsa );
3804 if( status != PSA_SUCCESS )
3805 return status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003806 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003807 if( output_size < mbedtls_rsa_get_len( &rsa ) )
3808 {
3809 mbedtls_rsa_free( &rsa );
Vikas Katariya21599b62019-08-02 12:26:29 +01003810 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooremana01795d2020-07-24 22:48:15 +02003811 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003812#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003813 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003814 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003815 ret = mbedtls_rsa_pkcs1_encrypt( &rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003816 mbedtls_ctr_drbg_random,
3817 &global_data.ctr_drbg,
3818 MBEDTLS_RSA_PUBLIC,
3819 input_length,
3820 input,
3821 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003822 }
3823 else
3824#endif /* MBEDTLS_PKCS1_V15 */
3825#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003826 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003827 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003828 psa_rsa_oaep_set_padding_mode( alg, &rsa );
3829 ret = mbedtls_rsa_rsaes_oaep_encrypt( &rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003830 mbedtls_ctr_drbg_random,
3831 &global_data.ctr_drbg,
3832 MBEDTLS_RSA_PUBLIC,
3833 salt, salt_length,
3834 input_length,
3835 input,
3836 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003837 }
3838 else
3839#endif /* MBEDTLS_PKCS1_V21 */
3840 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003841 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003842 return( PSA_ERROR_INVALID_ARGUMENT );
3843 }
3844 if( ret == 0 )
Steven Cooremana01795d2020-07-24 22:48:15 +02003845 *output_length = mbedtls_rsa_get_len( &rsa );
3846
3847 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003848 return( mbedtls_to_psa_error( ret ) );
3849 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003850 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003851#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003852 {
3853 return( PSA_ERROR_NOT_SUPPORTED );
3854 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003855}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003856
Gilles Peskinec5487a82018-12-03 18:08:14 +01003857psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003858 psa_algorithm_t alg,
3859 const uint8_t *input,
3860 size_t input_length,
3861 const uint8_t *salt,
3862 size_t salt_length,
3863 uint8_t *output,
3864 size_t output_size,
3865 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003866{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003867 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003868 psa_status_t status;
3869
Darryl Green5cc689a2018-07-24 15:34:10 +01003870 (void) input;
3871 (void) input_length;
3872 (void) salt;
3873 (void) output;
3874 (void) output_size;
3875
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003876 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003877
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003878 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3879 return( PSA_ERROR_INVALID_ARGUMENT );
3880
Gilles Peskine28f8f302019-07-24 13:30:31 +02003881 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003882 if( status != PSA_SUCCESS )
3883 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003884 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885 return( PSA_ERROR_INVALID_ARGUMENT );
3886
3887#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003888 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003889 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003890 mbedtls_rsa_context rsa;
3891 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3892
3893 status = psa_load_rsa_representation( slot, &rsa );
3894 if( status != PSA_SUCCESS )
3895 return status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003896 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003897
Steven Cooremana01795d2020-07-24 22:48:15 +02003898 if( input_length != mbedtls_rsa_get_len( &rsa ) )
3899 {
3900 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana01795d2020-07-24 22:48:15 +02003902 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003903
3904#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003905 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003906 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003907 ret = mbedtls_rsa_pkcs1_decrypt( &rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003908 mbedtls_ctr_drbg_random,
3909 &global_data.ctr_drbg,
3910 MBEDTLS_RSA_PRIVATE,
3911 output_length,
3912 input,
3913 output,
3914 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003915 }
3916 else
3917#endif /* MBEDTLS_PKCS1_V15 */
3918#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003919 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003920 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003921 psa_rsa_oaep_set_padding_mode( alg, &rsa );
3922 ret = mbedtls_rsa_rsaes_oaep_decrypt( &rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003923 mbedtls_ctr_drbg_random,
3924 &global_data.ctr_drbg,
3925 MBEDTLS_RSA_PRIVATE,
3926 salt, salt_length,
3927 output_length,
3928 input,
3929 output,
3930 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931 }
3932 else
3933#endif /* MBEDTLS_PKCS1_V21 */
3934 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003935 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003936 return( PSA_ERROR_INVALID_ARGUMENT );
3937 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003938
Steven Cooremana01795d2020-07-24 22:48:15 +02003939 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003940 return( mbedtls_to_psa_error( ret ) );
3941 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003942 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003943#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944 {
3945 return( PSA_ERROR_NOT_SUPPORTED );
3946 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003947}
Gilles Peskine20035e32018-02-03 22:44:14 +01003948
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003949
3950
mohammad1603503973b2018-03-12 15:59:30 +02003951/****************************************************************/
3952/* Symmetric cryptography */
3953/****************************************************************/
3954
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003955/* Initialize the cipher operation structure. Once this function has been
3956 * called, psa_cipher_abort can run and will do the right thing. */
3957static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3958 psa_algorithm_t alg )
3959{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003960 if( ! PSA_ALG_IS_CIPHER( alg ) )
3961 {
3962 memset( operation, 0, sizeof( *operation ) );
3963 return( PSA_ERROR_INVALID_ARGUMENT );
3964 }
3965
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003966 operation->alg = alg;
3967 operation->key_set = 0;
3968 operation->iv_set = 0;
3969 operation->iv_required = 1;
3970 operation->iv_size = 0;
3971 operation->block_size = 0;
3972 mbedtls_cipher_init( &operation->ctx.cipher );
3973 return( PSA_SUCCESS );
3974}
3975
Gilles Peskinee553c652018-06-04 16:22:46 +02003976static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003977 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003978 psa_algorithm_t alg,
3979 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003980{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003981 int ret = 0;
3982 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003983 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003984 size_t key_bits;
3985 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003986 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3987 PSA_KEY_USAGE_ENCRYPT :
3988 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003989
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003990 /* A context must be freshly initialized before it can be set up. */
3991 if( operation->alg != 0 )
3992 {
3993 return( PSA_ERROR_BAD_STATE );
3994 }
3995
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003996 status = psa_cipher_init( operation, alg );
3997 if( status != PSA_SUCCESS )
3998 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003999
Gilles Peskine28f8f302019-07-24 13:30:31 +02004000 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004001 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004002 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004003 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004004
Gilles Peskine8e338702019-07-30 20:06:31 +02004005 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004006 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004007 {
4008 status = PSA_ERROR_NOT_SUPPORTED;
4009 goto exit;
4010 }
mohammad1603503973b2018-03-12 15:59:30 +02004011
mohammad1603503973b2018-03-12 15:59:30 +02004012 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004013 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004014 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004015
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004016#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004017 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004018 {
4019 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004020 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004021 memcpy( keys, slot->data.key.data, 16 );
4022 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004023 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4024 keys,
4025 192, cipher_operation );
4026 }
4027 else
4028#endif
4029 {
4030 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004031 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004032 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004033 }
Moran Peker41deec42018-04-04 15:43:05 +03004034 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004035 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004036
mohammad16038481e742018-03-18 13:57:31 +02004037#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004038 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004039 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004040 case PSA_ALG_CBC_NO_PADDING:
4041 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4042 MBEDTLS_PADDING_NONE );
4043 break;
4044 case PSA_ALG_CBC_PKCS7:
4045 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4046 MBEDTLS_PADDING_PKCS7 );
4047 break;
4048 default:
4049 /* The algorithm doesn't involve padding. */
4050 ret = 0;
4051 break;
4052 }
4053 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004054 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004055#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4056
mohammad1603503973b2018-03-12 15:59:30 +02004057 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004058 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004059 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004060 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004061 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004062 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004063 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004064#if defined(MBEDTLS_CHACHA20_C)
4065 else
4066 if( alg == PSA_ALG_CHACHA20 )
4067 operation->iv_size = 12;
4068#endif
mohammad1603503973b2018-03-12 15:59:30 +02004069
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004070exit:
4071 if( status == 0 )
4072 status = mbedtls_to_psa_error( ret );
4073 if( status != 0 )
4074 psa_cipher_abort( operation );
4075 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004076}
4077
Gilles Peskinefe119512018-07-08 21:39:34 +02004078psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004079 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004080 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004081{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004082 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004083}
4084
Gilles Peskinefe119512018-07-08 21:39:34 +02004085psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004086 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004087 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004088{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004089 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004090}
4091
Gilles Peskinefe119512018-07-08 21:39:34 +02004092psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004093 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004094 size_t iv_size,
4095 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004096{
itayzafrir534bd7c2018-08-02 13:56:32 +03004097 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004099 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004100 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004101 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004102 }
Moran Peker41deec42018-04-04 15:43:05 +03004103 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004104 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004105 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004106 goto exit;
4107 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004108 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4109 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004110 if( ret != 0 )
4111 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004112 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004113 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004114 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004115
mohammad16038481e742018-03-18 13:57:31 +02004116 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004117 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004118
Moran Peker395db872018-05-31 14:07:14 +03004119exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004120 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004121 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004122 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004123}
4124
Gilles Peskinefe119512018-07-08 21:39:34 +02004125psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004126 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004127 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004128{
itayzafrir534bd7c2018-08-02 13:56:32 +03004129 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004131 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004132 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004133 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004134 }
Moran Pekera28258c2018-05-29 16:25:04 +03004135 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004136 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004137 status = PSA_ERROR_INVALID_ARGUMENT;
4138 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004139 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004140 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4141 status = mbedtls_to_psa_error( ret );
4142exit:
4143 if( status == PSA_SUCCESS )
4144 operation->iv_set = 1;
4145 else
mohammad1603503973b2018-03-12 15:59:30 +02004146 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004147 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004148}
4149
Gilles Peskinee553c652018-06-04 16:22:46 +02004150psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4151 const uint8_t *input,
4152 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004153 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004154 size_t output_size,
4155 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004156{
itayzafrir534bd7c2018-08-02 13:56:32 +03004157 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004159 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004160
4161 if( operation->alg == 0 )
4162 {
4163 return( PSA_ERROR_BAD_STATE );
4164 }
4165
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004166 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004167 {
4168 /* Take the unprocessed partial block left over from previous
4169 * update calls, if any, plus the input to this call. Remove
4170 * the last partial block, if any. You get the data that will be
4171 * output in this call. */
4172 expected_output_size =
4173 ( operation->ctx.cipher.unprocessed_len + input_length )
4174 / operation->block_size * operation->block_size;
4175 }
4176 else
4177 {
4178 expected_output_size = input_length;
4179 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004180
Gilles Peskine89d789c2018-06-04 17:17:16 +02004181 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004182 {
4183 status = PSA_ERROR_BUFFER_TOO_SMALL;
4184 goto exit;
4185 }
mohammad160382759612018-03-12 18:16:40 +02004186
mohammad1603503973b2018-03-12 15:59:30 +02004187 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004188 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004189 status = mbedtls_to_psa_error( ret );
4190exit:
4191 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004192 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004193 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004194}
4195
Gilles Peskinee553c652018-06-04 16:22:46 +02004196psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4197 uint8_t *output,
4198 size_t output_size,
4199 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004200{
David Saadab4ecc272019-02-14 13:48:10 +02004201 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004202 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004203 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004204
mohammad1603503973b2018-03-12 15:59:30 +02004205 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004206 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004207 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004208 }
4209 if( operation->iv_required && ! operation->iv_set )
4210 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004211 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004212 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004213
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004214 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004215 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4216 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004217 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004218 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004219 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004220 }
4221
Janos Follath315b51c2018-07-09 16:04:51 +01004222 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4223 temp_output_buffer,
4224 output_length );
4225 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004226 {
Janos Follath315b51c2018-07-09 16:04:51 +01004227 status = mbedtls_to_psa_error( cipher_ret );
4228 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004229 }
Janos Follath315b51c2018-07-09 16:04:51 +01004230
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004231 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004232 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004233 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004234 memcpy( output, temp_output_buffer, *output_length );
4235 else
4236 {
Janos Follath315b51c2018-07-09 16:04:51 +01004237 status = PSA_ERROR_BUFFER_TOO_SMALL;
4238 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004239 }
mohammad1603503973b2018-03-12 15:59:30 +02004240
Gilles Peskine3f108122018-12-07 18:14:53 +01004241 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004242 status = psa_cipher_abort( operation );
4243
4244 return( status );
4245
4246error:
4247
4248 *output_length = 0;
4249
Gilles Peskine3f108122018-12-07 18:14:53 +01004250 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004251 (void) psa_cipher_abort( operation );
4252
4253 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004254}
4255
Gilles Peskinee553c652018-06-04 16:22:46 +02004256psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4257{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004258 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004259 {
4260 /* The object has (apparently) been initialized but it is not
4261 * in use. It's ok to call abort on such an object, and there's
4262 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004263 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004264 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004265
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004266 /* Sanity check (shouldn't happen: operation->alg should
4267 * always have been initialized to a valid value). */
4268 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4269 return( PSA_ERROR_BAD_STATE );
4270
mohammad1603503973b2018-03-12 15:59:30 +02004271 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004272
Moran Peker41deec42018-04-04 15:43:05 +03004273 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004274 operation->key_set = 0;
4275 operation->iv_set = 0;
4276 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004277 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004278 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004279
Moran Peker395db872018-05-31 14:07:14 +03004280 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004281}
4282
Gilles Peskinea0655c32018-04-30 17:06:50 +02004283
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004284
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004285
mohammad16035955c982018-04-26 00:53:03 +03004286/****************************************************************/
4287/* AEAD */
4288/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004289
Gilles Peskineedf9a652018-08-17 18:11:56 +02004290typedef struct
4291{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004292 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004293 const mbedtls_cipher_info_t *cipher_info;
4294 union
4295 {
4296#if defined(MBEDTLS_CCM_C)
4297 mbedtls_ccm_context ccm;
4298#endif /* MBEDTLS_CCM_C */
4299#if defined(MBEDTLS_GCM_C)
4300 mbedtls_gcm_context gcm;
4301#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004302#if defined(MBEDTLS_CHACHAPOLY_C)
4303 mbedtls_chachapoly_context chachapoly;
4304#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004305 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004306 psa_algorithm_t core_alg;
4307 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004308 uint8_t tag_length;
4309} aead_operation_t;
4310
Gilles Peskine30a9e412019-01-14 18:36:12 +01004311static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004312{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004313 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004314 {
4315#if defined(MBEDTLS_CCM_C)
4316 case PSA_ALG_CCM:
4317 mbedtls_ccm_free( &operation->ctx.ccm );
4318 break;
4319#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004320#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004321 case PSA_ALG_GCM:
4322 mbedtls_gcm_free( &operation->ctx.gcm );
4323 break;
4324#endif /* MBEDTLS_GCM_C */
4325 }
4326}
4327
4328static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004329 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004330 psa_key_usage_t usage,
4331 psa_algorithm_t alg )
4332{
4333 psa_status_t status;
4334 size_t key_bits;
4335 mbedtls_cipher_id_t cipher_id;
4336
Gilles Peskine28f8f302019-07-24 13:30:31 +02004337 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004338 if( status != PSA_SUCCESS )
4339 return( status );
4340
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004341 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004342
4343 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004344 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004345 &cipher_id );
4346 if( operation->cipher_info == NULL )
4347 return( PSA_ERROR_NOT_SUPPORTED );
4348
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004349 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004350 {
4351#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004352 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4353 operation->core_alg = PSA_ALG_CCM;
4354 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004355 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4356 * The call to mbedtls_ccm_encrypt_and_tag or
4357 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004358 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004359 return( PSA_ERROR_INVALID_ARGUMENT );
4360 mbedtls_ccm_init( &operation->ctx.ccm );
4361 status = mbedtls_to_psa_error(
4362 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004363 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004364 (unsigned int) key_bits ) );
4365 if( status != 0 )
4366 goto cleanup;
4367 break;
4368#endif /* MBEDTLS_CCM_C */
4369
4370#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004371 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4372 operation->core_alg = PSA_ALG_GCM;
4373 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004374 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4375 * The call to mbedtls_gcm_crypt_and_tag or
4376 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004377 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004378 return( PSA_ERROR_INVALID_ARGUMENT );
4379 mbedtls_gcm_init( &operation->ctx.gcm );
4380 status = mbedtls_to_psa_error(
4381 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004382 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004383 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004384 if( status != 0 )
4385 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004386 break;
4387#endif /* MBEDTLS_GCM_C */
4388
Gilles Peskine26869f22019-05-06 15:25:00 +02004389#if defined(MBEDTLS_CHACHAPOLY_C)
4390 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4391 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4392 operation->full_tag_length = 16;
4393 /* We only support the default tag length. */
4394 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4395 return( PSA_ERROR_NOT_SUPPORTED );
4396 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4397 status = mbedtls_to_psa_error(
4398 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004399 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004400 if( status != 0 )
4401 goto cleanup;
4402 break;
4403#endif /* MBEDTLS_CHACHAPOLY_C */
4404
Gilles Peskineedf9a652018-08-17 18:11:56 +02004405 default:
4406 return( PSA_ERROR_NOT_SUPPORTED );
4407 }
4408
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004409 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4410 {
4411 status = PSA_ERROR_INVALID_ARGUMENT;
4412 goto cleanup;
4413 }
4414 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004415
Gilles Peskineedf9a652018-08-17 18:11:56 +02004416 return( PSA_SUCCESS );
4417
4418cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004419 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004420 return( status );
4421}
4422
Gilles Peskinec5487a82018-12-03 18:08:14 +01004423psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004424 psa_algorithm_t alg,
4425 const uint8_t *nonce,
4426 size_t nonce_length,
4427 const uint8_t *additional_data,
4428 size_t additional_data_length,
4429 const uint8_t *plaintext,
4430 size_t plaintext_length,
4431 uint8_t *ciphertext,
4432 size_t ciphertext_size,
4433 size_t *ciphertext_length )
4434{
mohammad16035955c982018-04-26 00:53:03 +03004435 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004436 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004437 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004438
mohammad1603f08a5502018-06-03 15:05:47 +03004439 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004440
Gilles Peskinec5487a82018-12-03 18:08:14 +01004441 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004442 if( status != PSA_SUCCESS )
4443 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004444
Gilles Peskineedf9a652018-08-17 18:11:56 +02004445 /* For all currently supported modes, the tag is at the end of the
4446 * ciphertext. */
4447 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4448 {
4449 status = PSA_ERROR_BUFFER_TOO_SMALL;
4450 goto exit;
4451 }
4452 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004453
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004454#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004455 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004456 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004457 status = mbedtls_to_psa_error(
4458 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4459 MBEDTLS_GCM_ENCRYPT,
4460 plaintext_length,
4461 nonce, nonce_length,
4462 additional_data, additional_data_length,
4463 plaintext, ciphertext,
4464 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004465 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004466 else
4467#endif /* MBEDTLS_GCM_C */
4468#if defined(MBEDTLS_CCM_C)
4469 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004470 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004471 status = mbedtls_to_psa_error(
4472 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4473 plaintext_length,
4474 nonce, nonce_length,
4475 additional_data,
4476 additional_data_length,
4477 plaintext, ciphertext,
4478 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004479 }
mohammad16035c8845f2018-05-09 05:40:09 -07004480 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004481#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004482#if defined(MBEDTLS_CHACHAPOLY_C)
4483 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4484 {
4485 if( nonce_length != 12 || operation.tag_length != 16 )
4486 {
4487 status = PSA_ERROR_NOT_SUPPORTED;
4488 goto exit;
4489 }
4490 status = mbedtls_to_psa_error(
4491 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4492 plaintext_length,
4493 nonce,
4494 additional_data,
4495 additional_data_length,
4496 plaintext,
4497 ciphertext,
4498 tag ) );
4499 }
4500 else
4501#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004502 {
mohammad1603554faad2018-06-03 15:07:38 +03004503 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004504 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004505
Gilles Peskineedf9a652018-08-17 18:11:56 +02004506 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4507 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004508
Gilles Peskineedf9a652018-08-17 18:11:56 +02004509exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004510 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004511 if( status == PSA_SUCCESS )
4512 *ciphertext_length = plaintext_length + operation.tag_length;
4513 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004514}
4515
Gilles Peskineee652a32018-06-01 19:23:52 +02004516/* Locate the tag in a ciphertext buffer containing the encrypted data
4517 * followed by the tag. Return the length of the part preceding the tag in
4518 * *plaintext_length. This is the size of the plaintext in modes where
4519 * the encrypted data has the same size as the plaintext, such as
4520 * CCM and GCM. */
4521static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4522 const uint8_t *ciphertext,
4523 size_t ciphertext_length,
4524 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004525 const uint8_t **p_tag )
4526{
4527 size_t payload_length;
4528 if( tag_length > ciphertext_length )
4529 return( PSA_ERROR_INVALID_ARGUMENT );
4530 payload_length = ciphertext_length - tag_length;
4531 if( payload_length > plaintext_size )
4532 return( PSA_ERROR_BUFFER_TOO_SMALL );
4533 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004534 return( PSA_SUCCESS );
4535}
4536
Gilles Peskinec5487a82018-12-03 18:08:14 +01004537psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004538 psa_algorithm_t alg,
4539 const uint8_t *nonce,
4540 size_t nonce_length,
4541 const uint8_t *additional_data,
4542 size_t additional_data_length,
4543 const uint8_t *ciphertext,
4544 size_t ciphertext_length,
4545 uint8_t *plaintext,
4546 size_t plaintext_size,
4547 size_t *plaintext_length )
4548{
mohammad16035955c982018-04-26 00:53:03 +03004549 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004550 aead_operation_t operation;
4551 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004552
Gilles Peskineee652a32018-06-01 19:23:52 +02004553 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004554
Gilles Peskinec5487a82018-12-03 18:08:14 +01004555 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004556 if( status != PSA_SUCCESS )
4557 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004558
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004559 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4560 ciphertext, ciphertext_length,
4561 plaintext_size, &tag );
4562 if( status != PSA_SUCCESS )
4563 goto exit;
4564
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004565#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004566 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004567 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004568 status = mbedtls_to_psa_error(
4569 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4570 ciphertext_length - operation.tag_length,
4571 nonce, nonce_length,
4572 additional_data,
4573 additional_data_length,
4574 tag, operation.tag_length,
4575 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004576 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004577 else
4578#endif /* MBEDTLS_GCM_C */
4579#if defined(MBEDTLS_CCM_C)
4580 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004581 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004582 status = mbedtls_to_psa_error(
4583 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4584 ciphertext_length - operation.tag_length,
4585 nonce, nonce_length,
4586 additional_data,
4587 additional_data_length,
4588 ciphertext, plaintext,
4589 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004590 }
mohammad160339574652018-06-01 04:39:53 -07004591 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004592#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004593#if defined(MBEDTLS_CHACHAPOLY_C)
4594 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4595 {
4596 if( nonce_length != 12 || operation.tag_length != 16 )
4597 {
4598 status = PSA_ERROR_NOT_SUPPORTED;
4599 goto exit;
4600 }
4601 status = mbedtls_to_psa_error(
4602 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4603 ciphertext_length - operation.tag_length,
4604 nonce,
4605 additional_data,
4606 additional_data_length,
4607 tag,
4608 ciphertext,
4609 plaintext ) );
4610 }
4611 else
4612#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004613 {
mohammad1603554faad2018-06-03 15:07:38 +03004614 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004615 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004616
Gilles Peskineedf9a652018-08-17 18:11:56 +02004617 if( status != PSA_SUCCESS && plaintext_size != 0 )
4618 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004619
Gilles Peskineedf9a652018-08-17 18:11:56 +02004620exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004621 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004622 if( status == PSA_SUCCESS )
4623 *plaintext_length = ciphertext_length - operation.tag_length;
4624 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004625}
4626
Gilles Peskinea0655c32018-04-30 17:06:50 +02004627
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004628
Gilles Peskine20035e32018-02-03 22:44:14 +01004629/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004630/* Generators */
4631/****************************************************************/
4632
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004633#define HKDF_STATE_INIT 0 /* no input yet */
4634#define HKDF_STATE_STARTED 1 /* got salt */
4635#define HKDF_STATE_KEYED 2 /* got key */
4636#define HKDF_STATE_OUTPUT 3 /* output started */
4637
Gilles Peskinecbe66502019-05-16 16:59:18 +02004638static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004639 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004640{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004641 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4642 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004643 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004644 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004645}
4646
4647
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004649{
4650 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004651 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004652 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004653 {
4654 /* The object has (apparently) been initialized but it is not
4655 * in use. It's ok to call abort on such an object, and there's
4656 * nothing to do. */
4657 }
4658 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004659#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004660 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004661 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 mbedtls_free( operation->ctx.hkdf.info );
4663 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004664 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004665 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004666 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004667 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004668 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004669 if( operation->ctx.tls12_prf.seed != NULL )
4670 {
4671 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4672 operation->ctx.tls12_prf.seed_length );
4673 mbedtls_free( operation->ctx.tls12_prf.seed );
4674 }
4675
4676 if( operation->ctx.tls12_prf.label != NULL )
4677 {
4678 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4679 operation->ctx.tls12_prf.label_length );
4680 mbedtls_free( operation->ctx.tls12_prf.label );
4681 }
4682
4683 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4684
4685 /* We leave the fields Ai and output_block to be erased safely by the
4686 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004687 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004688 else
4689#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004690 {
4691 status = PSA_ERROR_BAD_STATE;
4692 }
Janos Follath083036a2019-06-11 10:22:26 +01004693 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004694 return( status );
4695}
4696
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004697psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004698 size_t *capacity)
4699{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004700 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004701 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004702 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004703 return PSA_ERROR_BAD_STATE;
4704 }
4705
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004706 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004707 return( PSA_SUCCESS );
4708}
4709
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004710psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004711 size_t capacity )
4712{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004713 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004714 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004715 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004716 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004717 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004718 return( PSA_SUCCESS );
4719}
4720
Gilles Peskinebef7f142018-07-12 17:22:21 +02004721#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004723 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004724static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004725 psa_algorithm_t hash_alg,
4726 uint8_t *output,
4727 size_t output_length )
4728{
4729 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4730 psa_status_t status;
4731
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004732 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4733 return( PSA_ERROR_BAD_STATE );
4734 hkdf->state = HKDF_STATE_OUTPUT;
4735
Gilles Peskinebef7f142018-07-12 17:22:21 +02004736 while( output_length != 0 )
4737 {
4738 /* Copy what remains of the current block */
4739 uint8_t n = hash_length - hkdf->offset_in_block;
4740 if( n > output_length )
4741 n = (uint8_t) output_length;
4742 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4743 output += n;
4744 output_length -= n;
4745 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004746 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004747 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004748 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004749 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004751 * object was corrupted or if this function is called directly
4752 * inside the library. */
4753 if( hkdf->block_number == 0xff )
4754 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004755
4756 /* We need a new block */
4757 ++hkdf->block_number;
4758 hkdf->offset_in_block = 0;
4759 status = psa_hmac_setup_internal( &hkdf->hmac,
4760 hkdf->prk, hash_length,
4761 hash_alg );
4762 if( status != PSA_SUCCESS )
4763 return( status );
4764 if( hkdf->block_number != 1 )
4765 {
4766 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4767 hkdf->output_block,
4768 hash_length );
4769 if( status != PSA_SUCCESS )
4770 return( status );
4771 }
4772 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4773 hkdf->info,
4774 hkdf->info_length );
4775 if( status != PSA_SUCCESS )
4776 return( status );
4777 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4778 &hkdf->block_number, 1 );
4779 if( status != PSA_SUCCESS )
4780 return( status );
4781 status = psa_hmac_finish_internal( &hkdf->hmac,
4782 hkdf->output_block,
4783 sizeof( hkdf->output_block ) );
4784 if( status != PSA_SUCCESS )
4785 return( status );
4786 }
4787
4788 return( PSA_SUCCESS );
4789}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004790
Janos Follath7742fee2019-06-17 12:58:10 +01004791static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4792 psa_tls12_prf_key_derivation_t *tls12_prf,
4793 psa_algorithm_t alg )
4794{
4795 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4796 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004797 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4798 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004799
Janos Follath7742fee2019-06-17 12:58:10 +01004800 /* We can't be wanting more output after block 0xff, otherwise
4801 * the capacity check in psa_key_derivation_output_bytes() would have
4802 * prevented this call. It could happen only if the operation
4803 * object was corrupted or if this function is called directly
4804 * inside the library. */
4805 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004806 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004807
4808 /* We need a new block */
4809 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004810 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004811
4812 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4813 *
4814 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4815 *
4816 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4817 * HMAC_hash(secret, A(2) + seed) +
4818 * HMAC_hash(secret, A(3) + seed) + ...
4819 *
4820 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004821 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004822 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004823 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004824 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004825 * is currently extracted as `output_block` and where i is
4826 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004827 */
4828
Janos Follathea29bfb2019-06-19 12:21:20 +01004829 /* Save the hash context before using it, to preserve the hash state with
4830 * only the inner padding in it. We need this, because inner padding depends
4831 * on the key (secret in the RFC's terminology). */
4832 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4833 if( status != PSA_SUCCESS )
4834 goto cleanup;
4835
4836 /* Calculate A(i) where i = tls12_prf->block_number. */
4837 if( tls12_prf->block_number == 1 )
4838 {
4839 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4840 * the variable seed and in this instance means it in the context of the
4841 * P_hash function, where seed = label + seed.) */
4842 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4843 tls12_prf->label, tls12_prf->label_length );
4844 if( status != PSA_SUCCESS )
4845 goto cleanup;
4846 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4847 tls12_prf->seed, tls12_prf->seed_length );
4848 if( status != PSA_SUCCESS )
4849 goto cleanup;
4850 }
4851 else
4852 {
4853 /* A(i) = HMAC_hash(secret, A(i-1)) */
4854 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4855 tls12_prf->Ai, hash_length );
4856 if( status != PSA_SUCCESS )
4857 goto cleanup;
4858 }
4859
4860 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4861 tls12_prf->Ai, hash_length );
4862 if( status != PSA_SUCCESS )
4863 goto cleanup;
4864 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4865 if( status != PSA_SUCCESS )
4866 goto cleanup;
4867
4868 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4869 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4870 tls12_prf->Ai, hash_length );
4871 if( status != PSA_SUCCESS )
4872 goto cleanup;
4873 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4874 tls12_prf->label, tls12_prf->label_length );
4875 if( status != PSA_SUCCESS )
4876 goto cleanup;
4877 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4878 tls12_prf->seed, tls12_prf->seed_length );
4879 if( status != PSA_SUCCESS )
4880 goto cleanup;
4881 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4882 tls12_prf->output_block, hash_length );
4883 if( status != PSA_SUCCESS )
4884 goto cleanup;
4885 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4886 if( status != PSA_SUCCESS )
4887 goto cleanup;
4888
Janos Follath7742fee2019-06-17 12:58:10 +01004889
4890cleanup:
4891
Janos Follathea29bfb2019-06-19 12:21:20 +01004892 cleanup_status = psa_hash_abort( &backup );
4893 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4894 status = cleanup_status;
4895
4896 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004897}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004898
Janos Follath844eb0e2019-06-19 12:10:49 +01004899static psa_status_t psa_key_derivation_tls12_prf_read(
4900 psa_tls12_prf_key_derivation_t *tls12_prf,
4901 psa_algorithm_t alg,
4902 uint8_t *output,
4903 size_t output_length )
4904{
4905 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4906 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4907 psa_status_t status;
4908 uint8_t offset, length;
4909
4910 while( output_length != 0 )
4911 {
4912 /* Check if we have fully processed the current block. */
4913 if( tls12_prf->left_in_block == 0 )
4914 {
4915 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4916 alg );
4917 if( status != PSA_SUCCESS )
4918 return( status );
4919
4920 continue;
4921 }
4922
4923 if( tls12_prf->left_in_block > output_length )
4924 length = (uint8_t) output_length;
4925 else
4926 length = tls12_prf->left_in_block;
4927
4928 offset = hash_length - tls12_prf->left_in_block;
4929 memcpy( output, tls12_prf->output_block + offset, length );
4930 output += length;
4931 output_length -= length;
4932 tls12_prf->left_in_block -= length;
4933 }
4934
4935 return( PSA_SUCCESS );
4936}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004937#endif /* MBEDTLS_MD_C */
4938
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004939psa_status_t psa_key_derivation_output_bytes(
4940 psa_key_derivation_operation_t *operation,
4941 uint8_t *output,
4942 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004943{
4944 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004946
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004947 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004948 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004950 return PSA_ERROR_BAD_STATE;
4951 }
4952
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004954 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004955 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004956 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004957 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004958 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004959 goto exit;
4960 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004962 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004964 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004965 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4966 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004968 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004969 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004970 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004972
Gilles Peskinebef7f142018-07-12 17:22:21 +02004973#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004974 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004975 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004976 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004978 output, output_length );
4979 }
Janos Follathadbec812019-06-14 11:05:39 +01004980 else
Janos Follathadbec812019-06-14 11:05:39 +01004981 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004982 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004983 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004985 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004986 output_length );
4987 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004988 else
4989#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004990 {
4991 return( PSA_ERROR_BAD_STATE );
4992 }
4993
4994exit:
4995 if( status != PSA_SUCCESS )
4996 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004997 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004998 * This allows us to differentiate between exhausted operations and
4999 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5000 * operations. */
5001 psa_algorithm_t alg = operation->alg;
5002 psa_key_derivation_abort( operation );
5003 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005004 memset( output, '!', output_length );
5005 }
5006 return( status );
5007}
5008
Gilles Peskine08542d82018-07-19 17:05:42 +02005009#if defined(MBEDTLS_DES_C)
5010static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5011{
5012 if( data_size >= 8 )
5013 mbedtls_des_key_set_parity( data );
5014 if( data_size >= 16 )
5015 mbedtls_des_key_set_parity( data + 8 );
5016 if( data_size >= 24 )
5017 mbedtls_des_key_set_parity( data + 16 );
5018}
5019#endif /* MBEDTLS_DES_C */
5020
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005021static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005022 psa_key_slot_t *slot,
5023 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005025{
5026 uint8_t *data = NULL;
5027 size_t bytes = PSA_BITS_TO_BYTES( bits );
5028 psa_status_t status;
5029
Gilles Peskine8e338702019-07-30 20:06:31 +02005030 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005031 return( PSA_ERROR_INVALID_ARGUMENT );
5032 if( bits % 8 != 0 )
5033 return( PSA_ERROR_INVALID_ARGUMENT );
5034 data = mbedtls_calloc( 1, bytes );
5035 if( data == NULL )
5036 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5037
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005038 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005039 if( status != PSA_SUCCESS )
5040 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005041#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005042 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005043 psa_des_set_key_parity( data, bytes );
5044#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005045 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005046
5047exit:
5048 mbedtls_free( data );
5049 return( status );
5050}
5051
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005052psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005054 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005055{
5056 psa_status_t status;
5057 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005058 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005059
5060 /* Reject any attempt to create a zero-length key so that we don't
5061 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5062 if( psa_get_key_bits( attributes ) == 0 )
5063 return( PSA_ERROR_INVALID_ARGUMENT );
5064
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005065 if( ! operation->can_output_key )
5066 return( PSA_ERROR_NOT_PERMITTED );
5067
Gilles Peskinedf179142019-07-15 22:02:14 +02005068 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5069 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005070#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5071 if( driver != NULL )
5072 {
5073 /* Deriving a key in a secure element is not implemented yet. */
5074 status = PSA_ERROR_NOT_SUPPORTED;
5075 }
5076#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005077 if( status == PSA_SUCCESS )
5078 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005079 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005080 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005082 }
5083 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005084 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005085 if( status != PSA_SUCCESS )
5086 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005087 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005088 *handle = 0;
5089 }
5090 return( status );
5091}
5092
Gilles Peskineeab56e42018-07-12 17:12:33 +02005093
5094
5095/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005096/* Key derivation */
5097/****************************************************************/
5098
Gilles Peskine969c5d62019-01-16 15:53:06 +01005099static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005100 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005101 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005102{
Janos Follath5fe19732019-06-20 15:09:30 +01005103 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5104 * initialisers for this union leave some bytes unspecified.) */
5105 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5106
Gilles Peskine969c5d62019-01-16 15:53:06 +01005107 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005108#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005109 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5110 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5111 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005113 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005114 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5115 if( hash_size == 0 )
5116 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005117 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5118 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005119 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005120 {
5121 return( PSA_ERROR_NOT_SUPPORTED );
5122 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005123 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005124 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005125 }
5126#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005127 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005128 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005129}
5130
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005131psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005132 psa_algorithm_t alg )
5133{
5134 psa_status_t status;
5135
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005136 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005137 return( PSA_ERROR_BAD_STATE );
5138
Gilles Peskine6843c292019-01-18 16:44:49 +01005139 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5140 return( PSA_ERROR_INVALID_ARGUMENT );
5141 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005142 {
5143 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005144 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005145 }
5146 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5147 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005149 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005150 else
5151 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005152
5153 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005154 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005155 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005156}
5157
5158#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005159static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005160 psa_algorithm_t hash_alg,
5161 psa_key_derivation_step_t step,
5162 const uint8_t *data,
5163 size_t data_length )
5164{
5165 psa_status_t status;
5166 switch( step )
5167 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005168 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005169 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005170 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005171 status = psa_hmac_setup_internal( &hkdf->hmac,
5172 data, data_length,
5173 hash_alg );
5174 if( status != PSA_SUCCESS )
5175 return( status );
5176 hkdf->state = HKDF_STATE_STARTED;
5177 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005178 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005179 /* If no salt was provided, use an empty salt. */
5180 if( hkdf->state == HKDF_STATE_INIT )
5181 {
5182 status = psa_hmac_setup_internal( &hkdf->hmac,
5183 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005184 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005185 if( status != PSA_SUCCESS )
5186 return( status );
5187 hkdf->state = HKDF_STATE_STARTED;
5188 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005189 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005190 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005191 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5192 data, data_length );
5193 if( status != PSA_SUCCESS )
5194 return( status );
5195 status = psa_hmac_finish_internal( &hkdf->hmac,
5196 hkdf->prk,
5197 sizeof( hkdf->prk ) );
5198 if( status != PSA_SUCCESS )
5199 return( status );
5200 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5201 hkdf->block_number = 0;
5202 hkdf->state = HKDF_STATE_KEYED;
5203 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005204 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005205 if( hkdf->state == HKDF_STATE_OUTPUT )
5206 return( PSA_ERROR_BAD_STATE );
5207 if( hkdf->info_set )
5208 return( PSA_ERROR_BAD_STATE );
5209 hkdf->info_length = data_length;
5210 if( data_length != 0 )
5211 {
5212 hkdf->info = mbedtls_calloc( 1, data_length );
5213 if( hkdf->info == NULL )
5214 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5215 memcpy( hkdf->info, data, data_length );
5216 }
5217 hkdf->info_set = 1;
5218 return( PSA_SUCCESS );
5219 default:
5220 return( PSA_ERROR_INVALID_ARGUMENT );
5221 }
5222}
Janos Follathb03233e2019-06-11 15:30:30 +01005223
Janos Follathf08e2652019-06-13 09:05:41 +01005224static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5225 const uint8_t *data,
5226 size_t data_length )
5227{
5228 if( prf->state != TLS12_PRF_STATE_INIT )
5229 return( PSA_ERROR_BAD_STATE );
5230
Janos Follathd6dce9f2019-07-04 09:11:38 +01005231 if( data_length != 0 )
5232 {
5233 prf->seed = mbedtls_calloc( 1, data_length );
5234 if( prf->seed == NULL )
5235 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005236
Janos Follathd6dce9f2019-07-04 09:11:38 +01005237 memcpy( prf->seed, data, data_length );
5238 prf->seed_length = data_length;
5239 }
Janos Follathf08e2652019-06-13 09:05:41 +01005240
5241 prf->state = TLS12_PRF_STATE_SEED_SET;
5242
5243 return( PSA_SUCCESS );
5244}
5245
Janos Follath81550542019-06-13 14:26:34 +01005246static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5247 psa_algorithm_t hash_alg,
5248 const uint8_t *data,
5249 size_t data_length )
5250{
5251 psa_status_t status;
5252 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5253 return( PSA_ERROR_BAD_STATE );
5254
5255 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5256 if( status != PSA_SUCCESS )
5257 return( status );
5258
5259 prf->state = TLS12_PRF_STATE_KEY_SET;
5260
5261 return( PSA_SUCCESS );
5262}
5263
Janos Follath6660f0e2019-06-17 08:44:03 +01005264static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5265 psa_tls12_prf_key_derivation_t *prf,
5266 psa_algorithm_t hash_alg,
5267 const uint8_t *data,
5268 size_t data_length )
5269{
5270 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005271 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5272 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005273
5274 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5275 return( PSA_ERROR_INVALID_ARGUMENT );
5276
5277 /* Quoting RFC 4279, Section 2:
5278 *
5279 * The premaster secret is formed as follows: if the PSK is N octets
5280 * long, concatenate a uint16 with the value N, N zero octets, a second
5281 * uint16 with the value N, and the PSK itself.
5282 */
5283
Janos Follath40e13932019-06-26 13:22:29 +01005284 *cur++ = ( data_length >> 8 ) & 0xff;
5285 *cur++ = ( data_length >> 0 ) & 0xff;
5286 memset( cur, 0, data_length );
5287 cur += data_length;
5288 *cur++ = pms[0];
5289 *cur++ = pms[1];
5290 memcpy( cur, data, data_length );
5291 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005292
Janos Follath40e13932019-06-26 13:22:29 +01005293 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005294
5295 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5296 return( status );
5297}
5298
Janos Follath63028dd2019-06-13 09:15:47 +01005299static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5300 const uint8_t *data,
5301 size_t data_length )
5302{
5303 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5304 return( PSA_ERROR_BAD_STATE );
5305
Janos Follathd6dce9f2019-07-04 09:11:38 +01005306 if( data_length != 0 )
5307 {
5308 prf->label = mbedtls_calloc( 1, data_length );
5309 if( prf->label == NULL )
5310 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005311
Janos Follathd6dce9f2019-07-04 09:11:38 +01005312 memcpy( prf->label, data, data_length );
5313 prf->label_length = data_length;
5314 }
Janos Follath63028dd2019-06-13 09:15:47 +01005315
5316 prf->state = TLS12_PRF_STATE_LABEL_SET;
5317
5318 return( PSA_SUCCESS );
5319}
5320
Janos Follathb03233e2019-06-11 15:30:30 +01005321static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5322 psa_algorithm_t hash_alg,
5323 psa_key_derivation_step_t step,
5324 const uint8_t *data,
5325 size_t data_length )
5326{
Janos Follathb03233e2019-06-11 15:30:30 +01005327 switch( step )
5328 {
Janos Follathf08e2652019-06-13 09:05:41 +01005329 case PSA_KEY_DERIVATION_INPUT_SEED:
5330 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005331 case PSA_KEY_DERIVATION_INPUT_SECRET:
5332 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005333 case PSA_KEY_DERIVATION_INPUT_LABEL:
5334 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005335 default:
5336 return( PSA_ERROR_INVALID_ARGUMENT );
5337 }
5338}
Janos Follath6660f0e2019-06-17 08:44:03 +01005339
5340static psa_status_t psa_tls12_prf_psk_to_ms_input(
5341 psa_tls12_prf_key_derivation_t *prf,
5342 psa_algorithm_t hash_alg,
5343 psa_key_derivation_step_t step,
5344 const uint8_t *data,
5345 size_t data_length )
5346{
5347 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005348 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005349 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5350 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005351 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005352
5353 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5354}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005355#endif /* MBEDTLS_MD_C */
5356
Gilles Peskineb8965192019-09-24 16:21:10 +02005357/** Check whether the given key type is acceptable for the given
5358 * input step of a key derivation.
5359 *
5360 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5361 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5362 * Both secret and non-secret inputs can alternatively have the type
5363 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5364 * that the input was passed as a buffer rather than via a key object.
5365 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005366static int psa_key_derivation_check_input_type(
5367 psa_key_derivation_step_t step,
5368 psa_key_type_t key_type )
5369{
5370 switch( step )
5371 {
5372 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005373 if( key_type == PSA_KEY_TYPE_DERIVE )
5374 return( PSA_SUCCESS );
5375 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005376 return( PSA_SUCCESS );
5377 break;
5378 case PSA_KEY_DERIVATION_INPUT_LABEL:
5379 case PSA_KEY_DERIVATION_INPUT_SALT:
5380 case PSA_KEY_DERIVATION_INPUT_INFO:
5381 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005382 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5383 return( PSA_SUCCESS );
5384 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005385 return( PSA_SUCCESS );
5386 break;
5387 }
5388 return( PSA_ERROR_INVALID_ARGUMENT );
5389}
5390
Janos Follathb80a94e2019-06-12 15:54:46 +01005391static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005392 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005393 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005394 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005395 const uint8_t *data,
5396 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005397{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005398 psa_status_t status;
5399 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5400
5401 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005402 if( status != PSA_SUCCESS )
5403 goto exit;
5404
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005405#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005406 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005407 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005408 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005409 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005410 step, data, data_length );
5411 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005412 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005413 {
Janos Follathb03233e2019-06-11 15:30:30 +01005414 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5415 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5416 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005417 }
5418 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5419 {
5420 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5421 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5422 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005423 }
5424 else
5425#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005426 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005427 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005428 return( PSA_ERROR_BAD_STATE );
5429 }
5430
Gilles Peskine224b0d62019-09-23 18:13:17 +02005431exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005432 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005434 return( status );
5435}
5436
Janos Follath51f4a0f2019-06-14 11:35:55 +01005437psa_status_t psa_key_derivation_input_bytes(
5438 psa_key_derivation_operation_t *operation,
5439 psa_key_derivation_step_t step,
5440 const uint8_t *data,
5441 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005442{
Gilles Peskineb8965192019-09-24 16:21:10 +02005443 return( psa_key_derivation_input_internal( operation, step,
5444 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005445 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005446}
5447
Janos Follath51f4a0f2019-06-14 11:35:55 +01005448psa_status_t psa_key_derivation_input_key(
5449 psa_key_derivation_operation_t *operation,
5450 psa_key_derivation_step_t step,
5451 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005452{
5453 psa_key_slot_t *slot;
5454 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005455
Gilles Peskine28f8f302019-07-24 13:30:31 +02005456 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005457 PSA_KEY_USAGE_DERIVE,
5458 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005459 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005460 {
5461 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005462 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005463 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005464
5465 /* Passing a key object as a SECRET input unlocks the permission
5466 * to output to a key object. */
5467 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5468 operation->can_output_key = 1;
5469
Janos Follathb80a94e2019-06-12 15:54:46 +01005470 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005471 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005472 slot->data.key.data,
5473 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005474}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005475
5476
5477
5478/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005479/* Key agreement */
5480/****************************************************************/
5481
Gilles Peskinea05219c2018-11-16 16:02:56 +01005482#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005483static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5484 size_t peer_key_length,
5485 const mbedtls_ecp_keypair *our_key,
5486 uint8_t *shared_secret,
5487 size_t shared_secret_size,
5488 size_t *shared_secret_length )
5489{
Steven Cooremanacda8342020-07-24 23:09:52 +02005490 mbedtls_ecp_keypair their_key;
5491 psa_key_slot_t their_key_slot;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005492 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005493 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005494 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005495 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005496 mbedtls_ecdh_init( &ecdh );
Steven Cooremanacda8342020-07-24 23:09:52 +02005497 memset(&their_key_slot, 0, sizeof(their_key_slot));
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005498
Steven Cooremanacda8342020-07-24 23:09:52 +02005499 /* Creating ephemeral key slot for import purposes */
5500 their_key_slot.attr.type = PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve);
5501 their_key_slot.data.key.data = (uint8_t*) peer_key;
5502 their_key_slot.data.key.bytes = peer_key_length;
5503
5504 status = psa_load_ecp_representation( &their_key_slot, &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005505 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005506 goto exit;
5507
Jaeden Amero97271b32019-01-10 19:38:51 +00005508 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02005509 mbedtls_ecdh_get_params( &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005510 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005511 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005512 status = mbedtls_to_psa_error(
5513 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5514 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005515 goto exit;
5516
Jaeden Amero97271b32019-01-10 19:38:51 +00005517 status = mbedtls_to_psa_error(
5518 mbedtls_ecdh_calc_secret( &ecdh,
5519 shared_secret_length,
5520 shared_secret, shared_secret_size,
5521 mbedtls_ctr_drbg_random,
5522 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005523 if( status != PSA_SUCCESS )
5524 goto exit;
5525 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5526 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005527
5528exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005529 if( status != PSA_SUCCESS )
5530 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005531 mbedtls_ecdh_free( &ecdh );
Steven Cooremanacda8342020-07-24 23:09:52 +02005532 mbedtls_ecp_keypair_free( &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005533 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005534}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005535#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005536
Gilles Peskine01d718c2018-09-18 12:01:02 +02005537#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5538
Gilles Peskine0216fe12019-04-11 21:23:21 +02005539static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5540 psa_key_slot_t *private_key,
5541 const uint8_t *peer_key,
5542 size_t peer_key_length,
5543 uint8_t *shared_secret,
5544 size_t shared_secret_size,
5545 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005546{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005547 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005548 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005549#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005550 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005551 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005552 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005553 mbedtls_ecp_keypair ecp;
5554 psa_status_t status = psa_load_ecp_representation( private_key, &ecp );
5555 if( status != PSA_SUCCESS )
5556 return status;
5557 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
5558 &ecp,
5559 shared_secret, shared_secret_size,
5560 shared_secret_length );
5561 mbedtls_ecp_keypair_free( &ecp );
5562 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005563#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005564 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005565 (void) private_key;
5566 (void) peer_key;
5567 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005568 (void) shared_secret;
5569 (void) shared_secret_size;
5570 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005571 return( PSA_ERROR_NOT_SUPPORTED );
5572 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005573}
5574
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005575/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005576 * to potentially free embedded data structures and wipe confidential data.
5577 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005578static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005579 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005580 psa_key_slot_t *private_key,
5581 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005582 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005583{
5584 psa_status_t status;
5585 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5586 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005587 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005588
5589 /* Step 1: run the secret agreement algorithm to generate the shared
5590 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005591 status = psa_key_agreement_raw_internal( ka_alg,
5592 private_key,
5593 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005594 shared_secret,
5595 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005596 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005597 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005598 goto exit;
5599
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005600 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005601 * the shared secret. A shared secret is permitted wherever a key
5602 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005603 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005604 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005605 shared_secret,
5606 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005607
Gilles Peskine12313cd2018-06-20 00:20:32 +02005608exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005609 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005610 return( status );
5611}
5612
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005613psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005614 psa_key_derivation_step_t step,
5615 psa_key_handle_t private_key,
5616 const uint8_t *peer_key,
5617 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005618{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005619 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005620 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005621 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005622 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005623 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005624 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005625 if( status != PSA_SUCCESS )
5626 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005627 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005628 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005629 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005630 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005631 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005632 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005633}
5634
Gilles Peskinebe697d82019-05-16 18:00:41 +02005635psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5636 psa_key_handle_t private_key,
5637 const uint8_t *peer_key,
5638 size_t peer_key_length,
5639 uint8_t *output,
5640 size_t output_size,
5641 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005642{
5643 psa_key_slot_t *slot;
5644 psa_status_t status;
5645
5646 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5647 {
5648 status = PSA_ERROR_INVALID_ARGUMENT;
5649 goto exit;
5650 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005651 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005652 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005653 if( status != PSA_SUCCESS )
5654 goto exit;
5655
5656 status = psa_key_agreement_raw_internal( alg, slot,
5657 peer_key, peer_key_length,
5658 output, output_size,
5659 output_length );
5660
5661exit:
5662 if( status != PSA_SUCCESS )
5663 {
5664 /* If an error happens and is not handled properly, the output
5665 * may be used as a key to protect sensitive data. Arrange for such
5666 * a key to be random, which is likely to result in decryption or
5667 * verification errors. This is better than filling the buffer with
5668 * some constant data such as zeros, which would result in the data
5669 * being protected with a reproducible, easily knowable key.
5670 */
5671 psa_generate_random( output, output_size );
5672 *output_length = output_size;
5673 }
5674 return( status );
5675}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005676
5677
5678/****************************************************************/
5679/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005680/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005681
Gilles Peskine4c317f42018-07-12 01:24:09 +02005682psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005683 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005684{
Janos Follath24eed8d2019-11-22 13:21:35 +00005685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005686 GUARD_MODULE_INITIALIZED;
5687
Gilles Peskinef181eca2019-08-07 13:49:00 +02005688 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5689 {
5690 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5691 output,
5692 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5693 if( ret != 0 )
5694 return( mbedtls_to_psa_error( ret ) );
5695 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5696 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5697 }
5698
Gilles Peskinee59236f2018-01-27 23:32:46 +01005699 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5700 return( mbedtls_to_psa_error( ret ) );
5701}
5702
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005703#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5704#include "mbedtls/entropy_poll.h"
5705
Gilles Peskine7228da22019-07-15 11:06:15 +02005706psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005707 size_t seed_size )
5708{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005709 if( global_data.initialized )
5710 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005711
5712 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5713 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5714 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5715 return( PSA_ERROR_INVALID_ARGUMENT );
5716
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005717 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005718}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005719#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005720
Gilles Peskinee56e8782019-04-26 17:34:02 +02005721#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5722static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5723 size_t domain_parameters_size,
5724 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005725{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005726 size_t i;
5727 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005728
Gilles Peskinee56e8782019-04-26 17:34:02 +02005729 if( domain_parameters_size == 0 )
5730 {
5731 *exponent = 65537;
5732 return( PSA_SUCCESS );
5733 }
5734
5735 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5736 * support values that fit in a 32-bit integer, which is larger than
5737 * int on just about every platform anyway. */
5738 if( domain_parameters_size > sizeof( acc ) )
5739 return( PSA_ERROR_NOT_SUPPORTED );
5740 for( i = 0; i < domain_parameters_size; i++ )
5741 acc = ( acc << 8 ) | domain_parameters[i];
5742 if( acc > INT_MAX )
5743 return( PSA_ERROR_NOT_SUPPORTED );
5744 *exponent = acc;
5745 return( PSA_SUCCESS );
5746}
5747#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5748
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005749static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005750 psa_key_slot_t *slot, size_t bits,
5751 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005752{
Gilles Peskine8e338702019-07-30 20:06:31 +02005753 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005754
Gilles Peskinee56e8782019-04-26 17:34:02 +02005755 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005756 return( PSA_ERROR_INVALID_ARGUMENT );
5757
Gilles Peskinee59236f2018-01-27 23:32:46 +01005758 if( key_type_is_raw_bytes( type ) )
5759 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005760 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005761
5762 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005763 if( status != PSA_SUCCESS )
5764 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005765
5766 /* Allocate memory for the key */
5767 slot->data.key.bytes = PSA_BITS_TO_BYTES( bits );
5768 slot->data.key.data = mbedtls_calloc( 1, slot->data.key.bytes );
5769 if( slot->data.key.data == NULL )
5770 {
5771 slot->data.key.bytes = 0;
5772 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5773 }
5774
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005775 status = psa_generate_random( slot->data.key.data,
5776 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005777 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005778 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005779
5780 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005781#if defined(MBEDTLS_DES_C)
5782 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005783 psa_des_set_key_parity( slot->data.key.data,
5784 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005785#endif /* MBEDTLS_DES_C */
5786 }
5787 else
5788
5789#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005790 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005791 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005792 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005794 int exponent;
5795 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005796 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5797 return( PSA_ERROR_NOT_SUPPORTED );
5798 /* Accept only byte-aligned keys, for the same reasons as
5799 * in psa_import_rsa_key(). */
5800 if( bits % 8 != 0 )
5801 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005802 status = psa_read_rsa_exponent( domain_parameters,
5803 domain_parameters_size,
5804 &exponent );
5805 if( status != PSA_SUCCESS )
5806 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005807 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5808 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005809 mbedtls_ctr_drbg_random,
5810 &global_data.ctr_drbg,
5811 (unsigned int) bits,
5812 exponent );
5813 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005814 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005815
5816 /* Make sure to always have an export representation available */
5817 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5818
5819 slot->data.key.data = mbedtls_calloc( 1, bytes );
5820 if( slot->data.key.data == NULL )
5821 {
5822 mbedtls_rsa_free( &rsa );
5823 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005824 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005825
5826 status = psa_export_rsa_key( type,
5827 &rsa,
5828 slot->data.key.data,
5829 bytes,
5830 &slot->data.key.bytes );
5831 mbedtls_rsa_free( &rsa );
5832 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005833 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005834 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005835 }
5836 else
5837#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5838
5839#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005840 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005841 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005842 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005843 mbedtls_ecp_group_id grp_id =
5844 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005845 const mbedtls_ecp_curve_info *curve_info =
5846 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02005847 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005849 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005850 return( PSA_ERROR_NOT_SUPPORTED );
5851 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5852 return( PSA_ERROR_NOT_SUPPORTED );
5853 if( curve_info->bit_size != bits )
5854 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005855 mbedtls_ecp_keypair_init( &ecp );
5856 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005857 mbedtls_ctr_drbg_random,
5858 &global_data.ctr_drbg );
5859 if( ret != 0 )
5860 {
Steven Cooremanacda8342020-07-24 23:09:52 +02005861 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005862 return( mbedtls_to_psa_error( ret ) );
5863 }
Steven Cooremanacda8342020-07-24 23:09:52 +02005864
5865
5866 /* Make sure to always have an export representation available */
5867 size_t bytes = PSA_BITS_TO_BYTES( bits );
5868 slot->data.key.data = mbedtls_calloc( 1, bytes );
5869 if( slot->data.key.data == NULL )
5870 {
5871 mbedtls_ecp_keypair_free( &ecp );
5872 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5873 }
5874 slot->data.key.bytes = bytes;
5875 psa_status_t status = mbedtls_to_psa_error(
5876 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
5877
5878 mbedtls_ecp_keypair_free( &ecp );
5879 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005880 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005881 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005882 }
5883 else
5884#endif /* MBEDTLS_ECP_C */
5885
5886 return( PSA_ERROR_NOT_SUPPORTED );
5887
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005888 return( PSA_SUCCESS );
5889}
Darryl Green0c6575a2018-11-07 16:05:30 +00005890
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005891psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005892 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005893{
5894 psa_status_t status;
5895 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005896 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005897
Gilles Peskine0f84d622019-09-12 19:03:13 +02005898 /* Reject any attempt to create a zero-length key so that we don't
5899 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5900 if( psa_get_key_bits( attributes ) == 0 )
5901 return( PSA_ERROR_INVALID_ARGUMENT );
5902
Gilles Peskinedf179142019-07-15 22:02:14 +02005903 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5904 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005905 if( status != PSA_SUCCESS )
5906 goto exit;
5907
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005908#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5909 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005910 {
Gilles Peskine11792082019-08-06 18:36:36 +02005911 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5912 size_t pubkey_length = 0; /* We don't support this feature yet */
5913 if( drv->key_management == NULL ||
5914 drv->key_management->p_generate == NULL )
5915 {
5916 status = PSA_ERROR_NOT_SUPPORTED;
5917 goto exit;
5918 }
5919 status = drv->key_management->p_generate(
5920 psa_get_se_driver_context( driver ),
5921 slot->data.se.slot_number, attributes,
5922 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005923 }
Gilles Peskine11792082019-08-06 18:36:36 +02005924 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005925#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005926 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005927 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005928 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005929 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005930 }
Gilles Peskine11792082019-08-06 18:36:36 +02005931
5932exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005933 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005934 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005935 if( status != PSA_SUCCESS )
5936 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005937 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005938 *handle = 0;
5939 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005940 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005941}
5942
5943
Gilles Peskinee59236f2018-01-27 23:32:46 +01005944
5945/****************************************************************/
5946/* Module setup */
5947/****************************************************************/
5948
Gilles Peskine5e769522018-11-20 21:59:56 +01005949psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5950 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5951 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5952{
5953 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5954 return( PSA_ERROR_BAD_STATE );
5955 global_data.entropy_init = entropy_init;
5956 global_data.entropy_free = entropy_free;
5957 return( PSA_SUCCESS );
5958}
5959
Gilles Peskinee59236f2018-01-27 23:32:46 +01005960void mbedtls_psa_crypto_free( void )
5961{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005962 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005963 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5964 {
5965 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005966 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005967 }
5968 /* Wipe all remaining data, including configuration.
5969 * In particular, this sets all state indicator to the value
5970 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005971 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005972#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005973 /* Unregister all secure element drivers, so that we restart from
5974 * a pristine state. */
5975 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005976#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005977}
5978
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005979#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5980/** Recover a transaction that was interrupted by a power failure.
5981 *
5982 * This function is called during initialization, before psa_crypto_init()
5983 * returns. If this function returns a failure status, the initialization
5984 * fails.
5985 */
5986static psa_status_t psa_crypto_recover_transaction(
5987 const psa_crypto_transaction_t *transaction )
5988{
5989 switch( transaction->unknown.type )
5990 {
5991 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5992 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005993 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005994 * is implemented.
5995 * https://github.com/ARMmbed/mbed-crypto/issues/218
5996 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005997 default:
5998 /* We found an unsupported transaction in the storage.
5999 * We don't know what state the storage is in. Give up. */
6000 return( PSA_ERROR_STORAGE_FAILURE );
6001 }
6002}
6003#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6004
Gilles Peskinee59236f2018-01-27 23:32:46 +01006005psa_status_t psa_crypto_init( void )
6006{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006007 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006008 const unsigned char drbg_seed[] = "PSA";
6009
Gilles Peskinec6b69072018-11-20 21:42:52 +01006010 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006011 if( global_data.initialized != 0 )
6012 return( PSA_SUCCESS );
6013
Gilles Peskine5e769522018-11-20 21:59:56 +01006014 /* Set default configuration if
6015 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6016 if( global_data.entropy_init == NULL )
6017 global_data.entropy_init = mbedtls_entropy_init;
6018 if( global_data.entropy_free == NULL )
6019 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006020
Gilles Peskinec6b69072018-11-20 21:42:52 +01006021 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006022 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006023#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6024 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6025 /* The PSA entropy injection feature depends on using NV seed as an entropy
6026 * source. Add NV seed as an entropy source for PSA entropy injection. */
6027 mbedtls_entropy_add_source( &global_data.entropy,
6028 mbedtls_nv_seed_poll, NULL,
6029 MBEDTLS_ENTROPY_BLOCK_SIZE,
6030 MBEDTLS_ENTROPY_SOURCE_STRONG );
6031#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006032 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006033 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006034 status = mbedtls_to_psa_error(
6035 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6036 mbedtls_entropy_func,
6037 &global_data.entropy,
6038 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6039 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006040 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006041 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006042
Gilles Peskine66fb1262018-12-10 16:29:04 +01006043 status = psa_initialize_key_slots( );
6044 if( status != PSA_SUCCESS )
6045 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006046
Gilles Peskined9348f22019-10-01 15:22:29 +02006047#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6048 status = psa_init_all_se_drivers( );
6049 if( status != PSA_SUCCESS )
6050 goto exit;
6051#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6052
Gilles Peskine4b734222019-07-24 15:56:31 +02006053#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006054 status = psa_crypto_load_transaction( );
6055 if( status == PSA_SUCCESS )
6056 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006057 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6058 if( status != PSA_SUCCESS )
6059 goto exit;
6060 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006061 }
6062 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6063 {
6064 /* There's no transaction to complete. It's all good. */
6065 status = PSA_SUCCESS;
6066 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006067#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006068
Gilles Peskinec6b69072018-11-20 21:42:52 +01006069 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006070 global_data.initialized = 1;
6071
6072exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006073 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006074 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006075 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006076}
6077
6078#endif /* MBEDTLS_PSA_CRYPTO_C */