blob: 4a3877cc127d73fc882903d6c7e5829f0d4b075c [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 }
Steven Cooreman19fd5742020-07-24 23:31:01 +0200954 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
955 {
Gilles Peskinef76aa772018-10-29 19:24:33 +0100956#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +0200957 status = psa_import_ecp_key( slot,
958 data, data_length );
959#else
960 /* No drivers have been implemented yet, so without mbed TLS backing
961 * there's no way to do ECP with the current library. */
962 return( PSA_ERROR_NOT_SUPPORTED );
963#endif /* defined(MBEDTLS_ECP_C) */
964 }
965 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100966 {
Steven Cooreman19fd5742020-07-24 23:31:01 +0200967#if defined(MBEDTLS_RSA_C)
968 status = psa_import_rsa_key( slot,
969 data, data_length );
970#else
971 /* No drivers have been implemented yet, so without mbed TLS backing
972 * there's no way to do RSA with the current library. */
973 status = PSA_ERROR_NOT_SUPPORTED;
974#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975 }
976 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100977 {
Steven Cooreman19fd5742020-07-24 23:31:01 +0200978 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100979 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +0100980 }
Darryl Green940d72c2018-07-13 13:18:51 +0100981
Darryl Green06fd18d2018-07-16 11:21:11 +0100982 return( status );
983}
984
Gilles Peskinef603c712019-01-19 13:40:11 +0100985/** Calculate the intersection of two algorithm usage policies.
986 *
987 * Return 0 (which allows no operation) on incompatibility.
988 */
989static psa_algorithm_t psa_key_policy_algorithm_intersection(
990 psa_algorithm_t alg1,
991 psa_algorithm_t alg2 )
992{
Gilles Peskine549ea862019-05-22 11:45:59 +0200993 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100994 if( alg1 == alg2 )
995 return( alg1 );
996 /* If the policies are from the same hash-and-sign family, check
997 * if one is a wildcard. If so the other has the specific algorithm. */
998 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
999 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1000 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1001 {
1002 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1003 return( alg2 );
1004 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1005 return( alg1 );
1006 }
1007 /* If the policies are incompatible, allow nothing. */
1008 return( 0 );
1009}
1010
Gilles Peskined6f371b2019-05-10 19:33:38 +02001011static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1012 psa_algorithm_t requested_alg )
1013{
Gilles Peskine549ea862019-05-22 11:45:59 +02001014 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001015 if( requested_alg == policy_alg )
1016 return( 1 );
1017 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001018 * and requested_alg is the same hash-and-sign family with any hash,
1019 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001020 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1021 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1022 {
1023 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1024 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1025 }
1026 /* If it isn't permitted, it's forbidden. */
1027 return( 0 );
1028}
1029
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001030/** Test whether a policy permits an algorithm.
1031 *
1032 * The caller must test usage flags separately.
1033 */
1034static int psa_key_policy_permits( const psa_key_policy_t *policy,
1035 psa_algorithm_t alg )
1036{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001037 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1038 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001039}
1040
Gilles Peskinef603c712019-01-19 13:40:11 +01001041/** Restrict a key policy based on a constraint.
1042 *
1043 * \param[in,out] policy The policy to restrict.
1044 * \param[in] constraint The policy constraint to apply.
1045 *
1046 * \retval #PSA_SUCCESS
1047 * \c *policy contains the intersection of the original value of
1048 * \c *policy and \c *constraint.
1049 * \retval #PSA_ERROR_INVALID_ARGUMENT
1050 * \c *policy and \c *constraint are incompatible.
1051 * \c *policy is unchanged.
1052 */
1053static psa_status_t psa_restrict_key_policy(
1054 psa_key_policy_t *policy,
1055 const psa_key_policy_t *constraint )
1056{
1057 psa_algorithm_t intersection_alg =
1058 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001059 psa_algorithm_t intersection_alg2 =
1060 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001061 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1062 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001063 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1064 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001065 policy->usage &= constraint->usage;
1066 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001067 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001068 return( PSA_SUCCESS );
1069}
1070
Darryl Green06fd18d2018-07-16 11:21:11 +01001071/** Retrieve a slot which must contain a key. The key must have allow all the
1072 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1073 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001074static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001075 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001076 psa_key_usage_t usage,
1077 psa_algorithm_t alg )
1078{
1079 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001080 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001081
1082 *p_slot = NULL;
1083
Gilles Peskinec5487a82018-12-03 18:08:14 +01001084 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001085 if( status != PSA_SUCCESS )
1086 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001087
1088 /* Enforce that usage policy for the key slot contains all the flags
1089 * required by the usage parameter. There is one exception: public
1090 * keys can always be exported, so we treat public key objects as
1091 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001092 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001093 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001094 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001095 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001096
1097 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001098 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001099 return( PSA_ERROR_NOT_PERMITTED );
1100
1101 *p_slot = slot;
1102 return( PSA_SUCCESS );
1103}
Darryl Green940d72c2018-07-13 13:18:51 +01001104
Gilles Peskine28f8f302019-07-24 13:30:31 +02001105/** Retrieve a slot which must contain a transparent key.
1106 *
1107 * A transparent key is a key for which the key material is directly
1108 * available, as opposed to a key in a secure element.
1109 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001110 * This is a temporary function to use instead of psa_get_key_from_slot()
1111 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001112 */
1113#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1114static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1115 psa_key_slot_t **p_slot,
1116 psa_key_usage_t usage,
1117 psa_algorithm_t alg )
1118{
1119 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1120 if( status != PSA_SUCCESS )
1121 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001122 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001123 {
1124 *p_slot = NULL;
1125 return( PSA_ERROR_NOT_SUPPORTED );
1126 }
1127 return( PSA_SUCCESS );
1128}
1129#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1130/* With no secure element support, all keys are transparent. */
1131#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1132 psa_get_key_from_slot( handle, p_slot, usage, alg )
1133#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1134
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001135/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001136static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001137{
Gilles Peskine73167e12019-07-12 23:44:37 +02001138#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1139 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001140 {
1141 /* No key material to clean. */
1142 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001143 else
1144#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001145 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001146 {
1147 /* No key material to clean. */
1148 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001149 else if( key_type_is_raw_bytes( slot->attr.type ) ||
1150 PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1151 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001152 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001153 mbedtls_free( slot->data.key.data );
1154 slot->data.key.data = NULL;
1155 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001156 }
1157 else
Darryl Green40225ba2018-11-15 14:48:15 +00001158 {
1159 /* Shouldn't happen: the key type is not any type that we
1160 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001161 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001162 }
1163
1164 return( PSA_SUCCESS );
1165}
1166
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001167/** Completely wipe a slot in memory, including its policy.
1168 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001169psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001170{
1171 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001172 /* Multipart operations may still be using the key. This is safe
1173 * because all multipart operation objects are independent from
1174 * the key slot: if they need to access the key after the setup
1175 * phase, they have a copy of the key. Note that this means that
1176 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001177 /* At this point, key material and other type-specific content has
1178 * been wiped. Clear remaining metadata. We can call memset and not
1179 * zeroize because the metadata is not particularly sensitive. */
1180 memset( slot, 0, sizeof( *slot ) );
1181 return( status );
1182}
1183
Gilles Peskinec5487a82018-12-03 18:08:14 +01001184psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001185{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001186 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001187 psa_status_t status; /* status of the last operation */
1188 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001189#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1190 psa_se_drv_table_entry_t *driver;
1191#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001192
Gilles Peskine1841cf42019-10-08 15:48:25 +02001193 if( handle == 0 )
1194 return( PSA_SUCCESS );
1195
Gilles Peskinec5487a82018-12-03 18:08:14 +01001196 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001197 if( status != PSA_SUCCESS )
1198 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001199
1200#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001201 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001202 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001203 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001204 /* For a key in a secure element, we need to do three things:
1205 * remove the key file in internal storage, destroy the
1206 * key inside the secure element, and update the driver's
1207 * persistent data. Start a transaction that will encompass these
1208 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001209 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001210 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001211 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001212 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001213 status = psa_crypto_save_transaction( );
1214 if( status != PSA_SUCCESS )
1215 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001216 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001217 /* We should still try to destroy the key in the secure
1218 * element and the key metadata in storage. This is especially
1219 * important if the error is that the storage is full.
1220 * But how to do it exactly without risking an inconsistent
1221 * state after a reset?
1222 * https://github.com/ARMmbed/mbed-crypto/issues/215
1223 */
1224 overall_status = status;
1225 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001226 }
1227
Gilles Peskine354f7672019-07-12 23:46:38 +02001228 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001229 if( overall_status == PSA_SUCCESS )
1230 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001231 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001232#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1233
Darryl Greend49a4992018-06-18 17:27:26 +01001234#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001235 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001236 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001237 status = psa_destroy_persistent_key( slot->attr.id );
1238 if( overall_status == PSA_SUCCESS )
1239 overall_status = status;
1240
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001241 /* TODO: other slots may have a copy of the same key. We should
1242 * invalidate them.
1243 * https://github.com/ARMmbed/mbed-crypto/issues/214
1244 */
Darryl Greend49a4992018-06-18 17:27:26 +01001245 }
1246#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001247
Gilles Peskinefc762652019-07-22 19:30:34 +02001248#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1249 if( driver != NULL )
1250 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001251 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001252 if( overall_status == PSA_SUCCESS )
1253 overall_status = status;
1254 status = psa_crypto_stop_transaction( );
1255 if( overall_status == PSA_SUCCESS )
1256 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001257 }
1258#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1259
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001260#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1261exit:
1262#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001263 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001264 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1265 if( overall_status == PSA_SUCCESS )
1266 overall_status = status;
1267 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001268}
1269
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001270void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001271{
Gilles Peskineb699f072019-04-26 16:06:02 +02001272 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001273 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001274}
1275
Gilles Peskineb699f072019-04-26 16:06:02 +02001276psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1277 psa_key_type_t type,
1278 const uint8_t *data,
1279 size_t data_length )
1280{
1281 uint8_t *copy = NULL;
1282
1283 if( data_length != 0 )
1284 {
1285 copy = mbedtls_calloc( 1, data_length );
1286 if( copy == NULL )
1287 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1288 memcpy( copy, data, data_length );
1289 }
1290 /* After this point, this function is guaranteed to succeed, so it
1291 * can start modifying `*attributes`. */
1292
1293 if( attributes->domain_parameters != NULL )
1294 {
1295 mbedtls_free( attributes->domain_parameters );
1296 attributes->domain_parameters = NULL;
1297 attributes->domain_parameters_size = 0;
1298 }
1299
1300 attributes->domain_parameters = copy;
1301 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001302 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001303 return( PSA_SUCCESS );
1304}
1305
1306psa_status_t psa_get_key_domain_parameters(
1307 const psa_key_attributes_t *attributes,
1308 uint8_t *data, size_t data_size, size_t *data_length )
1309{
1310 if( attributes->domain_parameters_size > data_size )
1311 return( PSA_ERROR_BUFFER_TOO_SMALL );
1312 *data_length = attributes->domain_parameters_size;
1313 if( attributes->domain_parameters_size != 0 )
1314 memcpy( data, attributes->domain_parameters,
1315 attributes->domain_parameters_size );
1316 return( PSA_SUCCESS );
1317}
1318
1319#if defined(MBEDTLS_RSA_C)
1320static psa_status_t psa_get_rsa_public_exponent(
1321 const mbedtls_rsa_context *rsa,
1322 psa_key_attributes_t *attributes )
1323{
1324 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001326 uint8_t *buffer = NULL;
1327 size_t buflen;
1328 mbedtls_mpi_init( &mpi );
1329
1330 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1331 if( ret != 0 )
1332 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001333 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1334 {
1335 /* It's the default value, which is reported as an empty string,
1336 * so there's nothing to do. */
1337 goto exit;
1338 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001339
1340 buflen = mbedtls_mpi_size( &mpi );
1341 buffer = mbedtls_calloc( 1, buflen );
1342 if( buffer == NULL )
1343 {
1344 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1345 goto exit;
1346 }
1347 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1348 if( ret != 0 )
1349 goto exit;
1350 attributes->domain_parameters = buffer;
1351 attributes->domain_parameters_size = buflen;
1352
1353exit:
1354 mbedtls_mpi_free( &mpi );
1355 if( ret != 0 )
1356 mbedtls_free( buffer );
1357 return( mbedtls_to_psa_error( ret ) );
1358}
1359#endif /* MBEDTLS_RSA_C */
1360
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001361/** Retrieve all the publicly-accessible attributes of a key.
1362 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001363psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1364 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001365{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001366 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001367 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001368
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001369 psa_reset_key_attributes( attributes );
1370
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001371 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001372 if( status != PSA_SUCCESS )
1373 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001374
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001375 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001376 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1377 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1378
1379#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1380 if( psa_key_slot_is_external( slot ) )
1381 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1382#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001383
Gilles Peskine8e338702019-07-30 20:06:31 +02001384 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001385 {
1386#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001387 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001388 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001389#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001390 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001391 * is not yet implemented.
1392 * https://github.com/ARMmbed/mbed-crypto/issues/216
1393 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001394 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001395 break;
1396#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001397 {
1398 mbedtls_rsa_context rsa;
1399 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
1400
1401 status = psa_load_rsa_representation( slot, &rsa );
1402 if( status != PSA_SUCCESS )
1403 break;
1404
1405 status = psa_get_rsa_public_exponent( &rsa,
1406 attributes );
1407 mbedtls_rsa_free( &rsa );
1408 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001409 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001410#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001411 default:
1412 /* Nothing else to do. */
1413 break;
1414 }
1415
1416 if( status != PSA_SUCCESS )
1417 psa_reset_key_attributes( attributes );
1418 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001419}
1420
Gilles Peskinec8000c02019-08-02 20:15:51 +02001421#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1422psa_status_t psa_get_key_slot_number(
1423 const psa_key_attributes_t *attributes,
1424 psa_key_slot_number_t *slot_number )
1425{
1426 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1427 {
1428 *slot_number = attributes->slot_number;
1429 return( PSA_SUCCESS );
1430 }
1431 else
1432 return( PSA_ERROR_INVALID_ARGUMENT );
1433}
1434#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1435
Steven Cooremana01795d2020-07-24 22:48:15 +02001436static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1437 uint8_t *data,
1438 size_t data_size,
1439 size_t *data_length )
1440{
1441 if( slot->data.key.bytes > data_size )
1442 return( PSA_ERROR_BUFFER_TOO_SMALL );
1443 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1444 memset( data + slot->data.key.bytes, 0,
1445 data_size - slot->data.key.bytes );
1446 *data_length = slot->data.key.bytes;
1447 return( PSA_SUCCESS );
1448}
1449
Gilles Peskinef603c712019-01-19 13:40:11 +01001450static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1451 uint8_t *data,
1452 size_t data_size,
1453 size_t *data_length,
1454 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001455{
Gilles Peskine5d309672019-07-12 23:47:28 +02001456#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1457 const psa_drv_se_t *drv;
1458 psa_drv_se_context_t *drv_context;
1459#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1460
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001461 *data_length = 0;
1462
Gilles Peskine8e338702019-07-30 20:06:31 +02001463 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001464 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001465
Gilles Peskinef9168942019-09-12 19:20:29 +02001466 /* Reject a zero-length output buffer now, since this can never be a
1467 * valid key representation. This way we know that data must be a valid
1468 * pointer and we can do things like memset(data, ..., data_size). */
1469 if( data_size == 0 )
1470 return( PSA_ERROR_BUFFER_TOO_SMALL );
1471
Gilles Peskine5d309672019-07-12 23:47:28 +02001472#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001473 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001474 {
1475 psa_drv_se_export_key_t method;
1476 if( drv->key_management == NULL )
1477 return( PSA_ERROR_NOT_SUPPORTED );
1478 method = ( export_public_key ?
1479 drv->key_management->p_export_public :
1480 drv->key_management->p_export );
1481 if( method == NULL )
1482 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001483 return( method( drv_context,
1484 slot->data.se.slot_number,
1485 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001486 }
1487#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1488
Gilles Peskine8e338702019-07-30 20:06:31 +02001489 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001490 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001491 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001492 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001493 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1494 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001495 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001496 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001497 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001498 /* Exporting public -> public */
1499 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1500 }
1501 else if( !export_public_key )
1502 {
1503 /* Exporting private -> private */
1504 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1505 }
1506 /* Need to export the public part of a private key,
1507 * so conversion is needed */
1508 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1509 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001510#if defined(MBEDTLS_RSA_C)
Steven Cooreman560c28a2020-07-24 23:20:24 +02001511 mbedtls_rsa_context rsa;
1512 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
Steven Cooremana01795d2020-07-24 22:48:15 +02001513
Steven Cooreman560c28a2020-07-24 23:20:24 +02001514 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
1515 if( status != PSA_SUCCESS )
1516 return status;
Steven Cooremana01795d2020-07-24 22:48:15 +02001517
Steven Cooreman560c28a2020-07-24 23:20:24 +02001518 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
1519 &rsa,
1520 data,
1521 data_size,
1522 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001523
Steven Cooreman560c28a2020-07-24 23:20:24 +02001524 mbedtls_rsa_free( &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001525
Steven Cooreman560c28a2020-07-24 23:20:24 +02001526 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001527#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001528 /* We don't know how to convert a private RSA key to public. */
1529 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001530#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001531 }
1532 else
Moran Pekera998bc62018-04-16 18:16:20 +03001533 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001534#if defined(MBEDTLS_ECP_C)
1535 mbedtls_ecp_keypair ecp;
1536 psa_status_t status = psa_load_ecp_representation( slot, &ecp );
1537 if( status != PSA_SUCCESS )
1538 return status;
1539
1540 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1541 PSA_KEY_TYPE_ECC_GET_FAMILY(
1542 slot->attr.type ) ),
1543 &ecp,
1544 data,
1545 data_size,
1546 data_length );
1547
1548 mbedtls_ecp_keypair_free( &ecp );
1549 return( status );
1550#else
1551 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001552 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001553#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001554 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001555 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001556 else
1557 {
1558 /* This shouldn't happen in the reference implementation, but
1559 it is valid for a special-purpose implementation to omit
1560 support for exporting certain key types. */
1561 return( PSA_ERROR_NOT_SUPPORTED );
1562 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001563}
1564
Gilles Peskinec5487a82018-12-03 18:08:14 +01001565psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001566 uint8_t *data,
1567 size_t data_size,
1568 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001569{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001570 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001571 psa_status_t status;
1572
1573 /* Set the key to empty now, so that even when there are errors, we always
1574 * set data_length to a value between 0 and data_size. On error, setting
1575 * the key to empty is a good choice because an empty key representation is
1576 * unlikely to be accepted anywhere. */
1577 *data_length = 0;
1578
1579 /* Export requires the EXPORT flag. There is an exception for public keys,
1580 * which don't require any flag, but psa_get_key_from_slot takes
1581 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001582 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001583 if( status != PSA_SUCCESS )
1584 return( status );
1585 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001586 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001587}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001588
Gilles Peskinec5487a82018-12-03 18:08:14 +01001589psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001590 uint8_t *data,
1591 size_t data_size,
1592 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001593{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001594 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001595 psa_status_t status;
1596
1597 /* Set the key to empty now, so that even when there are errors, we always
1598 * set data_length to a value between 0 and data_size. On error, setting
1599 * the key to empty is a good choice because an empty key representation is
1600 * unlikely to be accepted anywhere. */
1601 *data_length = 0;
1602
1603 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001604 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001605 if( status != PSA_SUCCESS )
1606 return( status );
1607 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001608 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001609}
1610
Gilles Peskine91e8c332019-08-02 19:19:39 +02001611#if defined(static_assert)
1612static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1613 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001614static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001615 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001616static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001617 "One or more key attribute flag is listed as both internal-only and external-only" );
1618#endif
1619
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001620/** Validate that a key policy is internally well-formed.
1621 *
1622 * This function only rejects invalid policies. It does not validate the
1623 * consistency of the policy with respect to other attributes of the key
1624 * such as the key type.
1625 */
1626static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001627{
1628 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001629 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001630 PSA_KEY_USAGE_ENCRYPT |
1631 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001632 PSA_KEY_USAGE_SIGN_HASH |
1633 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001634 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1635 return( PSA_ERROR_INVALID_ARGUMENT );
1636
Gilles Peskine4747d192019-04-17 15:05:45 +02001637 return( PSA_SUCCESS );
1638}
1639
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001640/** Validate the internal consistency of key attributes.
1641 *
1642 * This function only rejects invalid attribute values. If does not
1643 * validate the consistency of the attributes with any key data that may
1644 * be involved in the creation of the key.
1645 *
1646 * Call this function early in the key creation process.
1647 *
1648 * \param[in] attributes Key attributes for the new key.
1649 * \param[out] p_drv On any return, the driver for the key, if any.
1650 * NULL for a transparent key.
1651 *
1652 */
1653static psa_status_t psa_validate_key_attributes(
1654 const psa_key_attributes_t *attributes,
1655 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001656{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001657 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001658
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001659 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1660 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001661 if( status != PSA_SUCCESS )
1662 return( status );
1663
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001664 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1665 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001666 if( status != PSA_SUCCESS )
1667 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001668
1669 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001670 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001671 return( status );
1672
1673 /* Refuse to create overly large keys.
1674 * Note that this doesn't trigger on import if the attributes don't
1675 * explicitly specify a size (so psa_get_key_bits returns 0), so
1676 * psa_import_key() needs its own checks. */
1677 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1678 return( PSA_ERROR_NOT_SUPPORTED );
1679
Gilles Peskine91e8c332019-08-02 19:19:39 +02001680 /* Reject invalid flags. These should not be reachable through the API. */
1681 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1682 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1683 return( PSA_ERROR_INVALID_ARGUMENT );
1684
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001685 return( PSA_SUCCESS );
1686}
1687
Gilles Peskine4747d192019-04-17 15:05:45 +02001688/** Prepare a key slot to receive key material.
1689 *
1690 * This function allocates a key slot and sets its metadata.
1691 *
1692 * If this function fails, call psa_fail_key_creation().
1693 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001694 * This function is intended to be used as follows:
1695 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1696 * it with the specified attributes, and assign it a handle.
1697 * -# Populate the slot with the key material.
1698 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1699 * In case of failure at any step, stop the sequence and call
1700 * psa_fail_key_creation().
1701 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001702 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001703 * \param[in] attributes Key attributes for the new key.
1704 * \param[out] handle On success, a handle for the allocated slot.
1705 * \param[out] p_slot On success, a pointer to the prepared slot.
1706 * \param[out] p_drv On any return, the driver for the key, if any.
1707 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001708 *
1709 * \retval #PSA_SUCCESS
1710 * The key slot is ready to receive key material.
1711 * \return If this function fails, the key slot is an invalid state.
1712 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001713 */
1714static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001715 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001716 const psa_key_attributes_t *attributes,
1717 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001718 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001719 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001720{
1721 psa_status_t status;
1722 psa_key_slot_t *slot;
1723
Gilles Peskinedf179142019-07-15 22:02:14 +02001724 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001725 *p_drv = NULL;
1726
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001727 status = psa_validate_key_attributes( attributes, p_drv );
1728 if( status != PSA_SUCCESS )
1729 return( status );
1730
Gilles Peskineedbed562019-08-07 18:19:59 +02001731 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001732 if( status != PSA_SUCCESS )
1733 return( status );
1734 slot = *p_slot;
1735
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001736 /* We're storing the declared bit-size of the key. It's up to each
1737 * creation mechanism to verify that this information is correct.
1738 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001739 * an input (generate, device) but not for those where the bit-size
1740 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001741
1742 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001743
Gilles Peskine91e8c332019-08-02 19:19:39 +02001744 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001745 * external-only flags, query `attributes`. Thanks to the check
1746 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001747 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001748 * may have set. */
1749 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001750
Gilles Peskinecbaff462019-07-12 23:46:04 +02001751#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001752 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001753 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001754 * create the key file in internal storage, create the
1755 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001756 * persistent data. This is done by starting a transaction that will
1757 * encompass these three actions.
1758 * For registering a volatile key, we just need to find an appropriate
1759 * slot number inside the SE. Since the key is designated volatile, creating
1760 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001761 /* The first thing to do is to find a slot number for the new key.
1762 * We save the slot number in persistent storage as part of the
1763 * transaction data. It will be needed to recover if the power
1764 * fails during the key creation process, to clean up on the secure
1765 * element side after restarting. Obtaining a slot number from the
1766 * secure element driver updates its persistent state, but we do not yet
1767 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001768 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001769 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001770 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001771 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001772 &slot->data.se.slot_number );
1773 if( status != PSA_SUCCESS )
1774 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001775
1776 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001777 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001778 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1779 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1780 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1781 psa_crypto_transaction.key.id = slot->attr.id;
1782 status = psa_crypto_save_transaction( );
1783 if( status != PSA_SUCCESS )
1784 {
1785 (void) psa_crypto_stop_transaction( );
1786 return( status );
1787 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001788 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001789 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001790
1791 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1792 {
1793 /* Key registration only makes sense with a secure element. */
1794 return( PSA_ERROR_INVALID_ARGUMENT );
1795 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001796#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1797
Darryl Green0c6575a2018-11-07 16:05:30 +00001798 return( status );
1799}
Gilles Peskine4747d192019-04-17 15:05:45 +02001800
1801/** Finalize the creation of a key once its key material has been set.
1802 *
1803 * This entails writing the key to persistent storage.
1804 *
1805 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001806 * See the documentation of psa_start_key_creation() for the intended use
1807 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001808 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001809 * \param[in,out] slot Pointer to the slot with key material.
1810 * \param[in] driver The secure element driver for the key,
1811 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001812 *
1813 * \retval #PSA_SUCCESS
1814 * The key was successfully created. The handle is now valid.
1815 * \return If this function fails, the key slot is an invalid state.
1816 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001817 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001818static psa_status_t psa_finish_key_creation(
1819 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001820 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001821{
1822 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001823 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001824 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001825
1826#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001827 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001828 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001829#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1830 if( driver != NULL )
1831 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001832 psa_se_key_data_storage_t data;
1833#if defined(static_assert)
1834 static_assert( sizeof( slot->data.se.slot_number ) ==
1835 sizeof( data.slot_number ),
1836 "Slot number size does not match psa_se_key_data_storage_t" );
1837 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1838 "Bit-size size does not match psa_se_key_data_storage_t" );
1839#endif
1840 memcpy( &data.slot_number, &slot->data.se.slot_number,
1841 sizeof( slot->data.se.slot_number ) );
1842 memcpy( &data.bits, &slot->attr.bits,
1843 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001844 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001845 (uint8_t*) &data,
1846 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001847 }
1848 else
1849#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1850 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001851 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001852 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001853 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001854 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1855 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001856 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001857 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1858 status = psa_internal_export_key( slot,
1859 buffer, buffer_size, &length,
1860 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001861 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001862 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001863 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001864
Gilles Peskinef9168942019-09-12 19:20:29 +02001865 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001866 mbedtls_free( buffer );
1867 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001868 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001869#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1870
Gilles Peskinecbaff462019-07-12 23:46:04 +02001871#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001872 /* Finish the transaction for a key creation. This does not
1873 * happen when registering an existing key. Detect this case
1874 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001875 * creation of a persistent key in a secure element requires a transaction,
1876 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001877 if( driver != NULL &&
1878 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001879 {
1880 status = psa_save_se_persistent_data( driver );
1881 if( status != PSA_SUCCESS )
1882 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001883 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001884 return( status );
1885 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001886 status = psa_crypto_stop_transaction( );
1887 if( status != PSA_SUCCESS )
1888 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001889 }
1890#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1891
Gilles Peskine4747d192019-04-17 15:05:45 +02001892 return( status );
1893}
1894
1895/** Abort the creation of a key.
1896 *
1897 * You may call this function after calling psa_start_key_creation(),
1898 * or after psa_finish_key_creation() fails. In other circumstances, this
1899 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001900 * See the documentation of psa_start_key_creation() for the intended use
1901 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001902 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001903 * \param[in,out] slot Pointer to the slot with key material.
1904 * \param[in] driver The secure element driver for the key,
1905 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001906 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001907static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001908 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001909{
Gilles Peskine011e4282019-06-26 18:34:38 +02001910 (void) driver;
1911
Gilles Peskine4747d192019-04-17 15:05:45 +02001912 if( slot == NULL )
1913 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001914
1915#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001916 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001917 * element, and the failure happened later (when saving metadata
1918 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001919 * element.
1920 * https://github.com/ARMmbed/mbed-crypto/issues/217
1921 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001922
Gilles Peskined7729582019-08-05 15:55:54 +02001923 /* Abort the ongoing transaction if any (there may not be one if
1924 * the creation process failed before starting one, or if the
1925 * key creation is a registration of a key in a secure element).
1926 * Earlier functions must already have done what it takes to undo any
1927 * partial creation. All that's left is to update the transaction data
1928 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001929 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001930#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1931
Gilles Peskine4747d192019-04-17 15:05:45 +02001932 psa_wipe_key_slot( slot );
1933}
1934
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001935/** Validate optional attributes during key creation.
1936 *
1937 * Some key attributes are optional during key creation. If they are
1938 * specified in the attributes structure, check that they are consistent
1939 * with the data in the slot.
1940 *
1941 * This function should be called near the end of key creation, after
1942 * the slot in memory is fully populated but before saving persistent data.
1943 */
1944static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001945 const psa_key_slot_t *slot,
1946 const psa_key_attributes_t *attributes )
1947{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001948 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001950 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951 return( PSA_ERROR_INVALID_ARGUMENT );
1952 }
1953
1954 if( attributes->domain_parameters_size != 0 )
1955 {
1956#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001957 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001958 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001959 mbedtls_rsa_context rsa;
1960 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
1961
1962 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
1963 if( status != PSA_SUCCESS )
1964 return status;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001965 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001966 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001967 mbedtls_mpi_init( &actual );
1968 mbedtls_mpi_init( &required );
Steven Cooremana01795d2020-07-24 22:48:15 +02001969 ret = mbedtls_rsa_export( &rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001970 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana01795d2020-07-24 22:48:15 +02001971 mbedtls_rsa_free( &rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001972 if( ret != 0 )
1973 goto rsa_exit;
1974 ret = mbedtls_mpi_read_binary( &required,
1975 attributes->domain_parameters,
1976 attributes->domain_parameters_size );
1977 if( ret != 0 )
1978 goto rsa_exit;
1979 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1980 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1981 rsa_exit:
1982 mbedtls_mpi_free( &actual );
1983 mbedtls_mpi_free( &required );
1984 if( ret != 0)
1985 return( mbedtls_to_psa_error( ret ) );
1986 }
1987 else
1988#endif
1989 {
1990 return( PSA_ERROR_INVALID_ARGUMENT );
1991 }
1992 }
1993
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001994 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001995 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001996 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001997 return( PSA_ERROR_INVALID_ARGUMENT );
1998 }
1999
2000 return( PSA_SUCCESS );
2001}
2002
Gilles Peskine4747d192019-04-17 15:05:45 +02002003psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002004 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002005 size_t data_length,
2006 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002007{
2008 psa_status_t status;
2009 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002010 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002011
Gilles Peskine0f84d622019-09-12 19:03:13 +02002012 /* Reject zero-length symmetric keys (including raw data key objects).
2013 * This also rejects any key which might be encoded as an empty string,
2014 * which is never valid. */
2015 if( data_length == 0 )
2016 return( PSA_ERROR_INVALID_ARGUMENT );
2017
Gilles Peskinedf179142019-07-15 22:02:14 +02002018 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2019 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020 if( status != PSA_SUCCESS )
2021 goto exit;
2022
Gilles Peskine5d309672019-07-12 23:47:28 +02002023#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2024 if( driver != NULL )
2025 {
2026 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002027 /* The driver should set the number of key bits, however in
2028 * case it doesn't, we initialize bits to an invalid value. */
2029 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002030 if( drv->key_management == NULL ||
2031 drv->key_management->p_import == NULL )
2032 {
2033 status = PSA_ERROR_NOT_SUPPORTED;
2034 goto exit;
2035 }
2036 status = drv->key_management->p_import(
2037 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002038 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002039 &bits );
2040 if( status != PSA_SUCCESS )
2041 goto exit;
2042 if( bits > PSA_MAX_KEY_BITS )
2043 {
2044 status = PSA_ERROR_NOT_SUPPORTED;
2045 goto exit;
2046 }
2047 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002048 }
2049 else
2050#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2051 {
2052 status = psa_import_key_into_slot( slot, data, data_length );
2053 if( status != PSA_SUCCESS )
2054 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002055 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002056 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002057 if( status != PSA_SUCCESS )
2058 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002059
Gilles Peskine011e4282019-06-26 18:34:38 +02002060 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002061exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002062 if( status != PSA_SUCCESS )
2063 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002064 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002065 *handle = 0;
2066 }
2067 return( status );
2068}
2069
Gilles Peskined7729582019-08-05 15:55:54 +02002070#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2071psa_status_t mbedtls_psa_register_se_key(
2072 const psa_key_attributes_t *attributes )
2073{
2074 psa_status_t status;
2075 psa_key_slot_t *slot = NULL;
2076 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002077 psa_key_handle_t handle = 0;
2078
2079 /* Leaving attributes unspecified is not currently supported.
2080 * It could make sense to query the key type and size from the
2081 * secure element, but not all secure elements support this
2082 * and the driver HAL doesn't currently support it. */
2083 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2084 return( PSA_ERROR_NOT_SUPPORTED );
2085 if( psa_get_key_bits( attributes ) == 0 )
2086 return( PSA_ERROR_NOT_SUPPORTED );
2087
2088 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2089 &handle, &slot, &driver );
2090 if( status != PSA_SUCCESS )
2091 goto exit;
2092
Gilles Peskined7729582019-08-05 15:55:54 +02002093 status = psa_finish_key_creation( slot, driver );
2094
2095exit:
2096 if( status != PSA_SUCCESS )
2097 {
2098 psa_fail_key_creation( slot, driver );
2099 }
2100 /* Registration doesn't keep the key in RAM. */
2101 psa_close_key( handle );
2102 return( status );
2103}
2104#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2105
Gilles Peskinef603c712019-01-19 13:40:11 +01002106static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002107 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002108{
2109 psa_status_t status;
2110 uint8_t *buffer = NULL;
2111 size_t buffer_size = 0;
2112 size_t length;
2113
Gilles Peskine8e338702019-07-30 20:06:31 +02002114 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002115 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002116 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002117 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002118 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002119 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2120 if( status != PSA_SUCCESS )
2121 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002122 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002123 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002124
2125exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002126 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002127 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002128 return( status );
2129}
2130
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2132 const psa_key_attributes_t *specified_attributes,
2133 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002134{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002135 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002136 psa_key_slot_t *source_slot = NULL;
2137 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002138 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002139 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002140
Gilles Peskine28f8f302019-07-24 13:30:31 +02002141 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002142 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002143 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002144 goto exit;
2145
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002146 status = psa_validate_optional_attributes( source_slot,
2147 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002148 if( status != PSA_SUCCESS )
2149 goto exit;
2150
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002151 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002152 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002153 if( status != PSA_SUCCESS )
2154 goto exit;
2155
Gilles Peskinedf179142019-07-15 22:02:14 +02002156 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2157 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002158 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002159 if( status != PSA_SUCCESS )
2160 goto exit;
2161
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002162#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2163 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002164 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002165 /* Copying to a secure element is not implemented yet. */
2166 status = PSA_ERROR_NOT_SUPPORTED;
2167 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002168 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002169#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002170
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002171 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002172 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002173 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002174
Gilles Peskine011e4282019-06-26 18:34:38 +02002175 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002176exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002177 if( status != PSA_SUCCESS )
2178 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002179 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002180 *target_handle = 0;
2181 }
2182 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002183}
2184
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002185
2186
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002187/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002188/* Message digests */
2189/****************************************************************/
2190
Gilles Peskineb16841e2019-10-10 20:36:12 +02002191#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002192static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002193{
2194 switch( alg )
2195 {
2196#if defined(MBEDTLS_MD2_C)
2197 case PSA_ALG_MD2:
2198 return( &mbedtls_md2_info );
2199#endif
2200#if defined(MBEDTLS_MD4_C)
2201 case PSA_ALG_MD4:
2202 return( &mbedtls_md4_info );
2203#endif
2204#if defined(MBEDTLS_MD5_C)
2205 case PSA_ALG_MD5:
2206 return( &mbedtls_md5_info );
2207#endif
2208#if defined(MBEDTLS_RIPEMD160_C)
2209 case PSA_ALG_RIPEMD160:
2210 return( &mbedtls_ripemd160_info );
2211#endif
2212#if defined(MBEDTLS_SHA1_C)
2213 case PSA_ALG_SHA_1:
2214 return( &mbedtls_sha1_info );
2215#endif
2216#if defined(MBEDTLS_SHA256_C)
2217 case PSA_ALG_SHA_224:
2218 return( &mbedtls_sha224_info );
2219 case PSA_ALG_SHA_256:
2220 return( &mbedtls_sha256_info );
2221#endif
2222#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002223#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002224 case PSA_ALG_SHA_384:
2225 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002226#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002227 case PSA_ALG_SHA_512:
2228 return( &mbedtls_sha512_info );
2229#endif
2230 default:
2231 return( NULL );
2232 }
2233}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002234#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002235
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002236psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2237{
2238 switch( operation->alg )
2239 {
Gilles Peskine81736312018-06-26 15:04:31 +02002240 case 0:
2241 /* The object has (apparently) been initialized but it is not
2242 * in use. It's ok to call abort on such an object, and there's
2243 * nothing to do. */
2244 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002245#if defined(MBEDTLS_MD2_C)
2246 case PSA_ALG_MD2:
2247 mbedtls_md2_free( &operation->ctx.md2 );
2248 break;
2249#endif
2250#if defined(MBEDTLS_MD4_C)
2251 case PSA_ALG_MD4:
2252 mbedtls_md4_free( &operation->ctx.md4 );
2253 break;
2254#endif
2255#if defined(MBEDTLS_MD5_C)
2256 case PSA_ALG_MD5:
2257 mbedtls_md5_free( &operation->ctx.md5 );
2258 break;
2259#endif
2260#if defined(MBEDTLS_RIPEMD160_C)
2261 case PSA_ALG_RIPEMD160:
2262 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2263 break;
2264#endif
2265#if defined(MBEDTLS_SHA1_C)
2266 case PSA_ALG_SHA_1:
2267 mbedtls_sha1_free( &operation->ctx.sha1 );
2268 break;
2269#endif
2270#if defined(MBEDTLS_SHA256_C)
2271 case PSA_ALG_SHA_224:
2272 case PSA_ALG_SHA_256:
2273 mbedtls_sha256_free( &operation->ctx.sha256 );
2274 break;
2275#endif
2276#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002277#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002278 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002279#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280 case PSA_ALG_SHA_512:
2281 mbedtls_sha512_free( &operation->ctx.sha512 );
2282 break;
2283#endif
2284 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002285 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286 }
2287 operation->alg = 0;
2288 return( PSA_SUCCESS );
2289}
2290
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002291psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292 psa_algorithm_t alg )
2293{
Janos Follath24eed8d2019-11-22 13:21:35 +00002294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002295
2296 /* A context must be freshly initialized before it can be set up. */
2297 if( operation->alg != 0 )
2298 {
2299 return( PSA_ERROR_BAD_STATE );
2300 }
2301
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002302 switch( alg )
2303 {
2304#if defined(MBEDTLS_MD2_C)
2305 case PSA_ALG_MD2:
2306 mbedtls_md2_init( &operation->ctx.md2 );
2307 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2308 break;
2309#endif
2310#if defined(MBEDTLS_MD4_C)
2311 case PSA_ALG_MD4:
2312 mbedtls_md4_init( &operation->ctx.md4 );
2313 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2314 break;
2315#endif
2316#if defined(MBEDTLS_MD5_C)
2317 case PSA_ALG_MD5:
2318 mbedtls_md5_init( &operation->ctx.md5 );
2319 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2320 break;
2321#endif
2322#if defined(MBEDTLS_RIPEMD160_C)
2323 case PSA_ALG_RIPEMD160:
2324 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2325 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2326 break;
2327#endif
2328#if defined(MBEDTLS_SHA1_C)
2329 case PSA_ALG_SHA_1:
2330 mbedtls_sha1_init( &operation->ctx.sha1 );
2331 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2332 break;
2333#endif
2334#if defined(MBEDTLS_SHA256_C)
2335 case PSA_ALG_SHA_224:
2336 mbedtls_sha256_init( &operation->ctx.sha256 );
2337 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2338 break;
2339 case PSA_ALG_SHA_256:
2340 mbedtls_sha256_init( &operation->ctx.sha256 );
2341 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2342 break;
2343#endif
2344#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002345#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 case PSA_ALG_SHA_384:
2347 mbedtls_sha512_init( &operation->ctx.sha512 );
2348 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2349 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002350#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351 case PSA_ALG_SHA_512:
2352 mbedtls_sha512_init( &operation->ctx.sha512 );
2353 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2354 break;
2355#endif
2356 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002357 return( PSA_ALG_IS_HASH( alg ) ?
2358 PSA_ERROR_NOT_SUPPORTED :
2359 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360 }
2361 if( ret == 0 )
2362 operation->alg = alg;
2363 else
2364 psa_hash_abort( operation );
2365 return( mbedtls_to_psa_error( ret ) );
2366}
2367
2368psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2369 const uint8_t *input,
2370 size_t input_length )
2371{
Janos Follath24eed8d2019-11-22 13:21:35 +00002372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002373
2374 /* Don't require hash implementations to behave correctly on a
2375 * zero-length input, which may have an invalid pointer. */
2376 if( input_length == 0 )
2377 return( PSA_SUCCESS );
2378
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002379 switch( operation->alg )
2380 {
2381#if defined(MBEDTLS_MD2_C)
2382 case PSA_ALG_MD2:
2383 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2384 input, input_length );
2385 break;
2386#endif
2387#if defined(MBEDTLS_MD4_C)
2388 case PSA_ALG_MD4:
2389 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2390 input, input_length );
2391 break;
2392#endif
2393#if defined(MBEDTLS_MD5_C)
2394 case PSA_ALG_MD5:
2395 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2396 input, input_length );
2397 break;
2398#endif
2399#if defined(MBEDTLS_RIPEMD160_C)
2400 case PSA_ALG_RIPEMD160:
2401 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2402 input, input_length );
2403 break;
2404#endif
2405#if defined(MBEDTLS_SHA1_C)
2406 case PSA_ALG_SHA_1:
2407 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2408 input, input_length );
2409 break;
2410#endif
2411#if defined(MBEDTLS_SHA256_C)
2412 case PSA_ALG_SHA_224:
2413 case PSA_ALG_SHA_256:
2414 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2415 input, input_length );
2416 break;
2417#endif
2418#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002419#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002420 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002421#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002422 case PSA_ALG_SHA_512:
2423 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2424 input, input_length );
2425 break;
2426#endif
2427 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002428 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002429 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002430
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002431 if( ret != 0 )
2432 psa_hash_abort( operation );
2433 return( mbedtls_to_psa_error( ret ) );
2434}
2435
2436psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2437 uint8_t *hash,
2438 size_t hash_size,
2439 size_t *hash_length )
2440{
itayzafrir40835d42018-08-02 13:14:17 +03002441 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002443 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002444
2445 /* Fill the output buffer with something that isn't a valid hash
2446 * (barring an attack on the hash and deliberately-crafted input),
2447 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002448 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002449 /* If hash_size is 0 then hash may be NULL and then the
2450 * call to memset would have undefined behavior. */
2451 if( hash_size != 0 )
2452 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002453
2454 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002455 {
2456 status = PSA_ERROR_BUFFER_TOO_SMALL;
2457 goto exit;
2458 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002459
2460 switch( operation->alg )
2461 {
2462#if defined(MBEDTLS_MD2_C)
2463 case PSA_ALG_MD2:
2464 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2465 break;
2466#endif
2467#if defined(MBEDTLS_MD4_C)
2468 case PSA_ALG_MD4:
2469 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2470 break;
2471#endif
2472#if defined(MBEDTLS_MD5_C)
2473 case PSA_ALG_MD5:
2474 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2475 break;
2476#endif
2477#if defined(MBEDTLS_RIPEMD160_C)
2478 case PSA_ALG_RIPEMD160:
2479 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2480 break;
2481#endif
2482#if defined(MBEDTLS_SHA1_C)
2483 case PSA_ALG_SHA_1:
2484 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2485 break;
2486#endif
2487#if defined(MBEDTLS_SHA256_C)
2488 case PSA_ALG_SHA_224:
2489 case PSA_ALG_SHA_256:
2490 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2491 break;
2492#endif
2493#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002494#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002495 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002496#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002497 case PSA_ALG_SHA_512:
2498 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2499 break;
2500#endif
2501 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002502 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002503 }
itayzafrir40835d42018-08-02 13:14:17 +03002504 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002505
itayzafrir40835d42018-08-02 13:14:17 +03002506exit:
2507 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002508 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002509 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002510 return( psa_hash_abort( operation ) );
2511 }
2512 else
2513 {
2514 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002515 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002516 }
2517}
2518
Gilles Peskine2d277862018-06-18 15:41:12 +02002519psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2520 const uint8_t *hash,
2521 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002522{
2523 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2524 size_t actual_hash_length;
2525 psa_status_t status = psa_hash_finish( operation,
2526 actual_hash, sizeof( actual_hash ),
2527 &actual_hash_length );
2528 if( status != PSA_SUCCESS )
2529 return( status );
2530 if( actual_hash_length != hash_length )
2531 return( PSA_ERROR_INVALID_SIGNATURE );
2532 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2533 return( PSA_ERROR_INVALID_SIGNATURE );
2534 return( PSA_SUCCESS );
2535}
2536
Gilles Peskine0a749c82019-11-28 19:33:58 +01002537psa_status_t psa_hash_compute( psa_algorithm_t alg,
2538 const uint8_t *input, size_t input_length,
2539 uint8_t *hash, size_t hash_size,
2540 size_t *hash_length )
2541{
2542 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2543 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2544
2545 *hash_length = hash_size;
2546 status = psa_hash_setup( &operation, alg );
2547 if( status != PSA_SUCCESS )
2548 goto exit;
2549 status = psa_hash_update( &operation, input, input_length );
2550 if( status != PSA_SUCCESS )
2551 goto exit;
2552 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2553 if( status != PSA_SUCCESS )
2554 goto exit;
2555
2556exit:
2557 if( status == PSA_SUCCESS )
2558 status = psa_hash_abort( &operation );
2559 else
2560 psa_hash_abort( &operation );
2561 return( status );
2562}
2563
2564psa_status_t psa_hash_compare( psa_algorithm_t alg,
2565 const uint8_t *input, size_t input_length,
2566 const uint8_t *hash, size_t hash_length )
2567{
2568 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2569 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2570
2571 status = psa_hash_setup( &operation, alg );
2572 if( status != PSA_SUCCESS )
2573 goto exit;
2574 status = psa_hash_update( &operation, input, input_length );
2575 if( status != PSA_SUCCESS )
2576 goto exit;
2577 status = psa_hash_verify( &operation, hash, hash_length );
2578 if( status != PSA_SUCCESS )
2579 goto exit;
2580
2581exit:
2582 if( status == PSA_SUCCESS )
2583 status = psa_hash_abort( &operation );
2584 else
2585 psa_hash_abort( &operation );
2586 return( status );
2587}
2588
Gilles Peskineeb35d782019-01-22 17:56:16 +01002589psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2590 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002591{
2592 if( target_operation->alg != 0 )
2593 return( PSA_ERROR_BAD_STATE );
2594
2595 switch( source_operation->alg )
2596 {
2597 case 0:
2598 return( PSA_ERROR_BAD_STATE );
2599#if defined(MBEDTLS_MD2_C)
2600 case PSA_ALG_MD2:
2601 mbedtls_md2_clone( &target_operation->ctx.md2,
2602 &source_operation->ctx.md2 );
2603 break;
2604#endif
2605#if defined(MBEDTLS_MD4_C)
2606 case PSA_ALG_MD4:
2607 mbedtls_md4_clone( &target_operation->ctx.md4,
2608 &source_operation->ctx.md4 );
2609 break;
2610#endif
2611#if defined(MBEDTLS_MD5_C)
2612 case PSA_ALG_MD5:
2613 mbedtls_md5_clone( &target_operation->ctx.md5,
2614 &source_operation->ctx.md5 );
2615 break;
2616#endif
2617#if defined(MBEDTLS_RIPEMD160_C)
2618 case PSA_ALG_RIPEMD160:
2619 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2620 &source_operation->ctx.ripemd160 );
2621 break;
2622#endif
2623#if defined(MBEDTLS_SHA1_C)
2624 case PSA_ALG_SHA_1:
2625 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2626 &source_operation->ctx.sha1 );
2627 break;
2628#endif
2629#if defined(MBEDTLS_SHA256_C)
2630 case PSA_ALG_SHA_224:
2631 case PSA_ALG_SHA_256:
2632 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2633 &source_operation->ctx.sha256 );
2634 break;
2635#endif
2636#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002637#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002638 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002639#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002640 case PSA_ALG_SHA_512:
2641 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2642 &source_operation->ctx.sha512 );
2643 break;
2644#endif
2645 default:
2646 return( PSA_ERROR_NOT_SUPPORTED );
2647 }
2648
2649 target_operation->alg = source_operation->alg;
2650 return( PSA_SUCCESS );
2651}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002652
2653
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002654/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002655/* MAC */
2656/****************************************************************/
2657
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002658static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002659 psa_algorithm_t alg,
2660 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002661 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002662 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002663{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002665 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002666
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002667 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002668 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002669
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2671 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002672 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002674 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002675 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002676 mode = MBEDTLS_MODE_STREAM;
2677 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002678 case PSA_ALG_CTR:
2679 mode = MBEDTLS_MODE_CTR;
2680 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002681 case PSA_ALG_CFB:
2682 mode = MBEDTLS_MODE_CFB;
2683 break;
2684 case PSA_ALG_OFB:
2685 mode = MBEDTLS_MODE_OFB;
2686 break;
2687 case PSA_ALG_CBC_NO_PADDING:
2688 mode = MBEDTLS_MODE_CBC;
2689 break;
2690 case PSA_ALG_CBC_PKCS7:
2691 mode = MBEDTLS_MODE_CBC;
2692 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002693 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694 mode = MBEDTLS_MODE_CCM;
2695 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002696 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002697 mode = MBEDTLS_MODE_GCM;
2698 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002699 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2700 mode = MBEDTLS_MODE_CHACHAPOLY;
2701 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002702 default:
2703 return( NULL );
2704 }
2705 }
2706 else if( alg == PSA_ALG_CMAC )
2707 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708 else
2709 return( NULL );
2710
2711 switch( key_type )
2712 {
2713 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002714 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715 break;
2716 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002717 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2718 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002719 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002720 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002721 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002722 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002723 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2724 * but two-key Triple-DES is functionally three-key Triple-DES
2725 * with K1=K3, so that's how we present it to mbedtls. */
2726 if( key_bits == 128 )
2727 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728 break;
2729 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002730 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002731 break;
2732 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002733 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002734 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002735 case PSA_KEY_TYPE_CHACHA20:
2736 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2737 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002738 default:
2739 return( NULL );
2740 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002741 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002742 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743
Jaeden Amero23bbb752018-06-26 14:16:54 +01002744 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2745 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746}
2747
Gilles Peskinea05219c2018-11-16 16:02:56 +01002748#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002749static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002750{
Gilles Peskine2d277862018-06-18 15:41:12 +02002751 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002752 {
2753 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002754 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002755 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002756 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002757 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002758 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002759 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002760 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002761 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002762 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002763 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002764 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002765 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002766 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002767 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002768 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002769 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002770 return( 128 );
2771 default:
2772 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002773 }
2774}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002775#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002776
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002777/* Initialize the MAC operation structure. Once this function has been
2778 * called, psa_mac_abort can run and will do the right thing. */
2779static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2780 psa_algorithm_t alg )
2781{
2782 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2783
2784 operation->alg = alg;
2785 operation->key_set = 0;
2786 operation->iv_set = 0;
2787 operation->iv_required = 0;
2788 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002789 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002790
2791#if defined(MBEDTLS_CMAC_C)
2792 if( alg == PSA_ALG_CMAC )
2793 {
2794 operation->iv_required = 0;
2795 mbedtls_cipher_init( &operation->ctx.cmac );
2796 status = PSA_SUCCESS;
2797 }
2798 else
2799#endif /* MBEDTLS_CMAC_C */
2800#if defined(MBEDTLS_MD_C)
2801 if( PSA_ALG_IS_HMAC( operation->alg ) )
2802 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002803 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2804 operation->ctx.hmac.hash_ctx.alg = 0;
2805 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002806 }
2807 else
2808#endif /* MBEDTLS_MD_C */
2809 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002810 if( ! PSA_ALG_IS_MAC( alg ) )
2811 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002812 }
2813
2814 if( status != PSA_SUCCESS )
2815 memset( operation, 0, sizeof( *operation ) );
2816 return( status );
2817}
2818
Gilles Peskine01126fa2018-07-12 17:04:55 +02002819#if defined(MBEDTLS_MD_C)
2820static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2821{
Gilles Peskine3f108122018-12-07 18:14:53 +01002822 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002823 return( psa_hash_abort( &hmac->hash_ctx ) );
2824}
2825#endif /* MBEDTLS_MD_C */
2826
Gilles Peskine8c9def32018-02-08 10:02:12 +01002827psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2828{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002829 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002830 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002831 /* The object has (apparently) been initialized but it is not
2832 * in use. It's ok to call abort on such an object, and there's
2833 * nothing to do. */
2834 return( PSA_SUCCESS );
2835 }
2836 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002837#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002838 if( operation->alg == PSA_ALG_CMAC )
2839 {
2840 mbedtls_cipher_free( &operation->ctx.cmac );
2841 }
2842 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002843#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002844#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002845 if( PSA_ALG_IS_HMAC( operation->alg ) )
2846 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002847 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002848 }
2849 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002851 {
2852 /* Sanity check (shouldn't happen: operation->alg should
2853 * always have been initialized to a valid value). */
2854 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002855 }
Moran Peker41deec42018-04-04 15:43:05 +03002856
Gilles Peskine8c9def32018-02-08 10:02:12 +01002857 operation->alg = 0;
2858 operation->key_set = 0;
2859 operation->iv_set = 0;
2860 operation->iv_required = 0;
2861 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002862 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002863
Gilles Peskine8c9def32018-02-08 10:02:12 +01002864 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002865
2866bad_state:
2867 /* If abort is called on an uninitialized object, we can't trust
2868 * anything. Wipe the object in case it contains confidential data.
2869 * This may result in a memory leak if a pointer gets overwritten,
2870 * but it's too late to do anything about this. */
2871 memset( operation, 0, sizeof( *operation ) );
2872 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002873}
2874
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002875#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002876static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002877 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002878 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002879 const mbedtls_cipher_info_t *cipher_info )
2880{
Janos Follath24eed8d2019-11-22 13:21:35 +00002881 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002882
2883 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002884
2885 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2886 if( ret != 0 )
2887 return( ret );
2888
2889 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002890 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002891 key_bits );
2892 return( ret );
2893}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002894#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002895
Gilles Peskine248051a2018-06-20 16:09:38 +02002896#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002897static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2898 const uint8_t *key,
2899 size_t key_length,
2900 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002901{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002902 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002903 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002904 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002905 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002906 psa_status_t status;
2907
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002908 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2909 * overflow below. This should never trigger if the hash algorithm
2910 * is implemented correctly. */
2911 /* The size checks against the ipad and opad buffers cannot be written
2912 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2913 * because that triggers -Wlogical-op on GCC 7.3. */
2914 if( block_size > sizeof( ipad ) )
2915 return( PSA_ERROR_NOT_SUPPORTED );
2916 if( block_size > sizeof( hmac->opad ) )
2917 return( PSA_ERROR_NOT_SUPPORTED );
2918 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002919 return( PSA_ERROR_NOT_SUPPORTED );
2920
Gilles Peskined223b522018-06-11 18:12:58 +02002921 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002922 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002923 status = psa_hash_compute( hash_alg, key, key_length,
2924 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002925 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002926 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002927 }
Gilles Peskine96889972018-07-12 17:07:03 +02002928 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2929 * but it is permitted. It is common when HMAC is used in HKDF, for
2930 * example. Don't call `memcpy` in the 0-length because `key` could be
2931 * an invalid pointer which would make the behavior undefined. */
2932 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002933 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002934
Gilles Peskined223b522018-06-11 18:12:58 +02002935 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2936 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002937 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002938 ipad[i] ^= 0x36;
2939 memset( ipad + key_length, 0x36, block_size - key_length );
2940
2941 /* Copy the key material from ipad to opad, flipping the requisite bits,
2942 * and filling the rest of opad with the requisite constant. */
2943 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002944 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2945 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002946
Gilles Peskine01126fa2018-07-12 17:04:55 +02002947 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002948 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002949 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002950
Gilles Peskine01126fa2018-07-12 17:04:55 +02002951 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002952
2953cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002954 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002955
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002956 return( status );
2957}
Gilles Peskine248051a2018-06-20 16:09:38 +02002958#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002959
Gilles Peskine89167cb2018-07-08 20:12:23 +02002960static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002961 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002962 psa_algorithm_t alg,
2963 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002964{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002966 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002967 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002968 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002969 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002970 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002971 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002972
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002973 /* A context must be freshly initialized before it can be set up. */
2974 if( operation->alg != 0 )
2975 {
2976 return( PSA_ERROR_BAD_STATE );
2977 }
2978
Gilles Peskined911eb72018-08-14 15:18:45 +02002979 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002980 if( status != PSA_SUCCESS )
2981 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002982 if( is_sign )
2983 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002984
Gilles Peskine28f8f302019-07-24 13:30:31 +02002985 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002986 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002987 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002988 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002989
Gilles Peskine8c9def32018-02-08 10:02:12 +01002990#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002991 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002992 {
2993 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002994 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002995 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002996 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002997 if( cipher_info == NULL )
2998 {
2999 status = PSA_ERROR_NOT_SUPPORTED;
3000 goto exit;
3001 }
3002 operation->mac_size = cipher_info->block_size;
3003 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3004 status = mbedtls_to_psa_error( ret );
3005 }
3006 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003007#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003009 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003010 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003011 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003012 if( hash_alg == 0 )
3013 {
3014 status = PSA_ERROR_NOT_SUPPORTED;
3015 goto exit;
3016 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003017
3018 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3019 /* Sanity check. This shouldn't fail on a valid configuration. */
3020 if( operation->mac_size == 0 ||
3021 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3022 {
3023 status = PSA_ERROR_NOT_SUPPORTED;
3024 goto exit;
3025 }
3026
Gilles Peskine8e338702019-07-30 20:06:31 +02003027 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003028 {
3029 status = PSA_ERROR_INVALID_ARGUMENT;
3030 goto exit;
3031 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003032
Gilles Peskine01126fa2018-07-12 17:04:55 +02003033 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003034 slot->data.key.data,
3035 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003036 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003037 }
3038 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003039#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003040 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003041 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003042 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003044
Gilles Peskined911eb72018-08-14 15:18:45 +02003045 if( truncated == 0 )
3046 {
3047 /* The "normal" case: untruncated algorithm. Nothing to do. */
3048 }
3049 else if( truncated < 4 )
3050 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003051 /* A very short MAC is too short for security since it can be
3052 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3053 * so we make this our minimum, even though 32 bits is still
3054 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003055 status = PSA_ERROR_NOT_SUPPORTED;
3056 }
3057 else if( truncated > operation->mac_size )
3058 {
3059 /* It's impossible to "truncate" to a larger length. */
3060 status = PSA_ERROR_INVALID_ARGUMENT;
3061 }
3062 else
3063 operation->mac_size = truncated;
3064
Gilles Peskinefbfac682018-07-08 20:51:54 +02003065exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003066 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003067 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003068 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003069 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003070 else
3071 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003072 operation->key_set = 1;
3073 }
3074 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003075}
3076
Gilles Peskine89167cb2018-07-08 20:12:23 +02003077psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003078 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003079 psa_algorithm_t alg )
3080{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003081 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003082}
3083
3084psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003085 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003086 psa_algorithm_t alg )
3087{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003088 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003089}
3090
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3092 const uint8_t *input,
3093 size_t input_length )
3094{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003095 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003096 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003097 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003098 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003099 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003100 operation->has_input = 1;
3101
Gilles Peskine8c9def32018-02-08 10:02:12 +01003102#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003103 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003104 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003105 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3106 input, input_length );
3107 status = mbedtls_to_psa_error( ret );
3108 }
3109 else
3110#endif /* MBEDTLS_CMAC_C */
3111#if defined(MBEDTLS_MD_C)
3112 if( PSA_ALG_IS_HMAC( operation->alg ) )
3113 {
3114 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3115 input_length );
3116 }
3117 else
3118#endif /* MBEDTLS_MD_C */
3119 {
3120 /* This shouldn't happen if `operation` was initialized by
3121 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003122 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003123 }
3124
Gilles Peskinefbfac682018-07-08 20:51:54 +02003125 if( status != PSA_SUCCESS )
3126 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003127 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003128}
3129
Gilles Peskine01126fa2018-07-12 17:04:55 +02003130#if defined(MBEDTLS_MD_C)
3131static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3132 uint8_t *mac,
3133 size_t mac_size )
3134{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003135 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003136 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3137 size_t hash_size = 0;
3138 size_t block_size = psa_get_hash_block_size( hash_alg );
3139 psa_status_t status;
3140
Gilles Peskine01126fa2018-07-12 17:04:55 +02003141 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3142 if( status != PSA_SUCCESS )
3143 return( status );
3144 /* From here on, tmp needs to be wiped. */
3145
3146 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3147 if( status != PSA_SUCCESS )
3148 goto exit;
3149
3150 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3151 if( status != PSA_SUCCESS )
3152 goto exit;
3153
3154 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3155 if( status != PSA_SUCCESS )
3156 goto exit;
3157
Gilles Peskined911eb72018-08-14 15:18:45 +02003158 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3159 if( status != PSA_SUCCESS )
3160 goto exit;
3161
3162 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003163
3164exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003165 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003166 return( status );
3167}
3168#endif /* MBEDTLS_MD_C */
3169
mohammad16036df908f2018-04-02 08:34:15 -07003170static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003171 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003172 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003173{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003174 if( ! operation->key_set )
3175 return( PSA_ERROR_BAD_STATE );
3176 if( operation->iv_required && ! operation->iv_set )
3177 return( PSA_ERROR_BAD_STATE );
3178
Gilles Peskine8c9def32018-02-08 10:02:12 +01003179 if( mac_size < operation->mac_size )
3180 return( PSA_ERROR_BUFFER_TOO_SMALL );
3181
Gilles Peskine8c9def32018-02-08 10:02:12 +01003182#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003183 if( operation->alg == PSA_ALG_CMAC )
3184 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003185 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3186 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3187 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003188 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003189 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003190 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003191 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003192 else
3193#endif /* MBEDTLS_CMAC_C */
3194#if defined(MBEDTLS_MD_C)
3195 if( PSA_ALG_IS_HMAC( operation->alg ) )
3196 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003197 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003198 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003199 }
3200 else
3201#endif /* MBEDTLS_MD_C */
3202 {
3203 /* This shouldn't happen if `operation` was initialized by
3204 * a setup function. */
3205 return( PSA_ERROR_BAD_STATE );
3206 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003207}
3208
Gilles Peskineacd4be32018-07-08 19:56:25 +02003209psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3210 uint8_t *mac,
3211 size_t mac_size,
3212 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003213{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003214 psa_status_t status;
3215
Jaeden Amero252ef282019-02-15 14:05:35 +00003216 if( operation->alg == 0 )
3217 {
3218 return( PSA_ERROR_BAD_STATE );
3219 }
3220
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003221 /* Fill the output buffer with something that isn't a valid mac
3222 * (barring an attack on the mac and deliberately-crafted input),
3223 * in case the caller doesn't check the return status properly. */
3224 *mac_length = mac_size;
3225 /* If mac_size is 0 then mac may be NULL and then the
3226 * call to memset would have undefined behavior. */
3227 if( mac_size != 0 )
3228 memset( mac, '!', mac_size );
3229
Gilles Peskine89167cb2018-07-08 20:12:23 +02003230 if( ! operation->is_sign )
3231 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003232 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003233 }
mohammad16036df908f2018-04-02 08:34:15 -07003234
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003235 status = psa_mac_finish_internal( operation, mac, mac_size );
3236
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003237 if( status == PSA_SUCCESS )
3238 {
3239 status = psa_mac_abort( operation );
3240 if( status == PSA_SUCCESS )
3241 *mac_length = operation->mac_size;
3242 else
3243 memset( mac, '!', mac_size );
3244 }
3245 else
3246 psa_mac_abort( operation );
3247 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003248}
3249
Gilles Peskineacd4be32018-07-08 19:56:25 +02003250psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3251 const uint8_t *mac,
3252 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003253{
Gilles Peskine828ed142018-06-18 23:25:51 +02003254 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003255 psa_status_t status;
3256
Jaeden Amero252ef282019-02-15 14:05:35 +00003257 if( operation->alg == 0 )
3258 {
3259 return( PSA_ERROR_BAD_STATE );
3260 }
3261
Gilles Peskine89167cb2018-07-08 20:12:23 +02003262 if( operation->is_sign )
3263 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003264 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003265 }
3266 if( operation->mac_size != mac_length )
3267 {
3268 status = PSA_ERROR_INVALID_SIGNATURE;
3269 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003270 }
mohammad16036df908f2018-04-02 08:34:15 -07003271
3272 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003273 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003274 if( status != PSA_SUCCESS )
3275 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003276
3277 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3278 status = PSA_ERROR_INVALID_SIGNATURE;
3279
3280cleanup:
3281 if( status == PSA_SUCCESS )
3282 status = psa_mac_abort( operation );
3283 else
3284 psa_mac_abort( operation );
3285
Gilles Peskine3f108122018-12-07 18:14:53 +01003286 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003287
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003288 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003289}
3290
3291
Gilles Peskine20035e32018-02-03 22:44:14 +01003292
Gilles Peskine20035e32018-02-03 22:44:14 +01003293/****************************************************************/
3294/* Asymmetric cryptography */
3295/****************************************************************/
3296
Gilles Peskine2b450e32018-06-27 15:42:46 +02003297#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003298/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003299 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003300static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3301 size_t hash_length,
3302 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003303{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003304 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003305 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003306 *md_alg = mbedtls_md_get_type( md_info );
3307
3308 /* The Mbed TLS RSA module uses an unsigned int for hash length
3309 * parameters. Validate that it fits so that we don't risk an
3310 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003311#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003312 if( hash_length > UINT_MAX )
3313 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003314#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003315
3316#if defined(MBEDTLS_PKCS1_V15)
3317 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3318 * must be correct. */
3319 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3320 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003321 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003322 if( md_info == NULL )
3323 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003324 if( mbedtls_md_get_size( md_info ) != hash_length )
3325 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003326 }
3327#endif /* MBEDTLS_PKCS1_V15 */
3328
3329#if defined(MBEDTLS_PKCS1_V21)
3330 /* PSS requires a hash internally. */
3331 if( PSA_ALG_IS_RSA_PSS( alg ) )
3332 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003333 if( md_info == NULL )
3334 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003335 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003336#endif /* MBEDTLS_PKCS1_V21 */
3337
Gilles Peskine61b91d42018-06-08 16:09:36 +02003338 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003339}
3340
Gilles Peskine2b450e32018-06-27 15:42:46 +02003341static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3342 psa_algorithm_t alg,
3343 const uint8_t *hash,
3344 size_t hash_length,
3345 uint8_t *signature,
3346 size_t signature_size,
3347 size_t *signature_length )
3348{
3349 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003351 mbedtls_md_type_t md_alg;
3352
3353 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3354 if( status != PSA_SUCCESS )
3355 return( status );
3356
Gilles Peskine630a18a2018-06-29 17:49:35 +02003357 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003358 return( PSA_ERROR_BUFFER_TOO_SMALL );
3359
3360#if defined(MBEDTLS_PKCS1_V15)
3361 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3362 {
3363 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3364 MBEDTLS_MD_NONE );
3365 ret = mbedtls_rsa_pkcs1_sign( rsa,
3366 mbedtls_ctr_drbg_random,
3367 &global_data.ctr_drbg,
3368 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003369 md_alg,
3370 (unsigned int) hash_length,
3371 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003372 signature );
3373 }
3374 else
3375#endif /* MBEDTLS_PKCS1_V15 */
3376#if defined(MBEDTLS_PKCS1_V21)
3377 if( PSA_ALG_IS_RSA_PSS( alg ) )
3378 {
3379 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3380 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3381 mbedtls_ctr_drbg_random,
3382 &global_data.ctr_drbg,
3383 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003384 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003385 (unsigned int) hash_length,
3386 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003387 signature );
3388 }
3389 else
3390#endif /* MBEDTLS_PKCS1_V21 */
3391 {
3392 return( PSA_ERROR_INVALID_ARGUMENT );
3393 }
3394
3395 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003396 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003397 return( mbedtls_to_psa_error( ret ) );
3398}
3399
3400static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3401 psa_algorithm_t alg,
3402 const uint8_t *hash,
3403 size_t hash_length,
3404 const uint8_t *signature,
3405 size_t signature_length )
3406{
3407 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003409 mbedtls_md_type_t md_alg;
3410
3411 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3412 if( status != PSA_SUCCESS )
3413 return( status );
3414
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003415 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3416 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003417
3418#if defined(MBEDTLS_PKCS1_V15)
3419 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3420 {
3421 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3422 MBEDTLS_MD_NONE );
3423 ret = mbedtls_rsa_pkcs1_verify( rsa,
3424 mbedtls_ctr_drbg_random,
3425 &global_data.ctr_drbg,
3426 MBEDTLS_RSA_PUBLIC,
3427 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003428 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003429 hash,
3430 signature );
3431 }
3432 else
3433#endif /* MBEDTLS_PKCS1_V15 */
3434#if defined(MBEDTLS_PKCS1_V21)
3435 if( PSA_ALG_IS_RSA_PSS( alg ) )
3436 {
3437 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3438 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3439 mbedtls_ctr_drbg_random,
3440 &global_data.ctr_drbg,
3441 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003442 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003443 (unsigned int) hash_length,
3444 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003445 signature );
3446 }
3447 else
3448#endif /* MBEDTLS_PKCS1_V21 */
3449 {
3450 return( PSA_ERROR_INVALID_ARGUMENT );
3451 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003452
3453 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3454 * the rest of the signature is invalid". This has little use in
3455 * practice and PSA doesn't report this distinction. */
3456 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3457 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003458 return( mbedtls_to_psa_error( ret ) );
3459}
3460#endif /* MBEDTLS_RSA_C */
3461
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003462#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003463/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3464 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3465 * (even though these functions don't modify it). */
3466static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3467 psa_algorithm_t alg,
3468 const uint8_t *hash,
3469 size_t hash_length,
3470 uint8_t *signature,
3471 size_t signature_size,
3472 size_t *signature_length )
3473{
Janos Follath24eed8d2019-11-22 13:21:35 +00003474 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003475 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003476 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003477 mbedtls_mpi_init( &r );
3478 mbedtls_mpi_init( &s );
3479
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003480 if( signature_size < 2 * curve_bytes )
3481 {
3482 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3483 goto cleanup;
3484 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003485
Gilles Peskinea05219c2018-11-16 16:02:56 +01003486#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003487 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3488 {
3489 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3490 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3491 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003492 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3493 &ecp->d, hash,
3494 hash_length, md_alg,
3495 mbedtls_ctr_drbg_random,
3496 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003497 }
3498 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003499#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003500 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003501 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003502 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3503 hash, hash_length,
3504 mbedtls_ctr_drbg_random,
3505 &global_data.ctr_drbg ) );
3506 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003507
3508 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3509 signature,
3510 curve_bytes ) );
3511 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3512 signature + curve_bytes,
3513 curve_bytes ) );
3514
3515cleanup:
3516 mbedtls_mpi_free( &r );
3517 mbedtls_mpi_free( &s );
3518 if( ret == 0 )
3519 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003520 return( mbedtls_to_psa_error( ret ) );
3521}
3522
3523static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3524 const uint8_t *hash,
3525 size_t hash_length,
3526 const uint8_t *signature,
3527 size_t signature_length )
3528{
Janos Follath24eed8d2019-11-22 13:21:35 +00003529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003530 mbedtls_mpi r, s;
3531 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3532 mbedtls_mpi_init( &r );
3533 mbedtls_mpi_init( &s );
3534
3535 if( signature_length != 2 * curve_bytes )
3536 return( PSA_ERROR_INVALID_SIGNATURE );
3537
3538 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3539 signature,
3540 curve_bytes ) );
3541 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3542 signature + curve_bytes,
3543 curve_bytes ) );
3544
Steven Cooremanacda8342020-07-24 23:09:52 +02003545 /* Check whether the public part is loaded. If not, load it. */
3546 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3547 {
3548 MBEDTLS_MPI_CHK(
3549 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3550 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3551 }
3552
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003553 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3554 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003555
3556cleanup:
3557 mbedtls_mpi_free( &r );
3558 mbedtls_mpi_free( &s );
3559 return( mbedtls_to_psa_error( ret ) );
3560}
3561#endif /* MBEDTLS_ECDSA_C */
3562
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003563psa_status_t psa_sign_hash( psa_key_handle_t handle,
3564 psa_algorithm_t alg,
3565 const uint8_t *hash,
3566 size_t hash_length,
3567 uint8_t *signature,
3568 size_t signature_size,
3569 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003570{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003571 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003572 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003573#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3574 const psa_drv_se_t *drv;
3575 psa_drv_se_context_t *drv_context;
3576#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003577
3578 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003579 /* Immediately reject a zero-length signature buffer. This guarantees
3580 * that signature must be a valid pointer. (On the other hand, the hash
3581 * buffer can in principle be empty since it doesn't actually have
3582 * to be a hash.) */
3583 if( signature_size == 0 )
3584 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003585
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003586 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003587 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003588 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003589 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003590 {
3591 status = PSA_ERROR_INVALID_ARGUMENT;
3592 goto exit;
3593 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003594
Gilles Peskineedc64242019-08-07 21:05:07 +02003595#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3596 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3597 {
3598 if( drv->asymmetric == NULL ||
3599 drv->asymmetric->p_sign == NULL )
3600 {
3601 status = PSA_ERROR_NOT_SUPPORTED;
3602 goto exit;
3603 }
3604 status = drv->asymmetric->p_sign( drv_context,
3605 slot->data.se.slot_number,
3606 alg,
3607 hash, hash_length,
3608 signature, signature_size,
3609 signature_length );
3610 }
3611 else
3612#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003613#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003614 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003615 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003616 mbedtls_rsa_context rsa;
3617 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3618
3619 status = psa_load_rsa_representation( slot,
3620 &rsa );
3621 if( status != PSA_SUCCESS )
3622 goto exit;
3623
3624 status = psa_rsa_sign( &rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003625 alg,
3626 hash, hash_length,
3627 signature, signature_size,
3628 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003629
3630 mbedtls_rsa_free( &rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003631 }
3632 else
3633#endif /* defined(MBEDTLS_RSA_C) */
3634#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003635 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003636 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003637#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003638 if(
3639#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3640 PSA_ALG_IS_ECDSA( alg )
3641#else
3642 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3643#endif
3644 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003645 {
3646 mbedtls_ecp_keypair ecp;
3647 status = psa_load_ecp_representation( slot, &ecp );
3648 if( status != PSA_SUCCESS )
3649 goto exit;
3650 status = psa_ecdsa_sign( &ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003651 alg,
3652 hash, hash_length,
3653 signature, signature_size,
3654 signature_length );
Steven Cooremanacda8342020-07-24 23:09:52 +02003655 mbedtls_ecp_keypair_free( &ecp );
3656 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003657 else
3658#endif /* defined(MBEDTLS_ECDSA_C) */
3659 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003660 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003661 }
itayzafrir5c753392018-05-08 11:18:38 +03003662 }
3663 else
3664#endif /* defined(MBEDTLS_ECP_C) */
3665 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003666 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003667 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003668
3669exit:
3670 /* Fill the unused part of the output buffer (the whole buffer on error,
3671 * the trailing part on success) with something that isn't a valid mac
3672 * (barring an attack on the mac and deliberately-crafted input),
3673 * in case the caller doesn't check the return status properly. */
3674 if( status == PSA_SUCCESS )
3675 memset( signature + *signature_length, '!',
3676 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003677 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003678 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003679 /* If signature_size is 0 then we have nothing to do. We must not call
3680 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003681 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003682}
3683
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003684psa_status_t psa_verify_hash( psa_key_handle_t handle,
3685 psa_algorithm_t alg,
3686 const uint8_t *hash,
3687 size_t hash_length,
3688 const uint8_t *signature,
3689 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003690{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003691 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003692 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003693#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3694 const psa_drv_se_t *drv;
3695 psa_drv_se_context_t *drv_context;
3696#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003697
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003698 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003699 if( status != PSA_SUCCESS )
3700 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003701
Gilles Peskineedc64242019-08-07 21:05:07 +02003702#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3703 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3704 {
3705 if( drv->asymmetric == NULL ||
3706 drv->asymmetric->p_verify == NULL )
3707 return( PSA_ERROR_NOT_SUPPORTED );
3708 return( drv->asymmetric->p_verify( drv_context,
3709 slot->data.se.slot_number,
3710 alg,
3711 hash, hash_length,
3712 signature, signature_length ) );
3713 }
3714 else
3715#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003716#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003717 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003718 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003719 mbedtls_rsa_context rsa;
3720 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3721
3722 status = psa_load_rsa_representation( slot, &rsa );
3723 if( status != PSA_SUCCESS )
3724 return status;
3725
3726 status = psa_rsa_verify( &rsa,
3727 alg,
3728 hash, hash_length,
3729 signature, signature_length );
3730 mbedtls_rsa_free( &rsa );
3731 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003732 }
3733 else
3734#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003735#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003736 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003737 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003738#if defined(MBEDTLS_ECDSA_C)
3739 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003740 {
3741 mbedtls_ecp_keypair ecp;
3742 status = psa_load_ecp_representation( slot, &ecp );
3743 if( status != PSA_SUCCESS )
3744 return status;
3745 status = psa_ecdsa_verify( &ecp,
3746 hash, hash_length,
3747 signature, signature_length );
3748 mbedtls_ecp_keypair_free( &ecp );
3749 return status;
3750 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003751 else
3752#endif /* defined(MBEDTLS_ECDSA_C) */
3753 {
3754 return( PSA_ERROR_INVALID_ARGUMENT );
3755 }
itayzafrir5c753392018-05-08 11:18:38 +03003756 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003757 else
3758#endif /* defined(MBEDTLS_ECP_C) */
3759 {
3760 return( PSA_ERROR_NOT_SUPPORTED );
3761 }
3762}
3763
Gilles Peskine072ac562018-06-30 00:21:29 +02003764#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3765static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3766 mbedtls_rsa_context *rsa )
3767{
3768 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3769 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3770 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3771 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3772}
3773#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3774
Gilles Peskinec5487a82018-12-03 18:08:14 +01003775psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003776 psa_algorithm_t alg,
3777 const uint8_t *input,
3778 size_t input_length,
3779 const uint8_t *salt,
3780 size_t salt_length,
3781 uint8_t *output,
3782 size_t output_size,
3783 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003784{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003785 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003786 psa_status_t status;
3787
Darryl Green5cc689a2018-07-24 15:34:10 +01003788 (void) input;
3789 (void) input_length;
3790 (void) salt;
3791 (void) output;
3792 (void) output_size;
3793
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003794 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003795
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003796 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3797 return( PSA_ERROR_INVALID_ARGUMENT );
3798
Gilles Peskine28f8f302019-07-24 13:30:31 +02003799 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003800 if( status != PSA_SUCCESS )
3801 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003802 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3803 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003804 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003805
3806#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003807 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003808 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003809 mbedtls_rsa_context rsa;
3810 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3811
3812 status = psa_load_rsa_representation( slot, &rsa );
3813 if( status != PSA_SUCCESS )
3814 return status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003815 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003816 if( output_size < mbedtls_rsa_get_len( &rsa ) )
3817 {
3818 mbedtls_rsa_free( &rsa );
Vikas Katariya21599b62019-08-02 12:26:29 +01003819 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooremana01795d2020-07-24 22:48:15 +02003820 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003821#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003822 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003823 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003824 ret = mbedtls_rsa_pkcs1_encrypt( &rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003825 mbedtls_ctr_drbg_random,
3826 &global_data.ctr_drbg,
3827 MBEDTLS_RSA_PUBLIC,
3828 input_length,
3829 input,
3830 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003831 }
3832 else
3833#endif /* MBEDTLS_PKCS1_V15 */
3834#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003835 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003836 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003837 psa_rsa_oaep_set_padding_mode( alg, &rsa );
3838 ret = mbedtls_rsa_rsaes_oaep_encrypt( &rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003839 mbedtls_ctr_drbg_random,
3840 &global_data.ctr_drbg,
3841 MBEDTLS_RSA_PUBLIC,
3842 salt, salt_length,
3843 input_length,
3844 input,
3845 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003846 }
3847 else
3848#endif /* MBEDTLS_PKCS1_V21 */
3849 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003850 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003851 return( PSA_ERROR_INVALID_ARGUMENT );
3852 }
3853 if( ret == 0 )
Steven Cooremana01795d2020-07-24 22:48:15 +02003854 *output_length = mbedtls_rsa_get_len( &rsa );
3855
3856 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003857 return( mbedtls_to_psa_error( ret ) );
3858 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003859 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003860#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003861 {
3862 return( PSA_ERROR_NOT_SUPPORTED );
3863 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003864}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003865
Gilles Peskinec5487a82018-12-03 18:08:14 +01003866psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003867 psa_algorithm_t alg,
3868 const uint8_t *input,
3869 size_t input_length,
3870 const uint8_t *salt,
3871 size_t salt_length,
3872 uint8_t *output,
3873 size_t output_size,
3874 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003876 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003877 psa_status_t status;
3878
Darryl Green5cc689a2018-07-24 15:34:10 +01003879 (void) input;
3880 (void) input_length;
3881 (void) salt;
3882 (void) output;
3883 (void) output_size;
3884
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003885 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003886
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003887 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3888 return( PSA_ERROR_INVALID_ARGUMENT );
3889
Gilles Peskine28f8f302019-07-24 13:30:31 +02003890 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003891 if( status != PSA_SUCCESS )
3892 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003893 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003894 return( PSA_ERROR_INVALID_ARGUMENT );
3895
3896#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003897 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003898 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003899 mbedtls_rsa_context rsa;
3900 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3901
3902 status = psa_load_rsa_representation( slot, &rsa );
3903 if( status != PSA_SUCCESS )
3904 return status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003906
Steven Cooremana01795d2020-07-24 22:48:15 +02003907 if( input_length != mbedtls_rsa_get_len( &rsa ) )
3908 {
3909 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003910 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana01795d2020-07-24 22:48:15 +02003911 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003912
3913#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003914 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003915 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003916 ret = mbedtls_rsa_pkcs1_decrypt( &rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003917 mbedtls_ctr_drbg_random,
3918 &global_data.ctr_drbg,
3919 MBEDTLS_RSA_PRIVATE,
3920 output_length,
3921 input,
3922 output,
3923 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003924 }
3925 else
3926#endif /* MBEDTLS_PKCS1_V15 */
3927#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003928 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003929 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003930 psa_rsa_oaep_set_padding_mode( alg, &rsa );
3931 ret = mbedtls_rsa_rsaes_oaep_decrypt( &rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003932 mbedtls_ctr_drbg_random,
3933 &global_data.ctr_drbg,
3934 MBEDTLS_RSA_PRIVATE,
3935 salt, salt_length,
3936 output_length,
3937 input,
3938 output,
3939 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003940 }
3941 else
3942#endif /* MBEDTLS_PKCS1_V21 */
3943 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003944 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003945 return( PSA_ERROR_INVALID_ARGUMENT );
3946 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003947
Steven Cooremana01795d2020-07-24 22:48:15 +02003948 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003949 return( mbedtls_to_psa_error( ret ) );
3950 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003951 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003952#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003953 {
3954 return( PSA_ERROR_NOT_SUPPORTED );
3955 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003956}
Gilles Peskine20035e32018-02-03 22:44:14 +01003957
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003958
3959
mohammad1603503973b2018-03-12 15:59:30 +02003960/****************************************************************/
3961/* Symmetric cryptography */
3962/****************************************************************/
3963
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003964/* Initialize the cipher operation structure. Once this function has been
3965 * called, psa_cipher_abort can run and will do the right thing. */
3966static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3967 psa_algorithm_t alg )
3968{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003969 if( ! PSA_ALG_IS_CIPHER( alg ) )
3970 {
3971 memset( operation, 0, sizeof( *operation ) );
3972 return( PSA_ERROR_INVALID_ARGUMENT );
3973 }
3974
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003975 operation->alg = alg;
3976 operation->key_set = 0;
3977 operation->iv_set = 0;
3978 operation->iv_required = 1;
3979 operation->iv_size = 0;
3980 operation->block_size = 0;
3981 mbedtls_cipher_init( &operation->ctx.cipher );
3982 return( PSA_SUCCESS );
3983}
3984
Gilles Peskinee553c652018-06-04 16:22:46 +02003985static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003986 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003987 psa_algorithm_t alg,
3988 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003989{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003990 int ret = 0;
3991 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003992 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003993 size_t key_bits;
3994 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003995 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3996 PSA_KEY_USAGE_ENCRYPT :
3997 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003998
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003999 /* A context must be freshly initialized before it can be set up. */
4000 if( operation->alg != 0 )
4001 {
4002 return( PSA_ERROR_BAD_STATE );
4003 }
4004
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004005 status = psa_cipher_init( operation, alg );
4006 if( status != PSA_SUCCESS )
4007 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004008
Gilles Peskine28f8f302019-07-24 13:30:31 +02004009 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004010 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004011 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004012 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004013
Gilles Peskine8e338702019-07-30 20:06:31 +02004014 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004015 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004016 {
4017 status = PSA_ERROR_NOT_SUPPORTED;
4018 goto exit;
4019 }
mohammad1603503973b2018-03-12 15:59:30 +02004020
mohammad1603503973b2018-03-12 15:59:30 +02004021 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004022 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004023 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004024
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004025#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004026 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004027 {
4028 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004029 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004030 memcpy( keys, slot->data.key.data, 16 );
4031 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004032 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4033 keys,
4034 192, cipher_operation );
4035 }
4036 else
4037#endif
4038 {
4039 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004040 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004041 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004042 }
Moran Peker41deec42018-04-04 15:43:05 +03004043 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004044 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004045
mohammad16038481e742018-03-18 13:57:31 +02004046#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004047 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004048 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004049 case PSA_ALG_CBC_NO_PADDING:
4050 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4051 MBEDTLS_PADDING_NONE );
4052 break;
4053 case PSA_ALG_CBC_PKCS7:
4054 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4055 MBEDTLS_PADDING_PKCS7 );
4056 break;
4057 default:
4058 /* The algorithm doesn't involve padding. */
4059 ret = 0;
4060 break;
4061 }
4062 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004063 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004064#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4065
mohammad1603503973b2018-03-12 15:59:30 +02004066 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004067 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004068 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004069 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004070 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004071 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004072 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004073#if defined(MBEDTLS_CHACHA20_C)
4074 else
4075 if( alg == PSA_ALG_CHACHA20 )
4076 operation->iv_size = 12;
4077#endif
mohammad1603503973b2018-03-12 15:59:30 +02004078
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004079exit:
4080 if( status == 0 )
4081 status = mbedtls_to_psa_error( ret );
4082 if( status != 0 )
4083 psa_cipher_abort( operation );
4084 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004085}
4086
Gilles Peskinefe119512018-07-08 21:39:34 +02004087psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004088 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004089 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004090{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004091 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004092}
4093
Gilles Peskinefe119512018-07-08 21:39:34 +02004094psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004095 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004096 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004097{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004098 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004099}
4100
Gilles Peskinefe119512018-07-08 21:39:34 +02004101psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004102 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004103 size_t iv_size,
4104 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004105{
itayzafrir534bd7c2018-08-02 13:56:32 +03004106 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004108 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004109 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004110 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004111 }
Moran Peker41deec42018-04-04 15:43:05 +03004112 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004113 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004114 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004115 goto exit;
4116 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004117 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4118 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004119 if( ret != 0 )
4120 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004121 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004122 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004123 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004124
mohammad16038481e742018-03-18 13:57:31 +02004125 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004126 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004127
Moran Peker395db872018-05-31 14:07:14 +03004128exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004129 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004130 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004131 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004132}
4133
Gilles Peskinefe119512018-07-08 21:39:34 +02004134psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004135 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004136 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004137{
itayzafrir534bd7c2018-08-02 13:56:32 +03004138 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004139 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004140 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004141 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004142 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004143 }
Moran Pekera28258c2018-05-29 16:25:04 +03004144 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004145 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004146 status = PSA_ERROR_INVALID_ARGUMENT;
4147 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004148 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004149 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4150 status = mbedtls_to_psa_error( ret );
4151exit:
4152 if( status == PSA_SUCCESS )
4153 operation->iv_set = 1;
4154 else
mohammad1603503973b2018-03-12 15:59:30 +02004155 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004156 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004157}
4158
Gilles Peskinee553c652018-06-04 16:22:46 +02004159psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4160 const uint8_t *input,
4161 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004162 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004163 size_t output_size,
4164 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004165{
itayzafrir534bd7c2018-08-02 13:56:32 +03004166 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004168 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004169
4170 if( operation->alg == 0 )
4171 {
4172 return( PSA_ERROR_BAD_STATE );
4173 }
4174
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004175 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004176 {
4177 /* Take the unprocessed partial block left over from previous
4178 * update calls, if any, plus the input to this call. Remove
4179 * the last partial block, if any. You get the data that will be
4180 * output in this call. */
4181 expected_output_size =
4182 ( operation->ctx.cipher.unprocessed_len + input_length )
4183 / operation->block_size * operation->block_size;
4184 }
4185 else
4186 {
4187 expected_output_size = input_length;
4188 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004189
Gilles Peskine89d789c2018-06-04 17:17:16 +02004190 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004191 {
4192 status = PSA_ERROR_BUFFER_TOO_SMALL;
4193 goto exit;
4194 }
mohammad160382759612018-03-12 18:16:40 +02004195
mohammad1603503973b2018-03-12 15:59:30 +02004196 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004197 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004198 status = mbedtls_to_psa_error( ret );
4199exit:
4200 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004201 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004202 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004203}
4204
Gilles Peskinee553c652018-06-04 16:22:46 +02004205psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4206 uint8_t *output,
4207 size_t output_size,
4208 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004209{
David Saadab4ecc272019-02-14 13:48:10 +02004210 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004211 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004212 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004213
mohammad1603503973b2018-03-12 15:59:30 +02004214 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004215 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004216 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004217 }
4218 if( operation->iv_required && ! operation->iv_set )
4219 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004220 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004221 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004222
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004223 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004224 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4225 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004226 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004227 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004228 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004229 }
4230
Janos Follath315b51c2018-07-09 16:04:51 +01004231 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4232 temp_output_buffer,
4233 output_length );
4234 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004235 {
Janos Follath315b51c2018-07-09 16:04:51 +01004236 status = mbedtls_to_psa_error( cipher_ret );
4237 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004238 }
Janos Follath315b51c2018-07-09 16:04:51 +01004239
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004240 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004241 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004242 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004243 memcpy( output, temp_output_buffer, *output_length );
4244 else
4245 {
Janos Follath315b51c2018-07-09 16:04:51 +01004246 status = PSA_ERROR_BUFFER_TOO_SMALL;
4247 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004248 }
mohammad1603503973b2018-03-12 15:59:30 +02004249
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 status = psa_cipher_abort( operation );
4252
4253 return( status );
4254
4255error:
4256
4257 *output_length = 0;
4258
Gilles Peskine3f108122018-12-07 18:14:53 +01004259 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004260 (void) psa_cipher_abort( operation );
4261
4262 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004263}
4264
Gilles Peskinee553c652018-06-04 16:22:46 +02004265psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4266{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004267 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004268 {
4269 /* The object has (apparently) been initialized but it is not
4270 * in use. It's ok to call abort on such an object, and there's
4271 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004272 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004273 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004274
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004275 /* Sanity check (shouldn't happen: operation->alg should
4276 * always have been initialized to a valid value). */
4277 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4278 return( PSA_ERROR_BAD_STATE );
4279
mohammad1603503973b2018-03-12 15:59:30 +02004280 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004281
Moran Peker41deec42018-04-04 15:43:05 +03004282 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004283 operation->key_set = 0;
4284 operation->iv_set = 0;
4285 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004286 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004287 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004288
Moran Peker395db872018-05-31 14:07:14 +03004289 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004290}
4291
Gilles Peskinea0655c32018-04-30 17:06:50 +02004292
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004293
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004294
mohammad16035955c982018-04-26 00:53:03 +03004295/****************************************************************/
4296/* AEAD */
4297/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004298
Gilles Peskineedf9a652018-08-17 18:11:56 +02004299typedef struct
4300{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004301 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004302 const mbedtls_cipher_info_t *cipher_info;
4303 union
4304 {
4305#if defined(MBEDTLS_CCM_C)
4306 mbedtls_ccm_context ccm;
4307#endif /* MBEDTLS_CCM_C */
4308#if defined(MBEDTLS_GCM_C)
4309 mbedtls_gcm_context gcm;
4310#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004311#if defined(MBEDTLS_CHACHAPOLY_C)
4312 mbedtls_chachapoly_context chachapoly;
4313#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004314 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004315 psa_algorithm_t core_alg;
4316 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004317 uint8_t tag_length;
4318} aead_operation_t;
4319
Gilles Peskine30a9e412019-01-14 18:36:12 +01004320static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004321{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004322 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004323 {
4324#if defined(MBEDTLS_CCM_C)
4325 case PSA_ALG_CCM:
4326 mbedtls_ccm_free( &operation->ctx.ccm );
4327 break;
4328#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004329#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004330 case PSA_ALG_GCM:
4331 mbedtls_gcm_free( &operation->ctx.gcm );
4332 break;
4333#endif /* MBEDTLS_GCM_C */
4334 }
4335}
4336
4337static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004338 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004339 psa_key_usage_t usage,
4340 psa_algorithm_t alg )
4341{
4342 psa_status_t status;
4343 size_t key_bits;
4344 mbedtls_cipher_id_t cipher_id;
4345
Gilles Peskine28f8f302019-07-24 13:30:31 +02004346 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004347 if( status != PSA_SUCCESS )
4348 return( status );
4349
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004350 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004351
4352 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004353 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004354 &cipher_id );
4355 if( operation->cipher_info == NULL )
4356 return( PSA_ERROR_NOT_SUPPORTED );
4357
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004358 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004359 {
4360#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004361 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4362 operation->core_alg = PSA_ALG_CCM;
4363 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004364 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4365 * The call to mbedtls_ccm_encrypt_and_tag or
4366 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004367 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004368 return( PSA_ERROR_INVALID_ARGUMENT );
4369 mbedtls_ccm_init( &operation->ctx.ccm );
4370 status = mbedtls_to_psa_error(
4371 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004372 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004373 (unsigned int) key_bits ) );
4374 if( status != 0 )
4375 goto cleanup;
4376 break;
4377#endif /* MBEDTLS_CCM_C */
4378
4379#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004380 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4381 operation->core_alg = PSA_ALG_GCM;
4382 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004383 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4384 * The call to mbedtls_gcm_crypt_and_tag or
4385 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004386 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004387 return( PSA_ERROR_INVALID_ARGUMENT );
4388 mbedtls_gcm_init( &operation->ctx.gcm );
4389 status = mbedtls_to_psa_error(
4390 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004391 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004392 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004393 if( status != 0 )
4394 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004395 break;
4396#endif /* MBEDTLS_GCM_C */
4397
Gilles Peskine26869f22019-05-06 15:25:00 +02004398#if defined(MBEDTLS_CHACHAPOLY_C)
4399 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4400 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4401 operation->full_tag_length = 16;
4402 /* We only support the default tag length. */
4403 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4404 return( PSA_ERROR_NOT_SUPPORTED );
4405 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4406 status = mbedtls_to_psa_error(
4407 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004408 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004409 if( status != 0 )
4410 goto cleanup;
4411 break;
4412#endif /* MBEDTLS_CHACHAPOLY_C */
4413
Gilles Peskineedf9a652018-08-17 18:11:56 +02004414 default:
4415 return( PSA_ERROR_NOT_SUPPORTED );
4416 }
4417
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004418 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4419 {
4420 status = PSA_ERROR_INVALID_ARGUMENT;
4421 goto cleanup;
4422 }
4423 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004424
Gilles Peskineedf9a652018-08-17 18:11:56 +02004425 return( PSA_SUCCESS );
4426
4427cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004428 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004429 return( status );
4430}
4431
Gilles Peskinec5487a82018-12-03 18:08:14 +01004432psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004433 psa_algorithm_t alg,
4434 const uint8_t *nonce,
4435 size_t nonce_length,
4436 const uint8_t *additional_data,
4437 size_t additional_data_length,
4438 const uint8_t *plaintext,
4439 size_t plaintext_length,
4440 uint8_t *ciphertext,
4441 size_t ciphertext_size,
4442 size_t *ciphertext_length )
4443{
mohammad16035955c982018-04-26 00:53:03 +03004444 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004445 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004446 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004447
mohammad1603f08a5502018-06-03 15:05:47 +03004448 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004449
Gilles Peskinec5487a82018-12-03 18:08:14 +01004450 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004451 if( status != PSA_SUCCESS )
4452 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004453
Gilles Peskineedf9a652018-08-17 18:11:56 +02004454 /* For all currently supported modes, the tag is at the end of the
4455 * ciphertext. */
4456 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4457 {
4458 status = PSA_ERROR_BUFFER_TOO_SMALL;
4459 goto exit;
4460 }
4461 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004462
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004463#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004464 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004465 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004466 status = mbedtls_to_psa_error(
4467 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4468 MBEDTLS_GCM_ENCRYPT,
4469 plaintext_length,
4470 nonce, nonce_length,
4471 additional_data, additional_data_length,
4472 plaintext, ciphertext,
4473 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004474 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004475 else
4476#endif /* MBEDTLS_GCM_C */
4477#if defined(MBEDTLS_CCM_C)
4478 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004479 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004480 status = mbedtls_to_psa_error(
4481 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4482 plaintext_length,
4483 nonce, nonce_length,
4484 additional_data,
4485 additional_data_length,
4486 plaintext, ciphertext,
4487 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004488 }
mohammad16035c8845f2018-05-09 05:40:09 -07004489 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004490#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004491#if defined(MBEDTLS_CHACHAPOLY_C)
4492 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4493 {
4494 if( nonce_length != 12 || operation.tag_length != 16 )
4495 {
4496 status = PSA_ERROR_NOT_SUPPORTED;
4497 goto exit;
4498 }
4499 status = mbedtls_to_psa_error(
4500 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4501 plaintext_length,
4502 nonce,
4503 additional_data,
4504 additional_data_length,
4505 plaintext,
4506 ciphertext,
4507 tag ) );
4508 }
4509 else
4510#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004511 {
mohammad1603554faad2018-06-03 15:07:38 +03004512 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004513 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004514
Gilles Peskineedf9a652018-08-17 18:11:56 +02004515 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4516 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004517
Gilles Peskineedf9a652018-08-17 18:11:56 +02004518exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004519 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004520 if( status == PSA_SUCCESS )
4521 *ciphertext_length = plaintext_length + operation.tag_length;
4522 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004523}
4524
Gilles Peskineee652a32018-06-01 19:23:52 +02004525/* Locate the tag in a ciphertext buffer containing the encrypted data
4526 * followed by the tag. Return the length of the part preceding the tag in
4527 * *plaintext_length. This is the size of the plaintext in modes where
4528 * the encrypted data has the same size as the plaintext, such as
4529 * CCM and GCM. */
4530static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4531 const uint8_t *ciphertext,
4532 size_t ciphertext_length,
4533 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004534 const uint8_t **p_tag )
4535{
4536 size_t payload_length;
4537 if( tag_length > ciphertext_length )
4538 return( PSA_ERROR_INVALID_ARGUMENT );
4539 payload_length = ciphertext_length - tag_length;
4540 if( payload_length > plaintext_size )
4541 return( PSA_ERROR_BUFFER_TOO_SMALL );
4542 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004543 return( PSA_SUCCESS );
4544}
4545
Gilles Peskinec5487a82018-12-03 18:08:14 +01004546psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004547 psa_algorithm_t alg,
4548 const uint8_t *nonce,
4549 size_t nonce_length,
4550 const uint8_t *additional_data,
4551 size_t additional_data_length,
4552 const uint8_t *ciphertext,
4553 size_t ciphertext_length,
4554 uint8_t *plaintext,
4555 size_t plaintext_size,
4556 size_t *plaintext_length )
4557{
mohammad16035955c982018-04-26 00:53:03 +03004558 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004559 aead_operation_t operation;
4560 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004561
Gilles Peskineee652a32018-06-01 19:23:52 +02004562 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004563
Gilles Peskinec5487a82018-12-03 18:08:14 +01004564 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004565 if( status != PSA_SUCCESS )
4566 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004567
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004568 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4569 ciphertext, ciphertext_length,
4570 plaintext_size, &tag );
4571 if( status != PSA_SUCCESS )
4572 goto exit;
4573
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004574#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004575 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004576 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004577 status = mbedtls_to_psa_error(
4578 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4579 ciphertext_length - operation.tag_length,
4580 nonce, nonce_length,
4581 additional_data,
4582 additional_data_length,
4583 tag, operation.tag_length,
4584 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004585 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004586 else
4587#endif /* MBEDTLS_GCM_C */
4588#if defined(MBEDTLS_CCM_C)
4589 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004590 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004591 status = mbedtls_to_psa_error(
4592 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4593 ciphertext_length - operation.tag_length,
4594 nonce, nonce_length,
4595 additional_data,
4596 additional_data_length,
4597 ciphertext, plaintext,
4598 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004599 }
mohammad160339574652018-06-01 04:39:53 -07004600 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004601#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004602#if defined(MBEDTLS_CHACHAPOLY_C)
4603 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4604 {
4605 if( nonce_length != 12 || operation.tag_length != 16 )
4606 {
4607 status = PSA_ERROR_NOT_SUPPORTED;
4608 goto exit;
4609 }
4610 status = mbedtls_to_psa_error(
4611 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4612 ciphertext_length - operation.tag_length,
4613 nonce,
4614 additional_data,
4615 additional_data_length,
4616 tag,
4617 ciphertext,
4618 plaintext ) );
4619 }
4620 else
4621#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004622 {
mohammad1603554faad2018-06-03 15:07:38 +03004623 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004624 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004625
Gilles Peskineedf9a652018-08-17 18:11:56 +02004626 if( status != PSA_SUCCESS && plaintext_size != 0 )
4627 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004628
Gilles Peskineedf9a652018-08-17 18:11:56 +02004629exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004630 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004631 if( status == PSA_SUCCESS )
4632 *plaintext_length = ciphertext_length - operation.tag_length;
4633 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004634}
4635
Gilles Peskinea0655c32018-04-30 17:06:50 +02004636
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004637
Gilles Peskine20035e32018-02-03 22:44:14 +01004638/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004639/* Generators */
4640/****************************************************************/
4641
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004642#define HKDF_STATE_INIT 0 /* no input yet */
4643#define HKDF_STATE_STARTED 1 /* got salt */
4644#define HKDF_STATE_KEYED 2 /* got key */
4645#define HKDF_STATE_OUTPUT 3 /* output started */
4646
Gilles Peskinecbe66502019-05-16 16:59:18 +02004647static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004649{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004650 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4651 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004652 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004653 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004654}
4655
4656
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004657psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004658{
4659 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004660 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004661 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004662 {
4663 /* The object has (apparently) been initialized but it is not
4664 * in use. It's ok to call abort on such an object, and there's
4665 * nothing to do. */
4666 }
4667 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004668#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004669 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004670 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004671 mbedtls_free( operation->ctx.hkdf.info );
4672 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004673 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004674 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004676 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004677 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004678 if( operation->ctx.tls12_prf.seed != NULL )
4679 {
4680 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4681 operation->ctx.tls12_prf.seed_length );
4682 mbedtls_free( operation->ctx.tls12_prf.seed );
4683 }
4684
4685 if( operation->ctx.tls12_prf.label != NULL )
4686 {
4687 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4688 operation->ctx.tls12_prf.label_length );
4689 mbedtls_free( operation->ctx.tls12_prf.label );
4690 }
4691
4692 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4693
4694 /* We leave the fields Ai and output_block to be erased safely by the
4695 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004696 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004697 else
4698#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004699 {
4700 status = PSA_ERROR_BAD_STATE;
4701 }
Janos Follath083036a2019-06-11 10:22:26 +01004702 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004703 return( status );
4704}
4705
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004706psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004707 size_t *capacity)
4708{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004709 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004710 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004711 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004712 return PSA_ERROR_BAD_STATE;
4713 }
4714
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004715 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004716 return( PSA_SUCCESS );
4717}
4718
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004719psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004720 size_t capacity )
4721{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004723 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004725 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004727 return( PSA_SUCCESS );
4728}
4729
Gilles Peskinebef7f142018-07-12 17:22:21 +02004730#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004732 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004733static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004734 psa_algorithm_t hash_alg,
4735 uint8_t *output,
4736 size_t output_length )
4737{
4738 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4739 psa_status_t status;
4740
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004741 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4742 return( PSA_ERROR_BAD_STATE );
4743 hkdf->state = HKDF_STATE_OUTPUT;
4744
Gilles Peskinebef7f142018-07-12 17:22:21 +02004745 while( output_length != 0 )
4746 {
4747 /* Copy what remains of the current block */
4748 uint8_t n = hash_length - hkdf->offset_in_block;
4749 if( n > output_length )
4750 n = (uint8_t) output_length;
4751 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4752 output += n;
4753 output_length -= n;
4754 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004755 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004756 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004757 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004758 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004760 * object was corrupted or if this function is called directly
4761 * inside the library. */
4762 if( hkdf->block_number == 0xff )
4763 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004764
4765 /* We need a new block */
4766 ++hkdf->block_number;
4767 hkdf->offset_in_block = 0;
4768 status = psa_hmac_setup_internal( &hkdf->hmac,
4769 hkdf->prk, hash_length,
4770 hash_alg );
4771 if( status != PSA_SUCCESS )
4772 return( status );
4773 if( hkdf->block_number != 1 )
4774 {
4775 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4776 hkdf->output_block,
4777 hash_length );
4778 if( status != PSA_SUCCESS )
4779 return( status );
4780 }
4781 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4782 hkdf->info,
4783 hkdf->info_length );
4784 if( status != PSA_SUCCESS )
4785 return( status );
4786 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4787 &hkdf->block_number, 1 );
4788 if( status != PSA_SUCCESS )
4789 return( status );
4790 status = psa_hmac_finish_internal( &hkdf->hmac,
4791 hkdf->output_block,
4792 sizeof( hkdf->output_block ) );
4793 if( status != PSA_SUCCESS )
4794 return( status );
4795 }
4796
4797 return( PSA_SUCCESS );
4798}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004799
Janos Follath7742fee2019-06-17 12:58:10 +01004800static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4801 psa_tls12_prf_key_derivation_t *tls12_prf,
4802 psa_algorithm_t alg )
4803{
4804 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4805 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004806 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4807 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004808
Janos Follath7742fee2019-06-17 12:58:10 +01004809 /* We can't be wanting more output after block 0xff, otherwise
4810 * the capacity check in psa_key_derivation_output_bytes() would have
4811 * prevented this call. It could happen only if the operation
4812 * object was corrupted or if this function is called directly
4813 * inside the library. */
4814 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004815 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004816
4817 /* We need a new block */
4818 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004819 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004820
4821 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4822 *
4823 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4824 *
4825 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4826 * HMAC_hash(secret, A(2) + seed) +
4827 * HMAC_hash(secret, A(3) + seed) + ...
4828 *
4829 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004830 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004831 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004832 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004833 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004834 * is currently extracted as `output_block` and where i is
4835 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004836 */
4837
Janos Follathea29bfb2019-06-19 12:21:20 +01004838 /* Save the hash context before using it, to preserve the hash state with
4839 * only the inner padding in it. We need this, because inner padding depends
4840 * on the key (secret in the RFC's terminology). */
4841 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4842 if( status != PSA_SUCCESS )
4843 goto cleanup;
4844
4845 /* Calculate A(i) where i = tls12_prf->block_number. */
4846 if( tls12_prf->block_number == 1 )
4847 {
4848 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4849 * the variable seed and in this instance means it in the context of the
4850 * P_hash function, where seed = label + seed.) */
4851 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4852 tls12_prf->label, tls12_prf->label_length );
4853 if( status != PSA_SUCCESS )
4854 goto cleanup;
4855 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4856 tls12_prf->seed, tls12_prf->seed_length );
4857 if( status != PSA_SUCCESS )
4858 goto cleanup;
4859 }
4860 else
4861 {
4862 /* A(i) = HMAC_hash(secret, A(i-1)) */
4863 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4864 tls12_prf->Ai, hash_length );
4865 if( status != PSA_SUCCESS )
4866 goto cleanup;
4867 }
4868
4869 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4870 tls12_prf->Ai, hash_length );
4871 if( status != PSA_SUCCESS )
4872 goto cleanup;
4873 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4874 if( status != PSA_SUCCESS )
4875 goto cleanup;
4876
4877 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4878 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4879 tls12_prf->Ai, hash_length );
4880 if( status != PSA_SUCCESS )
4881 goto cleanup;
4882 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4883 tls12_prf->label, tls12_prf->label_length );
4884 if( status != PSA_SUCCESS )
4885 goto cleanup;
4886 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4887 tls12_prf->seed, tls12_prf->seed_length );
4888 if( status != PSA_SUCCESS )
4889 goto cleanup;
4890 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4891 tls12_prf->output_block, hash_length );
4892 if( status != PSA_SUCCESS )
4893 goto cleanup;
4894 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4895 if( status != PSA_SUCCESS )
4896 goto cleanup;
4897
Janos Follath7742fee2019-06-17 12:58:10 +01004898
4899cleanup:
4900
Janos Follathea29bfb2019-06-19 12:21:20 +01004901 cleanup_status = psa_hash_abort( &backup );
4902 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4903 status = cleanup_status;
4904
4905 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004906}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004907
Janos Follath844eb0e2019-06-19 12:10:49 +01004908static psa_status_t psa_key_derivation_tls12_prf_read(
4909 psa_tls12_prf_key_derivation_t *tls12_prf,
4910 psa_algorithm_t alg,
4911 uint8_t *output,
4912 size_t output_length )
4913{
4914 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4915 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4916 psa_status_t status;
4917 uint8_t offset, length;
4918
4919 while( output_length != 0 )
4920 {
4921 /* Check if we have fully processed the current block. */
4922 if( tls12_prf->left_in_block == 0 )
4923 {
4924 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4925 alg );
4926 if( status != PSA_SUCCESS )
4927 return( status );
4928
4929 continue;
4930 }
4931
4932 if( tls12_prf->left_in_block > output_length )
4933 length = (uint8_t) output_length;
4934 else
4935 length = tls12_prf->left_in_block;
4936
4937 offset = hash_length - tls12_prf->left_in_block;
4938 memcpy( output, tls12_prf->output_block + offset, length );
4939 output += length;
4940 output_length -= length;
4941 tls12_prf->left_in_block -= length;
4942 }
4943
4944 return( PSA_SUCCESS );
4945}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004946#endif /* MBEDTLS_MD_C */
4947
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004948psa_status_t psa_key_derivation_output_bytes(
4949 psa_key_derivation_operation_t *operation,
4950 uint8_t *output,
4951 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004952{
4953 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004955
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004957 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004959 return PSA_ERROR_BAD_STATE;
4960 }
4961
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004962 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004963 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004965 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004967 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004968 goto exit;
4969 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004971 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004972 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004973 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004974 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4975 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004977 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004978 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004979 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004980 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004981
Gilles Peskinebef7f142018-07-12 17:22:21 +02004982#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004983 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004984 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004985 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004987 output, output_length );
4988 }
Janos Follathadbec812019-06-14 11:05:39 +01004989 else
Janos Follathadbec812019-06-14 11:05:39 +01004990 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004991 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004992 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004993 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004994 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004995 output_length );
4996 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004997 else
4998#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004999 {
5000 return( PSA_ERROR_BAD_STATE );
5001 }
5002
5003exit:
5004 if( status != PSA_SUCCESS )
5005 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005006 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 * This allows us to differentiate between exhausted operations and
5008 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5009 * operations. */
5010 psa_algorithm_t alg = operation->alg;
5011 psa_key_derivation_abort( operation );
5012 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005013 memset( output, '!', output_length );
5014 }
5015 return( status );
5016}
5017
Gilles Peskine08542d82018-07-19 17:05:42 +02005018#if defined(MBEDTLS_DES_C)
5019static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5020{
5021 if( data_size >= 8 )
5022 mbedtls_des_key_set_parity( data );
5023 if( data_size >= 16 )
5024 mbedtls_des_key_set_parity( data + 8 );
5025 if( data_size >= 24 )
5026 mbedtls_des_key_set_parity( data + 16 );
5027}
5028#endif /* MBEDTLS_DES_C */
5029
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005030static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005031 psa_key_slot_t *slot,
5032 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005033 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005034{
5035 uint8_t *data = NULL;
5036 size_t bytes = PSA_BITS_TO_BYTES( bits );
5037 psa_status_t status;
5038
Gilles Peskine8e338702019-07-30 20:06:31 +02005039 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005040 return( PSA_ERROR_INVALID_ARGUMENT );
5041 if( bits % 8 != 0 )
5042 return( PSA_ERROR_INVALID_ARGUMENT );
5043 data = mbedtls_calloc( 1, bytes );
5044 if( data == NULL )
5045 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5046
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005047 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005048 if( status != PSA_SUCCESS )
5049 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005050#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005051 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005052 psa_des_set_key_parity( data, bytes );
5053#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005054 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005055
5056exit:
5057 mbedtls_free( data );
5058 return( status );
5059}
5060
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005061psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005063 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005064{
5065 psa_status_t status;
5066 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005067 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005068
5069 /* Reject any attempt to create a zero-length key so that we don't
5070 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5071 if( psa_get_key_bits( attributes ) == 0 )
5072 return( PSA_ERROR_INVALID_ARGUMENT );
5073
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005074 if( ! operation->can_output_key )
5075 return( PSA_ERROR_NOT_PERMITTED );
5076
Gilles Peskinedf179142019-07-15 22:02:14 +02005077 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5078 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005079#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5080 if( driver != NULL )
5081 {
5082 /* Deriving a key in a secure element is not implemented yet. */
5083 status = PSA_ERROR_NOT_SUPPORTED;
5084 }
5085#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005086 if( status == PSA_SUCCESS )
5087 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005088 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005089 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005090 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005091 }
5092 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005093 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005094 if( status != PSA_SUCCESS )
5095 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005096 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005097 *handle = 0;
5098 }
5099 return( status );
5100}
5101
Gilles Peskineeab56e42018-07-12 17:12:33 +02005102
5103
5104/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005105/* Key derivation */
5106/****************************************************************/
5107
Gilles Peskine969c5d62019-01-16 15:53:06 +01005108static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005109 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005110 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005111{
Janos Follath5fe19732019-06-20 15:09:30 +01005112 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5113 * initialisers for this union leave some bytes unspecified.) */
5114 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5115
Gilles Peskine969c5d62019-01-16 15:53:06 +01005116 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005117#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005118 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5119 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5120 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005121 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005122 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005123 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5124 if( hash_size == 0 )
5125 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005126 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5127 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005128 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005129 {
5130 return( PSA_ERROR_NOT_SUPPORTED );
5131 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005132 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005133 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005134 }
5135#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005136 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005137 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005138}
5139
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005141 psa_algorithm_t alg )
5142{
5143 psa_status_t status;
5144
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005145 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005146 return( PSA_ERROR_BAD_STATE );
5147
Gilles Peskine6843c292019-01-18 16:44:49 +01005148 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5149 return( PSA_ERROR_INVALID_ARGUMENT );
5150 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005151 {
5152 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005153 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005154 }
5155 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5156 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005157 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005158 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005159 else
5160 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005161
5162 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005163 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005164 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005165}
5166
5167#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005168static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005169 psa_algorithm_t hash_alg,
5170 psa_key_derivation_step_t step,
5171 const uint8_t *data,
5172 size_t data_length )
5173{
5174 psa_status_t status;
5175 switch( step )
5176 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005177 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005178 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005179 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005180 status = psa_hmac_setup_internal( &hkdf->hmac,
5181 data, data_length,
5182 hash_alg );
5183 if( status != PSA_SUCCESS )
5184 return( status );
5185 hkdf->state = HKDF_STATE_STARTED;
5186 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005187 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005188 /* If no salt was provided, use an empty salt. */
5189 if( hkdf->state == HKDF_STATE_INIT )
5190 {
5191 status = psa_hmac_setup_internal( &hkdf->hmac,
5192 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005193 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194 if( status != PSA_SUCCESS )
5195 return( status );
5196 hkdf->state = HKDF_STATE_STARTED;
5197 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005198 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005199 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005200 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5201 data, data_length );
5202 if( status != PSA_SUCCESS )
5203 return( status );
5204 status = psa_hmac_finish_internal( &hkdf->hmac,
5205 hkdf->prk,
5206 sizeof( hkdf->prk ) );
5207 if( status != PSA_SUCCESS )
5208 return( status );
5209 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5210 hkdf->block_number = 0;
5211 hkdf->state = HKDF_STATE_KEYED;
5212 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005213 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005214 if( hkdf->state == HKDF_STATE_OUTPUT )
5215 return( PSA_ERROR_BAD_STATE );
5216 if( hkdf->info_set )
5217 return( PSA_ERROR_BAD_STATE );
5218 hkdf->info_length = data_length;
5219 if( data_length != 0 )
5220 {
5221 hkdf->info = mbedtls_calloc( 1, data_length );
5222 if( hkdf->info == NULL )
5223 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5224 memcpy( hkdf->info, data, data_length );
5225 }
5226 hkdf->info_set = 1;
5227 return( PSA_SUCCESS );
5228 default:
5229 return( PSA_ERROR_INVALID_ARGUMENT );
5230 }
5231}
Janos Follathb03233e2019-06-11 15:30:30 +01005232
Janos Follathf08e2652019-06-13 09:05:41 +01005233static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5234 const uint8_t *data,
5235 size_t data_length )
5236{
5237 if( prf->state != TLS12_PRF_STATE_INIT )
5238 return( PSA_ERROR_BAD_STATE );
5239
Janos Follathd6dce9f2019-07-04 09:11:38 +01005240 if( data_length != 0 )
5241 {
5242 prf->seed = mbedtls_calloc( 1, data_length );
5243 if( prf->seed == NULL )
5244 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005245
Janos Follathd6dce9f2019-07-04 09:11:38 +01005246 memcpy( prf->seed, data, data_length );
5247 prf->seed_length = data_length;
5248 }
Janos Follathf08e2652019-06-13 09:05:41 +01005249
5250 prf->state = TLS12_PRF_STATE_SEED_SET;
5251
5252 return( PSA_SUCCESS );
5253}
5254
Janos Follath81550542019-06-13 14:26:34 +01005255static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5256 psa_algorithm_t hash_alg,
5257 const uint8_t *data,
5258 size_t data_length )
5259{
5260 psa_status_t status;
5261 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5262 return( PSA_ERROR_BAD_STATE );
5263
5264 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5265 if( status != PSA_SUCCESS )
5266 return( status );
5267
5268 prf->state = TLS12_PRF_STATE_KEY_SET;
5269
5270 return( PSA_SUCCESS );
5271}
5272
Janos Follath6660f0e2019-06-17 08:44:03 +01005273static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5274 psa_tls12_prf_key_derivation_t *prf,
5275 psa_algorithm_t hash_alg,
5276 const uint8_t *data,
5277 size_t data_length )
5278{
5279 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005280 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5281 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005282
5283 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5284 return( PSA_ERROR_INVALID_ARGUMENT );
5285
5286 /* Quoting RFC 4279, Section 2:
5287 *
5288 * The premaster secret is formed as follows: if the PSK is N octets
5289 * long, concatenate a uint16 with the value N, N zero octets, a second
5290 * uint16 with the value N, and the PSK itself.
5291 */
5292
Janos Follath40e13932019-06-26 13:22:29 +01005293 *cur++ = ( data_length >> 8 ) & 0xff;
5294 *cur++ = ( data_length >> 0 ) & 0xff;
5295 memset( cur, 0, data_length );
5296 cur += data_length;
5297 *cur++ = pms[0];
5298 *cur++ = pms[1];
5299 memcpy( cur, data, data_length );
5300 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005301
Janos Follath40e13932019-06-26 13:22:29 +01005302 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005303
5304 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5305 return( status );
5306}
5307
Janos Follath63028dd2019-06-13 09:15:47 +01005308static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5309 const uint8_t *data,
5310 size_t data_length )
5311{
5312 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5313 return( PSA_ERROR_BAD_STATE );
5314
Janos Follathd6dce9f2019-07-04 09:11:38 +01005315 if( data_length != 0 )
5316 {
5317 prf->label = mbedtls_calloc( 1, data_length );
5318 if( prf->label == NULL )
5319 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005320
Janos Follathd6dce9f2019-07-04 09:11:38 +01005321 memcpy( prf->label, data, data_length );
5322 prf->label_length = data_length;
5323 }
Janos Follath63028dd2019-06-13 09:15:47 +01005324
5325 prf->state = TLS12_PRF_STATE_LABEL_SET;
5326
5327 return( PSA_SUCCESS );
5328}
5329
Janos Follathb03233e2019-06-11 15:30:30 +01005330static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5331 psa_algorithm_t hash_alg,
5332 psa_key_derivation_step_t step,
5333 const uint8_t *data,
5334 size_t data_length )
5335{
Janos Follathb03233e2019-06-11 15:30:30 +01005336 switch( step )
5337 {
Janos Follathf08e2652019-06-13 09:05:41 +01005338 case PSA_KEY_DERIVATION_INPUT_SEED:
5339 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005340 case PSA_KEY_DERIVATION_INPUT_SECRET:
5341 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005342 case PSA_KEY_DERIVATION_INPUT_LABEL:
5343 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005344 default:
5345 return( PSA_ERROR_INVALID_ARGUMENT );
5346 }
5347}
Janos Follath6660f0e2019-06-17 08:44:03 +01005348
5349static psa_status_t psa_tls12_prf_psk_to_ms_input(
5350 psa_tls12_prf_key_derivation_t *prf,
5351 psa_algorithm_t hash_alg,
5352 psa_key_derivation_step_t step,
5353 const uint8_t *data,
5354 size_t data_length )
5355{
5356 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005357 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005358 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5359 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005360 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005361
5362 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5363}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005364#endif /* MBEDTLS_MD_C */
5365
Gilles Peskineb8965192019-09-24 16:21:10 +02005366/** Check whether the given key type is acceptable for the given
5367 * input step of a key derivation.
5368 *
5369 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5370 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5371 * Both secret and non-secret inputs can alternatively have the type
5372 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5373 * that the input was passed as a buffer rather than via a key object.
5374 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005375static int psa_key_derivation_check_input_type(
5376 psa_key_derivation_step_t step,
5377 psa_key_type_t key_type )
5378{
5379 switch( step )
5380 {
5381 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005382 if( key_type == PSA_KEY_TYPE_DERIVE )
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 case PSA_KEY_DERIVATION_INPUT_LABEL:
5388 case PSA_KEY_DERIVATION_INPUT_SALT:
5389 case PSA_KEY_DERIVATION_INPUT_INFO:
5390 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005391 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5392 return( PSA_SUCCESS );
5393 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005394 return( PSA_SUCCESS );
5395 break;
5396 }
5397 return( PSA_ERROR_INVALID_ARGUMENT );
5398}
5399
Janos Follathb80a94e2019-06-12 15:54:46 +01005400static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005401 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005402 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005403 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005404 const uint8_t *data,
5405 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005406{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005407 psa_status_t status;
5408 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5409
5410 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005411 if( status != PSA_SUCCESS )
5412 goto exit;
5413
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005414#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005415 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005416 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005417 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005418 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005419 step, data, data_length );
5420 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005421 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005422 {
Janos Follathb03233e2019-06-11 15:30:30 +01005423 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5424 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5425 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005426 }
5427 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5428 {
5429 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5430 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5431 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005432 }
5433 else
5434#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005435 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005436 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005437 return( PSA_ERROR_BAD_STATE );
5438 }
5439
Gilles Peskine224b0d62019-09-23 18:13:17 +02005440exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005441 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005443 return( status );
5444}
5445
Janos Follath51f4a0f2019-06-14 11:35:55 +01005446psa_status_t psa_key_derivation_input_bytes(
5447 psa_key_derivation_operation_t *operation,
5448 psa_key_derivation_step_t step,
5449 const uint8_t *data,
5450 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005451{
Gilles Peskineb8965192019-09-24 16:21:10 +02005452 return( psa_key_derivation_input_internal( operation, step,
5453 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005454 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005455}
5456
Janos Follath51f4a0f2019-06-14 11:35:55 +01005457psa_status_t psa_key_derivation_input_key(
5458 psa_key_derivation_operation_t *operation,
5459 psa_key_derivation_step_t step,
5460 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005461{
5462 psa_key_slot_t *slot;
5463 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005464
Gilles Peskine28f8f302019-07-24 13:30:31 +02005465 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005466 PSA_KEY_USAGE_DERIVE,
5467 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005468 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005469 {
5470 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005471 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005472 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005473
5474 /* Passing a key object as a SECRET input unlocks the permission
5475 * to output to a key object. */
5476 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5477 operation->can_output_key = 1;
5478
Janos Follathb80a94e2019-06-12 15:54:46 +01005479 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005480 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005481 slot->data.key.data,
5482 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005483}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005484
5485
5486
5487/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005488/* Key agreement */
5489/****************************************************************/
5490
Gilles Peskinea05219c2018-11-16 16:02:56 +01005491#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005492static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5493 size_t peer_key_length,
5494 const mbedtls_ecp_keypair *our_key,
5495 uint8_t *shared_secret,
5496 size_t shared_secret_size,
5497 size_t *shared_secret_length )
5498{
Steven Cooremanacda8342020-07-24 23:09:52 +02005499 mbedtls_ecp_keypair their_key;
5500 psa_key_slot_t their_key_slot;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005501 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005502 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005503 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005504 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005505 mbedtls_ecdh_init( &ecdh );
Steven Cooremanacda8342020-07-24 23:09:52 +02005506 memset(&their_key_slot, 0, sizeof(their_key_slot));
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005507
Steven Cooremanacda8342020-07-24 23:09:52 +02005508 /* Creating ephemeral key slot for import purposes */
5509 their_key_slot.attr.type = PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve);
5510 their_key_slot.data.key.data = (uint8_t*) peer_key;
5511 their_key_slot.data.key.bytes = peer_key_length;
5512
5513 status = psa_load_ecp_representation( &their_key_slot, &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005514 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(
Steven Cooremanacda8342020-07-24 23:09:52 +02005518 mbedtls_ecdh_get_params( &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005519 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005520 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005521 status = mbedtls_to_psa_error(
5522 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5523 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005524 goto exit;
5525
Jaeden Amero97271b32019-01-10 19:38:51 +00005526 status = mbedtls_to_psa_error(
5527 mbedtls_ecdh_calc_secret( &ecdh,
5528 shared_secret_length,
5529 shared_secret, shared_secret_size,
5530 mbedtls_ctr_drbg_random,
5531 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005532 if( status != PSA_SUCCESS )
5533 goto exit;
5534 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5535 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005536
5537exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005538 if( status != PSA_SUCCESS )
5539 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005540 mbedtls_ecdh_free( &ecdh );
Steven Cooremanacda8342020-07-24 23:09:52 +02005541 mbedtls_ecp_keypair_free( &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005542 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005543}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005544#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005545
Gilles Peskine01d718c2018-09-18 12:01:02 +02005546#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5547
Gilles Peskine0216fe12019-04-11 21:23:21 +02005548static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5549 psa_key_slot_t *private_key,
5550 const uint8_t *peer_key,
5551 size_t peer_key_length,
5552 uint8_t *shared_secret,
5553 size_t shared_secret_size,
5554 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005555{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005556 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005557 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005558#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005559 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005560 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005561 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005562 mbedtls_ecp_keypair ecp;
5563 psa_status_t status = psa_load_ecp_representation( private_key, &ecp );
5564 if( status != PSA_SUCCESS )
5565 return status;
5566 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
5567 &ecp,
5568 shared_secret, shared_secret_size,
5569 shared_secret_length );
5570 mbedtls_ecp_keypair_free( &ecp );
5571 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005572#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005573 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005574 (void) private_key;
5575 (void) peer_key;
5576 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005577 (void) shared_secret;
5578 (void) shared_secret_size;
5579 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005580 return( PSA_ERROR_NOT_SUPPORTED );
5581 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005582}
5583
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005584/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005585 * to potentially free embedded data structures and wipe confidential data.
5586 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005587static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005588 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005589 psa_key_slot_t *private_key,
5590 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005591 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005592{
5593 psa_status_t status;
5594 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5595 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005596 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005597
5598 /* Step 1: run the secret agreement algorithm to generate the shared
5599 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005600 status = psa_key_agreement_raw_internal( ka_alg,
5601 private_key,
5602 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005603 shared_secret,
5604 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005605 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005606 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005607 goto exit;
5608
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005609 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005610 * the shared secret. A shared secret is permitted wherever a key
5611 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005612 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005613 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005614 shared_secret,
5615 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005616
Gilles Peskine12313cd2018-06-20 00:20:32 +02005617exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005618 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005619 return( status );
5620}
5621
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005622psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005623 psa_key_derivation_step_t step,
5624 psa_key_handle_t private_key,
5625 const uint8_t *peer_key,
5626 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005627{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005628 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005629 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005630 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005631 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005632 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005633 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005634 if( status != PSA_SUCCESS )
5635 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005636 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005637 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005638 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005639 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005640 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005641 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005642}
5643
Gilles Peskinebe697d82019-05-16 18:00:41 +02005644psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5645 psa_key_handle_t private_key,
5646 const uint8_t *peer_key,
5647 size_t peer_key_length,
5648 uint8_t *output,
5649 size_t output_size,
5650 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005651{
5652 psa_key_slot_t *slot;
5653 psa_status_t status;
5654
5655 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5656 {
5657 status = PSA_ERROR_INVALID_ARGUMENT;
5658 goto exit;
5659 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005660 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005661 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005662 if( status != PSA_SUCCESS )
5663 goto exit;
5664
5665 status = psa_key_agreement_raw_internal( alg, slot,
5666 peer_key, peer_key_length,
5667 output, output_size,
5668 output_length );
5669
5670exit:
5671 if( status != PSA_SUCCESS )
5672 {
5673 /* If an error happens and is not handled properly, the output
5674 * may be used as a key to protect sensitive data. Arrange for such
5675 * a key to be random, which is likely to result in decryption or
5676 * verification errors. This is better than filling the buffer with
5677 * some constant data such as zeros, which would result in the data
5678 * being protected with a reproducible, easily knowable key.
5679 */
5680 psa_generate_random( output, output_size );
5681 *output_length = output_size;
5682 }
5683 return( status );
5684}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005685
5686
5687/****************************************************************/
5688/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005689/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005690
Gilles Peskine4c317f42018-07-12 01:24:09 +02005691psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005692 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005693{
Janos Follath24eed8d2019-11-22 13:21:35 +00005694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005695 GUARD_MODULE_INITIALIZED;
5696
Gilles Peskinef181eca2019-08-07 13:49:00 +02005697 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5698 {
5699 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5700 output,
5701 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5702 if( ret != 0 )
5703 return( mbedtls_to_psa_error( ret ) );
5704 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5705 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5706 }
5707
Gilles Peskinee59236f2018-01-27 23:32:46 +01005708 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5709 return( mbedtls_to_psa_error( ret ) );
5710}
5711
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005712#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5713#include "mbedtls/entropy_poll.h"
5714
Gilles Peskine7228da22019-07-15 11:06:15 +02005715psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005716 size_t seed_size )
5717{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005718 if( global_data.initialized )
5719 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005720
5721 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5722 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5723 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5724 return( PSA_ERROR_INVALID_ARGUMENT );
5725
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005726 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005727}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005728#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005729
Gilles Peskinee56e8782019-04-26 17:34:02 +02005730#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5731static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5732 size_t domain_parameters_size,
5733 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005734{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005735 size_t i;
5736 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005737
Gilles Peskinee56e8782019-04-26 17:34:02 +02005738 if( domain_parameters_size == 0 )
5739 {
5740 *exponent = 65537;
5741 return( PSA_SUCCESS );
5742 }
5743
5744 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5745 * support values that fit in a 32-bit integer, which is larger than
5746 * int on just about every platform anyway. */
5747 if( domain_parameters_size > sizeof( acc ) )
5748 return( PSA_ERROR_NOT_SUPPORTED );
5749 for( i = 0; i < domain_parameters_size; i++ )
5750 acc = ( acc << 8 ) | domain_parameters[i];
5751 if( acc > INT_MAX )
5752 return( PSA_ERROR_NOT_SUPPORTED );
5753 *exponent = acc;
5754 return( PSA_SUCCESS );
5755}
5756#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5757
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005758static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005759 psa_key_slot_t *slot, size_t bits,
5760 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005761{
Gilles Peskine8e338702019-07-30 20:06:31 +02005762 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005763
Gilles Peskinee56e8782019-04-26 17:34:02 +02005764 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005765 return( PSA_ERROR_INVALID_ARGUMENT );
5766
Gilles Peskinee59236f2018-01-27 23:32:46 +01005767 if( key_type_is_raw_bytes( type ) )
5768 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005769 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005770
5771 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005772 if( status != PSA_SUCCESS )
5773 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005774
5775 /* Allocate memory for the key */
5776 slot->data.key.bytes = PSA_BITS_TO_BYTES( bits );
5777 slot->data.key.data = mbedtls_calloc( 1, slot->data.key.bytes );
5778 if( slot->data.key.data == NULL )
5779 {
5780 slot->data.key.bytes = 0;
5781 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5782 }
5783
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005784 status = psa_generate_random( slot->data.key.data,
5785 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005786 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005787 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005788
5789 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005790#if defined(MBEDTLS_DES_C)
5791 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005792 psa_des_set_key_parity( slot->data.key.data,
5793 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005794#endif /* MBEDTLS_DES_C */
5795 }
5796 else
5797
5798#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005799 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005800 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005801 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005802 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005803 int exponent;
5804 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005805 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5806 return( PSA_ERROR_NOT_SUPPORTED );
5807 /* Accept only byte-aligned keys, for the same reasons as
5808 * in psa_import_rsa_key(). */
5809 if( bits % 8 != 0 )
5810 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005811 status = psa_read_rsa_exponent( domain_parameters,
5812 domain_parameters_size,
5813 &exponent );
5814 if( status != PSA_SUCCESS )
5815 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005816 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5817 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005818 mbedtls_ctr_drbg_random,
5819 &global_data.ctr_drbg,
5820 (unsigned int) bits,
5821 exponent );
5822 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005823 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005824
5825 /* Make sure to always have an export representation available */
5826 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5827
5828 slot->data.key.data = mbedtls_calloc( 1, bytes );
5829 if( slot->data.key.data == NULL )
5830 {
5831 mbedtls_rsa_free( &rsa );
5832 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005833 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005834
5835 status = psa_export_rsa_key( type,
5836 &rsa,
5837 slot->data.key.data,
5838 bytes,
5839 &slot->data.key.bytes );
5840 mbedtls_rsa_free( &rsa );
5841 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005842 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005843 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005844 }
5845 else
5846#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5847
5848#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005849 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005850 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005851 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005852 mbedtls_ecp_group_id grp_id =
5853 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005854 const mbedtls_ecp_curve_info *curve_info =
5855 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02005856 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005858 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005859 return( PSA_ERROR_NOT_SUPPORTED );
5860 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5861 return( PSA_ERROR_NOT_SUPPORTED );
5862 if( curve_info->bit_size != bits )
5863 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005864 mbedtls_ecp_keypair_init( &ecp );
5865 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005866 mbedtls_ctr_drbg_random,
5867 &global_data.ctr_drbg );
5868 if( ret != 0 )
5869 {
Steven Cooremanacda8342020-07-24 23:09:52 +02005870 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005871 return( mbedtls_to_psa_error( ret ) );
5872 }
Steven Cooremanacda8342020-07-24 23:09:52 +02005873
5874
5875 /* Make sure to always have an export representation available */
5876 size_t bytes = PSA_BITS_TO_BYTES( bits );
5877 slot->data.key.data = mbedtls_calloc( 1, bytes );
5878 if( slot->data.key.data == NULL )
5879 {
5880 mbedtls_ecp_keypair_free( &ecp );
5881 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5882 }
5883 slot->data.key.bytes = bytes;
5884 psa_status_t status = mbedtls_to_psa_error(
5885 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
5886
5887 mbedtls_ecp_keypair_free( &ecp );
5888 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005889 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005890 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005891 }
5892 else
5893#endif /* MBEDTLS_ECP_C */
5894
5895 return( PSA_ERROR_NOT_SUPPORTED );
5896
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005897 return( PSA_SUCCESS );
5898}
Darryl Green0c6575a2018-11-07 16:05:30 +00005899
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005900psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005901 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005902{
5903 psa_status_t status;
5904 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005905 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005906
Gilles Peskine0f84d622019-09-12 19:03:13 +02005907 /* Reject any attempt to create a zero-length key so that we don't
5908 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5909 if( psa_get_key_bits( attributes ) == 0 )
5910 return( PSA_ERROR_INVALID_ARGUMENT );
5911
Gilles Peskinedf179142019-07-15 22:02:14 +02005912 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5913 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005914 if( status != PSA_SUCCESS )
5915 goto exit;
5916
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005917#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5918 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005919 {
Gilles Peskine11792082019-08-06 18:36:36 +02005920 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5921 size_t pubkey_length = 0; /* We don't support this feature yet */
5922 if( drv->key_management == NULL ||
5923 drv->key_management->p_generate == NULL )
5924 {
5925 status = PSA_ERROR_NOT_SUPPORTED;
5926 goto exit;
5927 }
5928 status = drv->key_management->p_generate(
5929 psa_get_se_driver_context( driver ),
5930 slot->data.se.slot_number, attributes,
5931 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005932 }
Gilles Peskine11792082019-08-06 18:36:36 +02005933 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005934#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005935 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005936 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005937 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005938 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005939 }
Gilles Peskine11792082019-08-06 18:36:36 +02005940
5941exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005942 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005943 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005944 if( status != PSA_SUCCESS )
5945 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005946 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005947 *handle = 0;
5948 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005949 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005950}
5951
5952
Gilles Peskinee59236f2018-01-27 23:32:46 +01005953
5954/****************************************************************/
5955/* Module setup */
5956/****************************************************************/
5957
Gilles Peskine5e769522018-11-20 21:59:56 +01005958psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5959 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5960 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5961{
5962 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5963 return( PSA_ERROR_BAD_STATE );
5964 global_data.entropy_init = entropy_init;
5965 global_data.entropy_free = entropy_free;
5966 return( PSA_SUCCESS );
5967}
5968
Gilles Peskinee59236f2018-01-27 23:32:46 +01005969void mbedtls_psa_crypto_free( void )
5970{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005971 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005972 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5973 {
5974 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005975 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005976 }
5977 /* Wipe all remaining data, including configuration.
5978 * In particular, this sets all state indicator to the value
5979 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005980 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005981#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005982 /* Unregister all secure element drivers, so that we restart from
5983 * a pristine state. */
5984 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005985#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005986}
5987
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005988#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5989/** Recover a transaction that was interrupted by a power failure.
5990 *
5991 * This function is called during initialization, before psa_crypto_init()
5992 * returns. If this function returns a failure status, the initialization
5993 * fails.
5994 */
5995static psa_status_t psa_crypto_recover_transaction(
5996 const psa_crypto_transaction_t *transaction )
5997{
5998 switch( transaction->unknown.type )
5999 {
6000 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6001 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006002 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006003 * is implemented.
6004 * https://github.com/ARMmbed/mbed-crypto/issues/218
6005 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006006 default:
6007 /* We found an unsupported transaction in the storage.
6008 * We don't know what state the storage is in. Give up. */
6009 return( PSA_ERROR_STORAGE_FAILURE );
6010 }
6011}
6012#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6013
Gilles Peskinee59236f2018-01-27 23:32:46 +01006014psa_status_t psa_crypto_init( void )
6015{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006016 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006017 const unsigned char drbg_seed[] = "PSA";
6018
Gilles Peskinec6b69072018-11-20 21:42:52 +01006019 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006020 if( global_data.initialized != 0 )
6021 return( PSA_SUCCESS );
6022
Gilles Peskine5e769522018-11-20 21:59:56 +01006023 /* Set default configuration if
6024 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6025 if( global_data.entropy_init == NULL )
6026 global_data.entropy_init = mbedtls_entropy_init;
6027 if( global_data.entropy_free == NULL )
6028 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006029
Gilles Peskinec6b69072018-11-20 21:42:52 +01006030 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006031 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006032#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6033 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6034 /* The PSA entropy injection feature depends on using NV seed as an entropy
6035 * source. Add NV seed as an entropy source for PSA entropy injection. */
6036 mbedtls_entropy_add_source( &global_data.entropy,
6037 mbedtls_nv_seed_poll, NULL,
6038 MBEDTLS_ENTROPY_BLOCK_SIZE,
6039 MBEDTLS_ENTROPY_SOURCE_STRONG );
6040#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006041 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006042 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006043 status = mbedtls_to_psa_error(
6044 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6045 mbedtls_entropy_func,
6046 &global_data.entropy,
6047 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6048 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006049 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006050 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006051
Gilles Peskine66fb1262018-12-10 16:29:04 +01006052 status = psa_initialize_key_slots( );
6053 if( status != PSA_SUCCESS )
6054 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006055
Gilles Peskined9348f22019-10-01 15:22:29 +02006056#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6057 status = psa_init_all_se_drivers( );
6058 if( status != PSA_SUCCESS )
6059 goto exit;
6060#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6061
Gilles Peskine4b734222019-07-24 15:56:31 +02006062#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006063 status = psa_crypto_load_transaction( );
6064 if( status == PSA_SUCCESS )
6065 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006066 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6067 if( status != PSA_SUCCESS )
6068 goto exit;
6069 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006070 }
6071 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6072 {
6073 /* There's no transaction to complete. It's all good. */
6074 status = PSA_SUCCESS;
6075 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006076#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006077
Gilles Peskinec6b69072018-11-20 21:42:52 +01006078 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006079 global_data.initialized = 1;
6080
6081exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006082 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006083 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006084 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006085}
6086
6087#endif /* MBEDTLS_PSA_CRYPTO_C */