blob: 12f05d13535d133a7a73e3967624658d3c1efc09 [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 ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200570 if( status != PSA_SUCCESS )
571 goto exit;
572
Steven Cooremana2371e52020-07-28 14:30:39 +0200573 /* Copy out the pointer to the RSA context, and reset the PK context
574 * such that pk_free doesn't free the RSA context we just grabbed. */
575 *p_rsa = mbedtls_pk_rsa( ctx );
576 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000577
578exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200579 mbedtls_pk_free( &ctx );
580 return( status );
581#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200582 (void) data;
583 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200584 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200585 (void) rsa;
586 return( PSA_ERROR_NOT_SUPPORTED );
587#endif /* MBEDTLS_PK_PARSE_C */
588}
589
590/** Export an RSA key to export representation
591 *
592 * \param[in] type The type of key (public/private) to export
593 * \param[in] rsa The internal RSA representation from which to export
594 * \param[out] data The buffer to export to
595 * \param[in] data_size The length of the buffer to export to
596 * \param[out] data_length The amount of bytes written to \p data
597 */
598static psa_status_t psa_export_rsa_key( psa_key_type_t type,
599 mbedtls_rsa_context *rsa,
600 uint8_t *data,
601 size_t data_size,
602 size_t *data_length )
603{
604#if defined(MBEDTLS_PK_WRITE_C)
605 int ret;
606 mbedtls_pk_context pk;
607 uint8_t *pos = data + data_size;
608
609 mbedtls_pk_init( &pk );
610 pk.pk_info = &mbedtls_rsa_info;
611 pk.pk_ctx = rsa;
612
613 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200614 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
615 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200616 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
617 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
618 else
619 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
620
621 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200622 {
623 /* Clean up in case pk_write failed halfway through. */
624 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200625 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200626 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200627
628 /* The mbedtls_pk_xxx functions write to the end of the buffer.
629 * Move the data to the beginning and erase remaining data
630 * at the original location. */
631 if( 2 * (size_t) ret <= data_size )
632 {
633 memcpy( data, data + data_size - ret, ret );
634 memset( data + data_size - ret, 0, ret );
635 }
636 else if( (size_t) ret < data_size )
637 {
638 memmove( data, data + data_size - ret, ret );
639 memset( data + ret, 0, data_size - ret );
640 }
641
642 *data_length = ret;
643 return( PSA_SUCCESS );
644#else
645 (void) type;
646 (void) rsa;
647 (void) data;
648 (void) data_size;
649 (void) data_length;
650 return( PSA_ERROR_NOT_SUPPORTED );
651#endif /* MBEDTLS_PK_WRITE_C */
652}
653
654/** Import an RSA key from import representation to a slot
655 *
656 * \param[in,out] slot The slot where to store the export representation to
657 * \param[in] data The buffer containing the import representation
658 * \param[in] data_length The amount of bytes in \p data
659 */
660static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
661 const uint8_t *data,
662 size_t data_length )
663{
664 psa_status_t status;
665 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200666 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200667
Steven Cooremana01795d2020-07-24 22:48:15 +0200668 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200669 status = psa_load_rsa_representation( slot->attr.type,
670 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200671 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200672 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200673 if( status != PSA_SUCCESS )
674 goto exit;
675
676 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200677 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200678
Steven Cooreman75b74362020-07-28 14:30:13 +0200679 /* Re-export the data to PSA export format, such that we can store export
680 * representation in the key slot. Export representation in case of RSA is
681 * the smallest representation that's allowed as input, so a straight-up
682 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200683 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200684 if( output == NULL )
685 {
686 status = PSA_ERROR_INSUFFICIENT_MEMORY;
687 goto exit;
688 }
689
Steven Cooremana01795d2020-07-24 22:48:15 +0200690 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200691 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200692 output,
693 data_length,
694 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200695exit:
696 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200697 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200698 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200699
700 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000701 if( status != PSA_SUCCESS )
702 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200703 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000704 return( status );
705 }
706
Steven Cooremana01795d2020-07-24 22:48:15 +0200707 /* On success, store the allocated export-formatted key. */
708 slot->data.key.data = output;
709 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000710
711 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200712}
Steven Cooremana01795d2020-07-24 22:48:15 +0200713#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200714
Jaeden Ameroccdce902019-01-10 11:42:27 +0000715#if defined(MBEDTLS_ECP_C)
Steven Cooreman7f391872020-07-30 14:57:44 +0200716/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200717 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200718 * \param[in] type The type of key contained in \p data.
719 * \param[in] data The buffer from which to load the representation.
720 * \param[in] data_length The size in bytes of \p data.
721 * \param[out] p_ecp Returns a pointer to an ECP context on success.
722 * The caller is responsible for freeing both the
723 * contents of the context and the context itself
724 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200725 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200726static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
727 const uint8_t *data,
728 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200729 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100730{
731 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200732 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200733 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200734 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100735
Steven Cooreman4fed4552020-08-03 14:46:03 +0200736 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
737 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100738 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200739 /* A Weierstrass public key is represented as:
740 * - The byte 0x04;
741 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
742 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
743 * So its data length is 2m+1 where n is the key size in bits.
744 */
745 if( ( data_length & 1 ) == 0 )
746 return( PSA_ERROR_INVALID_ARGUMENT );
747 curve_size = data_length / 2;
748
749 /* Montgomery public keys are represented in compressed format, meaning
750 * their curve_size is equal to the amount of input. */
751
752 /* Private keys are represented in uncompressed private random integer
753 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100754 }
755
Steven Cooreman6d839f02020-07-30 11:36:45 +0200756 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200757 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200758 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200759 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200760 mbedtls_ecp_keypair_init( ecp );
761
Gilles Peskine4cd32772019-12-02 20:49:42 +0100762 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200763 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
764 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100765 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200766 {
767 status = PSA_ERROR_INVALID_ARGUMENT;
768 goto exit;
769 }
770
Steven Cooremanacda8342020-07-24 23:09:52 +0200771 status = mbedtls_to_psa_error(
772 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
773 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200774 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200775
Steven Cooreman6d839f02020-07-30 11:36:45 +0200776 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200777 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200778 {
779 /* Load the public value. */
780 status = mbedtls_to_psa_error(
781 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200782 data,
783 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200784 if( status != PSA_SUCCESS )
785 goto exit;
786
787 /* Check that the point is on the curve. */
788 status = mbedtls_to_psa_error(
789 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
790 if( status != PSA_SUCCESS )
791 goto exit;
792 }
793 else
794 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200795 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200796 status = mbedtls_to_psa_error(
797 mbedtls_ecp_read_key( ecp->grp.id,
798 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200799 data,
800 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200801 if( status != PSA_SUCCESS )
802 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200803 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200804
805 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200806exit:
807 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200808 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200809 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200810 mbedtls_free( ecp );
811 }
812
Steven Cooreman29149862020-08-05 15:43:42 +0200813 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100814}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000815
Steven Cooreman4fed4552020-08-03 14:46:03 +0200816/** Export an ECP key to export representation
817 *
818 * \param[in] type The type of key (public/private) to export
819 * \param[in] ecp The internal ECP representation from which to export
820 * \param[out] data The buffer to export to
821 * \param[in] data_size The length of the buffer to export to
822 * \param[out] data_length The amount of bytes written to \p data
823 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200824static psa_status_t psa_export_ecp_key( psa_key_type_t type,
825 mbedtls_ecp_keypair *ecp,
826 uint8_t *data,
827 size_t data_size,
828 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200829{
Steven Cooremanacda8342020-07-24 23:09:52 +0200830 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000831
Steven Cooremanacda8342020-07-24 23:09:52 +0200832 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200833 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200834 /* Check whether the public part is loaded */
835 if( mbedtls_ecp_is_zero( &ecp->Q ) )
836 {
837 /* Calculate the public key */
838 status = mbedtls_to_psa_error(
839 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
840 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
841 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200842 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200843 }
844
Steven Cooreman4fed4552020-08-03 14:46:03 +0200845 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200846 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
847 MBEDTLS_ECP_PF_UNCOMPRESSED,
848 data_length,
849 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200850 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200851 if( status != PSA_SUCCESS )
852 memset( data, 0, data_size );
853
Steven Cooreman29149862020-08-05 15:43:42 +0200854 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200855 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200856 else
857 {
Steven Cooreman29149862020-08-05 15:43:42 +0200858 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200859 return( PSA_ERROR_BUFFER_TOO_SMALL );
860
861 status = mbedtls_to_psa_error(
862 mbedtls_ecp_write_key( ecp,
863 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200864 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200865 if( status == PSA_SUCCESS )
866 {
Steven Cooreman29149862020-08-05 15:43:42 +0200867 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanacda8342020-07-24 23:09:52 +0200868 }
869
870 return( status );
871 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200872}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200873
Steven Cooreman4fed4552020-08-03 14:46:03 +0200874/** Import an ECP key from import representation to a slot
875 *
876 * \param[in,out] slot The slot where to store the export representation to
877 * \param[in] data The buffer containing the import representation
878 * \param[in] data_length The amount of bytes in \p data
879 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200880static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
881 const uint8_t *data,
882 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100883{
Steven Cooremanacda8342020-07-24 23:09:52 +0200884 psa_status_t status;
885 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200886 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100887
Steven Cooremanacda8342020-07-24 23:09:52 +0200888 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200889 status = psa_load_ecp_representation( slot->attr.type,
890 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200891 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200892 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100893 if( status != PSA_SUCCESS )
894 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100895
Steven Cooremanacda8342020-07-24 23:09:52 +0200896 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200897 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200898 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200899 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200900
Steven Cooremanacda8342020-07-24 23:09:52 +0200901 /* Re-export the data to PSA export format. There is currently no support
902 * for other input formats then the export format, so this is a 1-1
903 * copy operation. */
904 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200905 if( output == NULL )
906 {
907 status = PSA_ERROR_INSUFFICIENT_MEMORY;
908 goto exit;
909 }
910
911 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200912 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200913 output,
914 data_length,
915 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100916exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200917 /* Always free the PK object (will also free contained ECP context) */
918 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200919 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200920
921 /* Free the allocated buffer only on error. */
922 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100923 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200924 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200925 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100926 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200927
928 /* On success, store the allocated export-formatted key. */
929 slot->data.key.data = output;
930 slot->data.key.bytes = data_length;
931
932 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100933}
934#endif /* defined(MBEDTLS_ECP_C) */
935
Gilles Peskineb46bef22019-07-30 21:32:04 +0200936/** Return the size of the key in the given slot, in bits.
937 *
938 * \param[in] slot A key slot.
939 *
940 * \return The key size in bits, read from the metadata in the slot.
941 */
942static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
943{
944 return( slot->attr.bits );
945}
946
Steven Cooreman75b74362020-07-28 14:30:13 +0200947/** Try to allocate a buffer to an empty key slot.
948 *
949 * \param[in,out] slot Key slot to attach buffer to.
950 * \param[in] buffer_length Requested size of the buffer.
951 *
952 * \retval #PSA_SUCCESS
953 * The buffer has been successfully allocated.
954 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
955 * Not enough memory was available for allocation.
956 * \retval #PSA_ERROR_ALREADY_EXISTS
957 * Trying to allocate a buffer to a non-empty key slot.
958 */
959static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
960 size_t buffer_length )
961{
962 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200963 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200964
965 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
966 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200967 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200968
969 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200970 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200971}
972
Gilles Peskine8e338702019-07-30 20:06:31 +0200973/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100974 * previously. This function assumes that the slot does not contain
975 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100976psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
977 const uint8_t *data,
978 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100979{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200980 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200982 /* zero-length keys are never supported. */
983 if( data_length == 0 )
984 return( PSA_ERROR_NOT_SUPPORTED );
985
Gilles Peskine8e338702019-07-30 20:06:31 +0200986 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100987 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200988 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200989
Steven Cooreman75b74362020-07-28 14:30:13 +0200990 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
991 if( data_length > SIZE_MAX / 8 )
992 return( PSA_ERROR_NOT_SUPPORTED );
993
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200994 /* Enforce a size limit, and in particular ensure that the bit
995 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200996 if( bit_size > PSA_MAX_KEY_BITS )
997 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200998
999 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001000 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001001 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001002
1003 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02001004 status = psa_allocate_buffer_to_slot( slot, data_length );
1005 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001006 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001007
1008 /* copy key into allocated buffer */
Steven Cooreman29149862020-08-05 15:43:42 +02001009 memcpy( slot->data.key.data, data, data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001010
1011 /* Write the actual key size to the slot.
1012 * psa_start_key_creation() wrote the size declared by the
1013 * caller, which may be 0 (meaning unspecified) or wrong. */
1014 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001016 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1017 {
Gilles Peskinef76aa772018-10-29 19:24:33 +01001018#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +02001019 status = psa_import_ecp_key( slot,
1020 data, data_length );
1021#else
1022 /* No drivers have been implemented yet, so without mbed TLS backing
1023 * there's no way to do ECP with the current library. */
1024 return( PSA_ERROR_NOT_SUPPORTED );
1025#endif /* defined(MBEDTLS_ECP_C) */
1026 }
1027 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +01001028 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001029#if defined(MBEDTLS_RSA_C)
1030 status = psa_import_rsa_key( slot,
1031 data, data_length );
1032#else
1033 /* No drivers have been implemented yet, so without mbed TLS backing
1034 * there's no way to do RSA with the current library. */
1035 status = PSA_ERROR_NOT_SUPPORTED;
1036#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001037 }
1038 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001039 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001040 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001041 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001042 }
Darryl Green940d72c2018-07-13 13:18:51 +01001043
Darryl Green06fd18d2018-07-16 11:21:11 +01001044 return( status );
1045}
1046
Gilles Peskinef603c712019-01-19 13:40:11 +01001047/** Calculate the intersection of two algorithm usage policies.
1048 *
1049 * Return 0 (which allows no operation) on incompatibility.
1050 */
1051static psa_algorithm_t psa_key_policy_algorithm_intersection(
1052 psa_algorithm_t alg1,
1053 psa_algorithm_t alg2 )
1054{
Gilles Peskine549ea862019-05-22 11:45:59 +02001055 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001056 if( alg1 == alg2 )
1057 return( alg1 );
1058 /* If the policies are from the same hash-and-sign family, check
1059 * if one is a wildcard. If so the other has the specific algorithm. */
1060 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1061 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1062 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1063 {
1064 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1065 return( alg2 );
1066 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1067 return( alg1 );
1068 }
1069 /* If the policies are incompatible, allow nothing. */
1070 return( 0 );
1071}
1072
Gilles Peskined6f371b2019-05-10 19:33:38 +02001073static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1074 psa_algorithm_t requested_alg )
1075{
Gilles Peskine549ea862019-05-22 11:45:59 +02001076 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001077 if( requested_alg == policy_alg )
1078 return( 1 );
1079 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001080 * and requested_alg is the same hash-and-sign family with any hash,
1081 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001082 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1083 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1084 {
1085 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1086 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1087 }
1088 /* If it isn't permitted, it's forbidden. */
1089 return( 0 );
1090}
1091
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001092/** Test whether a policy permits an algorithm.
1093 *
1094 * The caller must test usage flags separately.
1095 */
1096static int psa_key_policy_permits( const psa_key_policy_t *policy,
1097 psa_algorithm_t alg )
1098{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001099 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1100 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001101}
1102
Gilles Peskinef603c712019-01-19 13:40:11 +01001103/** Restrict a key policy based on a constraint.
1104 *
1105 * \param[in,out] policy The policy to restrict.
1106 * \param[in] constraint The policy constraint to apply.
1107 *
1108 * \retval #PSA_SUCCESS
1109 * \c *policy contains the intersection of the original value of
1110 * \c *policy and \c *constraint.
1111 * \retval #PSA_ERROR_INVALID_ARGUMENT
1112 * \c *policy and \c *constraint are incompatible.
1113 * \c *policy is unchanged.
1114 */
1115static psa_status_t psa_restrict_key_policy(
1116 psa_key_policy_t *policy,
1117 const psa_key_policy_t *constraint )
1118{
1119 psa_algorithm_t intersection_alg =
1120 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001121 psa_algorithm_t intersection_alg2 =
1122 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001123 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1124 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001125 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1126 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001127 policy->usage &= constraint->usage;
1128 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001129 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001130 return( PSA_SUCCESS );
1131}
1132
Darryl Green06fd18d2018-07-16 11:21:11 +01001133/** Retrieve a slot which must contain a key. The key must have allow all the
1134 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1135 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001136static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001137 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001138 psa_key_usage_t usage,
1139 psa_algorithm_t alg )
1140{
1141 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001142 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001143
1144 *p_slot = NULL;
1145
Gilles Peskinec5487a82018-12-03 18:08:14 +01001146 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001147 if( status != PSA_SUCCESS )
1148 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001149
1150 /* Enforce that usage policy for the key slot contains all the flags
1151 * required by the usage parameter. There is one exception: public
1152 * keys can always be exported, so we treat public key objects as
1153 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001154 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001155 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001156 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001157 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001158
1159 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001160 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001161 return( PSA_ERROR_NOT_PERMITTED );
1162
1163 *p_slot = slot;
1164 return( PSA_SUCCESS );
1165}
Darryl Green940d72c2018-07-13 13:18:51 +01001166
Gilles Peskine28f8f302019-07-24 13:30:31 +02001167/** Retrieve a slot which must contain a transparent key.
1168 *
1169 * A transparent key is a key for which the key material is directly
1170 * available, as opposed to a key in a secure element.
1171 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001172 * This is a temporary function to use instead of psa_get_key_from_slot()
1173 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001174 */
1175#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1176static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1177 psa_key_slot_t **p_slot,
1178 psa_key_usage_t usage,
1179 psa_algorithm_t alg )
1180{
1181 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1182 if( status != PSA_SUCCESS )
1183 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001184 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001185 {
1186 *p_slot = NULL;
1187 return( PSA_ERROR_NOT_SUPPORTED );
1188 }
1189 return( PSA_SUCCESS );
1190}
1191#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1192/* With no secure element support, all keys are transparent. */
1193#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1194 psa_get_key_from_slot( handle, p_slot, usage, alg )
1195#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1196
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001197/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001198static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001199{
Gilles Peskine73167e12019-07-12 23:44:37 +02001200#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1201 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001202 {
1203 /* No key material to clean. */
1204 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001205 else
1206#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001207 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001208 {
1209 /* No key material to clean. */
1210 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001211 else if( key_type_is_raw_bytes( slot->attr.type ) ||
1212 PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1213 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001214 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001215 mbedtls_free( slot->data.key.data );
1216 slot->data.key.data = NULL;
1217 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001218 }
1219 else
Darryl Green40225ba2018-11-15 14:48:15 +00001220 {
1221 /* Shouldn't happen: the key type is not any type that we
1222 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001223 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001224 }
1225
1226 return( PSA_SUCCESS );
1227}
1228
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001229/** Completely wipe a slot in memory, including its policy.
1230 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001231psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001232{
1233 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001234 /* Multipart operations may still be using the key. This is safe
1235 * because all multipart operation objects are independent from
1236 * the key slot: if they need to access the key after the setup
1237 * phase, they have a copy of the key. Note that this means that
1238 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001239 /* At this point, key material and other type-specific content has
1240 * been wiped. Clear remaining metadata. We can call memset and not
1241 * zeroize because the metadata is not particularly sensitive. */
1242 memset( slot, 0, sizeof( *slot ) );
1243 return( status );
1244}
1245
Gilles Peskinec5487a82018-12-03 18:08:14 +01001246psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001247{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001248 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001249 psa_status_t status; /* status of the last operation */
1250 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001251#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1252 psa_se_drv_table_entry_t *driver;
1253#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254
Gilles Peskine1841cf42019-10-08 15:48:25 +02001255 if( handle == 0 )
1256 return( PSA_SUCCESS );
1257
Gilles Peskinec5487a82018-12-03 18:08:14 +01001258 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001259 if( status != PSA_SUCCESS )
1260 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001261
1262#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001263 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001264 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001265 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001266 /* For a key in a secure element, we need to do three things:
1267 * remove the key file in internal storage, destroy the
1268 * key inside the secure element, and update the driver's
1269 * persistent data. Start a transaction that will encompass these
1270 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001271 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001272 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001273 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001274 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001275 status = psa_crypto_save_transaction( );
1276 if( status != PSA_SUCCESS )
1277 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001278 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001279 /* We should still try to destroy the key in the secure
1280 * element and the key metadata in storage. This is especially
1281 * important if the error is that the storage is full.
1282 * But how to do it exactly without risking an inconsistent
1283 * state after a reset?
1284 * https://github.com/ARMmbed/mbed-crypto/issues/215
1285 */
1286 overall_status = status;
1287 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001288 }
1289
Gilles Peskine354f7672019-07-12 23:46:38 +02001290 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001291 if( overall_status == PSA_SUCCESS )
1292 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001293 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001294#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1295
Darryl Greend49a4992018-06-18 17:27:26 +01001296#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001297 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001298 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001299 status = psa_destroy_persistent_key( slot->attr.id );
1300 if( overall_status == PSA_SUCCESS )
1301 overall_status = status;
1302
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001303 /* TODO: other slots may have a copy of the same key. We should
1304 * invalidate them.
1305 * https://github.com/ARMmbed/mbed-crypto/issues/214
1306 */
Darryl Greend49a4992018-06-18 17:27:26 +01001307 }
1308#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001309
Gilles Peskinefc762652019-07-22 19:30:34 +02001310#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1311 if( driver != NULL )
1312 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001313 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001314 if( overall_status == PSA_SUCCESS )
1315 overall_status = status;
1316 status = psa_crypto_stop_transaction( );
1317 if( overall_status == PSA_SUCCESS )
1318 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001319 }
1320#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1321
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001322#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1323exit:
1324#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001325 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001326 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1327 if( overall_status == PSA_SUCCESS )
1328 overall_status = status;
1329 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001330}
1331
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001332void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001333{
Gilles Peskineb699f072019-04-26 16:06:02 +02001334 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001335 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001336}
1337
Gilles Peskineb699f072019-04-26 16:06:02 +02001338psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1339 psa_key_type_t type,
1340 const uint8_t *data,
1341 size_t data_length )
1342{
1343 uint8_t *copy = NULL;
1344
1345 if( data_length != 0 )
1346 {
1347 copy = mbedtls_calloc( 1, data_length );
1348 if( copy == NULL )
1349 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1350 memcpy( copy, data, data_length );
1351 }
1352 /* After this point, this function is guaranteed to succeed, so it
1353 * can start modifying `*attributes`. */
1354
1355 if( attributes->domain_parameters != NULL )
1356 {
1357 mbedtls_free( attributes->domain_parameters );
1358 attributes->domain_parameters = NULL;
1359 attributes->domain_parameters_size = 0;
1360 }
1361
1362 attributes->domain_parameters = copy;
1363 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001364 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001365 return( PSA_SUCCESS );
1366}
1367
1368psa_status_t psa_get_key_domain_parameters(
1369 const psa_key_attributes_t *attributes,
1370 uint8_t *data, size_t data_size, size_t *data_length )
1371{
1372 if( attributes->domain_parameters_size > data_size )
1373 return( PSA_ERROR_BUFFER_TOO_SMALL );
1374 *data_length = attributes->domain_parameters_size;
1375 if( attributes->domain_parameters_size != 0 )
1376 memcpy( data, attributes->domain_parameters,
1377 attributes->domain_parameters_size );
1378 return( PSA_SUCCESS );
1379}
1380
1381#if defined(MBEDTLS_RSA_C)
1382static psa_status_t psa_get_rsa_public_exponent(
1383 const mbedtls_rsa_context *rsa,
1384 psa_key_attributes_t *attributes )
1385{
1386 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001388 uint8_t *buffer = NULL;
1389 size_t buflen;
1390 mbedtls_mpi_init( &mpi );
1391
1392 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1393 if( ret != 0 )
1394 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001395 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1396 {
1397 /* It's the default value, which is reported as an empty string,
1398 * so there's nothing to do. */
1399 goto exit;
1400 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001401
1402 buflen = mbedtls_mpi_size( &mpi );
1403 buffer = mbedtls_calloc( 1, buflen );
1404 if( buffer == NULL )
1405 {
1406 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1407 goto exit;
1408 }
1409 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1410 if( ret != 0 )
1411 goto exit;
1412 attributes->domain_parameters = buffer;
1413 attributes->domain_parameters_size = buflen;
1414
1415exit:
1416 mbedtls_mpi_free( &mpi );
1417 if( ret != 0 )
1418 mbedtls_free( buffer );
1419 return( mbedtls_to_psa_error( ret ) );
1420}
1421#endif /* MBEDTLS_RSA_C */
1422
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001423/** Retrieve all the publicly-accessible attributes of a key.
1424 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001425psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1426 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001427{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001428 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001429 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001430
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001431 psa_reset_key_attributes( attributes );
1432
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001433 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001434 if( status != PSA_SUCCESS )
1435 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001436
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001437 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001438 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1439 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1440
1441#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1442 if( psa_key_slot_is_external( slot ) )
1443 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1444#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001445
Gilles Peskine8e338702019-07-30 20:06:31 +02001446 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001447 {
1448#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001449 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001450 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001451#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001452 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001453 * is not yet implemented.
1454 * https://github.com/ARMmbed/mbed-crypto/issues/216
1455 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001456 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001457 break;
1458#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001459 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001460 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001461
Steven Cooreman4fed4552020-08-03 14:46:03 +02001462 status = psa_load_rsa_representation( slot->attr.type,
1463 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001464 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001465 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001466 if( status != PSA_SUCCESS )
1467 break;
1468
Steven Cooremana2371e52020-07-28 14:30:39 +02001469 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001470 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001471 mbedtls_rsa_free( rsa );
1472 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001473 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001474 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001475#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001476 default:
1477 /* Nothing else to do. */
1478 break;
1479 }
1480
1481 if( status != PSA_SUCCESS )
1482 psa_reset_key_attributes( attributes );
1483 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001484}
1485
Gilles Peskinec8000c02019-08-02 20:15:51 +02001486#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1487psa_status_t psa_get_key_slot_number(
1488 const psa_key_attributes_t *attributes,
1489 psa_key_slot_number_t *slot_number )
1490{
1491 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1492 {
1493 *slot_number = attributes->slot_number;
1494 return( PSA_SUCCESS );
1495 }
1496 else
1497 return( PSA_ERROR_INVALID_ARGUMENT );
1498}
1499#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1500
Steven Cooremana01795d2020-07-24 22:48:15 +02001501static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1502 uint8_t *data,
1503 size_t data_size,
1504 size_t *data_length )
1505{
1506 if( slot->data.key.bytes > data_size )
1507 return( PSA_ERROR_BUFFER_TOO_SMALL );
1508 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1509 memset( data + slot->data.key.bytes, 0,
1510 data_size - slot->data.key.bytes );
1511 *data_length = slot->data.key.bytes;
1512 return( PSA_SUCCESS );
1513}
1514
Gilles Peskinef603c712019-01-19 13:40:11 +01001515static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1516 uint8_t *data,
1517 size_t data_size,
1518 size_t *data_length,
1519 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001520{
Gilles Peskine5d309672019-07-12 23:47:28 +02001521#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1522 const psa_drv_se_t *drv;
1523 psa_drv_se_context_t *drv_context;
1524#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1525
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001526 *data_length = 0;
1527
Gilles Peskine8e338702019-07-30 20:06:31 +02001528 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001529 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001530
Gilles Peskinef9168942019-09-12 19:20:29 +02001531 /* Reject a zero-length output buffer now, since this can never be a
1532 * valid key representation. This way we know that data must be a valid
1533 * pointer and we can do things like memset(data, ..., data_size). */
1534 if( data_size == 0 )
1535 return( PSA_ERROR_BUFFER_TOO_SMALL );
1536
Gilles Peskine5d309672019-07-12 23:47:28 +02001537#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001538 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001539 {
1540 psa_drv_se_export_key_t method;
1541 if( drv->key_management == NULL )
1542 return( PSA_ERROR_NOT_SUPPORTED );
1543 method = ( export_public_key ?
1544 drv->key_management->p_export_public :
1545 drv->key_management->p_export );
1546 if( method == NULL )
1547 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001548 return( method( drv_context,
1549 slot->data.se.slot_number,
1550 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001551 }
1552#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1553
Gilles Peskine8e338702019-07-30 20:06:31 +02001554 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001555 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001556 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001557 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001558 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1559 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001560 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001561 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001562 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001563 /* Exporting public -> public */
1564 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1565 }
1566 else if( !export_public_key )
1567 {
1568 /* Exporting private -> private */
1569 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1570 }
1571 /* Need to export the public part of a private key,
1572 * so conversion is needed */
1573 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1574 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001575#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001576 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001577 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001578 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001579 slot->data.key.data,
1580 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001581 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001582 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001583 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001584
Steven Cooreman560c28a2020-07-24 23:20:24 +02001585 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001586 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001587 data,
1588 data_size,
1589 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001590
Steven Cooremana2371e52020-07-28 14:30:39 +02001591 mbedtls_rsa_free( rsa );
1592 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001593
Steven Cooreman560c28a2020-07-24 23:20:24 +02001594 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001595#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001596 /* We don't know how to convert a private RSA key to public. */
1597 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001598#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001599 }
1600 else
Moran Pekera998bc62018-04-16 18:16:20 +03001601 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001602#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001603 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001604 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001605 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001606 slot->data.key.data,
1607 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001608 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001609 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001610 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001611
1612 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1613 PSA_KEY_TYPE_ECC_GET_FAMILY(
1614 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001615 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001616 data,
1617 data_size,
1618 data_length );
1619
Steven Cooremana2371e52020-07-28 14:30:39 +02001620 mbedtls_ecp_keypair_free( ecp );
1621 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001622 return( status );
1623#else
1624 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001625 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001626#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001627 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001628 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001629 else
1630 {
1631 /* This shouldn't happen in the reference implementation, but
1632 it is valid for a special-purpose implementation to omit
1633 support for exporting certain key types. */
1634 return( PSA_ERROR_NOT_SUPPORTED );
1635 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001636}
1637
Gilles Peskinec5487a82018-12-03 18:08:14 +01001638psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001639 uint8_t *data,
1640 size_t data_size,
1641 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001642{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001643 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001644 psa_status_t status;
1645
1646 /* Set the key to empty now, so that even when there are errors, we always
1647 * set data_length to a value between 0 and data_size. On error, setting
1648 * the key to empty is a good choice because an empty key representation is
1649 * unlikely to be accepted anywhere. */
1650 *data_length = 0;
1651
1652 /* Export requires the EXPORT flag. There is an exception for public keys,
1653 * which don't require any flag, but psa_get_key_from_slot takes
1654 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001655 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001656 if( status != PSA_SUCCESS )
1657 return( status );
1658 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001659 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001660}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001661
Gilles Peskinec5487a82018-12-03 18:08:14 +01001662psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001663 uint8_t *data,
1664 size_t data_size,
1665 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001666{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001667 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001668 psa_status_t status;
1669
1670 /* Set the key to empty now, so that even when there are errors, we always
1671 * set data_length to a value between 0 and data_size. On error, setting
1672 * the key to empty is a good choice because an empty key representation is
1673 * unlikely to be accepted anywhere. */
1674 *data_length = 0;
1675
1676 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001677 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001678 if( status != PSA_SUCCESS )
1679 return( status );
1680 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001681 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001682}
1683
Gilles Peskine91e8c332019-08-02 19:19:39 +02001684#if defined(static_assert)
1685static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1686 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001687static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001688 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001689static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001690 "One or more key attribute flag is listed as both internal-only and external-only" );
1691#endif
1692
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001693/** Validate that a key policy is internally well-formed.
1694 *
1695 * This function only rejects invalid policies. It does not validate the
1696 * consistency of the policy with respect to other attributes of the key
1697 * such as the key type.
1698 */
1699static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001700{
1701 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001702 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001703 PSA_KEY_USAGE_ENCRYPT |
1704 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001705 PSA_KEY_USAGE_SIGN_HASH |
1706 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001707 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1708 return( PSA_ERROR_INVALID_ARGUMENT );
1709
Gilles Peskine4747d192019-04-17 15:05:45 +02001710 return( PSA_SUCCESS );
1711}
1712
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001713/** Validate the internal consistency of key attributes.
1714 *
1715 * This function only rejects invalid attribute values. If does not
1716 * validate the consistency of the attributes with any key data that may
1717 * be involved in the creation of the key.
1718 *
1719 * Call this function early in the key creation process.
1720 *
1721 * \param[in] attributes Key attributes for the new key.
1722 * \param[out] p_drv On any return, the driver for the key, if any.
1723 * NULL for a transparent key.
1724 *
1725 */
1726static psa_status_t psa_validate_key_attributes(
1727 const psa_key_attributes_t *attributes,
1728 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001729{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001730 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001731
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001732 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1733 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001734 if( status != PSA_SUCCESS )
1735 return( status );
1736
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001737 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1738 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001739 if( status != PSA_SUCCESS )
1740 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001741
1742 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001743 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001744 return( status );
1745
1746 /* Refuse to create overly large keys.
1747 * Note that this doesn't trigger on import if the attributes don't
1748 * explicitly specify a size (so psa_get_key_bits returns 0), so
1749 * psa_import_key() needs its own checks. */
1750 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1751 return( PSA_ERROR_NOT_SUPPORTED );
1752
Gilles Peskine91e8c332019-08-02 19:19:39 +02001753 /* Reject invalid flags. These should not be reachable through the API. */
1754 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1755 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1756 return( PSA_ERROR_INVALID_ARGUMENT );
1757
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001758 return( PSA_SUCCESS );
1759}
1760
Gilles Peskine4747d192019-04-17 15:05:45 +02001761/** Prepare a key slot to receive key material.
1762 *
1763 * This function allocates a key slot and sets its metadata.
1764 *
1765 * If this function fails, call psa_fail_key_creation().
1766 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001767 * This function is intended to be used as follows:
1768 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1769 * it with the specified attributes, and assign it a handle.
1770 * -# Populate the slot with the key material.
1771 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1772 * In case of failure at any step, stop the sequence and call
1773 * psa_fail_key_creation().
1774 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001775 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001776 * \param[in] attributes Key attributes for the new key.
1777 * \param[out] handle On success, a handle for the allocated slot.
1778 * \param[out] p_slot On success, a pointer to the prepared slot.
1779 * \param[out] p_drv On any return, the driver for the key, if any.
1780 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001781 *
1782 * \retval #PSA_SUCCESS
1783 * The key slot is ready to receive key material.
1784 * \return If this function fails, the key slot is an invalid state.
1785 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001786 */
1787static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001788 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001789 const psa_key_attributes_t *attributes,
1790 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001791 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001792 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001793{
1794 psa_status_t status;
1795 psa_key_slot_t *slot;
1796
Gilles Peskinedf179142019-07-15 22:02:14 +02001797 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001798 *p_drv = NULL;
1799
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001800 status = psa_validate_key_attributes( attributes, p_drv );
1801 if( status != PSA_SUCCESS )
1802 return( status );
1803
Gilles Peskineedbed562019-08-07 18:19:59 +02001804 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001805 if( status != PSA_SUCCESS )
1806 return( status );
1807 slot = *p_slot;
1808
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001809 /* We're storing the declared bit-size of the key. It's up to each
1810 * creation mechanism to verify that this information is correct.
1811 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001812 * an input (generate, device) but not for those where the bit-size
1813 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001814
1815 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001816
Gilles Peskine91e8c332019-08-02 19:19:39 +02001817 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001818 * external-only flags, query `attributes`. Thanks to the check
1819 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001820 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001821 * may have set. */
1822 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001823
Gilles Peskinecbaff462019-07-12 23:46:04 +02001824#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001825 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001826 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001827 * create the key file in internal storage, create the
1828 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001829 * persistent data. This is done by starting a transaction that will
1830 * encompass these three actions.
1831 * For registering a volatile key, we just need to find an appropriate
1832 * slot number inside the SE. Since the key is designated volatile, creating
1833 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001834 /* The first thing to do is to find a slot number for the new key.
1835 * We save the slot number in persistent storage as part of the
1836 * transaction data. It will be needed to recover if the power
1837 * fails during the key creation process, to clean up on the secure
1838 * element side after restarting. Obtaining a slot number from the
1839 * secure element driver updates its persistent state, but we do not yet
1840 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001841 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001842 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001843 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001844 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001845 &slot->data.se.slot_number );
1846 if( status != PSA_SUCCESS )
1847 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001848
1849 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001850 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001851 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1852 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1853 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1854 psa_crypto_transaction.key.id = slot->attr.id;
1855 status = psa_crypto_save_transaction( );
1856 if( status != PSA_SUCCESS )
1857 {
1858 (void) psa_crypto_stop_transaction( );
1859 return( status );
1860 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001861 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001862 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001863
1864 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1865 {
1866 /* Key registration only makes sense with a secure element. */
1867 return( PSA_ERROR_INVALID_ARGUMENT );
1868 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001869#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1870
Darryl Green0c6575a2018-11-07 16:05:30 +00001871 return( status );
1872}
Gilles Peskine4747d192019-04-17 15:05:45 +02001873
1874/** Finalize the creation of a key once its key material has been set.
1875 *
1876 * This entails writing the key to persistent storage.
1877 *
1878 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001879 * See the documentation of psa_start_key_creation() for the intended use
1880 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001881 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001882 * \param[in,out] slot Pointer to the slot with key material.
1883 * \param[in] driver The secure element driver for the key,
1884 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001885 *
1886 * \retval #PSA_SUCCESS
1887 * The key was successfully created. The handle is now valid.
1888 * \return If this function fails, the key slot is an invalid state.
1889 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001890 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001891static psa_status_t psa_finish_key_creation(
1892 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001893 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001894{
1895 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001896 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001897 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001898
1899#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001900 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001901 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001902#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1903 if( driver != NULL )
1904 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001905 psa_se_key_data_storage_t data;
1906#if defined(static_assert)
1907 static_assert( sizeof( slot->data.se.slot_number ) ==
1908 sizeof( data.slot_number ),
1909 "Slot number size does not match psa_se_key_data_storage_t" );
1910 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1911 "Bit-size size does not match psa_se_key_data_storage_t" );
1912#endif
1913 memcpy( &data.slot_number, &slot->data.se.slot_number,
1914 sizeof( slot->data.se.slot_number ) );
1915 memcpy( &data.bits, &slot->attr.bits,
1916 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001917 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001918 (uint8_t*) &data,
1919 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001920 }
1921 else
1922#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1923 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001924 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001925 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001926 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001927 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1928 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001929 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001930 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1931 status = psa_internal_export_key( slot,
1932 buffer, buffer_size, &length,
1933 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001934 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001935 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001936 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001937
Gilles Peskinef9168942019-09-12 19:20:29 +02001938 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001939 mbedtls_free( buffer );
1940 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001941 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001942#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1943
Gilles Peskinecbaff462019-07-12 23:46:04 +02001944#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001945 /* Finish the transaction for a key creation. This does not
1946 * happen when registering an existing key. Detect this case
1947 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001948 * creation of a persistent key in a secure element requires a transaction,
1949 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001950 if( driver != NULL &&
1951 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001952 {
1953 status = psa_save_se_persistent_data( driver );
1954 if( status != PSA_SUCCESS )
1955 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001956 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001957 return( status );
1958 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001959 status = psa_crypto_stop_transaction( );
1960 if( status != PSA_SUCCESS )
1961 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001962 }
1963#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1964
Gilles Peskine4747d192019-04-17 15:05:45 +02001965 return( status );
1966}
1967
1968/** Abort the creation of a key.
1969 *
1970 * You may call this function after calling psa_start_key_creation(),
1971 * or after psa_finish_key_creation() fails. In other circumstances, this
1972 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001973 * See the documentation of psa_start_key_creation() for the intended use
1974 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001975 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001976 * \param[in,out] slot Pointer to the slot with key material.
1977 * \param[in] driver The secure element driver for the key,
1978 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001979 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001980static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001981 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001982{
Gilles Peskine011e4282019-06-26 18:34:38 +02001983 (void) driver;
1984
Gilles Peskine4747d192019-04-17 15:05:45 +02001985 if( slot == NULL )
1986 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001987
1988#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001989 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001990 * element, and the failure happened later (when saving metadata
1991 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001992 * element.
1993 * https://github.com/ARMmbed/mbed-crypto/issues/217
1994 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001995
Gilles Peskined7729582019-08-05 15:55:54 +02001996 /* Abort the ongoing transaction if any (there may not be one if
1997 * the creation process failed before starting one, or if the
1998 * key creation is a registration of a key in a secure element).
1999 * Earlier functions must already have done what it takes to undo any
2000 * partial creation. All that's left is to update the transaction data
2001 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002002 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002003#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2004
Gilles Peskine4747d192019-04-17 15:05:45 +02002005 psa_wipe_key_slot( slot );
2006}
2007
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002008/** Validate optional attributes during key creation.
2009 *
2010 * Some key attributes are optional during key creation. If they are
2011 * specified in the attributes structure, check that they are consistent
2012 * with the data in the slot.
2013 *
2014 * This function should be called near the end of key creation, after
2015 * the slot in memory is fully populated but before saving persistent data.
2016 */
2017static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018 const psa_key_slot_t *slot,
2019 const psa_key_attributes_t *attributes )
2020{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002021 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002022 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002023 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002024 return( PSA_ERROR_INVALID_ARGUMENT );
2025 }
2026
2027 if( attributes->domain_parameters_size != 0 )
2028 {
2029#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002030 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002031 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002032 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002033 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002035
Steven Cooreman7f391872020-07-30 14:57:44 +02002036 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002037 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002038 slot->data.key.data,
2039 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002040 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002041 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002042 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002043
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002044 mbedtls_mpi_init( &actual );
2045 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002046 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002047 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002048 mbedtls_rsa_free( rsa );
2049 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002050 if( ret != 0 )
2051 goto rsa_exit;
2052 ret = mbedtls_mpi_read_binary( &required,
2053 attributes->domain_parameters,
2054 attributes->domain_parameters_size );
2055 if( ret != 0 )
2056 goto rsa_exit;
2057 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2058 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2059 rsa_exit:
2060 mbedtls_mpi_free( &actual );
2061 mbedtls_mpi_free( &required );
2062 if( ret != 0)
2063 return( mbedtls_to_psa_error( ret ) );
2064 }
2065 else
2066#endif
2067 {
2068 return( PSA_ERROR_INVALID_ARGUMENT );
2069 }
2070 }
2071
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002072 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002073 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002074 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002075 return( PSA_ERROR_INVALID_ARGUMENT );
2076 }
2077
2078 return( PSA_SUCCESS );
2079}
2080
Gilles Peskine4747d192019-04-17 15:05:45 +02002081psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002082 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002083 size_t data_length,
2084 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002085{
2086 psa_status_t status;
2087 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002088 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002089
Gilles Peskine0f84d622019-09-12 19:03:13 +02002090 /* Reject zero-length symmetric keys (including raw data key objects).
2091 * This also rejects any key which might be encoded as an empty string,
2092 * which is never valid. */
2093 if( data_length == 0 )
2094 return( PSA_ERROR_INVALID_ARGUMENT );
2095
Gilles Peskinedf179142019-07-15 22:02:14 +02002096 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2097 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002098 if( status != PSA_SUCCESS )
2099 goto exit;
2100
Gilles Peskine5d309672019-07-12 23:47:28 +02002101#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2102 if( driver != NULL )
2103 {
2104 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002105 /* The driver should set the number of key bits, however in
2106 * case it doesn't, we initialize bits to an invalid value. */
2107 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002108 if( drv->key_management == NULL ||
2109 drv->key_management->p_import == NULL )
2110 {
2111 status = PSA_ERROR_NOT_SUPPORTED;
2112 goto exit;
2113 }
2114 status = drv->key_management->p_import(
2115 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002116 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002117 &bits );
2118 if( status != PSA_SUCCESS )
2119 goto exit;
2120 if( bits > PSA_MAX_KEY_BITS )
2121 {
2122 status = PSA_ERROR_NOT_SUPPORTED;
2123 goto exit;
2124 }
2125 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002126 }
2127 else
2128#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2129 {
2130 status = psa_import_key_into_slot( slot, data, data_length );
2131 if( status != PSA_SUCCESS )
2132 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002133 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002134 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002135 if( status != PSA_SUCCESS )
2136 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002137
Gilles Peskine011e4282019-06-26 18:34:38 +02002138 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002139exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002140 if( status != PSA_SUCCESS )
2141 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002142 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002143 *handle = 0;
2144 }
2145 return( status );
2146}
2147
Gilles Peskined7729582019-08-05 15:55:54 +02002148#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2149psa_status_t mbedtls_psa_register_se_key(
2150 const psa_key_attributes_t *attributes )
2151{
2152 psa_status_t status;
2153 psa_key_slot_t *slot = NULL;
2154 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002155 psa_key_handle_t handle = 0;
2156
2157 /* Leaving attributes unspecified is not currently supported.
2158 * It could make sense to query the key type and size from the
2159 * secure element, but not all secure elements support this
2160 * and the driver HAL doesn't currently support it. */
2161 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2162 return( PSA_ERROR_NOT_SUPPORTED );
2163 if( psa_get_key_bits( attributes ) == 0 )
2164 return( PSA_ERROR_NOT_SUPPORTED );
2165
2166 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2167 &handle, &slot, &driver );
2168 if( status != PSA_SUCCESS )
2169 goto exit;
2170
Gilles Peskined7729582019-08-05 15:55:54 +02002171 status = psa_finish_key_creation( slot, driver );
2172
2173exit:
2174 if( status != PSA_SUCCESS )
2175 {
2176 psa_fail_key_creation( slot, driver );
2177 }
2178 /* Registration doesn't keep the key in RAM. */
2179 psa_close_key( handle );
2180 return( status );
2181}
2182#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2183
Gilles Peskinef603c712019-01-19 13:40:11 +01002184static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002185 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002186{
2187 psa_status_t status;
2188 uint8_t *buffer = NULL;
2189 size_t buffer_size = 0;
2190 size_t length;
2191
Gilles Peskine8e338702019-07-30 20:06:31 +02002192 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002193 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002194 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002195 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002196 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002197 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2198 if( status != PSA_SUCCESS )
2199 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002200 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002201 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002202
2203exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002204 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002205 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002206 return( status );
2207}
2208
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002209psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2210 const psa_key_attributes_t *specified_attributes,
2211 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002212{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002213 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002214 psa_key_slot_t *source_slot = NULL;
2215 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002216 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002217 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002218
Gilles Peskine28f8f302019-07-24 13:30:31 +02002219 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002220 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002221 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002222 goto exit;
2223
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002224 status = psa_validate_optional_attributes( source_slot,
2225 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002226 if( status != PSA_SUCCESS )
2227 goto exit;
2228
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002229 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002230 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002231 if( status != PSA_SUCCESS )
2232 goto exit;
2233
Gilles Peskinedf179142019-07-15 22:02:14 +02002234 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2235 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002236 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002237 if( status != PSA_SUCCESS )
2238 goto exit;
2239
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002240#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2241 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002242 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002243 /* Copying to a secure element is not implemented yet. */
2244 status = PSA_ERROR_NOT_SUPPORTED;
2245 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002246 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002247#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002248
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002249 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002250 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002251 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002252
Gilles Peskine011e4282019-06-26 18:34:38 +02002253 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002254exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002255 if( status != PSA_SUCCESS )
2256 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002257 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002258 *target_handle = 0;
2259 }
2260 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002261}
2262
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002263
2264
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002265/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002266/* Message digests */
2267/****************************************************************/
2268
Gilles Peskineb16841e2019-10-10 20:36:12 +02002269#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002270static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002271{
2272 switch( alg )
2273 {
2274#if defined(MBEDTLS_MD2_C)
2275 case PSA_ALG_MD2:
2276 return( &mbedtls_md2_info );
2277#endif
2278#if defined(MBEDTLS_MD4_C)
2279 case PSA_ALG_MD4:
2280 return( &mbedtls_md4_info );
2281#endif
2282#if defined(MBEDTLS_MD5_C)
2283 case PSA_ALG_MD5:
2284 return( &mbedtls_md5_info );
2285#endif
2286#if defined(MBEDTLS_RIPEMD160_C)
2287 case PSA_ALG_RIPEMD160:
2288 return( &mbedtls_ripemd160_info );
2289#endif
2290#if defined(MBEDTLS_SHA1_C)
2291 case PSA_ALG_SHA_1:
2292 return( &mbedtls_sha1_info );
2293#endif
2294#if defined(MBEDTLS_SHA256_C)
2295 case PSA_ALG_SHA_224:
2296 return( &mbedtls_sha224_info );
2297 case PSA_ALG_SHA_256:
2298 return( &mbedtls_sha256_info );
2299#endif
2300#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002301#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002302 case PSA_ALG_SHA_384:
2303 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002304#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002305 case PSA_ALG_SHA_512:
2306 return( &mbedtls_sha512_info );
2307#endif
2308 default:
2309 return( NULL );
2310 }
2311}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002312#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002313
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2315{
2316 switch( operation->alg )
2317 {
Gilles Peskine81736312018-06-26 15:04:31 +02002318 case 0:
2319 /* The object has (apparently) been initialized but it is not
2320 * in use. It's ok to call abort on such an object, and there's
2321 * nothing to do. */
2322 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323#if defined(MBEDTLS_MD2_C)
2324 case PSA_ALG_MD2:
2325 mbedtls_md2_free( &operation->ctx.md2 );
2326 break;
2327#endif
2328#if defined(MBEDTLS_MD4_C)
2329 case PSA_ALG_MD4:
2330 mbedtls_md4_free( &operation->ctx.md4 );
2331 break;
2332#endif
2333#if defined(MBEDTLS_MD5_C)
2334 case PSA_ALG_MD5:
2335 mbedtls_md5_free( &operation->ctx.md5 );
2336 break;
2337#endif
2338#if defined(MBEDTLS_RIPEMD160_C)
2339 case PSA_ALG_RIPEMD160:
2340 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2341 break;
2342#endif
2343#if defined(MBEDTLS_SHA1_C)
2344 case PSA_ALG_SHA_1:
2345 mbedtls_sha1_free( &operation->ctx.sha1 );
2346 break;
2347#endif
2348#if defined(MBEDTLS_SHA256_C)
2349 case PSA_ALG_SHA_224:
2350 case PSA_ALG_SHA_256:
2351 mbedtls_sha256_free( &operation->ctx.sha256 );
2352 break;
2353#endif
2354#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002355#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002356 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002357#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358 case PSA_ALG_SHA_512:
2359 mbedtls_sha512_free( &operation->ctx.sha512 );
2360 break;
2361#endif
2362 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002363 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002364 }
2365 operation->alg = 0;
2366 return( PSA_SUCCESS );
2367}
2368
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002369psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002370 psa_algorithm_t alg )
2371{
Janos Follath24eed8d2019-11-22 13:21:35 +00002372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002373
2374 /* A context must be freshly initialized before it can be set up. */
2375 if( operation->alg != 0 )
2376 {
2377 return( PSA_ERROR_BAD_STATE );
2378 }
2379
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002380 switch( alg )
2381 {
2382#if defined(MBEDTLS_MD2_C)
2383 case PSA_ALG_MD2:
2384 mbedtls_md2_init( &operation->ctx.md2 );
2385 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2386 break;
2387#endif
2388#if defined(MBEDTLS_MD4_C)
2389 case PSA_ALG_MD4:
2390 mbedtls_md4_init( &operation->ctx.md4 );
2391 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2392 break;
2393#endif
2394#if defined(MBEDTLS_MD5_C)
2395 case PSA_ALG_MD5:
2396 mbedtls_md5_init( &operation->ctx.md5 );
2397 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2398 break;
2399#endif
2400#if defined(MBEDTLS_RIPEMD160_C)
2401 case PSA_ALG_RIPEMD160:
2402 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2403 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2404 break;
2405#endif
2406#if defined(MBEDTLS_SHA1_C)
2407 case PSA_ALG_SHA_1:
2408 mbedtls_sha1_init( &operation->ctx.sha1 );
2409 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2410 break;
2411#endif
2412#if defined(MBEDTLS_SHA256_C)
2413 case PSA_ALG_SHA_224:
2414 mbedtls_sha256_init( &operation->ctx.sha256 );
2415 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2416 break;
2417 case PSA_ALG_SHA_256:
2418 mbedtls_sha256_init( &operation->ctx.sha256 );
2419 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2420 break;
2421#endif
2422#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002423#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002424 case PSA_ALG_SHA_384:
2425 mbedtls_sha512_init( &operation->ctx.sha512 );
2426 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2427 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002428#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002429 case PSA_ALG_SHA_512:
2430 mbedtls_sha512_init( &operation->ctx.sha512 );
2431 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2432 break;
2433#endif
2434 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002435 return( PSA_ALG_IS_HASH( alg ) ?
2436 PSA_ERROR_NOT_SUPPORTED :
2437 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002438 }
2439 if( ret == 0 )
2440 operation->alg = alg;
2441 else
2442 psa_hash_abort( operation );
2443 return( mbedtls_to_psa_error( ret ) );
2444}
2445
2446psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2447 const uint8_t *input,
2448 size_t input_length )
2449{
Janos Follath24eed8d2019-11-22 13:21:35 +00002450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002451
2452 /* Don't require hash implementations to behave correctly on a
2453 * zero-length input, which may have an invalid pointer. */
2454 if( input_length == 0 )
2455 return( PSA_SUCCESS );
2456
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002457 switch( operation->alg )
2458 {
2459#if defined(MBEDTLS_MD2_C)
2460 case PSA_ALG_MD2:
2461 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2462 input, input_length );
2463 break;
2464#endif
2465#if defined(MBEDTLS_MD4_C)
2466 case PSA_ALG_MD4:
2467 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2468 input, input_length );
2469 break;
2470#endif
2471#if defined(MBEDTLS_MD5_C)
2472 case PSA_ALG_MD5:
2473 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2474 input, input_length );
2475 break;
2476#endif
2477#if defined(MBEDTLS_RIPEMD160_C)
2478 case PSA_ALG_RIPEMD160:
2479 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2480 input, input_length );
2481 break;
2482#endif
2483#if defined(MBEDTLS_SHA1_C)
2484 case PSA_ALG_SHA_1:
2485 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2486 input, input_length );
2487 break;
2488#endif
2489#if defined(MBEDTLS_SHA256_C)
2490 case PSA_ALG_SHA_224:
2491 case PSA_ALG_SHA_256:
2492 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2493 input, input_length );
2494 break;
2495#endif
2496#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002497#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002498 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002499#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002500 case PSA_ALG_SHA_512:
2501 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2502 input, input_length );
2503 break;
2504#endif
2505 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002506 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002507 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002508
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002509 if( ret != 0 )
2510 psa_hash_abort( operation );
2511 return( mbedtls_to_psa_error( ret ) );
2512}
2513
2514psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2515 uint8_t *hash,
2516 size_t hash_size,
2517 size_t *hash_length )
2518{
itayzafrir40835d42018-08-02 13:14:17 +03002519 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002521 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002522
2523 /* Fill the output buffer with something that isn't a valid hash
2524 * (barring an attack on the hash and deliberately-crafted input),
2525 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002526 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002527 /* If hash_size is 0 then hash may be NULL and then the
2528 * call to memset would have undefined behavior. */
2529 if( hash_size != 0 )
2530 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002531
2532 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002533 {
2534 status = PSA_ERROR_BUFFER_TOO_SMALL;
2535 goto exit;
2536 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002537
2538 switch( operation->alg )
2539 {
2540#if defined(MBEDTLS_MD2_C)
2541 case PSA_ALG_MD2:
2542 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2543 break;
2544#endif
2545#if defined(MBEDTLS_MD4_C)
2546 case PSA_ALG_MD4:
2547 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2548 break;
2549#endif
2550#if defined(MBEDTLS_MD5_C)
2551 case PSA_ALG_MD5:
2552 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2553 break;
2554#endif
2555#if defined(MBEDTLS_RIPEMD160_C)
2556 case PSA_ALG_RIPEMD160:
2557 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2558 break;
2559#endif
2560#if defined(MBEDTLS_SHA1_C)
2561 case PSA_ALG_SHA_1:
2562 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2563 break;
2564#endif
2565#if defined(MBEDTLS_SHA256_C)
2566 case PSA_ALG_SHA_224:
2567 case PSA_ALG_SHA_256:
2568 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2569 break;
2570#endif
2571#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002572#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002574#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002575 case PSA_ALG_SHA_512:
2576 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2577 break;
2578#endif
2579 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002580 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002581 }
itayzafrir40835d42018-08-02 13:14:17 +03002582 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002583
itayzafrir40835d42018-08-02 13:14:17 +03002584exit:
2585 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002586 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002587 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002588 return( psa_hash_abort( operation ) );
2589 }
2590 else
2591 {
2592 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002593 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002594 }
2595}
2596
Gilles Peskine2d277862018-06-18 15:41:12 +02002597psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2598 const uint8_t *hash,
2599 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002600{
2601 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2602 size_t actual_hash_length;
2603 psa_status_t status = psa_hash_finish( operation,
2604 actual_hash, sizeof( actual_hash ),
2605 &actual_hash_length );
2606 if( status != PSA_SUCCESS )
2607 return( status );
2608 if( actual_hash_length != hash_length )
2609 return( PSA_ERROR_INVALID_SIGNATURE );
2610 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2611 return( PSA_ERROR_INVALID_SIGNATURE );
2612 return( PSA_SUCCESS );
2613}
2614
Gilles Peskine0a749c82019-11-28 19:33:58 +01002615psa_status_t psa_hash_compute( psa_algorithm_t alg,
2616 const uint8_t *input, size_t input_length,
2617 uint8_t *hash, size_t hash_size,
2618 size_t *hash_length )
2619{
2620 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2621 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2622
2623 *hash_length = hash_size;
2624 status = psa_hash_setup( &operation, alg );
2625 if( status != PSA_SUCCESS )
2626 goto exit;
2627 status = psa_hash_update( &operation, input, input_length );
2628 if( status != PSA_SUCCESS )
2629 goto exit;
2630 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2631 if( status != PSA_SUCCESS )
2632 goto exit;
2633
2634exit:
2635 if( status == PSA_SUCCESS )
2636 status = psa_hash_abort( &operation );
2637 else
2638 psa_hash_abort( &operation );
2639 return( status );
2640}
2641
2642psa_status_t psa_hash_compare( psa_algorithm_t alg,
2643 const uint8_t *input, size_t input_length,
2644 const uint8_t *hash, size_t hash_length )
2645{
2646 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2647 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2648
2649 status = psa_hash_setup( &operation, alg );
2650 if( status != PSA_SUCCESS )
2651 goto exit;
2652 status = psa_hash_update( &operation, input, input_length );
2653 if( status != PSA_SUCCESS )
2654 goto exit;
2655 status = psa_hash_verify( &operation, hash, hash_length );
2656 if( status != PSA_SUCCESS )
2657 goto exit;
2658
2659exit:
2660 if( status == PSA_SUCCESS )
2661 status = psa_hash_abort( &operation );
2662 else
2663 psa_hash_abort( &operation );
2664 return( status );
2665}
2666
Gilles Peskineeb35d782019-01-22 17:56:16 +01002667psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2668 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002669{
2670 if( target_operation->alg != 0 )
2671 return( PSA_ERROR_BAD_STATE );
2672
2673 switch( source_operation->alg )
2674 {
2675 case 0:
2676 return( PSA_ERROR_BAD_STATE );
2677#if defined(MBEDTLS_MD2_C)
2678 case PSA_ALG_MD2:
2679 mbedtls_md2_clone( &target_operation->ctx.md2,
2680 &source_operation->ctx.md2 );
2681 break;
2682#endif
2683#if defined(MBEDTLS_MD4_C)
2684 case PSA_ALG_MD4:
2685 mbedtls_md4_clone( &target_operation->ctx.md4,
2686 &source_operation->ctx.md4 );
2687 break;
2688#endif
2689#if defined(MBEDTLS_MD5_C)
2690 case PSA_ALG_MD5:
2691 mbedtls_md5_clone( &target_operation->ctx.md5,
2692 &source_operation->ctx.md5 );
2693 break;
2694#endif
2695#if defined(MBEDTLS_RIPEMD160_C)
2696 case PSA_ALG_RIPEMD160:
2697 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2698 &source_operation->ctx.ripemd160 );
2699 break;
2700#endif
2701#if defined(MBEDTLS_SHA1_C)
2702 case PSA_ALG_SHA_1:
2703 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2704 &source_operation->ctx.sha1 );
2705 break;
2706#endif
2707#if defined(MBEDTLS_SHA256_C)
2708 case PSA_ALG_SHA_224:
2709 case PSA_ALG_SHA_256:
2710 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2711 &source_operation->ctx.sha256 );
2712 break;
2713#endif
2714#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002715#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002716 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002717#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002718 case PSA_ALG_SHA_512:
2719 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2720 &source_operation->ctx.sha512 );
2721 break;
2722#endif
2723 default:
2724 return( PSA_ERROR_NOT_SUPPORTED );
2725 }
2726
2727 target_operation->alg = source_operation->alg;
2728 return( PSA_SUCCESS );
2729}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002730
2731
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002732/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733/* MAC */
2734/****************************************************************/
2735
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002736static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002737 psa_algorithm_t alg,
2738 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002739 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002740 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002743 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002744
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002745 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002746 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002747
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2749 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002750 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002752 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002753 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754 mode = MBEDTLS_MODE_STREAM;
2755 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756 case PSA_ALG_CTR:
2757 mode = MBEDTLS_MODE_CTR;
2758 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002759 case PSA_ALG_CFB:
2760 mode = MBEDTLS_MODE_CFB;
2761 break;
2762 case PSA_ALG_OFB:
2763 mode = MBEDTLS_MODE_OFB;
2764 break;
2765 case PSA_ALG_CBC_NO_PADDING:
2766 mode = MBEDTLS_MODE_CBC;
2767 break;
2768 case PSA_ALG_CBC_PKCS7:
2769 mode = MBEDTLS_MODE_CBC;
2770 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002771 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772 mode = MBEDTLS_MODE_CCM;
2773 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002774 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002775 mode = MBEDTLS_MODE_GCM;
2776 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002777 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2778 mode = MBEDTLS_MODE_CHACHAPOLY;
2779 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002780 default:
2781 return( NULL );
2782 }
2783 }
2784 else if( alg == PSA_ALG_CMAC )
2785 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002786 else
2787 return( NULL );
2788
2789 switch( key_type )
2790 {
2791 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002792 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002793 break;
2794 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002795 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2796 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002797 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002798 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002799 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002800 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002801 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2802 * but two-key Triple-DES is functionally three-key Triple-DES
2803 * with K1=K3, so that's how we present it to mbedtls. */
2804 if( key_bits == 128 )
2805 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002806 break;
2807 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002808 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002809 break;
2810 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002811 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002812 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002813 case PSA_KEY_TYPE_CHACHA20:
2814 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2815 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002816 default:
2817 return( NULL );
2818 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002819 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002820 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002821
Jaeden Amero23bbb752018-06-26 14:16:54 +01002822 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2823 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002824}
2825
Gilles Peskinea05219c2018-11-16 16:02:56 +01002826#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002827static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002828{
Gilles Peskine2d277862018-06-18 15:41:12 +02002829 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002830 {
2831 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002832 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002833 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002834 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002835 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002836 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002837 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002838 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002839 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002840 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002841 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002842 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002843 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002844 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002845 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002846 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002847 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002848 return( 128 );
2849 default:
2850 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002851 }
2852}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002853#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002854
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002855/* Initialize the MAC operation structure. Once this function has been
2856 * called, psa_mac_abort can run and will do the right thing. */
2857static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2858 psa_algorithm_t alg )
2859{
2860 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2861
2862 operation->alg = alg;
2863 operation->key_set = 0;
2864 operation->iv_set = 0;
2865 operation->iv_required = 0;
2866 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002867 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002868
2869#if defined(MBEDTLS_CMAC_C)
2870 if( alg == PSA_ALG_CMAC )
2871 {
2872 operation->iv_required = 0;
2873 mbedtls_cipher_init( &operation->ctx.cmac );
2874 status = PSA_SUCCESS;
2875 }
2876 else
2877#endif /* MBEDTLS_CMAC_C */
2878#if defined(MBEDTLS_MD_C)
2879 if( PSA_ALG_IS_HMAC( operation->alg ) )
2880 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002881 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2882 operation->ctx.hmac.hash_ctx.alg = 0;
2883 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002884 }
2885 else
2886#endif /* MBEDTLS_MD_C */
2887 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002888 if( ! PSA_ALG_IS_MAC( alg ) )
2889 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002890 }
2891
2892 if( status != PSA_SUCCESS )
2893 memset( operation, 0, sizeof( *operation ) );
2894 return( status );
2895}
2896
Gilles Peskine01126fa2018-07-12 17:04:55 +02002897#if defined(MBEDTLS_MD_C)
2898static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2899{
Gilles Peskine3f108122018-12-07 18:14:53 +01002900 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002901 return( psa_hash_abort( &hmac->hash_ctx ) );
2902}
2903#endif /* MBEDTLS_MD_C */
2904
Gilles Peskine8c9def32018-02-08 10:02:12 +01002905psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2906{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002907 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002908 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002909 /* The object has (apparently) been initialized but it is not
2910 * in use. It's ok to call abort on such an object, and there's
2911 * nothing to do. */
2912 return( PSA_SUCCESS );
2913 }
2914 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002915#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002916 if( operation->alg == PSA_ALG_CMAC )
2917 {
2918 mbedtls_cipher_free( &operation->ctx.cmac );
2919 }
2920 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002922#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002923 if( PSA_ALG_IS_HMAC( operation->alg ) )
2924 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002925 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002926 }
2927 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002928#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002929 {
2930 /* Sanity check (shouldn't happen: operation->alg should
2931 * always have been initialized to a valid value). */
2932 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933 }
Moran Peker41deec42018-04-04 15:43:05 +03002934
Gilles Peskine8c9def32018-02-08 10:02:12 +01002935 operation->alg = 0;
2936 operation->key_set = 0;
2937 operation->iv_set = 0;
2938 operation->iv_required = 0;
2939 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002940 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002941
Gilles Peskine8c9def32018-02-08 10:02:12 +01002942 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002943
2944bad_state:
2945 /* If abort is called on an uninitialized object, we can't trust
2946 * anything. Wipe the object in case it contains confidential data.
2947 * This may result in a memory leak if a pointer gets overwritten,
2948 * but it's too late to do anything about this. */
2949 memset( operation, 0, sizeof( *operation ) );
2950 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002951}
2952
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002953#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002954static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002955 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002956 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002957 const mbedtls_cipher_info_t *cipher_info )
2958{
Janos Follath24eed8d2019-11-22 13:21:35 +00002959 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002960
2961 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002962
2963 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2964 if( ret != 0 )
2965 return( ret );
2966
2967 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002968 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002969 key_bits );
2970 return( ret );
2971}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002972#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002973
Gilles Peskine248051a2018-06-20 16:09:38 +02002974#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002975static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2976 const uint8_t *key,
2977 size_t key_length,
2978 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002979{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002980 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002981 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002982 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002983 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002984 psa_status_t status;
2985
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002986 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2987 * overflow below. This should never trigger if the hash algorithm
2988 * is implemented correctly. */
2989 /* The size checks against the ipad and opad buffers cannot be written
2990 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2991 * because that triggers -Wlogical-op on GCC 7.3. */
2992 if( block_size > sizeof( ipad ) )
2993 return( PSA_ERROR_NOT_SUPPORTED );
2994 if( block_size > sizeof( hmac->opad ) )
2995 return( PSA_ERROR_NOT_SUPPORTED );
2996 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002997 return( PSA_ERROR_NOT_SUPPORTED );
2998
Gilles Peskined223b522018-06-11 18:12:58 +02002999 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003000 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003001 status = psa_hash_compute( hash_alg, key, key_length,
3002 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003003 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003004 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003005 }
Gilles Peskine96889972018-07-12 17:07:03 +02003006 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3007 * but it is permitted. It is common when HMAC is used in HKDF, for
3008 * example. Don't call `memcpy` in the 0-length because `key` could be
3009 * an invalid pointer which would make the behavior undefined. */
3010 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003011 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003012
Gilles Peskined223b522018-06-11 18:12:58 +02003013 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3014 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003015 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003016 ipad[i] ^= 0x36;
3017 memset( ipad + key_length, 0x36, block_size - key_length );
3018
3019 /* Copy the key material from ipad to opad, flipping the requisite bits,
3020 * and filling the rest of opad with the requisite constant. */
3021 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003022 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3023 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003024
Gilles Peskine01126fa2018-07-12 17:04:55 +02003025 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003026 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003027 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003028
Gilles Peskine01126fa2018-07-12 17:04:55 +02003029 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003030
3031cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003032 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003033
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003034 return( status );
3035}
Gilles Peskine248051a2018-06-20 16:09:38 +02003036#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003037
Gilles Peskine89167cb2018-07-08 20:12:23 +02003038static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003039 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003040 psa_algorithm_t alg,
3041 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003042{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003044 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003045 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003046 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003047 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003048 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003049 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003050
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003051 /* A context must be freshly initialized before it can be set up. */
3052 if( operation->alg != 0 )
3053 {
3054 return( PSA_ERROR_BAD_STATE );
3055 }
3056
Gilles Peskined911eb72018-08-14 15:18:45 +02003057 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003058 if( status != PSA_SUCCESS )
3059 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003060 if( is_sign )
3061 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003062
Gilles Peskine28f8f302019-07-24 13:30:31 +02003063 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003064 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003065 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003066 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003067
Gilles Peskine8c9def32018-02-08 10:02:12 +01003068#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003069 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003070 {
3071 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003072 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003073 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003075 if( cipher_info == NULL )
3076 {
3077 status = PSA_ERROR_NOT_SUPPORTED;
3078 goto exit;
3079 }
3080 operation->mac_size = cipher_info->block_size;
3081 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3082 status = mbedtls_to_psa_error( ret );
3083 }
3084 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003085#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003086#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003087 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003088 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003089 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003090 if( hash_alg == 0 )
3091 {
3092 status = PSA_ERROR_NOT_SUPPORTED;
3093 goto exit;
3094 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003095
3096 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3097 /* Sanity check. This shouldn't fail on a valid configuration. */
3098 if( operation->mac_size == 0 ||
3099 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3100 {
3101 status = PSA_ERROR_NOT_SUPPORTED;
3102 goto exit;
3103 }
3104
Gilles Peskine8e338702019-07-30 20:06:31 +02003105 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003106 {
3107 status = PSA_ERROR_INVALID_ARGUMENT;
3108 goto exit;
3109 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003110
Gilles Peskine01126fa2018-07-12 17:04:55 +02003111 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003112 slot->data.key.data,
3113 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003114 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003115 }
3116 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003117#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003118 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003119 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003120 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003121 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003122
Gilles Peskined911eb72018-08-14 15:18:45 +02003123 if( truncated == 0 )
3124 {
3125 /* The "normal" case: untruncated algorithm. Nothing to do. */
3126 }
3127 else if( truncated < 4 )
3128 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003129 /* A very short MAC is too short for security since it can be
3130 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3131 * so we make this our minimum, even though 32 bits is still
3132 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003133 status = PSA_ERROR_NOT_SUPPORTED;
3134 }
3135 else if( truncated > operation->mac_size )
3136 {
3137 /* It's impossible to "truncate" to a larger length. */
3138 status = PSA_ERROR_INVALID_ARGUMENT;
3139 }
3140 else
3141 operation->mac_size = truncated;
3142
Gilles Peskinefbfac682018-07-08 20:51:54 +02003143exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003144 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003145 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003146 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003147 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003148 else
3149 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003150 operation->key_set = 1;
3151 }
3152 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003153}
3154
Gilles Peskine89167cb2018-07-08 20:12:23 +02003155psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003156 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003157 psa_algorithm_t alg )
3158{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003159 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003160}
3161
3162psa_status_t psa_mac_verify_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, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003167}
3168
Gilles Peskine8c9def32018-02-08 10:02:12 +01003169psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3170 const uint8_t *input,
3171 size_t input_length )
3172{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003173 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003174 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003175 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003176 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003177 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003178 operation->has_input = 1;
3179
Gilles Peskine8c9def32018-02-08 10:02:12 +01003180#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003181 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003182 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003183 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3184 input, input_length );
3185 status = mbedtls_to_psa_error( ret );
3186 }
3187 else
3188#endif /* MBEDTLS_CMAC_C */
3189#if defined(MBEDTLS_MD_C)
3190 if( PSA_ALG_IS_HMAC( operation->alg ) )
3191 {
3192 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3193 input_length );
3194 }
3195 else
3196#endif /* MBEDTLS_MD_C */
3197 {
3198 /* This shouldn't happen if `operation` was initialized by
3199 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003200 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003201 }
3202
Gilles Peskinefbfac682018-07-08 20:51:54 +02003203 if( status != PSA_SUCCESS )
3204 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003205 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003206}
3207
Gilles Peskine01126fa2018-07-12 17:04:55 +02003208#if defined(MBEDTLS_MD_C)
3209static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3210 uint8_t *mac,
3211 size_t mac_size )
3212{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003213 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003214 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3215 size_t hash_size = 0;
3216 size_t block_size = psa_get_hash_block_size( hash_alg );
3217 psa_status_t status;
3218
Gilles Peskine01126fa2018-07-12 17:04:55 +02003219 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3220 if( status != PSA_SUCCESS )
3221 return( status );
3222 /* From here on, tmp needs to be wiped. */
3223
3224 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3225 if( status != PSA_SUCCESS )
3226 goto exit;
3227
3228 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3229 if( status != PSA_SUCCESS )
3230 goto exit;
3231
3232 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3233 if( status != PSA_SUCCESS )
3234 goto exit;
3235
Gilles Peskined911eb72018-08-14 15:18:45 +02003236 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3237 if( status != PSA_SUCCESS )
3238 goto exit;
3239
3240 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003241
3242exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003243 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003244 return( status );
3245}
3246#endif /* MBEDTLS_MD_C */
3247
mohammad16036df908f2018-04-02 08:34:15 -07003248static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003249 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003250 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003251{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003252 if( ! operation->key_set )
3253 return( PSA_ERROR_BAD_STATE );
3254 if( operation->iv_required && ! operation->iv_set )
3255 return( PSA_ERROR_BAD_STATE );
3256
Gilles Peskine8c9def32018-02-08 10:02:12 +01003257 if( mac_size < operation->mac_size )
3258 return( PSA_ERROR_BUFFER_TOO_SMALL );
3259
Gilles Peskine8c9def32018-02-08 10:02:12 +01003260#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003261 if( operation->alg == PSA_ALG_CMAC )
3262 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003263 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3264 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3265 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003266 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003267 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003268 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003269 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003270 else
3271#endif /* MBEDTLS_CMAC_C */
3272#if defined(MBEDTLS_MD_C)
3273 if( PSA_ALG_IS_HMAC( operation->alg ) )
3274 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003275 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003276 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003277 }
3278 else
3279#endif /* MBEDTLS_MD_C */
3280 {
3281 /* This shouldn't happen if `operation` was initialized by
3282 * a setup function. */
3283 return( PSA_ERROR_BAD_STATE );
3284 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003285}
3286
Gilles Peskineacd4be32018-07-08 19:56:25 +02003287psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3288 uint8_t *mac,
3289 size_t mac_size,
3290 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003291{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003292 psa_status_t status;
3293
Jaeden Amero252ef282019-02-15 14:05:35 +00003294 if( operation->alg == 0 )
3295 {
3296 return( PSA_ERROR_BAD_STATE );
3297 }
3298
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003299 /* Fill the output buffer with something that isn't a valid mac
3300 * (barring an attack on the mac and deliberately-crafted input),
3301 * in case the caller doesn't check the return status properly. */
3302 *mac_length = mac_size;
3303 /* If mac_size is 0 then mac may be NULL and then the
3304 * call to memset would have undefined behavior. */
3305 if( mac_size != 0 )
3306 memset( mac, '!', mac_size );
3307
Gilles Peskine89167cb2018-07-08 20:12:23 +02003308 if( ! operation->is_sign )
3309 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003310 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003311 }
mohammad16036df908f2018-04-02 08:34:15 -07003312
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003313 status = psa_mac_finish_internal( operation, mac, mac_size );
3314
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003315 if( status == PSA_SUCCESS )
3316 {
3317 status = psa_mac_abort( operation );
3318 if( status == PSA_SUCCESS )
3319 *mac_length = operation->mac_size;
3320 else
3321 memset( mac, '!', mac_size );
3322 }
3323 else
3324 psa_mac_abort( operation );
3325 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003326}
3327
Gilles Peskineacd4be32018-07-08 19:56:25 +02003328psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3329 const uint8_t *mac,
3330 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003331{
Gilles Peskine828ed142018-06-18 23:25:51 +02003332 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003333 psa_status_t status;
3334
Jaeden Amero252ef282019-02-15 14:05:35 +00003335 if( operation->alg == 0 )
3336 {
3337 return( PSA_ERROR_BAD_STATE );
3338 }
3339
Gilles Peskine89167cb2018-07-08 20:12:23 +02003340 if( operation->is_sign )
3341 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003342 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003343 }
3344 if( operation->mac_size != mac_length )
3345 {
3346 status = PSA_ERROR_INVALID_SIGNATURE;
3347 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003348 }
mohammad16036df908f2018-04-02 08:34:15 -07003349
3350 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003351 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003352 if( status != PSA_SUCCESS )
3353 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003354
3355 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3356 status = PSA_ERROR_INVALID_SIGNATURE;
3357
3358cleanup:
3359 if( status == PSA_SUCCESS )
3360 status = psa_mac_abort( operation );
3361 else
3362 psa_mac_abort( operation );
3363
Gilles Peskine3f108122018-12-07 18:14:53 +01003364 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003365
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003366 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003367}
3368
3369
Gilles Peskine20035e32018-02-03 22:44:14 +01003370
Gilles Peskine20035e32018-02-03 22:44:14 +01003371/****************************************************************/
3372/* Asymmetric cryptography */
3373/****************************************************************/
3374
Gilles Peskine2b450e32018-06-27 15:42:46 +02003375#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003376/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003377 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003378static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3379 size_t hash_length,
3380 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003381{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003382 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003383 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003384 *md_alg = mbedtls_md_get_type( md_info );
3385
3386 /* The Mbed TLS RSA module uses an unsigned int for hash length
3387 * parameters. Validate that it fits so that we don't risk an
3388 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003389#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003390 if( hash_length > UINT_MAX )
3391 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003392#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003393
3394#if defined(MBEDTLS_PKCS1_V15)
3395 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3396 * must be correct. */
3397 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3398 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003399 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003400 if( md_info == NULL )
3401 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003402 if( mbedtls_md_get_size( md_info ) != hash_length )
3403 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003404 }
3405#endif /* MBEDTLS_PKCS1_V15 */
3406
3407#if defined(MBEDTLS_PKCS1_V21)
3408 /* PSS requires a hash internally. */
3409 if( PSA_ALG_IS_RSA_PSS( alg ) )
3410 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003411 if( md_info == NULL )
3412 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003413 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003414#endif /* MBEDTLS_PKCS1_V21 */
3415
Gilles Peskine61b91d42018-06-08 16:09:36 +02003416 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003417}
3418
Gilles Peskine2b450e32018-06-27 15:42:46 +02003419static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3420 psa_algorithm_t alg,
3421 const uint8_t *hash,
3422 size_t hash_length,
3423 uint8_t *signature,
3424 size_t signature_size,
3425 size_t *signature_length )
3426{
3427 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003428 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003429 mbedtls_md_type_t md_alg;
3430
3431 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3432 if( status != PSA_SUCCESS )
3433 return( status );
3434
Gilles Peskine630a18a2018-06-29 17:49:35 +02003435 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003436 return( PSA_ERROR_BUFFER_TOO_SMALL );
3437
3438#if defined(MBEDTLS_PKCS1_V15)
3439 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3440 {
3441 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3442 MBEDTLS_MD_NONE );
3443 ret = mbedtls_rsa_pkcs1_sign( rsa,
3444 mbedtls_ctr_drbg_random,
3445 &global_data.ctr_drbg,
3446 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003447 md_alg,
3448 (unsigned int) hash_length,
3449 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003450 signature );
3451 }
3452 else
3453#endif /* MBEDTLS_PKCS1_V15 */
3454#if defined(MBEDTLS_PKCS1_V21)
3455 if( PSA_ALG_IS_RSA_PSS( alg ) )
3456 {
3457 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3458 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3459 mbedtls_ctr_drbg_random,
3460 &global_data.ctr_drbg,
3461 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003462 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003463 (unsigned int) hash_length,
3464 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003465 signature );
3466 }
3467 else
3468#endif /* MBEDTLS_PKCS1_V21 */
3469 {
3470 return( PSA_ERROR_INVALID_ARGUMENT );
3471 }
3472
3473 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003474 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003475 return( mbedtls_to_psa_error( ret ) );
3476}
3477
3478static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3479 psa_algorithm_t alg,
3480 const uint8_t *hash,
3481 size_t hash_length,
3482 const uint8_t *signature,
3483 size_t signature_length )
3484{
3485 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003487 mbedtls_md_type_t md_alg;
3488
3489 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3490 if( status != PSA_SUCCESS )
3491 return( status );
3492
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003493 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3494 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003495
3496#if defined(MBEDTLS_PKCS1_V15)
3497 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3498 {
3499 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3500 MBEDTLS_MD_NONE );
3501 ret = mbedtls_rsa_pkcs1_verify( rsa,
3502 mbedtls_ctr_drbg_random,
3503 &global_data.ctr_drbg,
3504 MBEDTLS_RSA_PUBLIC,
3505 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003506 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003507 hash,
3508 signature );
3509 }
3510 else
3511#endif /* MBEDTLS_PKCS1_V15 */
3512#if defined(MBEDTLS_PKCS1_V21)
3513 if( PSA_ALG_IS_RSA_PSS( alg ) )
3514 {
3515 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3516 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3517 mbedtls_ctr_drbg_random,
3518 &global_data.ctr_drbg,
3519 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003520 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003521 (unsigned int) hash_length,
3522 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003523 signature );
3524 }
3525 else
3526#endif /* MBEDTLS_PKCS1_V21 */
3527 {
3528 return( PSA_ERROR_INVALID_ARGUMENT );
3529 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003530
3531 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3532 * the rest of the signature is invalid". This has little use in
3533 * practice and PSA doesn't report this distinction. */
3534 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3535 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003536 return( mbedtls_to_psa_error( ret ) );
3537}
3538#endif /* MBEDTLS_RSA_C */
3539
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003540#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003541/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3542 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3543 * (even though these functions don't modify it). */
3544static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3545 psa_algorithm_t alg,
3546 const uint8_t *hash,
3547 size_t hash_length,
3548 uint8_t *signature,
3549 size_t signature_size,
3550 size_t *signature_length )
3551{
Janos Follath24eed8d2019-11-22 13:21:35 +00003552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003553 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003554 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003555 mbedtls_mpi_init( &r );
3556 mbedtls_mpi_init( &s );
3557
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003558 if( signature_size < 2 * curve_bytes )
3559 {
3560 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3561 goto cleanup;
3562 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003563
Gilles Peskinea05219c2018-11-16 16:02:56 +01003564#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003565 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3566 {
3567 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3568 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3569 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003570 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3571 &ecp->d, hash,
3572 hash_length, md_alg,
3573 mbedtls_ctr_drbg_random,
3574 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003575 }
3576 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003577#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003578 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003579 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003580 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3581 hash, hash_length,
3582 mbedtls_ctr_drbg_random,
3583 &global_data.ctr_drbg ) );
3584 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003585
3586 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3587 signature,
3588 curve_bytes ) );
3589 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3590 signature + curve_bytes,
3591 curve_bytes ) );
3592
3593cleanup:
3594 mbedtls_mpi_free( &r );
3595 mbedtls_mpi_free( &s );
3596 if( ret == 0 )
3597 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003598 return( mbedtls_to_psa_error( ret ) );
3599}
3600
3601static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3602 const uint8_t *hash,
3603 size_t hash_length,
3604 const uint8_t *signature,
3605 size_t signature_length )
3606{
Janos Follath24eed8d2019-11-22 13:21:35 +00003607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003608 mbedtls_mpi r, s;
3609 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3610 mbedtls_mpi_init( &r );
3611 mbedtls_mpi_init( &s );
3612
3613 if( signature_length != 2 * curve_bytes )
3614 return( PSA_ERROR_INVALID_SIGNATURE );
3615
3616 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3617 signature,
3618 curve_bytes ) );
3619 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3620 signature + curve_bytes,
3621 curve_bytes ) );
3622
Steven Cooremanacda8342020-07-24 23:09:52 +02003623 /* Check whether the public part is loaded. If not, load it. */
3624 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3625 {
3626 MBEDTLS_MPI_CHK(
3627 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3628 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3629 }
3630
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003631 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3632 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003633
3634cleanup:
3635 mbedtls_mpi_free( &r );
3636 mbedtls_mpi_free( &s );
3637 return( mbedtls_to_psa_error( ret ) );
3638}
3639#endif /* MBEDTLS_ECDSA_C */
3640
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003641psa_status_t psa_sign_hash( psa_key_handle_t handle,
3642 psa_algorithm_t alg,
3643 const uint8_t *hash,
3644 size_t hash_length,
3645 uint8_t *signature,
3646 size_t signature_size,
3647 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003648{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003649 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003650 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003651#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3652 const psa_drv_se_t *drv;
3653 psa_drv_se_context_t *drv_context;
3654#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003655
3656 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003657 /* Immediately reject a zero-length signature buffer. This guarantees
3658 * that signature must be a valid pointer. (On the other hand, the hash
3659 * buffer can in principle be empty since it doesn't actually have
3660 * to be a hash.) */
3661 if( signature_size == 0 )
3662 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003663
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003664 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003665 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003666 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003667 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003668 {
3669 status = PSA_ERROR_INVALID_ARGUMENT;
3670 goto exit;
3671 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003672
Gilles Peskineedc64242019-08-07 21:05:07 +02003673#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3674 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3675 {
3676 if( drv->asymmetric == NULL ||
3677 drv->asymmetric->p_sign == NULL )
3678 {
3679 status = PSA_ERROR_NOT_SUPPORTED;
3680 goto exit;
3681 }
3682 status = drv->asymmetric->p_sign( drv_context,
3683 slot->data.se.slot_number,
3684 alg,
3685 hash, hash_length,
3686 signature, signature_size,
3687 signature_length );
3688 }
3689 else
3690#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003691#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003692 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003693 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003694 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003695
Steven Cooreman4fed4552020-08-03 14:46:03 +02003696 status = psa_load_rsa_representation( slot->attr.type,
3697 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003698 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003699 &rsa );
3700 if( status != PSA_SUCCESS )
3701 goto exit;
3702
Steven Cooremana2371e52020-07-28 14:30:39 +02003703 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003704 alg,
3705 hash, hash_length,
3706 signature, signature_size,
3707 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003708
Steven Cooremana2371e52020-07-28 14:30:39 +02003709 mbedtls_rsa_free( rsa );
3710 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003711 }
3712 else
3713#endif /* defined(MBEDTLS_RSA_C) */
3714#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003715 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003716 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003717#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003718 if(
3719#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3720 PSA_ALG_IS_ECDSA( alg )
3721#else
3722 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3723#endif
3724 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003725 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003726 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003727 status = psa_load_ecp_representation( slot->attr.type,
3728 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003729 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003730 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003731 if( status != PSA_SUCCESS )
3732 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003733 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003734 alg,
3735 hash, hash_length,
3736 signature, signature_size,
3737 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003738 mbedtls_ecp_keypair_free( ecp );
3739 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003740 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003741 else
3742#endif /* defined(MBEDTLS_ECDSA_C) */
3743 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003744 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003745 }
itayzafrir5c753392018-05-08 11:18:38 +03003746 }
3747 else
3748#endif /* defined(MBEDTLS_ECP_C) */
3749 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003750 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003751 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003752
3753exit:
3754 /* Fill the unused part of the output buffer (the whole buffer on error,
3755 * the trailing part on success) with something that isn't a valid mac
3756 * (barring an attack on the mac and deliberately-crafted input),
3757 * in case the caller doesn't check the return status properly. */
3758 if( status == PSA_SUCCESS )
3759 memset( signature + *signature_length, '!',
3760 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003761 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003762 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003763 /* If signature_size is 0 then we have nothing to do. We must not call
3764 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003765 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003766}
3767
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003768psa_status_t psa_verify_hash( psa_key_handle_t handle,
3769 psa_algorithm_t alg,
3770 const uint8_t *hash,
3771 size_t hash_length,
3772 const uint8_t *signature,
3773 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003774{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003775 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003776 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003777#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3778 const psa_drv_se_t *drv;
3779 psa_drv_se_context_t *drv_context;
3780#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003781
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003782 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003783 if( status != PSA_SUCCESS )
3784 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003785
Gilles Peskineedc64242019-08-07 21:05:07 +02003786#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3787 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3788 {
3789 if( drv->asymmetric == NULL ||
3790 drv->asymmetric->p_verify == NULL )
3791 return( PSA_ERROR_NOT_SUPPORTED );
3792 return( drv->asymmetric->p_verify( drv_context,
3793 slot->data.se.slot_number,
3794 alg,
3795 hash, hash_length,
3796 signature, signature_length ) );
3797 }
3798 else
3799#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003800#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003801 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003802 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003803 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003804
Steven Cooreman4fed4552020-08-03 14:46:03 +02003805 status = psa_load_rsa_representation( slot->attr.type,
3806 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003807 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003808 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003809 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003810 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003811
Steven Cooremana2371e52020-07-28 14:30:39 +02003812 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003813 alg,
3814 hash, hash_length,
3815 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003816 mbedtls_rsa_free( rsa );
3817 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003818 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003819 }
3820 else
3821#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003822#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003823 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003824 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003825#if defined(MBEDTLS_ECDSA_C)
3826 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003827 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003828 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003829 status = psa_load_ecp_representation( slot->attr.type,
3830 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003831 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003832 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003833 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003834 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003835 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003836 hash, hash_length,
3837 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003838 mbedtls_ecp_keypair_free( ecp );
3839 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003840 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003841 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003842 else
3843#endif /* defined(MBEDTLS_ECDSA_C) */
3844 {
3845 return( PSA_ERROR_INVALID_ARGUMENT );
3846 }
itayzafrir5c753392018-05-08 11:18:38 +03003847 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003848 else
3849#endif /* defined(MBEDTLS_ECP_C) */
3850 {
3851 return( PSA_ERROR_NOT_SUPPORTED );
3852 }
3853}
3854
Gilles Peskine072ac562018-06-30 00:21:29 +02003855#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3856static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3857 mbedtls_rsa_context *rsa )
3858{
3859 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3860 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3861 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3862 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3863}
3864#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3865
Gilles Peskinec5487a82018-12-03 18:08:14 +01003866psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003867 psa_algorithm_t alg,
3868 const uint8_t *input,
3869 size_t input_length,
3870 const uint8_t *salt,
3871 size_t salt_length,
3872 uint8_t *output,
3873 size_t output_size,
3874 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003876 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003877 psa_status_t status;
3878
Darryl Green5cc689a2018-07-24 15:34:10 +01003879 (void) input;
3880 (void) input_length;
3881 (void) salt;
3882 (void) output;
3883 (void) output_size;
3884
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003885 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003886
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003887 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3888 return( PSA_ERROR_INVALID_ARGUMENT );
3889
Gilles Peskine28f8f302019-07-24 13:30:31 +02003890 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003891 if( status != PSA_SUCCESS )
3892 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003893 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3894 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003895 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896
3897#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003898 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003899 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003900 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003901 status = psa_load_rsa_representation( slot->attr.type,
3902 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003903 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003904 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003905 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003906 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003907
3908 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003909 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003910 status = PSA_ERROR_BUFFER_TOO_SMALL;
3911 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003912 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003913#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003914 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003915 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003916 status = mbedtls_to_psa_error(
3917 mbedtls_rsa_pkcs1_encrypt( rsa,
3918 mbedtls_ctr_drbg_random,
3919 &global_data.ctr_drbg,
3920 MBEDTLS_RSA_PUBLIC,
3921 input_length,
3922 input,
3923 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003924 }
3925 else
3926#endif /* MBEDTLS_PKCS1_V15 */
3927#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003928 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003929 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003930 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003931 status = mbedtls_to_psa_error(
3932 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3933 mbedtls_ctr_drbg_random,
3934 &global_data.ctr_drbg,
3935 MBEDTLS_RSA_PUBLIC,
3936 salt, salt_length,
3937 input_length,
3938 input,
3939 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003940 }
3941 else
3942#endif /* MBEDTLS_PKCS1_V21 */
3943 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003944 status = PSA_ERROR_INVALID_ARGUMENT;
3945 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003947rsa_exit:
3948 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003949 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003950
Steven Cooremana2371e52020-07-28 14:30:39 +02003951 mbedtls_rsa_free( rsa );
3952 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003953 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003955 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003956#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003957 {
3958 return( PSA_ERROR_NOT_SUPPORTED );
3959 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003960}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003961
Gilles Peskinec5487a82018-12-03 18:08:14 +01003962psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003963 psa_algorithm_t alg,
3964 const uint8_t *input,
3965 size_t input_length,
3966 const uint8_t *salt,
3967 size_t salt_length,
3968 uint8_t *output,
3969 size_t output_size,
3970 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003971{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003972 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003973 psa_status_t status;
3974
Darryl Green5cc689a2018-07-24 15:34:10 +01003975 (void) input;
3976 (void) input_length;
3977 (void) salt;
3978 (void) output;
3979 (void) output_size;
3980
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003981 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003982
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003983 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3984 return( PSA_ERROR_INVALID_ARGUMENT );
3985
Gilles Peskine28f8f302019-07-24 13:30:31 +02003986 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003987 if( status != PSA_SUCCESS )
3988 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003989 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003990 return( PSA_ERROR_INVALID_ARGUMENT );
3991
3992#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003993 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003994 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003995 mbedtls_rsa_context *rsa = NULL;
3996 status = psa_load_rsa_representation( slot->attr.type,
3997 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003998 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003999 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004000 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004001 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004002
Steven Cooremana2371e52020-07-28 14:30:39 +02004003 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004004 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004005 status = PSA_ERROR_INVALID_ARGUMENT;
4006 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004007 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004008
4009#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004010 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004011 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004012 status = mbedtls_to_psa_error(
4013 mbedtls_rsa_pkcs1_decrypt( rsa,
4014 mbedtls_ctr_drbg_random,
4015 &global_data.ctr_drbg,
4016 MBEDTLS_RSA_PRIVATE,
4017 output_length,
4018 input,
4019 output,
4020 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021 }
4022 else
4023#endif /* MBEDTLS_PKCS1_V15 */
4024#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004025 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004026 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004027 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004028 status = mbedtls_to_psa_error(
4029 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4030 mbedtls_ctr_drbg_random,
4031 &global_data.ctr_drbg,
4032 MBEDTLS_RSA_PRIVATE,
4033 salt, salt_length,
4034 output_length,
4035 input,
4036 output,
4037 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004038 }
4039 else
4040#endif /* MBEDTLS_PKCS1_V21 */
4041 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004042 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004043 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004044
Steven Cooreman4fed4552020-08-03 14:46:03 +02004045rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004046 mbedtls_rsa_free( rsa );
4047 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004048 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004049 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004051#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004052 {
4053 return( PSA_ERROR_NOT_SUPPORTED );
4054 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004055}
Gilles Peskine20035e32018-02-03 22:44:14 +01004056
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004057
4058
mohammad1603503973b2018-03-12 15:59:30 +02004059/****************************************************************/
4060/* Symmetric cryptography */
4061/****************************************************************/
4062
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004063/* Initialize the cipher operation structure. Once this function has been
4064 * called, psa_cipher_abort can run and will do the right thing. */
4065static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
4066 psa_algorithm_t alg )
4067{
Gilles Peskinec06e0712018-06-20 16:21:04 +02004068 if( ! PSA_ALG_IS_CIPHER( alg ) )
4069 {
4070 memset( operation, 0, sizeof( *operation ) );
4071 return( PSA_ERROR_INVALID_ARGUMENT );
4072 }
4073
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004074 operation->alg = alg;
4075 operation->key_set = 0;
4076 operation->iv_set = 0;
4077 operation->iv_required = 1;
4078 operation->iv_size = 0;
4079 operation->block_size = 0;
4080 mbedtls_cipher_init( &operation->ctx.cipher );
4081 return( PSA_SUCCESS );
4082}
4083
Gilles Peskinee553c652018-06-04 16:22:46 +02004084static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004085 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004086 psa_algorithm_t alg,
4087 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004088{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004089 int ret = 0;
4090 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004091 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004092 size_t key_bits;
4093 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004094 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4095 PSA_KEY_USAGE_ENCRYPT :
4096 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004097
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004098 /* A context must be freshly initialized before it can be set up. */
4099 if( operation->alg != 0 )
4100 {
4101 return( PSA_ERROR_BAD_STATE );
4102 }
4103
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004104 status = psa_cipher_init( operation, alg );
4105 if( status != PSA_SUCCESS )
4106 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004107
Gilles Peskine28f8f302019-07-24 13:30:31 +02004108 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004109 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004110 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004111 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004112
Gilles Peskine8e338702019-07-30 20:06:31 +02004113 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004114 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004115 {
4116 status = PSA_ERROR_NOT_SUPPORTED;
4117 goto exit;
4118 }
mohammad1603503973b2018-03-12 15:59:30 +02004119
mohammad1603503973b2018-03-12 15:59:30 +02004120 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004121 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004122 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004123
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004124#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004125 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004126 {
4127 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004128 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004129 memcpy( keys, slot->data.key.data, 16 );
4130 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004131 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4132 keys,
4133 192, cipher_operation );
4134 }
4135 else
4136#endif
4137 {
4138 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004139 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004140 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004141 }
Moran Peker41deec42018-04-04 15:43:05 +03004142 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004143 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004144
mohammad16038481e742018-03-18 13:57:31 +02004145#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004146 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004147 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004148 case PSA_ALG_CBC_NO_PADDING:
4149 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4150 MBEDTLS_PADDING_NONE );
4151 break;
4152 case PSA_ALG_CBC_PKCS7:
4153 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4154 MBEDTLS_PADDING_PKCS7 );
4155 break;
4156 default:
4157 /* The algorithm doesn't involve padding. */
4158 ret = 0;
4159 break;
4160 }
4161 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004162 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004163#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4164
mohammad1603503973b2018-03-12 15:59:30 +02004165 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004166 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004167 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004168 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004169 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004170 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004171 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004172#if defined(MBEDTLS_CHACHA20_C)
4173 else
4174 if( alg == PSA_ALG_CHACHA20 )
4175 operation->iv_size = 12;
4176#endif
mohammad1603503973b2018-03-12 15:59:30 +02004177
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004178exit:
4179 if( status == 0 )
4180 status = mbedtls_to_psa_error( ret );
4181 if( status != 0 )
4182 psa_cipher_abort( operation );
4183 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004184}
4185
Gilles Peskinefe119512018-07-08 21:39:34 +02004186psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004187 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004188 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004189{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004190 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004191}
4192
Gilles Peskinefe119512018-07-08 21:39:34 +02004193psa_status_t psa_cipher_decrypt_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_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004198}
4199
Gilles Peskinefe119512018-07-08 21:39:34 +02004200psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004201 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004202 size_t iv_size,
4203 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004204{
itayzafrir534bd7c2018-08-02 13:56:32 +03004205 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004207 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004208 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004209 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004210 }
Moran Peker41deec42018-04-04 15:43:05 +03004211 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004212 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004213 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004214 goto exit;
4215 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004216 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4217 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004218 if( ret != 0 )
4219 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004220 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004221 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004222 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004223
mohammad16038481e742018-03-18 13:57:31 +02004224 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004225 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004226
Moran Peker395db872018-05-31 14:07:14 +03004227exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004228 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004229 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004230 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004231}
4232
Gilles Peskinefe119512018-07-08 21:39:34 +02004233psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004234 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004235 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004236{
itayzafrir534bd7c2018-08-02 13:56:32 +03004237 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004239 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004240 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004241 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004242 }
Moran Pekera28258c2018-05-29 16:25:04 +03004243 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004244 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004245 status = PSA_ERROR_INVALID_ARGUMENT;
4246 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004247 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004248 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4249 status = mbedtls_to_psa_error( ret );
4250exit:
4251 if( status == PSA_SUCCESS )
4252 operation->iv_set = 1;
4253 else
mohammad1603503973b2018-03-12 15:59:30 +02004254 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004255 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004256}
4257
Gilles Peskinee553c652018-06-04 16:22:46 +02004258psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4259 const uint8_t *input,
4260 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004261 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004262 size_t output_size,
4263 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004264{
itayzafrir534bd7c2018-08-02 13:56:32 +03004265 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004267 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004268
4269 if( operation->alg == 0 )
4270 {
4271 return( PSA_ERROR_BAD_STATE );
4272 }
4273
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004274 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004275 {
4276 /* Take the unprocessed partial block left over from previous
4277 * update calls, if any, plus the input to this call. Remove
4278 * the last partial block, if any. You get the data that will be
4279 * output in this call. */
4280 expected_output_size =
4281 ( operation->ctx.cipher.unprocessed_len + input_length )
4282 / operation->block_size * operation->block_size;
4283 }
4284 else
4285 {
4286 expected_output_size = input_length;
4287 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004288
Gilles Peskine89d789c2018-06-04 17:17:16 +02004289 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004290 {
4291 status = PSA_ERROR_BUFFER_TOO_SMALL;
4292 goto exit;
4293 }
mohammad160382759612018-03-12 18:16:40 +02004294
mohammad1603503973b2018-03-12 15:59:30 +02004295 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004296 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004297 status = mbedtls_to_psa_error( ret );
4298exit:
4299 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004300 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004301 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004302}
4303
Gilles Peskinee553c652018-06-04 16:22:46 +02004304psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4305 uint8_t *output,
4306 size_t output_size,
4307 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004308{
David Saadab4ecc272019-02-14 13:48:10 +02004309 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004310 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004311 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004312
mohammad1603503973b2018-03-12 15:59:30 +02004313 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004314 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004315 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004316 }
4317 if( operation->iv_required && ! operation->iv_set )
4318 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004319 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004320 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004321
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004322 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004323 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4324 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004325 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004326 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004327 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004328 }
4329
Janos Follath315b51c2018-07-09 16:04:51 +01004330 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4331 temp_output_buffer,
4332 output_length );
4333 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004334 {
Janos Follath315b51c2018-07-09 16:04:51 +01004335 status = mbedtls_to_psa_error( cipher_ret );
4336 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004337 }
Janos Follath315b51c2018-07-09 16:04:51 +01004338
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004339 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004340 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004341 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004342 memcpy( output, temp_output_buffer, *output_length );
4343 else
4344 {
Janos Follath315b51c2018-07-09 16:04:51 +01004345 status = PSA_ERROR_BUFFER_TOO_SMALL;
4346 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004347 }
mohammad1603503973b2018-03-12 15:59:30 +02004348
Gilles Peskine3f108122018-12-07 18:14:53 +01004349 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004350 status = psa_cipher_abort( operation );
4351
4352 return( status );
4353
4354error:
4355
4356 *output_length = 0;
4357
Gilles Peskine3f108122018-12-07 18:14:53 +01004358 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004359 (void) psa_cipher_abort( operation );
4360
4361 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004362}
4363
Gilles Peskinee553c652018-06-04 16:22:46 +02004364psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4365{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004366 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004367 {
4368 /* The object has (apparently) been initialized but it is not
4369 * in use. It's ok to call abort on such an object, and there's
4370 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004371 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004372 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004373
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004374 /* Sanity check (shouldn't happen: operation->alg should
4375 * always have been initialized to a valid value). */
4376 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4377 return( PSA_ERROR_BAD_STATE );
4378
mohammad1603503973b2018-03-12 15:59:30 +02004379 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004380
Moran Peker41deec42018-04-04 15:43:05 +03004381 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004382 operation->key_set = 0;
4383 operation->iv_set = 0;
4384 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004385 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004386 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004387
Moran Peker395db872018-05-31 14:07:14 +03004388 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004389}
4390
Gilles Peskinea0655c32018-04-30 17:06:50 +02004391
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004392
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004393
mohammad16035955c982018-04-26 00:53:03 +03004394/****************************************************************/
4395/* AEAD */
4396/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004397
Gilles Peskineedf9a652018-08-17 18:11:56 +02004398typedef struct
4399{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004400 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004401 const mbedtls_cipher_info_t *cipher_info;
4402 union
4403 {
4404#if defined(MBEDTLS_CCM_C)
4405 mbedtls_ccm_context ccm;
4406#endif /* MBEDTLS_CCM_C */
4407#if defined(MBEDTLS_GCM_C)
4408 mbedtls_gcm_context gcm;
4409#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004410#if defined(MBEDTLS_CHACHAPOLY_C)
4411 mbedtls_chachapoly_context chachapoly;
4412#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004413 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004414 psa_algorithm_t core_alg;
4415 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004416 uint8_t tag_length;
4417} aead_operation_t;
4418
Gilles Peskine30a9e412019-01-14 18:36:12 +01004419static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004420{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004421 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004422 {
4423#if defined(MBEDTLS_CCM_C)
4424 case PSA_ALG_CCM:
4425 mbedtls_ccm_free( &operation->ctx.ccm );
4426 break;
4427#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004428#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004429 case PSA_ALG_GCM:
4430 mbedtls_gcm_free( &operation->ctx.gcm );
4431 break;
4432#endif /* MBEDTLS_GCM_C */
4433 }
4434}
4435
4436static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004437 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004438 psa_key_usage_t usage,
4439 psa_algorithm_t alg )
4440{
4441 psa_status_t status;
4442 size_t key_bits;
4443 mbedtls_cipher_id_t cipher_id;
4444
Gilles Peskine28f8f302019-07-24 13:30:31 +02004445 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004446 if( status != PSA_SUCCESS )
4447 return( status );
4448
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004449 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004450
4451 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004452 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004453 &cipher_id );
4454 if( operation->cipher_info == NULL )
4455 return( PSA_ERROR_NOT_SUPPORTED );
4456
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004457 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004458 {
4459#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004460 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4461 operation->core_alg = PSA_ALG_CCM;
4462 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004463 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4464 * The call to mbedtls_ccm_encrypt_and_tag or
4465 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004466 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004467 return( PSA_ERROR_INVALID_ARGUMENT );
4468 mbedtls_ccm_init( &operation->ctx.ccm );
4469 status = mbedtls_to_psa_error(
4470 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004471 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004472 (unsigned int) key_bits ) );
4473 if( status != 0 )
4474 goto cleanup;
4475 break;
4476#endif /* MBEDTLS_CCM_C */
4477
4478#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004479 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4480 operation->core_alg = PSA_ALG_GCM;
4481 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004482 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4483 * The call to mbedtls_gcm_crypt_and_tag or
4484 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004485 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004486 return( PSA_ERROR_INVALID_ARGUMENT );
4487 mbedtls_gcm_init( &operation->ctx.gcm );
4488 status = mbedtls_to_psa_error(
4489 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004490 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004491 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004492 if( status != 0 )
4493 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004494 break;
4495#endif /* MBEDTLS_GCM_C */
4496
Gilles Peskine26869f22019-05-06 15:25:00 +02004497#if defined(MBEDTLS_CHACHAPOLY_C)
4498 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4499 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4500 operation->full_tag_length = 16;
4501 /* We only support the default tag length. */
4502 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4503 return( PSA_ERROR_NOT_SUPPORTED );
4504 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4505 status = mbedtls_to_psa_error(
4506 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004507 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004508 if( status != 0 )
4509 goto cleanup;
4510 break;
4511#endif /* MBEDTLS_CHACHAPOLY_C */
4512
Gilles Peskineedf9a652018-08-17 18:11:56 +02004513 default:
4514 return( PSA_ERROR_NOT_SUPPORTED );
4515 }
4516
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004517 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4518 {
4519 status = PSA_ERROR_INVALID_ARGUMENT;
4520 goto cleanup;
4521 }
4522 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004523
Gilles Peskineedf9a652018-08-17 18:11:56 +02004524 return( PSA_SUCCESS );
4525
4526cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004527 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004528 return( status );
4529}
4530
Gilles Peskinec5487a82018-12-03 18:08:14 +01004531psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004532 psa_algorithm_t alg,
4533 const uint8_t *nonce,
4534 size_t nonce_length,
4535 const uint8_t *additional_data,
4536 size_t additional_data_length,
4537 const uint8_t *plaintext,
4538 size_t plaintext_length,
4539 uint8_t *ciphertext,
4540 size_t ciphertext_size,
4541 size_t *ciphertext_length )
4542{
mohammad16035955c982018-04-26 00:53:03 +03004543 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004544 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004545 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004546
mohammad1603f08a5502018-06-03 15:05:47 +03004547 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004548
Gilles Peskinec5487a82018-12-03 18:08:14 +01004549 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004550 if( status != PSA_SUCCESS )
4551 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004552
Gilles Peskineedf9a652018-08-17 18:11:56 +02004553 /* For all currently supported modes, the tag is at the end of the
4554 * ciphertext. */
4555 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4556 {
4557 status = PSA_ERROR_BUFFER_TOO_SMALL;
4558 goto exit;
4559 }
4560 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004561
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004562#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004563 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004564 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004565 status = mbedtls_to_psa_error(
4566 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4567 MBEDTLS_GCM_ENCRYPT,
4568 plaintext_length,
4569 nonce, nonce_length,
4570 additional_data, additional_data_length,
4571 plaintext, ciphertext,
4572 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004573 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004574 else
4575#endif /* MBEDTLS_GCM_C */
4576#if defined(MBEDTLS_CCM_C)
4577 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004578 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004579 status = mbedtls_to_psa_error(
4580 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4581 plaintext_length,
4582 nonce, nonce_length,
4583 additional_data,
4584 additional_data_length,
4585 plaintext, ciphertext,
4586 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004587 }
mohammad16035c8845f2018-05-09 05:40:09 -07004588 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004589#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004590#if defined(MBEDTLS_CHACHAPOLY_C)
4591 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4592 {
4593 if( nonce_length != 12 || operation.tag_length != 16 )
4594 {
4595 status = PSA_ERROR_NOT_SUPPORTED;
4596 goto exit;
4597 }
4598 status = mbedtls_to_psa_error(
4599 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4600 plaintext_length,
4601 nonce,
4602 additional_data,
4603 additional_data_length,
4604 plaintext,
4605 ciphertext,
4606 tag ) );
4607 }
4608 else
4609#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004610 {
mohammad1603554faad2018-06-03 15:07:38 +03004611 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004612 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004613
Gilles Peskineedf9a652018-08-17 18:11:56 +02004614 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4615 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004616
Gilles Peskineedf9a652018-08-17 18:11:56 +02004617exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004618 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004619 if( status == PSA_SUCCESS )
4620 *ciphertext_length = plaintext_length + operation.tag_length;
4621 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004622}
4623
Gilles Peskineee652a32018-06-01 19:23:52 +02004624/* Locate the tag in a ciphertext buffer containing the encrypted data
4625 * followed by the tag. Return the length of the part preceding the tag in
4626 * *plaintext_length. This is the size of the plaintext in modes where
4627 * the encrypted data has the same size as the plaintext, such as
4628 * CCM and GCM. */
4629static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4630 const uint8_t *ciphertext,
4631 size_t ciphertext_length,
4632 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004633 const uint8_t **p_tag )
4634{
4635 size_t payload_length;
4636 if( tag_length > ciphertext_length )
4637 return( PSA_ERROR_INVALID_ARGUMENT );
4638 payload_length = ciphertext_length - tag_length;
4639 if( payload_length > plaintext_size )
4640 return( PSA_ERROR_BUFFER_TOO_SMALL );
4641 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004642 return( PSA_SUCCESS );
4643}
4644
Gilles Peskinec5487a82018-12-03 18:08:14 +01004645psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004646 psa_algorithm_t alg,
4647 const uint8_t *nonce,
4648 size_t nonce_length,
4649 const uint8_t *additional_data,
4650 size_t additional_data_length,
4651 const uint8_t *ciphertext,
4652 size_t ciphertext_length,
4653 uint8_t *plaintext,
4654 size_t plaintext_size,
4655 size_t *plaintext_length )
4656{
mohammad16035955c982018-04-26 00:53:03 +03004657 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004658 aead_operation_t operation;
4659 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004660
Gilles Peskineee652a32018-06-01 19:23:52 +02004661 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004662
Gilles Peskinec5487a82018-12-03 18:08:14 +01004663 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004664 if( status != PSA_SUCCESS )
4665 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004666
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004667 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4668 ciphertext, ciphertext_length,
4669 plaintext_size, &tag );
4670 if( status != PSA_SUCCESS )
4671 goto exit;
4672
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004673#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004674 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004675 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004676 status = mbedtls_to_psa_error(
4677 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4678 ciphertext_length - operation.tag_length,
4679 nonce, nonce_length,
4680 additional_data,
4681 additional_data_length,
4682 tag, operation.tag_length,
4683 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004684 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004685 else
4686#endif /* MBEDTLS_GCM_C */
4687#if defined(MBEDTLS_CCM_C)
4688 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004689 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004690 status = mbedtls_to_psa_error(
4691 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4692 ciphertext_length - operation.tag_length,
4693 nonce, nonce_length,
4694 additional_data,
4695 additional_data_length,
4696 ciphertext, plaintext,
4697 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004698 }
mohammad160339574652018-06-01 04:39:53 -07004699 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004700#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004701#if defined(MBEDTLS_CHACHAPOLY_C)
4702 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4703 {
4704 if( nonce_length != 12 || operation.tag_length != 16 )
4705 {
4706 status = PSA_ERROR_NOT_SUPPORTED;
4707 goto exit;
4708 }
4709 status = mbedtls_to_psa_error(
4710 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4711 ciphertext_length - operation.tag_length,
4712 nonce,
4713 additional_data,
4714 additional_data_length,
4715 tag,
4716 ciphertext,
4717 plaintext ) );
4718 }
4719 else
4720#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004721 {
mohammad1603554faad2018-06-03 15:07:38 +03004722 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004723 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004724
Gilles Peskineedf9a652018-08-17 18:11:56 +02004725 if( status != PSA_SUCCESS && plaintext_size != 0 )
4726 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004727
Gilles Peskineedf9a652018-08-17 18:11:56 +02004728exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004729 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004730 if( status == PSA_SUCCESS )
4731 *plaintext_length = ciphertext_length - operation.tag_length;
4732 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004733}
4734
Gilles Peskinea0655c32018-04-30 17:06:50 +02004735
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004736
Gilles Peskine20035e32018-02-03 22:44:14 +01004737/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004738/* Generators */
4739/****************************************************************/
4740
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004741#define HKDF_STATE_INIT 0 /* no input yet */
4742#define HKDF_STATE_STARTED 1 /* got salt */
4743#define HKDF_STATE_KEYED 2 /* got key */
4744#define HKDF_STATE_OUTPUT 3 /* output started */
4745
Gilles Peskinecbe66502019-05-16 16:59:18 +02004746static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004748{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4750 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004751 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004753}
4754
4755
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004756psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004757{
4758 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004760 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004761 {
4762 /* The object has (apparently) been initialized but it is not
4763 * in use. It's ok to call abort on such an object, and there's
4764 * nothing to do. */
4765 }
4766 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004767#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004768 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004769 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004770 mbedtls_free( operation->ctx.hkdf.info );
4771 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004772 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004773 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004774 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004775 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004776 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004777 if( operation->ctx.tls12_prf.seed != NULL )
4778 {
4779 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4780 operation->ctx.tls12_prf.seed_length );
4781 mbedtls_free( operation->ctx.tls12_prf.seed );
4782 }
4783
4784 if( operation->ctx.tls12_prf.label != NULL )
4785 {
4786 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4787 operation->ctx.tls12_prf.label_length );
4788 mbedtls_free( operation->ctx.tls12_prf.label );
4789 }
4790
4791 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4792
4793 /* We leave the fields Ai and output_block to be erased safely by the
4794 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004795 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004796 else
4797#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004798 {
4799 status = PSA_ERROR_BAD_STATE;
4800 }
Janos Follath083036a2019-06-11 10:22:26 +01004801 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004802 return( status );
4803}
4804
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004806 size_t *capacity)
4807{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004809 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004811 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004812 }
4813
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004814 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004815 return( PSA_SUCCESS );
4816}
4817
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004818psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004819 size_t capacity )
4820{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004821 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004822 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004823 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004824 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004825 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004826 return( PSA_SUCCESS );
4827}
4828
Gilles Peskinebef7f142018-07-12 17:22:21 +02004829#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004831 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004832static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004833 psa_algorithm_t hash_alg,
4834 uint8_t *output,
4835 size_t output_length )
4836{
4837 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4838 psa_status_t status;
4839
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004840 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4841 return( PSA_ERROR_BAD_STATE );
4842 hkdf->state = HKDF_STATE_OUTPUT;
4843
Gilles Peskinebef7f142018-07-12 17:22:21 +02004844 while( output_length != 0 )
4845 {
4846 /* Copy what remains of the current block */
4847 uint8_t n = hash_length - hkdf->offset_in_block;
4848 if( n > output_length )
4849 n = (uint8_t) output_length;
4850 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4851 output += n;
4852 output_length -= n;
4853 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004854 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004855 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004856 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004857 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004859 * object was corrupted or if this function is called directly
4860 * inside the library. */
4861 if( hkdf->block_number == 0xff )
4862 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004863
4864 /* We need a new block */
4865 ++hkdf->block_number;
4866 hkdf->offset_in_block = 0;
4867 status = psa_hmac_setup_internal( &hkdf->hmac,
4868 hkdf->prk, hash_length,
4869 hash_alg );
4870 if( status != PSA_SUCCESS )
4871 return( status );
4872 if( hkdf->block_number != 1 )
4873 {
4874 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4875 hkdf->output_block,
4876 hash_length );
4877 if( status != PSA_SUCCESS )
4878 return( status );
4879 }
4880 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4881 hkdf->info,
4882 hkdf->info_length );
4883 if( status != PSA_SUCCESS )
4884 return( status );
4885 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4886 &hkdf->block_number, 1 );
4887 if( status != PSA_SUCCESS )
4888 return( status );
4889 status = psa_hmac_finish_internal( &hkdf->hmac,
4890 hkdf->output_block,
4891 sizeof( hkdf->output_block ) );
4892 if( status != PSA_SUCCESS )
4893 return( status );
4894 }
4895
4896 return( PSA_SUCCESS );
4897}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004898
Janos Follath7742fee2019-06-17 12:58:10 +01004899static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4900 psa_tls12_prf_key_derivation_t *tls12_prf,
4901 psa_algorithm_t alg )
4902{
4903 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4904 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004905 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4906 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004907
Janos Follath7742fee2019-06-17 12:58:10 +01004908 /* We can't be wanting more output after block 0xff, otherwise
4909 * the capacity check in psa_key_derivation_output_bytes() would have
4910 * prevented this call. It could happen only if the operation
4911 * object was corrupted or if this function is called directly
4912 * inside the library. */
4913 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004914 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004915
4916 /* We need a new block */
4917 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004918 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004919
4920 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4921 *
4922 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4923 *
4924 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4925 * HMAC_hash(secret, A(2) + seed) +
4926 * HMAC_hash(secret, A(3) + seed) + ...
4927 *
4928 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004929 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004930 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004931 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004932 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004933 * is currently extracted as `output_block` and where i is
4934 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004935 */
4936
Janos Follathea29bfb2019-06-19 12:21:20 +01004937 /* Save the hash context before using it, to preserve the hash state with
4938 * only the inner padding in it. We need this, because inner padding depends
4939 * on the key (secret in the RFC's terminology). */
4940 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4941 if( status != PSA_SUCCESS )
4942 goto cleanup;
4943
4944 /* Calculate A(i) where i = tls12_prf->block_number. */
4945 if( tls12_prf->block_number == 1 )
4946 {
4947 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4948 * the variable seed and in this instance means it in the context of the
4949 * P_hash function, where seed = label + seed.) */
4950 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4951 tls12_prf->label, tls12_prf->label_length );
4952 if( status != PSA_SUCCESS )
4953 goto cleanup;
4954 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4955 tls12_prf->seed, tls12_prf->seed_length );
4956 if( status != PSA_SUCCESS )
4957 goto cleanup;
4958 }
4959 else
4960 {
4961 /* A(i) = HMAC_hash(secret, A(i-1)) */
4962 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4963 tls12_prf->Ai, hash_length );
4964 if( status != PSA_SUCCESS )
4965 goto cleanup;
4966 }
4967
4968 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4969 tls12_prf->Ai, hash_length );
4970 if( status != PSA_SUCCESS )
4971 goto cleanup;
4972 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4973 if( status != PSA_SUCCESS )
4974 goto cleanup;
4975
4976 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4977 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4978 tls12_prf->Ai, hash_length );
4979 if( status != PSA_SUCCESS )
4980 goto cleanup;
4981 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4982 tls12_prf->label, tls12_prf->label_length );
4983 if( status != PSA_SUCCESS )
4984 goto cleanup;
4985 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4986 tls12_prf->seed, tls12_prf->seed_length );
4987 if( status != PSA_SUCCESS )
4988 goto cleanup;
4989 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4990 tls12_prf->output_block, hash_length );
4991 if( status != PSA_SUCCESS )
4992 goto cleanup;
4993 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4994 if( status != PSA_SUCCESS )
4995 goto cleanup;
4996
Janos Follath7742fee2019-06-17 12:58:10 +01004997
4998cleanup:
4999
Janos Follathea29bfb2019-06-19 12:21:20 +01005000 cleanup_status = psa_hash_abort( &backup );
5001 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5002 status = cleanup_status;
5003
5004 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005005}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005006
Janos Follath844eb0e2019-06-19 12:10:49 +01005007static psa_status_t psa_key_derivation_tls12_prf_read(
5008 psa_tls12_prf_key_derivation_t *tls12_prf,
5009 psa_algorithm_t alg,
5010 uint8_t *output,
5011 size_t output_length )
5012{
5013 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5014 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5015 psa_status_t status;
5016 uint8_t offset, length;
5017
5018 while( output_length != 0 )
5019 {
5020 /* Check if we have fully processed the current block. */
5021 if( tls12_prf->left_in_block == 0 )
5022 {
5023 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5024 alg );
5025 if( status != PSA_SUCCESS )
5026 return( status );
5027
5028 continue;
5029 }
5030
5031 if( tls12_prf->left_in_block > output_length )
5032 length = (uint8_t) output_length;
5033 else
5034 length = tls12_prf->left_in_block;
5035
5036 offset = hash_length - tls12_prf->left_in_block;
5037 memcpy( output, tls12_prf->output_block + offset, length );
5038 output += length;
5039 output_length -= length;
5040 tls12_prf->left_in_block -= length;
5041 }
5042
5043 return( PSA_SUCCESS );
5044}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005045#endif /* MBEDTLS_MD_C */
5046
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005047psa_status_t psa_key_derivation_output_bytes(
5048 psa_key_derivation_operation_t *operation,
5049 uint8_t *output,
5050 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005051{
5052 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005054
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005055 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005056 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005057 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005058 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005059 }
5060
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005061 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005062 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005064 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005065 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005066 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005067 goto exit;
5068 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005069 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005070 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005071 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005072 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005073 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5074 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005075 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005076 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005077 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005078 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005079 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005080
Gilles Peskinebef7f142018-07-12 17:22:21 +02005081#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005082 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005083 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005084 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005085 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005086 output, output_length );
5087 }
Janos Follathadbec812019-06-14 11:05:39 +01005088 else
Janos Follathadbec812019-06-14 11:05:39 +01005089 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005090 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005091 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005092 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005093 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005094 output_length );
5095 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005096 else
5097#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005098 {
5099 return( PSA_ERROR_BAD_STATE );
5100 }
5101
5102exit:
5103 if( status != PSA_SUCCESS )
5104 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005105 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005106 * This allows us to differentiate between exhausted operations and
5107 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5108 * operations. */
5109 psa_algorithm_t alg = operation->alg;
5110 psa_key_derivation_abort( operation );
5111 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005112 memset( output, '!', output_length );
5113 }
5114 return( status );
5115}
5116
Gilles Peskine08542d82018-07-19 17:05:42 +02005117#if defined(MBEDTLS_DES_C)
5118static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5119{
5120 if( data_size >= 8 )
5121 mbedtls_des_key_set_parity( data );
5122 if( data_size >= 16 )
5123 mbedtls_des_key_set_parity( data + 8 );
5124 if( data_size >= 24 )
5125 mbedtls_des_key_set_parity( data + 16 );
5126}
5127#endif /* MBEDTLS_DES_C */
5128
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005129static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005130 psa_key_slot_t *slot,
5131 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005132 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005133{
5134 uint8_t *data = NULL;
5135 size_t bytes = PSA_BITS_TO_BYTES( bits );
5136 psa_status_t status;
5137
Gilles Peskine8e338702019-07-30 20:06:31 +02005138 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005139 return( PSA_ERROR_INVALID_ARGUMENT );
5140 if( bits % 8 != 0 )
5141 return( PSA_ERROR_INVALID_ARGUMENT );
5142 data = mbedtls_calloc( 1, bytes );
5143 if( data == NULL )
5144 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5145
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005146 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005147 if( status != PSA_SUCCESS )
5148 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005149#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005150 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005151 psa_des_set_key_parity( data, bytes );
5152#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005153 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005154
5155exit:
5156 mbedtls_free( data );
5157 return( status );
5158}
5159
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005160psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005161 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005162 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005163{
5164 psa_status_t status;
5165 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005166 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005167
5168 /* Reject any attempt to create a zero-length key so that we don't
5169 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5170 if( psa_get_key_bits( attributes ) == 0 )
5171 return( PSA_ERROR_INVALID_ARGUMENT );
5172
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005173 if( ! operation->can_output_key )
5174 return( PSA_ERROR_NOT_PERMITTED );
5175
Gilles Peskinedf179142019-07-15 22:02:14 +02005176 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5177 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005178#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5179 if( driver != NULL )
5180 {
5181 /* Deriving a key in a secure element is not implemented yet. */
5182 status = PSA_ERROR_NOT_SUPPORTED;
5183 }
5184#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005185 if( status == PSA_SUCCESS )
5186 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005187 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005188 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005189 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005190 }
5191 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005192 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005193 if( status != PSA_SUCCESS )
5194 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005195 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005196 *handle = 0;
5197 }
5198 return( status );
5199}
5200
Gilles Peskineeab56e42018-07-12 17:12:33 +02005201
5202
5203/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005204/* Key derivation */
5205/****************************************************************/
5206
Gilles Peskine969c5d62019-01-16 15:53:06 +01005207static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005209 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005210{
Janos Follath5fe19732019-06-20 15:09:30 +01005211 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5212 * initialisers for this union leave some bytes unspecified.) */
5213 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5214
Gilles Peskine969c5d62019-01-16 15:53:06 +01005215 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005216#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005217 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5218 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5219 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005220 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005221 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005222 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5223 if( hash_size == 0 )
5224 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005225 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5226 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005227 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005228 {
5229 return( PSA_ERROR_NOT_SUPPORTED );
5230 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005231 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005232 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005233 }
5234#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005235 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005236 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005237}
5238
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005239psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005240 psa_algorithm_t alg )
5241{
5242 psa_status_t status;
5243
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005244 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005245 return( PSA_ERROR_BAD_STATE );
5246
Gilles Peskine6843c292019-01-18 16:44:49 +01005247 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5248 return( PSA_ERROR_INVALID_ARGUMENT );
5249 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005250 {
5251 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005252 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005253 }
5254 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5255 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005257 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005258 else
5259 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005260
5261 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005262 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005263 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005264}
5265
5266#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005267static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005268 psa_algorithm_t hash_alg,
5269 psa_key_derivation_step_t step,
5270 const uint8_t *data,
5271 size_t data_length )
5272{
5273 psa_status_t status;
5274 switch( step )
5275 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005276 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005277 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005278 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005279 status = psa_hmac_setup_internal( &hkdf->hmac,
5280 data, data_length,
5281 hash_alg );
5282 if( status != PSA_SUCCESS )
5283 return( status );
5284 hkdf->state = HKDF_STATE_STARTED;
5285 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005286 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005287 /* If no salt was provided, use an empty salt. */
5288 if( hkdf->state == HKDF_STATE_INIT )
5289 {
5290 status = psa_hmac_setup_internal( &hkdf->hmac,
5291 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005292 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005293 if( status != PSA_SUCCESS )
5294 return( status );
5295 hkdf->state = HKDF_STATE_STARTED;
5296 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005297 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005298 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005299 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5300 data, data_length );
5301 if( status != PSA_SUCCESS )
5302 return( status );
5303 status = psa_hmac_finish_internal( &hkdf->hmac,
5304 hkdf->prk,
5305 sizeof( hkdf->prk ) );
5306 if( status != PSA_SUCCESS )
5307 return( status );
5308 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5309 hkdf->block_number = 0;
5310 hkdf->state = HKDF_STATE_KEYED;
5311 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005312 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005313 if( hkdf->state == HKDF_STATE_OUTPUT )
5314 return( PSA_ERROR_BAD_STATE );
5315 if( hkdf->info_set )
5316 return( PSA_ERROR_BAD_STATE );
5317 hkdf->info_length = data_length;
5318 if( data_length != 0 )
5319 {
5320 hkdf->info = mbedtls_calloc( 1, data_length );
5321 if( hkdf->info == NULL )
5322 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5323 memcpy( hkdf->info, data, data_length );
5324 }
5325 hkdf->info_set = 1;
5326 return( PSA_SUCCESS );
5327 default:
5328 return( PSA_ERROR_INVALID_ARGUMENT );
5329 }
5330}
Janos Follathb03233e2019-06-11 15:30:30 +01005331
Janos Follathf08e2652019-06-13 09:05:41 +01005332static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5333 const uint8_t *data,
5334 size_t data_length )
5335{
5336 if( prf->state != TLS12_PRF_STATE_INIT )
5337 return( PSA_ERROR_BAD_STATE );
5338
Janos Follathd6dce9f2019-07-04 09:11:38 +01005339 if( data_length != 0 )
5340 {
5341 prf->seed = mbedtls_calloc( 1, data_length );
5342 if( prf->seed == NULL )
5343 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005344
Janos Follathd6dce9f2019-07-04 09:11:38 +01005345 memcpy( prf->seed, data, data_length );
5346 prf->seed_length = data_length;
5347 }
Janos Follathf08e2652019-06-13 09:05:41 +01005348
5349 prf->state = TLS12_PRF_STATE_SEED_SET;
5350
5351 return( PSA_SUCCESS );
5352}
5353
Janos Follath81550542019-06-13 14:26:34 +01005354static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5355 psa_algorithm_t hash_alg,
5356 const uint8_t *data,
5357 size_t data_length )
5358{
5359 psa_status_t status;
5360 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5361 return( PSA_ERROR_BAD_STATE );
5362
5363 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5364 if( status != PSA_SUCCESS )
5365 return( status );
5366
5367 prf->state = TLS12_PRF_STATE_KEY_SET;
5368
5369 return( PSA_SUCCESS );
5370}
5371
Janos Follath6660f0e2019-06-17 08:44:03 +01005372static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5373 psa_tls12_prf_key_derivation_t *prf,
5374 psa_algorithm_t hash_alg,
5375 const uint8_t *data,
5376 size_t data_length )
5377{
5378 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005379 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5380 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005381
5382 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5383 return( PSA_ERROR_INVALID_ARGUMENT );
5384
5385 /* Quoting RFC 4279, Section 2:
5386 *
5387 * The premaster secret is formed as follows: if the PSK is N octets
5388 * long, concatenate a uint16 with the value N, N zero octets, a second
5389 * uint16 with the value N, and the PSK itself.
5390 */
5391
Janos Follath40e13932019-06-26 13:22:29 +01005392 *cur++ = ( data_length >> 8 ) & 0xff;
5393 *cur++ = ( data_length >> 0 ) & 0xff;
5394 memset( cur, 0, data_length );
5395 cur += data_length;
5396 *cur++ = pms[0];
5397 *cur++ = pms[1];
5398 memcpy( cur, data, data_length );
5399 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005400
Janos Follath40e13932019-06-26 13:22:29 +01005401 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005402
5403 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5404 return( status );
5405}
5406
Janos Follath63028dd2019-06-13 09:15:47 +01005407static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5408 const uint8_t *data,
5409 size_t data_length )
5410{
5411 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5412 return( PSA_ERROR_BAD_STATE );
5413
Janos Follathd6dce9f2019-07-04 09:11:38 +01005414 if( data_length != 0 )
5415 {
5416 prf->label = mbedtls_calloc( 1, data_length );
5417 if( prf->label == NULL )
5418 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005419
Janos Follathd6dce9f2019-07-04 09:11:38 +01005420 memcpy( prf->label, data, data_length );
5421 prf->label_length = data_length;
5422 }
Janos Follath63028dd2019-06-13 09:15:47 +01005423
5424 prf->state = TLS12_PRF_STATE_LABEL_SET;
5425
5426 return( PSA_SUCCESS );
5427}
5428
Janos Follathb03233e2019-06-11 15:30:30 +01005429static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5430 psa_algorithm_t hash_alg,
5431 psa_key_derivation_step_t step,
5432 const uint8_t *data,
5433 size_t data_length )
5434{
Janos Follathb03233e2019-06-11 15:30:30 +01005435 switch( step )
5436 {
Janos Follathf08e2652019-06-13 09:05:41 +01005437 case PSA_KEY_DERIVATION_INPUT_SEED:
5438 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005439 case PSA_KEY_DERIVATION_INPUT_SECRET:
5440 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005441 case PSA_KEY_DERIVATION_INPUT_LABEL:
5442 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005443 default:
5444 return( PSA_ERROR_INVALID_ARGUMENT );
5445 }
5446}
Janos Follath6660f0e2019-06-17 08:44:03 +01005447
5448static psa_status_t psa_tls12_prf_psk_to_ms_input(
5449 psa_tls12_prf_key_derivation_t *prf,
5450 psa_algorithm_t hash_alg,
5451 psa_key_derivation_step_t step,
5452 const uint8_t *data,
5453 size_t data_length )
5454{
5455 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005456 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005457 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5458 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005459 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005460
5461 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5462}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005463#endif /* MBEDTLS_MD_C */
5464
Gilles Peskineb8965192019-09-24 16:21:10 +02005465/** Check whether the given key type is acceptable for the given
5466 * input step of a key derivation.
5467 *
5468 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5469 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5470 * Both secret and non-secret inputs can alternatively have the type
5471 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5472 * that the input was passed as a buffer rather than via a key object.
5473 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005474static int psa_key_derivation_check_input_type(
5475 psa_key_derivation_step_t step,
5476 psa_key_type_t key_type )
5477{
5478 switch( step )
5479 {
5480 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005481 if( key_type == PSA_KEY_TYPE_DERIVE )
5482 return( PSA_SUCCESS );
5483 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005484 return( PSA_SUCCESS );
5485 break;
5486 case PSA_KEY_DERIVATION_INPUT_LABEL:
5487 case PSA_KEY_DERIVATION_INPUT_SALT:
5488 case PSA_KEY_DERIVATION_INPUT_INFO:
5489 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005490 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5491 return( PSA_SUCCESS );
5492 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005493 return( PSA_SUCCESS );
5494 break;
5495 }
5496 return( PSA_ERROR_INVALID_ARGUMENT );
5497}
5498
Janos Follathb80a94e2019-06-12 15:54:46 +01005499static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005500 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005501 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005502 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005503 const uint8_t *data,
5504 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005505{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005506 psa_status_t status;
5507 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5508
5509 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005510 if( status != PSA_SUCCESS )
5511 goto exit;
5512
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005513#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005514 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005515 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005516 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005517 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005518 step, data, data_length );
5519 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005520 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005521 {
Janos Follathb03233e2019-06-11 15:30:30 +01005522 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5523 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5524 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005525 }
5526 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5527 {
5528 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5529 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5530 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005531 }
5532 else
5533#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005534 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005535 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005536 return( PSA_ERROR_BAD_STATE );
5537 }
5538
Gilles Peskine224b0d62019-09-23 18:13:17 +02005539exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005540 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005541 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005542 return( status );
5543}
5544
Janos Follath51f4a0f2019-06-14 11:35:55 +01005545psa_status_t psa_key_derivation_input_bytes(
5546 psa_key_derivation_operation_t *operation,
5547 psa_key_derivation_step_t step,
5548 const uint8_t *data,
5549 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005550{
Gilles Peskineb8965192019-09-24 16:21:10 +02005551 return( psa_key_derivation_input_internal( operation, step,
5552 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005553 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005554}
5555
Janos Follath51f4a0f2019-06-14 11:35:55 +01005556psa_status_t psa_key_derivation_input_key(
5557 psa_key_derivation_operation_t *operation,
5558 psa_key_derivation_step_t step,
5559 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005560{
5561 psa_key_slot_t *slot;
5562 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005563
Gilles Peskine28f8f302019-07-24 13:30:31 +02005564 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005565 PSA_KEY_USAGE_DERIVE,
5566 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005567 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005568 {
5569 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005570 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005571 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005572
5573 /* Passing a key object as a SECRET input unlocks the permission
5574 * to output to a key object. */
5575 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5576 operation->can_output_key = 1;
5577
Janos Follathb80a94e2019-06-12 15:54:46 +01005578 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005579 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005580 slot->data.key.data,
5581 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005582}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005583
5584
5585
5586/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005587/* Key agreement */
5588/****************************************************************/
5589
Gilles Peskinea05219c2018-11-16 16:02:56 +01005590#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005591static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5592 size_t peer_key_length,
5593 const mbedtls_ecp_keypair *our_key,
5594 uint8_t *shared_secret,
5595 size_t shared_secret_size,
5596 size_t *shared_secret_length )
5597{
Steven Cooremana2371e52020-07-28 14:30:39 +02005598 mbedtls_ecp_keypair *their_key;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005599 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005600 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005601 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005602 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005603 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005604
Steven Cooreman4fed4552020-08-03 14:46:03 +02005605 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5606 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005607 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005608 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005609 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005610 goto exit;
5611
Jaeden Amero97271b32019-01-10 19:38:51 +00005612 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005613 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005614 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005615 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005616 status = mbedtls_to_psa_error(
5617 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5618 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005619 goto exit;
5620
Jaeden Amero97271b32019-01-10 19:38:51 +00005621 status = mbedtls_to_psa_error(
5622 mbedtls_ecdh_calc_secret( &ecdh,
5623 shared_secret_length,
5624 shared_secret, shared_secret_size,
5625 mbedtls_ctr_drbg_random,
5626 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005627 if( status != PSA_SUCCESS )
5628 goto exit;
5629 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5630 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005631
5632exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005633 if( status != PSA_SUCCESS )
5634 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005635 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005636 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005637 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005638
Jaeden Amero97271b32019-01-10 19:38:51 +00005639 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005640}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005641#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005642
Gilles Peskine01d718c2018-09-18 12:01:02 +02005643#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5644
Gilles Peskine0216fe12019-04-11 21:23:21 +02005645static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5646 psa_key_slot_t *private_key,
5647 const uint8_t *peer_key,
5648 size_t peer_key_length,
5649 uint8_t *shared_secret,
5650 size_t shared_secret_size,
5651 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005652{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005653 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005654 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005655#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005656 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005657 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005658 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005659 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005660 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005661 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005662 private_key->data.key.data,
5663 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005664 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005665 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005666 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005667 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005668 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005669 shared_secret, shared_secret_size,
5670 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005671 mbedtls_ecp_keypair_free( ecp );
5672 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005673 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005674#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005675 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005676 (void) private_key;
5677 (void) peer_key;
5678 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005679 (void) shared_secret;
5680 (void) shared_secret_size;
5681 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005682 return( PSA_ERROR_NOT_SUPPORTED );
5683 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005684}
5685
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005686/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005687 * to potentially free embedded data structures and wipe confidential data.
5688 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005689static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005690 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005691 psa_key_slot_t *private_key,
5692 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005693 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005694{
5695 psa_status_t status;
5696 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5697 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005698 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005699
5700 /* Step 1: run the secret agreement algorithm to generate the shared
5701 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005702 status = psa_key_agreement_raw_internal( ka_alg,
5703 private_key,
5704 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005705 shared_secret,
5706 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005707 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005708 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005709 goto exit;
5710
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005711 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005712 * the shared secret. A shared secret is permitted wherever a key
5713 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005714 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005715 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005716 shared_secret,
5717 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005718
Gilles Peskine12313cd2018-06-20 00:20:32 +02005719exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005720 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005721 return( status );
5722}
5723
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005724psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005725 psa_key_derivation_step_t step,
5726 psa_key_handle_t private_key,
5727 const uint8_t *peer_key,
5728 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005729{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005730 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005731 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005732 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005733 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005734 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005735 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005736 if( status != PSA_SUCCESS )
5737 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005738 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005739 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005740 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005741 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005742 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005743 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005744}
5745
Gilles Peskinebe697d82019-05-16 18:00:41 +02005746psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5747 psa_key_handle_t private_key,
5748 const uint8_t *peer_key,
5749 size_t peer_key_length,
5750 uint8_t *output,
5751 size_t output_size,
5752 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005753{
5754 psa_key_slot_t *slot;
5755 psa_status_t status;
5756
5757 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5758 {
5759 status = PSA_ERROR_INVALID_ARGUMENT;
5760 goto exit;
5761 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005762 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005763 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005764 if( status != PSA_SUCCESS )
5765 goto exit;
5766
5767 status = psa_key_agreement_raw_internal( alg, slot,
5768 peer_key, peer_key_length,
5769 output, output_size,
5770 output_length );
5771
5772exit:
5773 if( status != PSA_SUCCESS )
5774 {
5775 /* If an error happens and is not handled properly, the output
5776 * may be used as a key to protect sensitive data. Arrange for such
5777 * a key to be random, which is likely to result in decryption or
5778 * verification errors. This is better than filling the buffer with
5779 * some constant data such as zeros, which would result in the data
5780 * being protected with a reproducible, easily knowable key.
5781 */
5782 psa_generate_random( output, output_size );
5783 *output_length = output_size;
5784 }
5785 return( status );
5786}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005787
5788
5789/****************************************************************/
5790/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005791/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005792
Gilles Peskine4c317f42018-07-12 01:24:09 +02005793psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005794 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005795{
Janos Follath24eed8d2019-11-22 13:21:35 +00005796 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005797 GUARD_MODULE_INITIALIZED;
5798
Gilles Peskinef181eca2019-08-07 13:49:00 +02005799 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5800 {
5801 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5802 output,
5803 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5804 if( ret != 0 )
5805 return( mbedtls_to_psa_error( ret ) );
5806 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5807 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5808 }
5809
Gilles Peskinee59236f2018-01-27 23:32:46 +01005810 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5811 return( mbedtls_to_psa_error( ret ) );
5812}
5813
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005814#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5815#include "mbedtls/entropy_poll.h"
5816
Gilles Peskine7228da22019-07-15 11:06:15 +02005817psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005818 size_t seed_size )
5819{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005820 if( global_data.initialized )
5821 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005822
5823 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5824 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5825 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5826 return( PSA_ERROR_INVALID_ARGUMENT );
5827
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005828 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005829}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005830#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005831
Gilles Peskinee56e8782019-04-26 17:34:02 +02005832#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5833static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5834 size_t domain_parameters_size,
5835 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005836{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005837 size_t i;
5838 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005839
Gilles Peskinee56e8782019-04-26 17:34:02 +02005840 if( domain_parameters_size == 0 )
5841 {
5842 *exponent = 65537;
5843 return( PSA_SUCCESS );
5844 }
5845
5846 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5847 * support values that fit in a 32-bit integer, which is larger than
5848 * int on just about every platform anyway. */
5849 if( domain_parameters_size > sizeof( acc ) )
5850 return( PSA_ERROR_NOT_SUPPORTED );
5851 for( i = 0; i < domain_parameters_size; i++ )
5852 acc = ( acc << 8 ) | domain_parameters[i];
5853 if( acc > INT_MAX )
5854 return( PSA_ERROR_NOT_SUPPORTED );
5855 *exponent = acc;
5856 return( PSA_SUCCESS );
5857}
5858#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5859
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005860static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005861 psa_key_slot_t *slot, size_t bits,
5862 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005863{
Gilles Peskine8e338702019-07-30 20:06:31 +02005864 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005865
Gilles Peskinee56e8782019-04-26 17:34:02 +02005866 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005867 return( PSA_ERROR_INVALID_ARGUMENT );
5868
Gilles Peskinee59236f2018-01-27 23:32:46 +01005869 if( key_type_is_raw_bytes( type ) )
5870 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005871 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005872
5873 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005874 if( status != PSA_SUCCESS )
5875 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005876
5877 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02005878 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
5879 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005880 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005881
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005882 status = psa_generate_random( slot->data.key.data,
5883 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005884 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005885 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005886
5887 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005888#if defined(MBEDTLS_DES_C)
5889 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005890 psa_des_set_key_parity( slot->data.key.data,
5891 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005892#endif /* MBEDTLS_DES_C */
5893 }
5894 else
5895
5896#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005897 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005898 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005899 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005901 int exponent;
5902 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005903 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5904 return( PSA_ERROR_NOT_SUPPORTED );
5905 /* Accept only byte-aligned keys, for the same reasons as
5906 * in psa_import_rsa_key(). */
5907 if( bits % 8 != 0 )
5908 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005909 status = psa_read_rsa_exponent( domain_parameters,
5910 domain_parameters_size,
5911 &exponent );
5912 if( status != PSA_SUCCESS )
5913 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005914 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5915 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005916 mbedtls_ctr_drbg_random,
5917 &global_data.ctr_drbg,
5918 (unsigned int) bits,
5919 exponent );
5920 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005921 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005922
5923 /* Make sure to always have an export representation available */
5924 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5925
Steven Cooreman75b74362020-07-28 14:30:13 +02005926 status = psa_allocate_buffer_to_slot( slot, bytes );
5927 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005928 {
5929 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02005930 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005931 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005932
5933 status = psa_export_rsa_key( type,
5934 &rsa,
5935 slot->data.key.data,
5936 bytes,
5937 &slot->data.key.bytes );
5938 mbedtls_rsa_free( &rsa );
5939 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005940 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005941 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005942 }
5943 else
5944#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5945
5946#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005947 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005948 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005949 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005950 mbedtls_ecp_group_id grp_id =
5951 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005952 const mbedtls_ecp_curve_info *curve_info =
5953 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02005954 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005955 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005956 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005957 return( PSA_ERROR_NOT_SUPPORTED );
5958 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5959 return( PSA_ERROR_NOT_SUPPORTED );
5960 if( curve_info->bit_size != bits )
5961 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005962 mbedtls_ecp_keypair_init( &ecp );
5963 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005964 mbedtls_ctr_drbg_random,
5965 &global_data.ctr_drbg );
5966 if( ret != 0 )
5967 {
Steven Cooremanacda8342020-07-24 23:09:52 +02005968 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005969 return( mbedtls_to_psa_error( ret ) );
5970 }
Steven Cooremanacda8342020-07-24 23:09:52 +02005971
5972
5973 /* Make sure to always have an export representation available */
5974 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02005975 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
5976 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005977 {
5978 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005979 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005980 }
Steven Cooreman75b74362020-07-28 14:30:13 +02005981
5982 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02005983 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
5984
5985 mbedtls_ecp_keypair_free( &ecp );
5986 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005987 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005988 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005989 }
5990 else
5991#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02005992 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005993 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02005994 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005995
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005996 return( PSA_SUCCESS );
5997}
Darryl Green0c6575a2018-11-07 16:05:30 +00005998
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005999psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006000 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006001{
6002 psa_status_t status;
6003 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006004 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006005
Gilles Peskine0f84d622019-09-12 19:03:13 +02006006 /* Reject any attempt to create a zero-length key so that we don't
6007 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6008 if( psa_get_key_bits( attributes ) == 0 )
6009 return( PSA_ERROR_INVALID_ARGUMENT );
6010
Gilles Peskinedf179142019-07-15 22:02:14 +02006011 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6012 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006013 if( status != PSA_SUCCESS )
6014 goto exit;
6015
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006016#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6017 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00006018 {
Gilles Peskine11792082019-08-06 18:36:36 +02006019 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
6020 size_t pubkey_length = 0; /* We don't support this feature yet */
6021 if( drv->key_management == NULL ||
6022 drv->key_management->p_generate == NULL )
6023 {
6024 status = PSA_ERROR_NOT_SUPPORTED;
6025 goto exit;
6026 }
6027 status = drv->key_management->p_generate(
6028 psa_get_se_driver_context( driver ),
6029 slot->data.se.slot_number, attributes,
6030 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006031 }
Gilles Peskine11792082019-08-06 18:36:36 +02006032 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006033#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006034 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006035 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02006036 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006037 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006038 }
Gilles Peskine11792082019-08-06 18:36:36 +02006039
6040exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006041 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006042 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006043 if( status != PSA_SUCCESS )
6044 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006045 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006046 *handle = 0;
6047 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006048 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006049}
6050
6051
Gilles Peskinee59236f2018-01-27 23:32:46 +01006052
6053/****************************************************************/
6054/* Module setup */
6055/****************************************************************/
6056
Gilles Peskine5e769522018-11-20 21:59:56 +01006057psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6058 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6059 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6060{
6061 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6062 return( PSA_ERROR_BAD_STATE );
6063 global_data.entropy_init = entropy_init;
6064 global_data.entropy_free = entropy_free;
6065 return( PSA_SUCCESS );
6066}
6067
Gilles Peskinee59236f2018-01-27 23:32:46 +01006068void mbedtls_psa_crypto_free( void )
6069{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006070 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006071 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6072 {
6073 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006074 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006075 }
6076 /* Wipe all remaining data, including configuration.
6077 * In particular, this sets all state indicator to the value
6078 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006079 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006080#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006081 /* Unregister all secure element drivers, so that we restart from
6082 * a pristine state. */
6083 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006084#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006085}
6086
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006087#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6088/** Recover a transaction that was interrupted by a power failure.
6089 *
6090 * This function is called during initialization, before psa_crypto_init()
6091 * returns. If this function returns a failure status, the initialization
6092 * fails.
6093 */
6094static psa_status_t psa_crypto_recover_transaction(
6095 const psa_crypto_transaction_t *transaction )
6096{
6097 switch( transaction->unknown.type )
6098 {
6099 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6100 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006101 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006102 * is implemented.
6103 * https://github.com/ARMmbed/mbed-crypto/issues/218
6104 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006105 default:
6106 /* We found an unsupported transaction in the storage.
6107 * We don't know what state the storage is in. Give up. */
6108 return( PSA_ERROR_STORAGE_FAILURE );
6109 }
6110}
6111#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6112
Gilles Peskinee59236f2018-01-27 23:32:46 +01006113psa_status_t psa_crypto_init( void )
6114{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006115 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006116 const unsigned char drbg_seed[] = "PSA";
6117
Gilles Peskinec6b69072018-11-20 21:42:52 +01006118 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006119 if( global_data.initialized != 0 )
6120 return( PSA_SUCCESS );
6121
Gilles Peskine5e769522018-11-20 21:59:56 +01006122 /* Set default configuration if
6123 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6124 if( global_data.entropy_init == NULL )
6125 global_data.entropy_init = mbedtls_entropy_init;
6126 if( global_data.entropy_free == NULL )
6127 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006128
Gilles Peskinec6b69072018-11-20 21:42:52 +01006129 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006130 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006131#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6132 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6133 /* The PSA entropy injection feature depends on using NV seed as an entropy
6134 * source. Add NV seed as an entropy source for PSA entropy injection. */
6135 mbedtls_entropy_add_source( &global_data.entropy,
6136 mbedtls_nv_seed_poll, NULL,
6137 MBEDTLS_ENTROPY_BLOCK_SIZE,
6138 MBEDTLS_ENTROPY_SOURCE_STRONG );
6139#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006140 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006141 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006142 status = mbedtls_to_psa_error(
6143 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6144 mbedtls_entropy_func,
6145 &global_data.entropy,
6146 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6147 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006148 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006149 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006150
Gilles Peskine66fb1262018-12-10 16:29:04 +01006151 status = psa_initialize_key_slots( );
6152 if( status != PSA_SUCCESS )
6153 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006154
Gilles Peskined9348f22019-10-01 15:22:29 +02006155#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6156 status = psa_init_all_se_drivers( );
6157 if( status != PSA_SUCCESS )
6158 goto exit;
6159#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6160
Gilles Peskine4b734222019-07-24 15:56:31 +02006161#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006162 status = psa_crypto_load_transaction( );
6163 if( status == PSA_SUCCESS )
6164 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006165 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6166 if( status != PSA_SUCCESS )
6167 goto exit;
6168 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006169 }
6170 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6171 {
6172 /* There's no transaction to complete. It's all good. */
6173 status = PSA_SUCCESS;
6174 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006175#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006176
Gilles Peskinec6b69072018-11-20 21:42:52 +01006177 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006178 global_data.initialized = 1;
6179
6180exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006181 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006182 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006183 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006184}
6185
6186#endif /* MBEDTLS_PSA_CRYPTO_C */