blob: adc730b57394f5d35118597ae9c9e5e491307487 [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. */
Ronald Cron84cc9942020-11-25 14:30:05 +01001761 psa_key_attributes_t attributes = {
1762 .core = slot->attr
1763 };
1764 psa_status_t status = psa_driver_wrapper_export_public_key(
1765 &attributes, slot->key.data, slot->key.bytes,
1766 data, data_size, data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +02001767
1768 if( status != PSA_ERROR_NOT_SUPPORTED ||
1769 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1770 return( status );
1771
Steven Cooreman560c28a2020-07-24 23:20:24 +02001772 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1773 {
John Durkop0e005192020-10-31 22:06:54 -07001774#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1775 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001776 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001777 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001778 slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001779 slot->key.data,
1780 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001781 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001782 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001783 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001784
Steven Cooreman560c28a2020-07-24 23:20:24 +02001785 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001786 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001787 data,
1788 data_size,
1789 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001790
Steven Cooremana2371e52020-07-28 14:30:39 +02001791 mbedtls_rsa_free( rsa );
1792 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001793
Steven Cooreman560c28a2020-07-24 23:20:24 +02001794 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001795#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001796 /* We don't know how to convert a private RSA key to public. */
1797 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001798#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1799 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001800 }
1801 else
Moran Pekera998bc62018-04-16 18:16:20 +03001802 {
John Durkop6ba40d12020-11-10 08:50:04 -08001803#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1804 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001805 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001806 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001807 slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001808 slot->key.data,
1809 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001810 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001811 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001812 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001813
1814 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1815 PSA_KEY_TYPE_ECC_GET_FAMILY(
1816 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001817 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001818 data,
1819 data_size,
1820 data_length );
1821
Steven Cooremana2371e52020-07-28 14:30:39 +02001822 mbedtls_ecp_keypair_free( ecp );
1823 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001824 return( status );
1825#else
1826 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001827 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001828#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1829 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001830 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001831 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001832 else
1833 {
1834 /* This shouldn't happen in the reference implementation, but
1835 it is valid for a special-purpose implementation to omit
1836 support for exporting certain key types. */
1837 return( PSA_ERROR_NOT_SUPPORTED );
1838 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001839}
1840
Ronald Croncf56a0a2020-08-04 09:51:30 +02001841psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001842 uint8_t *data,
1843 size_t data_size,
1844 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001845{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001846 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001847 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001848 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001849
1850 /* Set the key to empty now, so that even when there are errors, we always
1851 * set data_length to a value between 0 and data_size. On error, setting
1852 * the key to empty is a good choice because an empty key representation is
1853 * unlikely to be accepted anywhere. */
1854 *data_length = 0;
1855
1856 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001857 * which don't require any flag, but
1858 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1859 */
1860 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1861 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001862 if( status != PSA_SUCCESS )
1863 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001864
1865 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001866 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001867
Ronald Cron5c522922020-11-14 16:35:34 +01001868 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001869}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001870
Ronald Croncf56a0a2020-08-04 09:51:30 +02001871psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001872 uint8_t *data,
1873 size_t data_size,
1874 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001875{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001876 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001877 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001878 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001879
1880 /* Set the key to empty now, so that even when there are errors, we always
1881 * set data_length to a value between 0 and data_size. On error, setting
1882 * the key to empty is a good choice because an empty key representation is
1883 * unlikely to be accepted anywhere. */
1884 *data_length = 0;
1885
1886 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001887 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001888 if( status != PSA_SUCCESS )
1889 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001890
1891 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001892 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001893
Ronald Cron5c522922020-11-14 16:35:34 +01001894 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001895}
1896
Gilles Peskine91e8c332019-08-02 19:19:39 +02001897#if defined(static_assert)
1898static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1899 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001900static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001901 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001902static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001903 "One or more key attribute flag is listed as both internal-only and external-only" );
1904#endif
1905
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001906/** Validate that a key policy is internally well-formed.
1907 *
1908 * This function only rejects invalid policies. It does not validate the
1909 * consistency of the policy with respect to other attributes of the key
1910 * such as the key type.
1911 */
1912static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001913{
1914 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001915 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001916 PSA_KEY_USAGE_ENCRYPT |
1917 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001918 PSA_KEY_USAGE_SIGN_HASH |
1919 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001920 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1921 return( PSA_ERROR_INVALID_ARGUMENT );
1922
Gilles Peskine4747d192019-04-17 15:05:45 +02001923 return( PSA_SUCCESS );
1924}
1925
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001926/** Validate the internal consistency of key attributes.
1927 *
1928 * This function only rejects invalid attribute values. If does not
1929 * validate the consistency of the attributes with any key data that may
1930 * be involved in the creation of the key.
1931 *
1932 * Call this function early in the key creation process.
1933 *
1934 * \param[in] attributes Key attributes for the new key.
1935 * \param[out] p_drv On any return, the driver for the key, if any.
1936 * NULL for a transparent key.
1937 *
1938 */
1939static psa_status_t psa_validate_key_attributes(
1940 const psa_key_attributes_t *attributes,
1941 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001942{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001943 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001944 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001945 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001946
Ronald Cron54b90082020-10-29 15:26:43 +01001947 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001948 if( status != PSA_SUCCESS )
1949 return( status );
1950
Ronald Crond2ed4812020-07-17 16:11:30 +02001951 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001952 if( status != PSA_SUCCESS )
1953 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001954
Ronald Cron65f38a32020-10-23 17:11:13 +02001955 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1956 {
1957 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1958 return( PSA_ERROR_INVALID_ARGUMENT );
1959 }
1960 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001961 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001962 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001963 if( status != PSA_SUCCESS )
1964 return( status );
1965 }
1966
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001967 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001968 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001969 return( status );
1970
1971 /* Refuse to create overly large keys.
1972 * Note that this doesn't trigger on import if the attributes don't
1973 * explicitly specify a size (so psa_get_key_bits returns 0), so
1974 * psa_import_key() needs its own checks. */
1975 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1976 return( PSA_ERROR_NOT_SUPPORTED );
1977
Gilles Peskine91e8c332019-08-02 19:19:39 +02001978 /* Reject invalid flags. These should not be reachable through the API. */
1979 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1980 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1981 return( PSA_ERROR_INVALID_ARGUMENT );
1982
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001983 return( PSA_SUCCESS );
1984}
1985
Gilles Peskine4747d192019-04-17 15:05:45 +02001986/** Prepare a key slot to receive key material.
1987 *
1988 * This function allocates a key slot and sets its metadata.
1989 *
1990 * If this function fails, call psa_fail_key_creation().
1991 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001992 * This function is intended to be used as follows:
1993 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001994 * it with the specified attributes, and in case of a volatile key assign it
1995 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001996 * -# Populate the slot with the key material.
1997 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1998 * In case of failure at any step, stop the sequence and call
1999 * psa_fail_key_creation().
2000 *
Ronald Cron5c522922020-11-14 16:35:34 +01002001 * On success, the key slot is locked. It is the responsibility of the caller
2002 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02002003 *
Gilles Peskinedf179142019-07-15 22:02:14 +02002004 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02002005 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02002006 * \param[out] p_slot On success, a pointer to the prepared slot.
2007 * \param[out] p_drv On any return, the driver for the key, if any.
2008 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002009 *
2010 * \retval #PSA_SUCCESS
2011 * The key slot is ready to receive key material.
2012 * \return If this function fails, the key slot is an invalid state.
2013 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002014 */
2015static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02002016 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02002017 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002018 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002019 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02002020{
2021 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02002022 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02002023 psa_key_slot_t *slot;
2024
Gilles Peskinedf179142019-07-15 22:02:14 +02002025 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02002026 *p_drv = NULL;
2027
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002028 status = psa_validate_key_attributes( attributes, p_drv );
2029 if( status != PSA_SUCCESS )
2030 return( status );
2031
Ronald Cronc4d1b512020-07-31 11:26:37 +02002032 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02002033 if( status != PSA_SUCCESS )
2034 return( status );
2035 slot = *p_slot;
2036
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002037 /* We're storing the declared bit-size of the key. It's up to each
2038 * creation mechanism to verify that this information is correct.
2039 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002040 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02002041 * is optional (import, copy). In case of a volatile key, assign it the
2042 * volatile key identifier associated to the slot returned to contain its
2043 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002044
2045 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002046 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2047 {
2048#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2049 slot->attr.id = volatile_key_id;
2050#else
2051 slot->attr.id.key_id = volatile_key_id;
2052#endif
2053 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002054
Gilles Peskine91e8c332019-08-02 19:19:39 +02002055 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002056 * external-only flags, query `attributes`. Thanks to the check
2057 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002058 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002059 * may have set. */
2060 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002061
Gilles Peskinecbaff462019-07-12 23:46:04 +02002062#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002063 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002064 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002065 * create the key file in internal storage, create the
2066 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002067 * persistent data. This is done by starting a transaction that will
2068 * encompass these three actions.
2069 * For registering a volatile key, we just need to find an appropriate
2070 * slot number inside the SE. Since the key is designated volatile, creating
2071 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002072 /* The first thing to do is to find a slot number for the new key.
2073 * We save the slot number in persistent storage as part of the
2074 * transaction data. It will be needed to recover if the power
2075 * fails during the key creation process, to clean up on the secure
2076 * element side after restarting. Obtaining a slot number from the
2077 * secure element driver updates its persistent state, but we do not yet
2078 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002079 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002080 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002081 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01002082 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002083 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002084 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002085 if( status != PSA_SUCCESS )
2086 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002087
2088 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002089 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002090 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2091 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01002092 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002093 psa_crypto_transaction.key.id = slot->attr.id;
2094 status = psa_crypto_save_transaction( );
2095 if( status != PSA_SUCCESS )
2096 {
2097 (void) psa_crypto_stop_transaction( );
2098 return( status );
2099 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002100 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01002101
2102 status = psa_copy_key_material_into_slot(
2103 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00002104 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002105
2106 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2107 {
2108 /* Key registration only makes sense with a secure element. */
2109 return( PSA_ERROR_INVALID_ARGUMENT );
2110 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002111#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2112
Ronald Cronc4d1b512020-07-31 11:26:37 +02002113 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002114}
Gilles Peskine4747d192019-04-17 15:05:45 +02002115
2116/** Finalize the creation of a key once its key material has been set.
2117 *
2118 * This entails writing the key to persistent storage.
2119 *
2120 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002121 * See the documentation of psa_start_key_creation() for the intended use
2122 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002123 *
Ronald Cron5c522922020-11-14 16:35:34 +01002124 * If the finalization succeeds, the function unlocks the key slot (it was
2125 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2126 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002127 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002128 * \param[in,out] slot Pointer to the slot with key material.
2129 * \param[in] driver The secure element driver for the key,
2130 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002131 * \param[out] key On success, identifier of the key. Note that the
2132 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002133 *
2134 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002135 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002136 * \return If this function fails, the key slot is an invalid state.
2137 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002138 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002139static psa_status_t psa_finish_key_creation(
2140 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002141 psa_se_drv_table_entry_t *driver,
2142 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002143{
2144 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002145 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002146 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002147
2148#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002149 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002150 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002151#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2152 if( driver != NULL )
2153 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002154 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01002155 psa_key_slot_number_t slot_number =
2156 psa_key_slot_get_slot_number( slot ) ;
2157
Gilles Peskineb46bef22019-07-30 21:32:04 +02002158#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01002159 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02002160 sizeof( data.slot_number ),
2161 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002162#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01002163 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002164 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002165 (uint8_t*) &data,
2166 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002167 }
2168 else
2169#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2170 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002171 /* Key material is saved in export representation in the slot, so
2172 * just pass the slot buffer for storage. */
2173 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002174 slot->key.data,
2175 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002176 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002177 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002178#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2179
Gilles Peskinecbaff462019-07-12 23:46:04 +02002180#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002181 /* Finish the transaction for a key creation. This does not
2182 * happen when registering an existing key. Detect this case
2183 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002184 * creation of a persistent key in a secure element requires a transaction,
2185 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002186 if( driver != NULL &&
2187 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002188 {
2189 status = psa_save_se_persistent_data( driver );
2190 if( status != PSA_SUCCESS )
2191 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002192 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002193 return( status );
2194 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002195 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002196 }
2197#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2198
Ronald Cron50972942020-11-14 11:28:25 +01002199 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002200 {
2201 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002202 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002203 if( status != PSA_SUCCESS )
2204 *key = MBEDTLS_SVC_KEY_ID_INIT;
2205 }
Ronald Cron50972942020-11-14 11:28:25 +01002206
Gilles Peskine4747d192019-04-17 15:05:45 +02002207 return( status );
2208}
2209
2210/** Abort the creation of a key.
2211 *
2212 * You may call this function after calling psa_start_key_creation(),
2213 * or after psa_finish_key_creation() fails. In other circumstances, this
2214 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002215 * See the documentation of psa_start_key_creation() for the intended use
2216 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002217 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002218 * \param[in,out] slot Pointer to the slot with key material.
2219 * \param[in] driver The secure element driver for the key,
2220 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002221 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002222static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002223 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002224{
Gilles Peskine011e4282019-06-26 18:34:38 +02002225 (void) driver;
2226
Gilles Peskine4747d192019-04-17 15:05:45 +02002227 if( slot == NULL )
2228 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002229
2230#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002231 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002232 * element, and the failure happened later (when saving metadata
2233 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002234 * element.
2235 * https://github.com/ARMmbed/mbed-crypto/issues/217
2236 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002237
Gilles Peskined7729582019-08-05 15:55:54 +02002238 /* Abort the ongoing transaction if any (there may not be one if
2239 * the creation process failed before starting one, or if the
2240 * key creation is a registration of a key in a secure element).
2241 * Earlier functions must already have done what it takes to undo any
2242 * partial creation. All that's left is to update the transaction data
2243 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002244 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002245#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2246
Gilles Peskine4747d192019-04-17 15:05:45 +02002247 psa_wipe_key_slot( slot );
2248}
2249
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002250/** Validate optional attributes during key creation.
2251 *
2252 * Some key attributes are optional during key creation. If they are
2253 * specified in the attributes structure, check that they are consistent
2254 * with the data in the slot.
2255 *
2256 * This function should be called near the end of key creation, after
2257 * the slot in memory is fully populated but before saving persistent data.
2258 */
2259static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002260 const psa_key_slot_t *slot,
2261 const psa_key_attributes_t *attributes )
2262{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002263 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002264 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002265 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002266 return( PSA_ERROR_INVALID_ARGUMENT );
2267 }
2268
2269 if( attributes->domain_parameters_size != 0 )
2270 {
John Durkop0e005192020-10-31 22:06:54 -07002271#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2272 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002273 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002274 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002275 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002276 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002278
Steven Cooreman7f391872020-07-30 14:57:44 +02002279 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002280 slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002281 slot->key.data,
2282 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002283 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002284 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002285 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002286
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002287 mbedtls_mpi_init( &actual );
2288 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002289 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002290 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002291 mbedtls_rsa_free( rsa );
2292 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002293 if( ret != 0 )
2294 goto rsa_exit;
2295 ret = mbedtls_mpi_read_binary( &required,
2296 attributes->domain_parameters,
2297 attributes->domain_parameters_size );
2298 if( ret != 0 )
2299 goto rsa_exit;
2300 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2301 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2302 rsa_exit:
2303 mbedtls_mpi_free( &actual );
2304 mbedtls_mpi_free( &required );
2305 if( ret != 0)
2306 return( mbedtls_to_psa_error( ret ) );
2307 }
2308 else
John Durkop9814fa22020-11-04 12:28:15 -08002309#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2310 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002311 {
2312 return( PSA_ERROR_INVALID_ARGUMENT );
2313 }
2314 }
2315
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002316 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002317 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002318 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002319 return( PSA_ERROR_INVALID_ARGUMENT );
2320 }
2321
2322 return( PSA_SUCCESS );
2323}
2324
Gilles Peskine4747d192019-04-17 15:05:45 +02002325psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002326 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002327 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002328 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002329{
2330 psa_status_t status;
2331 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002332 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002333
Ronald Cron81709fc2020-11-14 12:10:32 +01002334 *key = MBEDTLS_SVC_KEY_ID_INIT;
2335
Gilles Peskine0f84d622019-09-12 19:03:13 +02002336 /* Reject zero-length symmetric keys (including raw data key objects).
2337 * This also rejects any key which might be encoded as an empty string,
2338 * which is never valid. */
2339 if( data_length == 0 )
2340 return( PSA_ERROR_INVALID_ARGUMENT );
2341
Gilles Peskinedf179142019-07-15 22:02:14 +02002342 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002343 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002344 if( status != PSA_SUCCESS )
2345 goto exit;
2346
Gilles Peskine5d309672019-07-12 23:47:28 +02002347#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2348 if( driver != NULL )
2349 {
2350 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002351 /* The driver should set the number of key bits, however in
2352 * case it doesn't, we initialize bits to an invalid value. */
2353 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002354 if( drv->key_management == NULL ||
2355 drv->key_management->p_import == NULL )
2356 {
2357 status = PSA_ERROR_NOT_SUPPORTED;
2358 goto exit;
2359 }
2360 status = drv->key_management->p_import(
2361 psa_get_se_driver_context( driver ),
Ronald Cronea0f8a62020-11-25 17:52:23 +01002362 psa_key_slot_get_slot_number( slot ),
2363 attributes, data, data_length, &bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002364 if( status != PSA_SUCCESS )
2365 goto exit;
2366 if( bits > PSA_MAX_KEY_BITS )
2367 {
2368 status = PSA_ERROR_NOT_SUPPORTED;
2369 goto exit;
2370 }
2371 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002372 }
2373 else
2374#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01002375 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
2376 {
2377 /* Importing a key with external lifetime through the driver wrapper
2378 * interface is not yet supported. Return as if this was an invalid
2379 * lifetime. */
2380 status = PSA_ERROR_INVALID_ARGUMENT;
2381 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002382 }
2383 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002384 {
2385 status = psa_import_key_into_slot( slot, data, data_length );
2386 if( status != PSA_SUCCESS )
2387 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002388 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002389 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002390 if( status != PSA_SUCCESS )
2391 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002392
Ronald Cron81709fc2020-11-14 12:10:32 +01002393 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002394exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002395 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002396 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002397
Gilles Peskine4747d192019-04-17 15:05:45 +02002398 return( status );
2399}
2400
Gilles Peskined7729582019-08-05 15:55:54 +02002401#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2402psa_status_t mbedtls_psa_register_se_key(
2403 const psa_key_attributes_t *attributes )
2404{
2405 psa_status_t status;
2406 psa_key_slot_t *slot = NULL;
2407 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002408 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002409
2410 /* Leaving attributes unspecified is not currently supported.
2411 * It could make sense to query the key type and size from the
2412 * secure element, but not all secure elements support this
2413 * and the driver HAL doesn't currently support it. */
2414 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2415 return( PSA_ERROR_NOT_SUPPORTED );
2416 if( psa_get_key_bits( attributes ) == 0 )
2417 return( PSA_ERROR_NOT_SUPPORTED );
2418
2419 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002420 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002421 if( status != PSA_SUCCESS )
2422 goto exit;
2423
Ronald Cron81709fc2020-11-14 12:10:32 +01002424 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002425
2426exit:
2427 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002428 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002429
Gilles Peskined7729582019-08-05 15:55:54 +02002430 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002431 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002432 return( status );
2433}
2434#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2435
Gilles Peskinef603c712019-01-19 13:40:11 +01002436static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002437 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002438{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002439 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002440 source->key.data,
2441 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002442 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002443 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002444
Steven Cooreman398aee52020-10-13 14:35:45 +02002445 target->attr.type = source->attr.type;
2446 target->attr.bits = source->attr.bits;
2447
2448 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002449}
2450
Ronald Croncf56a0a2020-08-04 09:51:30 +02002451psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002452 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002453 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002454{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002455 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002456 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002457 psa_key_slot_t *source_slot = NULL;
2458 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002459 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002460 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002461
Ronald Cron81709fc2020-11-14 12:10:32 +01002462 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2463
Ronald Cron5c522922020-11-14 16:35:34 +01002464 status = psa_get_and_lock_transparent_key_slot_with_policy(
2465 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002466 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002467 goto exit;
2468
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002469 status = psa_validate_optional_attributes( source_slot,
2470 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002471 if( status != PSA_SUCCESS )
2472 goto exit;
2473
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002474 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002475 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002476 if( status != PSA_SUCCESS )
2477 goto exit;
2478
Ronald Cron81709fc2020-11-14 12:10:32 +01002479 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2480 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002481 if( status != PSA_SUCCESS )
2482 goto exit;
2483
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002484#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2485 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002486 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002487 /* Copying to a secure element is not implemented yet. */
2488 status = PSA_ERROR_NOT_SUPPORTED;
2489 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002490 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002491#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002492
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002493 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002494 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002495 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002496
Ronald Cron81709fc2020-11-14 12:10:32 +01002497 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002498exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002499 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002500 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002501
Ronald Cron5c522922020-11-14 16:35:34 +01002502 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002503
Ronald Cron5c522922020-11-14 16:35:34 +01002504 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002505}
2506
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002507
2508
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002509/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002510/* Message digests */
2511/****************************************************************/
2512
John Durkop07cc04a2020-11-16 22:08:34 -08002513#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002514 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2515 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002516 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002517static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002518{
2519 switch( alg )
2520 {
John Durkopee4e6602020-11-27 08:48:46 -08002521#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002522 case PSA_ALG_MD2:
2523 return( &mbedtls_md2_info );
2524#endif
John Durkopee4e6602020-11-27 08:48:46 -08002525#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002526 case PSA_ALG_MD4:
2527 return( &mbedtls_md4_info );
2528#endif
John Durkopee4e6602020-11-27 08:48:46 -08002529#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002530 case PSA_ALG_MD5:
2531 return( &mbedtls_md5_info );
2532#endif
John Durkopee4e6602020-11-27 08:48:46 -08002533#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002534 case PSA_ALG_RIPEMD160:
2535 return( &mbedtls_ripemd160_info );
2536#endif
John Durkopee4e6602020-11-27 08:48:46 -08002537#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002538 case PSA_ALG_SHA_1:
2539 return( &mbedtls_sha1_info );
2540#endif
John Durkopee4e6602020-11-27 08:48:46 -08002541#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002542 case PSA_ALG_SHA_224:
2543 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002544#endif
2545#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002546 case PSA_ALG_SHA_256:
2547 return( &mbedtls_sha256_info );
2548#endif
John Durkopee4e6602020-11-27 08:48:46 -08002549#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002550 case PSA_ALG_SHA_384:
2551 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002552#endif
John Durkopee4e6602020-11-27 08:48:46 -08002553#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002554 case PSA_ALG_SHA_512:
2555 return( &mbedtls_sha512_info );
2556#endif
2557 default:
2558 return( NULL );
2559 }
2560}
John Durkop07cc04a2020-11-16 22:08:34 -08002561#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002562 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2563 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2564 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002565
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002566psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2567{
2568 switch( operation->alg )
2569 {
Gilles Peskine81736312018-06-26 15:04:31 +02002570 case 0:
2571 /* The object has (apparently) been initialized but it is not
2572 * in use. It's ok to call abort on such an object, and there's
2573 * nothing to do. */
2574 break;
John Durkopee4e6602020-11-27 08:48:46 -08002575#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002576 case PSA_ALG_MD2:
2577 mbedtls_md2_free( &operation->ctx.md2 );
2578 break;
2579#endif
John Durkopee4e6602020-11-27 08:48:46 -08002580#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002581 case PSA_ALG_MD4:
2582 mbedtls_md4_free( &operation->ctx.md4 );
2583 break;
2584#endif
John Durkopee4e6602020-11-27 08:48:46 -08002585#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002586 case PSA_ALG_MD5:
2587 mbedtls_md5_free( &operation->ctx.md5 );
2588 break;
2589#endif
John Durkopee4e6602020-11-27 08:48:46 -08002590#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002591 case PSA_ALG_RIPEMD160:
2592 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2593 break;
2594#endif
John Durkopee4e6602020-11-27 08:48:46 -08002595#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002596 case PSA_ALG_SHA_1:
2597 mbedtls_sha1_free( &operation->ctx.sha1 );
2598 break;
2599#endif
John Durkop6ca23272020-12-03 06:01:32 -08002600#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002601 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002602 mbedtls_sha256_free( &operation->ctx.sha256 );
2603 break;
2604#endif
2605#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002606 case PSA_ALG_SHA_256:
2607 mbedtls_sha256_free( &operation->ctx.sha256 );
2608 break;
2609#endif
John Durkop6ca23272020-12-03 06:01:32 -08002610#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002611 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002612 mbedtls_sha512_free( &operation->ctx.sha512 );
2613 break;
2614#endif
2615#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002616 case PSA_ALG_SHA_512:
2617 mbedtls_sha512_free( &operation->ctx.sha512 );
2618 break;
2619#endif
2620 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002621 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002622 }
2623 operation->alg = 0;
2624 return( PSA_SUCCESS );
2625}
2626
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002627psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002628 psa_algorithm_t alg )
2629{
Janos Follath24eed8d2019-11-22 13:21:35 +00002630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002631
2632 /* A context must be freshly initialized before it can be set up. */
2633 if( operation->alg != 0 )
2634 {
2635 return( PSA_ERROR_BAD_STATE );
2636 }
2637
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002638 switch( alg )
2639 {
John Durkopee4e6602020-11-27 08:48:46 -08002640#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002641 case PSA_ALG_MD2:
2642 mbedtls_md2_init( &operation->ctx.md2 );
2643 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2644 break;
2645#endif
John Durkopee4e6602020-11-27 08:48:46 -08002646#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002647 case PSA_ALG_MD4:
2648 mbedtls_md4_init( &operation->ctx.md4 );
2649 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2650 break;
2651#endif
John Durkopee4e6602020-11-27 08:48:46 -08002652#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002653 case PSA_ALG_MD5:
2654 mbedtls_md5_init( &operation->ctx.md5 );
2655 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2656 break;
2657#endif
John Durkopee4e6602020-11-27 08:48:46 -08002658#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002659 case PSA_ALG_RIPEMD160:
2660 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2661 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2662 break;
2663#endif
John Durkopee4e6602020-11-27 08:48:46 -08002664#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002665 case PSA_ALG_SHA_1:
2666 mbedtls_sha1_init( &operation->ctx.sha1 );
2667 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2668 break;
2669#endif
John Durkopee4e6602020-11-27 08:48:46 -08002670#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002671 case PSA_ALG_SHA_224:
2672 mbedtls_sha256_init( &operation->ctx.sha256 );
2673 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2674 break;
John Durkopee4e6602020-11-27 08:48:46 -08002675#endif
2676#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002677 case PSA_ALG_SHA_256:
2678 mbedtls_sha256_init( &operation->ctx.sha256 );
2679 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2680 break;
2681#endif
John Durkopee4e6602020-11-27 08:48:46 -08002682#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002683 case PSA_ALG_SHA_384:
2684 mbedtls_sha512_init( &operation->ctx.sha512 );
2685 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2686 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002687#endif
John Durkopee4e6602020-11-27 08:48:46 -08002688#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002689 case PSA_ALG_SHA_512:
2690 mbedtls_sha512_init( &operation->ctx.sha512 );
2691 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2692 break;
2693#endif
2694 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002695 return( PSA_ALG_IS_HASH( alg ) ?
2696 PSA_ERROR_NOT_SUPPORTED :
2697 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002698 }
2699 if( ret == 0 )
2700 operation->alg = alg;
2701 else
2702 psa_hash_abort( operation );
2703 return( mbedtls_to_psa_error( ret ) );
2704}
2705
2706psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2707 const uint8_t *input,
2708 size_t input_length )
2709{
Janos Follath24eed8d2019-11-22 13:21:35 +00002710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002711
2712 /* Don't require hash implementations to behave correctly on a
2713 * zero-length input, which may have an invalid pointer. */
2714 if( input_length == 0 )
2715 return( PSA_SUCCESS );
2716
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002717 switch( operation->alg )
2718 {
John Durkopee4e6602020-11-27 08:48:46 -08002719#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002720 case PSA_ALG_MD2:
2721 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2722 input, input_length );
2723 break;
2724#endif
John Durkopee4e6602020-11-27 08:48:46 -08002725#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002726 case PSA_ALG_MD4:
2727 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2728 input, input_length );
2729 break;
2730#endif
John Durkopee4e6602020-11-27 08:48:46 -08002731#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002732 case PSA_ALG_MD5:
2733 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2734 input, input_length );
2735 break;
2736#endif
John Durkopee4e6602020-11-27 08:48:46 -08002737#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002738 case PSA_ALG_RIPEMD160:
2739 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2740 input, input_length );
2741 break;
2742#endif
John Durkopee4e6602020-11-27 08:48:46 -08002743#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002744 case PSA_ALG_SHA_1:
2745 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2746 input, input_length );
2747 break;
2748#endif
John Durkop6ca23272020-12-03 06:01:32 -08002749#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002750 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002751 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2752 input, input_length );
2753 break;
2754#endif
2755#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002756 case PSA_ALG_SHA_256:
2757 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2758 input, input_length );
2759 break;
2760#endif
John Durkop6ca23272020-12-03 06:01:32 -08002761#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002762 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002763 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2764 input, input_length );
2765 break;
2766#endif
2767#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002768 case PSA_ALG_SHA_512:
2769 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2770 input, input_length );
2771 break;
2772#endif
2773 default:
John Durkopee4e6602020-11-27 08:48:46 -08002774 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002775 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002776 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002777
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002778 if( ret != 0 )
2779 psa_hash_abort( operation );
2780 return( mbedtls_to_psa_error( ret ) );
2781}
2782
2783psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2784 uint8_t *hash,
2785 size_t hash_size,
2786 size_t *hash_length )
2787{
itayzafrir40835d42018-08-02 13:14:17 +03002788 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002789 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002790 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002791
2792 /* Fill the output buffer with something that isn't a valid hash
2793 * (barring an attack on the hash and deliberately-crafted input),
2794 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002795 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002796 /* If hash_size is 0 then hash may be NULL and then the
2797 * call to memset would have undefined behavior. */
2798 if( hash_size != 0 )
2799 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002800
2801 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002802 {
2803 status = PSA_ERROR_BUFFER_TOO_SMALL;
2804 goto exit;
2805 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002806
2807 switch( operation->alg )
2808 {
John Durkopee4e6602020-11-27 08:48:46 -08002809#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002810 case PSA_ALG_MD2:
2811 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2812 break;
2813#endif
John Durkopee4e6602020-11-27 08:48:46 -08002814#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002815 case PSA_ALG_MD4:
2816 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2817 break;
2818#endif
John Durkopee4e6602020-11-27 08:48:46 -08002819#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002820 case PSA_ALG_MD5:
2821 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2822 break;
2823#endif
John Durkopee4e6602020-11-27 08:48:46 -08002824#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002825 case PSA_ALG_RIPEMD160:
2826 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2827 break;
2828#endif
John Durkopee4e6602020-11-27 08:48:46 -08002829#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002830 case PSA_ALG_SHA_1:
2831 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2832 break;
2833#endif
John Durkop6ca23272020-12-03 06:01:32 -08002834#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002835 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002836 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2837 break;
2838#endif
2839#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002840 case PSA_ALG_SHA_256:
2841 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2842 break;
2843#endif
John Durkop6ca23272020-12-03 06:01:32 -08002844#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002845 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002846 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2847 break;
2848#endif
2849#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002850 case PSA_ALG_SHA_512:
2851 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2852 break;
2853#endif
2854 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002855 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002856 }
itayzafrir40835d42018-08-02 13:14:17 +03002857 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002858
itayzafrir40835d42018-08-02 13:14:17 +03002859exit:
2860 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002861 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002862 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002863 return( psa_hash_abort( operation ) );
2864 }
2865 else
2866 {
2867 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002868 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002869 }
2870}
2871
Gilles Peskine2d277862018-06-18 15:41:12 +02002872psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2873 const uint8_t *hash,
2874 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002875{
2876 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2877 size_t actual_hash_length;
2878 psa_status_t status = psa_hash_finish( operation,
2879 actual_hash, sizeof( actual_hash ),
2880 &actual_hash_length );
2881 if( status != PSA_SUCCESS )
2882 return( status );
2883 if( actual_hash_length != hash_length )
2884 return( PSA_ERROR_INVALID_SIGNATURE );
2885 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2886 return( PSA_ERROR_INVALID_SIGNATURE );
2887 return( PSA_SUCCESS );
2888}
2889
Gilles Peskine0a749c82019-11-28 19:33:58 +01002890psa_status_t psa_hash_compute( psa_algorithm_t alg,
2891 const uint8_t *input, size_t input_length,
2892 uint8_t *hash, size_t hash_size,
2893 size_t *hash_length )
2894{
2895 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2897
2898 *hash_length = hash_size;
2899 status = psa_hash_setup( &operation, alg );
2900 if( status != PSA_SUCCESS )
2901 goto exit;
2902 status = psa_hash_update( &operation, input, input_length );
2903 if( status != PSA_SUCCESS )
2904 goto exit;
2905 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2906 if( status != PSA_SUCCESS )
2907 goto exit;
2908
2909exit:
2910 if( status == PSA_SUCCESS )
2911 status = psa_hash_abort( &operation );
2912 else
2913 psa_hash_abort( &operation );
2914 return( status );
2915}
2916
2917psa_status_t psa_hash_compare( psa_algorithm_t alg,
2918 const uint8_t *input, size_t input_length,
2919 const uint8_t *hash, size_t hash_length )
2920{
2921 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2922 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2923
2924 status = psa_hash_setup( &operation, alg );
2925 if( status != PSA_SUCCESS )
2926 goto exit;
2927 status = psa_hash_update( &operation, input, input_length );
2928 if( status != PSA_SUCCESS )
2929 goto exit;
2930 status = psa_hash_verify( &operation, hash, hash_length );
2931 if( status != PSA_SUCCESS )
2932 goto exit;
2933
2934exit:
2935 if( status == PSA_SUCCESS )
2936 status = psa_hash_abort( &operation );
2937 else
2938 psa_hash_abort( &operation );
2939 return( status );
2940}
2941
Gilles Peskineeb35d782019-01-22 17:56:16 +01002942psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2943 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002944{
2945 if( target_operation->alg != 0 )
2946 return( PSA_ERROR_BAD_STATE );
2947
2948 switch( source_operation->alg )
2949 {
2950 case 0:
2951 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002952#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002953 case PSA_ALG_MD2:
2954 mbedtls_md2_clone( &target_operation->ctx.md2,
2955 &source_operation->ctx.md2 );
2956 break;
2957#endif
John Durkopee4e6602020-11-27 08:48:46 -08002958#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002959 case PSA_ALG_MD4:
2960 mbedtls_md4_clone( &target_operation->ctx.md4,
2961 &source_operation->ctx.md4 );
2962 break;
2963#endif
John Durkopee4e6602020-11-27 08:48:46 -08002964#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002965 case PSA_ALG_MD5:
2966 mbedtls_md5_clone( &target_operation->ctx.md5,
2967 &source_operation->ctx.md5 );
2968 break;
2969#endif
John Durkopee4e6602020-11-27 08:48:46 -08002970#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002971 case PSA_ALG_RIPEMD160:
2972 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2973 &source_operation->ctx.ripemd160 );
2974 break;
2975#endif
John Durkopee4e6602020-11-27 08:48:46 -08002976#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002977 case PSA_ALG_SHA_1:
2978 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2979 &source_operation->ctx.sha1 );
2980 break;
2981#endif
John Durkop6ca23272020-12-03 06:01:32 -08002982#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002983 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002984 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2985 &source_operation->ctx.sha256 );
2986 break;
2987#endif
2988#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002989 case PSA_ALG_SHA_256:
2990 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2991 &source_operation->ctx.sha256 );
2992 break;
2993#endif
John Durkop6ca23272020-12-03 06:01:32 -08002994#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002995 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002996 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2997 &source_operation->ctx.sha512 );
2998 break;
2999#endif
3000#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003001 case PSA_ALG_SHA_512:
3002 mbedtls_sha512_clone( &target_operation->ctx.sha512,
3003 &source_operation->ctx.sha512 );
3004 break;
3005#endif
3006 default:
3007 return( PSA_ERROR_NOT_SUPPORTED );
3008 }
3009
3010 target_operation->alg = source_operation->alg;
3011 return( PSA_SUCCESS );
3012}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003013
3014
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003015/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016/* MAC */
3017/****************************************************************/
3018
Gilles Peskinedc2fc842018-03-07 16:42:59 +01003019static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01003020 psa_algorithm_t alg,
3021 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02003022 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03003023 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003024{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003025 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03003026 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003027
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003028 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003029 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003030
Gilles Peskine8c9def32018-02-08 10:02:12 +01003031 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
3032 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03003033 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01003035 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01003036 mode = MBEDTLS_MODE_STREAM;
3037 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003038 case PSA_ALG_CTR:
3039 mode = MBEDTLS_MODE_CTR;
3040 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003041 case PSA_ALG_CFB:
3042 mode = MBEDTLS_MODE_CFB;
3043 break;
3044 case PSA_ALG_OFB:
3045 mode = MBEDTLS_MODE_OFB;
3046 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003047 case PSA_ALG_ECB_NO_PADDING:
3048 mode = MBEDTLS_MODE_ECB;
3049 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003050 case PSA_ALG_CBC_NO_PADDING:
3051 mode = MBEDTLS_MODE_CBC;
3052 break;
3053 case PSA_ALG_CBC_PKCS7:
3054 mode = MBEDTLS_MODE_CBC;
3055 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003056 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003057 mode = MBEDTLS_MODE_CCM;
3058 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003059 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003060 mode = MBEDTLS_MODE_GCM;
3061 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003062 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3063 mode = MBEDTLS_MODE_CHACHAPOLY;
3064 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003065 default:
3066 return( NULL );
3067 }
3068 }
3069 else if( alg == PSA_ALG_CMAC )
3070 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003071 else
3072 return( NULL );
3073
3074 switch( key_type )
3075 {
3076 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003077 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003078 break;
3079 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003080 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3081 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003082 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003083 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003084 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003085 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003086 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3087 * but two-key Triple-DES is functionally three-key Triple-DES
3088 * with K1=K3, so that's how we present it to mbedtls. */
3089 if( key_bits == 128 )
3090 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091 break;
3092 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003093 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003094 break;
3095 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003096 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003097 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003098 case PSA_KEY_TYPE_CHACHA20:
3099 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3100 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003101 default:
3102 return( NULL );
3103 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003104 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003105 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003106
Jaeden Amero23bbb752018-06-26 14:16:54 +01003107 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3108 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003109}
3110
John Durkop6ba40d12020-11-10 08:50:04 -08003111#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003112static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003113{
Gilles Peskine2d277862018-06-18 15:41:12 +02003114 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003115 {
3116 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003117 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003118 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003119 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003120 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003121 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003122 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003123 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003124 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003125 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003126 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003127 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003128 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003129 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003130 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003131 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003132 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003133 return( 128 );
3134 default:
3135 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003136 }
3137}
John Durkop07cc04a2020-11-16 22:08:34 -08003138#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003139
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003140/* Initialize the MAC operation structure. Once this function has been
3141 * called, psa_mac_abort can run and will do the right thing. */
3142static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3143 psa_algorithm_t alg )
3144{
3145 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3146
3147 operation->alg = alg;
3148 operation->key_set = 0;
3149 operation->iv_set = 0;
3150 operation->iv_required = 0;
3151 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003152 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003153
3154#if defined(MBEDTLS_CMAC_C)
3155 if( alg == PSA_ALG_CMAC )
3156 {
3157 operation->iv_required = 0;
3158 mbedtls_cipher_init( &operation->ctx.cmac );
3159 status = PSA_SUCCESS;
3160 }
3161 else
3162#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003163#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003164 if( PSA_ALG_IS_HMAC( operation->alg ) )
3165 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003166 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3167 operation->ctx.hmac.hash_ctx.alg = 0;
3168 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003169 }
3170 else
John Durkopd0321952020-10-29 21:37:36 -07003171#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003172 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003173 if( ! PSA_ALG_IS_MAC( alg ) )
3174 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003175 }
3176
3177 if( status != PSA_SUCCESS )
3178 memset( operation, 0, sizeof( *operation ) );
3179 return( status );
3180}
3181
John Durkop6ba40d12020-11-10 08:50:04 -08003182#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003183static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3184{
Gilles Peskine3f108122018-12-07 18:14:53 +01003185 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003186 return( psa_hash_abort( &hmac->hash_ctx ) );
3187}
John Durkop6ba40d12020-11-10 08:50:04 -08003188#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003189
Gilles Peskine8c9def32018-02-08 10:02:12 +01003190psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3191{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003192 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003193 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003194 /* The object has (apparently) been initialized but it is not
3195 * in use. It's ok to call abort on such an object, and there's
3196 * nothing to do. */
3197 return( PSA_SUCCESS );
3198 }
3199 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003200#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003201 if( operation->alg == PSA_ALG_CMAC )
3202 {
3203 mbedtls_cipher_free( &operation->ctx.cmac );
3204 }
3205 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003206#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003207#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003208 if( PSA_ALG_IS_HMAC( operation->alg ) )
3209 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003210 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003211 }
3212 else
John Durkopd0321952020-10-29 21:37:36 -07003213#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003214 {
3215 /* Sanity check (shouldn't happen: operation->alg should
3216 * always have been initialized to a valid value). */
3217 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003218 }
Moran Peker41deec42018-04-04 15:43:05 +03003219
Gilles Peskine8c9def32018-02-08 10:02:12 +01003220 operation->alg = 0;
3221 operation->key_set = 0;
3222 operation->iv_set = 0;
3223 operation->iv_required = 0;
3224 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003225 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003226
Gilles Peskine8c9def32018-02-08 10:02:12 +01003227 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003228
3229bad_state:
3230 /* If abort is called on an uninitialized object, we can't trust
3231 * anything. Wipe the object in case it contains confidential data.
3232 * This may result in a memory leak if a pointer gets overwritten,
3233 * but it's too late to do anything about this. */
3234 memset( operation, 0, sizeof( *operation ) );
3235 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003236}
3237
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003238#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003239static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003240 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003241 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003242 const mbedtls_cipher_info_t *cipher_info )
3243{
Janos Follath24eed8d2019-11-22 13:21:35 +00003244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003245
3246 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003247
3248 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3249 if( ret != 0 )
3250 return( ret );
3251
3252 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003253 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003254 key_bits );
3255 return( ret );
3256}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003257#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003258
John Durkop6ba40d12020-11-10 08:50:04 -08003259#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003260static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3261 const uint8_t *key,
3262 size_t key_length,
3263 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003264{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003265 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003266 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003267 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003268 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003269 psa_status_t status;
3270
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003271 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3272 * overflow below. This should never trigger if the hash algorithm
3273 * is implemented correctly. */
3274 /* The size checks against the ipad and opad buffers cannot be written
3275 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3276 * because that triggers -Wlogical-op on GCC 7.3. */
3277 if( block_size > sizeof( ipad ) )
3278 return( PSA_ERROR_NOT_SUPPORTED );
3279 if( block_size > sizeof( hmac->opad ) )
3280 return( PSA_ERROR_NOT_SUPPORTED );
3281 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003282 return( PSA_ERROR_NOT_SUPPORTED );
3283
Gilles Peskined223b522018-06-11 18:12:58 +02003284 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003285 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003286 status = psa_hash_compute( hash_alg, key, key_length,
3287 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003288 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003289 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003290 }
Gilles Peskine96889972018-07-12 17:07:03 +02003291 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3292 * but it is permitted. It is common when HMAC is used in HKDF, for
3293 * example. Don't call `memcpy` in the 0-length because `key` could be
3294 * an invalid pointer which would make the behavior undefined. */
3295 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003296 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003297
Gilles Peskined223b522018-06-11 18:12:58 +02003298 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3299 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003300 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003301 ipad[i] ^= 0x36;
3302 memset( ipad + key_length, 0x36, block_size - key_length );
3303
3304 /* Copy the key material from ipad to opad, flipping the requisite bits,
3305 * and filling the rest of opad with the requisite constant. */
3306 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003307 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3308 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003309
Gilles Peskine01126fa2018-07-12 17:04:55 +02003310 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003311 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003312 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003313
Gilles Peskine01126fa2018-07-12 17:04:55 +02003314 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003315
3316cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003317 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003318
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003319 return( status );
3320}
John Durkop6ba40d12020-11-10 08:50:04 -08003321#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003322
Gilles Peskine89167cb2018-07-08 20:12:23 +02003323static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003324 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003325 psa_algorithm_t alg,
3326 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003327{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003328 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003329 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003330 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003331 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003332 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003333 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003334 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003335 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003336
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003337 /* A context must be freshly initialized before it can be set up. */
3338 if( operation->alg != 0 )
3339 {
3340 return( PSA_ERROR_BAD_STATE );
3341 }
3342
Gilles Peskined911eb72018-08-14 15:18:45 +02003343 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003344 if( status != PSA_SUCCESS )
3345 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003346 if( is_sign )
3347 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003348
Ronald Cron5c522922020-11-14 16:35:34 +01003349 status = psa_get_and_lock_transparent_key_slot_with_policy(
3350 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003351 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003352 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003353 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003354
Gilles Peskine8c9def32018-02-08 10:02:12 +01003355#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003356 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003357 {
3358 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003359 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003360 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003362 if( cipher_info == NULL )
3363 {
3364 status = PSA_ERROR_NOT_SUPPORTED;
3365 goto exit;
3366 }
3367 operation->mac_size = cipher_info->block_size;
3368 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3369 status = mbedtls_to_psa_error( ret );
3370 }
3371 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003372#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003373#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003374 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003375 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003376 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003377 if( hash_alg == 0 )
3378 {
3379 status = PSA_ERROR_NOT_SUPPORTED;
3380 goto exit;
3381 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003382
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003383 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003384 /* Sanity check. This shouldn't fail on a valid configuration. */
3385 if( operation->mac_size == 0 ||
3386 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3387 {
3388 status = PSA_ERROR_NOT_SUPPORTED;
3389 goto exit;
3390 }
3391
Gilles Peskine8e338702019-07-30 20:06:31 +02003392 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003393 {
3394 status = PSA_ERROR_INVALID_ARGUMENT;
3395 goto exit;
3396 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003397
Gilles Peskine01126fa2018-07-12 17:04:55 +02003398 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003399 slot->key.data,
3400 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003401 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003402 }
3403 else
John Durkopd0321952020-10-29 21:37:36 -07003404#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003405 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003406 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003407 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003408 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003409
Gilles Peskined911eb72018-08-14 15:18:45 +02003410 if( truncated == 0 )
3411 {
3412 /* The "normal" case: untruncated algorithm. Nothing to do. */
3413 }
3414 else if( truncated < 4 )
3415 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003416 /* A very short MAC is too short for security since it can be
3417 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3418 * so we make this our minimum, even though 32 bits is still
3419 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003420 status = PSA_ERROR_NOT_SUPPORTED;
3421 }
3422 else if( truncated > operation->mac_size )
3423 {
3424 /* It's impossible to "truncate" to a larger length. */
3425 status = PSA_ERROR_INVALID_ARGUMENT;
3426 }
3427 else
3428 operation->mac_size = truncated;
3429
Gilles Peskinefbfac682018-07-08 20:51:54 +02003430exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003431 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003432 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003433 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003434 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003435 else
3436 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003437 operation->key_set = 1;
3438 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003439
Ronald Cron5c522922020-11-14 16:35:34 +01003440 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003441
Ronald Cron5c522922020-11-14 16:35:34 +01003442 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003443}
3444
Gilles Peskine89167cb2018-07-08 20:12:23 +02003445psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003446 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003447 psa_algorithm_t alg )
3448{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003449 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003450}
3451
3452psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003453 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003454 psa_algorithm_t alg )
3455{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003456 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003457}
3458
Gilles Peskine8c9def32018-02-08 10:02:12 +01003459psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3460 const uint8_t *input,
3461 size_t input_length )
3462{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003463 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003464 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003465 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003466 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003467 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003468 operation->has_input = 1;
3469
Gilles Peskine8c9def32018-02-08 10:02:12 +01003470#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003471 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003472 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003473 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3474 input, input_length );
3475 status = mbedtls_to_psa_error( ret );
3476 }
3477 else
3478#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003479#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003480 if( PSA_ALG_IS_HMAC( operation->alg ) )
3481 {
3482 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3483 input_length );
3484 }
3485 else
John Durkopd0321952020-10-29 21:37:36 -07003486#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003487 {
3488 /* This shouldn't happen if `operation` was initialized by
3489 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003490 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003491 }
3492
Gilles Peskinefbfac682018-07-08 20:51:54 +02003493 if( status != PSA_SUCCESS )
3494 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003495 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003496}
3497
John Durkop6ba40d12020-11-10 08:50:04 -08003498#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003499static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3500 uint8_t *mac,
3501 size_t mac_size )
3502{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003503 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003504 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3505 size_t hash_size = 0;
3506 size_t block_size = psa_get_hash_block_size( hash_alg );
3507 psa_status_t status;
3508
Gilles Peskine01126fa2018-07-12 17:04:55 +02003509 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3510 if( status != PSA_SUCCESS )
3511 return( status );
3512 /* From here on, tmp needs to be wiped. */
3513
3514 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3515 if( status != PSA_SUCCESS )
3516 goto exit;
3517
3518 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3519 if( status != PSA_SUCCESS )
3520 goto exit;
3521
3522 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3523 if( status != PSA_SUCCESS )
3524 goto exit;
3525
Gilles Peskined911eb72018-08-14 15:18:45 +02003526 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3527 if( status != PSA_SUCCESS )
3528 goto exit;
3529
3530 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003531
3532exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003533 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003534 return( status );
3535}
John Durkop6ba40d12020-11-10 08:50:04 -08003536#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003537
mohammad16036df908f2018-04-02 08:34:15 -07003538static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003539 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003540 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003541{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003542 if( ! operation->key_set )
3543 return( PSA_ERROR_BAD_STATE );
3544 if( operation->iv_required && ! operation->iv_set )
3545 return( PSA_ERROR_BAD_STATE );
3546
Gilles Peskine8c9def32018-02-08 10:02:12 +01003547 if( mac_size < operation->mac_size )
3548 return( PSA_ERROR_BUFFER_TOO_SMALL );
3549
Gilles Peskine8c9def32018-02-08 10:02:12 +01003550#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003551 if( operation->alg == PSA_ALG_CMAC )
3552 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003553 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003554 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3555 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003556 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003557 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003558 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003559 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003560 else
3561#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003562#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003563 if( PSA_ALG_IS_HMAC( operation->alg ) )
3564 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003565 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003566 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003567 }
3568 else
John Durkopd0321952020-10-29 21:37:36 -07003569#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003570 {
3571 /* This shouldn't happen if `operation` was initialized by
3572 * a setup function. */
3573 return( PSA_ERROR_BAD_STATE );
3574 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003575}
3576
Gilles Peskineacd4be32018-07-08 19:56:25 +02003577psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3578 uint8_t *mac,
3579 size_t mac_size,
3580 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003581{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003582 psa_status_t status;
3583
Jaeden Amero252ef282019-02-15 14:05:35 +00003584 if( operation->alg == 0 )
3585 {
3586 return( PSA_ERROR_BAD_STATE );
3587 }
3588
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003589 /* Fill the output buffer with something that isn't a valid mac
3590 * (barring an attack on the mac and deliberately-crafted input),
3591 * in case the caller doesn't check the return status properly. */
3592 *mac_length = mac_size;
3593 /* If mac_size is 0 then mac may be NULL and then the
3594 * call to memset would have undefined behavior. */
3595 if( mac_size != 0 )
3596 memset( mac, '!', mac_size );
3597
Gilles Peskine89167cb2018-07-08 20:12:23 +02003598 if( ! operation->is_sign )
3599 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003600 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003601 }
mohammad16036df908f2018-04-02 08:34:15 -07003602
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003603 status = psa_mac_finish_internal( operation, mac, mac_size );
3604
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003605 if( status == PSA_SUCCESS )
3606 {
3607 status = psa_mac_abort( operation );
3608 if( status == PSA_SUCCESS )
3609 *mac_length = operation->mac_size;
3610 else
3611 memset( mac, '!', mac_size );
3612 }
3613 else
3614 psa_mac_abort( operation );
3615 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003616}
3617
Gilles Peskineacd4be32018-07-08 19:56:25 +02003618psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3619 const uint8_t *mac,
3620 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003621{
Gilles Peskine828ed142018-06-18 23:25:51 +02003622 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003623 psa_status_t status;
3624
Jaeden Amero252ef282019-02-15 14:05:35 +00003625 if( operation->alg == 0 )
3626 {
3627 return( PSA_ERROR_BAD_STATE );
3628 }
3629
Gilles Peskine89167cb2018-07-08 20:12:23 +02003630 if( operation->is_sign )
3631 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003632 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003633 }
3634 if( operation->mac_size != mac_length )
3635 {
3636 status = PSA_ERROR_INVALID_SIGNATURE;
3637 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003638 }
mohammad16036df908f2018-04-02 08:34:15 -07003639
3640 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003641 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003642 if( status != PSA_SUCCESS )
3643 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003644
3645 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3646 status = PSA_ERROR_INVALID_SIGNATURE;
3647
3648cleanup:
3649 if( status == PSA_SUCCESS )
3650 status = psa_mac_abort( operation );
3651 else
3652 psa_mac_abort( operation );
3653
Gilles Peskine3f108122018-12-07 18:14:53 +01003654 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003655
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003656 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003657}
3658
3659
Gilles Peskine20035e32018-02-03 22:44:14 +01003660
Gilles Peskine20035e32018-02-03 22:44:14 +01003661/****************************************************************/
3662/* Asymmetric cryptography */
3663/****************************************************************/
3664
John Durkop6ba40d12020-11-10 08:50:04 -08003665#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3666 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003667/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003668 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003669static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3670 size_t hash_length,
3671 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003672{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003673 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003674 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003675 *md_alg = mbedtls_md_get_type( md_info );
3676
3677 /* The Mbed TLS RSA module uses an unsigned int for hash length
3678 * parameters. Validate that it fits so that we don't risk an
3679 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003680#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003681 if( hash_length > UINT_MAX )
3682 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003683#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003684
John Durkop0e005192020-10-31 22:06:54 -07003685#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003686 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3687 * must be correct. */
3688 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3689 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003690 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003691 if( md_info == NULL )
3692 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003693 if( mbedtls_md_get_size( md_info ) != hash_length )
3694 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003695 }
John Durkop0e005192020-10-31 22:06:54 -07003696#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003697
John Durkop0e005192020-10-31 22:06:54 -07003698#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003699 /* PSS requires a hash internally. */
3700 if( PSA_ALG_IS_RSA_PSS( alg ) )
3701 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003702 if( md_info == NULL )
3703 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003704 }
John Durkop0e005192020-10-31 22:06:54 -07003705#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003706
Gilles Peskine61b91d42018-06-08 16:09:36 +02003707 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003708}
3709
Gilles Peskine2b450e32018-06-27 15:42:46 +02003710static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3711 psa_algorithm_t alg,
3712 const uint8_t *hash,
3713 size_t hash_length,
3714 uint8_t *signature,
3715 size_t signature_size,
3716 size_t *signature_length )
3717{
3718 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003720 mbedtls_md_type_t md_alg;
3721
3722 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3723 if( status != PSA_SUCCESS )
3724 return( status );
3725
Gilles Peskine630a18a2018-06-29 17:49:35 +02003726 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003727 return( PSA_ERROR_BUFFER_TOO_SMALL );
3728
John Durkop0e005192020-10-31 22:06:54 -07003729#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003730 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3731 {
3732 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3733 MBEDTLS_MD_NONE );
3734 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003735 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003736 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003737 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003738 md_alg,
3739 (unsigned int) hash_length,
3740 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003741 signature );
3742 }
3743 else
John Durkop0e005192020-10-31 22:06:54 -07003744#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3745#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003746 if( PSA_ALG_IS_RSA_PSS( alg ) )
3747 {
3748 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3749 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003750 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003751 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003752 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003753 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003754 (unsigned int) hash_length,
3755 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003756 signature );
3757 }
3758 else
John Durkop0e005192020-10-31 22:06:54 -07003759#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003760 {
3761 return( PSA_ERROR_INVALID_ARGUMENT );
3762 }
3763
3764 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003765 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003766 return( mbedtls_to_psa_error( ret ) );
3767}
3768
3769static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3770 psa_algorithm_t alg,
3771 const uint8_t *hash,
3772 size_t hash_length,
3773 const uint8_t *signature,
3774 size_t signature_length )
3775{
3776 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003777 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003778 mbedtls_md_type_t md_alg;
3779
3780 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3781 if( status != PSA_SUCCESS )
3782 return( status );
3783
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003784 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3785 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003786
John Durkop0e005192020-10-31 22:06:54 -07003787#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003788 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3789 {
3790 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3791 MBEDTLS_MD_NONE );
3792 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003793 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003794 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003795 MBEDTLS_RSA_PUBLIC,
3796 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003797 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003798 hash,
3799 signature );
3800 }
3801 else
John Durkop0e005192020-10-31 22:06:54 -07003802#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3803#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003804 if( PSA_ALG_IS_RSA_PSS( alg ) )
3805 {
3806 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3807 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003808 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003809 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003810 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003811 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003812 (unsigned int) hash_length,
3813 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003814 signature );
3815 }
3816 else
John Durkop0e005192020-10-31 22:06:54 -07003817#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003818 {
3819 return( PSA_ERROR_INVALID_ARGUMENT );
3820 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003821
3822 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3823 * the rest of the signature is invalid". This has little use in
3824 * practice and PSA doesn't report this distinction. */
3825 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3826 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003827 return( mbedtls_to_psa_error( ret ) );
3828}
John Durkop6ba40d12020-11-10 08:50:04 -08003829#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3830 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003831
John Durkop6ba40d12020-11-10 08:50:04 -08003832#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3833 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003834/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3835 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3836 * (even though these functions don't modify it). */
3837static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3838 psa_algorithm_t alg,
3839 const uint8_t *hash,
3840 size_t hash_length,
3841 uint8_t *signature,
3842 size_t signature_size,
3843 size_t *signature_length )
3844{
Janos Follath24eed8d2019-11-22 13:21:35 +00003845 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003846 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003847 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003848 mbedtls_mpi_init( &r );
3849 mbedtls_mpi_init( &s );
3850
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003851 if( signature_size < 2 * curve_bytes )
3852 {
3853 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3854 goto cleanup;
3855 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003856
John Durkop0ea39e02020-10-13 19:58:20 -07003857#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003858 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3859 {
3860 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3861 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3862 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003863 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3864 &ecp->d, hash,
3865 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003866 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003867 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003868 }
3869 else
John Durkop0ea39e02020-10-13 19:58:20 -07003870#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003871 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003872 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003873 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3874 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003875 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003876 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003877 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003878
3879 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3880 signature,
3881 curve_bytes ) );
3882 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3883 signature + curve_bytes,
3884 curve_bytes ) );
3885
3886cleanup:
3887 mbedtls_mpi_free( &r );
3888 mbedtls_mpi_free( &s );
3889 if( ret == 0 )
3890 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003891 return( mbedtls_to_psa_error( ret ) );
3892}
3893
3894static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3895 const uint8_t *hash,
3896 size_t hash_length,
3897 const uint8_t *signature,
3898 size_t signature_length )
3899{
Janos Follath24eed8d2019-11-22 13:21:35 +00003900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003901 mbedtls_mpi r, s;
3902 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3903 mbedtls_mpi_init( &r );
3904 mbedtls_mpi_init( &s );
3905
3906 if( signature_length != 2 * curve_bytes )
3907 return( PSA_ERROR_INVALID_SIGNATURE );
3908
3909 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3910 signature,
3911 curve_bytes ) );
3912 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3913 signature + curve_bytes,
3914 curve_bytes ) );
3915
Steven Cooremanacda8342020-07-24 23:09:52 +02003916 /* Check whether the public part is loaded. If not, load it. */
3917 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3918 {
3919 MBEDTLS_MPI_CHK(
3920 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003921 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003922 }
3923
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003924 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3925 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003926
3927cleanup:
3928 mbedtls_mpi_free( &r );
3929 mbedtls_mpi_free( &s );
3930 return( mbedtls_to_psa_error( ret ) );
3931}
John Durkop6ba40d12020-11-10 08:50:04 -08003932#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3933 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003934
Ronald Croncf56a0a2020-08-04 09:51:30 +02003935psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003936 psa_algorithm_t alg,
3937 const uint8_t *hash,
3938 size_t hash_length,
3939 uint8_t *signature,
3940 size_t signature_size,
3941 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003942{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003943 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003944 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003945 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003946
3947 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003948 /* Immediately reject a zero-length signature buffer. This guarantees
3949 * that signature must be a valid pointer. (On the other hand, the hash
3950 * buffer can in principle be empty since it doesn't actually have
3951 * to be a hash.) */
3952 if( signature_size == 0 )
3953 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003954
Ronald Cron5c522922020-11-14 16:35:34 +01003955 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3956 PSA_KEY_USAGE_SIGN_HASH,
3957 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003958 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003959 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003960 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003961 {
3962 status = PSA_ERROR_INVALID_ARGUMENT;
3963 goto exit;
3964 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003965
Steven Cooremancd84cb42020-07-16 20:28:36 +02003966 /* Try any of the available accelerators first */
3967 status = psa_driver_wrapper_sign_hash( slot,
3968 alg,
3969 hash,
3970 hash_length,
3971 signature,
3972 signature_size,
3973 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003974 if( status != PSA_ERROR_NOT_SUPPORTED ||
3975 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003976 goto exit;
3977
Steven Cooreman7a250572020-07-17 16:43:05 +02003978 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003979#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3980 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003981 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003982 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003983 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003984
Steven Cooreman4fed4552020-08-03 14:46:03 +02003985 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003986 slot->key.data,
3987 slot->key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003988 &rsa );
3989 if( status != PSA_SUCCESS )
3990 goto exit;
3991
Steven Cooremana2371e52020-07-28 14:30:39 +02003992 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003993 alg,
3994 hash, hash_length,
3995 signature, signature_size,
3996 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003997
Steven Cooremana2371e52020-07-28 14:30:39 +02003998 mbedtls_rsa_free( rsa );
3999 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01004000 }
4001 else
John Durkop6ba40d12020-11-10 08:50:04 -08004002#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4003 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004004 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01004005 {
John Durkop9814fa22020-11-04 12:28:15 -08004006#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4007 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01004008 if(
John Durkop0ea39e02020-10-13 19:58:20 -07004009#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01004010 PSA_ALG_IS_ECDSA( alg )
4011#else
4012 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
4013#endif
4014 )
Steven Cooremanacda8342020-07-24 23:09:52 +02004015 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004016 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004017 status = psa_load_ecp_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004018 slot->key.data,
4019 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004020 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004021 if( status != PSA_SUCCESS )
4022 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004023 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004024 alg,
4025 hash, hash_length,
4026 signature, signature_size,
4027 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004028 mbedtls_ecp_keypair_free( ecp );
4029 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004030 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004031 else
John Durkop9814fa22020-11-04 12:28:15 -08004032#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4033 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004034 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004035 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004036 }
itayzafrir5c753392018-05-08 11:18:38 +03004037 }
4038 else
itayzafrir5c753392018-05-08 11:18:38 +03004039 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004040 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004041 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004042
4043exit:
4044 /* Fill the unused part of the output buffer (the whole buffer on error,
4045 * the trailing part on success) with something that isn't a valid mac
4046 * (barring an attack on the mac and deliberately-crafted input),
4047 * in case the caller doesn't check the return status properly. */
4048 if( status == PSA_SUCCESS )
4049 memset( signature + *signature_length, '!',
4050 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02004051 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004052 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004053 /* If signature_size is 0 then we have nothing to do. We must not call
4054 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02004055
Ronald Cron5c522922020-11-14 16:35:34 +01004056 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004057
Ronald Cron5c522922020-11-14 16:35:34 +01004058 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03004059}
4060
Ronald Croncf56a0a2020-08-04 09:51:30 +02004061psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004062 psa_algorithm_t alg,
4063 const uint8_t *hash,
4064 size_t hash_length,
4065 const uint8_t *signature,
4066 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03004067{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004068 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004069 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004070 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004071
Ronald Cron5c522922020-11-14 16:35:34 +01004072 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4073 PSA_KEY_USAGE_VERIFY_HASH,
4074 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004075 if( status != PSA_SUCCESS )
4076 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004077
Steven Cooreman55ae2172020-07-17 19:46:15 +02004078 /* Try any of the available accelerators first */
4079 status = psa_driver_wrapper_verify_hash( slot,
4080 alg,
4081 hash,
4082 hash_length,
4083 signature,
4084 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004085 if( status != PSA_ERROR_NOT_SUPPORTED ||
4086 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004087 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004088
John Durkop6ba40d12020-11-10 08:50:04 -08004089#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4090 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004091 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004092 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004093 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004094
Steven Cooreman4fed4552020-08-03 14:46:03 +02004095 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004096 slot->key.data,
4097 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004098 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004099 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004100 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004101
Steven Cooremana2371e52020-07-28 14:30:39 +02004102 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004103 alg,
4104 hash, hash_length,
4105 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004106 mbedtls_rsa_free( rsa );
4107 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004108 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004109 }
4110 else
John Durkop6ba40d12020-11-10 08:50:04 -08004111#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4112 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004113 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004114 {
John Durkop9814fa22020-11-04 12:28:15 -08004115#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4116 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004117 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004118 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004119 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004120 status = psa_load_ecp_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004121 slot->key.data,
4122 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004123 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004124 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004125 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004126 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004127 hash, hash_length,
4128 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004129 mbedtls_ecp_keypair_free( ecp );
4130 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004131 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004132 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004133 else
John Durkop9814fa22020-11-04 12:28:15 -08004134#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4135 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004136 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004137 status = PSA_ERROR_INVALID_ARGUMENT;
4138 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004139 }
itayzafrir5c753392018-05-08 11:18:38 +03004140 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004141 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004142 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004143 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004144 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004145
4146exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004147 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004148
Ronald Cron5c522922020-11-14 16:35:34 +01004149 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004150}
4151
John Durkop0e005192020-10-31 22:06:54 -07004152#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004153static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4154 mbedtls_rsa_context *rsa )
4155{
4156 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4157 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4158 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4159 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4160}
John Durkop0e005192020-10-31 22:06:54 -07004161#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004162
Ronald Croncf56a0a2020-08-04 09:51:30 +02004163psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004164 psa_algorithm_t alg,
4165 const uint8_t *input,
4166 size_t input_length,
4167 const uint8_t *salt,
4168 size_t salt_length,
4169 uint8_t *output,
4170 size_t output_size,
4171 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004172{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004173 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004174 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004175 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004176
Darryl Green5cc689a2018-07-24 15:34:10 +01004177 (void) input;
4178 (void) input_length;
4179 (void) salt;
4180 (void) output;
4181 (void) output_size;
4182
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004183 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004184
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004185 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4186 return( PSA_ERROR_INVALID_ARGUMENT );
4187
Ronald Cron5c522922020-11-14 16:35:34 +01004188 status = psa_get_and_lock_transparent_key_slot_with_policy(
4189 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004190 if( status != PSA_SUCCESS )
4191 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004192 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4193 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004194 {
4195 status = PSA_ERROR_INVALID_ARGUMENT;
4196 goto exit;
4197 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004198
John Durkop6ba40d12020-11-10 08:50:04 -08004199#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4200 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004201 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004202 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004203 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004204 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004205 slot->key.data,
4206 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004207 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004208 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004209 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004210
4211 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004212 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004213 status = PSA_ERROR_BUFFER_TOO_SMALL;
4214 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004215 }
John Durkop0e005192020-10-31 22:06:54 -07004216#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004217 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004218 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004219 status = mbedtls_to_psa_error(
4220 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004221 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004222 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004223 MBEDTLS_RSA_PUBLIC,
4224 input_length,
4225 input,
4226 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004227 }
4228 else
John Durkop0e005192020-10-31 22:06:54 -07004229#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4230#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004231 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004232 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004233 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004234 status = mbedtls_to_psa_error(
4235 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004236 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004237 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004238 MBEDTLS_RSA_PUBLIC,
4239 salt, salt_length,
4240 input_length,
4241 input,
4242 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004243 }
4244 else
John Durkop0e005192020-10-31 22:06:54 -07004245#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004246 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004247 status = PSA_ERROR_INVALID_ARGUMENT;
4248 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004249 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004250rsa_exit:
4251 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004252 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004253
Steven Cooremana2371e52020-07-28 14:30:39 +02004254 mbedtls_rsa_free( rsa );
4255 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004256 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004257 else
John Durkop9814fa22020-11-04 12:28:15 -08004258#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004259 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004260 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004261 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004262 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004263
4264exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004265 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004266
Ronald Cron5c522922020-11-14 16:35:34 +01004267 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004268}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004269
Ronald Croncf56a0a2020-08-04 09:51:30 +02004270psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004271 psa_algorithm_t alg,
4272 const uint8_t *input,
4273 size_t input_length,
4274 const uint8_t *salt,
4275 size_t salt_length,
4276 uint8_t *output,
4277 size_t output_size,
4278 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004279{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004280 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004281 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004282 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004283
Darryl Green5cc689a2018-07-24 15:34:10 +01004284 (void) input;
4285 (void) input_length;
4286 (void) salt;
4287 (void) output;
4288 (void) output_size;
4289
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004290 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004291
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004292 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4293 return( PSA_ERROR_INVALID_ARGUMENT );
4294
Ronald Cron5c522922020-11-14 16:35:34 +01004295 status = psa_get_and_lock_transparent_key_slot_with_policy(
4296 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004297 if( status != PSA_SUCCESS )
4298 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004299 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004300 {
4301 status = PSA_ERROR_INVALID_ARGUMENT;
4302 goto exit;
4303 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004304
John Durkop6ba40d12020-11-10 08:50:04 -08004305#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4306 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004307 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004308 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004309 mbedtls_rsa_context *rsa = NULL;
4310 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004311 slot->key.data,
4312 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004313 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004314 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004315 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004316
Steven Cooremana2371e52020-07-28 14:30:39 +02004317 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004318 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004319 status = PSA_ERROR_INVALID_ARGUMENT;
4320 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004321 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004322
John Durkop0e005192020-10-31 22:06:54 -07004323#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004324 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004325 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004326 status = mbedtls_to_psa_error(
4327 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004328 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004329 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004330 MBEDTLS_RSA_PRIVATE,
4331 output_length,
4332 input,
4333 output,
4334 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004335 }
4336 else
John Durkop0e005192020-10-31 22:06:54 -07004337#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4338#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004339 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004340 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004341 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004342 status = mbedtls_to_psa_error(
4343 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004344 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004345 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004346 MBEDTLS_RSA_PRIVATE,
4347 salt, salt_length,
4348 output_length,
4349 input,
4350 output,
4351 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004352 }
4353 else
John Durkop0e005192020-10-31 22:06:54 -07004354#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004355 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004356 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004357 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004358
Steven Cooreman4fed4552020-08-03 14:46:03 +02004359rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004360 mbedtls_rsa_free( rsa );
4361 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004362 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004363 else
John Durkop6ba40d12020-11-10 08:50:04 -08004364#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4365 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004366 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004367 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004368 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004369
4370exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004371 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004372
Ronald Cron5c522922020-11-14 16:35:34 +01004373 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004374}
Gilles Peskine20035e32018-02-03 22:44:14 +01004375
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004376
4377
mohammad1603503973b2018-03-12 15:59:30 +02004378/****************************************************************/
4379/* Symmetric cryptography */
4380/****************************************************************/
4381
Gilles Peskinee553c652018-06-04 16:22:46 +02004382static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004383 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004384 psa_algorithm_t alg,
4385 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004386{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004387 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004388 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004389 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004390 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004391 size_t key_bits;
4392 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004393 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4394 PSA_KEY_USAGE_ENCRYPT :
4395 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004396
Steven Cooremand3feccd2020-09-01 15:56:14 +02004397 /* A context must be freshly initialized before it can be set up. */
4398 if( operation->alg != 0 )
4399 return( PSA_ERROR_BAD_STATE );
4400
Steven Cooremana07b9972020-09-10 14:54:14 +02004401 /* The requested algorithm must be one that can be processed by cipher. */
4402 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004403 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004404
Steven Cooremanef8575e2020-09-11 11:44:50 +02004405 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004406 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004407 if( status != PSA_SUCCESS )
4408 goto exit;
4409
4410 /* Initialize the operation struct members, except for alg. The alg member
4411 * is used to indicate to psa_cipher_abort that there are resources to free,
4412 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004413 operation->key_set = 0;
4414 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004415 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004416 operation->iv_size = 0;
4417 operation->block_size = 0;
4418 if( alg == PSA_ALG_ECB_NO_PADDING )
4419 operation->iv_required = 0;
4420 else
4421 operation->iv_required = 1;
4422
Steven Cooremana07b9972020-09-10 14:54:14 +02004423 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004424 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004425 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004426 slot,
4427 alg );
4428 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004429 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004430 slot,
4431 alg );
4432
Steven Cooremanef8575e2020-09-11 11:44:50 +02004433 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004434 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004435 /* Once the driver context is initialised, it needs to be freed using
4436 * psa_cipher_abort. Indicate this through setting alg. */
4437 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004438 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004439
Steven Cooremand3feccd2020-09-01 15:56:14 +02004440 if( status != PSA_ERROR_NOT_SUPPORTED ||
4441 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4442 goto exit;
4443
Steven Cooremana07b9972020-09-10 14:54:14 +02004444 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004445 * available for the given algorithm & key. */
4446 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004447
Steven Cooremana07b9972020-09-10 14:54:14 +02004448 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004449 * psa_cipher_abort. Indicate there is something to be freed through setting
4450 * alg, and indicate the operation is being done using mbedtls crypto through
4451 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004452 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004453 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004454
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004455 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004456 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004457 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004458 {
4459 status = PSA_ERROR_NOT_SUPPORTED;
4460 goto exit;
4461 }
mohammad1603503973b2018-03-12 15:59:30 +02004462
mohammad1603503973b2018-03-12 15:59:30 +02004463 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004464 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004465 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004466
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004467#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004468 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004469 {
4470 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004471 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004472 memcpy( keys, slot->key.data, 16 );
4473 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004474 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4475 keys,
4476 192, cipher_operation );
4477 }
4478 else
4479#endif
4480 {
4481 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004482 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004483 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004484 }
Moran Peker41deec42018-04-04 15:43:05 +03004485 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004486 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004487
mohammad16038481e742018-03-18 13:57:31 +02004488#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004489 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004490 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004491 case PSA_ALG_CBC_NO_PADDING:
4492 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4493 MBEDTLS_PADDING_NONE );
4494 break;
4495 case PSA_ALG_CBC_PKCS7:
4496 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4497 MBEDTLS_PADDING_PKCS7 );
4498 break;
4499 default:
4500 /* The algorithm doesn't involve padding. */
4501 ret = 0;
4502 break;
4503 }
4504 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004505 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004506#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4507
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004508 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004509 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004510 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4511 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004512 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004513 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004514 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004515#if defined(MBEDTLS_CHACHA20_C)
4516 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004517 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004518 operation->iv_size = 12;
4519#endif
mohammad1603503973b2018-03-12 15:59:30 +02004520
Steven Cooreman7df02922020-09-09 15:28:49 +02004521 status = PSA_SUCCESS;
4522
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004523exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004524 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004525 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004526 if( status == PSA_SUCCESS )
4527 {
4528 /* Update operation flags for both driver and software implementations */
4529 operation->key_set = 1;
4530 }
4531 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004532 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004533
Ronald Cron5c522922020-11-14 16:35:34 +01004534 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004535
Ronald Cron5c522922020-11-14 16:35:34 +01004536 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004537}
4538
Gilles Peskinefe119512018-07-08 21:39:34 +02004539psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004540 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004541 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004542{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004543 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004544}
4545
Gilles Peskinefe119512018-07-08 21:39:34 +02004546psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004547 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004548 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004549{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004550 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004551}
4552
Gilles Peskinefe119512018-07-08 21:39:34 +02004553psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004554 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004555 size_t iv_size,
4556 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004557{
itayzafrir534bd7c2018-08-02 13:56:32 +03004558 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004560 if( operation->iv_set || ! operation->iv_required )
4561 {
4562 return( PSA_ERROR_BAD_STATE );
4563 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004564
Steven Cooremanef8575e2020-09-11 11:44:50 +02004565 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004566 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004567 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004568 iv,
4569 iv_size,
4570 iv_length );
4571 goto exit;
4572 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004573
Moran Peker41deec42018-04-04 15:43:05 +03004574 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004575 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004576 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004577 goto exit;
4578 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004579 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004580 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004581 if( ret != 0 )
4582 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004583 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004584 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004585 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004586
mohammad16038481e742018-03-18 13:57:31 +02004587 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004588 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004589
Moran Peker395db872018-05-31 14:07:14 +03004590exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004591 if( status == PSA_SUCCESS )
4592 operation->iv_set = 1;
4593 else
Moran Peker395db872018-05-31 14:07:14 +03004594 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004595 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004596}
4597
Gilles Peskinefe119512018-07-08 21:39:34 +02004598psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004599 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004600 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004601{
itayzafrir534bd7c2018-08-02 13:56:32 +03004602 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004603 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004604 if( operation->iv_set || ! operation->iv_required )
4605 {
4606 return( PSA_ERROR_BAD_STATE );
4607 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004608
Steven Cooremanef8575e2020-09-11 11:44:50 +02004609 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004610 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004611 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004612 iv,
4613 iv_length );
4614 goto exit;
4615 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004616
Moran Pekera28258c2018-05-29 16:25:04 +03004617 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004618 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004619 status = PSA_ERROR_INVALID_ARGUMENT;
4620 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004621 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004622 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4623 status = mbedtls_to_psa_error( ret );
4624exit:
4625 if( status == PSA_SUCCESS )
4626 operation->iv_set = 1;
4627 else
mohammad1603503973b2018-03-12 15:59:30 +02004628 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004629 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004630}
4631
Steven Cooreman177deba2020-09-07 17:14:14 +02004632/* Process input for which the algorithm is set to ECB mode. This requires
4633 * manual processing, since the PSA API is defined as being able to process
4634 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4635 * underlying mbedtls_cipher_update only takes full blocks. */
4636static psa_status_t psa_cipher_update_ecb_internal(
4637 mbedtls_cipher_context_t *ctx,
4638 const uint8_t *input,
4639 size_t input_length,
4640 uint8_t *output,
4641 size_t output_size,
4642 size_t *output_length )
4643{
4644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4645 size_t block_size = ctx->cipher_info->block_size;
4646 size_t internal_output_length = 0;
4647 *output_length = 0;
4648
4649 if( input_length == 0 )
4650 {
4651 status = PSA_SUCCESS;
4652 goto exit;
4653 }
4654
4655 if( ctx->unprocessed_len > 0 )
4656 {
4657 /* Fill up to block size, and run the block if there's a full one. */
4658 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4659
4660 if( input_length < bytes_to_copy )
4661 bytes_to_copy = input_length;
4662
4663 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4664 input, bytes_to_copy );
4665 input_length -= bytes_to_copy;
4666 input += bytes_to_copy;
4667 ctx->unprocessed_len += bytes_to_copy;
4668
4669 if( ctx->unprocessed_len == block_size )
4670 {
4671 status = mbedtls_to_psa_error(
4672 mbedtls_cipher_update( ctx,
4673 ctx->unprocessed_data,
4674 block_size,
4675 output, &internal_output_length ) );
4676
4677 if( status != PSA_SUCCESS )
4678 goto exit;
4679
4680 output += internal_output_length;
4681 output_size -= internal_output_length;
4682 *output_length += internal_output_length;
4683 ctx->unprocessed_len = 0;
4684 }
4685 }
4686
4687 while( input_length >= block_size )
4688 {
4689 /* Run all full blocks we have, one by one */
4690 status = mbedtls_to_psa_error(
4691 mbedtls_cipher_update( ctx, input,
4692 block_size,
4693 output, &internal_output_length ) );
4694
4695 if( status != PSA_SUCCESS )
4696 goto exit;
4697
4698 input_length -= block_size;
4699 input += block_size;
4700
4701 output += internal_output_length;
4702 output_size -= internal_output_length;
4703 *output_length += internal_output_length;
4704 }
4705
4706 if( input_length > 0 )
4707 {
4708 /* Save unprocessed bytes for later processing */
4709 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4710 input, input_length );
4711 ctx->unprocessed_len += input_length;
4712 }
4713
4714 status = PSA_SUCCESS;
4715
4716exit:
4717 return( status );
4718}
4719
Gilles Peskinee553c652018-06-04 16:22:46 +02004720psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4721 const uint8_t *input,
4722 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004723 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004724 size_t output_size,
4725 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004726{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004727 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004728 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004729 if( operation->alg == 0 )
4730 {
4731 return( PSA_ERROR_BAD_STATE );
4732 }
4733 if( operation->iv_required && ! operation->iv_set )
4734 {
4735 return( PSA_ERROR_BAD_STATE );
4736 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004737
Steven Cooremanef8575e2020-09-11 11:44:50 +02004738 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004739 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004740 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004741 input,
4742 input_length,
4743 output,
4744 output_size,
4745 output_length );
4746 goto exit;
4747 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004748
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004749 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004750 {
4751 /* Take the unprocessed partial block left over from previous
4752 * update calls, if any, plus the input to this call. Remove
4753 * the last partial block, if any. You get the data that will be
4754 * output in this call. */
4755 expected_output_size =
4756 ( operation->ctx.cipher.unprocessed_len + input_length )
4757 / operation->block_size * operation->block_size;
4758 }
4759 else
4760 {
4761 expected_output_size = input_length;
4762 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004763
Gilles Peskine89d789c2018-06-04 17:17:16 +02004764 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004765 {
4766 status = PSA_ERROR_BUFFER_TOO_SMALL;
4767 goto exit;
4768 }
mohammad160382759612018-03-12 18:16:40 +02004769
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004770 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4771 {
4772 /* mbedtls_cipher_update has an API inconsistency: it will only
4773 * process a single block at a time in ECB mode. Abstract away that
4774 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004775 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4776 input,
4777 input_length,
4778 output,
4779 output_size,
4780 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004781 }
4782 else
4783 {
4784 status = mbedtls_to_psa_error(
4785 mbedtls_cipher_update( &operation->ctx.cipher, input,
4786 input_length, output, output_length ) );
4787 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004788exit:
4789 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004790 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004791 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004792}
4793
Gilles Peskinee553c652018-06-04 16:22:46 +02004794psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4795 uint8_t *output,
4796 size_t output_size,
4797 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004798{
David Saadab4ecc272019-02-14 13:48:10 +02004799 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004800 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004801 if( operation->alg == 0 )
4802 {
4803 return( PSA_ERROR_BAD_STATE );
4804 }
4805 if( operation->iv_required && ! operation->iv_set )
4806 {
4807 return( PSA_ERROR_BAD_STATE );
4808 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004809
Steven Cooremanef8575e2020-09-11 11:44:50 +02004810 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004811 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004812 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004813 output,
4814 output_size,
4815 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004816 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004817 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004818
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004819 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004820 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004821 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004822 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004823 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004824 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004825 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004826 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004827 }
4828
Steven Cooremanef8575e2020-09-11 11:44:50 +02004829 status = mbedtls_to_psa_error(
4830 mbedtls_cipher_finish( &operation->ctx.cipher,
4831 temp_output_buffer,
4832 output_length ) );
4833 if( status != PSA_SUCCESS )
4834 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004835
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004836 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004837 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004838 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004839 memcpy( output, temp_output_buffer, *output_length );
4840 else
Janos Follath315b51c2018-07-09 16:04:51 +01004841 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004842
Steven Cooremanef8575e2020-09-11 11:44:50 +02004843exit:
4844 if( operation->mbedtls_in_use == 1 )
4845 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004846
Steven Cooremanef8575e2020-09-11 11:44:50 +02004847 if( status == PSA_SUCCESS )
4848 return( psa_cipher_abort( operation ) );
4849 else
4850 {
4851 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004852 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004853
Steven Cooremanef8575e2020-09-11 11:44:50 +02004854 return( status );
4855 }
mohammad1603503973b2018-03-12 15:59:30 +02004856}
4857
Gilles Peskinee553c652018-06-04 16:22:46 +02004858psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4859{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004860 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004861 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004862 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004863 * in use. It's ok to call abort on such an object, and there's
4864 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004865 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004866 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004867
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004868 /* Sanity check (shouldn't happen: operation->alg should
4869 * always have been initialized to a valid value). */
4870 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4871 return( PSA_ERROR_BAD_STATE );
4872
Steven Cooremanef8575e2020-09-11 11:44:50 +02004873 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004874 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004875 else
4876 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004877
Moran Peker41deec42018-04-04 15:43:05 +03004878 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004879 operation->key_set = 0;
4880 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004881 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004882 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004883 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004884 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004885
Moran Peker395db872018-05-31 14:07:14 +03004886 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004887}
4888
Gilles Peskinea0655c32018-04-30 17:06:50 +02004889
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004890
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004891
mohammad16035955c982018-04-26 00:53:03 +03004892/****************************************************************/
4893/* AEAD */
4894/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004895
Gilles Peskineedf9a652018-08-17 18:11:56 +02004896typedef struct
4897{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004898 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004899 const mbedtls_cipher_info_t *cipher_info;
4900 union
4901 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004902 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004903#if defined(MBEDTLS_CCM_C)
4904 mbedtls_ccm_context ccm;
4905#endif /* MBEDTLS_CCM_C */
4906#if defined(MBEDTLS_GCM_C)
4907 mbedtls_gcm_context gcm;
4908#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004909#if defined(MBEDTLS_CHACHAPOLY_C)
4910 mbedtls_chachapoly_context chachapoly;
4911#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004912 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004913 psa_algorithm_t core_alg;
4914 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004915 uint8_t tag_length;
4916} aead_operation_t;
4917
Ronald Cronf95a2b12020-10-22 15:24:49 +02004918#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4919
Gilles Peskine30a9e412019-01-14 18:36:12 +01004920static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004921{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004922 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004923 {
4924#if defined(MBEDTLS_CCM_C)
4925 case PSA_ALG_CCM:
4926 mbedtls_ccm_free( &operation->ctx.ccm );
4927 break;
4928#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004929#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004930 case PSA_ALG_GCM:
4931 mbedtls_gcm_free( &operation->ctx.gcm );
4932 break;
4933#endif /* MBEDTLS_GCM_C */
4934 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004935
Ronald Cron5c522922020-11-14 16:35:34 +01004936 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004937}
4938
4939static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004940 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004941 psa_key_usage_t usage,
4942 psa_algorithm_t alg )
4943{
4944 psa_status_t status;
4945 size_t key_bits;
4946 mbedtls_cipher_id_t cipher_id;
4947
Ronald Cron5c522922020-11-14 16:35:34 +01004948 status = psa_get_and_lock_transparent_key_slot_with_policy(
4949 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004950 if( status != PSA_SUCCESS )
4951 return( status );
4952
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004953 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004954
4955 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004956 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004957 &cipher_id );
4958 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004959 {
4960 status = PSA_ERROR_NOT_SUPPORTED;
4961 goto cleanup;
4962 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004963
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004964 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004965 {
4966#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004967 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4968 operation->core_alg = PSA_ALG_CCM;
4969 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004970 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4971 * The call to mbedtls_ccm_encrypt_and_tag or
4972 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004973 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004974 {
4975 status = PSA_ERROR_INVALID_ARGUMENT;
4976 goto cleanup;
4977 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004978 mbedtls_ccm_init( &operation->ctx.ccm );
4979 status = mbedtls_to_psa_error(
4980 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004981 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004982 (unsigned int) key_bits ) );
4983 if( status != 0 )
4984 goto cleanup;
4985 break;
4986#endif /* MBEDTLS_CCM_C */
4987
4988#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004989 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4990 operation->core_alg = PSA_ALG_GCM;
4991 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004992 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4993 * The call to mbedtls_gcm_crypt_and_tag or
4994 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004995 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004996 {
4997 status = PSA_ERROR_INVALID_ARGUMENT;
4998 goto cleanup;
4999 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02005000 mbedtls_gcm_init( &operation->ctx.gcm );
5001 status = mbedtls_to_psa_error(
5002 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005003 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02005004 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005005 if( status != 0 )
5006 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005007 break;
5008#endif /* MBEDTLS_GCM_C */
5009
Gilles Peskine26869f22019-05-06 15:25:00 +02005010#if defined(MBEDTLS_CHACHAPOLY_C)
5011 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
5012 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
5013 operation->full_tag_length = 16;
5014 /* We only support the default tag length. */
5015 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02005016 {
5017 status = PSA_ERROR_NOT_SUPPORTED;
5018 goto cleanup;
5019 }
Gilles Peskine26869f22019-05-06 15:25:00 +02005020 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
5021 status = mbedtls_to_psa_error(
5022 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005023 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02005024 if( status != 0 )
5025 goto cleanup;
5026 break;
5027#endif /* MBEDTLS_CHACHAPOLY_C */
5028
Gilles Peskineedf9a652018-08-17 18:11:56 +02005029 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02005030 status = PSA_ERROR_NOT_SUPPORTED;
5031 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005032 }
5033
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005034 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
5035 {
5036 status = PSA_ERROR_INVALID_ARGUMENT;
5037 goto cleanup;
5038 }
5039 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005040
Gilles Peskineedf9a652018-08-17 18:11:56 +02005041 return( PSA_SUCCESS );
5042
5043cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005044 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005045 return( status );
5046}
5047
Ronald Croncf56a0a2020-08-04 09:51:30 +02005048psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005049 psa_algorithm_t alg,
5050 const uint8_t *nonce,
5051 size_t nonce_length,
5052 const uint8_t *additional_data,
5053 size_t additional_data_length,
5054 const uint8_t *plaintext,
5055 size_t plaintext_length,
5056 uint8_t *ciphertext,
5057 size_t ciphertext_size,
5058 size_t *ciphertext_length )
5059{
mohammad16035955c982018-04-26 00:53:03 +03005060 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005061 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03005062 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02005063
mohammad1603f08a5502018-06-03 15:05:47 +03005064 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07005065
Ronald Croncf56a0a2020-08-04 09:51:30 +02005066 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005067 if( status != PSA_SUCCESS )
5068 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005069
Gilles Peskineedf9a652018-08-17 18:11:56 +02005070 /* For all currently supported modes, the tag is at the end of the
5071 * ciphertext. */
5072 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5073 {
5074 status = PSA_ERROR_BUFFER_TOO_SMALL;
5075 goto exit;
5076 }
5077 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005078
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005079#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005080 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005081 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005082 status = mbedtls_to_psa_error(
5083 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5084 MBEDTLS_GCM_ENCRYPT,
5085 plaintext_length,
5086 nonce, nonce_length,
5087 additional_data, additional_data_length,
5088 plaintext, ciphertext,
5089 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005090 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005091 else
5092#endif /* MBEDTLS_GCM_C */
5093#if defined(MBEDTLS_CCM_C)
5094 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005095 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005096 status = mbedtls_to_psa_error(
5097 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5098 plaintext_length,
5099 nonce, nonce_length,
5100 additional_data,
5101 additional_data_length,
5102 plaintext, ciphertext,
5103 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005104 }
mohammad16035c8845f2018-05-09 05:40:09 -07005105 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005106#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005107#if defined(MBEDTLS_CHACHAPOLY_C)
5108 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5109 {
5110 if( nonce_length != 12 || operation.tag_length != 16 )
5111 {
5112 status = PSA_ERROR_NOT_SUPPORTED;
5113 goto exit;
5114 }
5115 status = mbedtls_to_psa_error(
5116 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5117 plaintext_length,
5118 nonce,
5119 additional_data,
5120 additional_data_length,
5121 plaintext,
5122 ciphertext,
5123 tag ) );
5124 }
5125 else
5126#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005127 {
mohammad1603554faad2018-06-03 15:07:38 +03005128 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005129 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005130
Gilles Peskineedf9a652018-08-17 18:11:56 +02005131 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5132 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005133
Gilles Peskineedf9a652018-08-17 18:11:56 +02005134exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005135 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005136 if( status == PSA_SUCCESS )
5137 *ciphertext_length = plaintext_length + operation.tag_length;
5138 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005139}
5140
Gilles Peskineee652a32018-06-01 19:23:52 +02005141/* Locate the tag in a ciphertext buffer containing the encrypted data
5142 * followed by the tag. Return the length of the part preceding the tag in
5143 * *plaintext_length. This is the size of the plaintext in modes where
5144 * the encrypted data has the same size as the plaintext, such as
5145 * CCM and GCM. */
5146static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5147 const uint8_t *ciphertext,
5148 size_t ciphertext_length,
5149 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005150 const uint8_t **p_tag )
5151{
5152 size_t payload_length;
5153 if( tag_length > ciphertext_length )
5154 return( PSA_ERROR_INVALID_ARGUMENT );
5155 payload_length = ciphertext_length - tag_length;
5156 if( payload_length > plaintext_size )
5157 return( PSA_ERROR_BUFFER_TOO_SMALL );
5158 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005159 return( PSA_SUCCESS );
5160}
5161
Ronald Croncf56a0a2020-08-04 09:51:30 +02005162psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005163 psa_algorithm_t alg,
5164 const uint8_t *nonce,
5165 size_t nonce_length,
5166 const uint8_t *additional_data,
5167 size_t additional_data_length,
5168 const uint8_t *ciphertext,
5169 size_t ciphertext_length,
5170 uint8_t *plaintext,
5171 size_t plaintext_size,
5172 size_t *plaintext_length )
5173{
mohammad16035955c982018-04-26 00:53:03 +03005174 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005175 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005176 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005177
Gilles Peskineee652a32018-06-01 19:23:52 +02005178 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005179
Ronald Croncf56a0a2020-08-04 09:51:30 +02005180 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005181 if( status != PSA_SUCCESS )
5182 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005183
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005184 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5185 ciphertext, ciphertext_length,
5186 plaintext_size, &tag );
5187 if( status != PSA_SUCCESS )
5188 goto exit;
5189
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005190#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005191 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005192 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005193 status = mbedtls_to_psa_error(
5194 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5195 ciphertext_length - operation.tag_length,
5196 nonce, nonce_length,
5197 additional_data,
5198 additional_data_length,
5199 tag, operation.tag_length,
5200 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005201 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005202 else
5203#endif /* MBEDTLS_GCM_C */
5204#if defined(MBEDTLS_CCM_C)
5205 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005206 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005207 status = mbedtls_to_psa_error(
5208 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5209 ciphertext_length - operation.tag_length,
5210 nonce, nonce_length,
5211 additional_data,
5212 additional_data_length,
5213 ciphertext, plaintext,
5214 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005215 }
mohammad160339574652018-06-01 04:39:53 -07005216 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005217#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005218#if defined(MBEDTLS_CHACHAPOLY_C)
5219 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5220 {
5221 if( nonce_length != 12 || operation.tag_length != 16 )
5222 {
5223 status = PSA_ERROR_NOT_SUPPORTED;
5224 goto exit;
5225 }
5226 status = mbedtls_to_psa_error(
5227 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5228 ciphertext_length - operation.tag_length,
5229 nonce,
5230 additional_data,
5231 additional_data_length,
5232 tag,
5233 ciphertext,
5234 plaintext ) );
5235 }
5236 else
5237#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005238 {
mohammad1603554faad2018-06-03 15:07:38 +03005239 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005240 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005241
Gilles Peskineedf9a652018-08-17 18:11:56 +02005242 if( status != PSA_SUCCESS && plaintext_size != 0 )
5243 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005244
Gilles Peskineedf9a652018-08-17 18:11:56 +02005245exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005246 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005247 if( status == PSA_SUCCESS )
5248 *plaintext_length = ciphertext_length - operation.tag_length;
5249 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005250}
5251
Gilles Peskinea0655c32018-04-30 17:06:50 +02005252
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005253
Gilles Peskine20035e32018-02-03 22:44:14 +01005254/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005255/* Generators */
5256/****************************************************************/
5257
John Durkop07cc04a2020-11-16 22:08:34 -08005258#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5259 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5260 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5261#define AT_LEAST_ONE_BUILTIN_KDF
5262#endif
5263
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005264#define HKDF_STATE_INIT 0 /* no input yet */
5265#define HKDF_STATE_STARTED 1 /* got salt */
5266#define HKDF_STATE_KEYED 2 /* got key */
5267#define HKDF_STATE_OUTPUT 3 /* output started */
5268
Gilles Peskinecbe66502019-05-16 16:59:18 +02005269static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005270 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005271{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5273 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005274 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005276}
5277
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005278psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005279{
5280 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005281 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005282 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005283 {
5284 /* The object has (apparently) been initialized but it is not
5285 * in use. It's ok to call abort on such an object, and there's
5286 * nothing to do. */
5287 }
5288 else
John Durkopd0321952020-10-29 21:37:36 -07005289#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005290 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005291 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005292 mbedtls_free( operation->ctx.hkdf.info );
5293 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005294 }
John Durkop07cc04a2020-11-16 22:08:34 -08005295 else
5296#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5297#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5298 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5299 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005301 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005302 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005303 if( operation->ctx.tls12_prf.seed != NULL )
5304 {
5305 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5306 operation->ctx.tls12_prf.seed_length );
5307 mbedtls_free( operation->ctx.tls12_prf.seed );
5308 }
5309
5310 if( operation->ctx.tls12_prf.label != NULL )
5311 {
5312 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5313 operation->ctx.tls12_prf.label_length );
5314 mbedtls_free( operation->ctx.tls12_prf.label );
5315 }
5316
5317 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5318
5319 /* We leave the fields Ai and output_block to be erased safely by the
5320 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005321 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005322 else
John Durkop07cc04a2020-11-16 22:08:34 -08005323#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5324 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005325 {
5326 status = PSA_ERROR_BAD_STATE;
5327 }
Janos Follath083036a2019-06-11 10:22:26 +01005328 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005329 return( status );
5330}
5331
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005333 size_t *capacity)
5334{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005335 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005336 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005338 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005339 }
5340
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005341 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005342 return( PSA_SUCCESS );
5343}
5344
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005345psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005346 size_t capacity )
5347{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005348 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005349 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005350 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005351 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005352 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005353 return( PSA_SUCCESS );
5354}
5355
John Durkopd0321952020-10-29 21:37:36 -07005356#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005357/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005358 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005359static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005360 psa_algorithm_t hash_alg,
5361 uint8_t *output,
5362 size_t output_length )
5363{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005364 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005365 psa_status_t status;
5366
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005367 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5368 return( PSA_ERROR_BAD_STATE );
5369 hkdf->state = HKDF_STATE_OUTPUT;
5370
Gilles Peskinebef7f142018-07-12 17:22:21 +02005371 while( output_length != 0 )
5372 {
5373 /* Copy what remains of the current block */
5374 uint8_t n = hash_length - hkdf->offset_in_block;
5375 if( n > output_length )
5376 n = (uint8_t) output_length;
5377 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5378 output += n;
5379 output_length -= n;
5380 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005381 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005382 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005383 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005384 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005385 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005386 * object was corrupted or if this function is called directly
5387 * inside the library. */
5388 if( hkdf->block_number == 0xff )
5389 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005390
5391 /* We need a new block */
5392 ++hkdf->block_number;
5393 hkdf->offset_in_block = 0;
5394 status = psa_hmac_setup_internal( &hkdf->hmac,
5395 hkdf->prk, hash_length,
5396 hash_alg );
5397 if( status != PSA_SUCCESS )
5398 return( status );
5399 if( hkdf->block_number != 1 )
5400 {
5401 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5402 hkdf->output_block,
5403 hash_length );
5404 if( status != PSA_SUCCESS )
5405 return( status );
5406 }
5407 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5408 hkdf->info,
5409 hkdf->info_length );
5410 if( status != PSA_SUCCESS )
5411 return( status );
5412 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5413 &hkdf->block_number, 1 );
5414 if( status != PSA_SUCCESS )
5415 return( status );
5416 status = psa_hmac_finish_internal( &hkdf->hmac,
5417 hkdf->output_block,
5418 sizeof( hkdf->output_block ) );
5419 if( status != PSA_SUCCESS )
5420 return( status );
5421 }
5422
5423 return( PSA_SUCCESS );
5424}
John Durkop07cc04a2020-11-16 22:08:34 -08005425#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005426
John Durkop07cc04a2020-11-16 22:08:34 -08005427#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5428 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005429static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5430 psa_tls12_prf_key_derivation_t *tls12_prf,
5431 psa_algorithm_t alg )
5432{
5433 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005434 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005435 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5436 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005437
Janos Follath7742fee2019-06-17 12:58:10 +01005438 /* We can't be wanting more output after block 0xff, otherwise
5439 * the capacity check in psa_key_derivation_output_bytes() would have
5440 * prevented this call. It could happen only if the operation
5441 * object was corrupted or if this function is called directly
5442 * inside the library. */
5443 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005444 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005445
5446 /* We need a new block */
5447 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005448 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005449
5450 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5451 *
5452 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5453 *
5454 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5455 * HMAC_hash(secret, A(2) + seed) +
5456 * HMAC_hash(secret, A(3) + seed) + ...
5457 *
5458 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005459 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005460 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005461 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005462 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005463 * is currently extracted as `output_block` and where i is
5464 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005465 */
5466
Janos Follathea29bfb2019-06-19 12:21:20 +01005467 /* Save the hash context before using it, to preserve the hash state with
5468 * only the inner padding in it. We need this, because inner padding depends
5469 * on the key (secret in the RFC's terminology). */
5470 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5471 if( status != PSA_SUCCESS )
5472 goto cleanup;
5473
5474 /* Calculate A(i) where i = tls12_prf->block_number. */
5475 if( tls12_prf->block_number == 1 )
5476 {
5477 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5478 * the variable seed and in this instance means it in the context of the
5479 * P_hash function, where seed = label + seed.) */
5480 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5481 tls12_prf->label, tls12_prf->label_length );
5482 if( status != PSA_SUCCESS )
5483 goto cleanup;
5484 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5485 tls12_prf->seed, tls12_prf->seed_length );
5486 if( status != PSA_SUCCESS )
5487 goto cleanup;
5488 }
5489 else
5490 {
5491 /* A(i) = HMAC_hash(secret, A(i-1)) */
5492 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5493 tls12_prf->Ai, hash_length );
5494 if( status != PSA_SUCCESS )
5495 goto cleanup;
5496 }
5497
5498 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5499 tls12_prf->Ai, hash_length );
5500 if( status != PSA_SUCCESS )
5501 goto cleanup;
5502 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5503 if( status != PSA_SUCCESS )
5504 goto cleanup;
5505
5506 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5507 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5508 tls12_prf->Ai, hash_length );
5509 if( status != PSA_SUCCESS )
5510 goto cleanup;
5511 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5512 tls12_prf->label, tls12_prf->label_length );
5513 if( status != PSA_SUCCESS )
5514 goto cleanup;
5515 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5516 tls12_prf->seed, tls12_prf->seed_length );
5517 if( status != PSA_SUCCESS )
5518 goto cleanup;
5519 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5520 tls12_prf->output_block, hash_length );
5521 if( status != PSA_SUCCESS )
5522 goto cleanup;
5523 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5524 if( status != PSA_SUCCESS )
5525 goto cleanup;
5526
Janos Follath7742fee2019-06-17 12:58:10 +01005527
5528cleanup:
5529
Janos Follathea29bfb2019-06-19 12:21:20 +01005530 cleanup_status = psa_hash_abort( &backup );
5531 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5532 status = cleanup_status;
5533
5534 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005535}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005536
Janos Follath844eb0e2019-06-19 12:10:49 +01005537static psa_status_t psa_key_derivation_tls12_prf_read(
5538 psa_tls12_prf_key_derivation_t *tls12_prf,
5539 psa_algorithm_t alg,
5540 uint8_t *output,
5541 size_t output_length )
5542{
5543 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005544 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005545 psa_status_t status;
5546 uint8_t offset, length;
5547
5548 while( output_length != 0 )
5549 {
5550 /* Check if we have fully processed the current block. */
5551 if( tls12_prf->left_in_block == 0 )
5552 {
5553 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5554 alg );
5555 if( status != PSA_SUCCESS )
5556 return( status );
5557
5558 continue;
5559 }
5560
5561 if( tls12_prf->left_in_block > output_length )
5562 length = (uint8_t) output_length;
5563 else
5564 length = tls12_prf->left_in_block;
5565
5566 offset = hash_length - tls12_prf->left_in_block;
5567 memcpy( output, tls12_prf->output_block + offset, length );
5568 output += length;
5569 output_length -= length;
5570 tls12_prf->left_in_block -= length;
5571 }
5572
5573 return( PSA_SUCCESS );
5574}
John Durkop07cc04a2020-11-16 22:08:34 -08005575#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5576 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005577
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005578psa_status_t psa_key_derivation_output_bytes(
5579 psa_key_derivation_operation_t *operation,
5580 uint8_t *output,
5581 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005582{
5583 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005584 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005585
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005586 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005587 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005588 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005589 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005590 }
5591
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005592 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005593 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005594 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005595 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005596 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005597 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005598 goto exit;
5599 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005600 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005601 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005602 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005603 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005604 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5605 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005606 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005607 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005608 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005609 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005610 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005611
John Durkopd0321952020-10-29 21:37:36 -07005612#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005613 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005614 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005615 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005616 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005617 output, output_length );
5618 }
Janos Follathadbec812019-06-14 11:05:39 +01005619 else
John Durkop07cc04a2020-11-16 22:08:34 -08005620#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5621#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5622 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005623 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005624 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005625 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005626 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005627 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005628 output_length );
5629 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005630 else
John Durkop07cc04a2020-11-16 22:08:34 -08005631#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5632 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005633 {
5634 return( PSA_ERROR_BAD_STATE );
5635 }
5636
5637exit:
5638 if( status != PSA_SUCCESS )
5639 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005640 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005641 * This allows us to differentiate between exhausted operations and
5642 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5643 * operations. */
5644 psa_algorithm_t alg = operation->alg;
5645 psa_key_derivation_abort( operation );
5646 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005647 memset( output, '!', output_length );
5648 }
5649 return( status );
5650}
5651
Gilles Peskine08542d82018-07-19 17:05:42 +02005652#if defined(MBEDTLS_DES_C)
5653static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5654{
5655 if( data_size >= 8 )
5656 mbedtls_des_key_set_parity( data );
5657 if( data_size >= 16 )
5658 mbedtls_des_key_set_parity( data + 8 );
5659 if( data_size >= 24 )
5660 mbedtls_des_key_set_parity( data + 16 );
5661}
5662#endif /* MBEDTLS_DES_C */
5663
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005664static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005665 psa_key_slot_t *slot,
5666 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005667 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005668{
5669 uint8_t *data = NULL;
5670 size_t bytes = PSA_BITS_TO_BYTES( bits );
5671 psa_status_t status;
5672
Gilles Peskine8e338702019-07-30 20:06:31 +02005673 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005674 return( PSA_ERROR_INVALID_ARGUMENT );
5675 if( bits % 8 != 0 )
5676 return( PSA_ERROR_INVALID_ARGUMENT );
5677 data = mbedtls_calloc( 1, bytes );
5678 if( data == NULL )
5679 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5680
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005681 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005682 if( status != PSA_SUCCESS )
5683 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005684#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005685 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005686 psa_des_set_key_parity( data, bytes );
5687#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005688 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005689
5690exit:
5691 mbedtls_free( data );
5692 return( status );
5693}
5694
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005695psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005696 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005697 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005698{
5699 psa_status_t status;
5700 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005701 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005702
Ronald Cron81709fc2020-11-14 12:10:32 +01005703 *key = MBEDTLS_SVC_KEY_ID_INIT;
5704
Gilles Peskine0f84d622019-09-12 19:03:13 +02005705 /* Reject any attempt to create a zero-length key so that we don't
5706 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5707 if( psa_get_key_bits( attributes ) == 0 )
5708 return( PSA_ERROR_INVALID_ARGUMENT );
5709
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005710 if( ! operation->can_output_key )
5711 return( PSA_ERROR_NOT_PERMITTED );
5712
Ronald Cron81709fc2020-11-14 12:10:32 +01005713 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5714 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005715#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5716 if( driver != NULL )
5717 {
5718 /* Deriving a key in a secure element is not implemented yet. */
5719 status = PSA_ERROR_NOT_SUPPORTED;
5720 }
5721#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005722 if( status == PSA_SUCCESS )
5723 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005724 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005725 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005726 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005727 }
5728 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005729 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005730 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005731 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005732
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005733 return( status );
5734}
5735
Gilles Peskineeab56e42018-07-12 17:12:33 +02005736
5737
5738/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005739/* Key derivation */
5740/****************************************************************/
5741
John Durkop07cc04a2020-11-16 22:08:34 -08005742#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005743static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005745 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005746{
John Durkop07cc04a2020-11-16 22:08:34 -08005747 int is_kdf_alg_supported;
5748
Janos Follath5fe19732019-06-20 15:09:30 +01005749 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5750 * initialisers for this union leave some bytes unspecified.) */
5751 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5752
Gilles Peskine969c5d62019-01-16 15:53:06 +01005753 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005754#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005755 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5756 is_kdf_alg_supported = 1;
5757 else
5758#endif
5759#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5760 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5761 is_kdf_alg_supported = 1;
5762 else
5763#endif
5764#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5765 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5766 is_kdf_alg_supported = 1;
5767 else
5768#endif
5769 is_kdf_alg_supported = 0;
5770
5771 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005772 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005773 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005774 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005775 if( hash_size == 0 )
5776 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005777 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5778 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005779 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005780 {
5781 return( PSA_ERROR_NOT_SUPPORTED );
5782 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005783 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005784 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005785 }
John Durkop07cc04a2020-11-16 22:08:34 -08005786
5787 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005788}
John Durkop07cc04a2020-11-16 22:08:34 -08005789#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005790
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005791psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005792 psa_algorithm_t alg )
5793{
5794 psa_status_t status;
5795
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005796 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005797 return( PSA_ERROR_BAD_STATE );
5798
Gilles Peskine6843c292019-01-18 16:44:49 +01005799 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5800 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005801#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005802 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005803 {
5804 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005805 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005806 }
5807 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5808 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005809 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005810 }
John Durkop07cc04a2020-11-16 22:08:34 -08005811#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005812 else
5813 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005814
5815 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005816 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005817 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005818}
5819
John Durkopd0321952020-10-29 21:37:36 -07005820#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005821static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005822 psa_algorithm_t hash_alg,
5823 psa_key_derivation_step_t step,
5824 const uint8_t *data,
5825 size_t data_length )
5826{
5827 psa_status_t status;
5828 switch( step )
5829 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005830 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005831 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005832 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005833 status = psa_hmac_setup_internal( &hkdf->hmac,
5834 data, data_length,
5835 hash_alg );
5836 if( status != PSA_SUCCESS )
5837 return( status );
5838 hkdf->state = HKDF_STATE_STARTED;
5839 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005840 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005841 /* If no salt was provided, use an empty salt. */
5842 if( hkdf->state == HKDF_STATE_INIT )
5843 {
5844 status = psa_hmac_setup_internal( &hkdf->hmac,
5845 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005846 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005847 if( status != PSA_SUCCESS )
5848 return( status );
5849 hkdf->state = HKDF_STATE_STARTED;
5850 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005851 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005852 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005853 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5854 data, data_length );
5855 if( status != PSA_SUCCESS )
5856 return( status );
5857 status = psa_hmac_finish_internal( &hkdf->hmac,
5858 hkdf->prk,
5859 sizeof( hkdf->prk ) );
5860 if( status != PSA_SUCCESS )
5861 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005862 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005863 hkdf->block_number = 0;
5864 hkdf->state = HKDF_STATE_KEYED;
5865 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005866 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005867 if( hkdf->state == HKDF_STATE_OUTPUT )
5868 return( PSA_ERROR_BAD_STATE );
5869 if( hkdf->info_set )
5870 return( PSA_ERROR_BAD_STATE );
5871 hkdf->info_length = data_length;
5872 if( data_length != 0 )
5873 {
5874 hkdf->info = mbedtls_calloc( 1, data_length );
5875 if( hkdf->info == NULL )
5876 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5877 memcpy( hkdf->info, data, data_length );
5878 }
5879 hkdf->info_set = 1;
5880 return( PSA_SUCCESS );
5881 default:
5882 return( PSA_ERROR_INVALID_ARGUMENT );
5883 }
5884}
John Durkop07cc04a2020-11-16 22:08:34 -08005885#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005886
John Durkop07cc04a2020-11-16 22:08:34 -08005887#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5888 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005889static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5890 const uint8_t *data,
5891 size_t data_length )
5892{
5893 if( prf->state != TLS12_PRF_STATE_INIT )
5894 return( PSA_ERROR_BAD_STATE );
5895
Janos Follathd6dce9f2019-07-04 09:11:38 +01005896 if( data_length != 0 )
5897 {
5898 prf->seed = mbedtls_calloc( 1, data_length );
5899 if( prf->seed == NULL )
5900 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005901
Janos Follathd6dce9f2019-07-04 09:11:38 +01005902 memcpy( prf->seed, data, data_length );
5903 prf->seed_length = data_length;
5904 }
Janos Follathf08e2652019-06-13 09:05:41 +01005905
5906 prf->state = TLS12_PRF_STATE_SEED_SET;
5907
5908 return( PSA_SUCCESS );
5909}
5910
Janos Follath81550542019-06-13 14:26:34 +01005911static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5912 psa_algorithm_t hash_alg,
5913 const uint8_t *data,
5914 size_t data_length )
5915{
5916 psa_status_t status;
5917 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5918 return( PSA_ERROR_BAD_STATE );
5919
5920 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5921 if( status != PSA_SUCCESS )
5922 return( status );
5923
5924 prf->state = TLS12_PRF_STATE_KEY_SET;
5925
5926 return( PSA_SUCCESS );
5927}
5928
Janos Follath63028dd2019-06-13 09:15:47 +01005929static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5930 const uint8_t *data,
5931 size_t data_length )
5932{
5933 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5934 return( PSA_ERROR_BAD_STATE );
5935
Janos Follathd6dce9f2019-07-04 09:11:38 +01005936 if( data_length != 0 )
5937 {
5938 prf->label = mbedtls_calloc( 1, data_length );
5939 if( prf->label == NULL )
5940 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005941
Janos Follathd6dce9f2019-07-04 09:11:38 +01005942 memcpy( prf->label, data, data_length );
5943 prf->label_length = data_length;
5944 }
Janos Follath63028dd2019-06-13 09:15:47 +01005945
5946 prf->state = TLS12_PRF_STATE_LABEL_SET;
5947
5948 return( PSA_SUCCESS );
5949}
5950
Janos Follathb03233e2019-06-11 15:30:30 +01005951static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5952 psa_algorithm_t hash_alg,
5953 psa_key_derivation_step_t step,
5954 const uint8_t *data,
5955 size_t data_length )
5956{
Janos Follathb03233e2019-06-11 15:30:30 +01005957 switch( step )
5958 {
Janos Follathf08e2652019-06-13 09:05:41 +01005959 case PSA_KEY_DERIVATION_INPUT_SEED:
5960 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005961 case PSA_KEY_DERIVATION_INPUT_SECRET:
5962 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005963 case PSA_KEY_DERIVATION_INPUT_LABEL:
5964 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005965 default:
5966 return( PSA_ERROR_INVALID_ARGUMENT );
5967 }
5968}
John Durkop07cc04a2020-11-16 22:08:34 -08005969#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5970 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5971
5972#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5973static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5974 psa_tls12_prf_key_derivation_t *prf,
5975 psa_algorithm_t hash_alg,
5976 const uint8_t *data,
5977 size_t data_length )
5978{
5979 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005980 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005981 uint8_t *cur = pms;
5982
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005983 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005984 return( PSA_ERROR_INVALID_ARGUMENT );
5985
5986 /* Quoting RFC 4279, Section 2:
5987 *
5988 * The premaster secret is formed as follows: if the PSK is N octets
5989 * long, concatenate a uint16 with the value N, N zero octets, a second
5990 * uint16 with the value N, and the PSK itself.
5991 */
5992
5993 *cur++ = ( data_length >> 8 ) & 0xff;
5994 *cur++ = ( data_length >> 0 ) & 0xff;
5995 memset( cur, 0, data_length );
5996 cur += data_length;
5997 *cur++ = pms[0];
5998 *cur++ = pms[1];
5999 memcpy( cur, data, data_length );
6000 cur += data_length;
6001
6002 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
6003
6004 mbedtls_platform_zeroize( pms, sizeof( pms ) );
6005 return( status );
6006}
Janos Follath6660f0e2019-06-17 08:44:03 +01006007
6008static psa_status_t psa_tls12_prf_psk_to_ms_input(
6009 psa_tls12_prf_key_derivation_t *prf,
6010 psa_algorithm_t hash_alg,
6011 psa_key_derivation_step_t step,
6012 const uint8_t *data,
6013 size_t data_length )
6014{
6015 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01006016 {
Janos Follath6660f0e2019-06-17 08:44:03 +01006017 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
6018 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01006019 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006020
6021 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
6022}
John Durkop07cc04a2020-11-16 22:08:34 -08006023#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006024
Gilles Peskineb8965192019-09-24 16:21:10 +02006025/** Check whether the given key type is acceptable for the given
6026 * input step of a key derivation.
6027 *
6028 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6029 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6030 * Both secret and non-secret inputs can alternatively have the type
6031 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6032 * that the input was passed as a buffer rather than via a key object.
6033 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006034static int psa_key_derivation_check_input_type(
6035 psa_key_derivation_step_t step,
6036 psa_key_type_t key_type )
6037{
6038 switch( step )
6039 {
6040 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02006041 if( key_type == PSA_KEY_TYPE_DERIVE )
6042 return( PSA_SUCCESS );
6043 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006044 return( PSA_SUCCESS );
6045 break;
6046 case PSA_KEY_DERIVATION_INPUT_LABEL:
6047 case PSA_KEY_DERIVATION_INPUT_SALT:
6048 case PSA_KEY_DERIVATION_INPUT_INFO:
6049 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02006050 if( key_type == PSA_KEY_TYPE_RAW_DATA )
6051 return( PSA_SUCCESS );
6052 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006053 return( PSA_SUCCESS );
6054 break;
6055 }
6056 return( PSA_ERROR_INVALID_ARGUMENT );
6057}
6058
Janos Follathb80a94e2019-06-12 15:54:46 +01006059static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006060 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006061 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006062 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006063 const uint8_t *data,
6064 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006065{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006066 psa_status_t status;
6067 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
6068
6069 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006070 if( status != PSA_SUCCESS )
6071 goto exit;
6072
John Durkopd0321952020-10-29 21:37:36 -07006073#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006074 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006075 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006076 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006077 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006078 step, data, data_length );
6079 }
John Durkop07cc04a2020-11-16 22:08:34 -08006080 else
6081#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6082#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6083 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006084 {
Janos Follathb03233e2019-06-11 15:30:30 +01006085 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6086 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6087 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006088 }
John Durkop07cc04a2020-11-16 22:08:34 -08006089 else
6090#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6091#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6092 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006093 {
6094 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6095 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6096 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006097 }
6098 else
John Durkop07cc04a2020-11-16 22:08:34 -08006099#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006100 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006101 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006102 return( PSA_ERROR_BAD_STATE );
6103 }
6104
Gilles Peskine224b0d62019-09-23 18:13:17 +02006105exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006106 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006107 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006108 return( status );
6109}
6110
Janos Follath51f4a0f2019-06-14 11:35:55 +01006111psa_status_t psa_key_derivation_input_bytes(
6112 psa_key_derivation_operation_t *operation,
6113 psa_key_derivation_step_t step,
6114 const uint8_t *data,
6115 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006116{
Gilles Peskineb8965192019-09-24 16:21:10 +02006117 return( psa_key_derivation_input_internal( operation, step,
6118 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006119 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006120}
6121
Janos Follath51f4a0f2019-06-14 11:35:55 +01006122psa_status_t psa_key_derivation_input_key(
6123 psa_key_derivation_operation_t *operation,
6124 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006125 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006126{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006127 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006128 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006129 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006130
Ronald Cron5c522922020-11-14 16:35:34 +01006131 status = psa_get_and_lock_transparent_key_slot_with_policy(
6132 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006133 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006134 {
6135 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006136 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006137 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006138
6139 /* Passing a key object as a SECRET input unlocks the permission
6140 * to output to a key object. */
6141 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6142 operation->can_output_key = 1;
6143
Ronald Cronf95a2b12020-10-22 15:24:49 +02006144 status = psa_key_derivation_input_internal( operation,
6145 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006146 slot->key.data,
6147 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006148
Ronald Cron5c522922020-11-14 16:35:34 +01006149 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006150
Ronald Cron5c522922020-11-14 16:35:34 +01006151 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006152}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006153
6154
6155
6156/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006157/* Key agreement */
6158/****************************************************************/
6159
John Durkop6ba40d12020-11-10 08:50:04 -08006160#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006161static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6162 size_t peer_key_length,
6163 const mbedtls_ecp_keypair *our_key,
6164 uint8_t *shared_secret,
6165 size_t shared_secret_size,
6166 size_t *shared_secret_length )
6167{
Steven Cooremand4867872020-08-05 16:31:39 +02006168 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006169 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006170 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006171 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006172 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006173 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006174
Steven Cooreman4fed4552020-08-03 14:46:03 +02006175 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6176 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006177 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006178 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006179 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006180 goto exit;
6181
Jaeden Amero97271b32019-01-10 19:38:51 +00006182 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006183 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006184 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006185 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006186 status = mbedtls_to_psa_error(
6187 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6188 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006189 goto exit;
6190
Jaeden Amero97271b32019-01-10 19:38:51 +00006191 status = mbedtls_to_psa_error(
6192 mbedtls_ecdh_calc_secret( &ecdh,
6193 shared_secret_length,
6194 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006195 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006196 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006197 if( status != PSA_SUCCESS )
6198 goto exit;
6199 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6200 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006201
6202exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006203 if( status != PSA_SUCCESS )
6204 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006205 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006206 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006207 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006208
Jaeden Amero97271b32019-01-10 19:38:51 +00006209 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006210}
John Durkop6ba40d12020-11-10 08:50:04 -08006211#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006212
Gilles Peskine01d718c2018-09-18 12:01:02 +02006213#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6214
Gilles Peskine0216fe12019-04-11 21:23:21 +02006215static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6216 psa_key_slot_t *private_key,
6217 const uint8_t *peer_key,
6218 size_t peer_key_length,
6219 uint8_t *shared_secret,
6220 size_t shared_secret_size,
6221 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006222{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006223 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006224 {
John Durkop6ba40d12020-11-10 08:50:04 -08006225#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006226 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006227 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006228 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006229 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006230 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006231 private_key->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006232 private_key->key.data,
6233 private_key->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006234 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006235 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006236 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006237 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006238 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006239 shared_secret, shared_secret_size,
6240 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006241 mbedtls_ecp_keypair_free( ecp );
6242 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006243 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006244#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006245 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006246 (void) private_key;
6247 (void) peer_key;
6248 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006249 (void) shared_secret;
6250 (void) shared_secret_size;
6251 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006252 return( PSA_ERROR_NOT_SUPPORTED );
6253 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006254}
6255
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006256/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006257 * to potentially free embedded data structures and wipe confidential data.
6258 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006259static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006260 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006261 psa_key_slot_t *private_key,
6262 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006263 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006264{
6265 psa_status_t status;
6266 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6267 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006268 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006269
6270 /* Step 1: run the secret agreement algorithm to generate the shared
6271 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006272 status = psa_key_agreement_raw_internal( ka_alg,
6273 private_key,
6274 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006275 shared_secret,
6276 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006277 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006278 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006279 goto exit;
6280
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006281 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006282 * the shared secret. A shared secret is permitted wherever a key
6283 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006284 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006285 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006286 shared_secret,
6287 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006288exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006289 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006290 return( status );
6291}
6292
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006293psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006294 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006295 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006296 const uint8_t *peer_key,
6297 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006298{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006299 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006300 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006301 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006302
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006303 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006304 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006305 status = psa_get_and_lock_transparent_key_slot_with_policy(
6306 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006307 if( status != PSA_SUCCESS )
6308 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006309 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006310 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006311 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006312 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006313 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006314 else
6315 {
6316 /* If a private key has been added as SECRET, we allow the derived
6317 * key material to be used as a key in PSA Crypto. */
6318 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6319 operation->can_output_key = 1;
6320 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006321
Ronald Cron5c522922020-11-14 16:35:34 +01006322 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006323
Ronald Cron5c522922020-11-14 16:35:34 +01006324 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006325}
6326
Gilles Peskinebe697d82019-05-16 18:00:41 +02006327psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006328 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006329 const uint8_t *peer_key,
6330 size_t peer_key_length,
6331 uint8_t *output,
6332 size_t output_size,
6333 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006334{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006335 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006336 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006337 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006338
6339 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6340 {
6341 status = PSA_ERROR_INVALID_ARGUMENT;
6342 goto exit;
6343 }
Ronald Cron5c522922020-11-14 16:35:34 +01006344 status = psa_get_and_lock_transparent_key_slot_with_policy(
6345 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006346 if( status != PSA_SUCCESS )
6347 goto exit;
6348
6349 status = psa_key_agreement_raw_internal( alg, slot,
6350 peer_key, peer_key_length,
6351 output, output_size,
6352 output_length );
6353
6354exit:
6355 if( status != PSA_SUCCESS )
6356 {
6357 /* If an error happens and is not handled properly, the output
6358 * may be used as a key to protect sensitive data. Arrange for such
6359 * a key to be random, which is likely to result in decryption or
6360 * verification errors. This is better than filling the buffer with
6361 * some constant data such as zeros, which would result in the data
6362 * being protected with a reproducible, easily knowable key.
6363 */
6364 psa_generate_random( output, output_size );
6365 *output_length = output_size;
6366 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006367
Ronald Cron5c522922020-11-14 16:35:34 +01006368 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006369
Ronald Cron5c522922020-11-14 16:35:34 +01006370 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006371}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006372
6373
Gilles Peskine30524eb2020-11-13 17:02:26 +01006374
Gilles Peskine86a440b2018-11-12 18:39:40 +01006375/****************************************************************/
6376/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006377/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006378
Gilles Peskine30524eb2020-11-13 17:02:26 +01006379/** Initialize the PSA random generator.
6380 */
6381static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6382{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006383#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6384 memset( rng, 0, sizeof( *rng ) );
6385#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6386
Gilles Peskine30524eb2020-11-13 17:02:26 +01006387 /* Set default configuration if
6388 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6389 if( rng->entropy_init == NULL )
6390 rng->entropy_init = mbedtls_entropy_init;
6391 if( rng->entropy_free == NULL )
6392 rng->entropy_free = mbedtls_entropy_free;
6393
6394 rng->entropy_init( &rng->entropy );
6395#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6396 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6397 /* The PSA entropy injection feature depends on using NV seed as an entropy
6398 * source. Add NV seed as an entropy source for PSA entropy injection. */
6399 mbedtls_entropy_add_source( &rng->entropy,
6400 mbedtls_nv_seed_poll, NULL,
6401 MBEDTLS_ENTROPY_BLOCK_SIZE,
6402 MBEDTLS_ENTROPY_SOURCE_STRONG );
6403#endif
6404
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006405 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006406#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006407}
6408
6409/** Deinitialize the PSA random generator.
6410 */
6411static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6412{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006413#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6414 memset( rng, 0, sizeof( *rng ) );
6415#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006416 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006417 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006418#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006419}
6420
6421/** Seed the PSA random generator.
6422 */
6423static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6424{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006425#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6426 /* Do nothing: the external RNG seeds itself. */
6427 (void) rng;
6428 return( PSA_SUCCESS );
6429#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006430 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006431 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6432 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006433 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006434#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006435}
6436
Gilles Peskinee59236f2018-01-27 23:32:46 +01006437psa_status_t psa_generate_random( uint8_t *output,
6438 size_t output_size )
6439{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006440 GUARD_MODULE_INITIALIZED;
6441
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006442#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6443
6444 size_t output_length = 0;
6445 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6446 output, output_size,
6447 &output_length );
6448 if( status != PSA_SUCCESS )
6449 return( status );
6450 /* Breaking up a request into smaller chunks is currently not supported
6451 * for the extrernal RNG interface. */
6452 if( output_length != output_size )
6453 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6454 return( PSA_SUCCESS );
6455
6456#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6457
Gilles Peskine71ddab92021-01-04 21:01:07 +01006458 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006459 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006460 size_t request_size =
6461 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6462 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6463 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006464 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6465 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006466 if( ret != 0 )
6467 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006468 output_size -= request_size;
6469 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006470 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006471 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006472#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006473}
6474
Gilles Peskine8814fc42020-12-14 15:33:44 +01006475/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006476 *
6477 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6478 * `psa_generate_random(...)`. The state parameter is ignored since the
6479 * PSA API doesn't support passing an explicit state.
6480 *
6481 * In the non-external case, psa_generate_random() calls an
6482 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6483 * and semantics as mbedtls_psa_get_random(). As an optimization,
6484 * instead of doing this back-and-forth between the PSA API and the
6485 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6486 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006487 */
6488#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6489int mbedtls_psa_get_random( void *p_rng,
6490 unsigned char *output,
6491 size_t output_size )
6492{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006493 /* This function takes a pointer to the RNG state because that's what
6494 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6495 * its own state internally and doesn't let the caller access that state.
6496 * So we just ignore the state parameter, and in practice we'll pass
6497 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006498 (void) p_rng;
6499 psa_status_t status = psa_generate_random( output, output_size );
6500 if( status == PSA_SUCCESS )
6501 return( 0 );
6502 else
6503 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6504}
6505#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6506
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006507#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6508#include "mbedtls/entropy_poll.h"
6509
Gilles Peskine7228da22019-07-15 11:06:15 +02006510psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006511 size_t seed_size )
6512{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006513 if( global_data.initialized )
6514 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006515
6516 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6517 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6518 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6519 return( PSA_ERROR_INVALID_ARGUMENT );
6520
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006521 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006522}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006523#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006524
John Durkop07cc04a2020-11-16 22:08:34 -08006525#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006526static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6527 size_t domain_parameters_size,
6528 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006529{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006530 size_t i;
6531 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006532
Gilles Peskinee56e8782019-04-26 17:34:02 +02006533 if( domain_parameters_size == 0 )
6534 {
6535 *exponent = 65537;
6536 return( PSA_SUCCESS );
6537 }
6538
6539 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6540 * support values that fit in a 32-bit integer, which is larger than
6541 * int on just about every platform anyway. */
6542 if( domain_parameters_size > sizeof( acc ) )
6543 return( PSA_ERROR_NOT_SUPPORTED );
6544 for( i = 0; i < domain_parameters_size; i++ )
6545 acc = ( acc << 8 ) | domain_parameters[i];
6546 if( acc > INT_MAX )
6547 return( PSA_ERROR_NOT_SUPPORTED );
6548 *exponent = acc;
6549 return( PSA_SUCCESS );
6550}
John Durkop07cc04a2020-11-16 22:08:34 -08006551#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006552
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006553static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006554 psa_key_slot_t *slot, size_t bits,
6555 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006556{
Gilles Peskine8e338702019-07-30 20:06:31 +02006557 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006558
Gilles Peskinee56e8782019-04-26 17:34:02 +02006559 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006560 return( PSA_ERROR_INVALID_ARGUMENT );
6561
Gilles Peskinee59236f2018-01-27 23:32:46 +01006562 if( key_type_is_raw_bytes( type ) )
6563 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006564 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006565
6566 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006567 if( status != PSA_SUCCESS )
6568 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006569
6570 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006571 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6572 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006573 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006574
Ronald Cronea0f8a62020-11-25 17:52:23 +01006575 status = psa_generate_random( slot->key.data,
6576 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006577 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006578 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006579
6580 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006581#if defined(MBEDTLS_DES_C)
6582 if( type == PSA_KEY_TYPE_DES )
Ronald Cronea0f8a62020-11-25 17:52:23 +01006583 psa_des_set_key_parity( slot->key.data,
6584 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006585#endif /* MBEDTLS_DES_C */
6586 }
6587 else
6588
John Durkop07cc04a2020-11-16 22:08:34 -08006589#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006590 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006591 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006592 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006594 int exponent;
6595 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006596 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6597 return( PSA_ERROR_NOT_SUPPORTED );
6598 /* Accept only byte-aligned keys, for the same reasons as
6599 * in psa_import_rsa_key(). */
6600 if( bits % 8 != 0 )
6601 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006602 status = psa_read_rsa_exponent( domain_parameters,
6603 domain_parameters_size,
6604 &exponent );
6605 if( status != PSA_SUCCESS )
6606 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006607 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6608 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006609 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006610 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006611 (unsigned int) bits,
6612 exponent );
6613 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006614 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006615
6616 /* Make sure to always have an export representation available */
6617 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6618
Steven Cooreman75b74362020-07-28 14:30:13 +02006619 status = psa_allocate_buffer_to_slot( slot, bytes );
6620 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006621 {
6622 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006623 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006624 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006625
6626 status = psa_export_rsa_key( type,
6627 &rsa,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006628 slot->key.data,
Steven Cooremana01795d2020-07-24 22:48:15 +02006629 bytes,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006630 &slot->key.bytes );
Steven Cooremana01795d2020-07-24 22:48:15 +02006631 mbedtls_rsa_free( &rsa );
6632 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006633 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006634 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006635 }
6636 else
John Durkop07cc04a2020-11-16 22:08:34 -08006637#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006638
John Durkop9814fa22020-11-04 12:28:15 -08006639#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006640 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006641 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006642 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006643 mbedtls_ecp_group_id grp_id =
6644 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006645 const mbedtls_ecp_curve_info *curve_info =
6646 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006647 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006649 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006650 return( PSA_ERROR_NOT_SUPPORTED );
6651 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6652 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006653 mbedtls_ecp_keypair_init( &ecp );
6654 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006655 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006656 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006657 if( ret != 0 )
6658 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006659 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006660 return( mbedtls_to_psa_error( ret ) );
6661 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006662
6663
6664 /* Make sure to always have an export representation available */
6665 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006666 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6667 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006668 {
6669 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006670 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006671 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006672
6673 status = mbedtls_to_psa_error(
Ronald Cronea0f8a62020-11-25 17:52:23 +01006674 mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02006675
6676 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006677 if( status != PSA_SUCCESS ) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01006678 memset( slot->key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006679 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006680 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006681 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006682 }
6683 else
John Durkop9814fa22020-11-04 12:28:15 -08006684#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006685 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006686 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006687 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006688
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006689 return( PSA_SUCCESS );
6690}
Darryl Green0c6575a2018-11-07 16:05:30 +00006691
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006692psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006693 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006694{
6695 psa_status_t status;
6696 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006697 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006698
Ronald Cron81709fc2020-11-14 12:10:32 +01006699 *key = MBEDTLS_SVC_KEY_ID_INIT;
6700
Gilles Peskine0f84d622019-09-12 19:03:13 +02006701 /* Reject any attempt to create a zero-length key so that we don't
6702 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6703 if( psa_get_key_bits( attributes ) == 0 )
6704 return( PSA_ERROR_INVALID_ARGUMENT );
6705
Ronald Cron81709fc2020-11-14 12:10:32 +01006706 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6707 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006708 if( status != PSA_SUCCESS )
6709 goto exit;
6710
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006711 status = psa_driver_wrapper_generate_key( attributes,
6712 slot );
6713 if( status != PSA_ERROR_NOT_SUPPORTED ||
6714 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6715 goto exit;
6716
6717 status = psa_generate_key_internal(
6718 slot, attributes->core.bits,
6719 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006720
6721exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006722 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006723 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006724 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006725 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006726
Darryl Green0c6575a2018-11-07 16:05:30 +00006727 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006728}
6729
6730
Gilles Peskinee59236f2018-01-27 23:32:46 +01006731
6732/****************************************************************/
6733/* Module setup */
6734/****************************************************************/
6735
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006736#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006737psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6738 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6739 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6740{
6741 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6742 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006743 global_data.rng.entropy_init = entropy_init;
6744 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006745 return( PSA_SUCCESS );
6746}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006747#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006748
Gilles Peskinee59236f2018-01-27 23:32:46 +01006749void mbedtls_psa_crypto_free( void )
6750{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006751 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006752 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6753 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006754 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006755 }
6756 /* Wipe all remaining data, including configuration.
6757 * In particular, this sets all state indicator to the value
6758 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006759 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006760#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006761 /* Unregister all secure element drivers, so that we restart from
6762 * a pristine state. */
6763 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006764#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006765}
6766
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006767#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6768/** Recover a transaction that was interrupted by a power failure.
6769 *
6770 * This function is called during initialization, before psa_crypto_init()
6771 * returns. If this function returns a failure status, the initialization
6772 * fails.
6773 */
6774static psa_status_t psa_crypto_recover_transaction(
6775 const psa_crypto_transaction_t *transaction )
6776{
6777 switch( transaction->unknown.type )
6778 {
6779 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6780 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006781 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006782 * is implemented.
6783 * https://github.com/ARMmbed/mbed-crypto/issues/218
6784 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006785 default:
6786 /* We found an unsupported transaction in the storage.
6787 * We don't know what state the storage is in. Give up. */
6788 return( PSA_ERROR_STORAGE_FAILURE );
6789 }
6790}
6791#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6792
Gilles Peskinee59236f2018-01-27 23:32:46 +01006793psa_status_t psa_crypto_init( void )
6794{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006795 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006796
Gilles Peskinec6b69072018-11-20 21:42:52 +01006797 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006798 if( global_data.initialized != 0 )
6799 return( PSA_SUCCESS );
6800
Gilles Peskine30524eb2020-11-13 17:02:26 +01006801 /* Initialize and seed the random generator. */
6802 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006803 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006804 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006805 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006806 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006807 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006808
Gilles Peskine66fb1262018-12-10 16:29:04 +01006809 status = psa_initialize_key_slots( );
6810 if( status != PSA_SUCCESS )
6811 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006812
Gilles Peskined9348f22019-10-01 15:22:29 +02006813#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6814 status = psa_init_all_se_drivers( );
6815 if( status != PSA_SUCCESS )
6816 goto exit;
6817#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6818
Gilles Peskine4b734222019-07-24 15:56:31 +02006819#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006820 status = psa_crypto_load_transaction( );
6821 if( status == PSA_SUCCESS )
6822 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006823 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6824 if( status != PSA_SUCCESS )
6825 goto exit;
6826 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006827 }
6828 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6829 {
6830 /* There's no transaction to complete. It's all good. */
6831 status = PSA_SUCCESS;
6832 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006833#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006834
Gilles Peskinec6b69072018-11-20 21:42:52 +01006835 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006836 global_data.initialized = 1;
6837
6838exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006839 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006840 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006841 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006842}
6843
6844#endif /* MBEDTLS_PSA_CRYPTO_C */