blob: dfd37ae4e7c283ffae72ac56a70e84bd2f3c0efa [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 Cooreman7f391872020-07-30 14:57:44 +0200521/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200522 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200523 * \param[in] type The type of key contained in \p data.
524 * \param[in] data The buffer from which to load the representation.
525 * \param[in] data_length The size in bytes of \p data.
526 * \param[out] p_rsa Returns a pointer to an RSA context on success.
527 * The caller is responsible for freeing both the
528 * contents of the context and the context itself
529 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200530 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200531static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
532 const uint8_t *data,
533 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200534 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200535{
Steven Cooremana01795d2020-07-24 22:48:15 +0200536#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000537 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200538 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000539 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200540 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000541
542 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200543 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000544 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200545 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200546 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000547 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200548 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000549 if( status != PSA_SUCCESS )
550 goto exit;
551
552 /* We have something that the pkparse module recognizes. If it is a
553 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200554 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200555 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556 status = PSA_ERROR_INVALID_ARGUMENT;
557 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200558 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000560 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
561 * supports non-byte-aligned key sizes, but not well. For example,
562 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200563 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000564 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
565 {
566 status = PSA_ERROR_NOT_SUPPORTED;
567 goto exit;
568 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200569 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
570
571 if( status != PSA_SUCCESS )
572 goto exit;
573
Steven Cooremana2371e52020-07-28 14:30:39 +0200574 /* Copy out the pointer to the RSA context, and reset the PK context
575 * such that pk_free doesn't free the RSA context we just grabbed. */
576 *p_rsa = mbedtls_pk_rsa( ctx );
577 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000578
579exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200580 mbedtls_pk_free( &ctx );
581 return( status );
582#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200583 (void) data;
584 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200585 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200586 (void) rsa;
587 return( PSA_ERROR_NOT_SUPPORTED );
588#endif /* MBEDTLS_PK_PARSE_C */
589}
590
591/** Export an RSA key to export representation
592 *
593 * \param[in] type The type of key (public/private) to export
594 * \param[in] rsa The internal RSA representation from which to export
595 * \param[out] data The buffer to export to
596 * \param[in] data_size The length of the buffer to export to
597 * \param[out] data_length The amount of bytes written to \p data
598 */
599static psa_status_t psa_export_rsa_key( psa_key_type_t type,
600 mbedtls_rsa_context *rsa,
601 uint8_t *data,
602 size_t data_size,
603 size_t *data_length )
604{
605#if defined(MBEDTLS_PK_WRITE_C)
606 int ret;
607 mbedtls_pk_context pk;
608 uint8_t *pos = data + data_size;
609
610 mbedtls_pk_init( &pk );
611 pk.pk_info = &mbedtls_rsa_info;
612 pk.pk_ctx = rsa;
613
614 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200615 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
616 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200617 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
618 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
619 else
620 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
621
622 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200623 {
624 /* Clean up in case pk_write failed halfway through. */
625 memset( data, 0, data_size );
Steven Cooremana01795d2020-07-24 22:48:15 +0200626 return mbedtls_to_psa_error( ret );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200627 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200628
629 /* The mbedtls_pk_xxx functions write to the end of the buffer.
630 * Move the data to the beginning and erase remaining data
631 * at the original location. */
632 if( 2 * (size_t) ret <= data_size )
633 {
634 memcpy( data, data + data_size - ret, ret );
635 memset( data + data_size - ret, 0, ret );
636 }
637 else if( (size_t) ret < data_size )
638 {
639 memmove( data, data + data_size - ret, ret );
640 memset( data + ret, 0, data_size - ret );
641 }
642
643 *data_length = ret;
644 return( PSA_SUCCESS );
645#else
646 (void) type;
647 (void) rsa;
648 (void) data;
649 (void) data_size;
650 (void) data_length;
651 return( PSA_ERROR_NOT_SUPPORTED );
652#endif /* MBEDTLS_PK_WRITE_C */
653}
654
655/** Import an RSA key from import representation to a slot
656 *
657 * \param[in,out] slot The slot where to store the export representation to
658 * \param[in] data The buffer containing the import representation
659 * \param[in] data_length The amount of bytes in \p data
660 */
661static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
662 const uint8_t *data,
663 size_t data_length )
664{
665 psa_status_t status;
666 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200667 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200668
Steven Cooremana01795d2020-07-24 22:48:15 +0200669 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200670 status = psa_load_rsa_representation( slot->attr.type,
671 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200672 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200673 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200674 if( status != PSA_SUCCESS )
675 goto exit;
676
677 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200678 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200679
Steven Cooreman75b74362020-07-28 14:30:13 +0200680 /* Re-export the data to PSA export format, such that we can store export
681 * representation in the key slot. Export representation in case of RSA is
682 * the smallest representation that's allowed as input, so a straight-up
683 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200684 output = mbedtls_calloc( 1, data_length );
685
686 if( output == NULL )
687 {
688 status = PSA_ERROR_INSUFFICIENT_MEMORY;
689 goto exit;
690 }
691
Steven Cooremana01795d2020-07-24 22:48:15 +0200692 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200693 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200694 output,
695 data_length,
696 &data_length);
697
698exit:
699 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200700 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200701 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200702
703 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000704 if( status != PSA_SUCCESS )
705 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200706 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000707 return( status );
708 }
709
Steven Cooremana01795d2020-07-24 22:48:15 +0200710 /* On success, store the allocated export-formatted key. */
711 slot->data.key.data = output;
712 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000713
714 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200715}
Steven Cooremana01795d2020-07-24 22:48:15 +0200716#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200717
Jaeden Ameroccdce902019-01-10 11:42:27 +0000718#if defined(MBEDTLS_ECP_C)
Steven Cooreman7f391872020-07-30 14:57:44 +0200719/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200720 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200721 * \param[in] type The type of key contained in \p data.
722 * \param[in] data The buffer from which to load the representation.
723 * \param[in] data_length The size in bytes of \p data.
724 * \param[out] p_ecp Returns a pointer to an ECP context on success.
725 * The caller is responsible for freeing both the
726 * contents of the context and the context itself
727 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200728 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200729static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
730 const uint8_t *data,
731 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200732 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100733{
734 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200735 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200736 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200737 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100738
Steven Cooreman4fed4552020-08-03 14:46:03 +0200739 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
740 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100741 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200742 /* A Weierstrass public key is represented as:
743 * - The byte 0x04;
744 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
745 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
746 * So its data length is 2m+1 where n is the key size in bits.
747 */
748 if( ( data_length & 1 ) == 0 )
749 return( PSA_ERROR_INVALID_ARGUMENT );
750 curve_size = data_length / 2;
751
752 /* Montgomery public keys are represented in compressed format, meaning
753 * their curve_size is equal to the amount of input. */
754
755 /* Private keys are represented in uncompressed private random integer
756 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100757 }
758
Steven Cooreman6d839f02020-07-30 11:36:45 +0200759 /* Allocate and initialize a key representation. */
760 ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
761 if( ecp == NULL )
762 return PSA_ERROR_INSUFFICIENT_MEMORY;
Steven Cooremana2371e52020-07-28 14:30:39 +0200763 mbedtls_ecp_keypair_init( ecp );
764
Gilles Peskine4cd32772019-12-02 20:49:42 +0100765 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200766 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
767 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100768 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200769 {
770 status = PSA_ERROR_INVALID_ARGUMENT;
771 goto exit;
772 }
773
Steven Cooremanacda8342020-07-24 23:09:52 +0200774 status = mbedtls_to_psa_error(
775 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
776 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200777 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200778
Steven Cooreman6d839f02020-07-30 11:36:45 +0200779 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200780 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200781 {
782 /* Load the public value. */
783 status = mbedtls_to_psa_error(
784 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200785 data,
786 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200787 if( status != PSA_SUCCESS )
788 goto exit;
789
790 /* Check that the point is on the curve. */
791 status = mbedtls_to_psa_error(
792 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
793 if( status != PSA_SUCCESS )
794 goto exit;
795 }
796 else
797 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200798 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200799 status = mbedtls_to_psa_error(
800 mbedtls_ecp_read_key( ecp->grp.id,
801 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200802 data,
803 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200804
805 if( status != PSA_SUCCESS )
806 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200807 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200808
809 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200810exit:
811 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200812 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200813 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200814 mbedtls_free( ecp );
815 }
816
Steven Cooremanacda8342020-07-24 23:09:52 +0200817 return status;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100818}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000819
Steven Cooreman4fed4552020-08-03 14:46:03 +0200820/** Export an ECP key to export representation
821 *
822 * \param[in] type The type of key (public/private) to export
823 * \param[in] ecp The internal ECP representation from which to export
824 * \param[out] data The buffer to export to
825 * \param[in] data_size The length of the buffer to export to
826 * \param[out] data_length The amount of bytes written to \p data
827 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200828static psa_status_t psa_export_ecp_key( psa_key_type_t type,
829 mbedtls_ecp_keypair *ecp,
830 uint8_t *data,
831 size_t data_size,
832 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200833{
Steven Cooremanacda8342020-07-24 23:09:52 +0200834 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000835
Steven Cooremanacda8342020-07-24 23:09:52 +0200836 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200837 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200838 /* Check whether the public part is loaded */
839 if( mbedtls_ecp_is_zero( &ecp->Q ) )
840 {
841 /* Calculate the public key */
842 status = mbedtls_to_psa_error(
843 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
844 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
845 if( status != PSA_SUCCESS )
846 return status;
847 }
848
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200850 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
851 MBEDTLS_ECP_PF_UNCOMPRESSED,
852 data_length,
853 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200854 data_size ) );
855
856 if( status != PSA_SUCCESS )
857 memset( data, 0, data_size );
858
859 return status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200860 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200861 else
862 {
863 if( data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits) )
864 return( PSA_ERROR_BUFFER_TOO_SMALL );
865
866 status = mbedtls_to_psa_error(
867 mbedtls_ecp_write_key( ecp,
868 data,
869 PSA_BITS_TO_BYTES(ecp->grp.nbits) ) );
870 if( status == PSA_SUCCESS )
871 {
872 *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits);
873 }
874
875 return( status );
876 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200877}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200878
Steven Cooreman4fed4552020-08-03 14:46:03 +0200879/** Import an ECP key from import representation to a slot
880 *
881 * \param[in,out] slot The slot where to store the export representation to
882 * \param[in] data The buffer containing the import representation
883 * \param[in] data_length The amount of bytes in \p data
884 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200885static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
886 const uint8_t *data,
887 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100888{
Steven Cooremanacda8342020-07-24 23:09:52 +0200889 psa_status_t status;
890 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200891 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100892
Steven Cooremanacda8342020-07-24 23:09:52 +0200893 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200894 status = psa_load_ecp_representation( slot->attr.type,
895 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200896 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200897 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100898 if( status != PSA_SUCCESS )
899 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100900
Steven Cooremanacda8342020-07-24 23:09:52 +0200901 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200902 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200903 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200904 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200905
Steven Cooremanacda8342020-07-24 23:09:52 +0200906 /* Re-export the data to PSA export format. There is currently no support
907 * for other input formats then the export format, so this is a 1-1
908 * copy operation. */
909 output = mbedtls_calloc( 1, data_length );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100910
Steven Cooremanacda8342020-07-24 23:09:52 +0200911 if( output == NULL )
912 {
913 status = PSA_ERROR_INSUFFICIENT_MEMORY;
914 goto exit;
915 }
916
917 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200918 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200919 output,
920 data_length,
921 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100922
923exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200924 /* Always free the PK object (will also free contained ECP context) */
925 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200926 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200927
928 /* Free the allocated buffer only on error. */
929 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100930 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200931 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200932 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100933 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200934
935 /* On success, store the allocated export-formatted key. */
936 slot->data.key.data = output;
937 slot->data.key.bytes = data_length;
938
939 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100940}
941#endif /* defined(MBEDTLS_ECP_C) */
942
Gilles Peskineb46bef22019-07-30 21:32:04 +0200943/** Return the size of the key in the given slot, in bits.
944 *
945 * \param[in] slot A key slot.
946 *
947 * \return The key size in bits, read from the metadata in the slot.
948 */
949static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
950{
951 return( slot->attr.bits );
952}
953
Steven Cooreman75b74362020-07-28 14:30:13 +0200954/** Try to allocate a buffer to an empty key slot.
955 *
956 * \param[in,out] slot Key slot to attach buffer to.
957 * \param[in] buffer_length Requested size of the buffer.
958 *
959 * \retval #PSA_SUCCESS
960 * The buffer has been successfully allocated.
961 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
962 * Not enough memory was available for allocation.
963 * \retval #PSA_ERROR_ALREADY_EXISTS
964 * Trying to allocate a buffer to a non-empty key slot.
965 */
966static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
967 size_t buffer_length )
968{
969 if( slot->data.key.data != NULL )
970 return PSA_ERROR_ALREADY_EXISTS;
971
972 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
973 if( slot->data.key.data == NULL )
974 return PSA_ERROR_INSUFFICIENT_MEMORY;
975
976 slot->data.key.bytes = buffer_length;
977 return PSA_SUCCESS;
978}
979
Gilles Peskine8e338702019-07-30 20:06:31 +0200980/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100981 * previously. This function assumes that the slot does not contain
982 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100983psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
984 const uint8_t *data,
985 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200987 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200989 /* zero-length keys are never supported. */
990 if( data_length == 0 )
991 return( PSA_ERROR_NOT_SUPPORTED );
992
Gilles Peskine8e338702019-07-30 20:06:31 +0200993 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200995 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200996
Steven Cooreman75b74362020-07-28 14:30:13 +0200997 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
998 if( data_length > SIZE_MAX / 8 )
999 return( PSA_ERROR_NOT_SUPPORTED );
1000
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001001 /* Enforce a size limit, and in particular ensure that the bit
1002 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001003 if( bit_size > PSA_MAX_KEY_BITS )
1004 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001005
1006 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001007 if( status != PSA_SUCCESS )
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001008 return status;
1009
1010 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02001011 status = psa_allocate_buffer_to_slot( slot, data_length );
1012 if( status != PSA_SUCCESS )
1013 return status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001014
1015 /* copy key into allocated buffer */
1016 memcpy(slot->data.key.data, data, data_length);
1017
1018 /* Write the actual key size to the slot.
1019 * psa_start_key_creation() wrote the size declared by the
1020 * caller, which may be 0 (meaning unspecified) or wrong. */
1021 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001023 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1024 {
Gilles Peskinef76aa772018-10-29 19:24:33 +01001025#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +02001026 status = psa_import_ecp_key( slot,
1027 data, data_length );
1028#else
1029 /* No drivers have been implemented yet, so without mbed TLS backing
1030 * there's no way to do ECP with the current library. */
1031 return( PSA_ERROR_NOT_SUPPORTED );
1032#endif /* defined(MBEDTLS_ECP_C) */
1033 }
1034 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +01001035 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001036#if defined(MBEDTLS_RSA_C)
1037 status = psa_import_rsa_key( slot,
1038 data, data_length );
1039#else
1040 /* No drivers have been implemented yet, so without mbed TLS backing
1041 * there's no way to do RSA with the current library. */
1042 status = PSA_ERROR_NOT_SUPPORTED;
1043#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001044 }
1045 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001046 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001047 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001048 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001049 }
Darryl Green940d72c2018-07-13 13:18:51 +01001050
Darryl Green06fd18d2018-07-16 11:21:11 +01001051 return( status );
1052}
1053
Gilles Peskinef603c712019-01-19 13:40:11 +01001054/** Calculate the intersection of two algorithm usage policies.
1055 *
1056 * Return 0 (which allows no operation) on incompatibility.
1057 */
1058static psa_algorithm_t psa_key_policy_algorithm_intersection(
1059 psa_algorithm_t alg1,
1060 psa_algorithm_t alg2 )
1061{
Gilles Peskine549ea862019-05-22 11:45:59 +02001062 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001063 if( alg1 == alg2 )
1064 return( alg1 );
1065 /* If the policies are from the same hash-and-sign family, check
1066 * if one is a wildcard. If so the other has the specific algorithm. */
1067 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1068 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1069 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1070 {
1071 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1072 return( alg2 );
1073 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1074 return( alg1 );
1075 }
1076 /* If the policies are incompatible, allow nothing. */
1077 return( 0 );
1078}
1079
Gilles Peskined6f371b2019-05-10 19:33:38 +02001080static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1081 psa_algorithm_t requested_alg )
1082{
Gilles Peskine549ea862019-05-22 11:45:59 +02001083 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001084 if( requested_alg == policy_alg )
1085 return( 1 );
1086 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001087 * and requested_alg is the same hash-and-sign family with any hash,
1088 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001089 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1090 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1091 {
1092 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1093 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1094 }
1095 /* If it isn't permitted, it's forbidden. */
1096 return( 0 );
1097}
1098
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001099/** Test whether a policy permits an algorithm.
1100 *
1101 * The caller must test usage flags separately.
1102 */
1103static int psa_key_policy_permits( const psa_key_policy_t *policy,
1104 psa_algorithm_t alg )
1105{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001106 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1107 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001108}
1109
Gilles Peskinef603c712019-01-19 13:40:11 +01001110/** Restrict a key policy based on a constraint.
1111 *
1112 * \param[in,out] policy The policy to restrict.
1113 * \param[in] constraint The policy constraint to apply.
1114 *
1115 * \retval #PSA_SUCCESS
1116 * \c *policy contains the intersection of the original value of
1117 * \c *policy and \c *constraint.
1118 * \retval #PSA_ERROR_INVALID_ARGUMENT
1119 * \c *policy and \c *constraint are incompatible.
1120 * \c *policy is unchanged.
1121 */
1122static psa_status_t psa_restrict_key_policy(
1123 psa_key_policy_t *policy,
1124 const psa_key_policy_t *constraint )
1125{
1126 psa_algorithm_t intersection_alg =
1127 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001128 psa_algorithm_t intersection_alg2 =
1129 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001130 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1131 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001132 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1133 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001134 policy->usage &= constraint->usage;
1135 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001136 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001137 return( PSA_SUCCESS );
1138}
1139
Darryl Green06fd18d2018-07-16 11:21:11 +01001140/** Retrieve a slot which must contain a key. The key must have allow all the
1141 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1142 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001143static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001144 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001145 psa_key_usage_t usage,
1146 psa_algorithm_t alg )
1147{
1148 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001149 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001150
1151 *p_slot = NULL;
1152
Gilles Peskinec5487a82018-12-03 18:08:14 +01001153 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001154 if( status != PSA_SUCCESS )
1155 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001156
1157 /* Enforce that usage policy for the key slot contains all the flags
1158 * required by the usage parameter. There is one exception: public
1159 * keys can always be exported, so we treat public key objects as
1160 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001161 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001162 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001163 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001164 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001165
1166 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001167 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001168 return( PSA_ERROR_NOT_PERMITTED );
1169
1170 *p_slot = slot;
1171 return( PSA_SUCCESS );
1172}
Darryl Green940d72c2018-07-13 13:18:51 +01001173
Gilles Peskine28f8f302019-07-24 13:30:31 +02001174/** Retrieve a slot which must contain a transparent key.
1175 *
1176 * A transparent key is a key for which the key material is directly
1177 * available, as opposed to a key in a secure element.
1178 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001179 * This is a temporary function to use instead of psa_get_key_from_slot()
1180 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001181 */
1182#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1183static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1184 psa_key_slot_t **p_slot,
1185 psa_key_usage_t usage,
1186 psa_algorithm_t alg )
1187{
1188 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1189 if( status != PSA_SUCCESS )
1190 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001191 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001192 {
1193 *p_slot = NULL;
1194 return( PSA_ERROR_NOT_SUPPORTED );
1195 }
1196 return( PSA_SUCCESS );
1197}
1198#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1199/* With no secure element support, all keys are transparent. */
1200#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1201 psa_get_key_from_slot( handle, p_slot, usage, alg )
1202#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1203
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001204/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001205static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001206{
Gilles Peskine73167e12019-07-12 23:44:37 +02001207#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1208 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001209 {
1210 /* No key material to clean. */
1211 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001212 else
1213#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001214 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001215 {
1216 /* No key material to clean. */
1217 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001218 else if( key_type_is_raw_bytes( slot->attr.type ) ||
1219 PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1220 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001221 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001222 mbedtls_free( slot->data.key.data );
1223 slot->data.key.data = NULL;
1224 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001225 }
1226 else
Darryl Green40225ba2018-11-15 14:48:15 +00001227 {
1228 /* Shouldn't happen: the key type is not any type that we
1229 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001230 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001231 }
1232
1233 return( PSA_SUCCESS );
1234}
1235
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001236/** Completely wipe a slot in memory, including its policy.
1237 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001238psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001239{
1240 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001241 /* Multipart operations may still be using the key. This is safe
1242 * because all multipart operation objects are independent from
1243 * the key slot: if they need to access the key after the setup
1244 * phase, they have a copy of the key. Note that this means that
1245 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001246 /* At this point, key material and other type-specific content has
1247 * been wiped. Clear remaining metadata. We can call memset and not
1248 * zeroize because the metadata is not particularly sensitive. */
1249 memset( slot, 0, sizeof( *slot ) );
1250 return( status );
1251}
1252
Gilles Peskinec5487a82018-12-03 18:08:14 +01001253psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001255 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001256 psa_status_t status; /* status of the last operation */
1257 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001258#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1259 psa_se_drv_table_entry_t *driver;
1260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001261
Gilles Peskine1841cf42019-10-08 15:48:25 +02001262 if( handle == 0 )
1263 return( PSA_SUCCESS );
1264
Gilles Peskinec5487a82018-12-03 18:08:14 +01001265 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001266 if( status != PSA_SUCCESS )
1267 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001268
1269#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001270 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001271 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001272 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001273 /* For a key in a secure element, we need to do three things:
1274 * remove the key file in internal storage, destroy the
1275 * key inside the secure element, and update the driver's
1276 * persistent data. Start a transaction that will encompass these
1277 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001278 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001279 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001280 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001281 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001282 status = psa_crypto_save_transaction( );
1283 if( status != PSA_SUCCESS )
1284 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001285 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001286 /* We should still try to destroy the key in the secure
1287 * element and the key metadata in storage. This is especially
1288 * important if the error is that the storage is full.
1289 * But how to do it exactly without risking an inconsistent
1290 * state after a reset?
1291 * https://github.com/ARMmbed/mbed-crypto/issues/215
1292 */
1293 overall_status = status;
1294 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001295 }
1296
Gilles Peskine354f7672019-07-12 23:46:38 +02001297 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001298 if( overall_status == PSA_SUCCESS )
1299 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001300 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001301#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1302
Darryl Greend49a4992018-06-18 17:27:26 +01001303#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001304 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001305 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001306 status = psa_destroy_persistent_key( slot->attr.id );
1307 if( overall_status == PSA_SUCCESS )
1308 overall_status = status;
1309
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001310 /* TODO: other slots may have a copy of the same key. We should
1311 * invalidate them.
1312 * https://github.com/ARMmbed/mbed-crypto/issues/214
1313 */
Darryl Greend49a4992018-06-18 17:27:26 +01001314 }
1315#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001316
Gilles Peskinefc762652019-07-22 19:30:34 +02001317#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1318 if( driver != NULL )
1319 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001320 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001321 if( overall_status == PSA_SUCCESS )
1322 overall_status = status;
1323 status = psa_crypto_stop_transaction( );
1324 if( overall_status == PSA_SUCCESS )
1325 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001326 }
1327#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1328
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001329#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1330exit:
1331#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001332 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001333 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1334 if( overall_status == PSA_SUCCESS )
1335 overall_status = status;
1336 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001337}
1338
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001339void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001340{
Gilles Peskineb699f072019-04-26 16:06:02 +02001341 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001342 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001343}
1344
Gilles Peskineb699f072019-04-26 16:06:02 +02001345psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1346 psa_key_type_t type,
1347 const uint8_t *data,
1348 size_t data_length )
1349{
1350 uint8_t *copy = NULL;
1351
1352 if( data_length != 0 )
1353 {
1354 copy = mbedtls_calloc( 1, data_length );
1355 if( copy == NULL )
1356 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1357 memcpy( copy, data, data_length );
1358 }
1359 /* After this point, this function is guaranteed to succeed, so it
1360 * can start modifying `*attributes`. */
1361
1362 if( attributes->domain_parameters != NULL )
1363 {
1364 mbedtls_free( attributes->domain_parameters );
1365 attributes->domain_parameters = NULL;
1366 attributes->domain_parameters_size = 0;
1367 }
1368
1369 attributes->domain_parameters = copy;
1370 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001371 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001372 return( PSA_SUCCESS );
1373}
1374
1375psa_status_t psa_get_key_domain_parameters(
1376 const psa_key_attributes_t *attributes,
1377 uint8_t *data, size_t data_size, size_t *data_length )
1378{
1379 if( attributes->domain_parameters_size > data_size )
1380 return( PSA_ERROR_BUFFER_TOO_SMALL );
1381 *data_length = attributes->domain_parameters_size;
1382 if( attributes->domain_parameters_size != 0 )
1383 memcpy( data, attributes->domain_parameters,
1384 attributes->domain_parameters_size );
1385 return( PSA_SUCCESS );
1386}
1387
1388#if defined(MBEDTLS_RSA_C)
1389static psa_status_t psa_get_rsa_public_exponent(
1390 const mbedtls_rsa_context *rsa,
1391 psa_key_attributes_t *attributes )
1392{
1393 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001395 uint8_t *buffer = NULL;
1396 size_t buflen;
1397 mbedtls_mpi_init( &mpi );
1398
1399 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1400 if( ret != 0 )
1401 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001402 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1403 {
1404 /* It's the default value, which is reported as an empty string,
1405 * so there's nothing to do. */
1406 goto exit;
1407 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001408
1409 buflen = mbedtls_mpi_size( &mpi );
1410 buffer = mbedtls_calloc( 1, buflen );
1411 if( buffer == NULL )
1412 {
1413 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1414 goto exit;
1415 }
1416 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1417 if( ret != 0 )
1418 goto exit;
1419 attributes->domain_parameters = buffer;
1420 attributes->domain_parameters_size = buflen;
1421
1422exit:
1423 mbedtls_mpi_free( &mpi );
1424 if( ret != 0 )
1425 mbedtls_free( buffer );
1426 return( mbedtls_to_psa_error( ret ) );
1427}
1428#endif /* MBEDTLS_RSA_C */
1429
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001430/** Retrieve all the publicly-accessible attributes of a key.
1431 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001432psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1433 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001434{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001435 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001436 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001437
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001438 psa_reset_key_attributes( attributes );
1439
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001440 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001441 if( status != PSA_SUCCESS )
1442 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001443
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001444 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001445 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1446 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1447
1448#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1449 if( psa_key_slot_is_external( slot ) )
1450 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1451#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001452
Gilles Peskine8e338702019-07-30 20:06:31 +02001453 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001454 {
1455#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001456 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001457 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001458#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001459 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001460 * is not yet implemented.
1461 * https://github.com/ARMmbed/mbed-crypto/issues/216
1462 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001463 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001464 break;
1465#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001466 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001467 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001468
Steven Cooreman4fed4552020-08-03 14:46:03 +02001469 status = psa_load_rsa_representation( slot->attr.type,
1470 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001471 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001472 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001473 if( status != PSA_SUCCESS )
1474 break;
1475
Steven Cooremana2371e52020-07-28 14:30:39 +02001476 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001477 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001478 mbedtls_rsa_free( rsa );
1479 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001480 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001481 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001482#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001483 default:
1484 /* Nothing else to do. */
1485 break;
1486 }
1487
1488 if( status != PSA_SUCCESS )
1489 psa_reset_key_attributes( attributes );
1490 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001491}
1492
Gilles Peskinec8000c02019-08-02 20:15:51 +02001493#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1494psa_status_t psa_get_key_slot_number(
1495 const psa_key_attributes_t *attributes,
1496 psa_key_slot_number_t *slot_number )
1497{
1498 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1499 {
1500 *slot_number = attributes->slot_number;
1501 return( PSA_SUCCESS );
1502 }
1503 else
1504 return( PSA_ERROR_INVALID_ARGUMENT );
1505}
1506#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1507
Steven Cooremana01795d2020-07-24 22:48:15 +02001508static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1509 uint8_t *data,
1510 size_t data_size,
1511 size_t *data_length )
1512{
1513 if( slot->data.key.bytes > data_size )
1514 return( PSA_ERROR_BUFFER_TOO_SMALL );
1515 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1516 memset( data + slot->data.key.bytes, 0,
1517 data_size - slot->data.key.bytes );
1518 *data_length = slot->data.key.bytes;
1519 return( PSA_SUCCESS );
1520}
1521
Gilles Peskinef603c712019-01-19 13:40:11 +01001522static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1523 uint8_t *data,
1524 size_t data_size,
1525 size_t *data_length,
1526 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001527{
Gilles Peskine5d309672019-07-12 23:47:28 +02001528#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1529 const psa_drv_se_t *drv;
1530 psa_drv_se_context_t *drv_context;
1531#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1532
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001533 *data_length = 0;
1534
Gilles Peskine8e338702019-07-30 20:06:31 +02001535 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001536 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001537
Gilles Peskinef9168942019-09-12 19:20:29 +02001538 /* Reject a zero-length output buffer now, since this can never be a
1539 * valid key representation. This way we know that data must be a valid
1540 * pointer and we can do things like memset(data, ..., data_size). */
1541 if( data_size == 0 )
1542 return( PSA_ERROR_BUFFER_TOO_SMALL );
1543
Gilles Peskine5d309672019-07-12 23:47:28 +02001544#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001545 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001546 {
1547 psa_drv_se_export_key_t method;
1548 if( drv->key_management == NULL )
1549 return( PSA_ERROR_NOT_SUPPORTED );
1550 method = ( export_public_key ?
1551 drv->key_management->p_export_public :
1552 drv->key_management->p_export );
1553 if( method == NULL )
1554 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001555 return( method( drv_context,
1556 slot->data.se.slot_number,
1557 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001558 }
1559#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1560
Gilles Peskine8e338702019-07-30 20:06:31 +02001561 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001562 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001563 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001564 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001565 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1566 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001567 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001568 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001569 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001570 /* Exporting public -> public */
1571 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1572 }
1573 else if( !export_public_key )
1574 {
1575 /* Exporting private -> private */
1576 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1577 }
1578 /* Need to export the public part of a private key,
1579 * so conversion is needed */
1580 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1581 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001582#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001583 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001584 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001585 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001586 slot->data.key.data,
1587 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001588 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001589 if( status != PSA_SUCCESS )
1590 return status;
Steven Cooremana01795d2020-07-24 22:48:15 +02001591
Steven Cooreman560c28a2020-07-24 23:20:24 +02001592 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001593 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001594 data,
1595 data_size,
1596 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001597
Steven Cooremana2371e52020-07-28 14:30:39 +02001598 mbedtls_rsa_free( rsa );
1599 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001600
Steven Cooreman560c28a2020-07-24 23:20:24 +02001601 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001602#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001603 /* We don't know how to convert a private RSA key to public. */
1604 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001605#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001606 }
1607 else
Moran Pekera998bc62018-04-16 18:16:20 +03001608 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001609#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001610 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001611 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001612 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001613 slot->data.key.data,
1614 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001615 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001616 if( status != PSA_SUCCESS )
1617 return status;
1618
1619 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1620 PSA_KEY_TYPE_ECC_GET_FAMILY(
1621 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001622 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001623 data,
1624 data_size,
1625 data_length );
1626
Steven Cooremana2371e52020-07-28 14:30:39 +02001627 mbedtls_ecp_keypair_free( ecp );
1628 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001629 return( status );
1630#else
1631 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001632 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001633#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001634 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001635 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001636 else
1637 {
1638 /* This shouldn't happen in the reference implementation, but
1639 it is valid for a special-purpose implementation to omit
1640 support for exporting certain key types. */
1641 return( PSA_ERROR_NOT_SUPPORTED );
1642 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001643}
1644
Gilles Peskinec5487a82018-12-03 18:08:14 +01001645psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001646 uint8_t *data,
1647 size_t data_size,
1648 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001649{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001650 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001651 psa_status_t status;
1652
1653 /* Set the key to empty now, so that even when there are errors, we always
1654 * set data_length to a value between 0 and data_size. On error, setting
1655 * the key to empty is a good choice because an empty key representation is
1656 * unlikely to be accepted anywhere. */
1657 *data_length = 0;
1658
1659 /* Export requires the EXPORT flag. There is an exception for public keys,
1660 * which don't require any flag, but psa_get_key_from_slot takes
1661 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001662 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001663 if( status != PSA_SUCCESS )
1664 return( status );
1665 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001666 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001667}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001668
Gilles Peskinec5487a82018-12-03 18:08:14 +01001669psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001670 uint8_t *data,
1671 size_t data_size,
1672 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001673{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001674 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001675 psa_status_t status;
1676
1677 /* Set the key to empty now, so that even when there are errors, we always
1678 * set data_length to a value between 0 and data_size. On error, setting
1679 * the key to empty is a good choice because an empty key representation is
1680 * unlikely to be accepted anywhere. */
1681 *data_length = 0;
1682
1683 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001684 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001685 if( status != PSA_SUCCESS )
1686 return( status );
1687 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001688 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001689}
1690
Gilles Peskine91e8c332019-08-02 19:19:39 +02001691#if defined(static_assert)
1692static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1693 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001694static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001695 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001696static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001697 "One or more key attribute flag is listed as both internal-only and external-only" );
1698#endif
1699
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001700/** Validate that a key policy is internally well-formed.
1701 *
1702 * This function only rejects invalid policies. It does not validate the
1703 * consistency of the policy with respect to other attributes of the key
1704 * such as the key type.
1705 */
1706static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001707{
1708 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001709 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001710 PSA_KEY_USAGE_ENCRYPT |
1711 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001712 PSA_KEY_USAGE_SIGN_HASH |
1713 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001714 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1715 return( PSA_ERROR_INVALID_ARGUMENT );
1716
Gilles Peskine4747d192019-04-17 15:05:45 +02001717 return( PSA_SUCCESS );
1718}
1719
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001720/** Validate the internal consistency of key attributes.
1721 *
1722 * This function only rejects invalid attribute values. If does not
1723 * validate the consistency of the attributes with any key data that may
1724 * be involved in the creation of the key.
1725 *
1726 * Call this function early in the key creation process.
1727 *
1728 * \param[in] attributes Key attributes for the new key.
1729 * \param[out] p_drv On any return, the driver for the key, if any.
1730 * NULL for a transparent key.
1731 *
1732 */
1733static psa_status_t psa_validate_key_attributes(
1734 const psa_key_attributes_t *attributes,
1735 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001736{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001737 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001738
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001739 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1740 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001741 if( status != PSA_SUCCESS )
1742 return( status );
1743
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001744 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1745 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001746 if( status != PSA_SUCCESS )
1747 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001748
1749 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001750 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001751 return( status );
1752
1753 /* Refuse to create overly large keys.
1754 * Note that this doesn't trigger on import if the attributes don't
1755 * explicitly specify a size (so psa_get_key_bits returns 0), so
1756 * psa_import_key() needs its own checks. */
1757 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1758 return( PSA_ERROR_NOT_SUPPORTED );
1759
Gilles Peskine91e8c332019-08-02 19:19:39 +02001760 /* Reject invalid flags. These should not be reachable through the API. */
1761 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1762 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1763 return( PSA_ERROR_INVALID_ARGUMENT );
1764
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001765 return( PSA_SUCCESS );
1766}
1767
Gilles Peskine4747d192019-04-17 15:05:45 +02001768/** Prepare a key slot to receive key material.
1769 *
1770 * This function allocates a key slot and sets its metadata.
1771 *
1772 * If this function fails, call psa_fail_key_creation().
1773 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001774 * This function is intended to be used as follows:
1775 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1776 * it with the specified attributes, and assign it a handle.
1777 * -# Populate the slot with the key material.
1778 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1779 * In case of failure at any step, stop the sequence and call
1780 * psa_fail_key_creation().
1781 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001782 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001783 * \param[in] attributes Key attributes for the new key.
1784 * \param[out] handle On success, a handle for the allocated slot.
1785 * \param[out] p_slot On success, a pointer to the prepared slot.
1786 * \param[out] p_drv On any return, the driver for the key, if any.
1787 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001788 *
1789 * \retval #PSA_SUCCESS
1790 * The key slot is ready to receive key material.
1791 * \return If this function fails, the key slot is an invalid state.
1792 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001793 */
1794static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001795 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001796 const psa_key_attributes_t *attributes,
1797 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001798 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001799 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001800{
1801 psa_status_t status;
1802 psa_key_slot_t *slot;
1803
Gilles Peskinedf179142019-07-15 22:02:14 +02001804 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001805 *p_drv = NULL;
1806
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001807 status = psa_validate_key_attributes( attributes, p_drv );
1808 if( status != PSA_SUCCESS )
1809 return( status );
1810
Gilles Peskineedbed562019-08-07 18:19:59 +02001811 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001812 if( status != PSA_SUCCESS )
1813 return( status );
1814 slot = *p_slot;
1815
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001816 /* We're storing the declared bit-size of the key. It's up to each
1817 * creation mechanism to verify that this information is correct.
1818 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001819 * an input (generate, device) but not for those where the bit-size
1820 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001821
1822 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001823
Gilles Peskine91e8c332019-08-02 19:19:39 +02001824 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001825 * external-only flags, query `attributes`. Thanks to the check
1826 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001827 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001828 * may have set. */
1829 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001830
Gilles Peskinecbaff462019-07-12 23:46:04 +02001831#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001832 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001833 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001834 * create the key file in internal storage, create the
1835 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001836 * persistent data. This is done by starting a transaction that will
1837 * encompass these three actions.
1838 * For registering a volatile key, we just need to find an appropriate
1839 * slot number inside the SE. Since the key is designated volatile, creating
1840 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001841 /* The first thing to do is to find a slot number for the new key.
1842 * We save the slot number in persistent storage as part of the
1843 * transaction data. It will be needed to recover if the power
1844 * fails during the key creation process, to clean up on the secure
1845 * element side after restarting. Obtaining a slot number from the
1846 * secure element driver updates its persistent state, but we do not yet
1847 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001848 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001849 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001850 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001851 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001852 &slot->data.se.slot_number );
1853 if( status != PSA_SUCCESS )
1854 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001855
1856 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001857 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001858 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1859 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1860 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1861 psa_crypto_transaction.key.id = slot->attr.id;
1862 status = psa_crypto_save_transaction( );
1863 if( status != PSA_SUCCESS )
1864 {
1865 (void) psa_crypto_stop_transaction( );
1866 return( status );
1867 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001868 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001869 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001870
1871 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1872 {
1873 /* Key registration only makes sense with a secure element. */
1874 return( PSA_ERROR_INVALID_ARGUMENT );
1875 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001876#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1877
Darryl Green0c6575a2018-11-07 16:05:30 +00001878 return( status );
1879}
Gilles Peskine4747d192019-04-17 15:05:45 +02001880
1881/** Finalize the creation of a key once its key material has been set.
1882 *
1883 * This entails writing the key to persistent storage.
1884 *
1885 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001886 * See the documentation of psa_start_key_creation() for the intended use
1887 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001888 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001889 * \param[in,out] slot Pointer to the slot with key material.
1890 * \param[in] driver The secure element driver for the key,
1891 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001892 *
1893 * \retval #PSA_SUCCESS
1894 * The key was successfully created. The handle is now valid.
1895 * \return If this function fails, the key slot is an invalid state.
1896 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001897 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001898static psa_status_t psa_finish_key_creation(
1899 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001900 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001901{
1902 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001903 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001904 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001905
1906#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001907 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001908 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001909#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1910 if( driver != NULL )
1911 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001912 psa_se_key_data_storage_t data;
1913#if defined(static_assert)
1914 static_assert( sizeof( slot->data.se.slot_number ) ==
1915 sizeof( data.slot_number ),
1916 "Slot number size does not match psa_se_key_data_storage_t" );
1917 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1918 "Bit-size size does not match psa_se_key_data_storage_t" );
1919#endif
1920 memcpy( &data.slot_number, &slot->data.se.slot_number,
1921 sizeof( slot->data.se.slot_number ) );
1922 memcpy( &data.bits, &slot->attr.bits,
1923 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001924 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001925 (uint8_t*) &data,
1926 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001927 }
1928 else
1929#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1930 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001931 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001932 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001933 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001934 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1935 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001936 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001937 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1938 status = psa_internal_export_key( slot,
1939 buffer, buffer_size, &length,
1940 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001941 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001942 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001943 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001944
Gilles Peskinef9168942019-09-12 19:20:29 +02001945 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001946 mbedtls_free( buffer );
1947 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001948 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001949#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1950
Gilles Peskinecbaff462019-07-12 23:46:04 +02001951#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001952 /* Finish the transaction for a key creation. This does not
1953 * happen when registering an existing key. Detect this case
1954 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001955 * creation of a persistent key in a secure element requires a transaction,
1956 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001957 if( driver != NULL &&
1958 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001959 {
1960 status = psa_save_se_persistent_data( driver );
1961 if( status != PSA_SUCCESS )
1962 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001963 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001964 return( status );
1965 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001966 status = psa_crypto_stop_transaction( );
1967 if( status != PSA_SUCCESS )
1968 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001969 }
1970#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1971
Gilles Peskine4747d192019-04-17 15:05:45 +02001972 return( status );
1973}
1974
1975/** Abort the creation of a key.
1976 *
1977 * You may call this function after calling psa_start_key_creation(),
1978 * or after psa_finish_key_creation() fails. In other circumstances, this
1979 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001980 * See the documentation of psa_start_key_creation() for the intended use
1981 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001982 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001983 * \param[in,out] slot Pointer to the slot with key material.
1984 * \param[in] driver The secure element driver for the key,
1985 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001986 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001987static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001988 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001989{
Gilles Peskine011e4282019-06-26 18:34:38 +02001990 (void) driver;
1991
Gilles Peskine4747d192019-04-17 15:05:45 +02001992 if( slot == NULL )
1993 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001994
1995#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001996 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001997 * element, and the failure happened later (when saving metadata
1998 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001999 * element.
2000 * https://github.com/ARMmbed/mbed-crypto/issues/217
2001 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002002
Gilles Peskined7729582019-08-05 15:55:54 +02002003 /* Abort the ongoing transaction if any (there may not be one if
2004 * the creation process failed before starting one, or if the
2005 * key creation is a registration of a key in a secure element).
2006 * Earlier functions must already have done what it takes to undo any
2007 * partial creation. All that's left is to update the transaction data
2008 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002009 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002010#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2011
Gilles Peskine4747d192019-04-17 15:05:45 +02002012 psa_wipe_key_slot( slot );
2013}
2014
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002015/** Validate optional attributes during key creation.
2016 *
2017 * Some key attributes are optional during key creation. If they are
2018 * specified in the attributes structure, check that they are consistent
2019 * with the data in the slot.
2020 *
2021 * This function should be called near the end of key creation, after
2022 * the slot in memory is fully populated but before saving persistent data.
2023 */
2024static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002025 const psa_key_slot_t *slot,
2026 const psa_key_attributes_t *attributes )
2027{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002028 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002029 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002030 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002031 return( PSA_ERROR_INVALID_ARGUMENT );
2032 }
2033
2034 if( attributes->domain_parameters_size != 0 )
2035 {
2036#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002037 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002038 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002039 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002040 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002042
Steven Cooreman7f391872020-07-30 14:57:44 +02002043 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002044 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002045 slot->data.key.data,
2046 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002047 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002048 if( status != PSA_SUCCESS )
2049 return status;
Steven Cooreman75b74362020-07-28 14:30:13 +02002050
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002051 mbedtls_mpi_init( &actual );
2052 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002053 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002054 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002055 mbedtls_rsa_free( rsa );
2056 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002057 if( ret != 0 )
2058 goto rsa_exit;
2059 ret = mbedtls_mpi_read_binary( &required,
2060 attributes->domain_parameters,
2061 attributes->domain_parameters_size );
2062 if( ret != 0 )
2063 goto rsa_exit;
2064 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2065 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2066 rsa_exit:
2067 mbedtls_mpi_free( &actual );
2068 mbedtls_mpi_free( &required );
2069 if( ret != 0)
2070 return( mbedtls_to_psa_error( ret ) );
2071 }
2072 else
2073#endif
2074 {
2075 return( PSA_ERROR_INVALID_ARGUMENT );
2076 }
2077 }
2078
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002079 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002080 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002081 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002082 return( PSA_ERROR_INVALID_ARGUMENT );
2083 }
2084
2085 return( PSA_SUCCESS );
2086}
2087
Gilles Peskine4747d192019-04-17 15:05:45 +02002088psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002089 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002090 size_t data_length,
2091 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002092{
2093 psa_status_t status;
2094 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002095 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002096
Gilles Peskine0f84d622019-09-12 19:03:13 +02002097 /* Reject zero-length symmetric keys (including raw data key objects).
2098 * This also rejects any key which might be encoded as an empty string,
2099 * which is never valid. */
2100 if( data_length == 0 )
2101 return( PSA_ERROR_INVALID_ARGUMENT );
2102
Gilles Peskinedf179142019-07-15 22:02:14 +02002103 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2104 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002105 if( status != PSA_SUCCESS )
2106 goto exit;
2107
Gilles Peskine5d309672019-07-12 23:47:28 +02002108#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2109 if( driver != NULL )
2110 {
2111 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002112 /* The driver should set the number of key bits, however in
2113 * case it doesn't, we initialize bits to an invalid value. */
2114 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002115 if( drv->key_management == NULL ||
2116 drv->key_management->p_import == NULL )
2117 {
2118 status = PSA_ERROR_NOT_SUPPORTED;
2119 goto exit;
2120 }
2121 status = drv->key_management->p_import(
2122 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002123 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002124 &bits );
2125 if( status != PSA_SUCCESS )
2126 goto exit;
2127 if( bits > PSA_MAX_KEY_BITS )
2128 {
2129 status = PSA_ERROR_NOT_SUPPORTED;
2130 goto exit;
2131 }
2132 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002133 }
2134 else
2135#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2136 {
2137 status = psa_import_key_into_slot( slot, data, data_length );
2138 if( status != PSA_SUCCESS )
2139 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002140 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002141 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002142 if( status != PSA_SUCCESS )
2143 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002144
Gilles Peskine011e4282019-06-26 18:34:38 +02002145 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002146exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002147 if( status != PSA_SUCCESS )
2148 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002149 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002150 *handle = 0;
2151 }
2152 return( status );
2153}
2154
Gilles Peskined7729582019-08-05 15:55:54 +02002155#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2156psa_status_t mbedtls_psa_register_se_key(
2157 const psa_key_attributes_t *attributes )
2158{
2159 psa_status_t status;
2160 psa_key_slot_t *slot = NULL;
2161 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002162 psa_key_handle_t handle = 0;
2163
2164 /* Leaving attributes unspecified is not currently supported.
2165 * It could make sense to query the key type and size from the
2166 * secure element, but not all secure elements support this
2167 * and the driver HAL doesn't currently support it. */
2168 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2169 return( PSA_ERROR_NOT_SUPPORTED );
2170 if( psa_get_key_bits( attributes ) == 0 )
2171 return( PSA_ERROR_NOT_SUPPORTED );
2172
2173 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2174 &handle, &slot, &driver );
2175 if( status != PSA_SUCCESS )
2176 goto exit;
2177
Gilles Peskined7729582019-08-05 15:55:54 +02002178 status = psa_finish_key_creation( slot, driver );
2179
2180exit:
2181 if( status != PSA_SUCCESS )
2182 {
2183 psa_fail_key_creation( slot, driver );
2184 }
2185 /* Registration doesn't keep the key in RAM. */
2186 psa_close_key( handle );
2187 return( status );
2188}
2189#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2190
Gilles Peskinef603c712019-01-19 13:40:11 +01002191static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002192 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002193{
2194 psa_status_t status;
2195 uint8_t *buffer = NULL;
2196 size_t buffer_size = 0;
2197 size_t length;
2198
Gilles Peskine8e338702019-07-30 20:06:31 +02002199 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002200 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002201 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002202 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002203 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002204 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2205 if( status != PSA_SUCCESS )
2206 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002207 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002208 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002209
2210exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002211 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002212 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002213 return( status );
2214}
2215
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002216psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2217 const psa_key_attributes_t *specified_attributes,
2218 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002219{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002220 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002221 psa_key_slot_t *source_slot = NULL;
2222 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002223 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002224 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002225
Gilles Peskine28f8f302019-07-24 13:30:31 +02002226 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002227 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002228 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002229 goto exit;
2230
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002231 status = psa_validate_optional_attributes( source_slot,
2232 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002233 if( status != PSA_SUCCESS )
2234 goto exit;
2235
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002236 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002237 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002238 if( status != PSA_SUCCESS )
2239 goto exit;
2240
Gilles Peskinedf179142019-07-15 22:02:14 +02002241 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2242 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002243 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002244 if( status != PSA_SUCCESS )
2245 goto exit;
2246
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002247#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2248 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002249 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002250 /* Copying to a secure element is not implemented yet. */
2251 status = PSA_ERROR_NOT_SUPPORTED;
2252 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002253 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002254#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002255
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002256 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002257 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002258 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002259
Gilles Peskine011e4282019-06-26 18:34:38 +02002260 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002261exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002262 if( status != PSA_SUCCESS )
2263 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002264 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002265 *target_handle = 0;
2266 }
2267 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002268}
2269
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002270
2271
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002272/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002273/* Message digests */
2274/****************************************************************/
2275
Gilles Peskineb16841e2019-10-10 20:36:12 +02002276#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002277static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002278{
2279 switch( alg )
2280 {
2281#if defined(MBEDTLS_MD2_C)
2282 case PSA_ALG_MD2:
2283 return( &mbedtls_md2_info );
2284#endif
2285#if defined(MBEDTLS_MD4_C)
2286 case PSA_ALG_MD4:
2287 return( &mbedtls_md4_info );
2288#endif
2289#if defined(MBEDTLS_MD5_C)
2290 case PSA_ALG_MD5:
2291 return( &mbedtls_md5_info );
2292#endif
2293#if defined(MBEDTLS_RIPEMD160_C)
2294 case PSA_ALG_RIPEMD160:
2295 return( &mbedtls_ripemd160_info );
2296#endif
2297#if defined(MBEDTLS_SHA1_C)
2298 case PSA_ALG_SHA_1:
2299 return( &mbedtls_sha1_info );
2300#endif
2301#if defined(MBEDTLS_SHA256_C)
2302 case PSA_ALG_SHA_224:
2303 return( &mbedtls_sha224_info );
2304 case PSA_ALG_SHA_256:
2305 return( &mbedtls_sha256_info );
2306#endif
2307#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002308#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002309 case PSA_ALG_SHA_384:
2310 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002311#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002312 case PSA_ALG_SHA_512:
2313 return( &mbedtls_sha512_info );
2314#endif
2315 default:
2316 return( NULL );
2317 }
2318}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002319#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002320
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2322{
2323 switch( operation->alg )
2324 {
Gilles Peskine81736312018-06-26 15:04:31 +02002325 case 0:
2326 /* The object has (apparently) been initialized but it is not
2327 * in use. It's ok to call abort on such an object, and there's
2328 * nothing to do. */
2329 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330#if defined(MBEDTLS_MD2_C)
2331 case PSA_ALG_MD2:
2332 mbedtls_md2_free( &operation->ctx.md2 );
2333 break;
2334#endif
2335#if defined(MBEDTLS_MD4_C)
2336 case PSA_ALG_MD4:
2337 mbedtls_md4_free( &operation->ctx.md4 );
2338 break;
2339#endif
2340#if defined(MBEDTLS_MD5_C)
2341 case PSA_ALG_MD5:
2342 mbedtls_md5_free( &operation->ctx.md5 );
2343 break;
2344#endif
2345#if defined(MBEDTLS_RIPEMD160_C)
2346 case PSA_ALG_RIPEMD160:
2347 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2348 break;
2349#endif
2350#if defined(MBEDTLS_SHA1_C)
2351 case PSA_ALG_SHA_1:
2352 mbedtls_sha1_free( &operation->ctx.sha1 );
2353 break;
2354#endif
2355#if defined(MBEDTLS_SHA256_C)
2356 case PSA_ALG_SHA_224:
2357 case PSA_ALG_SHA_256:
2358 mbedtls_sha256_free( &operation->ctx.sha256 );
2359 break;
2360#endif
2361#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002362#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002363 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002364#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365 case PSA_ALG_SHA_512:
2366 mbedtls_sha512_free( &operation->ctx.sha512 );
2367 break;
2368#endif
2369 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002370 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002371 }
2372 operation->alg = 0;
2373 return( PSA_SUCCESS );
2374}
2375
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002376psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002377 psa_algorithm_t alg )
2378{
Janos Follath24eed8d2019-11-22 13:21:35 +00002379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002380
2381 /* A context must be freshly initialized before it can be set up. */
2382 if( operation->alg != 0 )
2383 {
2384 return( PSA_ERROR_BAD_STATE );
2385 }
2386
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002387 switch( alg )
2388 {
2389#if defined(MBEDTLS_MD2_C)
2390 case PSA_ALG_MD2:
2391 mbedtls_md2_init( &operation->ctx.md2 );
2392 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2393 break;
2394#endif
2395#if defined(MBEDTLS_MD4_C)
2396 case PSA_ALG_MD4:
2397 mbedtls_md4_init( &operation->ctx.md4 );
2398 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2399 break;
2400#endif
2401#if defined(MBEDTLS_MD5_C)
2402 case PSA_ALG_MD5:
2403 mbedtls_md5_init( &operation->ctx.md5 );
2404 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2405 break;
2406#endif
2407#if defined(MBEDTLS_RIPEMD160_C)
2408 case PSA_ALG_RIPEMD160:
2409 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2410 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2411 break;
2412#endif
2413#if defined(MBEDTLS_SHA1_C)
2414 case PSA_ALG_SHA_1:
2415 mbedtls_sha1_init( &operation->ctx.sha1 );
2416 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2417 break;
2418#endif
2419#if defined(MBEDTLS_SHA256_C)
2420 case PSA_ALG_SHA_224:
2421 mbedtls_sha256_init( &operation->ctx.sha256 );
2422 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2423 break;
2424 case PSA_ALG_SHA_256:
2425 mbedtls_sha256_init( &operation->ctx.sha256 );
2426 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2427 break;
2428#endif
2429#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002430#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002431 case PSA_ALG_SHA_384:
2432 mbedtls_sha512_init( &operation->ctx.sha512 );
2433 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2434 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002435#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002436 case PSA_ALG_SHA_512:
2437 mbedtls_sha512_init( &operation->ctx.sha512 );
2438 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2439 break;
2440#endif
2441 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002442 return( PSA_ALG_IS_HASH( alg ) ?
2443 PSA_ERROR_NOT_SUPPORTED :
2444 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002445 }
2446 if( ret == 0 )
2447 operation->alg = alg;
2448 else
2449 psa_hash_abort( operation );
2450 return( mbedtls_to_psa_error( ret ) );
2451}
2452
2453psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2454 const uint8_t *input,
2455 size_t input_length )
2456{
Janos Follath24eed8d2019-11-22 13:21:35 +00002457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002458
2459 /* Don't require hash implementations to behave correctly on a
2460 * zero-length input, which may have an invalid pointer. */
2461 if( input_length == 0 )
2462 return( PSA_SUCCESS );
2463
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002464 switch( operation->alg )
2465 {
2466#if defined(MBEDTLS_MD2_C)
2467 case PSA_ALG_MD2:
2468 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2469 input, input_length );
2470 break;
2471#endif
2472#if defined(MBEDTLS_MD4_C)
2473 case PSA_ALG_MD4:
2474 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2475 input, input_length );
2476 break;
2477#endif
2478#if defined(MBEDTLS_MD5_C)
2479 case PSA_ALG_MD5:
2480 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2481 input, input_length );
2482 break;
2483#endif
2484#if defined(MBEDTLS_RIPEMD160_C)
2485 case PSA_ALG_RIPEMD160:
2486 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2487 input, input_length );
2488 break;
2489#endif
2490#if defined(MBEDTLS_SHA1_C)
2491 case PSA_ALG_SHA_1:
2492 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2493 input, input_length );
2494 break;
2495#endif
2496#if defined(MBEDTLS_SHA256_C)
2497 case PSA_ALG_SHA_224:
2498 case PSA_ALG_SHA_256:
2499 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2500 input, input_length );
2501 break;
2502#endif
2503#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002504#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002505 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002506#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002507 case PSA_ALG_SHA_512:
2508 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2509 input, input_length );
2510 break;
2511#endif
2512 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002513 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002514 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002515
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002516 if( ret != 0 )
2517 psa_hash_abort( operation );
2518 return( mbedtls_to_psa_error( ret ) );
2519}
2520
2521psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2522 uint8_t *hash,
2523 size_t hash_size,
2524 size_t *hash_length )
2525{
itayzafrir40835d42018-08-02 13:14:17 +03002526 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002528 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002529
2530 /* Fill the output buffer with something that isn't a valid hash
2531 * (barring an attack on the hash and deliberately-crafted input),
2532 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002533 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002534 /* If hash_size is 0 then hash may be NULL and then the
2535 * call to memset would have undefined behavior. */
2536 if( hash_size != 0 )
2537 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002538
2539 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002540 {
2541 status = PSA_ERROR_BUFFER_TOO_SMALL;
2542 goto exit;
2543 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002544
2545 switch( operation->alg )
2546 {
2547#if defined(MBEDTLS_MD2_C)
2548 case PSA_ALG_MD2:
2549 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2550 break;
2551#endif
2552#if defined(MBEDTLS_MD4_C)
2553 case PSA_ALG_MD4:
2554 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2555 break;
2556#endif
2557#if defined(MBEDTLS_MD5_C)
2558 case PSA_ALG_MD5:
2559 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2560 break;
2561#endif
2562#if defined(MBEDTLS_RIPEMD160_C)
2563 case PSA_ALG_RIPEMD160:
2564 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2565 break;
2566#endif
2567#if defined(MBEDTLS_SHA1_C)
2568 case PSA_ALG_SHA_1:
2569 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2570 break;
2571#endif
2572#if defined(MBEDTLS_SHA256_C)
2573 case PSA_ALG_SHA_224:
2574 case PSA_ALG_SHA_256:
2575 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2576 break;
2577#endif
2578#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002579#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002580 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002581#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002582 case PSA_ALG_SHA_512:
2583 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2584 break;
2585#endif
2586 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002587 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002588 }
itayzafrir40835d42018-08-02 13:14:17 +03002589 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002590
itayzafrir40835d42018-08-02 13:14:17 +03002591exit:
2592 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002593 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002594 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002595 return( psa_hash_abort( operation ) );
2596 }
2597 else
2598 {
2599 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002600 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002601 }
2602}
2603
Gilles Peskine2d277862018-06-18 15:41:12 +02002604psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2605 const uint8_t *hash,
2606 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002607{
2608 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2609 size_t actual_hash_length;
2610 psa_status_t status = psa_hash_finish( operation,
2611 actual_hash, sizeof( actual_hash ),
2612 &actual_hash_length );
2613 if( status != PSA_SUCCESS )
2614 return( status );
2615 if( actual_hash_length != hash_length )
2616 return( PSA_ERROR_INVALID_SIGNATURE );
2617 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2618 return( PSA_ERROR_INVALID_SIGNATURE );
2619 return( PSA_SUCCESS );
2620}
2621
Gilles Peskine0a749c82019-11-28 19:33:58 +01002622psa_status_t psa_hash_compute( psa_algorithm_t alg,
2623 const uint8_t *input, size_t input_length,
2624 uint8_t *hash, size_t hash_size,
2625 size_t *hash_length )
2626{
2627 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2628 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2629
2630 *hash_length = hash_size;
2631 status = psa_hash_setup( &operation, alg );
2632 if( status != PSA_SUCCESS )
2633 goto exit;
2634 status = psa_hash_update( &operation, input, input_length );
2635 if( status != PSA_SUCCESS )
2636 goto exit;
2637 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2638 if( status != PSA_SUCCESS )
2639 goto exit;
2640
2641exit:
2642 if( status == PSA_SUCCESS )
2643 status = psa_hash_abort( &operation );
2644 else
2645 psa_hash_abort( &operation );
2646 return( status );
2647}
2648
2649psa_status_t psa_hash_compare( psa_algorithm_t alg,
2650 const uint8_t *input, size_t input_length,
2651 const uint8_t *hash, size_t hash_length )
2652{
2653 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2654 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2655
2656 status = psa_hash_setup( &operation, alg );
2657 if( status != PSA_SUCCESS )
2658 goto exit;
2659 status = psa_hash_update( &operation, input, input_length );
2660 if( status != PSA_SUCCESS )
2661 goto exit;
2662 status = psa_hash_verify( &operation, hash, hash_length );
2663 if( status != PSA_SUCCESS )
2664 goto exit;
2665
2666exit:
2667 if( status == PSA_SUCCESS )
2668 status = psa_hash_abort( &operation );
2669 else
2670 psa_hash_abort( &operation );
2671 return( status );
2672}
2673
Gilles Peskineeb35d782019-01-22 17:56:16 +01002674psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2675 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002676{
2677 if( target_operation->alg != 0 )
2678 return( PSA_ERROR_BAD_STATE );
2679
2680 switch( source_operation->alg )
2681 {
2682 case 0:
2683 return( PSA_ERROR_BAD_STATE );
2684#if defined(MBEDTLS_MD2_C)
2685 case PSA_ALG_MD2:
2686 mbedtls_md2_clone( &target_operation->ctx.md2,
2687 &source_operation->ctx.md2 );
2688 break;
2689#endif
2690#if defined(MBEDTLS_MD4_C)
2691 case PSA_ALG_MD4:
2692 mbedtls_md4_clone( &target_operation->ctx.md4,
2693 &source_operation->ctx.md4 );
2694 break;
2695#endif
2696#if defined(MBEDTLS_MD5_C)
2697 case PSA_ALG_MD5:
2698 mbedtls_md5_clone( &target_operation->ctx.md5,
2699 &source_operation->ctx.md5 );
2700 break;
2701#endif
2702#if defined(MBEDTLS_RIPEMD160_C)
2703 case PSA_ALG_RIPEMD160:
2704 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2705 &source_operation->ctx.ripemd160 );
2706 break;
2707#endif
2708#if defined(MBEDTLS_SHA1_C)
2709 case PSA_ALG_SHA_1:
2710 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2711 &source_operation->ctx.sha1 );
2712 break;
2713#endif
2714#if defined(MBEDTLS_SHA256_C)
2715 case PSA_ALG_SHA_224:
2716 case PSA_ALG_SHA_256:
2717 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2718 &source_operation->ctx.sha256 );
2719 break;
2720#endif
2721#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002722#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002723 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002724#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002725 case PSA_ALG_SHA_512:
2726 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2727 &source_operation->ctx.sha512 );
2728 break;
2729#endif
2730 default:
2731 return( PSA_ERROR_NOT_SUPPORTED );
2732 }
2733
2734 target_operation->alg = source_operation->alg;
2735 return( PSA_SUCCESS );
2736}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002737
2738
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002739/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740/* MAC */
2741/****************************************************************/
2742
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002743static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002744 psa_algorithm_t alg,
2745 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002746 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002747 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002749 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002750 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002752 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002753 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002754
Gilles Peskine8c9def32018-02-08 10:02:12 +01002755 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2756 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002757 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002758 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002759 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002760 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002761 mode = MBEDTLS_MODE_STREAM;
2762 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002763 case PSA_ALG_CTR:
2764 mode = MBEDTLS_MODE_CTR;
2765 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002766 case PSA_ALG_CFB:
2767 mode = MBEDTLS_MODE_CFB;
2768 break;
2769 case PSA_ALG_OFB:
2770 mode = MBEDTLS_MODE_OFB;
2771 break;
2772 case PSA_ALG_CBC_NO_PADDING:
2773 mode = MBEDTLS_MODE_CBC;
2774 break;
2775 case PSA_ALG_CBC_PKCS7:
2776 mode = MBEDTLS_MODE_CBC;
2777 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002778 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002779 mode = MBEDTLS_MODE_CCM;
2780 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002781 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002782 mode = MBEDTLS_MODE_GCM;
2783 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002784 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2785 mode = MBEDTLS_MODE_CHACHAPOLY;
2786 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002787 default:
2788 return( NULL );
2789 }
2790 }
2791 else if( alg == PSA_ALG_CMAC )
2792 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002793 else
2794 return( NULL );
2795
2796 switch( key_type )
2797 {
2798 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002799 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002800 break;
2801 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002802 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2803 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002804 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002805 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002806 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002807 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002808 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2809 * but two-key Triple-DES is functionally three-key Triple-DES
2810 * with K1=K3, so that's how we present it to mbedtls. */
2811 if( key_bits == 128 )
2812 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002813 break;
2814 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002815 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002816 break;
2817 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002818 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002819 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002820 case PSA_KEY_TYPE_CHACHA20:
2821 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2822 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002823 default:
2824 return( NULL );
2825 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002826 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002827 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002828
Jaeden Amero23bbb752018-06-26 14:16:54 +01002829 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2830 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002831}
2832
Gilles Peskinea05219c2018-11-16 16:02:56 +01002833#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002834static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002835{
Gilles Peskine2d277862018-06-18 15:41:12 +02002836 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002837 {
2838 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002839 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002840 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002841 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002842 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002843 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002844 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002845 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002846 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002847 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002848 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002849 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002850 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002851 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002852 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002853 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002854 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002855 return( 128 );
2856 default:
2857 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002858 }
2859}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002860#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002861
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002862/* Initialize the MAC operation structure. Once this function has been
2863 * called, psa_mac_abort can run and will do the right thing. */
2864static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2865 psa_algorithm_t alg )
2866{
2867 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2868
2869 operation->alg = alg;
2870 operation->key_set = 0;
2871 operation->iv_set = 0;
2872 operation->iv_required = 0;
2873 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002874 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002875
2876#if defined(MBEDTLS_CMAC_C)
2877 if( alg == PSA_ALG_CMAC )
2878 {
2879 operation->iv_required = 0;
2880 mbedtls_cipher_init( &operation->ctx.cmac );
2881 status = PSA_SUCCESS;
2882 }
2883 else
2884#endif /* MBEDTLS_CMAC_C */
2885#if defined(MBEDTLS_MD_C)
2886 if( PSA_ALG_IS_HMAC( operation->alg ) )
2887 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002888 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2889 operation->ctx.hmac.hash_ctx.alg = 0;
2890 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002891 }
2892 else
2893#endif /* MBEDTLS_MD_C */
2894 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002895 if( ! PSA_ALG_IS_MAC( alg ) )
2896 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002897 }
2898
2899 if( status != PSA_SUCCESS )
2900 memset( operation, 0, sizeof( *operation ) );
2901 return( status );
2902}
2903
Gilles Peskine01126fa2018-07-12 17:04:55 +02002904#if defined(MBEDTLS_MD_C)
2905static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2906{
Gilles Peskine3f108122018-12-07 18:14:53 +01002907 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002908 return( psa_hash_abort( &hmac->hash_ctx ) );
2909}
2910#endif /* MBEDTLS_MD_C */
2911
Gilles Peskine8c9def32018-02-08 10:02:12 +01002912psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2913{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002914 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002915 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002916 /* The object has (apparently) been initialized but it is not
2917 * in use. It's ok to call abort on such an object, and there's
2918 * nothing to do. */
2919 return( PSA_SUCCESS );
2920 }
2921 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002922#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002923 if( operation->alg == PSA_ALG_CMAC )
2924 {
2925 mbedtls_cipher_free( &operation->ctx.cmac );
2926 }
2927 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002928#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002929#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002930 if( PSA_ALG_IS_HMAC( operation->alg ) )
2931 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002932 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002933 }
2934 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002935#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002936 {
2937 /* Sanity check (shouldn't happen: operation->alg should
2938 * always have been initialized to a valid value). */
2939 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002940 }
Moran Peker41deec42018-04-04 15:43:05 +03002941
Gilles Peskine8c9def32018-02-08 10:02:12 +01002942 operation->alg = 0;
2943 operation->key_set = 0;
2944 operation->iv_set = 0;
2945 operation->iv_required = 0;
2946 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002947 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002948
Gilles Peskine8c9def32018-02-08 10:02:12 +01002949 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002950
2951bad_state:
2952 /* If abort is called on an uninitialized object, we can't trust
2953 * anything. Wipe the object in case it contains confidential data.
2954 * This may result in a memory leak if a pointer gets overwritten,
2955 * but it's too late to do anything about this. */
2956 memset( operation, 0, sizeof( *operation ) );
2957 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002958}
2959
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002960#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002961static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002962 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002963 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002964 const mbedtls_cipher_info_t *cipher_info )
2965{
Janos Follath24eed8d2019-11-22 13:21:35 +00002966 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002967
2968 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002969
2970 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2971 if( ret != 0 )
2972 return( ret );
2973
2974 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002975 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002976 key_bits );
2977 return( ret );
2978}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002979#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002980
Gilles Peskine248051a2018-06-20 16:09:38 +02002981#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002982static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2983 const uint8_t *key,
2984 size_t key_length,
2985 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002986{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002987 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002988 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002989 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002990 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002991 psa_status_t status;
2992
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002993 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2994 * overflow below. This should never trigger if the hash algorithm
2995 * is implemented correctly. */
2996 /* The size checks against the ipad and opad buffers cannot be written
2997 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2998 * because that triggers -Wlogical-op on GCC 7.3. */
2999 if( block_size > sizeof( ipad ) )
3000 return( PSA_ERROR_NOT_SUPPORTED );
3001 if( block_size > sizeof( hmac->opad ) )
3002 return( PSA_ERROR_NOT_SUPPORTED );
3003 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003004 return( PSA_ERROR_NOT_SUPPORTED );
3005
Gilles Peskined223b522018-06-11 18:12:58 +02003006 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003007 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003008 status = psa_hash_compute( hash_alg, key, key_length,
3009 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003010 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003011 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003012 }
Gilles Peskine96889972018-07-12 17:07:03 +02003013 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3014 * but it is permitted. It is common when HMAC is used in HKDF, for
3015 * example. Don't call `memcpy` in the 0-length because `key` could be
3016 * an invalid pointer which would make the behavior undefined. */
3017 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003018 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003019
Gilles Peskined223b522018-06-11 18:12:58 +02003020 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3021 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003022 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003023 ipad[i] ^= 0x36;
3024 memset( ipad + key_length, 0x36, block_size - key_length );
3025
3026 /* Copy the key material from ipad to opad, flipping the requisite bits,
3027 * and filling the rest of opad with the requisite constant. */
3028 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003029 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3030 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003031
Gilles Peskine01126fa2018-07-12 17:04:55 +02003032 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003033 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003034 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003035
Gilles Peskine01126fa2018-07-12 17:04:55 +02003036 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003037
3038cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03003039 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003040
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003041 return( status );
3042}
Gilles Peskine248051a2018-06-20 16:09:38 +02003043#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003044
Gilles Peskine89167cb2018-07-08 20:12:23 +02003045static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003046 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003047 psa_algorithm_t alg,
3048 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003049{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003050 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003051 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003052 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003053 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003054 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003055 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003056 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003057
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003058 /* A context must be freshly initialized before it can be set up. */
3059 if( operation->alg != 0 )
3060 {
3061 return( PSA_ERROR_BAD_STATE );
3062 }
3063
Gilles Peskined911eb72018-08-14 15:18:45 +02003064 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003065 if( status != PSA_SUCCESS )
3066 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003067 if( is_sign )
3068 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003069
Gilles Peskine28f8f302019-07-24 13:30:31 +02003070 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003071 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003072 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003073 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003074
Gilles Peskine8c9def32018-02-08 10:02:12 +01003075#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003076 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003077 {
3078 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003079 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003080 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003082 if( cipher_info == NULL )
3083 {
3084 status = PSA_ERROR_NOT_SUPPORTED;
3085 goto exit;
3086 }
3087 operation->mac_size = cipher_info->block_size;
3088 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3089 status = mbedtls_to_psa_error( ret );
3090 }
3091 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003092#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003093#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003094 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003095 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003096 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003097 if( hash_alg == 0 )
3098 {
3099 status = PSA_ERROR_NOT_SUPPORTED;
3100 goto exit;
3101 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003102
3103 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3104 /* Sanity check. This shouldn't fail on a valid configuration. */
3105 if( operation->mac_size == 0 ||
3106 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3107 {
3108 status = PSA_ERROR_NOT_SUPPORTED;
3109 goto exit;
3110 }
3111
Gilles Peskine8e338702019-07-30 20:06:31 +02003112 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003113 {
3114 status = PSA_ERROR_INVALID_ARGUMENT;
3115 goto exit;
3116 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003117
Gilles Peskine01126fa2018-07-12 17:04:55 +02003118 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003119 slot->data.key.data,
3120 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003121 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003122 }
3123 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003124#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003125 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003126 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003127 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003128 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003129
Gilles Peskined911eb72018-08-14 15:18:45 +02003130 if( truncated == 0 )
3131 {
3132 /* The "normal" case: untruncated algorithm. Nothing to do. */
3133 }
3134 else if( truncated < 4 )
3135 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003136 /* A very short MAC is too short for security since it can be
3137 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3138 * so we make this our minimum, even though 32 bits is still
3139 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003140 status = PSA_ERROR_NOT_SUPPORTED;
3141 }
3142 else if( truncated > operation->mac_size )
3143 {
3144 /* It's impossible to "truncate" to a larger length. */
3145 status = PSA_ERROR_INVALID_ARGUMENT;
3146 }
3147 else
3148 operation->mac_size = truncated;
3149
Gilles Peskinefbfac682018-07-08 20:51:54 +02003150exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003151 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003152 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003153 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003154 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003155 else
3156 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003157 operation->key_set = 1;
3158 }
3159 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003160}
3161
Gilles Peskine89167cb2018-07-08 20:12:23 +02003162psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003163 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003164 psa_algorithm_t alg )
3165{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003166 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003167}
3168
3169psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003170 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003171 psa_algorithm_t alg )
3172{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003173 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003174}
3175
Gilles Peskine8c9def32018-02-08 10:02:12 +01003176psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3177 const uint8_t *input,
3178 size_t input_length )
3179{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003180 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003181 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003182 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003183 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003184 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003185 operation->has_input = 1;
3186
Gilles Peskine8c9def32018-02-08 10:02:12 +01003187#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003188 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003189 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003190 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3191 input, input_length );
3192 status = mbedtls_to_psa_error( ret );
3193 }
3194 else
3195#endif /* MBEDTLS_CMAC_C */
3196#if defined(MBEDTLS_MD_C)
3197 if( PSA_ALG_IS_HMAC( operation->alg ) )
3198 {
3199 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3200 input_length );
3201 }
3202 else
3203#endif /* MBEDTLS_MD_C */
3204 {
3205 /* This shouldn't happen if `operation` was initialized by
3206 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003207 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003208 }
3209
Gilles Peskinefbfac682018-07-08 20:51:54 +02003210 if( status != PSA_SUCCESS )
3211 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003212 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003213}
3214
Gilles Peskine01126fa2018-07-12 17:04:55 +02003215#if defined(MBEDTLS_MD_C)
3216static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3217 uint8_t *mac,
3218 size_t mac_size )
3219{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003220 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003221 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3222 size_t hash_size = 0;
3223 size_t block_size = psa_get_hash_block_size( hash_alg );
3224 psa_status_t status;
3225
Gilles Peskine01126fa2018-07-12 17:04:55 +02003226 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3227 if( status != PSA_SUCCESS )
3228 return( status );
3229 /* From here on, tmp needs to be wiped. */
3230
3231 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3232 if( status != PSA_SUCCESS )
3233 goto exit;
3234
3235 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3236 if( status != PSA_SUCCESS )
3237 goto exit;
3238
3239 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3240 if( status != PSA_SUCCESS )
3241 goto exit;
3242
Gilles Peskined911eb72018-08-14 15:18:45 +02003243 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3244 if( status != PSA_SUCCESS )
3245 goto exit;
3246
3247 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003248
3249exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003250 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003251 return( status );
3252}
3253#endif /* MBEDTLS_MD_C */
3254
mohammad16036df908f2018-04-02 08:34:15 -07003255static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003256 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003257 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003258{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003259 if( ! operation->key_set )
3260 return( PSA_ERROR_BAD_STATE );
3261 if( operation->iv_required && ! operation->iv_set )
3262 return( PSA_ERROR_BAD_STATE );
3263
Gilles Peskine8c9def32018-02-08 10:02:12 +01003264 if( mac_size < operation->mac_size )
3265 return( PSA_ERROR_BUFFER_TOO_SMALL );
3266
Gilles Peskine8c9def32018-02-08 10:02:12 +01003267#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003268 if( operation->alg == PSA_ALG_CMAC )
3269 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003270 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3271 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3272 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003273 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003274 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003275 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003276 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003277 else
3278#endif /* MBEDTLS_CMAC_C */
3279#if defined(MBEDTLS_MD_C)
3280 if( PSA_ALG_IS_HMAC( operation->alg ) )
3281 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003282 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003283 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003284 }
3285 else
3286#endif /* MBEDTLS_MD_C */
3287 {
3288 /* This shouldn't happen if `operation` was initialized by
3289 * a setup function. */
3290 return( PSA_ERROR_BAD_STATE );
3291 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003292}
3293
Gilles Peskineacd4be32018-07-08 19:56:25 +02003294psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3295 uint8_t *mac,
3296 size_t mac_size,
3297 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003298{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003299 psa_status_t status;
3300
Jaeden Amero252ef282019-02-15 14:05:35 +00003301 if( operation->alg == 0 )
3302 {
3303 return( PSA_ERROR_BAD_STATE );
3304 }
3305
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003306 /* Fill the output buffer with something that isn't a valid mac
3307 * (barring an attack on the mac and deliberately-crafted input),
3308 * in case the caller doesn't check the return status properly. */
3309 *mac_length = mac_size;
3310 /* If mac_size is 0 then mac may be NULL and then the
3311 * call to memset would have undefined behavior. */
3312 if( mac_size != 0 )
3313 memset( mac, '!', mac_size );
3314
Gilles Peskine89167cb2018-07-08 20:12:23 +02003315 if( ! operation->is_sign )
3316 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003317 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003318 }
mohammad16036df908f2018-04-02 08:34:15 -07003319
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003320 status = psa_mac_finish_internal( operation, mac, mac_size );
3321
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003322 if( status == PSA_SUCCESS )
3323 {
3324 status = psa_mac_abort( operation );
3325 if( status == PSA_SUCCESS )
3326 *mac_length = operation->mac_size;
3327 else
3328 memset( mac, '!', mac_size );
3329 }
3330 else
3331 psa_mac_abort( operation );
3332 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003333}
3334
Gilles Peskineacd4be32018-07-08 19:56:25 +02003335psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3336 const uint8_t *mac,
3337 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003338{
Gilles Peskine828ed142018-06-18 23:25:51 +02003339 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003340 psa_status_t status;
3341
Jaeden Amero252ef282019-02-15 14:05:35 +00003342 if( operation->alg == 0 )
3343 {
3344 return( PSA_ERROR_BAD_STATE );
3345 }
3346
Gilles Peskine89167cb2018-07-08 20:12:23 +02003347 if( operation->is_sign )
3348 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003349 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003350 }
3351 if( operation->mac_size != mac_length )
3352 {
3353 status = PSA_ERROR_INVALID_SIGNATURE;
3354 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003355 }
mohammad16036df908f2018-04-02 08:34:15 -07003356
3357 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003358 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003359 if( status != PSA_SUCCESS )
3360 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003361
3362 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3363 status = PSA_ERROR_INVALID_SIGNATURE;
3364
3365cleanup:
3366 if( status == PSA_SUCCESS )
3367 status = psa_mac_abort( operation );
3368 else
3369 psa_mac_abort( operation );
3370
Gilles Peskine3f108122018-12-07 18:14:53 +01003371 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003372
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003373 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003374}
3375
3376
Gilles Peskine20035e32018-02-03 22:44:14 +01003377
Gilles Peskine20035e32018-02-03 22:44:14 +01003378/****************************************************************/
3379/* Asymmetric cryptography */
3380/****************************************************************/
3381
Gilles Peskine2b450e32018-06-27 15:42:46 +02003382#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003383/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003384 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003385static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3386 size_t hash_length,
3387 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003388{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003389 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003390 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003391 *md_alg = mbedtls_md_get_type( md_info );
3392
3393 /* The Mbed TLS RSA module uses an unsigned int for hash length
3394 * parameters. Validate that it fits so that we don't risk an
3395 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003396#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003397 if( hash_length > UINT_MAX )
3398 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003399#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003400
3401#if defined(MBEDTLS_PKCS1_V15)
3402 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3403 * must be correct. */
3404 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3405 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003406 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003407 if( md_info == NULL )
3408 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003409 if( mbedtls_md_get_size( md_info ) != hash_length )
3410 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003411 }
3412#endif /* MBEDTLS_PKCS1_V15 */
3413
3414#if defined(MBEDTLS_PKCS1_V21)
3415 /* PSS requires a hash internally. */
3416 if( PSA_ALG_IS_RSA_PSS( alg ) )
3417 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003418 if( md_info == NULL )
3419 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003420 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003421#endif /* MBEDTLS_PKCS1_V21 */
3422
Gilles Peskine61b91d42018-06-08 16:09:36 +02003423 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003424}
3425
Gilles Peskine2b450e32018-06-27 15:42:46 +02003426static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3427 psa_algorithm_t alg,
3428 const uint8_t *hash,
3429 size_t hash_length,
3430 uint8_t *signature,
3431 size_t signature_size,
3432 size_t *signature_length )
3433{
3434 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003436 mbedtls_md_type_t md_alg;
3437
3438 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3439 if( status != PSA_SUCCESS )
3440 return( status );
3441
Gilles Peskine630a18a2018-06-29 17:49:35 +02003442 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003443 return( PSA_ERROR_BUFFER_TOO_SMALL );
3444
3445#if defined(MBEDTLS_PKCS1_V15)
3446 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3447 {
3448 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3449 MBEDTLS_MD_NONE );
3450 ret = mbedtls_rsa_pkcs1_sign( rsa,
3451 mbedtls_ctr_drbg_random,
3452 &global_data.ctr_drbg,
3453 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003454 md_alg,
3455 (unsigned int) hash_length,
3456 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003457 signature );
3458 }
3459 else
3460#endif /* MBEDTLS_PKCS1_V15 */
3461#if defined(MBEDTLS_PKCS1_V21)
3462 if( PSA_ALG_IS_RSA_PSS( alg ) )
3463 {
3464 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3465 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3466 mbedtls_ctr_drbg_random,
3467 &global_data.ctr_drbg,
3468 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003469 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003470 (unsigned int) hash_length,
3471 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003472 signature );
3473 }
3474 else
3475#endif /* MBEDTLS_PKCS1_V21 */
3476 {
3477 return( PSA_ERROR_INVALID_ARGUMENT );
3478 }
3479
3480 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003481 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003482 return( mbedtls_to_psa_error( ret ) );
3483}
3484
3485static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3486 psa_algorithm_t alg,
3487 const uint8_t *hash,
3488 size_t hash_length,
3489 const uint8_t *signature,
3490 size_t signature_length )
3491{
3492 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003494 mbedtls_md_type_t md_alg;
3495
3496 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3497 if( status != PSA_SUCCESS )
3498 return( status );
3499
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003500 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3501 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003502
3503#if defined(MBEDTLS_PKCS1_V15)
3504 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3505 {
3506 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3507 MBEDTLS_MD_NONE );
3508 ret = mbedtls_rsa_pkcs1_verify( rsa,
3509 mbedtls_ctr_drbg_random,
3510 &global_data.ctr_drbg,
3511 MBEDTLS_RSA_PUBLIC,
3512 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003513 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003514 hash,
3515 signature );
3516 }
3517 else
3518#endif /* MBEDTLS_PKCS1_V15 */
3519#if defined(MBEDTLS_PKCS1_V21)
3520 if( PSA_ALG_IS_RSA_PSS( alg ) )
3521 {
3522 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3523 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3524 mbedtls_ctr_drbg_random,
3525 &global_data.ctr_drbg,
3526 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003527 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003528 (unsigned int) hash_length,
3529 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003530 signature );
3531 }
3532 else
3533#endif /* MBEDTLS_PKCS1_V21 */
3534 {
3535 return( PSA_ERROR_INVALID_ARGUMENT );
3536 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003537
3538 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3539 * the rest of the signature is invalid". This has little use in
3540 * practice and PSA doesn't report this distinction. */
3541 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3542 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003543 return( mbedtls_to_psa_error( ret ) );
3544}
3545#endif /* MBEDTLS_RSA_C */
3546
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003547#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003548/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3549 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3550 * (even though these functions don't modify it). */
3551static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3552 psa_algorithm_t alg,
3553 const uint8_t *hash,
3554 size_t hash_length,
3555 uint8_t *signature,
3556 size_t signature_size,
3557 size_t *signature_length )
3558{
Janos Follath24eed8d2019-11-22 13:21:35 +00003559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003560 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003561 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003562 mbedtls_mpi_init( &r );
3563 mbedtls_mpi_init( &s );
3564
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003565 if( signature_size < 2 * curve_bytes )
3566 {
3567 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3568 goto cleanup;
3569 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003570
Gilles Peskinea05219c2018-11-16 16:02:56 +01003571#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003572 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3573 {
3574 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3575 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3576 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003577 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3578 &ecp->d, hash,
3579 hash_length, md_alg,
3580 mbedtls_ctr_drbg_random,
3581 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003582 }
3583 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003584#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003585 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003586 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003587 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3588 hash, hash_length,
3589 mbedtls_ctr_drbg_random,
3590 &global_data.ctr_drbg ) );
3591 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003592
3593 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3594 signature,
3595 curve_bytes ) );
3596 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3597 signature + curve_bytes,
3598 curve_bytes ) );
3599
3600cleanup:
3601 mbedtls_mpi_free( &r );
3602 mbedtls_mpi_free( &s );
3603 if( ret == 0 )
3604 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003605 return( mbedtls_to_psa_error( ret ) );
3606}
3607
3608static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3609 const uint8_t *hash,
3610 size_t hash_length,
3611 const uint8_t *signature,
3612 size_t signature_length )
3613{
Janos Follath24eed8d2019-11-22 13:21:35 +00003614 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003615 mbedtls_mpi r, s;
3616 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3617 mbedtls_mpi_init( &r );
3618 mbedtls_mpi_init( &s );
3619
3620 if( signature_length != 2 * curve_bytes )
3621 return( PSA_ERROR_INVALID_SIGNATURE );
3622
3623 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3624 signature,
3625 curve_bytes ) );
3626 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3627 signature + curve_bytes,
3628 curve_bytes ) );
3629
Steven Cooremanacda8342020-07-24 23:09:52 +02003630 /* Check whether the public part is loaded. If not, load it. */
3631 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3632 {
3633 MBEDTLS_MPI_CHK(
3634 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3635 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3636 }
3637
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003638 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3639 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003640
3641cleanup:
3642 mbedtls_mpi_free( &r );
3643 mbedtls_mpi_free( &s );
3644 return( mbedtls_to_psa_error( ret ) );
3645}
3646#endif /* MBEDTLS_ECDSA_C */
3647
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003648psa_status_t psa_sign_hash( psa_key_handle_t handle,
3649 psa_algorithm_t alg,
3650 const uint8_t *hash,
3651 size_t hash_length,
3652 uint8_t *signature,
3653 size_t signature_size,
3654 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003655{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003656 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003657 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003658#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3659 const psa_drv_se_t *drv;
3660 psa_drv_se_context_t *drv_context;
3661#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003662
3663 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003664 /* Immediately reject a zero-length signature buffer. This guarantees
3665 * that signature must be a valid pointer. (On the other hand, the hash
3666 * buffer can in principle be empty since it doesn't actually have
3667 * to be a hash.) */
3668 if( signature_size == 0 )
3669 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003670
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003671 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003672 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003673 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003674 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003675 {
3676 status = PSA_ERROR_INVALID_ARGUMENT;
3677 goto exit;
3678 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003679
Gilles Peskineedc64242019-08-07 21:05:07 +02003680#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3681 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3682 {
3683 if( drv->asymmetric == NULL ||
3684 drv->asymmetric->p_sign == NULL )
3685 {
3686 status = PSA_ERROR_NOT_SUPPORTED;
3687 goto exit;
3688 }
3689 status = drv->asymmetric->p_sign( drv_context,
3690 slot->data.se.slot_number,
3691 alg,
3692 hash, hash_length,
3693 signature, signature_size,
3694 signature_length );
3695 }
3696 else
3697#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003698#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003699 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003700 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003701 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003702
Steven Cooreman4fed4552020-08-03 14:46:03 +02003703 status = psa_load_rsa_representation( slot->attr.type,
3704 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003705 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003706 &rsa );
3707 if( status != PSA_SUCCESS )
3708 goto exit;
3709
Steven Cooremana2371e52020-07-28 14:30:39 +02003710 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003711 alg,
3712 hash, hash_length,
3713 signature, signature_size,
3714 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003715
Steven Cooremana2371e52020-07-28 14:30:39 +02003716 mbedtls_rsa_free( rsa );
3717 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003718 }
3719 else
3720#endif /* defined(MBEDTLS_RSA_C) */
3721#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003722 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003723 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003724#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003725 if(
3726#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3727 PSA_ALG_IS_ECDSA( alg )
3728#else
3729 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3730#endif
3731 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003732 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003733 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003734 status = psa_load_ecp_representation( slot->attr.type,
3735 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003736 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003737 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003738 if( status != PSA_SUCCESS )
3739 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003740 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003741 alg,
3742 hash, hash_length,
3743 signature, signature_size,
3744 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003745 mbedtls_ecp_keypair_free( ecp );
3746 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003747 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003748 else
3749#endif /* defined(MBEDTLS_ECDSA_C) */
3750 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003751 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003752 }
itayzafrir5c753392018-05-08 11:18:38 +03003753 }
3754 else
3755#endif /* defined(MBEDTLS_ECP_C) */
3756 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003757 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003758 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003759
3760exit:
3761 /* Fill the unused part of the output buffer (the whole buffer on error,
3762 * the trailing part on success) with something that isn't a valid mac
3763 * (barring an attack on the mac and deliberately-crafted input),
3764 * in case the caller doesn't check the return status properly. */
3765 if( status == PSA_SUCCESS )
3766 memset( signature + *signature_length, '!',
3767 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003768 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003769 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003770 /* If signature_size is 0 then we have nothing to do. We must not call
3771 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003772 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003773}
3774
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003775psa_status_t psa_verify_hash( psa_key_handle_t handle,
3776 psa_algorithm_t alg,
3777 const uint8_t *hash,
3778 size_t hash_length,
3779 const uint8_t *signature,
3780 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003781{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003782 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003783 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003784#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3785 const psa_drv_se_t *drv;
3786 psa_drv_se_context_t *drv_context;
3787#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003788
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003789 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003790 if( status != PSA_SUCCESS )
3791 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003792
Gilles Peskineedc64242019-08-07 21:05:07 +02003793#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3794 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3795 {
3796 if( drv->asymmetric == NULL ||
3797 drv->asymmetric->p_verify == NULL )
3798 return( PSA_ERROR_NOT_SUPPORTED );
3799 return( drv->asymmetric->p_verify( drv_context,
3800 slot->data.se.slot_number,
3801 alg,
3802 hash, hash_length,
3803 signature, signature_length ) );
3804 }
3805 else
3806#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003807#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003808 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003809 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003810 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003811
Steven Cooreman4fed4552020-08-03 14:46:03 +02003812 status = psa_load_rsa_representation( slot->attr.type,
3813 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003814 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003815 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003816 if( status != PSA_SUCCESS )
3817 return status;
3818
Steven Cooremana2371e52020-07-28 14:30:39 +02003819 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003820 alg,
3821 hash, hash_length,
3822 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003823 mbedtls_rsa_free( rsa );
3824 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003825 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003826 }
3827 else
3828#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003829#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003830 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003831 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003832#if defined(MBEDTLS_ECDSA_C)
3833 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003834 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003835 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003836 status = psa_load_ecp_representation( slot->attr.type,
3837 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003838 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003839 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003840 if( status != PSA_SUCCESS )
3841 return status;
Steven Cooremana2371e52020-07-28 14:30:39 +02003842 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003843 hash, hash_length,
3844 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003845 mbedtls_ecp_keypair_free( ecp );
3846 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003847 return status;
3848 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003849 else
3850#endif /* defined(MBEDTLS_ECDSA_C) */
3851 {
3852 return( PSA_ERROR_INVALID_ARGUMENT );
3853 }
itayzafrir5c753392018-05-08 11:18:38 +03003854 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003855 else
3856#endif /* defined(MBEDTLS_ECP_C) */
3857 {
3858 return( PSA_ERROR_NOT_SUPPORTED );
3859 }
3860}
3861
Gilles Peskine072ac562018-06-30 00:21:29 +02003862#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3863static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3864 mbedtls_rsa_context *rsa )
3865{
3866 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3867 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3868 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3869 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3870}
3871#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3872
Gilles Peskinec5487a82018-12-03 18:08:14 +01003873psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003874 psa_algorithm_t alg,
3875 const uint8_t *input,
3876 size_t input_length,
3877 const uint8_t *salt,
3878 size_t salt_length,
3879 uint8_t *output,
3880 size_t output_size,
3881 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003882{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003883 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003884 psa_status_t status;
3885
Darryl Green5cc689a2018-07-24 15:34:10 +01003886 (void) input;
3887 (void) input_length;
3888 (void) salt;
3889 (void) output;
3890 (void) output_size;
3891
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003892 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003893
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003894 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3895 return( PSA_ERROR_INVALID_ARGUMENT );
3896
Gilles Peskine28f8f302019-07-24 13:30:31 +02003897 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003898 if( status != PSA_SUCCESS )
3899 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003900 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3901 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003902 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003903
3904#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003905 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003906 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003907 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003908 status = psa_load_rsa_representation( slot->attr.type,
3909 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003910 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003911 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003912 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003913 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003914
3915 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003916 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003917 status = PSA_ERROR_BUFFER_TOO_SMALL;
3918 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003919 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003920#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003921 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003922 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003923 status = mbedtls_to_psa_error(
3924 mbedtls_rsa_pkcs1_encrypt( rsa,
3925 mbedtls_ctr_drbg_random,
3926 &global_data.ctr_drbg,
3927 MBEDTLS_RSA_PUBLIC,
3928 input_length,
3929 input,
3930 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931 }
3932 else
3933#endif /* MBEDTLS_PKCS1_V15 */
3934#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003935 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003936 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003937 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003938 status = mbedtls_to_psa_error(
3939 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3940 mbedtls_ctr_drbg_random,
3941 &global_data.ctr_drbg,
3942 MBEDTLS_RSA_PUBLIC,
3943 salt, salt_length,
3944 input_length,
3945 input,
3946 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003947 }
3948 else
3949#endif /* MBEDTLS_PKCS1_V21 */
3950 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003951 status = PSA_ERROR_INVALID_ARGUMENT;
3952 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003953 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003954rsa_exit:
3955 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003956 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003957
Steven Cooremana2371e52020-07-28 14:30:39 +02003958 mbedtls_rsa_free( rsa );
3959 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003960 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003961 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003963#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003964 {
3965 return( PSA_ERROR_NOT_SUPPORTED );
3966 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003967}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968
Gilles Peskinec5487a82018-12-03 18:08:14 +01003969psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003970 psa_algorithm_t alg,
3971 const uint8_t *input,
3972 size_t input_length,
3973 const uint8_t *salt,
3974 size_t salt_length,
3975 uint8_t *output,
3976 size_t output_size,
3977 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003978{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003979 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003980 psa_status_t status;
3981
Darryl Green5cc689a2018-07-24 15:34:10 +01003982 (void) input;
3983 (void) input_length;
3984 (void) salt;
3985 (void) output;
3986 (void) output_size;
3987
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003988 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003989
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003990 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3991 return( PSA_ERROR_INVALID_ARGUMENT );
3992
Gilles Peskine28f8f302019-07-24 13:30:31 +02003993 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003994 if( status != PSA_SUCCESS )
3995 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003996 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997 return( PSA_ERROR_INVALID_ARGUMENT );
3998
3999#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004000 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004001 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004002 mbedtls_rsa_context *rsa = NULL;
4003 status = psa_load_rsa_representation( slot->attr.type,
4004 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004005 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004006 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004007 if( status != PSA_SUCCESS )
4008 return status;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004009
Steven Cooremana2371e52020-07-28 14:30:39 +02004010 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004011 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004012 status = PSA_ERROR_INVALID_ARGUMENT;
4013 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004014 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015
4016#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004017 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004018 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004019 status = mbedtls_to_psa_error(
4020 mbedtls_rsa_pkcs1_decrypt( rsa,
4021 mbedtls_ctr_drbg_random,
4022 &global_data.ctr_drbg,
4023 MBEDTLS_RSA_PRIVATE,
4024 output_length,
4025 input,
4026 output,
4027 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028 }
4029 else
4030#endif /* MBEDTLS_PKCS1_V15 */
4031#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004032 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004033 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004034 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004035 status = mbedtls_to_psa_error(
4036 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4037 mbedtls_ctr_drbg_random,
4038 &global_data.ctr_drbg,
4039 MBEDTLS_RSA_PRIVATE,
4040 salt, salt_length,
4041 output_length,
4042 input,
4043 output,
4044 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004045 }
4046 else
4047#endif /* MBEDTLS_PKCS1_V21 */
4048 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004049 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004051
Steven Cooreman4fed4552020-08-03 14:46:03 +02004052rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004053 mbedtls_rsa_free( rsa );
4054 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004055 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004056 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004057 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004058#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004059 {
4060 return( PSA_ERROR_NOT_SUPPORTED );
4061 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004062}
Gilles Peskine20035e32018-02-03 22:44:14 +01004063
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004064
4065
mohammad1603503973b2018-03-12 15:59:30 +02004066/****************************************************************/
4067/* Symmetric cryptography */
4068/****************************************************************/
4069
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004070/* Initialize the cipher operation structure. Once this function has been
4071 * called, psa_cipher_abort can run and will do the right thing. */
4072static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
4073 psa_algorithm_t alg )
4074{
Gilles Peskinec06e0712018-06-20 16:21:04 +02004075 if( ! PSA_ALG_IS_CIPHER( alg ) )
4076 {
4077 memset( operation, 0, sizeof( *operation ) );
4078 return( PSA_ERROR_INVALID_ARGUMENT );
4079 }
4080
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004081 operation->alg = alg;
4082 operation->key_set = 0;
4083 operation->iv_set = 0;
4084 operation->iv_required = 1;
4085 operation->iv_size = 0;
4086 operation->block_size = 0;
4087 mbedtls_cipher_init( &operation->ctx.cipher );
4088 return( PSA_SUCCESS );
4089}
4090
Gilles Peskinee553c652018-06-04 16:22:46 +02004091static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004092 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004093 psa_algorithm_t alg,
4094 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004095{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004096 int ret = 0;
4097 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004098 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004099 size_t key_bits;
4100 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004101 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4102 PSA_KEY_USAGE_ENCRYPT :
4103 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004104
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004105 /* A context must be freshly initialized before it can be set up. */
4106 if( operation->alg != 0 )
4107 {
4108 return( PSA_ERROR_BAD_STATE );
4109 }
4110
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004111 status = psa_cipher_init( operation, alg );
4112 if( status != PSA_SUCCESS )
4113 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004114
Gilles Peskine28f8f302019-07-24 13:30:31 +02004115 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004116 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004117 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004118 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004119
Gilles Peskine8e338702019-07-30 20:06:31 +02004120 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004121 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004122 {
4123 status = PSA_ERROR_NOT_SUPPORTED;
4124 goto exit;
4125 }
mohammad1603503973b2018-03-12 15:59:30 +02004126
mohammad1603503973b2018-03-12 15:59:30 +02004127 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004128 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004129 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004130
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004131#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004132 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004133 {
4134 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004135 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004136 memcpy( keys, slot->data.key.data, 16 );
4137 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004138 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4139 keys,
4140 192, cipher_operation );
4141 }
4142 else
4143#endif
4144 {
4145 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004146 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004147 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004148 }
Moran Peker41deec42018-04-04 15:43:05 +03004149 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004150 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004151
mohammad16038481e742018-03-18 13:57:31 +02004152#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004153 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004154 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004155 case PSA_ALG_CBC_NO_PADDING:
4156 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4157 MBEDTLS_PADDING_NONE );
4158 break;
4159 case PSA_ALG_CBC_PKCS7:
4160 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4161 MBEDTLS_PADDING_PKCS7 );
4162 break;
4163 default:
4164 /* The algorithm doesn't involve padding. */
4165 ret = 0;
4166 break;
4167 }
4168 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004169 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004170#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4171
mohammad1603503973b2018-03-12 15:59:30 +02004172 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004173 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004174 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004175 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004176 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004177 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004178 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004179#if defined(MBEDTLS_CHACHA20_C)
4180 else
4181 if( alg == PSA_ALG_CHACHA20 )
4182 operation->iv_size = 12;
4183#endif
mohammad1603503973b2018-03-12 15:59:30 +02004184
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004185exit:
4186 if( status == 0 )
4187 status = mbedtls_to_psa_error( ret );
4188 if( status != 0 )
4189 psa_cipher_abort( operation );
4190 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004191}
4192
Gilles Peskinefe119512018-07-08 21:39:34 +02004193psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004194 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004195 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004196{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004197 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004198}
4199
Gilles Peskinefe119512018-07-08 21:39:34 +02004200psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004201 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004202 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004203{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004204 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004205}
4206
Gilles Peskinefe119512018-07-08 21:39:34 +02004207psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004208 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004209 size_t iv_size,
4210 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004211{
itayzafrir534bd7c2018-08-02 13:56:32 +03004212 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004214 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004215 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004216 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004217 }
Moran Peker41deec42018-04-04 15:43:05 +03004218 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004219 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004220 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004221 goto exit;
4222 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004223 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4224 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004225 if( ret != 0 )
4226 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004227 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004228 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004229 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004230
mohammad16038481e742018-03-18 13:57:31 +02004231 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004232 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004233
Moran Peker395db872018-05-31 14:07:14 +03004234exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004235 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004236 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004237 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004238}
4239
Gilles Peskinefe119512018-07-08 21:39:34 +02004240psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004241 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004242 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004243{
itayzafrir534bd7c2018-08-02 13:56:32 +03004244 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004246 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004247 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004248 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004249 }
Moran Pekera28258c2018-05-29 16:25:04 +03004250 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004251 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004252 status = PSA_ERROR_INVALID_ARGUMENT;
4253 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004254 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004255 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4256 status = mbedtls_to_psa_error( ret );
4257exit:
4258 if( status == PSA_SUCCESS )
4259 operation->iv_set = 1;
4260 else
mohammad1603503973b2018-03-12 15:59:30 +02004261 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004262 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004263}
4264
Gilles Peskinee553c652018-06-04 16:22:46 +02004265psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4266 const uint8_t *input,
4267 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004268 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004269 size_t output_size,
4270 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004271{
itayzafrir534bd7c2018-08-02 13:56:32 +03004272 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004274 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004275
4276 if( operation->alg == 0 )
4277 {
4278 return( PSA_ERROR_BAD_STATE );
4279 }
4280
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004281 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004282 {
4283 /* Take the unprocessed partial block left over from previous
4284 * update calls, if any, plus the input to this call. Remove
4285 * the last partial block, if any. You get the data that will be
4286 * output in this call. */
4287 expected_output_size =
4288 ( operation->ctx.cipher.unprocessed_len + input_length )
4289 / operation->block_size * operation->block_size;
4290 }
4291 else
4292 {
4293 expected_output_size = input_length;
4294 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004295
Gilles Peskine89d789c2018-06-04 17:17:16 +02004296 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004297 {
4298 status = PSA_ERROR_BUFFER_TOO_SMALL;
4299 goto exit;
4300 }
mohammad160382759612018-03-12 18:16:40 +02004301
mohammad1603503973b2018-03-12 15:59:30 +02004302 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004303 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004304 status = mbedtls_to_psa_error( ret );
4305exit:
4306 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004307 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004308 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004309}
4310
Gilles Peskinee553c652018-06-04 16:22:46 +02004311psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4312 uint8_t *output,
4313 size_t output_size,
4314 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004315{
David Saadab4ecc272019-02-14 13:48:10 +02004316 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004317 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004318 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004319
mohammad1603503973b2018-03-12 15:59:30 +02004320 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004321 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004322 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004323 }
4324 if( operation->iv_required && ! operation->iv_set )
4325 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004326 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004327 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004328
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004329 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004330 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4331 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004332 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004333 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004334 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004335 }
4336
Janos Follath315b51c2018-07-09 16:04:51 +01004337 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4338 temp_output_buffer,
4339 output_length );
4340 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004341 {
Janos Follath315b51c2018-07-09 16:04:51 +01004342 status = mbedtls_to_psa_error( cipher_ret );
4343 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004344 }
Janos Follath315b51c2018-07-09 16:04:51 +01004345
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004346 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004347 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004348 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004349 memcpy( output, temp_output_buffer, *output_length );
4350 else
4351 {
Janos Follath315b51c2018-07-09 16:04:51 +01004352 status = PSA_ERROR_BUFFER_TOO_SMALL;
4353 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004354 }
mohammad1603503973b2018-03-12 15:59:30 +02004355
Gilles Peskine3f108122018-12-07 18:14:53 +01004356 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004357 status = psa_cipher_abort( operation );
4358
4359 return( status );
4360
4361error:
4362
4363 *output_length = 0;
4364
Gilles Peskine3f108122018-12-07 18:14:53 +01004365 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004366 (void) psa_cipher_abort( operation );
4367
4368 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004369}
4370
Gilles Peskinee553c652018-06-04 16:22:46 +02004371psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4372{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004373 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004374 {
4375 /* The object has (apparently) been initialized but it is not
4376 * in use. It's ok to call abort on such an object, and there's
4377 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004378 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004379 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004380
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004381 /* Sanity check (shouldn't happen: operation->alg should
4382 * always have been initialized to a valid value). */
4383 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4384 return( PSA_ERROR_BAD_STATE );
4385
mohammad1603503973b2018-03-12 15:59:30 +02004386 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004387
Moran Peker41deec42018-04-04 15:43:05 +03004388 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004389 operation->key_set = 0;
4390 operation->iv_set = 0;
4391 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004392 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004393 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004394
Moran Peker395db872018-05-31 14:07:14 +03004395 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004396}
4397
Gilles Peskinea0655c32018-04-30 17:06:50 +02004398
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004399
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004400
mohammad16035955c982018-04-26 00:53:03 +03004401/****************************************************************/
4402/* AEAD */
4403/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004404
Gilles Peskineedf9a652018-08-17 18:11:56 +02004405typedef struct
4406{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004407 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004408 const mbedtls_cipher_info_t *cipher_info;
4409 union
4410 {
4411#if defined(MBEDTLS_CCM_C)
4412 mbedtls_ccm_context ccm;
4413#endif /* MBEDTLS_CCM_C */
4414#if defined(MBEDTLS_GCM_C)
4415 mbedtls_gcm_context gcm;
4416#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004417#if defined(MBEDTLS_CHACHAPOLY_C)
4418 mbedtls_chachapoly_context chachapoly;
4419#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004420 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004421 psa_algorithm_t core_alg;
4422 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004423 uint8_t tag_length;
4424} aead_operation_t;
4425
Gilles Peskine30a9e412019-01-14 18:36:12 +01004426static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004427{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004428 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004429 {
4430#if defined(MBEDTLS_CCM_C)
4431 case PSA_ALG_CCM:
4432 mbedtls_ccm_free( &operation->ctx.ccm );
4433 break;
4434#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004435#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004436 case PSA_ALG_GCM:
4437 mbedtls_gcm_free( &operation->ctx.gcm );
4438 break;
4439#endif /* MBEDTLS_GCM_C */
4440 }
4441}
4442
4443static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004444 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004445 psa_key_usage_t usage,
4446 psa_algorithm_t alg )
4447{
4448 psa_status_t status;
4449 size_t key_bits;
4450 mbedtls_cipher_id_t cipher_id;
4451
Gilles Peskine28f8f302019-07-24 13:30:31 +02004452 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004453 if( status != PSA_SUCCESS )
4454 return( status );
4455
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004456 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004457
4458 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004459 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004460 &cipher_id );
4461 if( operation->cipher_info == NULL )
4462 return( PSA_ERROR_NOT_SUPPORTED );
4463
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004464 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004465 {
4466#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004467 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4468 operation->core_alg = PSA_ALG_CCM;
4469 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004470 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4471 * The call to mbedtls_ccm_encrypt_and_tag or
4472 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004473 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004474 return( PSA_ERROR_INVALID_ARGUMENT );
4475 mbedtls_ccm_init( &operation->ctx.ccm );
4476 status = mbedtls_to_psa_error(
4477 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004478 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004479 (unsigned int) key_bits ) );
4480 if( status != 0 )
4481 goto cleanup;
4482 break;
4483#endif /* MBEDTLS_CCM_C */
4484
4485#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004486 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4487 operation->core_alg = PSA_ALG_GCM;
4488 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004489 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4490 * The call to mbedtls_gcm_crypt_and_tag or
4491 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004492 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004493 return( PSA_ERROR_INVALID_ARGUMENT );
4494 mbedtls_gcm_init( &operation->ctx.gcm );
4495 status = mbedtls_to_psa_error(
4496 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004497 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004498 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004499 if( status != 0 )
4500 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004501 break;
4502#endif /* MBEDTLS_GCM_C */
4503
Gilles Peskine26869f22019-05-06 15:25:00 +02004504#if defined(MBEDTLS_CHACHAPOLY_C)
4505 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4506 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4507 operation->full_tag_length = 16;
4508 /* We only support the default tag length. */
4509 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4510 return( PSA_ERROR_NOT_SUPPORTED );
4511 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4512 status = mbedtls_to_psa_error(
4513 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004514 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004515 if( status != 0 )
4516 goto cleanup;
4517 break;
4518#endif /* MBEDTLS_CHACHAPOLY_C */
4519
Gilles Peskineedf9a652018-08-17 18:11:56 +02004520 default:
4521 return( PSA_ERROR_NOT_SUPPORTED );
4522 }
4523
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004524 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4525 {
4526 status = PSA_ERROR_INVALID_ARGUMENT;
4527 goto cleanup;
4528 }
4529 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004530
Gilles Peskineedf9a652018-08-17 18:11:56 +02004531 return( PSA_SUCCESS );
4532
4533cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004534 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004535 return( status );
4536}
4537
Gilles Peskinec5487a82018-12-03 18:08:14 +01004538psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004539 psa_algorithm_t alg,
4540 const uint8_t *nonce,
4541 size_t nonce_length,
4542 const uint8_t *additional_data,
4543 size_t additional_data_length,
4544 const uint8_t *plaintext,
4545 size_t plaintext_length,
4546 uint8_t *ciphertext,
4547 size_t ciphertext_size,
4548 size_t *ciphertext_length )
4549{
mohammad16035955c982018-04-26 00:53:03 +03004550 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004551 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004552 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004553
mohammad1603f08a5502018-06-03 15:05:47 +03004554 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004555
Gilles Peskinec5487a82018-12-03 18:08:14 +01004556 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004557 if( status != PSA_SUCCESS )
4558 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004559
Gilles Peskineedf9a652018-08-17 18:11:56 +02004560 /* For all currently supported modes, the tag is at the end of the
4561 * ciphertext. */
4562 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4563 {
4564 status = PSA_ERROR_BUFFER_TOO_SMALL;
4565 goto exit;
4566 }
4567 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004568
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004569#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004570 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004571 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004572 status = mbedtls_to_psa_error(
4573 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4574 MBEDTLS_GCM_ENCRYPT,
4575 plaintext_length,
4576 nonce, nonce_length,
4577 additional_data, additional_data_length,
4578 plaintext, ciphertext,
4579 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004580 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004581 else
4582#endif /* MBEDTLS_GCM_C */
4583#if defined(MBEDTLS_CCM_C)
4584 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004585 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004586 status = mbedtls_to_psa_error(
4587 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4588 plaintext_length,
4589 nonce, nonce_length,
4590 additional_data,
4591 additional_data_length,
4592 plaintext, ciphertext,
4593 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004594 }
mohammad16035c8845f2018-05-09 05:40:09 -07004595 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004596#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004597#if defined(MBEDTLS_CHACHAPOLY_C)
4598 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4599 {
4600 if( nonce_length != 12 || operation.tag_length != 16 )
4601 {
4602 status = PSA_ERROR_NOT_SUPPORTED;
4603 goto exit;
4604 }
4605 status = mbedtls_to_psa_error(
4606 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4607 plaintext_length,
4608 nonce,
4609 additional_data,
4610 additional_data_length,
4611 plaintext,
4612 ciphertext,
4613 tag ) );
4614 }
4615 else
4616#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004617 {
mohammad1603554faad2018-06-03 15:07:38 +03004618 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004619 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004620
Gilles Peskineedf9a652018-08-17 18:11:56 +02004621 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4622 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004623
Gilles Peskineedf9a652018-08-17 18:11:56 +02004624exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004625 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004626 if( status == PSA_SUCCESS )
4627 *ciphertext_length = plaintext_length + operation.tag_length;
4628 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004629}
4630
Gilles Peskineee652a32018-06-01 19:23:52 +02004631/* Locate the tag in a ciphertext buffer containing the encrypted data
4632 * followed by the tag. Return the length of the part preceding the tag in
4633 * *plaintext_length. This is the size of the plaintext in modes where
4634 * the encrypted data has the same size as the plaintext, such as
4635 * CCM and GCM. */
4636static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4637 const uint8_t *ciphertext,
4638 size_t ciphertext_length,
4639 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004640 const uint8_t **p_tag )
4641{
4642 size_t payload_length;
4643 if( tag_length > ciphertext_length )
4644 return( PSA_ERROR_INVALID_ARGUMENT );
4645 payload_length = ciphertext_length - tag_length;
4646 if( payload_length > plaintext_size )
4647 return( PSA_ERROR_BUFFER_TOO_SMALL );
4648 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004649 return( PSA_SUCCESS );
4650}
4651
Gilles Peskinec5487a82018-12-03 18:08:14 +01004652psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004653 psa_algorithm_t alg,
4654 const uint8_t *nonce,
4655 size_t nonce_length,
4656 const uint8_t *additional_data,
4657 size_t additional_data_length,
4658 const uint8_t *ciphertext,
4659 size_t ciphertext_length,
4660 uint8_t *plaintext,
4661 size_t plaintext_size,
4662 size_t *plaintext_length )
4663{
mohammad16035955c982018-04-26 00:53:03 +03004664 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004665 aead_operation_t operation;
4666 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004667
Gilles Peskineee652a32018-06-01 19:23:52 +02004668 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004669
Gilles Peskinec5487a82018-12-03 18:08:14 +01004670 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004671 if( status != PSA_SUCCESS )
4672 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004673
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004674 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4675 ciphertext, ciphertext_length,
4676 plaintext_size, &tag );
4677 if( status != PSA_SUCCESS )
4678 goto exit;
4679
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004680#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004681 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004682 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004683 status = mbedtls_to_psa_error(
4684 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4685 ciphertext_length - operation.tag_length,
4686 nonce, nonce_length,
4687 additional_data,
4688 additional_data_length,
4689 tag, operation.tag_length,
4690 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004691 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004692 else
4693#endif /* MBEDTLS_GCM_C */
4694#if defined(MBEDTLS_CCM_C)
4695 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004696 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004697 status = mbedtls_to_psa_error(
4698 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4699 ciphertext_length - operation.tag_length,
4700 nonce, nonce_length,
4701 additional_data,
4702 additional_data_length,
4703 ciphertext, plaintext,
4704 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004705 }
mohammad160339574652018-06-01 04:39:53 -07004706 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004707#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004708#if defined(MBEDTLS_CHACHAPOLY_C)
4709 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4710 {
4711 if( nonce_length != 12 || operation.tag_length != 16 )
4712 {
4713 status = PSA_ERROR_NOT_SUPPORTED;
4714 goto exit;
4715 }
4716 status = mbedtls_to_psa_error(
4717 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4718 ciphertext_length - operation.tag_length,
4719 nonce,
4720 additional_data,
4721 additional_data_length,
4722 tag,
4723 ciphertext,
4724 plaintext ) );
4725 }
4726 else
4727#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004728 {
mohammad1603554faad2018-06-03 15:07:38 +03004729 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004730 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004731
Gilles Peskineedf9a652018-08-17 18:11:56 +02004732 if( status != PSA_SUCCESS && plaintext_size != 0 )
4733 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004734
Gilles Peskineedf9a652018-08-17 18:11:56 +02004735exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004736 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004737 if( status == PSA_SUCCESS )
4738 *plaintext_length = ciphertext_length - operation.tag_length;
4739 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004740}
4741
Gilles Peskinea0655c32018-04-30 17:06:50 +02004742
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004743
Gilles Peskine20035e32018-02-03 22:44:14 +01004744/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745/* Generators */
4746/****************************************************************/
4747
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004748#define HKDF_STATE_INIT 0 /* no input yet */
4749#define HKDF_STATE_STARTED 1 /* got salt */
4750#define HKDF_STATE_KEYED 2 /* got key */
4751#define HKDF_STATE_OUTPUT 3 /* output started */
4752
Gilles Peskinecbe66502019-05-16 16:59:18 +02004753static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004754 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004755{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004756 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4757 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004758 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004760}
4761
4762
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004764{
4765 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004767 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004768 {
4769 /* The object has (apparently) been initialized but it is not
4770 * in use. It's ok to call abort on such an object, and there's
4771 * nothing to do. */
4772 }
4773 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004774#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004775 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004776 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004777 mbedtls_free( operation->ctx.hkdf.info );
4778 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004779 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004780 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004781 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004782 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004783 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004784 if( operation->ctx.tls12_prf.seed != NULL )
4785 {
4786 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4787 operation->ctx.tls12_prf.seed_length );
4788 mbedtls_free( operation->ctx.tls12_prf.seed );
4789 }
4790
4791 if( operation->ctx.tls12_prf.label != NULL )
4792 {
4793 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4794 operation->ctx.tls12_prf.label_length );
4795 mbedtls_free( operation->ctx.tls12_prf.label );
4796 }
4797
4798 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4799
4800 /* We leave the fields Ai and output_block to be erased safely by the
4801 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004802 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004803 else
4804#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004805 {
4806 status = PSA_ERROR_BAD_STATE;
4807 }
Janos Follath083036a2019-06-11 10:22:26 +01004808 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004809 return( status );
4810}
4811
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004812psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004813 size_t *capacity)
4814{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004815 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004816 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004817 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004818 return PSA_ERROR_BAD_STATE;
4819 }
4820
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004821 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004822 return( PSA_SUCCESS );
4823}
4824
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004825psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004826 size_t capacity )
4827{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004829 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004831 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004832 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004833 return( PSA_SUCCESS );
4834}
4835
Gilles Peskinebef7f142018-07-12 17:22:21 +02004836#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004838 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004839static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004840 psa_algorithm_t hash_alg,
4841 uint8_t *output,
4842 size_t output_length )
4843{
4844 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4845 psa_status_t status;
4846
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004847 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4848 return( PSA_ERROR_BAD_STATE );
4849 hkdf->state = HKDF_STATE_OUTPUT;
4850
Gilles Peskinebef7f142018-07-12 17:22:21 +02004851 while( output_length != 0 )
4852 {
4853 /* Copy what remains of the current block */
4854 uint8_t n = hash_length - hkdf->offset_in_block;
4855 if( n > output_length )
4856 n = (uint8_t) output_length;
4857 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4858 output += n;
4859 output_length -= n;
4860 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004861 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004862 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004863 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004864 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004866 * object was corrupted or if this function is called directly
4867 * inside the library. */
4868 if( hkdf->block_number == 0xff )
4869 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004870
4871 /* We need a new block */
4872 ++hkdf->block_number;
4873 hkdf->offset_in_block = 0;
4874 status = psa_hmac_setup_internal( &hkdf->hmac,
4875 hkdf->prk, hash_length,
4876 hash_alg );
4877 if( status != PSA_SUCCESS )
4878 return( status );
4879 if( hkdf->block_number != 1 )
4880 {
4881 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4882 hkdf->output_block,
4883 hash_length );
4884 if( status != PSA_SUCCESS )
4885 return( status );
4886 }
4887 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4888 hkdf->info,
4889 hkdf->info_length );
4890 if( status != PSA_SUCCESS )
4891 return( status );
4892 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4893 &hkdf->block_number, 1 );
4894 if( status != PSA_SUCCESS )
4895 return( status );
4896 status = psa_hmac_finish_internal( &hkdf->hmac,
4897 hkdf->output_block,
4898 sizeof( hkdf->output_block ) );
4899 if( status != PSA_SUCCESS )
4900 return( status );
4901 }
4902
4903 return( PSA_SUCCESS );
4904}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004905
Janos Follath7742fee2019-06-17 12:58:10 +01004906static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4907 psa_tls12_prf_key_derivation_t *tls12_prf,
4908 psa_algorithm_t alg )
4909{
4910 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4911 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004912 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4913 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004914
Janos Follath7742fee2019-06-17 12:58:10 +01004915 /* We can't be wanting more output after block 0xff, otherwise
4916 * the capacity check in psa_key_derivation_output_bytes() would have
4917 * prevented this call. It could happen only if the operation
4918 * object was corrupted or if this function is called directly
4919 * inside the library. */
4920 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004921 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004922
4923 /* We need a new block */
4924 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004925 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004926
4927 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4928 *
4929 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4930 *
4931 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4932 * HMAC_hash(secret, A(2) + seed) +
4933 * HMAC_hash(secret, A(3) + seed) + ...
4934 *
4935 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004936 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004937 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004938 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004939 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004940 * is currently extracted as `output_block` and where i is
4941 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004942 */
4943
Janos Follathea29bfb2019-06-19 12:21:20 +01004944 /* Save the hash context before using it, to preserve the hash state with
4945 * only the inner padding in it. We need this, because inner padding depends
4946 * on the key (secret in the RFC's terminology). */
4947 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4948 if( status != PSA_SUCCESS )
4949 goto cleanup;
4950
4951 /* Calculate A(i) where i = tls12_prf->block_number. */
4952 if( tls12_prf->block_number == 1 )
4953 {
4954 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4955 * the variable seed and in this instance means it in the context of the
4956 * P_hash function, where seed = label + seed.) */
4957 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4958 tls12_prf->label, tls12_prf->label_length );
4959 if( status != PSA_SUCCESS )
4960 goto cleanup;
4961 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4962 tls12_prf->seed, tls12_prf->seed_length );
4963 if( status != PSA_SUCCESS )
4964 goto cleanup;
4965 }
4966 else
4967 {
4968 /* A(i) = HMAC_hash(secret, A(i-1)) */
4969 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4970 tls12_prf->Ai, hash_length );
4971 if( status != PSA_SUCCESS )
4972 goto cleanup;
4973 }
4974
4975 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4976 tls12_prf->Ai, hash_length );
4977 if( status != PSA_SUCCESS )
4978 goto cleanup;
4979 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4980 if( status != PSA_SUCCESS )
4981 goto cleanup;
4982
4983 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4984 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4985 tls12_prf->Ai, hash_length );
4986 if( status != PSA_SUCCESS )
4987 goto cleanup;
4988 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4989 tls12_prf->label, tls12_prf->label_length );
4990 if( status != PSA_SUCCESS )
4991 goto cleanup;
4992 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4993 tls12_prf->seed, tls12_prf->seed_length );
4994 if( status != PSA_SUCCESS )
4995 goto cleanup;
4996 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4997 tls12_prf->output_block, hash_length );
4998 if( status != PSA_SUCCESS )
4999 goto cleanup;
5000 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5001 if( status != PSA_SUCCESS )
5002 goto cleanup;
5003
Janos Follath7742fee2019-06-17 12:58:10 +01005004
5005cleanup:
5006
Janos Follathea29bfb2019-06-19 12:21:20 +01005007 cleanup_status = psa_hash_abort( &backup );
5008 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5009 status = cleanup_status;
5010
5011 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005012}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005013
Janos Follath844eb0e2019-06-19 12:10:49 +01005014static psa_status_t psa_key_derivation_tls12_prf_read(
5015 psa_tls12_prf_key_derivation_t *tls12_prf,
5016 psa_algorithm_t alg,
5017 uint8_t *output,
5018 size_t output_length )
5019{
5020 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5021 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5022 psa_status_t status;
5023 uint8_t offset, length;
5024
5025 while( output_length != 0 )
5026 {
5027 /* Check if we have fully processed the current block. */
5028 if( tls12_prf->left_in_block == 0 )
5029 {
5030 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5031 alg );
5032 if( status != PSA_SUCCESS )
5033 return( status );
5034
5035 continue;
5036 }
5037
5038 if( tls12_prf->left_in_block > output_length )
5039 length = (uint8_t) output_length;
5040 else
5041 length = tls12_prf->left_in_block;
5042
5043 offset = hash_length - tls12_prf->left_in_block;
5044 memcpy( output, tls12_prf->output_block + offset, length );
5045 output += length;
5046 output_length -= length;
5047 tls12_prf->left_in_block -= length;
5048 }
5049
5050 return( PSA_SUCCESS );
5051}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005052#endif /* MBEDTLS_MD_C */
5053
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005054psa_status_t psa_key_derivation_output_bytes(
5055 psa_key_derivation_operation_t *operation,
5056 uint8_t *output,
5057 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005058{
5059 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005060 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005061
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005063 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005065 return PSA_ERROR_BAD_STATE;
5066 }
5067
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005068 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005069 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005070 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005071 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005072 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005073 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005074 goto exit;
5075 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005076 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005077 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005078 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005079 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005080 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5081 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005082 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005083 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005084 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005085 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005087
Gilles Peskinebef7f142018-07-12 17:22:21 +02005088#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005089 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005090 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005091 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005092 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005093 output, output_length );
5094 }
Janos Follathadbec812019-06-14 11:05:39 +01005095 else
Janos Follathadbec812019-06-14 11:05:39 +01005096 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005097 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005098 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005100 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005101 output_length );
5102 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005103 else
5104#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005105 {
5106 return( PSA_ERROR_BAD_STATE );
5107 }
5108
5109exit:
5110 if( status != PSA_SUCCESS )
5111 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005112 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005113 * This allows us to differentiate between exhausted operations and
5114 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5115 * operations. */
5116 psa_algorithm_t alg = operation->alg;
5117 psa_key_derivation_abort( operation );
5118 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005119 memset( output, '!', output_length );
5120 }
5121 return( status );
5122}
5123
Gilles Peskine08542d82018-07-19 17:05:42 +02005124#if defined(MBEDTLS_DES_C)
5125static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5126{
5127 if( data_size >= 8 )
5128 mbedtls_des_key_set_parity( data );
5129 if( data_size >= 16 )
5130 mbedtls_des_key_set_parity( data + 8 );
5131 if( data_size >= 24 )
5132 mbedtls_des_key_set_parity( data + 16 );
5133}
5134#endif /* MBEDTLS_DES_C */
5135
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005136static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005137 psa_key_slot_t *slot,
5138 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005140{
5141 uint8_t *data = NULL;
5142 size_t bytes = PSA_BITS_TO_BYTES( bits );
5143 psa_status_t status;
5144
Gilles Peskine8e338702019-07-30 20:06:31 +02005145 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005146 return( PSA_ERROR_INVALID_ARGUMENT );
5147 if( bits % 8 != 0 )
5148 return( PSA_ERROR_INVALID_ARGUMENT );
5149 data = mbedtls_calloc( 1, bytes );
5150 if( data == NULL )
5151 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5152
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005153 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005154 if( status != PSA_SUCCESS )
5155 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005156#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005157 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005158 psa_des_set_key_parity( data, bytes );
5159#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005160 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005161
5162exit:
5163 mbedtls_free( data );
5164 return( status );
5165}
5166
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005167psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005168 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005169 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005170{
5171 psa_status_t status;
5172 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005173 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005174
5175 /* Reject any attempt to create a zero-length key so that we don't
5176 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5177 if( psa_get_key_bits( attributes ) == 0 )
5178 return( PSA_ERROR_INVALID_ARGUMENT );
5179
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005180 if( ! operation->can_output_key )
5181 return( PSA_ERROR_NOT_PERMITTED );
5182
Gilles Peskinedf179142019-07-15 22:02:14 +02005183 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5184 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005185#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5186 if( driver != NULL )
5187 {
5188 /* Deriving a key in a secure element is not implemented yet. */
5189 status = PSA_ERROR_NOT_SUPPORTED;
5190 }
5191#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005192 if( status == PSA_SUCCESS )
5193 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005194 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005195 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005196 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005197 }
5198 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005199 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005200 if( status != PSA_SUCCESS )
5201 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005202 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005203 *handle = 0;
5204 }
5205 return( status );
5206}
5207
Gilles Peskineeab56e42018-07-12 17:12:33 +02005208
5209
5210/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005211/* Key derivation */
5212/****************************************************************/
5213
Gilles Peskine969c5d62019-01-16 15:53:06 +01005214static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005216 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005217{
Janos Follath5fe19732019-06-20 15:09:30 +01005218 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5219 * initialisers for this union leave some bytes unspecified.) */
5220 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5221
Gilles Peskine969c5d62019-01-16 15:53:06 +01005222 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005223#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005224 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5225 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5226 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005227 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005228 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005229 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5230 if( hash_size == 0 )
5231 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005232 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5233 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005234 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005235 {
5236 return( PSA_ERROR_NOT_SUPPORTED );
5237 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005238 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005239 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005240 }
5241#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005242 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005243 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005244}
5245
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005246psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005247 psa_algorithm_t alg )
5248{
5249 psa_status_t status;
5250
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005252 return( PSA_ERROR_BAD_STATE );
5253
Gilles Peskine6843c292019-01-18 16:44:49 +01005254 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5255 return( PSA_ERROR_INVALID_ARGUMENT );
5256 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005257 {
5258 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005259 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005260 }
5261 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5262 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005264 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005265 else
5266 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005267
5268 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005270 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005271}
5272
5273#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005274static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005275 psa_algorithm_t hash_alg,
5276 psa_key_derivation_step_t step,
5277 const uint8_t *data,
5278 size_t data_length )
5279{
5280 psa_status_t status;
5281 switch( step )
5282 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005283 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005284 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005285 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005286 status = psa_hmac_setup_internal( &hkdf->hmac,
5287 data, data_length,
5288 hash_alg );
5289 if( status != PSA_SUCCESS )
5290 return( status );
5291 hkdf->state = HKDF_STATE_STARTED;
5292 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005293 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005294 /* If no salt was provided, use an empty salt. */
5295 if( hkdf->state == HKDF_STATE_INIT )
5296 {
5297 status = psa_hmac_setup_internal( &hkdf->hmac,
5298 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005299 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005300 if( status != PSA_SUCCESS )
5301 return( status );
5302 hkdf->state = HKDF_STATE_STARTED;
5303 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005304 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005305 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005306 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5307 data, data_length );
5308 if( status != PSA_SUCCESS )
5309 return( status );
5310 status = psa_hmac_finish_internal( &hkdf->hmac,
5311 hkdf->prk,
5312 sizeof( hkdf->prk ) );
5313 if( status != PSA_SUCCESS )
5314 return( status );
5315 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5316 hkdf->block_number = 0;
5317 hkdf->state = HKDF_STATE_KEYED;
5318 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005319 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005320 if( hkdf->state == HKDF_STATE_OUTPUT )
5321 return( PSA_ERROR_BAD_STATE );
5322 if( hkdf->info_set )
5323 return( PSA_ERROR_BAD_STATE );
5324 hkdf->info_length = data_length;
5325 if( data_length != 0 )
5326 {
5327 hkdf->info = mbedtls_calloc( 1, data_length );
5328 if( hkdf->info == NULL )
5329 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5330 memcpy( hkdf->info, data, data_length );
5331 }
5332 hkdf->info_set = 1;
5333 return( PSA_SUCCESS );
5334 default:
5335 return( PSA_ERROR_INVALID_ARGUMENT );
5336 }
5337}
Janos Follathb03233e2019-06-11 15:30:30 +01005338
Janos Follathf08e2652019-06-13 09:05:41 +01005339static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5340 const uint8_t *data,
5341 size_t data_length )
5342{
5343 if( prf->state != TLS12_PRF_STATE_INIT )
5344 return( PSA_ERROR_BAD_STATE );
5345
Janos Follathd6dce9f2019-07-04 09:11:38 +01005346 if( data_length != 0 )
5347 {
5348 prf->seed = mbedtls_calloc( 1, data_length );
5349 if( prf->seed == NULL )
5350 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005351
Janos Follathd6dce9f2019-07-04 09:11:38 +01005352 memcpy( prf->seed, data, data_length );
5353 prf->seed_length = data_length;
5354 }
Janos Follathf08e2652019-06-13 09:05:41 +01005355
5356 prf->state = TLS12_PRF_STATE_SEED_SET;
5357
5358 return( PSA_SUCCESS );
5359}
5360
Janos Follath81550542019-06-13 14:26:34 +01005361static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5362 psa_algorithm_t hash_alg,
5363 const uint8_t *data,
5364 size_t data_length )
5365{
5366 psa_status_t status;
5367 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5368 return( PSA_ERROR_BAD_STATE );
5369
5370 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5371 if( status != PSA_SUCCESS )
5372 return( status );
5373
5374 prf->state = TLS12_PRF_STATE_KEY_SET;
5375
5376 return( PSA_SUCCESS );
5377}
5378
Janos Follath6660f0e2019-06-17 08:44:03 +01005379static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5380 psa_tls12_prf_key_derivation_t *prf,
5381 psa_algorithm_t hash_alg,
5382 const uint8_t *data,
5383 size_t data_length )
5384{
5385 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005386 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5387 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005388
5389 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5390 return( PSA_ERROR_INVALID_ARGUMENT );
5391
5392 /* Quoting RFC 4279, Section 2:
5393 *
5394 * The premaster secret is formed as follows: if the PSK is N octets
5395 * long, concatenate a uint16 with the value N, N zero octets, a second
5396 * uint16 with the value N, and the PSK itself.
5397 */
5398
Janos Follath40e13932019-06-26 13:22:29 +01005399 *cur++ = ( data_length >> 8 ) & 0xff;
5400 *cur++ = ( data_length >> 0 ) & 0xff;
5401 memset( cur, 0, data_length );
5402 cur += data_length;
5403 *cur++ = pms[0];
5404 *cur++ = pms[1];
5405 memcpy( cur, data, data_length );
5406 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005407
Janos Follath40e13932019-06-26 13:22:29 +01005408 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005409
5410 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5411 return( status );
5412}
5413
Janos Follath63028dd2019-06-13 09:15:47 +01005414static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5415 const uint8_t *data,
5416 size_t data_length )
5417{
5418 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5419 return( PSA_ERROR_BAD_STATE );
5420
Janos Follathd6dce9f2019-07-04 09:11:38 +01005421 if( data_length != 0 )
5422 {
5423 prf->label = mbedtls_calloc( 1, data_length );
5424 if( prf->label == NULL )
5425 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005426
Janos Follathd6dce9f2019-07-04 09:11:38 +01005427 memcpy( prf->label, data, data_length );
5428 prf->label_length = data_length;
5429 }
Janos Follath63028dd2019-06-13 09:15:47 +01005430
5431 prf->state = TLS12_PRF_STATE_LABEL_SET;
5432
5433 return( PSA_SUCCESS );
5434}
5435
Janos Follathb03233e2019-06-11 15:30:30 +01005436static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5437 psa_algorithm_t hash_alg,
5438 psa_key_derivation_step_t step,
5439 const uint8_t *data,
5440 size_t data_length )
5441{
Janos Follathb03233e2019-06-11 15:30:30 +01005442 switch( step )
5443 {
Janos Follathf08e2652019-06-13 09:05:41 +01005444 case PSA_KEY_DERIVATION_INPUT_SEED:
5445 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005446 case PSA_KEY_DERIVATION_INPUT_SECRET:
5447 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005448 case PSA_KEY_DERIVATION_INPUT_LABEL:
5449 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005450 default:
5451 return( PSA_ERROR_INVALID_ARGUMENT );
5452 }
5453}
Janos Follath6660f0e2019-06-17 08:44:03 +01005454
5455static psa_status_t psa_tls12_prf_psk_to_ms_input(
5456 psa_tls12_prf_key_derivation_t *prf,
5457 psa_algorithm_t hash_alg,
5458 psa_key_derivation_step_t step,
5459 const uint8_t *data,
5460 size_t data_length )
5461{
5462 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005463 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005464 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5465 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005466 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005467
5468 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5469}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005470#endif /* MBEDTLS_MD_C */
5471
Gilles Peskineb8965192019-09-24 16:21:10 +02005472/** Check whether the given key type is acceptable for the given
5473 * input step of a key derivation.
5474 *
5475 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5476 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5477 * Both secret and non-secret inputs can alternatively have the type
5478 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5479 * that the input was passed as a buffer rather than via a key object.
5480 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005481static int psa_key_derivation_check_input_type(
5482 psa_key_derivation_step_t step,
5483 psa_key_type_t key_type )
5484{
5485 switch( step )
5486 {
5487 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005488 if( key_type == PSA_KEY_TYPE_DERIVE )
5489 return( PSA_SUCCESS );
5490 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005491 return( PSA_SUCCESS );
5492 break;
5493 case PSA_KEY_DERIVATION_INPUT_LABEL:
5494 case PSA_KEY_DERIVATION_INPUT_SALT:
5495 case PSA_KEY_DERIVATION_INPUT_INFO:
5496 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005497 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5498 return( PSA_SUCCESS );
5499 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005500 return( PSA_SUCCESS );
5501 break;
5502 }
5503 return( PSA_ERROR_INVALID_ARGUMENT );
5504}
5505
Janos Follathb80a94e2019-06-12 15:54:46 +01005506static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005507 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005508 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005509 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005510 const uint8_t *data,
5511 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005512{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005513 psa_status_t status;
5514 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5515
5516 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005517 if( status != PSA_SUCCESS )
5518 goto exit;
5519
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005520#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005521 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005522 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005524 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005525 step, data, data_length );
5526 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005527 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005528 {
Janos Follathb03233e2019-06-11 15:30:30 +01005529 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5530 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5531 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005532 }
5533 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5534 {
5535 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5536 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5537 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005538 }
5539 else
5540#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005541 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005542 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005543 return( PSA_ERROR_BAD_STATE );
5544 }
5545
Gilles Peskine224b0d62019-09-23 18:13:17 +02005546exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005547 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005548 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005549 return( status );
5550}
5551
Janos Follath51f4a0f2019-06-14 11:35:55 +01005552psa_status_t psa_key_derivation_input_bytes(
5553 psa_key_derivation_operation_t *operation,
5554 psa_key_derivation_step_t step,
5555 const uint8_t *data,
5556 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005557{
Gilles Peskineb8965192019-09-24 16:21:10 +02005558 return( psa_key_derivation_input_internal( operation, step,
5559 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005560 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005561}
5562
Janos Follath51f4a0f2019-06-14 11:35:55 +01005563psa_status_t psa_key_derivation_input_key(
5564 psa_key_derivation_operation_t *operation,
5565 psa_key_derivation_step_t step,
5566 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005567{
5568 psa_key_slot_t *slot;
5569 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005570
Gilles Peskine28f8f302019-07-24 13:30:31 +02005571 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005572 PSA_KEY_USAGE_DERIVE,
5573 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005574 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005575 {
5576 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005577 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005578 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005579
5580 /* Passing a key object as a SECRET input unlocks the permission
5581 * to output to a key object. */
5582 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5583 operation->can_output_key = 1;
5584
Janos Follathb80a94e2019-06-12 15:54:46 +01005585 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005586 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005587 slot->data.key.data,
5588 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005589}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005590
5591
5592
5593/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005594/* Key agreement */
5595/****************************************************************/
5596
Gilles Peskinea05219c2018-11-16 16:02:56 +01005597#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005598static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5599 size_t peer_key_length,
5600 const mbedtls_ecp_keypair *our_key,
5601 uint8_t *shared_secret,
5602 size_t shared_secret_size,
5603 size_t *shared_secret_length )
5604{
Steven Cooremana2371e52020-07-28 14:30:39 +02005605 mbedtls_ecp_keypair *their_key;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005606 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005607 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005608 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005609 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005610 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005611
Steven Cooreman4fed4552020-08-03 14:46:03 +02005612 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5613 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005614 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005615 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005616 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005617 goto exit;
5618
Jaeden Amero97271b32019-01-10 19:38:51 +00005619 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005620 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005621 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005622 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005623 status = mbedtls_to_psa_error(
5624 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5625 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005626 goto exit;
5627
Jaeden Amero97271b32019-01-10 19:38:51 +00005628 status = mbedtls_to_psa_error(
5629 mbedtls_ecdh_calc_secret( &ecdh,
5630 shared_secret_length,
5631 shared_secret, shared_secret_size,
5632 mbedtls_ctr_drbg_random,
5633 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005634 if( status != PSA_SUCCESS )
5635 goto exit;
5636 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5637 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005638
5639exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005640 if( status != PSA_SUCCESS )
5641 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005642 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005643 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005644 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005645
Jaeden Amero97271b32019-01-10 19:38:51 +00005646 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005647}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005648#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005649
Gilles Peskine01d718c2018-09-18 12:01:02 +02005650#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5651
Gilles Peskine0216fe12019-04-11 21:23:21 +02005652static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5653 psa_key_slot_t *private_key,
5654 const uint8_t *peer_key,
5655 size_t peer_key_length,
5656 uint8_t *shared_secret,
5657 size_t shared_secret_size,
5658 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005659{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005660 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005661 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005662#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005663 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005664 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005665 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005666 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005667 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005668 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005669 private_key->data.key.data,
5670 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005671 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005672 if( status != PSA_SUCCESS )
5673 return status;
5674 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005675 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005676 shared_secret, shared_secret_size,
5677 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005678 mbedtls_ecp_keypair_free( ecp );
5679 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005680 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005681#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005682 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005683 (void) private_key;
5684 (void) peer_key;
5685 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005686 (void) shared_secret;
5687 (void) shared_secret_size;
5688 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005689 return( PSA_ERROR_NOT_SUPPORTED );
5690 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005691}
5692
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005693/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005694 * to potentially free embedded data structures and wipe confidential data.
5695 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005696static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005697 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005698 psa_key_slot_t *private_key,
5699 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005700 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005701{
5702 psa_status_t status;
5703 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5704 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005705 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005706
5707 /* Step 1: run the secret agreement algorithm to generate the shared
5708 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005709 status = psa_key_agreement_raw_internal( ka_alg,
5710 private_key,
5711 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005712 shared_secret,
5713 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005714 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005715 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005716 goto exit;
5717
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005718 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005719 * the shared secret. A shared secret is permitted wherever a key
5720 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005721 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005722 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005723 shared_secret,
5724 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005725
Gilles Peskine12313cd2018-06-20 00:20:32 +02005726exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005727 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005728 return( status );
5729}
5730
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005731psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005732 psa_key_derivation_step_t step,
5733 psa_key_handle_t private_key,
5734 const uint8_t *peer_key,
5735 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005736{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005737 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005738 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005739 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005740 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005741 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005742 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005743 if( status != PSA_SUCCESS )
5744 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005745 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005746 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005747 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005748 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005749 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005750 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005751}
5752
Gilles Peskinebe697d82019-05-16 18:00:41 +02005753psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5754 psa_key_handle_t private_key,
5755 const uint8_t *peer_key,
5756 size_t peer_key_length,
5757 uint8_t *output,
5758 size_t output_size,
5759 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005760{
5761 psa_key_slot_t *slot;
5762 psa_status_t status;
5763
5764 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5765 {
5766 status = PSA_ERROR_INVALID_ARGUMENT;
5767 goto exit;
5768 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005769 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005770 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005771 if( status != PSA_SUCCESS )
5772 goto exit;
5773
5774 status = psa_key_agreement_raw_internal( alg, slot,
5775 peer_key, peer_key_length,
5776 output, output_size,
5777 output_length );
5778
5779exit:
5780 if( status != PSA_SUCCESS )
5781 {
5782 /* If an error happens and is not handled properly, the output
5783 * may be used as a key to protect sensitive data. Arrange for such
5784 * a key to be random, which is likely to result in decryption or
5785 * verification errors. This is better than filling the buffer with
5786 * some constant data such as zeros, which would result in the data
5787 * being protected with a reproducible, easily knowable key.
5788 */
5789 psa_generate_random( output, output_size );
5790 *output_length = output_size;
5791 }
5792 return( status );
5793}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005794
5795
5796/****************************************************************/
5797/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005798/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005799
Gilles Peskine4c317f42018-07-12 01:24:09 +02005800psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005801 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005802{
Janos Follath24eed8d2019-11-22 13:21:35 +00005803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005804 GUARD_MODULE_INITIALIZED;
5805
Gilles Peskinef181eca2019-08-07 13:49:00 +02005806 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5807 {
5808 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5809 output,
5810 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5811 if( ret != 0 )
5812 return( mbedtls_to_psa_error( ret ) );
5813 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5814 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5815 }
5816
Gilles Peskinee59236f2018-01-27 23:32:46 +01005817 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5818 return( mbedtls_to_psa_error( ret ) );
5819}
5820
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005821#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5822#include "mbedtls/entropy_poll.h"
5823
Gilles Peskine7228da22019-07-15 11:06:15 +02005824psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005825 size_t seed_size )
5826{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005827 if( global_data.initialized )
5828 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005829
5830 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5831 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5832 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5833 return( PSA_ERROR_INVALID_ARGUMENT );
5834
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005835 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005836}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005837#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005838
Gilles Peskinee56e8782019-04-26 17:34:02 +02005839#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5840static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5841 size_t domain_parameters_size,
5842 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005843{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005844 size_t i;
5845 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005846
Gilles Peskinee56e8782019-04-26 17:34:02 +02005847 if( domain_parameters_size == 0 )
5848 {
5849 *exponent = 65537;
5850 return( PSA_SUCCESS );
5851 }
5852
5853 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5854 * support values that fit in a 32-bit integer, which is larger than
5855 * int on just about every platform anyway. */
5856 if( domain_parameters_size > sizeof( acc ) )
5857 return( PSA_ERROR_NOT_SUPPORTED );
5858 for( i = 0; i < domain_parameters_size; i++ )
5859 acc = ( acc << 8 ) | domain_parameters[i];
5860 if( acc > INT_MAX )
5861 return( PSA_ERROR_NOT_SUPPORTED );
5862 *exponent = acc;
5863 return( PSA_SUCCESS );
5864}
5865#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5866
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005867static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005868 psa_key_slot_t *slot, size_t bits,
5869 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005870{
Gilles Peskine8e338702019-07-30 20:06:31 +02005871 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005872
Gilles Peskinee56e8782019-04-26 17:34:02 +02005873 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005874 return( PSA_ERROR_INVALID_ARGUMENT );
5875
Gilles Peskinee59236f2018-01-27 23:32:46 +01005876 if( key_type_is_raw_bytes( type ) )
5877 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005878 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005879
5880 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005881 if( status != PSA_SUCCESS )
5882 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005883
5884 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02005885 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
5886 if( status != PSA_SUCCESS )
5887 return status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005888
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005889 status = psa_generate_random( slot->data.key.data,
5890 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005891 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005892 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005893
5894 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005895#if defined(MBEDTLS_DES_C)
5896 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005897 psa_des_set_key_parity( slot->data.key.data,
5898 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005899#endif /* MBEDTLS_DES_C */
5900 }
5901 else
5902
5903#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005904 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005905 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005906 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005908 int exponent;
5909 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005910 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5911 return( PSA_ERROR_NOT_SUPPORTED );
5912 /* Accept only byte-aligned keys, for the same reasons as
5913 * in psa_import_rsa_key(). */
5914 if( bits % 8 != 0 )
5915 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005916 status = psa_read_rsa_exponent( domain_parameters,
5917 domain_parameters_size,
5918 &exponent );
5919 if( status != PSA_SUCCESS )
5920 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005921 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5922 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005923 mbedtls_ctr_drbg_random,
5924 &global_data.ctr_drbg,
5925 (unsigned int) bits,
5926 exponent );
5927 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005928 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005929
5930 /* Make sure to always have an export representation available */
5931 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5932
Steven Cooreman75b74362020-07-28 14:30:13 +02005933 status = psa_allocate_buffer_to_slot( slot, bytes );
5934 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005935 {
5936 mbedtls_rsa_free( &rsa );
Steven Cooreman75b74362020-07-28 14:30:13 +02005937 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005938 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005939
5940 status = psa_export_rsa_key( type,
5941 &rsa,
5942 slot->data.key.data,
5943 bytes,
5944 &slot->data.key.bytes );
5945 mbedtls_rsa_free( &rsa );
5946 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005947 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005948 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005949 }
5950 else
5951#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5952
5953#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005954 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005955 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005956 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005957 mbedtls_ecp_group_id grp_id =
5958 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005959 const mbedtls_ecp_curve_info *curve_info =
5960 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02005961 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005962 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005963 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005964 return( PSA_ERROR_NOT_SUPPORTED );
5965 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5966 return( PSA_ERROR_NOT_SUPPORTED );
5967 if( curve_info->bit_size != bits )
5968 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005969 mbedtls_ecp_keypair_init( &ecp );
5970 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005971 mbedtls_ctr_drbg_random,
5972 &global_data.ctr_drbg );
5973 if( ret != 0 )
5974 {
Steven Cooremanacda8342020-07-24 23:09:52 +02005975 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005976 return( mbedtls_to_psa_error( ret ) );
5977 }
Steven Cooremanacda8342020-07-24 23:09:52 +02005978
5979
5980 /* Make sure to always have an export representation available */
5981 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02005982 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
5983 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005984 {
5985 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman75b74362020-07-28 14:30:13 +02005986 return status;
Steven Cooremanacda8342020-07-24 23:09:52 +02005987 }
Steven Cooreman75b74362020-07-28 14:30:13 +02005988
5989 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02005990 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
5991
5992 mbedtls_ecp_keypair_free( &ecp );
5993 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005994 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005995 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005996 }
5997 else
5998#endif /* MBEDTLS_ECP_C */
5999
6000 return( PSA_ERROR_NOT_SUPPORTED );
6001
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006002 return( PSA_SUCCESS );
6003}
Darryl Green0c6575a2018-11-07 16:05:30 +00006004
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006005psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006006 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006007{
6008 psa_status_t status;
6009 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006010 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006011
Gilles Peskine0f84d622019-09-12 19:03:13 +02006012 /* Reject any attempt to create a zero-length key so that we don't
6013 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6014 if( psa_get_key_bits( attributes ) == 0 )
6015 return( PSA_ERROR_INVALID_ARGUMENT );
6016
Gilles Peskinedf179142019-07-15 22:02:14 +02006017 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6018 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006019 if( status != PSA_SUCCESS )
6020 goto exit;
6021
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006022#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6023 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00006024 {
Gilles Peskine11792082019-08-06 18:36:36 +02006025 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
6026 size_t pubkey_length = 0; /* We don't support this feature yet */
6027 if( drv->key_management == NULL ||
6028 drv->key_management->p_generate == NULL )
6029 {
6030 status = PSA_ERROR_NOT_SUPPORTED;
6031 goto exit;
6032 }
6033 status = drv->key_management->p_generate(
6034 psa_get_se_driver_context( driver ),
6035 slot->data.se.slot_number, attributes,
6036 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006037 }
Gilles Peskine11792082019-08-06 18:36:36 +02006038 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006039#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006040 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006041 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02006042 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006043 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006044 }
Gilles Peskine11792082019-08-06 18:36:36 +02006045
6046exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006047 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006048 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006049 if( status != PSA_SUCCESS )
6050 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006051 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006052 *handle = 0;
6053 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006054 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006055}
6056
6057
Gilles Peskinee59236f2018-01-27 23:32:46 +01006058
6059/****************************************************************/
6060/* Module setup */
6061/****************************************************************/
6062
Gilles Peskine5e769522018-11-20 21:59:56 +01006063psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6064 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6065 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6066{
6067 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6068 return( PSA_ERROR_BAD_STATE );
6069 global_data.entropy_init = entropy_init;
6070 global_data.entropy_free = entropy_free;
6071 return( PSA_SUCCESS );
6072}
6073
Gilles Peskinee59236f2018-01-27 23:32:46 +01006074void mbedtls_psa_crypto_free( void )
6075{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006076 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006077 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6078 {
6079 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006080 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006081 }
6082 /* Wipe all remaining data, including configuration.
6083 * In particular, this sets all state indicator to the value
6084 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006085 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006086#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006087 /* Unregister all secure element drivers, so that we restart from
6088 * a pristine state. */
6089 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006090#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006091}
6092
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006093#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6094/** Recover a transaction that was interrupted by a power failure.
6095 *
6096 * This function is called during initialization, before psa_crypto_init()
6097 * returns. If this function returns a failure status, the initialization
6098 * fails.
6099 */
6100static psa_status_t psa_crypto_recover_transaction(
6101 const psa_crypto_transaction_t *transaction )
6102{
6103 switch( transaction->unknown.type )
6104 {
6105 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6106 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006107 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006108 * is implemented.
6109 * https://github.com/ARMmbed/mbed-crypto/issues/218
6110 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006111 default:
6112 /* We found an unsupported transaction in the storage.
6113 * We don't know what state the storage is in. Give up. */
6114 return( PSA_ERROR_STORAGE_FAILURE );
6115 }
6116}
6117#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6118
Gilles Peskinee59236f2018-01-27 23:32:46 +01006119psa_status_t psa_crypto_init( void )
6120{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006121 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006122 const unsigned char drbg_seed[] = "PSA";
6123
Gilles Peskinec6b69072018-11-20 21:42:52 +01006124 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006125 if( global_data.initialized != 0 )
6126 return( PSA_SUCCESS );
6127
Gilles Peskine5e769522018-11-20 21:59:56 +01006128 /* Set default configuration if
6129 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6130 if( global_data.entropy_init == NULL )
6131 global_data.entropy_init = mbedtls_entropy_init;
6132 if( global_data.entropy_free == NULL )
6133 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006134
Gilles Peskinec6b69072018-11-20 21:42:52 +01006135 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006136 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006137#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6138 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6139 /* The PSA entropy injection feature depends on using NV seed as an entropy
6140 * source. Add NV seed as an entropy source for PSA entropy injection. */
6141 mbedtls_entropy_add_source( &global_data.entropy,
6142 mbedtls_nv_seed_poll, NULL,
6143 MBEDTLS_ENTROPY_BLOCK_SIZE,
6144 MBEDTLS_ENTROPY_SOURCE_STRONG );
6145#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006146 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006147 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006148 status = mbedtls_to_psa_error(
6149 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6150 mbedtls_entropy_func,
6151 &global_data.entropy,
6152 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6153 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006154 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006155 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006156
Gilles Peskine66fb1262018-12-10 16:29:04 +01006157 status = psa_initialize_key_slots( );
6158 if( status != PSA_SUCCESS )
6159 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006160
Gilles Peskined9348f22019-10-01 15:22:29 +02006161#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6162 status = psa_init_all_se_drivers( );
6163 if( status != PSA_SUCCESS )
6164 goto exit;
6165#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6166
Gilles Peskine4b734222019-07-24 15:56:31 +02006167#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006168 status = psa_crypto_load_transaction( );
6169 if( status == PSA_SUCCESS )
6170 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006171 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6172 if( status != PSA_SUCCESS )
6173 goto exit;
6174 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006175 }
6176 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6177 {
6178 /* There's no transaction to complete. It's all good. */
6179 status = PSA_SUCCESS;
6180 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006181#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006182
Gilles Peskinec6b69072018-11-20 21:42:52 +01006183 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006184 global_data.initialized = 1;
6185
6186exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006187 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006188 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006189 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006190}
6191
6192#endif /* MBEDTLS_PSA_CRYPTO_C */