blob: c35b2a64488fed73e96adbb208c9fb8786a1b7a2 [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/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
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.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb2b64d32020-12-14 16:43:58 +010043#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010044
Gilles Peskineb46bef22019-07-30 21:32:04 +020045#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020049#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#define mbedtls_calloc calloc
51#define mbedtls_free free
52#endif
53
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010054#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000057#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020058#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/blowfish.h"
60#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
72#include "mbedtls/md2.h"
73#include "mbedtls/md4.h"
74#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010075#include "mbedtls/md.h"
76#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010079#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000080#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010082#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/sha1.h"
84#include "mbedtls/sha256.h"
85#include "mbedtls/sha512.h"
86#include "mbedtls/xtea.h"
87
Gilles Peskine996deb12018-08-01 15:45:45 +020088#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
89
Gilles Peskine9ef733f2018-02-07 21:05:37 +010090/* constant-time buffer comparison */
91static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
92{
93 size_t i;
94 unsigned char diff = 0;
95
96 for( i = 0; i < n; i++ )
97 diff |= a[i] ^ b[i];
98
99 return( diff );
100}
101
102
103
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100104/****************************************************************/
105/* Global data, support functions and library management */
106/****************************************************************/
107
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108static int key_type_is_raw_bytes( psa_key_type_t type )
109{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200110 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200111}
112
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100113/* Values for psa_global_data_t::rng_state */
114#define RNG_NOT_INITIALIZED 0
115#define RNG_INITIALIZED 1
116#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100117
Gilles Peskine2d277862018-06-18 15:41:12 +0200118typedef struct
119{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100120 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100127#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
128mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
129 &global_data.rng.drbg;
130#endif
131
itayzafrir0adf0fc2018-09-06 16:24:41 +0300132#define GUARD_MODULE_INITIALIZED \
133 if( global_data.initialized == 0 ) \
134 return( PSA_ERROR_BAD_STATE );
135
Steven Cooreman01164162020-07-20 15:31:37 +0200136psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100138 /* Mbed TLS error codes can combine a high-level error code and a
139 * low-level error code. The low-level error usually reflects the
140 * root cause better, so dispatch on that preferably. */
141 int low_level_ret = - ( -ret & 0x007f );
142 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 {
144 case 0:
145 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100146
147 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
148 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
149 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
150 return( PSA_ERROR_NOT_SUPPORTED );
151 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
152 return( PSA_ERROR_HARDWARE_FAILURE );
153
154 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
155 return( PSA_ERROR_HARDWARE_FAILURE );
156
Gilles Peskine9a944802018-06-21 09:35:35 +0200157 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
158 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
159 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
160 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
161 case MBEDTLS_ERR_ASN1_INVALID_DATA:
162 return( PSA_ERROR_INVALID_ARGUMENT );
163 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
164 return( PSA_ERROR_INSUFFICIENT_MEMORY );
165 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
166 return( PSA_ERROR_BUFFER_TOO_SMALL );
167
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000169 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
171 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
Jaeden Amero93e21112019-02-20 13:57:28 +0000178#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000179 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000180#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
181 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
182#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
188 case MBEDTLS_ERR_CCM_BAD_INPUT:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190 case MBEDTLS_ERR_CCM_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
193 return( PSA_ERROR_HARDWARE_FAILURE );
194
Gilles Peskine26869f22019-05-06 15:25:00 +0200195 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197
198 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
199 return( PSA_ERROR_BAD_STATE );
200 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
201 return( PSA_ERROR_INVALID_SIGNATURE );
202
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
204 return( PSA_ERROR_NOT_SUPPORTED );
205 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
206 return( PSA_ERROR_INVALID_ARGUMENT );
207 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
208 return( PSA_ERROR_INSUFFICIENT_MEMORY );
209 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
210 return( PSA_ERROR_INVALID_PADDING );
211 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200212 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
214 return( PSA_ERROR_INVALID_SIGNATURE );
215 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200216 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
218 return( PSA_ERROR_HARDWARE_FAILURE );
219
220 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
221 return( PSA_ERROR_HARDWARE_FAILURE );
222
Gilles Peskine82e57d12020-11-13 21:31:17 +0100223#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
224 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100225 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
226 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
228 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
229 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
230 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
231 return( PSA_ERROR_NOT_SUPPORTED );
232 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100235
236 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
237 return( PSA_ERROR_NOT_SUPPORTED );
238 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
Gilles Peskinee59236f2018-01-27 23:32:46 +0100241 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
242 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
243 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
244 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100245
246 case MBEDTLS_ERR_GCM_AUTH_FAILED:
247 return( PSA_ERROR_INVALID_SIGNATURE );
248 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200249 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100250 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
251 return( PSA_ERROR_HARDWARE_FAILURE );
252
Gilles Peskine82e57d12020-11-13 21:31:17 +0100253#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
254 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100255 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
256 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
258 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
259 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
260 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
261 return( PSA_ERROR_NOT_SUPPORTED );
262 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
263 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
264#endif
265
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
267 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
268 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
269 return( PSA_ERROR_HARDWARE_FAILURE );
270
271 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_MD_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
278 return( PSA_ERROR_STORAGE_FAILURE );
279 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
280 return( PSA_ERROR_HARDWARE_FAILURE );
281
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
283 return( PSA_ERROR_STORAGE_FAILURE );
284 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
285 return( PSA_ERROR_INVALID_ARGUMENT );
286 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
289 return( PSA_ERROR_BUFFER_TOO_SMALL );
290 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
297 return( PSA_ERROR_INSUFFICIENT_MEMORY );
298
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 case MBEDTLS_ERR_PK_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
302 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
307 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
312 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
313 return( PSA_ERROR_NOT_PERMITTED );
314 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_PK_INVALID_ALG:
317 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
318 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
321 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
323 return( PSA_ERROR_HARDWARE_FAILURE );
324
Gilles Peskineff2d2002019-05-06 15:26:23 +0200325 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
328 return( PSA_ERROR_NOT_SUPPORTED );
329
Gilles Peskinea5905292018-02-07 20:59:33 +0100330 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
333 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
334 return( PSA_ERROR_INVALID_ARGUMENT );
335 case MBEDTLS_ERR_RSA_INVALID_PADDING:
336 return( PSA_ERROR_INVALID_PADDING );
337 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
340 return( PSA_ERROR_INVALID_ARGUMENT );
341 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
342 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200343 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100344 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
345 return( PSA_ERROR_INVALID_SIGNATURE );
346 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
347 return( PSA_ERROR_BUFFER_TOO_SMALL );
348 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100349 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
351 return( PSA_ERROR_NOT_SUPPORTED );
352 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
354
355 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
356 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
357 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
358 return( PSA_ERROR_HARDWARE_FAILURE );
359
360 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
361 return( PSA_ERROR_INVALID_ARGUMENT );
362 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
363 return( PSA_ERROR_HARDWARE_FAILURE );
364
itayzafrir5c753392018-05-08 11:18:38 +0300365 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300366 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300367 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300368 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
369 return( PSA_ERROR_BUFFER_TOO_SMALL );
370 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
371 return( PSA_ERROR_NOT_SUPPORTED );
372 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
373 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
374 return( PSA_ERROR_INVALID_SIGNATURE );
375 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
376 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100377 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
378 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300379 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
380 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100381
Janos Follatha13b9052019-11-22 12:48:59 +0000382 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
383 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300384
Gilles Peskinee59236f2018-01-27 23:32:46 +0100385 default:
David Saadab4ecc272019-02-14 13:48:10 +0200386 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100387 }
388}
389
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200390
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200391
392
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100393/****************************************************************/
394/* Key management */
395/****************************************************************/
396
Gilles Peskine73167e12019-07-12 23:44:37 +0200397#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
398static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
399{
Gilles Peskine8e338702019-07-30 20:06:31 +0200400 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200401}
402#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
403
John Durkop07cc04a2020-11-16 22:08:34 -0800404/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
405 * current test driver in key_management.c is using this function
406 * when accelerators are used for ECC key pair and public key.
407 * Once that dependency is resolved these guards can be removed.
408 */
John Durkop9814fa22020-11-04 12:28:15 -0800409#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800410 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
411 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
412 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100413mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100414 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200415{
416 switch( curve )
417 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100418 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100419 switch( byte_length )
420 {
421 case PSA_BITS_TO_BYTES( 192 ):
422 return( MBEDTLS_ECP_DP_SECP192R1 );
423 case PSA_BITS_TO_BYTES( 224 ):
424 return( MBEDTLS_ECP_DP_SECP224R1 );
425 case PSA_BITS_TO_BYTES( 256 ):
426 return( MBEDTLS_ECP_DP_SECP256R1 );
427 case PSA_BITS_TO_BYTES( 384 ):
428 return( MBEDTLS_ECP_DP_SECP384R1 );
429 case PSA_BITS_TO_BYTES( 521 ):
430 return( MBEDTLS_ECP_DP_SECP521R1 );
431 default:
432 return( MBEDTLS_ECP_DP_NONE );
433 }
434 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100435
Paul Elliott8ff510a2020-06-02 17:19:28 +0100436 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100437 switch( byte_length )
438 {
439 case PSA_BITS_TO_BYTES( 256 ):
440 return( MBEDTLS_ECP_DP_BP256R1 );
441 case PSA_BITS_TO_BYTES( 384 ):
442 return( MBEDTLS_ECP_DP_BP384R1 );
443 case PSA_BITS_TO_BYTES( 512 ):
444 return( MBEDTLS_ECP_DP_BP512R1 );
445 default:
446 return( MBEDTLS_ECP_DP_NONE );
447 }
448 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100449
Paul Elliott8ff510a2020-06-02 17:19:28 +0100450 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100451 switch( byte_length )
452 {
453 case PSA_BITS_TO_BYTES( 255 ):
454 return( MBEDTLS_ECP_DP_CURVE25519 );
455 case PSA_BITS_TO_BYTES( 448 ):
456 return( MBEDTLS_ECP_DP_CURVE448 );
457 default:
458 return( MBEDTLS_ECP_DP_NONE );
459 }
460 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100461
Paul Elliott8ff510a2020-06-02 17:19:28 +0100462 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100463 switch( byte_length )
464 {
465 case PSA_BITS_TO_BYTES( 192 ):
466 return( MBEDTLS_ECP_DP_SECP192K1 );
467 case PSA_BITS_TO_BYTES( 224 ):
468 return( MBEDTLS_ECP_DP_SECP224K1 );
469 case PSA_BITS_TO_BYTES( 256 ):
470 return( MBEDTLS_ECP_DP_SECP256K1 );
471 default:
472 return( MBEDTLS_ECP_DP_NONE );
473 }
474 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100475
Gilles Peskine12313cd2018-06-20 00:20:32 +0200476 default:
477 return( MBEDTLS_ECP_DP_NONE );
478 }
479}
John Durkop9814fa22020-11-04 12:28:15 -0800480#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800481 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
482 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
483 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200484
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200485static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
486 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487{
488 /* Check that the bit size is acceptable for the key type */
489 switch( type )
490 {
491 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200493 case PSA_KEY_TYPE_DERIVE:
494 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495#if defined(MBEDTLS_AES_C)
496 case PSA_KEY_TYPE_AES:
497 if( bits != 128 && bits != 192 && bits != 256 )
498 return( PSA_ERROR_INVALID_ARGUMENT );
499 break;
500#endif
501#if defined(MBEDTLS_CAMELLIA_C)
502 case PSA_KEY_TYPE_CAMELLIA:
503 if( bits != 128 && bits != 192 && bits != 256 )
504 return( PSA_ERROR_INVALID_ARGUMENT );
505 break;
506#endif
507#if defined(MBEDTLS_DES_C)
508 case PSA_KEY_TYPE_DES:
509 if( bits != 64 && bits != 128 && bits != 192 )
510 return( PSA_ERROR_INVALID_ARGUMENT );
511 break;
512#endif
513#if defined(MBEDTLS_ARC4_C)
514 case PSA_KEY_TYPE_ARC4:
515 if( bits < 8 || bits > 2048 )
516 return( PSA_ERROR_INVALID_ARGUMENT );
517 break;
518#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200519#if defined(MBEDTLS_CHACHA20_C)
520 case PSA_KEY_TYPE_CHACHA20:
521 if( bits != 256 )
522 return( PSA_ERROR_INVALID_ARGUMENT );
523 break;
524#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200525 default:
526 return( PSA_ERROR_NOT_SUPPORTED );
527 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200528 if( bits % 8 != 0 )
529 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200530
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200531 return( PSA_SUCCESS );
532}
533
John Durkop0e005192020-10-31 22:06:54 -0700534#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
535 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
536 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800537 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
538 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
539 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200540
Gilles Peskine86a440b2018-11-12 18:39:40 +0100541/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
542 * that are not a multiple of 8) well. For example, there is only
543 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
544 * way to return the exact bit size of a key.
545 * To keep things simple, reject non-byte-aligned key sizes. */
546static psa_status_t psa_check_rsa_key_byte_aligned(
547 const mbedtls_rsa_context *rsa )
548{
549 mbedtls_mpi n;
550 psa_status_t status;
551 mbedtls_mpi_init( &n );
552 status = mbedtls_to_psa_error(
553 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
554 if( status == PSA_SUCCESS )
555 {
556 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
557 status = PSA_ERROR_NOT_SUPPORTED;
558 }
559 mbedtls_mpi_free( &n );
560 return( status );
561}
562
Steven Cooreman7f391872020-07-30 14:57:44 +0200563/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200564 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200565 * \param[in] type The type of key contained in \p data.
566 * \param[in] data The buffer from which to load the representation.
567 * \param[in] data_length The size in bytes of \p data.
568 * \param[out] p_rsa Returns a pointer to an RSA context on success.
569 * The caller is responsible for freeing both the
570 * contents of the context and the context itself
571 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200572 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200573static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
574 const uint8_t *data,
575 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200576 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200577{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000578 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200579 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000580 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200581 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000582
583 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200584 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000585 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200586 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200587 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000588 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200589 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000590 if( status != PSA_SUCCESS )
591 goto exit;
592
593 /* We have something that the pkparse module recognizes. If it is a
594 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200595 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200596 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000597 status = PSA_ERROR_INVALID_ARGUMENT;
598 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200599 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000600
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000601 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
602 * supports non-byte-aligned key sizes, but not well. For example,
603 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200604 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000605 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
606 {
607 status = PSA_ERROR_NOT_SUPPORTED;
608 goto exit;
609 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200610 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200611 if( status != PSA_SUCCESS )
612 goto exit;
613
Steven Cooremana2371e52020-07-28 14:30:39 +0200614 /* Copy out the pointer to the RSA context, and reset the PK context
615 * such that pk_free doesn't free the RSA context we just grabbed. */
616 *p_rsa = mbedtls_pk_rsa( ctx );
617 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000618
619exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200620 mbedtls_pk_free( &ctx );
621 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +0200622}
623
John Durkop6ba40d12020-11-10 08:50:04 -0800624#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
625 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
626 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
627 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
628 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
629 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
630
631#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
632 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
633
Steven Cooremana01795d2020-07-24 22:48:15 +0200634/** Export an RSA key to export representation
635 *
636 * \param[in] type The type of key (public/private) to export
637 * \param[in] rsa The internal RSA representation from which to export
638 * \param[out] data The buffer to export to
639 * \param[in] data_size The length of the buffer to export to
640 * \param[out] data_length The amount of bytes written to \p data
641 */
642static psa_status_t psa_export_rsa_key( psa_key_type_t type,
643 mbedtls_rsa_context *rsa,
644 uint8_t *data,
645 size_t data_size,
646 size_t *data_length )
647{
648#if defined(MBEDTLS_PK_WRITE_C)
649 int ret;
650 mbedtls_pk_context pk;
651 uint8_t *pos = data + data_size;
652
653 mbedtls_pk_init( &pk );
654 pk.pk_info = &mbedtls_rsa_info;
655 pk.pk_ctx = rsa;
656
657 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200658 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
659 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200660 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
661 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
662 else
663 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
664
665 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200666 {
667 /* Clean up in case pk_write failed halfway through. */
668 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200669 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200670 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200671
672 /* The mbedtls_pk_xxx functions write to the end of the buffer.
673 * Move the data to the beginning and erase remaining data
674 * at the original location. */
675 if( 2 * (size_t) ret <= data_size )
676 {
677 memcpy( data, data + data_size - ret, ret );
678 memset( data + data_size - ret, 0, ret );
679 }
680 else if( (size_t) ret < data_size )
681 {
682 memmove( data, data + data_size - ret, ret );
683 memset( data + ret, 0, data_size - ret );
684 }
685
686 *data_length = ret;
687 return( PSA_SUCCESS );
688#else
689 (void) type;
690 (void) rsa;
691 (void) data;
692 (void) data_size;
693 (void) data_length;
694 return( PSA_ERROR_NOT_SUPPORTED );
695#endif /* MBEDTLS_PK_WRITE_C */
696}
697
698/** Import an RSA key from import representation to a slot
699 *
700 * \param[in,out] slot The slot where to store the export representation to
701 * \param[in] data The buffer containing the import representation
702 * \param[in] data_length The amount of bytes in \p data
703 */
704static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
705 const uint8_t *data,
706 size_t data_length )
707{
708 psa_status_t status;
709 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200710 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200711
Steven Cooremana01795d2020-07-24 22:48:15 +0200712 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200713 status = psa_load_rsa_representation( slot->attr.type,
714 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200715 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200716 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200717 if( status != PSA_SUCCESS )
718 goto exit;
719
720 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200721 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200722
Steven Cooreman75b74362020-07-28 14:30:13 +0200723 /* Re-export the data to PSA export format, such that we can store export
724 * representation in the key slot. Export representation in case of RSA is
725 * the smallest representation that's allowed as input, so a straight-up
726 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200727 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200728 if( output == NULL )
729 {
730 status = PSA_ERROR_INSUFFICIENT_MEMORY;
731 goto exit;
732 }
733
Steven Cooremana01795d2020-07-24 22:48:15 +0200734 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200735 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200736 output,
737 data_length,
738 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200739exit:
740 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200741 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200742 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200743
744 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000745 if( status != PSA_SUCCESS )
746 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200747 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000748 return( status );
749 }
750
Steven Cooremana01795d2020-07-24 22:48:15 +0200751 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100752 slot->key.data = output;
753 slot->key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000754
755 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200756}
John Durkop6ba40d12020-11-10 08:50:04 -0800757
758#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800759 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200760
John Durkop9814fa22020-11-04 12:28:15 -0800761#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800762 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
763 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
764 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
765 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200766/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200767 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200768 * \param[in] type The type of key contained in \p data.
769 * \param[in] data The buffer from which to load the representation.
770 * \param[in] data_length The size in bytes of \p data.
771 * \param[out] p_ecp Returns a pointer to an ECP context on success.
772 * The caller is responsible for freeing both the
773 * contents of the context and the context itself
774 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200775 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200776static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
777 const uint8_t *data,
778 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200779 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100780{
781 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200782 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200783 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200784 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100785
Steven Cooreman4fed4552020-08-03 14:46:03 +0200786 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
787 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100788 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200789 /* A Weierstrass public key is represented as:
790 * - The byte 0x04;
791 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
792 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200793 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200794 */
795 if( ( data_length & 1 ) == 0 )
796 return( PSA_ERROR_INVALID_ARGUMENT );
797 curve_size = data_length / 2;
798
799 /* Montgomery public keys are represented in compressed format, meaning
800 * their curve_size is equal to the amount of input. */
801
802 /* Private keys are represented in uncompressed private random integer
803 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100804 }
805
Steven Cooreman6d839f02020-07-30 11:36:45 +0200806 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200807 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200808 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200809 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200810 mbedtls_ecp_keypair_init( ecp );
811
Gilles Peskine4cd32772019-12-02 20:49:42 +0100812 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200813 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
814 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100815 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200816 {
817 status = PSA_ERROR_INVALID_ARGUMENT;
818 goto exit;
819 }
820
Steven Cooremanacda8342020-07-24 23:09:52 +0200821 status = mbedtls_to_psa_error(
822 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
823 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200824 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200825
Steven Cooreman6d839f02020-07-30 11:36:45 +0200826 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200827 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200828 {
829 /* Load the public value. */
830 status = mbedtls_to_psa_error(
831 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200832 data,
833 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200834 if( status != PSA_SUCCESS )
835 goto exit;
836
837 /* Check that the point is on the curve. */
838 status = mbedtls_to_psa_error(
839 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
840 if( status != PSA_SUCCESS )
841 goto exit;
842 }
843 else
844 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200845 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200846 status = mbedtls_to_psa_error(
847 mbedtls_ecp_read_key( ecp->grp.id,
848 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 data,
850 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200851 if( status != PSA_SUCCESS )
852 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200853 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200854
855 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200856exit:
857 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200858 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200859 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200860 mbedtls_free( ecp );
861 }
862
Steven Cooreman29149862020-08-05 15:43:42 +0200863 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100864}
John Durkop6ba40d12020-11-10 08:50:04 -0800865#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
866 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
867 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
868 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
869 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000870
John Durkop6ba40d12020-11-10 08:50:04 -0800871#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
872 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200873/** Export an ECP key to export representation
874 *
875 * \param[in] type The type of key (public/private) to export
876 * \param[in] ecp The internal ECP representation from which to export
877 * \param[out] data The buffer to export to
878 * \param[in] data_size The length of the buffer to export to
879 * \param[out] data_length The amount of bytes written to \p data
880 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200881static psa_status_t psa_export_ecp_key( psa_key_type_t type,
882 mbedtls_ecp_keypair *ecp,
883 uint8_t *data,
884 size_t data_size,
885 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200886{
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000888
Steven Cooremanacda8342020-07-24 23:09:52 +0200889 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200890 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200891 /* Check whether the public part is loaded */
892 if( mbedtls_ecp_is_zero( &ecp->Q ) )
893 {
894 /* Calculate the public key */
895 status = mbedtls_to_psa_error(
896 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100897 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200898 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200899 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200900 }
901
Steven Cooreman4fed4552020-08-03 14:46:03 +0200902 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200903 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
904 MBEDTLS_ECP_PF_UNCOMPRESSED,
905 data_length,
906 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200907 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200908 if( status != PSA_SUCCESS )
909 memset( data, 0, data_size );
910
Steven Cooreman29149862020-08-05 15:43:42 +0200911 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200912 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200913 else
914 {
Steven Cooreman29149862020-08-05 15:43:42 +0200915 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200916 return( PSA_ERROR_BUFFER_TOO_SMALL );
917
918 status = mbedtls_to_psa_error(
919 mbedtls_ecp_write_key( ecp,
920 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200921 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200922 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200923 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200924 else
925 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200926
927 return( status );
928 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200929}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200930
Steven Cooreman4fed4552020-08-03 14:46:03 +0200931/** Import an ECP key from import representation to a slot
932 *
933 * \param[in,out] slot The slot where to store the export representation to
934 * \param[in] data The buffer containing the import representation
935 * \param[in] data_length The amount of bytes in \p data
936 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200937static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
938 const uint8_t *data,
939 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100940{
Steven Cooremanacda8342020-07-24 23:09:52 +0200941 psa_status_t status;
942 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200943 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100944
Steven Cooremanacda8342020-07-24 23:09:52 +0200945 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200946 status = psa_load_ecp_representation( slot->attr.type,
947 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200948 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200949 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100950 if( status != PSA_SUCCESS )
951 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100952
Steven Cooremanacda8342020-07-24 23:09:52 +0200953 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200954 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200955 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200956 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200957
Steven Cooremanacda8342020-07-24 23:09:52 +0200958 /* Re-export the data to PSA export format. There is currently no support
959 * for other input formats then the export format, so this is a 1-1
960 * copy operation. */
961 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200962 if( output == NULL )
963 {
964 status = PSA_ERROR_INSUFFICIENT_MEMORY;
965 goto exit;
966 }
967
968 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200969 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200970 output,
971 data_length,
972 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100973exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200974 /* Always free the PK object (will also free contained ECP context) */
975 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200976 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200977
978 /* Free the allocated buffer only on error. */
979 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100980 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200981 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200982 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100983 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200984
985 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100986 slot->key.data = output;
987 slot->key.bytes = data_length;
Steven Cooremanacda8342020-07-24 23:09:52 +0200988
989 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100990}
John Durkop9814fa22020-11-04 12:28:15 -0800991#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
992 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100993
Gilles Peskineb46bef22019-07-30 21:32:04 +0200994/** Return the size of the key in the given slot, in bits.
995 *
996 * \param[in] slot A key slot.
997 *
998 * \return The key size in bits, read from the metadata in the slot.
999 */
1000static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
1001{
1002 return( slot->attr.bits );
1003}
1004
Steven Cooreman75b74362020-07-28 14:30:13 +02001005/** Try to allocate a buffer to an empty key slot.
1006 *
1007 * \param[in,out] slot Key slot to attach buffer to.
1008 * \param[in] buffer_length Requested size of the buffer.
1009 *
1010 * \retval #PSA_SUCCESS
1011 * The buffer has been successfully allocated.
1012 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1013 * Not enough memory was available for allocation.
1014 * \retval #PSA_ERROR_ALREADY_EXISTS
1015 * Trying to allocate a buffer to a non-empty key slot.
1016 */
1017static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
1018 size_t buffer_length )
1019{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001020 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001021 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001022
Ronald Cronea0f8a62020-11-25 17:52:23 +01001023 slot->key.data = mbedtls_calloc( 1, buffer_length );
1024 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001025 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +02001026
Ronald Cronea0f8a62020-11-25 17:52:23 +01001027 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001028 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001029}
1030
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001031psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1032 const uint8_t* data,
1033 size_t data_length )
1034{
1035 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1036 data_length );
1037 if( status != PSA_SUCCESS )
1038 return( status );
1039
Ronald Cronea0f8a62020-11-25 17:52:23 +01001040 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001041 return( PSA_SUCCESS );
1042}
1043
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001044/** Import key data into a slot.
1045 *
1046 * `slot->type` must have been set previously.
1047 * This function assumes that the slot does not contain any key material yet.
1048 * On failure, the slot content is unchanged.
1049 *
1050 * Persistent storage is not affected.
1051 *
1052 * \param[in,out] slot The key slot to import data into.
1053 * Its `type` field must have previously been set to
1054 * the desired key type.
1055 * It must not contain any key material yet.
1056 * \param[in] data Buffer containing the key material to parse and import.
1057 * \param data_length Size of \p data in bytes.
1058 *
1059 * \retval #PSA_SUCCESS
1060 * \retval #PSA_ERROR_INVALID_ARGUMENT
1061 * \retval #PSA_ERROR_NOT_SUPPORTED
1062 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1063 */
1064static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1065 const uint8_t *data,
1066 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001067{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001068 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001069 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001071 /* zero-length keys are never supported. */
1072 if( data_length == 0 )
1073 return( PSA_ERROR_NOT_SUPPORTED );
1074
Gilles Peskine8e338702019-07-30 20:06:31 +02001075 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001076 {
Steven Cooreman04524762020-10-13 17:43:44 +02001077 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001078
Steven Cooreman75b74362020-07-28 14:30:13 +02001079 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1080 if( data_length > SIZE_MAX / 8 )
1081 return( PSA_ERROR_NOT_SUPPORTED );
1082
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001083 /* Enforce a size limit, and in particular ensure that the bit
1084 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001085 if( bit_size > PSA_MAX_KEY_BITS )
1086 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001087
1088 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001089 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001090 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001091
1092 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001093 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001094 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001095 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001096
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001097 /* Write the actual key size to the slot.
1098 * psa_start_key_creation() wrote the size declared by the
1099 * caller, which may be 0 (meaning unspecified) or wrong. */
1100 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001101
1102 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001103 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001104 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001105 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001106 /* Try validation through accelerators first. */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001107 psa_key_attributes_t attributes = {
1108 .core = slot->attr
1109 };
Ronald Cron83282872020-11-22 14:02:39 +01001110
1111 status = psa_allocate_buffer_to_slot( slot, data_length );
1112 if( status != PSA_SUCCESS )
1113 return( status );
1114
1115 bit_size = slot->attr.bits;
1116 status = psa_driver_wrapper_import_key( &attributes,
1117 data, data_length,
1118 slot->key.data,
1119 slot->key.bytes,
1120 &slot->key.bytes,
1121 &bit_size );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001122 if( status == PSA_SUCCESS )
1123 {
Ronald Cron83282872020-11-22 14:02:39 +01001124 if( slot->attr.bits == 0 )
1125 slot->attr.bits = (psa_key_bits_t) bit_size;
1126 else if( bit_size != slot->attr.bits )
1127 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001128
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001129 return( PSA_SUCCESS );
1130 }
Ronald Cron83282872020-11-22 14:02:39 +01001131 else
1132 {
1133 if( status != PSA_ERROR_NOT_SUPPORTED )
1134 return( status );
1135 }
1136
1137 mbedtls_platform_zeroize( slot->key.data, data_length );
1138 mbedtls_free( slot->key.data );
1139 slot->key.data = NULL;
1140 slot->key.bytes = 0;
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001141
1142 /* Key format is not supported by any accelerator, try software fallback
1143 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001144#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1145 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001146 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1147 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001148 return( psa_import_ecp_key( slot, data, data_length ) );
1149 }
John Durkop9814fa22020-11-04 12:28:15 -08001150#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1151 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001152#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1153 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001154 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001155 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001156 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001157 }
John Durkop9814fa22020-11-04 12:28:15 -08001158#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1159 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001160
1161 /* Fell through the fallback as well, so have nothing else to try. */
1162 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001163 }
1164 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001165 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001166 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001167 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001168 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001169}
1170
Gilles Peskinef603c712019-01-19 13:40:11 +01001171/** Calculate the intersection of two algorithm usage policies.
1172 *
1173 * Return 0 (which allows no operation) on incompatibility.
1174 */
1175static psa_algorithm_t psa_key_policy_algorithm_intersection(
1176 psa_algorithm_t alg1,
1177 psa_algorithm_t alg2 )
1178{
Gilles Peskine549ea862019-05-22 11:45:59 +02001179 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001180 if( alg1 == alg2 )
1181 return( alg1 );
1182 /* If the policies are from the same hash-and-sign family, check
1183 * if one is a wildcard. If so the other has the specific algorithm. */
1184 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1185 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1186 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1187 {
1188 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1189 return( alg2 );
1190 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1191 return( alg1 );
1192 }
1193 /* If the policies are incompatible, allow nothing. */
1194 return( 0 );
1195}
1196
Gilles Peskined6f371b2019-05-10 19:33:38 +02001197static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1198 psa_algorithm_t requested_alg )
1199{
Gilles Peskine549ea862019-05-22 11:45:59 +02001200 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001201 if( requested_alg == policy_alg )
1202 return( 1 );
1203 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001204 * and requested_alg is the same hash-and-sign family with any hash,
1205 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001206 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1207 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1208 {
1209 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1210 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1211 }
Steven Cooremance48e852020-10-05 16:02:45 +02001212 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001213 * a key derivation with that key agreement should also be allowed. This
1214 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001215 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1216 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1217 {
1218 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1219 policy_alg );
1220 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001221 /* If it isn't permitted, it's forbidden. */
1222 return( 0 );
1223}
1224
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001225/** Test whether a policy permits an algorithm.
1226 *
1227 * The caller must test usage flags separately.
1228 */
1229static int psa_key_policy_permits( const psa_key_policy_t *policy,
1230 psa_algorithm_t alg )
1231{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001232 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1233 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001234}
1235
Gilles Peskinef603c712019-01-19 13:40:11 +01001236/** Restrict a key policy based on a constraint.
1237 *
1238 * \param[in,out] policy The policy to restrict.
1239 * \param[in] constraint The policy constraint to apply.
1240 *
1241 * \retval #PSA_SUCCESS
1242 * \c *policy contains the intersection of the original value of
1243 * \c *policy and \c *constraint.
1244 * \retval #PSA_ERROR_INVALID_ARGUMENT
1245 * \c *policy and \c *constraint are incompatible.
1246 * \c *policy is unchanged.
1247 */
1248static psa_status_t psa_restrict_key_policy(
1249 psa_key_policy_t *policy,
1250 const psa_key_policy_t *constraint )
1251{
1252 psa_algorithm_t intersection_alg =
1253 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001254 psa_algorithm_t intersection_alg2 =
1255 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001256 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1257 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001258 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1259 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001260 policy->usage &= constraint->usage;
1261 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001262 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001263 return( PSA_SUCCESS );
1264}
1265
Ronald Cron5c522922020-11-14 16:35:34 +01001266/** Get the description of a key given its identifier and policy constraints
1267 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001268 *
Ronald Cron5c522922020-11-14 16:35:34 +01001269 * The key must have allow all the usage flags set in \p usage. If \p alg is
1270 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001271 *
Ronald Cron5c522922020-11-14 16:35:34 +01001272 * In case of a persistent key, the function loads the description of the key
1273 * into a key slot if not already done.
1274 *
1275 * On success, the returned key slot is locked. It is the responsibility of
1276 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001277 */
Ronald Cron5c522922020-11-14 16:35:34 +01001278static psa_status_t psa_get_and_lock_key_slot_with_policy(
1279 mbedtls_svc_key_id_t key,
1280 psa_key_slot_t **p_slot,
1281 psa_key_usage_t usage,
1282 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001283{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001284 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1285 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001286
Ronald Cron5c522922020-11-14 16:35:34 +01001287 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001288 if( status != PSA_SUCCESS )
1289 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001290 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001291
1292 /* Enforce that usage policy for the key slot contains all the flags
1293 * required by the usage parameter. There is one exception: public
1294 * keys can always be exported, so we treat public key objects as
1295 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001296 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001297 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001298
1299 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001300 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001301 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001302
1303 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001304 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001305 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001306
Darryl Green06fd18d2018-07-16 11:21:11 +01001307 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001308
1309error:
1310 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001311 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001312
1313 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001314}
Darryl Green940d72c2018-07-13 13:18:51 +01001315
Ronald Cron5c522922020-11-14 16:35:34 +01001316/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001317 *
1318 * A transparent key is a key for which the key material is directly
1319 * available, as opposed to a key in a secure element.
1320 *
Ronald Cron5c522922020-11-14 16:35:34 +01001321 * This is a temporary function to use instead of
1322 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1323 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001324 *
Ronald Cron5c522922020-11-14 16:35:34 +01001325 * On success, the returned key slot is locked. It is the responsibility of the
1326 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001327 */
1328#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001329static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1330 mbedtls_svc_key_id_t key,
1331 psa_key_slot_t **p_slot,
1332 psa_key_usage_t usage,
1333 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001334{
Ronald Cron5c522922020-11-14 16:35:34 +01001335 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1336 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001337 if( status != PSA_SUCCESS )
1338 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001339
Gilles Peskineadad8132019-07-25 11:31:23 +02001340 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001341 {
Ronald Cron5c522922020-11-14 16:35:34 +01001342 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001343 *p_slot = NULL;
1344 return( PSA_ERROR_NOT_SUPPORTED );
1345 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001346
Gilles Peskine28f8f302019-07-24 13:30:31 +02001347 return( PSA_SUCCESS );
1348}
1349#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1350/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001351#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1352 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001353#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1354
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001355/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001356static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001357{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001358 /* Data pointer will always be either a valid pointer or NULL in an
1359 * initialized slot, so we can just free it. */
1360 if( slot->key.data != NULL )
1361 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1362
1363 mbedtls_free( slot->key.data );
1364 slot->key.data = NULL;
1365 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001366
1367 return( PSA_SUCCESS );
1368}
1369
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001370/** Completely wipe a slot in memory, including its policy.
1371 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001372psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001373{
1374 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001375
1376 /*
1377 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001378 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001379 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1380 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001381 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001382 */
Ronald Cron5c522922020-11-14 16:35:34 +01001383 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001384 {
1385#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001386 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001387#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001388 status = PSA_ERROR_CORRUPTION_DETECTED;
1389 }
1390
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001391 /* Multipart operations may still be using the key. This is safe
1392 * because all multipart operation objects are independent from
1393 * the key slot: if they need to access the key after the setup
1394 * phase, they have a copy of the key. Note that this means that
1395 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001396 /* At this point, key material and other type-specific content has
1397 * been wiped. Clear remaining metadata. We can call memset and not
1398 * zeroize because the metadata is not particularly sensitive. */
1399 memset( slot, 0, sizeof( *slot ) );
1400 return( status );
1401}
1402
Ronald Croncf56a0a2020-08-04 09:51:30 +02001403psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001404{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001405 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001406 psa_status_t status; /* status of the last operation */
1407 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001408#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1409 psa_se_drv_table_entry_t *driver;
1410#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001411
Ronald Croncf56a0a2020-08-04 09:51:30 +02001412 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001413 return( PSA_SUCCESS );
1414
Ronald Cronf2911112020-10-29 17:51:10 +01001415 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001416 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001417 * key, this will load the key description from persistent memory if not
1418 * done yet. We cannot avoid this loading as without it we don't know if
1419 * the key is operated by an SE or not and this information is needed by
1420 * the current implementation.
1421 */
Ronald Cron5c522922020-11-14 16:35:34 +01001422 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001423 if( status != PSA_SUCCESS )
1424 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001425
Ronald Cronf2911112020-10-29 17:51:10 +01001426 /*
1427 * If the key slot containing the key description is under access by the
1428 * library (apart from the present access), the key cannot be destroyed
1429 * yet. For the time being, just return in error. Eventually (to be
1430 * implemented), the key should be destroyed when all accesses have
1431 * stopped.
1432 */
Ronald Cron5c522922020-11-14 16:35:34 +01001433 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001434 {
Ronald Cron5c522922020-11-14 16:35:34 +01001435 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001436 return( PSA_ERROR_GENERIC_ERROR );
1437 }
1438
Gilles Peskine354f7672019-07-12 23:46:38 +02001439#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001440 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001441 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001442 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001443 /* For a key in a secure element, we need to do three things:
1444 * remove the key file in internal storage, destroy the
1445 * key inside the secure element, and update the driver's
1446 * persistent data. Start a transaction that will encompass these
1447 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001448 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001449 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001450 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001451 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001452 status = psa_crypto_save_transaction( );
1453 if( status != PSA_SUCCESS )
1454 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001455 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001456 /* We should still try to destroy the key in the secure
1457 * element and the key metadata in storage. This is especially
1458 * important if the error is that the storage is full.
1459 * But how to do it exactly without risking an inconsistent
1460 * state after a reset?
1461 * https://github.com/ARMmbed/mbed-crypto/issues/215
1462 */
1463 overall_status = status;
1464 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001465 }
1466
Ronald Cronea0f8a62020-11-25 17:52:23 +01001467 status = psa_destroy_se_key( driver,
1468 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001469 if( overall_status == PSA_SUCCESS )
1470 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001471 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001472#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1473
Darryl Greend49a4992018-06-18 17:27:26 +01001474#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001475 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001476 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001477 status = psa_destroy_persistent_key( slot->attr.id );
1478 if( overall_status == PSA_SUCCESS )
1479 overall_status = status;
1480
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001481 /* TODO: other slots may have a copy of the same key. We should
1482 * invalidate them.
1483 * https://github.com/ARMmbed/mbed-crypto/issues/214
1484 */
Darryl Greend49a4992018-06-18 17:27:26 +01001485 }
1486#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001487
Gilles Peskinefc762652019-07-22 19:30:34 +02001488#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1489 if( driver != NULL )
1490 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001491 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001492 if( overall_status == PSA_SUCCESS )
1493 overall_status = status;
1494 status = psa_crypto_stop_transaction( );
1495 if( overall_status == PSA_SUCCESS )
1496 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001497 }
1498#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1499
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001500#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1501exit:
1502#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001503 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001504 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1505 if( overall_status == PSA_SUCCESS )
1506 overall_status = status;
1507 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001508}
1509
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001510void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001511{
Gilles Peskineb699f072019-04-26 16:06:02 +02001512 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001513 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001514}
1515
Gilles Peskineb699f072019-04-26 16:06:02 +02001516psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1517 psa_key_type_t type,
1518 const uint8_t *data,
1519 size_t data_length )
1520{
1521 uint8_t *copy = NULL;
1522
1523 if( data_length != 0 )
1524 {
1525 copy = mbedtls_calloc( 1, data_length );
1526 if( copy == NULL )
1527 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1528 memcpy( copy, data, data_length );
1529 }
1530 /* After this point, this function is guaranteed to succeed, so it
1531 * can start modifying `*attributes`. */
1532
1533 if( attributes->domain_parameters != NULL )
1534 {
1535 mbedtls_free( attributes->domain_parameters );
1536 attributes->domain_parameters = NULL;
1537 attributes->domain_parameters_size = 0;
1538 }
1539
1540 attributes->domain_parameters = copy;
1541 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001542 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001543 return( PSA_SUCCESS );
1544}
1545
1546psa_status_t psa_get_key_domain_parameters(
1547 const psa_key_attributes_t *attributes,
1548 uint8_t *data, size_t data_size, size_t *data_length )
1549{
1550 if( attributes->domain_parameters_size > data_size )
1551 return( PSA_ERROR_BUFFER_TOO_SMALL );
1552 *data_length = attributes->domain_parameters_size;
1553 if( attributes->domain_parameters_size != 0 )
1554 memcpy( data, attributes->domain_parameters,
1555 attributes->domain_parameters_size );
1556 return( PSA_SUCCESS );
1557}
1558
John Durkop07cc04a2020-11-16 22:08:34 -08001559#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001560 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001561static psa_status_t psa_get_rsa_public_exponent(
1562 const mbedtls_rsa_context *rsa,
1563 psa_key_attributes_t *attributes )
1564{
1565 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001567 uint8_t *buffer = NULL;
1568 size_t buflen;
1569 mbedtls_mpi_init( &mpi );
1570
1571 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1572 if( ret != 0 )
1573 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001574 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1575 {
1576 /* It's the default value, which is reported as an empty string,
1577 * so there's nothing to do. */
1578 goto exit;
1579 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001580
1581 buflen = mbedtls_mpi_size( &mpi );
1582 buffer = mbedtls_calloc( 1, buflen );
1583 if( buffer == NULL )
1584 {
1585 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1586 goto exit;
1587 }
1588 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1589 if( ret != 0 )
1590 goto exit;
1591 attributes->domain_parameters = buffer;
1592 attributes->domain_parameters_size = buflen;
1593
1594exit:
1595 mbedtls_mpi_free( &mpi );
1596 if( ret != 0 )
1597 mbedtls_free( buffer );
1598 return( mbedtls_to_psa_error( ret ) );
1599}
John Durkop07cc04a2020-11-16 22:08:34 -08001600#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001601 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001602
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001603/** Retrieve all the publicly-accessible attributes of a key.
1604 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001605psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001606 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001607{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001609 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001610 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001611
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001612 psa_reset_key_attributes( attributes );
1613
Ronald Cron5c522922020-11-14 16:35:34 +01001614 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001615 if( status != PSA_SUCCESS )
1616 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001617
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001618 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001619 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1620 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1621
1622#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1623 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001624 psa_set_key_slot_number( attributes,
1625 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001626#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001627
Gilles Peskine8e338702019-07-30 20:06:31 +02001628 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001629 {
John Durkop07cc04a2020-11-16 22:08:34 -08001630#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001631 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001632 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001633 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001634#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001635 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001636 * is not yet implemented.
1637 * https://github.com/ARMmbed/mbed-crypto/issues/216
1638 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001639 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001640 break;
1641#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001642 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001643 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001644
Steven Cooreman4fed4552020-08-03 14:46:03 +02001645 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001646 slot->key.data,
1647 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001648 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001649 if( status != PSA_SUCCESS )
1650 break;
1651
Steven Cooremana2371e52020-07-28 14:30:39 +02001652 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001653 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001654 mbedtls_rsa_free( rsa );
1655 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001656 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001657 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001658#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001659 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001660 default:
1661 /* Nothing else to do. */
1662 break;
1663 }
1664
1665 if( status != PSA_SUCCESS )
1666 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001667
Ronald Cron5c522922020-11-14 16:35:34 +01001668 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001669
Ronald Cron5c522922020-11-14 16:35:34 +01001670 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001671}
1672
Gilles Peskinec8000c02019-08-02 20:15:51 +02001673#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1674psa_status_t psa_get_key_slot_number(
1675 const psa_key_attributes_t *attributes,
1676 psa_key_slot_number_t *slot_number )
1677{
1678 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1679 {
1680 *slot_number = attributes->slot_number;
1681 return( PSA_SUCCESS );
1682 }
1683 else
1684 return( PSA_ERROR_INVALID_ARGUMENT );
1685}
1686#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1687
Steven Cooremana01795d2020-07-24 22:48:15 +02001688static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1689 uint8_t *data,
1690 size_t data_size,
1691 size_t *data_length )
1692{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001693 if( slot->key.bytes > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001694 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Cronea0f8a62020-11-25 17:52:23 +01001695 memcpy( data, slot->key.data, slot->key.bytes );
1696 memset( data + slot->key.bytes, 0,
1697 data_size - slot->key.bytes );
1698 *data_length = slot->key.bytes;
Steven Cooremana01795d2020-07-24 22:48:15 +02001699 return( PSA_SUCCESS );
1700}
1701
Gilles Peskinef603c712019-01-19 13:40:11 +01001702static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1703 uint8_t *data,
1704 size_t data_size,
1705 size_t *data_length,
1706 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001707{
Gilles Peskine5d309672019-07-12 23:47:28 +02001708#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1709 const psa_drv_se_t *drv;
1710 psa_drv_se_context_t *drv_context;
1711#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1712
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001713 *data_length = 0;
1714
Gilles Peskine8e338702019-07-30 20:06:31 +02001715 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001716 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001717
Gilles Peskinef9168942019-09-12 19:20:29 +02001718 /* Reject a zero-length output buffer now, since this can never be a
1719 * valid key representation. This way we know that data must be a valid
1720 * pointer and we can do things like memset(data, ..., data_size). */
1721 if( data_size == 0 )
1722 return( PSA_ERROR_BUFFER_TOO_SMALL );
1723
Gilles Peskine5d309672019-07-12 23:47:28 +02001724#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001725 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001726 {
1727 psa_drv_se_export_key_t method;
1728 if( drv->key_management == NULL )
1729 return( PSA_ERROR_NOT_SUPPORTED );
1730 method = ( export_public_key ?
1731 drv->key_management->p_export_public :
1732 drv->key_management->p_export );
1733 if( method == NULL )
1734 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001735 return( method( drv_context,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001736 psa_key_slot_get_slot_number( slot ),
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001737 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001738 }
1739#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1740
Gilles Peskine8e338702019-07-30 20:06:31 +02001741 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001742 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001743 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001744 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001745 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1746 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001747 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001748 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001749 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001750 /* Exporting public -> public */
1751 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1752 }
1753 else if( !export_public_key )
1754 {
1755 /* Exporting private -> private */
1756 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1757 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001758
Steven Cooreman560c28a2020-07-24 23:20:24 +02001759 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001760 * so conversion is needed. Try the accelerators first. */
1761 psa_status_t status = psa_driver_wrapper_export_public_key( slot,
1762 data,
1763 data_size,
1764 data_length );
1765
1766 if( status != PSA_ERROR_NOT_SUPPORTED ||
1767 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1768 return( status );
1769
Steven Cooreman560c28a2020-07-24 23:20:24 +02001770 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1771 {
John Durkop0e005192020-10-31 22:06:54 -07001772#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1773 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001774 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001775 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001776 slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001777 slot->key.data,
1778 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001779 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001780 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001781 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001782
Steven Cooreman560c28a2020-07-24 23:20:24 +02001783 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001784 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001785 data,
1786 data_size,
1787 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001788
Steven Cooremana2371e52020-07-28 14:30:39 +02001789 mbedtls_rsa_free( rsa );
1790 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001791
Steven Cooreman560c28a2020-07-24 23:20:24 +02001792 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001793#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001794 /* We don't know how to convert a private RSA key to public. */
1795 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001796#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1797 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001798 }
1799 else
Moran Pekera998bc62018-04-16 18:16:20 +03001800 {
John Durkop6ba40d12020-11-10 08:50:04 -08001801#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1802 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001803 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001804 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001805 slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001806 slot->key.data,
1807 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001808 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001809 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001810 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001811
1812 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1813 PSA_KEY_TYPE_ECC_GET_FAMILY(
1814 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001815 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001816 data,
1817 data_size,
1818 data_length );
1819
Steven Cooremana2371e52020-07-28 14:30:39 +02001820 mbedtls_ecp_keypair_free( ecp );
1821 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001822 return( status );
1823#else
1824 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001825 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001826#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1827 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001828 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001829 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001830 else
1831 {
1832 /* This shouldn't happen in the reference implementation, but
1833 it is valid for a special-purpose implementation to omit
1834 support for exporting certain key types. */
1835 return( PSA_ERROR_NOT_SUPPORTED );
1836 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001837}
1838
Ronald Croncf56a0a2020-08-04 09:51:30 +02001839psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001840 uint8_t *data,
1841 size_t data_size,
1842 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001843{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001844 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001845 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001846 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001847
1848 /* Set the key to empty now, so that even when there are errors, we always
1849 * set data_length to a value between 0 and data_size. On error, setting
1850 * the key to empty is a good choice because an empty key representation is
1851 * unlikely to be accepted anywhere. */
1852 *data_length = 0;
1853
1854 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001855 * which don't require any flag, but
1856 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1857 */
1858 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1859 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001860 if( status != PSA_SUCCESS )
1861 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001862
1863 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001864 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001865
Ronald Cron5c522922020-11-14 16:35:34 +01001866 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001867}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001868
Ronald Croncf56a0a2020-08-04 09:51:30 +02001869psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001870 uint8_t *data,
1871 size_t data_size,
1872 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001873{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001874 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001875 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001876 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001877
1878 /* Set the key to empty now, so that even when there are errors, we always
1879 * set data_length to a value between 0 and data_size. On error, setting
1880 * the key to empty is a good choice because an empty key representation is
1881 * unlikely to be accepted anywhere. */
1882 *data_length = 0;
1883
1884 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001885 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001886 if( status != PSA_SUCCESS )
1887 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001888
1889 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001890 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001891
Ronald Cron5c522922020-11-14 16:35:34 +01001892 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001893}
1894
Gilles Peskine91e8c332019-08-02 19:19:39 +02001895#if defined(static_assert)
1896static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1897 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001898static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001899 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001900static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001901 "One or more key attribute flag is listed as both internal-only and external-only" );
1902#endif
1903
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001904/** Validate that a key policy is internally well-formed.
1905 *
1906 * This function only rejects invalid policies. It does not validate the
1907 * consistency of the policy with respect to other attributes of the key
1908 * such as the key type.
1909 */
1910static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001911{
1912 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001913 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001914 PSA_KEY_USAGE_ENCRYPT |
1915 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001916 PSA_KEY_USAGE_SIGN_HASH |
1917 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001918 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1919 return( PSA_ERROR_INVALID_ARGUMENT );
1920
Gilles Peskine4747d192019-04-17 15:05:45 +02001921 return( PSA_SUCCESS );
1922}
1923
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001924/** Validate the internal consistency of key attributes.
1925 *
1926 * This function only rejects invalid attribute values. If does not
1927 * validate the consistency of the attributes with any key data that may
1928 * be involved in the creation of the key.
1929 *
1930 * Call this function early in the key creation process.
1931 *
1932 * \param[in] attributes Key attributes for the new key.
1933 * \param[out] p_drv On any return, the driver for the key, if any.
1934 * NULL for a transparent key.
1935 *
1936 */
1937static psa_status_t psa_validate_key_attributes(
1938 const psa_key_attributes_t *attributes,
1939 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001940{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001941 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001942 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001943 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001944
Ronald Cron54b90082020-10-29 15:26:43 +01001945 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001946 if( status != PSA_SUCCESS )
1947 return( status );
1948
Ronald Crond2ed4812020-07-17 16:11:30 +02001949 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001950 if( status != PSA_SUCCESS )
1951 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001952
Ronald Cron65f38a32020-10-23 17:11:13 +02001953 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1954 {
1955 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1956 return( PSA_ERROR_INVALID_ARGUMENT );
1957 }
1958 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001959 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001960 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001961 if( status != PSA_SUCCESS )
1962 return( status );
1963 }
1964
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001965 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001966 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001967 return( status );
1968
1969 /* Refuse to create overly large keys.
1970 * Note that this doesn't trigger on import if the attributes don't
1971 * explicitly specify a size (so psa_get_key_bits returns 0), so
1972 * psa_import_key() needs its own checks. */
1973 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1974 return( PSA_ERROR_NOT_SUPPORTED );
1975
Gilles Peskine91e8c332019-08-02 19:19:39 +02001976 /* Reject invalid flags. These should not be reachable through the API. */
1977 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1978 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1979 return( PSA_ERROR_INVALID_ARGUMENT );
1980
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001981 return( PSA_SUCCESS );
1982}
1983
Gilles Peskine4747d192019-04-17 15:05:45 +02001984/** Prepare a key slot to receive key material.
1985 *
1986 * This function allocates a key slot and sets its metadata.
1987 *
1988 * If this function fails, call psa_fail_key_creation().
1989 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001990 * This function is intended to be used as follows:
1991 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001992 * it with the specified attributes, and in case of a volatile key assign it
1993 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001994 * -# Populate the slot with the key material.
1995 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1996 * In case of failure at any step, stop the sequence and call
1997 * psa_fail_key_creation().
1998 *
Ronald Cron5c522922020-11-14 16:35:34 +01001999 * On success, the key slot is locked. It is the responsibility of the caller
2000 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02002001 *
Gilles Peskinedf179142019-07-15 22:02:14 +02002002 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02002003 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02002004 * \param[out] p_slot On success, a pointer to the prepared slot.
2005 * \param[out] p_drv On any return, the driver for the key, if any.
2006 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002007 *
2008 * \retval #PSA_SUCCESS
2009 * The key slot is ready to receive key material.
2010 * \return If this function fails, the key slot is an invalid state.
2011 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002012 */
2013static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02002014 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02002015 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002016 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002017 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02002018{
2019 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02002020 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02002021 psa_key_slot_t *slot;
2022
Gilles Peskinedf179142019-07-15 22:02:14 +02002023 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02002024 *p_drv = NULL;
2025
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002026 status = psa_validate_key_attributes( attributes, p_drv );
2027 if( status != PSA_SUCCESS )
2028 return( status );
2029
Ronald Cronc4d1b512020-07-31 11:26:37 +02002030 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02002031 if( status != PSA_SUCCESS )
2032 return( status );
2033 slot = *p_slot;
2034
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002035 /* We're storing the declared bit-size of the key. It's up to each
2036 * creation mechanism to verify that this information is correct.
2037 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002038 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02002039 * is optional (import, copy). In case of a volatile key, assign it the
2040 * volatile key identifier associated to the slot returned to contain its
2041 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002042
2043 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002044 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2045 {
2046#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2047 slot->attr.id = volatile_key_id;
2048#else
2049 slot->attr.id.key_id = volatile_key_id;
2050#endif
2051 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002052
Gilles Peskine91e8c332019-08-02 19:19:39 +02002053 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002054 * external-only flags, query `attributes`. Thanks to the check
2055 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002056 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002057 * may have set. */
2058 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002059
Gilles Peskinecbaff462019-07-12 23:46:04 +02002060#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002061 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002062 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002063 * create the key file in internal storage, create the
2064 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002065 * persistent data. This is done by starting a transaction that will
2066 * encompass these three actions.
2067 * For registering a volatile key, we just need to find an appropriate
2068 * slot number inside the SE. Since the key is designated volatile, creating
2069 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002070 /* The first thing to do is to find a slot number for the new key.
2071 * We save the slot number in persistent storage as part of the
2072 * transaction data. It will be needed to recover if the power
2073 * fails during the key creation process, to clean up on the secure
2074 * element side after restarting. Obtaining a slot number from the
2075 * secure element driver updates its persistent state, but we do not yet
2076 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002077 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002078 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002079 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01002080 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002081 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002082 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002083 if( status != PSA_SUCCESS )
2084 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002085
2086 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002087 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002088 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2089 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01002090 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002091 psa_crypto_transaction.key.id = slot->attr.id;
2092 status = psa_crypto_save_transaction( );
2093 if( status != PSA_SUCCESS )
2094 {
2095 (void) psa_crypto_stop_transaction( );
2096 return( status );
2097 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002098 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01002099
2100 status = psa_copy_key_material_into_slot(
2101 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00002102 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002103
2104 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2105 {
2106 /* Key registration only makes sense with a secure element. */
2107 return( PSA_ERROR_INVALID_ARGUMENT );
2108 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002109#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2110
Ronald Cronc4d1b512020-07-31 11:26:37 +02002111 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002112}
Gilles Peskine4747d192019-04-17 15:05:45 +02002113
2114/** Finalize the creation of a key once its key material has been set.
2115 *
2116 * This entails writing the key to persistent storage.
2117 *
2118 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002119 * See the documentation of psa_start_key_creation() for the intended use
2120 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002121 *
Ronald Cron5c522922020-11-14 16:35:34 +01002122 * If the finalization succeeds, the function unlocks the key slot (it was
2123 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2124 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002125 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002126 * \param[in,out] slot Pointer to the slot with key material.
2127 * \param[in] driver The secure element driver for the key,
2128 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002129 * \param[out] key On success, identifier of the key. Note that the
2130 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002131 *
2132 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002133 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002134 * \return If this function fails, the key slot is an invalid state.
2135 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002136 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002137static psa_status_t psa_finish_key_creation(
2138 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002139 psa_se_drv_table_entry_t *driver,
2140 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002141{
2142 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002143 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002144 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002145
2146#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002147 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002148 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002149#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2150 if( driver != NULL )
2151 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002152 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01002153 psa_key_slot_number_t slot_number =
2154 psa_key_slot_get_slot_number( slot ) ;
2155
Gilles Peskineb46bef22019-07-30 21:32:04 +02002156#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01002157 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02002158 sizeof( data.slot_number ),
2159 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002160#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01002161 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002162 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002163 (uint8_t*) &data,
2164 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002165 }
2166 else
2167#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2168 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002169 /* Key material is saved in export representation in the slot, so
2170 * just pass the slot buffer for storage. */
2171 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002172 slot->key.data,
2173 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002174 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002175 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002176#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2177
Gilles Peskinecbaff462019-07-12 23:46:04 +02002178#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002179 /* Finish the transaction for a key creation. This does not
2180 * happen when registering an existing key. Detect this case
2181 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002182 * creation of a persistent key in a secure element requires a transaction,
2183 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002184 if( driver != NULL &&
2185 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002186 {
2187 status = psa_save_se_persistent_data( driver );
2188 if( status != PSA_SUCCESS )
2189 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002190 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002191 return( status );
2192 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002193 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002194 }
2195#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2196
Ronald Cron50972942020-11-14 11:28:25 +01002197 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002198 {
2199 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002200 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002201 if( status != PSA_SUCCESS )
2202 *key = MBEDTLS_SVC_KEY_ID_INIT;
2203 }
Ronald Cron50972942020-11-14 11:28:25 +01002204
Gilles Peskine4747d192019-04-17 15:05:45 +02002205 return( status );
2206}
2207
2208/** Abort the creation of a key.
2209 *
2210 * You may call this function after calling psa_start_key_creation(),
2211 * or after psa_finish_key_creation() fails. In other circumstances, this
2212 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002213 * See the documentation of psa_start_key_creation() for the intended use
2214 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002215 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002216 * \param[in,out] slot Pointer to the slot with key material.
2217 * \param[in] driver The secure element driver for the key,
2218 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002219 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002220static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002221 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002222{
Gilles Peskine011e4282019-06-26 18:34:38 +02002223 (void) driver;
2224
Gilles Peskine4747d192019-04-17 15:05:45 +02002225 if( slot == NULL )
2226 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002227
2228#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002229 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002230 * element, and the failure happened later (when saving metadata
2231 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002232 * element.
2233 * https://github.com/ARMmbed/mbed-crypto/issues/217
2234 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002235
Gilles Peskined7729582019-08-05 15:55:54 +02002236 /* Abort the ongoing transaction if any (there may not be one if
2237 * the creation process failed before starting one, or if the
2238 * key creation is a registration of a key in a secure element).
2239 * Earlier functions must already have done what it takes to undo any
2240 * partial creation. All that's left is to update the transaction data
2241 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002242 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002243#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2244
Gilles Peskine4747d192019-04-17 15:05:45 +02002245 psa_wipe_key_slot( slot );
2246}
2247
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002248/** Validate optional attributes during key creation.
2249 *
2250 * Some key attributes are optional during key creation. If they are
2251 * specified in the attributes structure, check that they are consistent
2252 * with the data in the slot.
2253 *
2254 * This function should be called near the end of key creation, after
2255 * the slot in memory is fully populated but before saving persistent data.
2256 */
2257static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002258 const psa_key_slot_t *slot,
2259 const psa_key_attributes_t *attributes )
2260{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002261 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002262 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002263 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002264 return( PSA_ERROR_INVALID_ARGUMENT );
2265 }
2266
2267 if( attributes->domain_parameters_size != 0 )
2268 {
John Durkop0e005192020-10-31 22:06:54 -07002269#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2270 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002271 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002272 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002273 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002274 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002276
Steven Cooreman7f391872020-07-30 14:57:44 +02002277 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002278 slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002279 slot->key.data,
2280 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002281 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002282 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002283 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002284
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002285 mbedtls_mpi_init( &actual );
2286 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002287 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002288 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002289 mbedtls_rsa_free( rsa );
2290 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002291 if( ret != 0 )
2292 goto rsa_exit;
2293 ret = mbedtls_mpi_read_binary( &required,
2294 attributes->domain_parameters,
2295 attributes->domain_parameters_size );
2296 if( ret != 0 )
2297 goto rsa_exit;
2298 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2299 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2300 rsa_exit:
2301 mbedtls_mpi_free( &actual );
2302 mbedtls_mpi_free( &required );
2303 if( ret != 0)
2304 return( mbedtls_to_psa_error( ret ) );
2305 }
2306 else
John Durkop9814fa22020-11-04 12:28:15 -08002307#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2308 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002309 {
2310 return( PSA_ERROR_INVALID_ARGUMENT );
2311 }
2312 }
2313
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002314 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002315 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002316 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002317 return( PSA_ERROR_INVALID_ARGUMENT );
2318 }
2319
2320 return( PSA_SUCCESS );
2321}
2322
Gilles Peskine4747d192019-04-17 15:05:45 +02002323psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002324 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002325 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002326 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002327{
2328 psa_status_t status;
2329 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002330 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002331
Ronald Cron81709fc2020-11-14 12:10:32 +01002332 *key = MBEDTLS_SVC_KEY_ID_INIT;
2333
Gilles Peskine0f84d622019-09-12 19:03:13 +02002334 /* Reject zero-length symmetric keys (including raw data key objects).
2335 * This also rejects any key which might be encoded as an empty string,
2336 * which is never valid. */
2337 if( data_length == 0 )
2338 return( PSA_ERROR_INVALID_ARGUMENT );
2339
Gilles Peskinedf179142019-07-15 22:02:14 +02002340 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002341 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002342 if( status != PSA_SUCCESS )
2343 goto exit;
2344
Gilles Peskine5d309672019-07-12 23:47:28 +02002345#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2346 if( driver != NULL )
2347 {
2348 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002349 /* The driver should set the number of key bits, however in
2350 * case it doesn't, we initialize bits to an invalid value. */
2351 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002352 if( drv->key_management == NULL ||
2353 drv->key_management->p_import == NULL )
2354 {
2355 status = PSA_ERROR_NOT_SUPPORTED;
2356 goto exit;
2357 }
2358 status = drv->key_management->p_import(
2359 psa_get_se_driver_context( driver ),
Ronald Cronea0f8a62020-11-25 17:52:23 +01002360 psa_key_slot_get_slot_number( slot ),
2361 attributes, data, data_length, &bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002362 if( status != PSA_SUCCESS )
2363 goto exit;
2364 if( bits > PSA_MAX_KEY_BITS )
2365 {
2366 status = PSA_ERROR_NOT_SUPPORTED;
2367 goto exit;
2368 }
2369 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002370 }
2371 else
2372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01002373 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
2374 {
2375 /* Importing a key with external lifetime through the driver wrapper
2376 * interface is not yet supported. Return as if this was an invalid
2377 * lifetime. */
2378 status = PSA_ERROR_INVALID_ARGUMENT;
2379 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002380 }
2381 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002382 {
2383 status = psa_import_key_into_slot( slot, data, data_length );
2384 if( status != PSA_SUCCESS )
2385 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002386 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002387 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002388 if( status != PSA_SUCCESS )
2389 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002390
Ronald Cron81709fc2020-11-14 12:10:32 +01002391 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002392exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002393 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002394 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002395
Gilles Peskine4747d192019-04-17 15:05:45 +02002396 return( status );
2397}
2398
Gilles Peskined7729582019-08-05 15:55:54 +02002399#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2400psa_status_t mbedtls_psa_register_se_key(
2401 const psa_key_attributes_t *attributes )
2402{
2403 psa_status_t status;
2404 psa_key_slot_t *slot = NULL;
2405 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002406 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002407
2408 /* Leaving attributes unspecified is not currently supported.
2409 * It could make sense to query the key type and size from the
2410 * secure element, but not all secure elements support this
2411 * and the driver HAL doesn't currently support it. */
2412 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2413 return( PSA_ERROR_NOT_SUPPORTED );
2414 if( psa_get_key_bits( attributes ) == 0 )
2415 return( PSA_ERROR_NOT_SUPPORTED );
2416
2417 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002418 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002419 if( status != PSA_SUCCESS )
2420 goto exit;
2421
Ronald Cron81709fc2020-11-14 12:10:32 +01002422 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002423
2424exit:
2425 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002426 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002427
Gilles Peskined7729582019-08-05 15:55:54 +02002428 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002429 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002430 return( status );
2431}
2432#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2433
Gilles Peskinef603c712019-01-19 13:40:11 +01002434static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002435 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002436{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002437 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002438 source->key.data,
2439 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002440 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002441 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002442
Steven Cooreman398aee52020-10-13 14:35:45 +02002443 target->attr.type = source->attr.type;
2444 target->attr.bits = source->attr.bits;
2445
2446 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002447}
2448
Ronald Croncf56a0a2020-08-04 09:51:30 +02002449psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002450 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002451 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002452{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002453 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002454 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002455 psa_key_slot_t *source_slot = NULL;
2456 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002457 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002458 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002459
Ronald Cron81709fc2020-11-14 12:10:32 +01002460 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2461
Ronald Cron5c522922020-11-14 16:35:34 +01002462 status = psa_get_and_lock_transparent_key_slot_with_policy(
2463 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002464 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002465 goto exit;
2466
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002467 status = psa_validate_optional_attributes( source_slot,
2468 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002469 if( status != PSA_SUCCESS )
2470 goto exit;
2471
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002472 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002473 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002474 if( status != PSA_SUCCESS )
2475 goto exit;
2476
Ronald Cron81709fc2020-11-14 12:10:32 +01002477 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2478 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002479 if( status != PSA_SUCCESS )
2480 goto exit;
2481
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002482#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2483 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002484 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002485 /* Copying to a secure element is not implemented yet. */
2486 status = PSA_ERROR_NOT_SUPPORTED;
2487 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002488 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002489#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002490
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002491 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002492 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002493 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002494
Ronald Cron81709fc2020-11-14 12:10:32 +01002495 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002496exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002497 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002498 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002499
Ronald Cron5c522922020-11-14 16:35:34 +01002500 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002501
Ronald Cron5c522922020-11-14 16:35:34 +01002502 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002503}
2504
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002505
2506
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002507/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002508/* Message digests */
2509/****************************************************************/
2510
John Durkop07cc04a2020-11-16 22:08:34 -08002511#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002512 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2513 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002514 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002515static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002516{
2517 switch( alg )
2518 {
John Durkopee4e6602020-11-27 08:48:46 -08002519#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002520 case PSA_ALG_MD2:
2521 return( &mbedtls_md2_info );
2522#endif
John Durkopee4e6602020-11-27 08:48:46 -08002523#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002524 case PSA_ALG_MD4:
2525 return( &mbedtls_md4_info );
2526#endif
John Durkopee4e6602020-11-27 08:48:46 -08002527#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002528 case PSA_ALG_MD5:
2529 return( &mbedtls_md5_info );
2530#endif
John Durkopee4e6602020-11-27 08:48:46 -08002531#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002532 case PSA_ALG_RIPEMD160:
2533 return( &mbedtls_ripemd160_info );
2534#endif
John Durkopee4e6602020-11-27 08:48:46 -08002535#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002536 case PSA_ALG_SHA_1:
2537 return( &mbedtls_sha1_info );
2538#endif
John Durkopee4e6602020-11-27 08:48:46 -08002539#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002540 case PSA_ALG_SHA_224:
2541 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002542#endif
2543#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002544 case PSA_ALG_SHA_256:
2545 return( &mbedtls_sha256_info );
2546#endif
John Durkopee4e6602020-11-27 08:48:46 -08002547#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002548 case PSA_ALG_SHA_384:
2549 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002550#endif
John Durkopee4e6602020-11-27 08:48:46 -08002551#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002552 case PSA_ALG_SHA_512:
2553 return( &mbedtls_sha512_info );
2554#endif
2555 default:
2556 return( NULL );
2557 }
2558}
John Durkop07cc04a2020-11-16 22:08:34 -08002559#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002560 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2561 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2562 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002563
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002564psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2565{
2566 switch( operation->alg )
2567 {
Gilles Peskine81736312018-06-26 15:04:31 +02002568 case 0:
2569 /* The object has (apparently) been initialized but it is not
2570 * in use. It's ok to call abort on such an object, and there's
2571 * nothing to do. */
2572 break;
John Durkopee4e6602020-11-27 08:48:46 -08002573#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002574 case PSA_ALG_MD2:
2575 mbedtls_md2_free( &operation->ctx.md2 );
2576 break;
2577#endif
John Durkopee4e6602020-11-27 08:48:46 -08002578#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002579 case PSA_ALG_MD4:
2580 mbedtls_md4_free( &operation->ctx.md4 );
2581 break;
2582#endif
John Durkopee4e6602020-11-27 08:48:46 -08002583#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002584 case PSA_ALG_MD5:
2585 mbedtls_md5_free( &operation->ctx.md5 );
2586 break;
2587#endif
John Durkopee4e6602020-11-27 08:48:46 -08002588#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002589 case PSA_ALG_RIPEMD160:
2590 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2591 break;
2592#endif
John Durkopee4e6602020-11-27 08:48:46 -08002593#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002594 case PSA_ALG_SHA_1:
2595 mbedtls_sha1_free( &operation->ctx.sha1 );
2596 break;
2597#endif
John Durkop6ca23272020-12-03 06:01:32 -08002598#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002599 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002600 mbedtls_sha256_free( &operation->ctx.sha256 );
2601 break;
2602#endif
2603#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002604 case PSA_ALG_SHA_256:
2605 mbedtls_sha256_free( &operation->ctx.sha256 );
2606 break;
2607#endif
John Durkop6ca23272020-12-03 06:01:32 -08002608#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002609 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002610 mbedtls_sha512_free( &operation->ctx.sha512 );
2611 break;
2612#endif
2613#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002614 case PSA_ALG_SHA_512:
2615 mbedtls_sha512_free( &operation->ctx.sha512 );
2616 break;
2617#endif
2618 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002619 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002620 }
2621 operation->alg = 0;
2622 return( PSA_SUCCESS );
2623}
2624
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002625psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002626 psa_algorithm_t alg )
2627{
Janos Follath24eed8d2019-11-22 13:21:35 +00002628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002629
2630 /* A context must be freshly initialized before it can be set up. */
2631 if( operation->alg != 0 )
2632 {
2633 return( PSA_ERROR_BAD_STATE );
2634 }
2635
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002636 switch( alg )
2637 {
John Durkopee4e6602020-11-27 08:48:46 -08002638#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002639 case PSA_ALG_MD2:
2640 mbedtls_md2_init( &operation->ctx.md2 );
2641 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2642 break;
2643#endif
John Durkopee4e6602020-11-27 08:48:46 -08002644#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002645 case PSA_ALG_MD4:
2646 mbedtls_md4_init( &operation->ctx.md4 );
2647 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2648 break;
2649#endif
John Durkopee4e6602020-11-27 08:48:46 -08002650#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002651 case PSA_ALG_MD5:
2652 mbedtls_md5_init( &operation->ctx.md5 );
2653 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2654 break;
2655#endif
John Durkopee4e6602020-11-27 08:48:46 -08002656#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002657 case PSA_ALG_RIPEMD160:
2658 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2659 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2660 break;
2661#endif
John Durkopee4e6602020-11-27 08:48:46 -08002662#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002663 case PSA_ALG_SHA_1:
2664 mbedtls_sha1_init( &operation->ctx.sha1 );
2665 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2666 break;
2667#endif
John Durkopee4e6602020-11-27 08:48:46 -08002668#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002669 case PSA_ALG_SHA_224:
2670 mbedtls_sha256_init( &operation->ctx.sha256 );
2671 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2672 break;
John Durkopee4e6602020-11-27 08:48:46 -08002673#endif
2674#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002675 case PSA_ALG_SHA_256:
2676 mbedtls_sha256_init( &operation->ctx.sha256 );
2677 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2678 break;
2679#endif
John Durkopee4e6602020-11-27 08:48:46 -08002680#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002681 case PSA_ALG_SHA_384:
2682 mbedtls_sha512_init( &operation->ctx.sha512 );
2683 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2684 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002685#endif
John Durkopee4e6602020-11-27 08:48:46 -08002686#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002687 case PSA_ALG_SHA_512:
2688 mbedtls_sha512_init( &operation->ctx.sha512 );
2689 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2690 break;
2691#endif
2692 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002693 return( PSA_ALG_IS_HASH( alg ) ?
2694 PSA_ERROR_NOT_SUPPORTED :
2695 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002696 }
2697 if( ret == 0 )
2698 operation->alg = alg;
2699 else
2700 psa_hash_abort( operation );
2701 return( mbedtls_to_psa_error( ret ) );
2702}
2703
2704psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2705 const uint8_t *input,
2706 size_t input_length )
2707{
Janos Follath24eed8d2019-11-22 13:21:35 +00002708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002709
2710 /* Don't require hash implementations to behave correctly on a
2711 * zero-length input, which may have an invalid pointer. */
2712 if( input_length == 0 )
2713 return( PSA_SUCCESS );
2714
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002715 switch( operation->alg )
2716 {
John Durkopee4e6602020-11-27 08:48:46 -08002717#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002718 case PSA_ALG_MD2:
2719 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2720 input, input_length );
2721 break;
2722#endif
John Durkopee4e6602020-11-27 08:48:46 -08002723#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002724 case PSA_ALG_MD4:
2725 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2726 input, input_length );
2727 break;
2728#endif
John Durkopee4e6602020-11-27 08:48:46 -08002729#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002730 case PSA_ALG_MD5:
2731 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2732 input, input_length );
2733 break;
2734#endif
John Durkopee4e6602020-11-27 08:48:46 -08002735#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002736 case PSA_ALG_RIPEMD160:
2737 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2738 input, input_length );
2739 break;
2740#endif
John Durkopee4e6602020-11-27 08:48:46 -08002741#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002742 case PSA_ALG_SHA_1:
2743 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2744 input, input_length );
2745 break;
2746#endif
John Durkop6ca23272020-12-03 06:01:32 -08002747#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002748 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002749 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2750 input, input_length );
2751 break;
2752#endif
2753#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002754 case PSA_ALG_SHA_256:
2755 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2756 input, input_length );
2757 break;
2758#endif
John Durkop6ca23272020-12-03 06:01:32 -08002759#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002760 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002761 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2762 input, input_length );
2763 break;
2764#endif
2765#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002766 case PSA_ALG_SHA_512:
2767 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2768 input, input_length );
2769 break;
2770#endif
2771 default:
John Durkopee4e6602020-11-27 08:48:46 -08002772 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002773 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002774 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002775
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002776 if( ret != 0 )
2777 psa_hash_abort( operation );
2778 return( mbedtls_to_psa_error( ret ) );
2779}
2780
2781psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2782 uint8_t *hash,
2783 size_t hash_size,
2784 size_t *hash_length )
2785{
itayzafrir40835d42018-08-02 13:14:17 +03002786 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002787 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002788 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002789
2790 /* Fill the output buffer with something that isn't a valid hash
2791 * (barring an attack on the hash and deliberately-crafted input),
2792 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002793 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002794 /* If hash_size is 0 then hash may be NULL and then the
2795 * call to memset would have undefined behavior. */
2796 if( hash_size != 0 )
2797 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002798
2799 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002800 {
2801 status = PSA_ERROR_BUFFER_TOO_SMALL;
2802 goto exit;
2803 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002804
2805 switch( operation->alg )
2806 {
John Durkopee4e6602020-11-27 08:48:46 -08002807#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002808 case PSA_ALG_MD2:
2809 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2810 break;
2811#endif
John Durkopee4e6602020-11-27 08:48:46 -08002812#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002813 case PSA_ALG_MD4:
2814 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2815 break;
2816#endif
John Durkopee4e6602020-11-27 08:48:46 -08002817#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002818 case PSA_ALG_MD5:
2819 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2820 break;
2821#endif
John Durkopee4e6602020-11-27 08:48:46 -08002822#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002823 case PSA_ALG_RIPEMD160:
2824 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2825 break;
2826#endif
John Durkopee4e6602020-11-27 08:48:46 -08002827#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002828 case PSA_ALG_SHA_1:
2829 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2830 break;
2831#endif
John Durkop6ca23272020-12-03 06:01:32 -08002832#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002833 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002834 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2835 break;
2836#endif
2837#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002838 case PSA_ALG_SHA_256:
2839 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2840 break;
2841#endif
John Durkop6ca23272020-12-03 06:01:32 -08002842#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002843 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002844 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2845 break;
2846#endif
2847#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002848 case PSA_ALG_SHA_512:
2849 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2850 break;
2851#endif
2852 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002853 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002854 }
itayzafrir40835d42018-08-02 13:14:17 +03002855 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002856
itayzafrir40835d42018-08-02 13:14:17 +03002857exit:
2858 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002859 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002860 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002861 return( psa_hash_abort( operation ) );
2862 }
2863 else
2864 {
2865 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002866 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002867 }
2868}
2869
Gilles Peskine2d277862018-06-18 15:41:12 +02002870psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2871 const uint8_t *hash,
2872 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002873{
2874 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2875 size_t actual_hash_length;
2876 psa_status_t status = psa_hash_finish( operation,
2877 actual_hash, sizeof( actual_hash ),
2878 &actual_hash_length );
2879 if( status != PSA_SUCCESS )
2880 return( status );
2881 if( actual_hash_length != hash_length )
2882 return( PSA_ERROR_INVALID_SIGNATURE );
2883 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2884 return( PSA_ERROR_INVALID_SIGNATURE );
2885 return( PSA_SUCCESS );
2886}
2887
Gilles Peskine0a749c82019-11-28 19:33:58 +01002888psa_status_t psa_hash_compute( psa_algorithm_t alg,
2889 const uint8_t *input, size_t input_length,
2890 uint8_t *hash, size_t hash_size,
2891 size_t *hash_length )
2892{
2893 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2894 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2895
2896 *hash_length = hash_size;
2897 status = psa_hash_setup( &operation, alg );
2898 if( status != PSA_SUCCESS )
2899 goto exit;
2900 status = psa_hash_update( &operation, input, input_length );
2901 if( status != PSA_SUCCESS )
2902 goto exit;
2903 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2904 if( status != PSA_SUCCESS )
2905 goto exit;
2906
2907exit:
2908 if( status == PSA_SUCCESS )
2909 status = psa_hash_abort( &operation );
2910 else
2911 psa_hash_abort( &operation );
2912 return( status );
2913}
2914
2915psa_status_t psa_hash_compare( psa_algorithm_t alg,
2916 const uint8_t *input, size_t input_length,
2917 const uint8_t *hash, size_t hash_length )
2918{
2919 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2920 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2921
2922 status = psa_hash_setup( &operation, alg );
2923 if( status != PSA_SUCCESS )
2924 goto exit;
2925 status = psa_hash_update( &operation, input, input_length );
2926 if( status != PSA_SUCCESS )
2927 goto exit;
2928 status = psa_hash_verify( &operation, hash, hash_length );
2929 if( status != PSA_SUCCESS )
2930 goto exit;
2931
2932exit:
2933 if( status == PSA_SUCCESS )
2934 status = psa_hash_abort( &operation );
2935 else
2936 psa_hash_abort( &operation );
2937 return( status );
2938}
2939
Gilles Peskineeb35d782019-01-22 17:56:16 +01002940psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2941 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002942{
2943 if( target_operation->alg != 0 )
2944 return( PSA_ERROR_BAD_STATE );
2945
2946 switch( source_operation->alg )
2947 {
2948 case 0:
2949 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002950#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002951 case PSA_ALG_MD2:
2952 mbedtls_md2_clone( &target_operation->ctx.md2,
2953 &source_operation->ctx.md2 );
2954 break;
2955#endif
John Durkopee4e6602020-11-27 08:48:46 -08002956#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002957 case PSA_ALG_MD4:
2958 mbedtls_md4_clone( &target_operation->ctx.md4,
2959 &source_operation->ctx.md4 );
2960 break;
2961#endif
John Durkopee4e6602020-11-27 08:48:46 -08002962#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002963 case PSA_ALG_MD5:
2964 mbedtls_md5_clone( &target_operation->ctx.md5,
2965 &source_operation->ctx.md5 );
2966 break;
2967#endif
John Durkopee4e6602020-11-27 08:48:46 -08002968#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002969 case PSA_ALG_RIPEMD160:
2970 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2971 &source_operation->ctx.ripemd160 );
2972 break;
2973#endif
John Durkopee4e6602020-11-27 08:48:46 -08002974#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002975 case PSA_ALG_SHA_1:
2976 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2977 &source_operation->ctx.sha1 );
2978 break;
2979#endif
John Durkop6ca23272020-12-03 06:01:32 -08002980#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002981 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002982 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2983 &source_operation->ctx.sha256 );
2984 break;
2985#endif
2986#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002987 case PSA_ALG_SHA_256:
2988 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2989 &source_operation->ctx.sha256 );
2990 break;
2991#endif
John Durkop6ca23272020-12-03 06:01:32 -08002992#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002993 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002994 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2995 &source_operation->ctx.sha512 );
2996 break;
2997#endif
2998#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002999 case PSA_ALG_SHA_512:
3000 mbedtls_sha512_clone( &target_operation->ctx.sha512,
3001 &source_operation->ctx.sha512 );
3002 break;
3003#endif
3004 default:
3005 return( PSA_ERROR_NOT_SUPPORTED );
3006 }
3007
3008 target_operation->alg = source_operation->alg;
3009 return( PSA_SUCCESS );
3010}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003011
3012
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003013/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01003014/* MAC */
3015/****************************************************************/
3016
Gilles Peskinedc2fc842018-03-07 16:42:59 +01003017static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01003018 psa_algorithm_t alg,
3019 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02003020 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03003021 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003022{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003023 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03003024 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003025
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003026 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003027 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003028
Gilles Peskine8c9def32018-02-08 10:02:12 +01003029 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
3030 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03003031 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003032 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01003033 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034 mode = MBEDTLS_MODE_STREAM;
3035 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003036 case PSA_ALG_CTR:
3037 mode = MBEDTLS_MODE_CTR;
3038 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003039 case PSA_ALG_CFB:
3040 mode = MBEDTLS_MODE_CFB;
3041 break;
3042 case PSA_ALG_OFB:
3043 mode = MBEDTLS_MODE_OFB;
3044 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003045 case PSA_ALG_ECB_NO_PADDING:
3046 mode = MBEDTLS_MODE_ECB;
3047 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003048 case PSA_ALG_CBC_NO_PADDING:
3049 mode = MBEDTLS_MODE_CBC;
3050 break;
3051 case PSA_ALG_CBC_PKCS7:
3052 mode = MBEDTLS_MODE_CBC;
3053 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003054 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003055 mode = MBEDTLS_MODE_CCM;
3056 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003057 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003058 mode = MBEDTLS_MODE_GCM;
3059 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003060 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3061 mode = MBEDTLS_MODE_CHACHAPOLY;
3062 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003063 default:
3064 return( NULL );
3065 }
3066 }
3067 else if( alg == PSA_ALG_CMAC )
3068 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003069 else
3070 return( NULL );
3071
3072 switch( key_type )
3073 {
3074 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003075 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003076 break;
3077 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003078 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3079 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003080 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003081 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003082 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003083 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003084 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3085 * but two-key Triple-DES is functionally three-key Triple-DES
3086 * with K1=K3, so that's how we present it to mbedtls. */
3087 if( key_bits == 128 )
3088 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089 break;
3090 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003091 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003092 break;
3093 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003094 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003095 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003096 case PSA_KEY_TYPE_CHACHA20:
3097 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3098 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003099 default:
3100 return( NULL );
3101 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003102 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003103 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003104
Jaeden Amero23bbb752018-06-26 14:16:54 +01003105 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3106 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003107}
3108
John Durkop6ba40d12020-11-10 08:50:04 -08003109#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003110static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003111{
Gilles Peskine2d277862018-06-18 15:41:12 +02003112 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003113 {
3114 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003115 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003116 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003117 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003118 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003119 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003120 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003121 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003122 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003123 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003124 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003125 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003126 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003127 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003128 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003129 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003130 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003131 return( 128 );
3132 default:
3133 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003134 }
3135}
John Durkop07cc04a2020-11-16 22:08:34 -08003136#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003137
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003138/* Initialize the MAC operation structure. Once this function has been
3139 * called, psa_mac_abort can run and will do the right thing. */
3140static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3141 psa_algorithm_t alg )
3142{
3143 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3144
3145 operation->alg = alg;
3146 operation->key_set = 0;
3147 operation->iv_set = 0;
3148 operation->iv_required = 0;
3149 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003150 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003151
3152#if defined(MBEDTLS_CMAC_C)
3153 if( alg == PSA_ALG_CMAC )
3154 {
3155 operation->iv_required = 0;
3156 mbedtls_cipher_init( &operation->ctx.cmac );
3157 status = PSA_SUCCESS;
3158 }
3159 else
3160#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003161#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003162 if( PSA_ALG_IS_HMAC( operation->alg ) )
3163 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003164 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3165 operation->ctx.hmac.hash_ctx.alg = 0;
3166 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003167 }
3168 else
John Durkopd0321952020-10-29 21:37:36 -07003169#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003170 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003171 if( ! PSA_ALG_IS_MAC( alg ) )
3172 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003173 }
3174
3175 if( status != PSA_SUCCESS )
3176 memset( operation, 0, sizeof( *operation ) );
3177 return( status );
3178}
3179
John Durkop6ba40d12020-11-10 08:50:04 -08003180#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003181static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3182{
Gilles Peskine3f108122018-12-07 18:14:53 +01003183 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003184 return( psa_hash_abort( &hmac->hash_ctx ) );
3185}
John Durkop6ba40d12020-11-10 08:50:04 -08003186#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003187
Gilles Peskine8c9def32018-02-08 10:02:12 +01003188psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3189{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003190 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003191 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003192 /* The object has (apparently) been initialized but it is not
3193 * in use. It's ok to call abort on such an object, and there's
3194 * nothing to do. */
3195 return( PSA_SUCCESS );
3196 }
3197 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003198#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003199 if( operation->alg == PSA_ALG_CMAC )
3200 {
3201 mbedtls_cipher_free( &operation->ctx.cmac );
3202 }
3203 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003204#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003205#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003206 if( PSA_ALG_IS_HMAC( operation->alg ) )
3207 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003208 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003209 }
3210 else
John Durkopd0321952020-10-29 21:37:36 -07003211#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003212 {
3213 /* Sanity check (shouldn't happen: operation->alg should
3214 * always have been initialized to a valid value). */
3215 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003216 }
Moran Peker41deec42018-04-04 15:43:05 +03003217
Gilles Peskine8c9def32018-02-08 10:02:12 +01003218 operation->alg = 0;
3219 operation->key_set = 0;
3220 operation->iv_set = 0;
3221 operation->iv_required = 0;
3222 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003223 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003224
Gilles Peskine8c9def32018-02-08 10:02:12 +01003225 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003226
3227bad_state:
3228 /* If abort is called on an uninitialized object, we can't trust
3229 * anything. Wipe the object in case it contains confidential data.
3230 * This may result in a memory leak if a pointer gets overwritten,
3231 * but it's too late to do anything about this. */
3232 memset( operation, 0, sizeof( *operation ) );
3233 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003234}
3235
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003236#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003237static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003238 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003239 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003240 const mbedtls_cipher_info_t *cipher_info )
3241{
Janos Follath24eed8d2019-11-22 13:21:35 +00003242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003243
3244 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003245
3246 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3247 if( ret != 0 )
3248 return( ret );
3249
3250 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003251 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003252 key_bits );
3253 return( ret );
3254}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003255#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003256
John Durkop6ba40d12020-11-10 08:50:04 -08003257#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003258static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3259 const uint8_t *key,
3260 size_t key_length,
3261 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003262{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003263 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003264 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003265 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003266 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003267 psa_status_t status;
3268
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003269 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3270 * overflow below. This should never trigger if the hash algorithm
3271 * is implemented correctly. */
3272 /* The size checks against the ipad and opad buffers cannot be written
3273 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3274 * because that triggers -Wlogical-op on GCC 7.3. */
3275 if( block_size > sizeof( ipad ) )
3276 return( PSA_ERROR_NOT_SUPPORTED );
3277 if( block_size > sizeof( hmac->opad ) )
3278 return( PSA_ERROR_NOT_SUPPORTED );
3279 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003280 return( PSA_ERROR_NOT_SUPPORTED );
3281
Gilles Peskined223b522018-06-11 18:12:58 +02003282 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003283 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003284 status = psa_hash_compute( hash_alg, key, key_length,
3285 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003286 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003287 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003288 }
Gilles Peskine96889972018-07-12 17:07:03 +02003289 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3290 * but it is permitted. It is common when HMAC is used in HKDF, for
3291 * example. Don't call `memcpy` in the 0-length because `key` could be
3292 * an invalid pointer which would make the behavior undefined. */
3293 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003294 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003295
Gilles Peskined223b522018-06-11 18:12:58 +02003296 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3297 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003298 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003299 ipad[i] ^= 0x36;
3300 memset( ipad + key_length, 0x36, block_size - key_length );
3301
3302 /* Copy the key material from ipad to opad, flipping the requisite bits,
3303 * and filling the rest of opad with the requisite constant. */
3304 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003305 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3306 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003307
Gilles Peskine01126fa2018-07-12 17:04:55 +02003308 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003309 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003310 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003311
Gilles Peskine01126fa2018-07-12 17:04:55 +02003312 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003313
3314cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003315 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003316
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003317 return( status );
3318}
John Durkop6ba40d12020-11-10 08:50:04 -08003319#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003320
Gilles Peskine89167cb2018-07-08 20:12:23 +02003321static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003322 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003323 psa_algorithm_t alg,
3324 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003325{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003327 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003328 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003329 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003330 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003331 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003332 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003333 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003334
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003335 /* A context must be freshly initialized before it can be set up. */
3336 if( operation->alg != 0 )
3337 {
3338 return( PSA_ERROR_BAD_STATE );
3339 }
3340
Gilles Peskined911eb72018-08-14 15:18:45 +02003341 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003342 if( status != PSA_SUCCESS )
3343 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003344 if( is_sign )
3345 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003346
Ronald Cron5c522922020-11-14 16:35:34 +01003347 status = psa_get_and_lock_transparent_key_slot_with_policy(
3348 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003349 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003350 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003351 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003352
Gilles Peskine8c9def32018-02-08 10:02:12 +01003353#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003354 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003355 {
3356 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003357 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003358 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003360 if( cipher_info == NULL )
3361 {
3362 status = PSA_ERROR_NOT_SUPPORTED;
3363 goto exit;
3364 }
3365 operation->mac_size = cipher_info->block_size;
3366 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3367 status = mbedtls_to_psa_error( ret );
3368 }
3369 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003370#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003371#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003372 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003373 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003374 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003375 if( hash_alg == 0 )
3376 {
3377 status = PSA_ERROR_NOT_SUPPORTED;
3378 goto exit;
3379 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003380
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003381 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003382 /* Sanity check. This shouldn't fail on a valid configuration. */
3383 if( operation->mac_size == 0 ||
3384 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3385 {
3386 status = PSA_ERROR_NOT_SUPPORTED;
3387 goto exit;
3388 }
3389
Gilles Peskine8e338702019-07-30 20:06:31 +02003390 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003391 {
3392 status = PSA_ERROR_INVALID_ARGUMENT;
3393 goto exit;
3394 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003395
Gilles Peskine01126fa2018-07-12 17:04:55 +02003396 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003397 slot->key.data,
3398 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003399 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003400 }
3401 else
John Durkopd0321952020-10-29 21:37:36 -07003402#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003403 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003404 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003405 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003406 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003407
Gilles Peskined911eb72018-08-14 15:18:45 +02003408 if( truncated == 0 )
3409 {
3410 /* The "normal" case: untruncated algorithm. Nothing to do. */
3411 }
3412 else if( truncated < 4 )
3413 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003414 /* A very short MAC is too short for security since it can be
3415 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3416 * so we make this our minimum, even though 32 bits is still
3417 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003418 status = PSA_ERROR_NOT_SUPPORTED;
3419 }
3420 else if( truncated > operation->mac_size )
3421 {
3422 /* It's impossible to "truncate" to a larger length. */
3423 status = PSA_ERROR_INVALID_ARGUMENT;
3424 }
3425 else
3426 operation->mac_size = truncated;
3427
Gilles Peskinefbfac682018-07-08 20:51:54 +02003428exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003429 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003430 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003431 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003432 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003433 else
3434 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003435 operation->key_set = 1;
3436 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003437
Ronald Cron5c522922020-11-14 16:35:34 +01003438 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003439
Ronald Cron5c522922020-11-14 16:35:34 +01003440 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003441}
3442
Gilles Peskine89167cb2018-07-08 20:12:23 +02003443psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003444 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003445 psa_algorithm_t alg )
3446{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003447 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003448}
3449
3450psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003451 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003452 psa_algorithm_t alg )
3453{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003454 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003455}
3456
Gilles Peskine8c9def32018-02-08 10:02:12 +01003457psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3458 const uint8_t *input,
3459 size_t input_length )
3460{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003461 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003462 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003463 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003464 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003465 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003466 operation->has_input = 1;
3467
Gilles Peskine8c9def32018-02-08 10:02:12 +01003468#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003469 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003470 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003471 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3472 input, input_length );
3473 status = mbedtls_to_psa_error( ret );
3474 }
3475 else
3476#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003477#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003478 if( PSA_ALG_IS_HMAC( operation->alg ) )
3479 {
3480 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3481 input_length );
3482 }
3483 else
John Durkopd0321952020-10-29 21:37:36 -07003484#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003485 {
3486 /* This shouldn't happen if `operation` was initialized by
3487 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003488 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003489 }
3490
Gilles Peskinefbfac682018-07-08 20:51:54 +02003491 if( status != PSA_SUCCESS )
3492 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003493 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003494}
3495
John Durkop6ba40d12020-11-10 08:50:04 -08003496#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003497static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3498 uint8_t *mac,
3499 size_t mac_size )
3500{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003501 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003502 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3503 size_t hash_size = 0;
3504 size_t block_size = psa_get_hash_block_size( hash_alg );
3505 psa_status_t status;
3506
Gilles Peskine01126fa2018-07-12 17:04:55 +02003507 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3508 if( status != PSA_SUCCESS )
3509 return( status );
3510 /* From here on, tmp needs to be wiped. */
3511
3512 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3513 if( status != PSA_SUCCESS )
3514 goto exit;
3515
3516 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3517 if( status != PSA_SUCCESS )
3518 goto exit;
3519
3520 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3521 if( status != PSA_SUCCESS )
3522 goto exit;
3523
Gilles Peskined911eb72018-08-14 15:18:45 +02003524 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3525 if( status != PSA_SUCCESS )
3526 goto exit;
3527
3528 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003529
3530exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003531 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003532 return( status );
3533}
John Durkop6ba40d12020-11-10 08:50:04 -08003534#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003535
mohammad16036df908f2018-04-02 08:34:15 -07003536static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003537 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003538 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003539{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003540 if( ! operation->key_set )
3541 return( PSA_ERROR_BAD_STATE );
3542 if( operation->iv_required && ! operation->iv_set )
3543 return( PSA_ERROR_BAD_STATE );
3544
Gilles Peskine8c9def32018-02-08 10:02:12 +01003545 if( mac_size < operation->mac_size )
3546 return( PSA_ERROR_BUFFER_TOO_SMALL );
3547
Gilles Peskine8c9def32018-02-08 10:02:12 +01003548#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003549 if( operation->alg == PSA_ALG_CMAC )
3550 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003551 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003552 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3553 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003554 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003555 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003556 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003557 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003558 else
3559#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003560#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003561 if( PSA_ALG_IS_HMAC( operation->alg ) )
3562 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003563 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003564 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003565 }
3566 else
John Durkopd0321952020-10-29 21:37:36 -07003567#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003568 {
3569 /* This shouldn't happen if `operation` was initialized by
3570 * a setup function. */
3571 return( PSA_ERROR_BAD_STATE );
3572 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003573}
3574
Gilles Peskineacd4be32018-07-08 19:56:25 +02003575psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3576 uint8_t *mac,
3577 size_t mac_size,
3578 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003579{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003580 psa_status_t status;
3581
Jaeden Amero252ef282019-02-15 14:05:35 +00003582 if( operation->alg == 0 )
3583 {
3584 return( PSA_ERROR_BAD_STATE );
3585 }
3586
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003587 /* Fill the output buffer with something that isn't a valid mac
3588 * (barring an attack on the mac and deliberately-crafted input),
3589 * in case the caller doesn't check the return status properly. */
3590 *mac_length = mac_size;
3591 /* If mac_size is 0 then mac may be NULL and then the
3592 * call to memset would have undefined behavior. */
3593 if( mac_size != 0 )
3594 memset( mac, '!', mac_size );
3595
Gilles Peskine89167cb2018-07-08 20:12:23 +02003596 if( ! operation->is_sign )
3597 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003598 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003599 }
mohammad16036df908f2018-04-02 08:34:15 -07003600
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003601 status = psa_mac_finish_internal( operation, mac, mac_size );
3602
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003603 if( status == PSA_SUCCESS )
3604 {
3605 status = psa_mac_abort( operation );
3606 if( status == PSA_SUCCESS )
3607 *mac_length = operation->mac_size;
3608 else
3609 memset( mac, '!', mac_size );
3610 }
3611 else
3612 psa_mac_abort( operation );
3613 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003614}
3615
Gilles Peskineacd4be32018-07-08 19:56:25 +02003616psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3617 const uint8_t *mac,
3618 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003619{
Gilles Peskine828ed142018-06-18 23:25:51 +02003620 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003621 psa_status_t status;
3622
Jaeden Amero252ef282019-02-15 14:05:35 +00003623 if( operation->alg == 0 )
3624 {
3625 return( PSA_ERROR_BAD_STATE );
3626 }
3627
Gilles Peskine89167cb2018-07-08 20:12:23 +02003628 if( operation->is_sign )
3629 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003630 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003631 }
3632 if( operation->mac_size != mac_length )
3633 {
3634 status = PSA_ERROR_INVALID_SIGNATURE;
3635 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003636 }
mohammad16036df908f2018-04-02 08:34:15 -07003637
3638 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003639 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003640 if( status != PSA_SUCCESS )
3641 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003642
3643 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3644 status = PSA_ERROR_INVALID_SIGNATURE;
3645
3646cleanup:
3647 if( status == PSA_SUCCESS )
3648 status = psa_mac_abort( operation );
3649 else
3650 psa_mac_abort( operation );
3651
Gilles Peskine3f108122018-12-07 18:14:53 +01003652 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003653
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003654 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003655}
3656
3657
Gilles Peskine20035e32018-02-03 22:44:14 +01003658
Gilles Peskine20035e32018-02-03 22:44:14 +01003659/****************************************************************/
3660/* Asymmetric cryptography */
3661/****************************************************************/
3662
John Durkop6ba40d12020-11-10 08:50:04 -08003663#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3664 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003665/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003666 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003667static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3668 size_t hash_length,
3669 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003670{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003671 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003672 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003673 *md_alg = mbedtls_md_get_type( md_info );
3674
3675 /* The Mbed TLS RSA module uses an unsigned int for hash length
3676 * parameters. Validate that it fits so that we don't risk an
3677 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003678#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003679 if( hash_length > UINT_MAX )
3680 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003681#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003682
John Durkop0e005192020-10-31 22:06:54 -07003683#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003684 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3685 * must be correct. */
3686 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3687 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003688 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003689 if( md_info == NULL )
3690 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003691 if( mbedtls_md_get_size( md_info ) != hash_length )
3692 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003693 }
John Durkop0e005192020-10-31 22:06:54 -07003694#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003695
John Durkop0e005192020-10-31 22:06:54 -07003696#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003697 /* PSS requires a hash internally. */
3698 if( PSA_ALG_IS_RSA_PSS( alg ) )
3699 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003700 if( md_info == NULL )
3701 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003702 }
John Durkop0e005192020-10-31 22:06:54 -07003703#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003704
Gilles Peskine61b91d42018-06-08 16:09:36 +02003705 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003706}
3707
Gilles Peskine2b450e32018-06-27 15:42:46 +02003708static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3709 psa_algorithm_t alg,
3710 const uint8_t *hash,
3711 size_t hash_length,
3712 uint8_t *signature,
3713 size_t signature_size,
3714 size_t *signature_length )
3715{
3716 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003718 mbedtls_md_type_t md_alg;
3719
3720 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3721 if( status != PSA_SUCCESS )
3722 return( status );
3723
Gilles Peskine630a18a2018-06-29 17:49:35 +02003724 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003725 return( PSA_ERROR_BUFFER_TOO_SMALL );
3726
John Durkop0e005192020-10-31 22:06:54 -07003727#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003728 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3729 {
3730 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3731 MBEDTLS_MD_NONE );
3732 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003733 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003734 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003735 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003736 md_alg,
3737 (unsigned int) hash_length,
3738 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003739 signature );
3740 }
3741 else
John Durkop0e005192020-10-31 22:06:54 -07003742#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3743#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003744 if( PSA_ALG_IS_RSA_PSS( alg ) )
3745 {
3746 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3747 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003748 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003749 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003750 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003751 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003752 (unsigned int) hash_length,
3753 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003754 signature );
3755 }
3756 else
John Durkop0e005192020-10-31 22:06:54 -07003757#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003758 {
3759 return( PSA_ERROR_INVALID_ARGUMENT );
3760 }
3761
3762 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003763 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003764 return( mbedtls_to_psa_error( ret ) );
3765}
3766
3767static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3768 psa_algorithm_t alg,
3769 const uint8_t *hash,
3770 size_t hash_length,
3771 const uint8_t *signature,
3772 size_t signature_length )
3773{
3774 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003776 mbedtls_md_type_t md_alg;
3777
3778 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3779 if( status != PSA_SUCCESS )
3780 return( status );
3781
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003782 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3783 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003784
John Durkop0e005192020-10-31 22:06:54 -07003785#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003786 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3787 {
3788 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3789 MBEDTLS_MD_NONE );
3790 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003791 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003792 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003793 MBEDTLS_RSA_PUBLIC,
3794 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003795 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003796 hash,
3797 signature );
3798 }
3799 else
John Durkop0e005192020-10-31 22:06:54 -07003800#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3801#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003802 if( PSA_ALG_IS_RSA_PSS( alg ) )
3803 {
3804 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3805 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003806 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003807 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003808 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003809 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003810 (unsigned int) hash_length,
3811 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003812 signature );
3813 }
3814 else
John Durkop0e005192020-10-31 22:06:54 -07003815#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003816 {
3817 return( PSA_ERROR_INVALID_ARGUMENT );
3818 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003819
3820 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3821 * the rest of the signature is invalid". This has little use in
3822 * practice and PSA doesn't report this distinction. */
3823 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3824 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003825 return( mbedtls_to_psa_error( ret ) );
3826}
John Durkop6ba40d12020-11-10 08:50:04 -08003827#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3828 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003829
John Durkop6ba40d12020-11-10 08:50:04 -08003830#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3831 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003832/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3833 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3834 * (even though these functions don't modify it). */
3835static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3836 psa_algorithm_t alg,
3837 const uint8_t *hash,
3838 size_t hash_length,
3839 uint8_t *signature,
3840 size_t signature_size,
3841 size_t *signature_length )
3842{
Janos Follath24eed8d2019-11-22 13:21:35 +00003843 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003844 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003845 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003846 mbedtls_mpi_init( &r );
3847 mbedtls_mpi_init( &s );
3848
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003849 if( signature_size < 2 * curve_bytes )
3850 {
3851 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3852 goto cleanup;
3853 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003854
John Durkop0ea39e02020-10-13 19:58:20 -07003855#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003856 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3857 {
3858 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3859 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3860 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003861 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3862 &ecp->d, hash,
3863 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003864 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003865 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003866 }
3867 else
John Durkop0ea39e02020-10-13 19:58:20 -07003868#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003869 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003870 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003871 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3872 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003873 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003874 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003875 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003876
3877 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3878 signature,
3879 curve_bytes ) );
3880 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3881 signature + curve_bytes,
3882 curve_bytes ) );
3883
3884cleanup:
3885 mbedtls_mpi_free( &r );
3886 mbedtls_mpi_free( &s );
3887 if( ret == 0 )
3888 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003889 return( mbedtls_to_psa_error( ret ) );
3890}
3891
3892static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3893 const uint8_t *hash,
3894 size_t hash_length,
3895 const uint8_t *signature,
3896 size_t signature_length )
3897{
Janos Follath24eed8d2019-11-22 13:21:35 +00003898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003899 mbedtls_mpi r, s;
3900 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3901 mbedtls_mpi_init( &r );
3902 mbedtls_mpi_init( &s );
3903
3904 if( signature_length != 2 * curve_bytes )
3905 return( PSA_ERROR_INVALID_SIGNATURE );
3906
3907 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3908 signature,
3909 curve_bytes ) );
3910 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3911 signature + curve_bytes,
3912 curve_bytes ) );
3913
Steven Cooremanacda8342020-07-24 23:09:52 +02003914 /* Check whether the public part is loaded. If not, load it. */
3915 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3916 {
3917 MBEDTLS_MPI_CHK(
3918 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003919 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003920 }
3921
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003922 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3923 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003924
3925cleanup:
3926 mbedtls_mpi_free( &r );
3927 mbedtls_mpi_free( &s );
3928 return( mbedtls_to_psa_error( ret ) );
3929}
John Durkop6ba40d12020-11-10 08:50:04 -08003930#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3931 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003932
Ronald Croncf56a0a2020-08-04 09:51:30 +02003933psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003934 psa_algorithm_t alg,
3935 const uint8_t *hash,
3936 size_t hash_length,
3937 uint8_t *signature,
3938 size_t signature_size,
3939 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003940{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003941 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003942 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003943 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003944
3945 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003946 /* Immediately reject a zero-length signature buffer. This guarantees
3947 * that signature must be a valid pointer. (On the other hand, the hash
3948 * buffer can in principle be empty since it doesn't actually have
3949 * to be a hash.) */
3950 if( signature_size == 0 )
3951 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003952
Ronald Cron5c522922020-11-14 16:35:34 +01003953 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3954 PSA_KEY_USAGE_SIGN_HASH,
3955 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003956 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003957 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003958 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003959 {
3960 status = PSA_ERROR_INVALID_ARGUMENT;
3961 goto exit;
3962 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003963
Steven Cooremancd84cb42020-07-16 20:28:36 +02003964 /* Try any of the available accelerators first */
3965 status = psa_driver_wrapper_sign_hash( slot,
3966 alg,
3967 hash,
3968 hash_length,
3969 signature,
3970 signature_size,
3971 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003972 if( status != PSA_ERROR_NOT_SUPPORTED ||
3973 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003974 goto exit;
3975
Steven Cooreman7a250572020-07-17 16:43:05 +02003976 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003977#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3978 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003979 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003980 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003981 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003982
Steven Cooreman4fed4552020-08-03 14:46:03 +02003983 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003984 slot->key.data,
3985 slot->key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003986 &rsa );
3987 if( status != PSA_SUCCESS )
3988 goto exit;
3989
Steven Cooremana2371e52020-07-28 14:30:39 +02003990 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003991 alg,
3992 hash, hash_length,
3993 signature, signature_size,
3994 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003995
Steven Cooremana2371e52020-07-28 14:30:39 +02003996 mbedtls_rsa_free( rsa );
3997 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003998 }
3999 else
John Durkop6ba40d12020-11-10 08:50:04 -08004000#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4001 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004002 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01004003 {
John Durkop9814fa22020-11-04 12:28:15 -08004004#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4005 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01004006 if(
John Durkop0ea39e02020-10-13 19:58:20 -07004007#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01004008 PSA_ALG_IS_ECDSA( alg )
4009#else
4010 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
4011#endif
4012 )
Steven Cooremanacda8342020-07-24 23:09:52 +02004013 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004014 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004015 status = psa_load_ecp_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004016 slot->key.data,
4017 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004018 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004019 if( status != PSA_SUCCESS )
4020 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004021 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004022 alg,
4023 hash, hash_length,
4024 signature, signature_size,
4025 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004026 mbedtls_ecp_keypair_free( ecp );
4027 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004028 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004029 else
John Durkop9814fa22020-11-04 12:28:15 -08004030#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4031 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004032 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004033 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004034 }
itayzafrir5c753392018-05-08 11:18:38 +03004035 }
4036 else
itayzafrir5c753392018-05-08 11:18:38 +03004037 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004038 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004039 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004040
4041exit:
4042 /* Fill the unused part of the output buffer (the whole buffer on error,
4043 * the trailing part on success) with something that isn't a valid mac
4044 * (barring an attack on the mac and deliberately-crafted input),
4045 * in case the caller doesn't check the return status properly. */
4046 if( status == PSA_SUCCESS )
4047 memset( signature + *signature_length, '!',
4048 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02004049 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004050 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004051 /* If signature_size is 0 then we have nothing to do. We must not call
4052 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02004053
Ronald Cron5c522922020-11-14 16:35:34 +01004054 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004055
Ronald Cron5c522922020-11-14 16:35:34 +01004056 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03004057}
4058
Ronald Croncf56a0a2020-08-04 09:51:30 +02004059psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004060 psa_algorithm_t alg,
4061 const uint8_t *hash,
4062 size_t hash_length,
4063 const uint8_t *signature,
4064 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03004065{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004066 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004067 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004068 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004069
Ronald Cron5c522922020-11-14 16:35:34 +01004070 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4071 PSA_KEY_USAGE_VERIFY_HASH,
4072 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004073 if( status != PSA_SUCCESS )
4074 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004075
Steven Cooreman55ae2172020-07-17 19:46:15 +02004076 /* Try any of the available accelerators first */
4077 status = psa_driver_wrapper_verify_hash( slot,
4078 alg,
4079 hash,
4080 hash_length,
4081 signature,
4082 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004083 if( status != PSA_ERROR_NOT_SUPPORTED ||
4084 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004085 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004086
John Durkop6ba40d12020-11-10 08:50:04 -08004087#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4088 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004089 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004090 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004091 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004092
Steven Cooreman4fed4552020-08-03 14:46:03 +02004093 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004094 slot->key.data,
4095 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004096 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004097 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004098 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004099
Steven Cooremana2371e52020-07-28 14:30:39 +02004100 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004101 alg,
4102 hash, hash_length,
4103 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004104 mbedtls_rsa_free( rsa );
4105 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004106 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004107 }
4108 else
John Durkop6ba40d12020-11-10 08:50:04 -08004109#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4110 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004111 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004112 {
John Durkop9814fa22020-11-04 12:28:15 -08004113#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4114 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004115 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004116 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004117 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004118 status = psa_load_ecp_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004119 slot->key.data,
4120 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004121 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004122 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004123 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004124 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004125 hash, hash_length,
4126 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004127 mbedtls_ecp_keypair_free( ecp );
4128 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004129 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004130 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004131 else
John Durkop9814fa22020-11-04 12:28:15 -08004132#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4133 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004134 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004135 status = PSA_ERROR_INVALID_ARGUMENT;
4136 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004137 }
itayzafrir5c753392018-05-08 11:18:38 +03004138 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004139 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004140 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004141 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004142 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004143
4144exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004145 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004146
Ronald Cron5c522922020-11-14 16:35:34 +01004147 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004148}
4149
John Durkop0e005192020-10-31 22:06:54 -07004150#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004151static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4152 mbedtls_rsa_context *rsa )
4153{
4154 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4155 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4156 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4157 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4158}
John Durkop0e005192020-10-31 22:06:54 -07004159#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004160
Ronald Croncf56a0a2020-08-04 09:51:30 +02004161psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004162 psa_algorithm_t alg,
4163 const uint8_t *input,
4164 size_t input_length,
4165 const uint8_t *salt,
4166 size_t salt_length,
4167 uint8_t *output,
4168 size_t output_size,
4169 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004170{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004171 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004172 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004173 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004174
Darryl Green5cc689a2018-07-24 15:34:10 +01004175 (void) input;
4176 (void) input_length;
4177 (void) salt;
4178 (void) output;
4179 (void) output_size;
4180
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004181 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004182
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004183 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4184 return( PSA_ERROR_INVALID_ARGUMENT );
4185
Ronald Cron5c522922020-11-14 16:35:34 +01004186 status = psa_get_and_lock_transparent_key_slot_with_policy(
4187 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004188 if( status != PSA_SUCCESS )
4189 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004190 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4191 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004192 {
4193 status = PSA_ERROR_INVALID_ARGUMENT;
4194 goto exit;
4195 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004196
John Durkop6ba40d12020-11-10 08:50:04 -08004197#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4198 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004199 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004200 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004201 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004202 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004203 slot->key.data,
4204 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004205 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004206 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004207 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004208
4209 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004210 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004211 status = PSA_ERROR_BUFFER_TOO_SMALL;
4212 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004213 }
John Durkop0e005192020-10-31 22:06:54 -07004214#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004215 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004216 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004217 status = mbedtls_to_psa_error(
4218 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004219 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004220 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004221 MBEDTLS_RSA_PUBLIC,
4222 input_length,
4223 input,
4224 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004225 }
4226 else
John Durkop0e005192020-10-31 22:06:54 -07004227#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4228#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004229 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004230 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004231 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004232 status = mbedtls_to_psa_error(
4233 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004234 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004235 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004236 MBEDTLS_RSA_PUBLIC,
4237 salt, salt_length,
4238 input_length,
4239 input,
4240 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004241 }
4242 else
John Durkop0e005192020-10-31 22:06:54 -07004243#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004244 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004245 status = PSA_ERROR_INVALID_ARGUMENT;
4246 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004247 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004248rsa_exit:
4249 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004250 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004251
Steven Cooremana2371e52020-07-28 14:30:39 +02004252 mbedtls_rsa_free( rsa );
4253 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004254 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004255 else
John Durkop9814fa22020-11-04 12:28:15 -08004256#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004257 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004258 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004259 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004260 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004261
4262exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004263 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004264
Ronald Cron5c522922020-11-14 16:35:34 +01004265 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004266}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004267
Ronald Croncf56a0a2020-08-04 09:51:30 +02004268psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004269 psa_algorithm_t alg,
4270 const uint8_t *input,
4271 size_t input_length,
4272 const uint8_t *salt,
4273 size_t salt_length,
4274 uint8_t *output,
4275 size_t output_size,
4276 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004277{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004278 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004279 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004280 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004281
Darryl Green5cc689a2018-07-24 15:34:10 +01004282 (void) input;
4283 (void) input_length;
4284 (void) salt;
4285 (void) output;
4286 (void) output_size;
4287
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004288 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004289
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004290 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4291 return( PSA_ERROR_INVALID_ARGUMENT );
4292
Ronald Cron5c522922020-11-14 16:35:34 +01004293 status = psa_get_and_lock_transparent_key_slot_with_policy(
4294 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004295 if( status != PSA_SUCCESS )
4296 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004297 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004298 {
4299 status = PSA_ERROR_INVALID_ARGUMENT;
4300 goto exit;
4301 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004302
John Durkop6ba40d12020-11-10 08:50:04 -08004303#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4304 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004305 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004306 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004307 mbedtls_rsa_context *rsa = NULL;
4308 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004309 slot->key.data,
4310 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004311 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004312 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004313 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004314
Steven Cooremana2371e52020-07-28 14:30:39 +02004315 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004316 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004317 status = PSA_ERROR_INVALID_ARGUMENT;
4318 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004319 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004320
John Durkop0e005192020-10-31 22:06:54 -07004321#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004322 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004323 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004324 status = mbedtls_to_psa_error(
4325 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004326 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004327 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004328 MBEDTLS_RSA_PRIVATE,
4329 output_length,
4330 input,
4331 output,
4332 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004333 }
4334 else
John Durkop0e005192020-10-31 22:06:54 -07004335#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4336#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004337 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004338 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004339 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004340 status = mbedtls_to_psa_error(
4341 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004342 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004343 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004344 MBEDTLS_RSA_PRIVATE,
4345 salt, salt_length,
4346 output_length,
4347 input,
4348 output,
4349 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004350 }
4351 else
John Durkop0e005192020-10-31 22:06:54 -07004352#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004353 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004354 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004355 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004356
Steven Cooreman4fed4552020-08-03 14:46:03 +02004357rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004358 mbedtls_rsa_free( rsa );
4359 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004360 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004361 else
John Durkop6ba40d12020-11-10 08:50:04 -08004362#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4363 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004364 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004365 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004366 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004367
4368exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004369 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004370
Ronald Cron5c522922020-11-14 16:35:34 +01004371 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004372}
Gilles Peskine20035e32018-02-03 22:44:14 +01004373
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004374
4375
mohammad1603503973b2018-03-12 15:59:30 +02004376/****************************************************************/
4377/* Symmetric cryptography */
4378/****************************************************************/
4379
Gilles Peskinee553c652018-06-04 16:22:46 +02004380static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004381 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004382 psa_algorithm_t alg,
4383 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004384{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004385 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004386 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004387 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004388 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004389 size_t key_bits;
4390 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004391 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4392 PSA_KEY_USAGE_ENCRYPT :
4393 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004394
Steven Cooremand3feccd2020-09-01 15:56:14 +02004395 /* A context must be freshly initialized before it can be set up. */
4396 if( operation->alg != 0 )
4397 return( PSA_ERROR_BAD_STATE );
4398
Steven Cooremana07b9972020-09-10 14:54:14 +02004399 /* The requested algorithm must be one that can be processed by cipher. */
4400 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004401 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004402
Steven Cooremanef8575e2020-09-11 11:44:50 +02004403 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004404 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004405 if( status != PSA_SUCCESS )
4406 goto exit;
4407
4408 /* Initialize the operation struct members, except for alg. The alg member
4409 * is used to indicate to psa_cipher_abort that there are resources to free,
4410 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004411 operation->key_set = 0;
4412 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004413 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004414 operation->iv_size = 0;
4415 operation->block_size = 0;
4416 if( alg == PSA_ALG_ECB_NO_PADDING )
4417 operation->iv_required = 0;
4418 else
4419 operation->iv_required = 1;
4420
Steven Cooremana07b9972020-09-10 14:54:14 +02004421 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004422 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004423 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004424 slot,
4425 alg );
4426 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004427 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004428 slot,
4429 alg );
4430
Steven Cooremanef8575e2020-09-11 11:44:50 +02004431 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004432 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004433 /* Once the driver context is initialised, it needs to be freed using
4434 * psa_cipher_abort. Indicate this through setting alg. */
4435 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004436 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004437
Steven Cooremand3feccd2020-09-01 15:56:14 +02004438 if( status != PSA_ERROR_NOT_SUPPORTED ||
4439 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4440 goto exit;
4441
Steven Cooremana07b9972020-09-10 14:54:14 +02004442 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004443 * available for the given algorithm & key. */
4444 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004445
Steven Cooremana07b9972020-09-10 14:54:14 +02004446 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004447 * psa_cipher_abort. Indicate there is something to be freed through setting
4448 * alg, and indicate the operation is being done using mbedtls crypto through
4449 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004450 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004451 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004452
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004453 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004454 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004455 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004456 {
4457 status = PSA_ERROR_NOT_SUPPORTED;
4458 goto exit;
4459 }
mohammad1603503973b2018-03-12 15:59:30 +02004460
mohammad1603503973b2018-03-12 15:59:30 +02004461 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004462 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004463 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004464
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004465#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004466 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004467 {
4468 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004469 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004470 memcpy( keys, slot->key.data, 16 );
4471 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004472 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4473 keys,
4474 192, cipher_operation );
4475 }
4476 else
4477#endif
4478 {
4479 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004480 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004481 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004482 }
Moran Peker41deec42018-04-04 15:43:05 +03004483 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004484 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004485
mohammad16038481e742018-03-18 13:57:31 +02004486#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004487 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004488 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004489 case PSA_ALG_CBC_NO_PADDING:
4490 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4491 MBEDTLS_PADDING_NONE );
4492 break;
4493 case PSA_ALG_CBC_PKCS7:
4494 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4495 MBEDTLS_PADDING_PKCS7 );
4496 break;
4497 default:
4498 /* The algorithm doesn't involve padding. */
4499 ret = 0;
4500 break;
4501 }
4502 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004503 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004504#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4505
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004506 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004507 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004508 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4509 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004510 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004511 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004512 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004513#if defined(MBEDTLS_CHACHA20_C)
4514 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004515 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004516 operation->iv_size = 12;
4517#endif
mohammad1603503973b2018-03-12 15:59:30 +02004518
Steven Cooreman7df02922020-09-09 15:28:49 +02004519 status = PSA_SUCCESS;
4520
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004521exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004522 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004523 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004524 if( status == PSA_SUCCESS )
4525 {
4526 /* Update operation flags for both driver and software implementations */
4527 operation->key_set = 1;
4528 }
4529 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004530 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004531
Ronald Cron5c522922020-11-14 16:35:34 +01004532 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004533
Ronald Cron5c522922020-11-14 16:35:34 +01004534 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004535}
4536
Gilles Peskinefe119512018-07-08 21:39:34 +02004537psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004538 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004539 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004540{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004541 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004542}
4543
Gilles Peskinefe119512018-07-08 21:39:34 +02004544psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004545 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004546 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004547{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004548 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004549}
4550
Gilles Peskinefe119512018-07-08 21:39:34 +02004551psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004552 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004553 size_t iv_size,
4554 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004555{
itayzafrir534bd7c2018-08-02 13:56:32 +03004556 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004558 if( operation->iv_set || ! operation->iv_required )
4559 {
4560 return( PSA_ERROR_BAD_STATE );
4561 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004562
Steven Cooremanef8575e2020-09-11 11:44:50 +02004563 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004564 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004565 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004566 iv,
4567 iv_size,
4568 iv_length );
4569 goto exit;
4570 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004571
Moran Peker41deec42018-04-04 15:43:05 +03004572 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004573 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004574 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004575 goto exit;
4576 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004577 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004578 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004579 if( ret != 0 )
4580 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004581 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004582 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004583 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004584
mohammad16038481e742018-03-18 13:57:31 +02004585 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004586 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004587
Moran Peker395db872018-05-31 14:07:14 +03004588exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004589 if( status == PSA_SUCCESS )
4590 operation->iv_set = 1;
4591 else
Moran Peker395db872018-05-31 14:07:14 +03004592 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004593 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004594}
4595
Gilles Peskinefe119512018-07-08 21:39:34 +02004596psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004597 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004598 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004599{
itayzafrir534bd7c2018-08-02 13:56:32 +03004600 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004602 if( operation->iv_set || ! operation->iv_required )
4603 {
4604 return( PSA_ERROR_BAD_STATE );
4605 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004606
Steven Cooremanef8575e2020-09-11 11:44:50 +02004607 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004608 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004609 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004610 iv,
4611 iv_length );
4612 goto exit;
4613 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004614
Moran Pekera28258c2018-05-29 16:25:04 +03004615 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004616 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004617 status = PSA_ERROR_INVALID_ARGUMENT;
4618 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004619 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004620 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4621 status = mbedtls_to_psa_error( ret );
4622exit:
4623 if( status == PSA_SUCCESS )
4624 operation->iv_set = 1;
4625 else
mohammad1603503973b2018-03-12 15:59:30 +02004626 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004627 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004628}
4629
Steven Cooreman177deba2020-09-07 17:14:14 +02004630/* Process input for which the algorithm is set to ECB mode. This requires
4631 * manual processing, since the PSA API is defined as being able to process
4632 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4633 * underlying mbedtls_cipher_update only takes full blocks. */
4634static psa_status_t psa_cipher_update_ecb_internal(
4635 mbedtls_cipher_context_t *ctx,
4636 const uint8_t *input,
4637 size_t input_length,
4638 uint8_t *output,
4639 size_t output_size,
4640 size_t *output_length )
4641{
4642 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4643 size_t block_size = ctx->cipher_info->block_size;
4644 size_t internal_output_length = 0;
4645 *output_length = 0;
4646
4647 if( input_length == 0 )
4648 {
4649 status = PSA_SUCCESS;
4650 goto exit;
4651 }
4652
4653 if( ctx->unprocessed_len > 0 )
4654 {
4655 /* Fill up to block size, and run the block if there's a full one. */
4656 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4657
4658 if( input_length < bytes_to_copy )
4659 bytes_to_copy = input_length;
4660
4661 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4662 input, bytes_to_copy );
4663 input_length -= bytes_to_copy;
4664 input += bytes_to_copy;
4665 ctx->unprocessed_len += bytes_to_copy;
4666
4667 if( ctx->unprocessed_len == block_size )
4668 {
4669 status = mbedtls_to_psa_error(
4670 mbedtls_cipher_update( ctx,
4671 ctx->unprocessed_data,
4672 block_size,
4673 output, &internal_output_length ) );
4674
4675 if( status != PSA_SUCCESS )
4676 goto exit;
4677
4678 output += internal_output_length;
4679 output_size -= internal_output_length;
4680 *output_length += internal_output_length;
4681 ctx->unprocessed_len = 0;
4682 }
4683 }
4684
4685 while( input_length >= block_size )
4686 {
4687 /* Run all full blocks we have, one by one */
4688 status = mbedtls_to_psa_error(
4689 mbedtls_cipher_update( ctx, input,
4690 block_size,
4691 output, &internal_output_length ) );
4692
4693 if( status != PSA_SUCCESS )
4694 goto exit;
4695
4696 input_length -= block_size;
4697 input += block_size;
4698
4699 output += internal_output_length;
4700 output_size -= internal_output_length;
4701 *output_length += internal_output_length;
4702 }
4703
4704 if( input_length > 0 )
4705 {
4706 /* Save unprocessed bytes for later processing */
4707 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4708 input, input_length );
4709 ctx->unprocessed_len += input_length;
4710 }
4711
4712 status = PSA_SUCCESS;
4713
4714exit:
4715 return( status );
4716}
4717
Gilles Peskinee553c652018-06-04 16:22:46 +02004718psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4719 const uint8_t *input,
4720 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004721 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004722 size_t output_size,
4723 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004724{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004726 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004727 if( operation->alg == 0 )
4728 {
4729 return( PSA_ERROR_BAD_STATE );
4730 }
4731 if( operation->iv_required && ! operation->iv_set )
4732 {
4733 return( PSA_ERROR_BAD_STATE );
4734 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004735
Steven Cooremanef8575e2020-09-11 11:44:50 +02004736 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004737 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004738 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004739 input,
4740 input_length,
4741 output,
4742 output_size,
4743 output_length );
4744 goto exit;
4745 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004746
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004747 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004748 {
4749 /* Take the unprocessed partial block left over from previous
4750 * update calls, if any, plus the input to this call. Remove
4751 * the last partial block, if any. You get the data that will be
4752 * output in this call. */
4753 expected_output_size =
4754 ( operation->ctx.cipher.unprocessed_len + input_length )
4755 / operation->block_size * operation->block_size;
4756 }
4757 else
4758 {
4759 expected_output_size = input_length;
4760 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004761
Gilles Peskine89d789c2018-06-04 17:17:16 +02004762 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004763 {
4764 status = PSA_ERROR_BUFFER_TOO_SMALL;
4765 goto exit;
4766 }
mohammad160382759612018-03-12 18:16:40 +02004767
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004768 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4769 {
4770 /* mbedtls_cipher_update has an API inconsistency: it will only
4771 * process a single block at a time in ECB mode. Abstract away that
4772 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004773 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4774 input,
4775 input_length,
4776 output,
4777 output_size,
4778 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004779 }
4780 else
4781 {
4782 status = mbedtls_to_psa_error(
4783 mbedtls_cipher_update( &operation->ctx.cipher, input,
4784 input_length, output, output_length ) );
4785 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004786exit:
4787 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004788 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004789 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004790}
4791
Gilles Peskinee553c652018-06-04 16:22:46 +02004792psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4793 uint8_t *output,
4794 size_t output_size,
4795 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004796{
David Saadab4ecc272019-02-14 13:48:10 +02004797 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004798 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004799 if( operation->alg == 0 )
4800 {
4801 return( PSA_ERROR_BAD_STATE );
4802 }
4803 if( operation->iv_required && ! operation->iv_set )
4804 {
4805 return( PSA_ERROR_BAD_STATE );
4806 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004807
Steven Cooremanef8575e2020-09-11 11:44:50 +02004808 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004809 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004810 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004811 output,
4812 output_size,
4813 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004814 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004815 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004816
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004817 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004818 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004819 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004820 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004821 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004822 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004823 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004824 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004825 }
4826
Steven Cooremanef8575e2020-09-11 11:44:50 +02004827 status = mbedtls_to_psa_error(
4828 mbedtls_cipher_finish( &operation->ctx.cipher,
4829 temp_output_buffer,
4830 output_length ) );
4831 if( status != PSA_SUCCESS )
4832 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004833
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004834 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004835 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004836 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004837 memcpy( output, temp_output_buffer, *output_length );
4838 else
Janos Follath315b51c2018-07-09 16:04:51 +01004839 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004840
Steven Cooremanef8575e2020-09-11 11:44:50 +02004841exit:
4842 if( operation->mbedtls_in_use == 1 )
4843 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004844
Steven Cooremanef8575e2020-09-11 11:44:50 +02004845 if( status == PSA_SUCCESS )
4846 return( psa_cipher_abort( operation ) );
4847 else
4848 {
4849 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004850 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004851
Steven Cooremanef8575e2020-09-11 11:44:50 +02004852 return( status );
4853 }
mohammad1603503973b2018-03-12 15:59:30 +02004854}
4855
Gilles Peskinee553c652018-06-04 16:22:46 +02004856psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4857{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004858 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004859 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004860 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004861 * in use. It's ok to call abort on such an object, and there's
4862 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004863 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004864 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004865
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004866 /* Sanity check (shouldn't happen: operation->alg should
4867 * always have been initialized to a valid value). */
4868 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4869 return( PSA_ERROR_BAD_STATE );
4870
Steven Cooremanef8575e2020-09-11 11:44:50 +02004871 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004872 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004873 else
4874 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004875
Moran Peker41deec42018-04-04 15:43:05 +03004876 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004877 operation->key_set = 0;
4878 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004879 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004880 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004881 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004882 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004883
Moran Peker395db872018-05-31 14:07:14 +03004884 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004885}
4886
Gilles Peskinea0655c32018-04-30 17:06:50 +02004887
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004888
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004889
mohammad16035955c982018-04-26 00:53:03 +03004890/****************************************************************/
4891/* AEAD */
4892/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004893
Gilles Peskineedf9a652018-08-17 18:11:56 +02004894typedef struct
4895{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004896 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004897 const mbedtls_cipher_info_t *cipher_info;
4898 union
4899 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004900 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004901#if defined(MBEDTLS_CCM_C)
4902 mbedtls_ccm_context ccm;
4903#endif /* MBEDTLS_CCM_C */
4904#if defined(MBEDTLS_GCM_C)
4905 mbedtls_gcm_context gcm;
4906#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004907#if defined(MBEDTLS_CHACHAPOLY_C)
4908 mbedtls_chachapoly_context chachapoly;
4909#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004910 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004911 psa_algorithm_t core_alg;
4912 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004913 uint8_t tag_length;
4914} aead_operation_t;
4915
Ronald Cronf95a2b12020-10-22 15:24:49 +02004916#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4917
Gilles Peskine30a9e412019-01-14 18:36:12 +01004918static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004919{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004920 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004921 {
4922#if defined(MBEDTLS_CCM_C)
4923 case PSA_ALG_CCM:
4924 mbedtls_ccm_free( &operation->ctx.ccm );
4925 break;
4926#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004927#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004928 case PSA_ALG_GCM:
4929 mbedtls_gcm_free( &operation->ctx.gcm );
4930 break;
4931#endif /* MBEDTLS_GCM_C */
4932 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004933
Ronald Cron5c522922020-11-14 16:35:34 +01004934 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004935}
4936
4937static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004938 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004939 psa_key_usage_t usage,
4940 psa_algorithm_t alg )
4941{
4942 psa_status_t status;
4943 size_t key_bits;
4944 mbedtls_cipher_id_t cipher_id;
4945
Ronald Cron5c522922020-11-14 16:35:34 +01004946 status = psa_get_and_lock_transparent_key_slot_with_policy(
4947 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004948 if( status != PSA_SUCCESS )
4949 return( status );
4950
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004951 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004952
4953 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004954 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004955 &cipher_id );
4956 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004957 {
4958 status = PSA_ERROR_NOT_SUPPORTED;
4959 goto cleanup;
4960 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004961
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004962 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004963 {
4964#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004965 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4966 operation->core_alg = PSA_ALG_CCM;
4967 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004968 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4969 * The call to mbedtls_ccm_encrypt_and_tag or
4970 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004971 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004972 {
4973 status = PSA_ERROR_INVALID_ARGUMENT;
4974 goto cleanup;
4975 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004976 mbedtls_ccm_init( &operation->ctx.ccm );
4977 status = mbedtls_to_psa_error(
4978 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004979 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004980 (unsigned int) key_bits ) );
4981 if( status != 0 )
4982 goto cleanup;
4983 break;
4984#endif /* MBEDTLS_CCM_C */
4985
4986#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004987 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4988 operation->core_alg = PSA_ALG_GCM;
4989 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004990 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4991 * The call to mbedtls_gcm_crypt_and_tag or
4992 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004993 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004994 {
4995 status = PSA_ERROR_INVALID_ARGUMENT;
4996 goto cleanup;
4997 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004998 mbedtls_gcm_init( &operation->ctx.gcm );
4999 status = mbedtls_to_psa_error(
5000 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005001 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02005002 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005003 if( status != 0 )
5004 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005005 break;
5006#endif /* MBEDTLS_GCM_C */
5007
Gilles Peskine26869f22019-05-06 15:25:00 +02005008#if defined(MBEDTLS_CHACHAPOLY_C)
5009 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
5010 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
5011 operation->full_tag_length = 16;
5012 /* We only support the default tag length. */
5013 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02005014 {
5015 status = PSA_ERROR_NOT_SUPPORTED;
5016 goto cleanup;
5017 }
Gilles Peskine26869f22019-05-06 15:25:00 +02005018 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
5019 status = mbedtls_to_psa_error(
5020 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005021 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02005022 if( status != 0 )
5023 goto cleanup;
5024 break;
5025#endif /* MBEDTLS_CHACHAPOLY_C */
5026
Gilles Peskineedf9a652018-08-17 18:11:56 +02005027 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02005028 status = PSA_ERROR_NOT_SUPPORTED;
5029 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005030 }
5031
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005032 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
5033 {
5034 status = PSA_ERROR_INVALID_ARGUMENT;
5035 goto cleanup;
5036 }
5037 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005038
Gilles Peskineedf9a652018-08-17 18:11:56 +02005039 return( PSA_SUCCESS );
5040
5041cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005042 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005043 return( status );
5044}
5045
Ronald Croncf56a0a2020-08-04 09:51:30 +02005046psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005047 psa_algorithm_t alg,
5048 const uint8_t *nonce,
5049 size_t nonce_length,
5050 const uint8_t *additional_data,
5051 size_t additional_data_length,
5052 const uint8_t *plaintext,
5053 size_t plaintext_length,
5054 uint8_t *ciphertext,
5055 size_t ciphertext_size,
5056 size_t *ciphertext_length )
5057{
mohammad16035955c982018-04-26 00:53:03 +03005058 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005059 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03005060 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02005061
mohammad1603f08a5502018-06-03 15:05:47 +03005062 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07005063
Ronald Croncf56a0a2020-08-04 09:51:30 +02005064 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005065 if( status != PSA_SUCCESS )
5066 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005067
Gilles Peskineedf9a652018-08-17 18:11:56 +02005068 /* For all currently supported modes, the tag is at the end of the
5069 * ciphertext. */
5070 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5071 {
5072 status = PSA_ERROR_BUFFER_TOO_SMALL;
5073 goto exit;
5074 }
5075 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005076
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005077#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005078 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005079 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005080 status = mbedtls_to_psa_error(
5081 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5082 MBEDTLS_GCM_ENCRYPT,
5083 plaintext_length,
5084 nonce, nonce_length,
5085 additional_data, additional_data_length,
5086 plaintext, ciphertext,
5087 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005088 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005089 else
5090#endif /* MBEDTLS_GCM_C */
5091#if defined(MBEDTLS_CCM_C)
5092 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005093 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005094 status = mbedtls_to_psa_error(
5095 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5096 plaintext_length,
5097 nonce, nonce_length,
5098 additional_data,
5099 additional_data_length,
5100 plaintext, ciphertext,
5101 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005102 }
mohammad16035c8845f2018-05-09 05:40:09 -07005103 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005104#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005105#if defined(MBEDTLS_CHACHAPOLY_C)
5106 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5107 {
5108 if( nonce_length != 12 || operation.tag_length != 16 )
5109 {
5110 status = PSA_ERROR_NOT_SUPPORTED;
5111 goto exit;
5112 }
5113 status = mbedtls_to_psa_error(
5114 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5115 plaintext_length,
5116 nonce,
5117 additional_data,
5118 additional_data_length,
5119 plaintext,
5120 ciphertext,
5121 tag ) );
5122 }
5123 else
5124#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005125 {
mohammad1603554faad2018-06-03 15:07:38 +03005126 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005127 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005128
Gilles Peskineedf9a652018-08-17 18:11:56 +02005129 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5130 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005131
Gilles Peskineedf9a652018-08-17 18:11:56 +02005132exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005133 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005134 if( status == PSA_SUCCESS )
5135 *ciphertext_length = plaintext_length + operation.tag_length;
5136 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005137}
5138
Gilles Peskineee652a32018-06-01 19:23:52 +02005139/* Locate the tag in a ciphertext buffer containing the encrypted data
5140 * followed by the tag. Return the length of the part preceding the tag in
5141 * *plaintext_length. This is the size of the plaintext in modes where
5142 * the encrypted data has the same size as the plaintext, such as
5143 * CCM and GCM. */
5144static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5145 const uint8_t *ciphertext,
5146 size_t ciphertext_length,
5147 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005148 const uint8_t **p_tag )
5149{
5150 size_t payload_length;
5151 if( tag_length > ciphertext_length )
5152 return( PSA_ERROR_INVALID_ARGUMENT );
5153 payload_length = ciphertext_length - tag_length;
5154 if( payload_length > plaintext_size )
5155 return( PSA_ERROR_BUFFER_TOO_SMALL );
5156 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005157 return( PSA_SUCCESS );
5158}
5159
Ronald Croncf56a0a2020-08-04 09:51:30 +02005160psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005161 psa_algorithm_t alg,
5162 const uint8_t *nonce,
5163 size_t nonce_length,
5164 const uint8_t *additional_data,
5165 size_t additional_data_length,
5166 const uint8_t *ciphertext,
5167 size_t ciphertext_length,
5168 uint8_t *plaintext,
5169 size_t plaintext_size,
5170 size_t *plaintext_length )
5171{
mohammad16035955c982018-04-26 00:53:03 +03005172 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005173 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005174 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005175
Gilles Peskineee652a32018-06-01 19:23:52 +02005176 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005177
Ronald Croncf56a0a2020-08-04 09:51:30 +02005178 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005179 if( status != PSA_SUCCESS )
5180 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005181
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005182 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5183 ciphertext, ciphertext_length,
5184 plaintext_size, &tag );
5185 if( status != PSA_SUCCESS )
5186 goto exit;
5187
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005188#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005189 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005190 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005191 status = mbedtls_to_psa_error(
5192 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5193 ciphertext_length - operation.tag_length,
5194 nonce, nonce_length,
5195 additional_data,
5196 additional_data_length,
5197 tag, operation.tag_length,
5198 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005199 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005200 else
5201#endif /* MBEDTLS_GCM_C */
5202#if defined(MBEDTLS_CCM_C)
5203 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005204 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005205 status = mbedtls_to_psa_error(
5206 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5207 ciphertext_length - operation.tag_length,
5208 nonce, nonce_length,
5209 additional_data,
5210 additional_data_length,
5211 ciphertext, plaintext,
5212 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005213 }
mohammad160339574652018-06-01 04:39:53 -07005214 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005215#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005216#if defined(MBEDTLS_CHACHAPOLY_C)
5217 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5218 {
5219 if( nonce_length != 12 || operation.tag_length != 16 )
5220 {
5221 status = PSA_ERROR_NOT_SUPPORTED;
5222 goto exit;
5223 }
5224 status = mbedtls_to_psa_error(
5225 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5226 ciphertext_length - operation.tag_length,
5227 nonce,
5228 additional_data,
5229 additional_data_length,
5230 tag,
5231 ciphertext,
5232 plaintext ) );
5233 }
5234 else
5235#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005236 {
mohammad1603554faad2018-06-03 15:07:38 +03005237 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005238 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005239
Gilles Peskineedf9a652018-08-17 18:11:56 +02005240 if( status != PSA_SUCCESS && plaintext_size != 0 )
5241 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005242
Gilles Peskineedf9a652018-08-17 18:11:56 +02005243exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005244 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005245 if( status == PSA_SUCCESS )
5246 *plaintext_length = ciphertext_length - operation.tag_length;
5247 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005248}
5249
Gilles Peskinea0655c32018-04-30 17:06:50 +02005250
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005251
Gilles Peskine20035e32018-02-03 22:44:14 +01005252/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005253/* Generators */
5254/****************************************************************/
5255
John Durkop07cc04a2020-11-16 22:08:34 -08005256#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5257 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5258 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5259#define AT_LEAST_ONE_BUILTIN_KDF
5260#endif
5261
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005262#define HKDF_STATE_INIT 0 /* no input yet */
5263#define HKDF_STATE_STARTED 1 /* got salt */
5264#define HKDF_STATE_KEYED 2 /* got key */
5265#define HKDF_STATE_OUTPUT 3 /* output started */
5266
Gilles Peskinecbe66502019-05-16 16:59:18 +02005267static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005268 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005269{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005270 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5271 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005272 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005273 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005274}
5275
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005277{
5278 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005280 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005281 {
5282 /* The object has (apparently) been initialized but it is not
5283 * in use. It's ok to call abort on such an object, and there's
5284 * nothing to do. */
5285 }
5286 else
John Durkopd0321952020-10-29 21:37:36 -07005287#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005288 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005289 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 mbedtls_free( operation->ctx.hkdf.info );
5291 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005292 }
John Durkop07cc04a2020-11-16 22:08:34 -08005293 else
5294#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5295#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5296 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5297 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005298 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005299 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005300 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005301 if( operation->ctx.tls12_prf.seed != NULL )
5302 {
5303 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5304 operation->ctx.tls12_prf.seed_length );
5305 mbedtls_free( operation->ctx.tls12_prf.seed );
5306 }
5307
5308 if( operation->ctx.tls12_prf.label != NULL )
5309 {
5310 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5311 operation->ctx.tls12_prf.label_length );
5312 mbedtls_free( operation->ctx.tls12_prf.label );
5313 }
5314
5315 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5316
5317 /* We leave the fields Ai and output_block to be erased safely by the
5318 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005319 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005320 else
John Durkop07cc04a2020-11-16 22:08:34 -08005321#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5322 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005323 {
5324 status = PSA_ERROR_BAD_STATE;
5325 }
Janos Follath083036a2019-06-11 10:22:26 +01005326 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005327 return( status );
5328}
5329
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005330psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005331 size_t *capacity)
5332{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005333 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005334 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005335 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005336 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005337 }
5338
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005339 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005340 return( PSA_SUCCESS );
5341}
5342
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005343psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005344 size_t capacity )
5345{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005346 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005347 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005348 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005349 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005350 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005351 return( PSA_SUCCESS );
5352}
5353
John Durkopd0321952020-10-29 21:37:36 -07005354#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005355/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005356 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005357static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005358 psa_algorithm_t hash_alg,
5359 uint8_t *output,
5360 size_t output_length )
5361{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005362 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005363 psa_status_t status;
5364
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005365 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5366 return( PSA_ERROR_BAD_STATE );
5367 hkdf->state = HKDF_STATE_OUTPUT;
5368
Gilles Peskinebef7f142018-07-12 17:22:21 +02005369 while( output_length != 0 )
5370 {
5371 /* Copy what remains of the current block */
5372 uint8_t n = hash_length - hkdf->offset_in_block;
5373 if( n > output_length )
5374 n = (uint8_t) output_length;
5375 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5376 output += n;
5377 output_length -= n;
5378 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005379 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005380 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005381 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005382 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005383 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005384 * object was corrupted or if this function is called directly
5385 * inside the library. */
5386 if( hkdf->block_number == 0xff )
5387 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005388
5389 /* We need a new block */
5390 ++hkdf->block_number;
5391 hkdf->offset_in_block = 0;
5392 status = psa_hmac_setup_internal( &hkdf->hmac,
5393 hkdf->prk, hash_length,
5394 hash_alg );
5395 if( status != PSA_SUCCESS )
5396 return( status );
5397 if( hkdf->block_number != 1 )
5398 {
5399 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5400 hkdf->output_block,
5401 hash_length );
5402 if( status != PSA_SUCCESS )
5403 return( status );
5404 }
5405 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5406 hkdf->info,
5407 hkdf->info_length );
5408 if( status != PSA_SUCCESS )
5409 return( status );
5410 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5411 &hkdf->block_number, 1 );
5412 if( status != PSA_SUCCESS )
5413 return( status );
5414 status = psa_hmac_finish_internal( &hkdf->hmac,
5415 hkdf->output_block,
5416 sizeof( hkdf->output_block ) );
5417 if( status != PSA_SUCCESS )
5418 return( status );
5419 }
5420
5421 return( PSA_SUCCESS );
5422}
John Durkop07cc04a2020-11-16 22:08:34 -08005423#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005424
John Durkop07cc04a2020-11-16 22:08:34 -08005425#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5426 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005427static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5428 psa_tls12_prf_key_derivation_t *tls12_prf,
5429 psa_algorithm_t alg )
5430{
5431 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005432 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005433 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5434 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005435
Janos Follath7742fee2019-06-17 12:58:10 +01005436 /* We can't be wanting more output after block 0xff, otherwise
5437 * the capacity check in psa_key_derivation_output_bytes() would have
5438 * prevented this call. It could happen only if the operation
5439 * object was corrupted or if this function is called directly
5440 * inside the library. */
5441 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005442 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005443
5444 /* We need a new block */
5445 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005446 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005447
5448 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5449 *
5450 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5451 *
5452 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5453 * HMAC_hash(secret, A(2) + seed) +
5454 * HMAC_hash(secret, A(3) + seed) + ...
5455 *
5456 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005457 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005458 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005459 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005460 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005461 * is currently extracted as `output_block` and where i is
5462 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005463 */
5464
Janos Follathea29bfb2019-06-19 12:21:20 +01005465 /* Save the hash context before using it, to preserve the hash state with
5466 * only the inner padding in it. We need this, because inner padding depends
5467 * on the key (secret in the RFC's terminology). */
5468 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5469 if( status != PSA_SUCCESS )
5470 goto cleanup;
5471
5472 /* Calculate A(i) where i = tls12_prf->block_number. */
5473 if( tls12_prf->block_number == 1 )
5474 {
5475 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5476 * the variable seed and in this instance means it in the context of the
5477 * P_hash function, where seed = label + seed.) */
5478 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5479 tls12_prf->label, tls12_prf->label_length );
5480 if( status != PSA_SUCCESS )
5481 goto cleanup;
5482 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5483 tls12_prf->seed, tls12_prf->seed_length );
5484 if( status != PSA_SUCCESS )
5485 goto cleanup;
5486 }
5487 else
5488 {
5489 /* A(i) = HMAC_hash(secret, A(i-1)) */
5490 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5491 tls12_prf->Ai, hash_length );
5492 if( status != PSA_SUCCESS )
5493 goto cleanup;
5494 }
5495
5496 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5497 tls12_prf->Ai, hash_length );
5498 if( status != PSA_SUCCESS )
5499 goto cleanup;
5500 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5501 if( status != PSA_SUCCESS )
5502 goto cleanup;
5503
5504 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5505 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5506 tls12_prf->Ai, hash_length );
5507 if( status != PSA_SUCCESS )
5508 goto cleanup;
5509 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5510 tls12_prf->label, tls12_prf->label_length );
5511 if( status != PSA_SUCCESS )
5512 goto cleanup;
5513 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5514 tls12_prf->seed, tls12_prf->seed_length );
5515 if( status != PSA_SUCCESS )
5516 goto cleanup;
5517 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5518 tls12_prf->output_block, hash_length );
5519 if( status != PSA_SUCCESS )
5520 goto cleanup;
5521 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5522 if( status != PSA_SUCCESS )
5523 goto cleanup;
5524
Janos Follath7742fee2019-06-17 12:58:10 +01005525
5526cleanup:
5527
Janos Follathea29bfb2019-06-19 12:21:20 +01005528 cleanup_status = psa_hash_abort( &backup );
5529 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5530 status = cleanup_status;
5531
5532 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005533}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005534
Janos Follath844eb0e2019-06-19 12:10:49 +01005535static psa_status_t psa_key_derivation_tls12_prf_read(
5536 psa_tls12_prf_key_derivation_t *tls12_prf,
5537 psa_algorithm_t alg,
5538 uint8_t *output,
5539 size_t output_length )
5540{
5541 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005542 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005543 psa_status_t status;
5544 uint8_t offset, length;
5545
5546 while( output_length != 0 )
5547 {
5548 /* Check if we have fully processed the current block. */
5549 if( tls12_prf->left_in_block == 0 )
5550 {
5551 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5552 alg );
5553 if( status != PSA_SUCCESS )
5554 return( status );
5555
5556 continue;
5557 }
5558
5559 if( tls12_prf->left_in_block > output_length )
5560 length = (uint8_t) output_length;
5561 else
5562 length = tls12_prf->left_in_block;
5563
5564 offset = hash_length - tls12_prf->left_in_block;
5565 memcpy( output, tls12_prf->output_block + offset, length );
5566 output += length;
5567 output_length -= length;
5568 tls12_prf->left_in_block -= length;
5569 }
5570
5571 return( PSA_SUCCESS );
5572}
John Durkop07cc04a2020-11-16 22:08:34 -08005573#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5574 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005575
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005576psa_status_t psa_key_derivation_output_bytes(
5577 psa_key_derivation_operation_t *operation,
5578 uint8_t *output,
5579 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005580{
5581 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005582 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005583
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005584 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005585 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005586 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005587 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005588 }
5589
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005590 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005591 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005592 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005593 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005594 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005595 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005596 goto exit;
5597 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005598 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005599 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005600 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005601 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005602 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5603 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005604 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005605 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005606 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005607 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005608 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005609
John Durkopd0321952020-10-29 21:37:36 -07005610#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005611 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005612 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005613 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005614 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005615 output, output_length );
5616 }
Janos Follathadbec812019-06-14 11:05:39 +01005617 else
John Durkop07cc04a2020-11-16 22:08:34 -08005618#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5619#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5620 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005621 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005622 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005623 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005624 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005625 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005626 output_length );
5627 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005628 else
John Durkop07cc04a2020-11-16 22:08:34 -08005629#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5630 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005631 {
5632 return( PSA_ERROR_BAD_STATE );
5633 }
5634
5635exit:
5636 if( status != PSA_SUCCESS )
5637 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005638 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005639 * This allows us to differentiate between exhausted operations and
5640 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5641 * operations. */
5642 psa_algorithm_t alg = operation->alg;
5643 psa_key_derivation_abort( operation );
5644 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005645 memset( output, '!', output_length );
5646 }
5647 return( status );
5648}
5649
Gilles Peskine08542d82018-07-19 17:05:42 +02005650#if defined(MBEDTLS_DES_C)
5651static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5652{
5653 if( data_size >= 8 )
5654 mbedtls_des_key_set_parity( data );
5655 if( data_size >= 16 )
5656 mbedtls_des_key_set_parity( data + 8 );
5657 if( data_size >= 24 )
5658 mbedtls_des_key_set_parity( data + 16 );
5659}
5660#endif /* MBEDTLS_DES_C */
5661
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005662static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005663 psa_key_slot_t *slot,
5664 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005665 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005666{
5667 uint8_t *data = NULL;
5668 size_t bytes = PSA_BITS_TO_BYTES( bits );
5669 psa_status_t status;
5670
Gilles Peskine8e338702019-07-30 20:06:31 +02005671 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005672 return( PSA_ERROR_INVALID_ARGUMENT );
5673 if( bits % 8 != 0 )
5674 return( PSA_ERROR_INVALID_ARGUMENT );
5675 data = mbedtls_calloc( 1, bytes );
5676 if( data == NULL )
5677 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5678
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005679 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005680 if( status != PSA_SUCCESS )
5681 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005682#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005683 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005684 psa_des_set_key_parity( data, bytes );
5685#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005686 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005687
5688exit:
5689 mbedtls_free( data );
5690 return( status );
5691}
5692
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005693psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005694 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005695 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005696{
5697 psa_status_t status;
5698 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005699 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005700
Ronald Cron81709fc2020-11-14 12:10:32 +01005701 *key = MBEDTLS_SVC_KEY_ID_INIT;
5702
Gilles Peskine0f84d622019-09-12 19:03:13 +02005703 /* Reject any attempt to create a zero-length key so that we don't
5704 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5705 if( psa_get_key_bits( attributes ) == 0 )
5706 return( PSA_ERROR_INVALID_ARGUMENT );
5707
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005708 if( ! operation->can_output_key )
5709 return( PSA_ERROR_NOT_PERMITTED );
5710
Ronald Cron81709fc2020-11-14 12:10:32 +01005711 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5712 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005713#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5714 if( driver != NULL )
5715 {
5716 /* Deriving a key in a secure element is not implemented yet. */
5717 status = PSA_ERROR_NOT_SUPPORTED;
5718 }
5719#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005720 if( status == PSA_SUCCESS )
5721 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005722 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005723 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005724 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005725 }
5726 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005727 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005728 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005729 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005730
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005731 return( status );
5732}
5733
Gilles Peskineeab56e42018-07-12 17:12:33 +02005734
5735
5736/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005737/* Key derivation */
5738/****************************************************************/
5739
John Durkop07cc04a2020-11-16 22:08:34 -08005740#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005741static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005742 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005743 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005744{
John Durkop07cc04a2020-11-16 22:08:34 -08005745 int is_kdf_alg_supported;
5746
Janos Follath5fe19732019-06-20 15:09:30 +01005747 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5748 * initialisers for this union leave some bytes unspecified.) */
5749 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5750
Gilles Peskine969c5d62019-01-16 15:53:06 +01005751 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005752#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005753 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5754 is_kdf_alg_supported = 1;
5755 else
5756#endif
5757#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5758 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5759 is_kdf_alg_supported = 1;
5760 else
5761#endif
5762#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5763 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5764 is_kdf_alg_supported = 1;
5765 else
5766#endif
5767 is_kdf_alg_supported = 0;
5768
5769 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005770 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005771 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005772 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005773 if( hash_size == 0 )
5774 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005775 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5776 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005777 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005778 {
5779 return( PSA_ERROR_NOT_SUPPORTED );
5780 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005781 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005782 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005783 }
John Durkop07cc04a2020-11-16 22:08:34 -08005784
5785 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005786}
John Durkop07cc04a2020-11-16 22:08:34 -08005787#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005788
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005789psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005790 psa_algorithm_t alg )
5791{
5792 psa_status_t status;
5793
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005794 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005795 return( PSA_ERROR_BAD_STATE );
5796
Gilles Peskine6843c292019-01-18 16:44:49 +01005797 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5798 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005799#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005800 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005801 {
5802 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005803 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005804 }
5805 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5806 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005807 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005808 }
John Durkop07cc04a2020-11-16 22:08:34 -08005809#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005810 else
5811 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005812
5813 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005814 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005815 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005816}
5817
John Durkopd0321952020-10-29 21:37:36 -07005818#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005819static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005820 psa_algorithm_t hash_alg,
5821 psa_key_derivation_step_t step,
5822 const uint8_t *data,
5823 size_t data_length )
5824{
5825 psa_status_t status;
5826 switch( step )
5827 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005828 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005829 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005830 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005831 status = psa_hmac_setup_internal( &hkdf->hmac,
5832 data, data_length,
5833 hash_alg );
5834 if( status != PSA_SUCCESS )
5835 return( status );
5836 hkdf->state = HKDF_STATE_STARTED;
5837 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005838 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005839 /* If no salt was provided, use an empty salt. */
5840 if( hkdf->state == HKDF_STATE_INIT )
5841 {
5842 status = psa_hmac_setup_internal( &hkdf->hmac,
5843 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005844 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005845 if( status != PSA_SUCCESS )
5846 return( status );
5847 hkdf->state = HKDF_STATE_STARTED;
5848 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005849 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005850 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005851 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5852 data, data_length );
5853 if( status != PSA_SUCCESS )
5854 return( status );
5855 status = psa_hmac_finish_internal( &hkdf->hmac,
5856 hkdf->prk,
5857 sizeof( hkdf->prk ) );
5858 if( status != PSA_SUCCESS )
5859 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005860 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005861 hkdf->block_number = 0;
5862 hkdf->state = HKDF_STATE_KEYED;
5863 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005864 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005865 if( hkdf->state == HKDF_STATE_OUTPUT )
5866 return( PSA_ERROR_BAD_STATE );
5867 if( hkdf->info_set )
5868 return( PSA_ERROR_BAD_STATE );
5869 hkdf->info_length = data_length;
5870 if( data_length != 0 )
5871 {
5872 hkdf->info = mbedtls_calloc( 1, data_length );
5873 if( hkdf->info == NULL )
5874 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5875 memcpy( hkdf->info, data, data_length );
5876 }
5877 hkdf->info_set = 1;
5878 return( PSA_SUCCESS );
5879 default:
5880 return( PSA_ERROR_INVALID_ARGUMENT );
5881 }
5882}
John Durkop07cc04a2020-11-16 22:08:34 -08005883#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005884
John Durkop07cc04a2020-11-16 22:08:34 -08005885#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5886 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005887static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5888 const uint8_t *data,
5889 size_t data_length )
5890{
5891 if( prf->state != TLS12_PRF_STATE_INIT )
5892 return( PSA_ERROR_BAD_STATE );
5893
Janos Follathd6dce9f2019-07-04 09:11:38 +01005894 if( data_length != 0 )
5895 {
5896 prf->seed = mbedtls_calloc( 1, data_length );
5897 if( prf->seed == NULL )
5898 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005899
Janos Follathd6dce9f2019-07-04 09:11:38 +01005900 memcpy( prf->seed, data, data_length );
5901 prf->seed_length = data_length;
5902 }
Janos Follathf08e2652019-06-13 09:05:41 +01005903
5904 prf->state = TLS12_PRF_STATE_SEED_SET;
5905
5906 return( PSA_SUCCESS );
5907}
5908
Janos Follath81550542019-06-13 14:26:34 +01005909static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5910 psa_algorithm_t hash_alg,
5911 const uint8_t *data,
5912 size_t data_length )
5913{
5914 psa_status_t status;
5915 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5916 return( PSA_ERROR_BAD_STATE );
5917
5918 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5919 if( status != PSA_SUCCESS )
5920 return( status );
5921
5922 prf->state = TLS12_PRF_STATE_KEY_SET;
5923
5924 return( PSA_SUCCESS );
5925}
5926
Janos Follath63028dd2019-06-13 09:15:47 +01005927static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5928 const uint8_t *data,
5929 size_t data_length )
5930{
5931 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5932 return( PSA_ERROR_BAD_STATE );
5933
Janos Follathd6dce9f2019-07-04 09:11:38 +01005934 if( data_length != 0 )
5935 {
5936 prf->label = mbedtls_calloc( 1, data_length );
5937 if( prf->label == NULL )
5938 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005939
Janos Follathd6dce9f2019-07-04 09:11:38 +01005940 memcpy( prf->label, data, data_length );
5941 prf->label_length = data_length;
5942 }
Janos Follath63028dd2019-06-13 09:15:47 +01005943
5944 prf->state = TLS12_PRF_STATE_LABEL_SET;
5945
5946 return( PSA_SUCCESS );
5947}
5948
Janos Follathb03233e2019-06-11 15:30:30 +01005949static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5950 psa_algorithm_t hash_alg,
5951 psa_key_derivation_step_t step,
5952 const uint8_t *data,
5953 size_t data_length )
5954{
Janos Follathb03233e2019-06-11 15:30:30 +01005955 switch( step )
5956 {
Janos Follathf08e2652019-06-13 09:05:41 +01005957 case PSA_KEY_DERIVATION_INPUT_SEED:
5958 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005959 case PSA_KEY_DERIVATION_INPUT_SECRET:
5960 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005961 case PSA_KEY_DERIVATION_INPUT_LABEL:
5962 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005963 default:
5964 return( PSA_ERROR_INVALID_ARGUMENT );
5965 }
5966}
John Durkop07cc04a2020-11-16 22:08:34 -08005967#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5968 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5969
5970#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5971static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5972 psa_tls12_prf_key_derivation_t *prf,
5973 psa_algorithm_t hash_alg,
5974 const uint8_t *data,
5975 size_t data_length )
5976{
5977 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005978 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005979 uint8_t *cur = pms;
5980
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005981 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005982 return( PSA_ERROR_INVALID_ARGUMENT );
5983
5984 /* Quoting RFC 4279, Section 2:
5985 *
5986 * The premaster secret is formed as follows: if the PSK is N octets
5987 * long, concatenate a uint16 with the value N, N zero octets, a second
5988 * uint16 with the value N, and the PSK itself.
5989 */
5990
5991 *cur++ = ( data_length >> 8 ) & 0xff;
5992 *cur++ = ( data_length >> 0 ) & 0xff;
5993 memset( cur, 0, data_length );
5994 cur += data_length;
5995 *cur++ = pms[0];
5996 *cur++ = pms[1];
5997 memcpy( cur, data, data_length );
5998 cur += data_length;
5999
6000 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
6001
6002 mbedtls_platform_zeroize( pms, sizeof( pms ) );
6003 return( status );
6004}
Janos Follath6660f0e2019-06-17 08:44:03 +01006005
6006static psa_status_t psa_tls12_prf_psk_to_ms_input(
6007 psa_tls12_prf_key_derivation_t *prf,
6008 psa_algorithm_t hash_alg,
6009 psa_key_derivation_step_t step,
6010 const uint8_t *data,
6011 size_t data_length )
6012{
6013 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01006014 {
Janos Follath6660f0e2019-06-17 08:44:03 +01006015 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
6016 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01006017 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006018
6019 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
6020}
John Durkop07cc04a2020-11-16 22:08:34 -08006021#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006022
Gilles Peskineb8965192019-09-24 16:21:10 +02006023/** Check whether the given key type is acceptable for the given
6024 * input step of a key derivation.
6025 *
6026 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6027 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6028 * Both secret and non-secret inputs can alternatively have the type
6029 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6030 * that the input was passed as a buffer rather than via a key object.
6031 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006032static int psa_key_derivation_check_input_type(
6033 psa_key_derivation_step_t step,
6034 psa_key_type_t key_type )
6035{
6036 switch( step )
6037 {
6038 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02006039 if( key_type == PSA_KEY_TYPE_DERIVE )
6040 return( PSA_SUCCESS );
6041 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006042 return( PSA_SUCCESS );
6043 break;
6044 case PSA_KEY_DERIVATION_INPUT_LABEL:
6045 case PSA_KEY_DERIVATION_INPUT_SALT:
6046 case PSA_KEY_DERIVATION_INPUT_INFO:
6047 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02006048 if( key_type == PSA_KEY_TYPE_RAW_DATA )
6049 return( PSA_SUCCESS );
6050 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006051 return( PSA_SUCCESS );
6052 break;
6053 }
6054 return( PSA_ERROR_INVALID_ARGUMENT );
6055}
6056
Janos Follathb80a94e2019-06-12 15:54:46 +01006057static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006058 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006059 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006060 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006061 const uint8_t *data,
6062 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006063{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006064 psa_status_t status;
6065 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
6066
6067 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006068 if( status != PSA_SUCCESS )
6069 goto exit;
6070
John Durkopd0321952020-10-29 21:37:36 -07006071#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006072 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006073 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006074 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006075 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006076 step, data, data_length );
6077 }
John Durkop07cc04a2020-11-16 22:08:34 -08006078 else
6079#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6080#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6081 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006082 {
Janos Follathb03233e2019-06-11 15:30:30 +01006083 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6084 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6085 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006086 }
John Durkop07cc04a2020-11-16 22:08:34 -08006087 else
6088#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6089#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6090 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006091 {
6092 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6093 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6094 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006095 }
6096 else
John Durkop07cc04a2020-11-16 22:08:34 -08006097#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006098 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006099 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006100 return( PSA_ERROR_BAD_STATE );
6101 }
6102
Gilles Peskine224b0d62019-09-23 18:13:17 +02006103exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006104 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006105 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006106 return( status );
6107}
6108
Janos Follath51f4a0f2019-06-14 11:35:55 +01006109psa_status_t psa_key_derivation_input_bytes(
6110 psa_key_derivation_operation_t *operation,
6111 psa_key_derivation_step_t step,
6112 const uint8_t *data,
6113 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006114{
Gilles Peskineb8965192019-09-24 16:21:10 +02006115 return( psa_key_derivation_input_internal( operation, step,
6116 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006117 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006118}
6119
Janos Follath51f4a0f2019-06-14 11:35:55 +01006120psa_status_t psa_key_derivation_input_key(
6121 psa_key_derivation_operation_t *operation,
6122 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006123 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006124{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006125 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006126 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006127 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006128
Ronald Cron5c522922020-11-14 16:35:34 +01006129 status = psa_get_and_lock_transparent_key_slot_with_policy(
6130 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006131 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006132 {
6133 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006134 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006135 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006136
6137 /* Passing a key object as a SECRET input unlocks the permission
6138 * to output to a key object. */
6139 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6140 operation->can_output_key = 1;
6141
Ronald Cronf95a2b12020-10-22 15:24:49 +02006142 status = psa_key_derivation_input_internal( operation,
6143 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006144 slot->key.data,
6145 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006146
Ronald Cron5c522922020-11-14 16:35:34 +01006147 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006148
Ronald Cron5c522922020-11-14 16:35:34 +01006149 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006150}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006151
6152
6153
6154/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006155/* Key agreement */
6156/****************************************************************/
6157
John Durkop6ba40d12020-11-10 08:50:04 -08006158#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006159static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6160 size_t peer_key_length,
6161 const mbedtls_ecp_keypair *our_key,
6162 uint8_t *shared_secret,
6163 size_t shared_secret_size,
6164 size_t *shared_secret_length )
6165{
Steven Cooremand4867872020-08-05 16:31:39 +02006166 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006167 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006168 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006169 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006170 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006171 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006172
Steven Cooreman4fed4552020-08-03 14:46:03 +02006173 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6174 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006175 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006176 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006177 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006178 goto exit;
6179
Jaeden Amero97271b32019-01-10 19:38:51 +00006180 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006181 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006182 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006183 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006184 status = mbedtls_to_psa_error(
6185 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6186 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006187 goto exit;
6188
Jaeden Amero97271b32019-01-10 19:38:51 +00006189 status = mbedtls_to_psa_error(
6190 mbedtls_ecdh_calc_secret( &ecdh,
6191 shared_secret_length,
6192 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006193 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006194 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006195 if( status != PSA_SUCCESS )
6196 goto exit;
6197 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6198 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006199
6200exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006201 if( status != PSA_SUCCESS )
6202 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006203 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006204 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006205 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006206
Jaeden Amero97271b32019-01-10 19:38:51 +00006207 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006208}
John Durkop6ba40d12020-11-10 08:50:04 -08006209#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006210
Gilles Peskine01d718c2018-09-18 12:01:02 +02006211#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6212
Gilles Peskine0216fe12019-04-11 21:23:21 +02006213static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6214 psa_key_slot_t *private_key,
6215 const uint8_t *peer_key,
6216 size_t peer_key_length,
6217 uint8_t *shared_secret,
6218 size_t shared_secret_size,
6219 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006220{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006221 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006222 {
John Durkop6ba40d12020-11-10 08:50:04 -08006223#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006224 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006225 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006226 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006227 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006228 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006229 private_key->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006230 private_key->key.data,
6231 private_key->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006232 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006233 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006234 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006235 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006236 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006237 shared_secret, shared_secret_size,
6238 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006239 mbedtls_ecp_keypair_free( ecp );
6240 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006241 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006242#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006243 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006244 (void) private_key;
6245 (void) peer_key;
6246 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006247 (void) shared_secret;
6248 (void) shared_secret_size;
6249 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006250 return( PSA_ERROR_NOT_SUPPORTED );
6251 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006252}
6253
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006254/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006255 * to potentially free embedded data structures and wipe confidential data.
6256 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006257static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006258 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006259 psa_key_slot_t *private_key,
6260 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006261 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006262{
6263 psa_status_t status;
6264 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6265 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006266 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006267
6268 /* Step 1: run the secret agreement algorithm to generate the shared
6269 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006270 status = psa_key_agreement_raw_internal( ka_alg,
6271 private_key,
6272 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006273 shared_secret,
6274 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006275 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006276 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006277 goto exit;
6278
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006279 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006280 * the shared secret. A shared secret is permitted wherever a key
6281 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006282 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006283 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006284 shared_secret,
6285 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006286exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006287 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006288 return( status );
6289}
6290
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006291psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006292 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006293 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006294 const uint8_t *peer_key,
6295 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006296{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006298 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006299 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006300
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006301 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006302 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006303 status = psa_get_and_lock_transparent_key_slot_with_policy(
6304 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006305 if( status != PSA_SUCCESS )
6306 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006307 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006308 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006309 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006310 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006311 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006312 else
6313 {
6314 /* If a private key has been added as SECRET, we allow the derived
6315 * key material to be used as a key in PSA Crypto. */
6316 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6317 operation->can_output_key = 1;
6318 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006319
Ronald Cron5c522922020-11-14 16:35:34 +01006320 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006321
Ronald Cron5c522922020-11-14 16:35:34 +01006322 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006323}
6324
Gilles Peskinebe697d82019-05-16 18:00:41 +02006325psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006326 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006327 const uint8_t *peer_key,
6328 size_t peer_key_length,
6329 uint8_t *output,
6330 size_t output_size,
6331 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006332{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006333 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006334 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006335 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006336
6337 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6338 {
6339 status = PSA_ERROR_INVALID_ARGUMENT;
6340 goto exit;
6341 }
Ronald Cron5c522922020-11-14 16:35:34 +01006342 status = psa_get_and_lock_transparent_key_slot_with_policy(
6343 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006344 if( status != PSA_SUCCESS )
6345 goto exit;
6346
6347 status = psa_key_agreement_raw_internal( alg, slot,
6348 peer_key, peer_key_length,
6349 output, output_size,
6350 output_length );
6351
6352exit:
6353 if( status != PSA_SUCCESS )
6354 {
6355 /* If an error happens and is not handled properly, the output
6356 * may be used as a key to protect sensitive data. Arrange for such
6357 * a key to be random, which is likely to result in decryption or
6358 * verification errors. This is better than filling the buffer with
6359 * some constant data such as zeros, which would result in the data
6360 * being protected with a reproducible, easily knowable key.
6361 */
6362 psa_generate_random( output, output_size );
6363 *output_length = output_size;
6364 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006365
Ronald Cron5c522922020-11-14 16:35:34 +01006366 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006367
Ronald Cron5c522922020-11-14 16:35:34 +01006368 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006369}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006370
6371
Gilles Peskine30524eb2020-11-13 17:02:26 +01006372
Gilles Peskine86a440b2018-11-12 18:39:40 +01006373/****************************************************************/
6374/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006375/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006376
Gilles Peskine30524eb2020-11-13 17:02:26 +01006377/** Initialize the PSA random generator.
6378 */
6379static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6380{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006381#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6382 memset( rng, 0, sizeof( *rng ) );
6383#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6384
Gilles Peskine30524eb2020-11-13 17:02:26 +01006385 /* Set default configuration if
6386 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6387 if( rng->entropy_init == NULL )
6388 rng->entropy_init = mbedtls_entropy_init;
6389 if( rng->entropy_free == NULL )
6390 rng->entropy_free = mbedtls_entropy_free;
6391
6392 rng->entropy_init( &rng->entropy );
6393#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6394 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6395 /* The PSA entropy injection feature depends on using NV seed as an entropy
6396 * source. Add NV seed as an entropy source for PSA entropy injection. */
6397 mbedtls_entropy_add_source( &rng->entropy,
6398 mbedtls_nv_seed_poll, NULL,
6399 MBEDTLS_ENTROPY_BLOCK_SIZE,
6400 MBEDTLS_ENTROPY_SOURCE_STRONG );
6401#endif
6402
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006403 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006404#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006405}
6406
6407/** Deinitialize the PSA random generator.
6408 */
6409static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6410{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006411#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6412 memset( rng, 0, sizeof( *rng ) );
6413#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006414 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006415 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006416#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006417}
6418
6419/** Seed the PSA random generator.
6420 */
6421static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6422{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006423#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6424 /* Do nothing: the external RNG seeds itself. */
6425 (void) rng;
6426 return( PSA_SUCCESS );
6427#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006428 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006429 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6430 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006431 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006432#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006433}
6434
Gilles Peskinee59236f2018-01-27 23:32:46 +01006435psa_status_t psa_generate_random( uint8_t *output,
6436 size_t output_size )
6437{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006438 GUARD_MODULE_INITIALIZED;
6439
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006440#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6441
6442 size_t output_length = 0;
6443 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6444 output, output_size,
6445 &output_length );
6446 if( status != PSA_SUCCESS )
6447 return( status );
6448 /* Breaking up a request into smaller chunks is currently not supported
6449 * for the extrernal RNG interface. */
6450 if( output_length != output_size )
6451 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6452 return( PSA_SUCCESS );
6453
6454#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6455
Gilles Peskine71ddab92021-01-04 21:01:07 +01006456 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006457 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006458 size_t request_size =
6459 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6460 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6461 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006462 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6463 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006464 if( ret != 0 )
6465 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006466 output_size -= request_size;
6467 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006468 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006469 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006470#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006471}
6472
Gilles Peskine8814fc42020-12-14 15:33:44 +01006473/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006474 *
6475 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6476 * `psa_generate_random(...)`. The state parameter is ignored since the
6477 * PSA API doesn't support passing an explicit state.
6478 *
6479 * In the non-external case, psa_generate_random() calls an
6480 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6481 * and semantics as mbedtls_psa_get_random(). As an optimization,
6482 * instead of doing this back-and-forth between the PSA API and the
6483 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6484 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006485 */
6486#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6487int mbedtls_psa_get_random( void *p_rng,
6488 unsigned char *output,
6489 size_t output_size )
6490{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006491 /* This function takes a pointer to the RNG state because that's what
6492 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6493 * its own state internally and doesn't let the caller access that state.
6494 * So we just ignore the state parameter, and in practice we'll pass
6495 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006496 (void) p_rng;
6497 psa_status_t status = psa_generate_random( output, output_size );
6498 if( status == PSA_SUCCESS )
6499 return( 0 );
6500 else
6501 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6502}
6503#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6504
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006505#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6506#include "mbedtls/entropy_poll.h"
6507
Gilles Peskine7228da22019-07-15 11:06:15 +02006508psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006509 size_t seed_size )
6510{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006511 if( global_data.initialized )
6512 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006513
6514 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6515 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6516 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6517 return( PSA_ERROR_INVALID_ARGUMENT );
6518
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006519 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006520}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006521#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006522
John Durkop07cc04a2020-11-16 22:08:34 -08006523#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006524static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6525 size_t domain_parameters_size,
6526 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006527{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006528 size_t i;
6529 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006530
Gilles Peskinee56e8782019-04-26 17:34:02 +02006531 if( domain_parameters_size == 0 )
6532 {
6533 *exponent = 65537;
6534 return( PSA_SUCCESS );
6535 }
6536
6537 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6538 * support values that fit in a 32-bit integer, which is larger than
6539 * int on just about every platform anyway. */
6540 if( domain_parameters_size > sizeof( acc ) )
6541 return( PSA_ERROR_NOT_SUPPORTED );
6542 for( i = 0; i < domain_parameters_size; i++ )
6543 acc = ( acc << 8 ) | domain_parameters[i];
6544 if( acc > INT_MAX )
6545 return( PSA_ERROR_NOT_SUPPORTED );
6546 *exponent = acc;
6547 return( PSA_SUCCESS );
6548}
John Durkop07cc04a2020-11-16 22:08:34 -08006549#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006550
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006551static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006552 psa_key_slot_t *slot, size_t bits,
6553 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006554{
Gilles Peskine8e338702019-07-30 20:06:31 +02006555 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006556
Gilles Peskinee56e8782019-04-26 17:34:02 +02006557 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006558 return( PSA_ERROR_INVALID_ARGUMENT );
6559
Gilles Peskinee59236f2018-01-27 23:32:46 +01006560 if( key_type_is_raw_bytes( type ) )
6561 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006562 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006563
6564 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006565 if( status != PSA_SUCCESS )
6566 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006567
6568 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006569 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6570 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006571 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006572
Ronald Cronea0f8a62020-11-25 17:52:23 +01006573 status = psa_generate_random( slot->key.data,
6574 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006575 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006576 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006577
6578 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006579#if defined(MBEDTLS_DES_C)
6580 if( type == PSA_KEY_TYPE_DES )
Ronald Cronea0f8a62020-11-25 17:52:23 +01006581 psa_des_set_key_parity( slot->key.data,
6582 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006583#endif /* MBEDTLS_DES_C */
6584 }
6585 else
6586
John Durkop07cc04a2020-11-16 22:08:34 -08006587#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006588 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006589 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006590 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006592 int exponent;
6593 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006594 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6595 return( PSA_ERROR_NOT_SUPPORTED );
6596 /* Accept only byte-aligned keys, for the same reasons as
6597 * in psa_import_rsa_key(). */
6598 if( bits % 8 != 0 )
6599 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006600 status = psa_read_rsa_exponent( domain_parameters,
6601 domain_parameters_size,
6602 &exponent );
6603 if( status != PSA_SUCCESS )
6604 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006605 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6606 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006607 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006608 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006609 (unsigned int) bits,
6610 exponent );
6611 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006612 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006613
6614 /* Make sure to always have an export representation available */
6615 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6616
Steven Cooreman75b74362020-07-28 14:30:13 +02006617 status = psa_allocate_buffer_to_slot( slot, bytes );
6618 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006619 {
6620 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006621 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006622 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006623
6624 status = psa_export_rsa_key( type,
6625 &rsa,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006626 slot->key.data,
Steven Cooremana01795d2020-07-24 22:48:15 +02006627 bytes,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006628 &slot->key.bytes );
Steven Cooremana01795d2020-07-24 22:48:15 +02006629 mbedtls_rsa_free( &rsa );
6630 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006631 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006632 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006633 }
6634 else
John Durkop07cc04a2020-11-16 22:08:34 -08006635#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006636
John Durkop9814fa22020-11-04 12:28:15 -08006637#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006638 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006639 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006640 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006641 mbedtls_ecp_group_id grp_id =
6642 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006643 const mbedtls_ecp_curve_info *curve_info =
6644 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006645 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006647 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006648 return( PSA_ERROR_NOT_SUPPORTED );
6649 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6650 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006651 mbedtls_ecp_keypair_init( &ecp );
6652 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006653 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006654 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006655 if( ret != 0 )
6656 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006657 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006658 return( mbedtls_to_psa_error( ret ) );
6659 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006660
6661
6662 /* Make sure to always have an export representation available */
6663 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006664 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6665 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006666 {
6667 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006668 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006669 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006670
6671 status = mbedtls_to_psa_error(
Ronald Cronea0f8a62020-11-25 17:52:23 +01006672 mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02006673
6674 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006675 if( status != PSA_SUCCESS ) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01006676 memset( slot->key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006677 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006678 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006679 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006680 }
6681 else
John Durkop9814fa22020-11-04 12:28:15 -08006682#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006683 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006684 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006685 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006686
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006687 return( PSA_SUCCESS );
6688}
Darryl Green0c6575a2018-11-07 16:05:30 +00006689
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006690psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006691 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006692{
6693 psa_status_t status;
6694 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006695 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006696
Ronald Cron81709fc2020-11-14 12:10:32 +01006697 *key = MBEDTLS_SVC_KEY_ID_INIT;
6698
Gilles Peskine0f84d622019-09-12 19:03:13 +02006699 /* Reject any attempt to create a zero-length key so that we don't
6700 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6701 if( psa_get_key_bits( attributes ) == 0 )
6702 return( PSA_ERROR_INVALID_ARGUMENT );
6703
Ronald Cron81709fc2020-11-14 12:10:32 +01006704 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6705 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006706 if( status != PSA_SUCCESS )
6707 goto exit;
6708
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006709 status = psa_driver_wrapper_generate_key( attributes,
6710 slot );
6711 if( status != PSA_ERROR_NOT_SUPPORTED ||
6712 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6713 goto exit;
6714
6715 status = psa_generate_key_internal(
6716 slot, attributes->core.bits,
6717 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006718
6719exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006720 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006721 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006722 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006723 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006724
Darryl Green0c6575a2018-11-07 16:05:30 +00006725 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006726}
6727
6728
Gilles Peskinee59236f2018-01-27 23:32:46 +01006729
6730/****************************************************************/
6731/* Module setup */
6732/****************************************************************/
6733
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006734#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006735psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6736 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6737 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6738{
6739 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6740 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006741 global_data.rng.entropy_init = entropy_init;
6742 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006743 return( PSA_SUCCESS );
6744}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006745#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006746
Gilles Peskinee59236f2018-01-27 23:32:46 +01006747void mbedtls_psa_crypto_free( void )
6748{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006749 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006750 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6751 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006752 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006753 }
6754 /* Wipe all remaining data, including configuration.
6755 * In particular, this sets all state indicator to the value
6756 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006757 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006758#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006759 /* Unregister all secure element drivers, so that we restart from
6760 * a pristine state. */
6761 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006762#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006763}
6764
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006765#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6766/** Recover a transaction that was interrupted by a power failure.
6767 *
6768 * This function is called during initialization, before psa_crypto_init()
6769 * returns. If this function returns a failure status, the initialization
6770 * fails.
6771 */
6772static psa_status_t psa_crypto_recover_transaction(
6773 const psa_crypto_transaction_t *transaction )
6774{
6775 switch( transaction->unknown.type )
6776 {
6777 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6778 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006779 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006780 * is implemented.
6781 * https://github.com/ARMmbed/mbed-crypto/issues/218
6782 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006783 default:
6784 /* We found an unsupported transaction in the storage.
6785 * We don't know what state the storage is in. Give up. */
6786 return( PSA_ERROR_STORAGE_FAILURE );
6787 }
6788}
6789#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6790
Gilles Peskinee59236f2018-01-27 23:32:46 +01006791psa_status_t psa_crypto_init( void )
6792{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006793 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006794
Gilles Peskinec6b69072018-11-20 21:42:52 +01006795 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006796 if( global_data.initialized != 0 )
6797 return( PSA_SUCCESS );
6798
Gilles Peskine30524eb2020-11-13 17:02:26 +01006799 /* Initialize and seed the random generator. */
6800 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006801 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006802 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006803 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006804 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006805 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006806
Gilles Peskine66fb1262018-12-10 16:29:04 +01006807 status = psa_initialize_key_slots( );
6808 if( status != PSA_SUCCESS )
6809 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006810
Gilles Peskined9348f22019-10-01 15:22:29 +02006811#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6812 status = psa_init_all_se_drivers( );
6813 if( status != PSA_SUCCESS )
6814 goto exit;
6815#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6816
Gilles Peskine4b734222019-07-24 15:56:31 +02006817#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006818 status = psa_crypto_load_transaction( );
6819 if( status == PSA_SUCCESS )
6820 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006821 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6822 if( status != PSA_SUCCESS )
6823 goto exit;
6824 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006825 }
6826 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6827 {
6828 /* There's no transaction to complete. It's all good. */
6829 status = PSA_SUCCESS;
6830 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006831#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006832
Gilles Peskinec6b69072018-11-20 21:42:52 +01006833 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006834 global_data.initialized = 1;
6835
6836exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006837 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006838 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006839 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006840}
6841
6842#endif /* MBEDTLS_PSA_CRYPTO_C */