blob: b0116ddfb4c9f270faa1acc66ea30513f26c9b17 [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
Gilles Peskinee59236f2018-01-27 23:32:46 +010029#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010030#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031
Ronald Crond6d28882020-12-14 14:56:02 +010032#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020035#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010037#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010038#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010039#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010040#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020042#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010044#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010045/* Include internal declarations that are useful for implementing persistently
46 * stored keys. */
47#include "psa_crypto_storage.h"
48
Gilles Peskineb2b64d32020-12-14 16:43:58 +010049#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010050
Gilles Peskineb46bef22019-07-30 21:32:04 +020051#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include <stdlib.h>
53#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020055#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056#define mbedtls_calloc calloc
57#define mbedtls_free free
58#endif
59
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010060#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020061#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000062#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020063#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020065#include "mbedtls/chacha20.h"
66#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/cipher.h"
68#include "mbedtls/ccm.h"
69#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020071#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010073#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/error.h"
75#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010077#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000078#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010079#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000080#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010081#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000082#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010084#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010085#include "mbedtls/sha1.h"
86#include "mbedtls/sha256.h"
87#include "mbedtls/sha512.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088
Gilles Peskine996deb12018-08-01 15:45:45 +020089#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
90
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020091#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
92 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
93 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020094#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020095#endif
96
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010097/****************************************************************/
98/* Global data, support functions and library management */
99/****************************************************************/
100
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200101static int key_type_is_raw_bytes( psa_key_type_t type )
102{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200103 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104}
105
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100106/* Values for psa_global_data_t::rng_state */
107#define RNG_NOT_INITIALIZED 0
108#define RNG_INITIALIZED 1
109#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100110
Gilles Peskine2d277862018-06-18 15:41:12 +0200111typedef struct
112{
Gilles Peskinec6b69072018-11-20 21:42:52 +0100113 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100114 unsigned rng_state : 2;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100115 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116} psa_global_data_t;
117
118static psa_global_data_t global_data;
119
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100120#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
121mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
122 &global_data.rng.drbg;
123#endif
124
itayzafrir0adf0fc2018-09-06 16:24:41 +0300125#define GUARD_MODULE_INITIALIZED \
126 if( global_data.initialized == 0 ) \
127 return( PSA_ERROR_BAD_STATE );
128
Steven Cooreman01164162020-07-20 15:31:37 +0200129psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100131 /* Mbed TLS error codes can combine a high-level error code and a
132 * low-level error code. The low-level error usually reflects the
133 * root cause better, so dispatch on that preferably. */
134 int low_level_ret = - ( -ret & 0x007f );
135 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskinea5905292018-02-07 20:59:33 +0100142 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9a944802018-06-21 09:35:35 +0200143 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
144 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
145 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
146 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
147 case MBEDTLS_ERR_ASN1_INVALID_DATA:
148 return( PSA_ERROR_INVALID_ARGUMENT );
149 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
150 return( PSA_ERROR_INSUFFICIENT_MEMORY );
151 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
152 return( PSA_ERROR_BUFFER_TOO_SMALL );
153
Jaeden Amero93e21112019-02-20 13:57:28 +0000154#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000155 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000156#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
158 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100159
160 case MBEDTLS_ERR_CCM_BAD_INPUT:
161 return( PSA_ERROR_INVALID_ARGUMENT );
162 case MBEDTLS_ERR_CCM_AUTH_FAILED:
163 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100164
Gilles Peskine26869f22019-05-06 15:25:00 +0200165 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
166 return( PSA_ERROR_INVALID_ARGUMENT );
167
168 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
169 return( PSA_ERROR_BAD_STATE );
170 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
171 return( PSA_ERROR_INVALID_SIGNATURE );
172
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
176 return( PSA_ERROR_INVALID_ARGUMENT );
177 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
178 return( PSA_ERROR_INSUFFICIENT_MEMORY );
179 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
180 return( PSA_ERROR_INVALID_PADDING );
181 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200182 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200186 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100187
Gilles Peskine82e57d12020-11-13 21:31:17 +0100188#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
189 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100190 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
191 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
193 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
194 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
195 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
196 return( PSA_ERROR_NOT_SUPPORTED );
197 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
198 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100199#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100200
201 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
202 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100203
Gilles Peskinee59236f2018-01-27 23:32:46 +0100204 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
205 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
206 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
207 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100208
209 case MBEDTLS_ERR_GCM_AUTH_FAILED:
210 return( PSA_ERROR_INVALID_SIGNATURE );
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200211 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
212 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200214 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100215
Gilles Peskine82e57d12020-11-13 21:31:17 +0100216#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
217 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100218 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
219 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100220 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
223 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
226 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
227#endif
228
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
230 return( PSA_ERROR_NOT_SUPPORTED );
231 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
232 return( PSA_ERROR_INVALID_ARGUMENT );
233 case MBEDTLS_ERR_MD_ALLOC_FAILED:
234 return( PSA_ERROR_INSUFFICIENT_MEMORY );
235 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
236 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237
Gilles Peskinef76aa772018-10-29 19:24:33 +0100238 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
239 return( PSA_ERROR_STORAGE_FAILURE );
240 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
241 return( PSA_ERROR_INVALID_ARGUMENT );
242 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
243 return( PSA_ERROR_INVALID_ARGUMENT );
244 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
245 return( PSA_ERROR_BUFFER_TOO_SMALL );
246 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
251 return( PSA_ERROR_INVALID_ARGUMENT );
252 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
253 return( PSA_ERROR_INSUFFICIENT_MEMORY );
254
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100255 case MBEDTLS_ERR_PK_ALLOC_FAILED:
256 return( PSA_ERROR_INSUFFICIENT_MEMORY );
257 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
258 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100261 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
263 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
266 return( PSA_ERROR_NOT_SUPPORTED );
267 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
268 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
269 return( PSA_ERROR_NOT_PERMITTED );
270 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_PK_INVALID_ALG:
273 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
274 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
275 return( PSA_ERROR_NOT_SUPPORTED );
276 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
277 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200278 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
279 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea5905292018-02-07 20:59:33 +0100280
Gilles Peskineff2d2002019-05-06 15:26:23 +0200281 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
282 return( PSA_ERROR_HARDWARE_FAILURE );
283 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
284 return( PSA_ERROR_NOT_SUPPORTED );
285
Gilles Peskinea5905292018-02-07 20:59:33 +0100286 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_RSA_INVALID_PADDING:
289 return( PSA_ERROR_INVALID_PADDING );
290 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
291 return( PSA_ERROR_HARDWARE_FAILURE );
292 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
295 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200296 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
298 return( PSA_ERROR_INVALID_SIGNATURE );
299 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
300 return( PSA_ERROR_BUFFER_TOO_SMALL );
301 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100302 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200303
itayzafrir5c753392018-05-08 11:18:38 +0300304 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300305 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300306 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300307 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
308 return( PSA_ERROR_BUFFER_TOO_SMALL );
309 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
312 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
313 return( PSA_ERROR_INVALID_SIGNATURE );
314 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
315 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100316 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
317 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100318
Janos Follatha13b9052019-11-22 12:48:59 +0000319 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
320 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300321
Gilles Peskinee59236f2018-01-27 23:32:46 +0100322 default:
David Saadab4ecc272019-02-14 13:48:10 +0200323 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100324 }
325}
326
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200327
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200328
329
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100330/****************************************************************/
331/* Key management */
332/****************************************************************/
333
John Durkop9814fa22020-11-04 12:28:15 -0800334#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800335 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100336 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
337 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
338 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100339mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100340 size_t bits,
341 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200342{
343 switch( curve )
344 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100345 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100346 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100347 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100348#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100349 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100350 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100351#endif
352#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100353 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100354 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100355#endif
356#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100357 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100358 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100359#endif
360#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100361 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100362 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100363#endif
364#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100365 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100366 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100367 case 528:
368 if( bits_is_sloppy )
369 return( MBEDTLS_ECP_DP_SECP521R1 );
370 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100371#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100372 }
373 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100374
Paul Elliott8ff510a2020-06-02 17:19:28 +0100375 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100376 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100377 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100378#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100379 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100381#endif
382#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100383 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100384 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100385#endif
386#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100387 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100388 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100389#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100390 }
391 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100392
Paul Elliott8ff510a2020-06-02 17:19:28 +0100393 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100395 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100396#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100397 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100398 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100399 case 256:
400 if( bits_is_sloppy )
401 return( MBEDTLS_ECP_DP_CURVE25519 );
402 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100403#endif
404#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100405 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100406 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100407#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100408 }
409 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100410
Paul Elliott8ff510a2020-06-02 17:19:28 +0100411 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100412 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100413 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100414#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100415 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100416 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100417#endif
418#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100419 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100420 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100421#endif
422#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100423 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100425#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 }
427 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200428 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100430 (void) bits_is_sloppy;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100431 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200432}
John Durkop9814fa22020-11-04 12:28:15 -0800433#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100434 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
435 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
436 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
437 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200438
Archana4d7ae1d2021-07-07 02:50:22 +0530439psa_status_t psa_validate_unstructured_key_bit_size( psa_key_type_t type,
Archana449608b2021-09-08 15:36:05 +0530440 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200441{
442 /* Check that the bit size is acceptable for the key type */
443 switch( type )
444 {
445 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200447 case PSA_KEY_TYPE_DERIVE:
448 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100449#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450 case PSA_KEY_TYPE_AES:
451 if( bits != 128 && bits != 192 && bits != 256 )
452 return( PSA_ERROR_INVALID_ARGUMENT );
453 break;
454#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200455#if defined(PSA_WANT_KEY_TYPE_ARIA)
456 case PSA_KEY_TYPE_ARIA:
457 if( bits != 128 && bits != 192 && bits != 256 )
458 return( PSA_ERROR_INVALID_ARGUMENT );
459 break;
460#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100461#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200462 case PSA_KEY_TYPE_CAMELLIA:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100467#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200468 case PSA_KEY_TYPE_DES:
469 if( bits != 64 && bits != 128 && bits != 192 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100473#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200474 case PSA_KEY_TYPE_CHACHA20:
475 if( bits != 256 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200479 default:
480 return( PSA_ERROR_NOT_SUPPORTED );
481 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200482 if( bits % 8 != 0 )
483 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485 return( PSA_SUCCESS );
486}
487
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100488/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100489 *
Steven Cooremancd640932021-03-08 12:00:27 +0100490 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
491 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100492 *
493 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100494 * \param[in] key_type The key type of the key to be used with the
495 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100496 *
497 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100498 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100499 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100500 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100501 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100502MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100503 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100504 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100505{
Steven Cooremancd640932021-03-08 12:00:27 +0100506 if( PSA_ALG_IS_HMAC( algorithm ) )
507 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100508 if( key_type == PSA_KEY_TYPE_HMAC )
509 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100510 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100511
512 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100513 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100514 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
515 * key. */
516 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
517 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
518 {
519 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
520 * the block length (larger than 1) for block ciphers. */
521 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
522 return( PSA_SUCCESS );
523 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100524 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100525
526 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100527}
528
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200529psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
530 size_t buffer_length )
Steven Cooreman75b74362020-07-28 14:30:13 +0200531{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100532 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200533 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200534
Ronald Cronea0f8a62020-11-25 17:52:23 +0100535 slot->key.data = mbedtls_calloc( 1, buffer_length );
536 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200537 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200538
Ronald Cronea0f8a62020-11-25 17:52:23 +0100539 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200540 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200541}
542
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200543psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
544 const uint8_t* data,
545 size_t data_length )
546{
547 psa_status_t status = psa_allocate_buffer_to_slot( slot,
548 data_length );
549 if( status != PSA_SUCCESS )
550 return( status );
551
Ronald Cronea0f8a62020-11-25 17:52:23 +0100552 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200553 return( PSA_SUCCESS );
554}
555
Ronald Crona0fe59f2020-11-29 11:14:53 +0100556psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100557 const psa_key_attributes_t *attributes,
558 const uint8_t *data, size_t data_length,
559 uint8_t *key_buffer, size_t key_buffer_size,
560 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100561{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
563 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200565 /* zero-length keys are never supported. */
566 if( data_length == 0 )
567 return( PSA_ERROR_NOT_SUPPORTED );
568
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100569 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100571 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200572
Archana449608b2021-09-08 15:36:05 +0530573 status = psa_validate_unstructured_key_bit_size( attributes->core.type,
574 *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200575 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200576 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200577
Ronald Crondd04d422020-11-28 15:14:42 +0100578 /* Copy the key material. */
579 memcpy( key_buffer, data, data_length );
580 *key_buffer_length = data_length;
581 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200582
Steven Cooreman40120f62020-10-29 11:42:22 +0100583 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100585 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200586 {
John Durkop9814fa22020-11-04 12:28:15 -0800587#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
588 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100589 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200590 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100591 return( mbedtls_psa_ecp_import_key( attributes,
592 data, data_length,
593 key_buffer, key_buffer_size,
594 key_buffer_length,
595 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100596 }
John Durkop9814fa22020-11-04 12:28:15 -0800597#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
598 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700599#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
600 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100601 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200602 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100603 return( mbedtls_psa_rsa_import_key( attributes,
604 data, data_length,
605 key_buffer, key_buffer_size,
606 key_buffer_length,
607 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200608 }
John Durkop9814fa22020-11-04 12:28:15 -0800609#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
610 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100611 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100612
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100613 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100614}
615
Gilles Peskinef603c712019-01-19 13:40:11 +0100616/** Calculate the intersection of two algorithm usage policies.
617 *
618 * Return 0 (which allows no operation) on incompatibility.
619 */
620static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100621 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100622 psa_algorithm_t alg1,
623 psa_algorithm_t alg2 )
624{
Gilles Peskine549ea862019-05-22 11:45:59 +0200625 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100626 if( alg1 == alg2 )
627 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100628 /* If the policies are from the same hash-and-sign family, check
629 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskinef7b41372021-09-22 16:15:05 +0200630 if( PSA_ALG_IS_SIGN_HASH( alg1 ) &&
631 PSA_ALG_IS_SIGN_HASH( alg2 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100632 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100633 {
Steven Cooreman03488022021-02-18 12:07:20 +0100634 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
635 return( alg2 );
636 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
637 return( alg1 );
638 }
639 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100640 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100641 * restrictive tag length. */
642 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
643 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
644 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
645 {
646 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
647 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100648 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100649
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100650 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100651 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
652 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100653 {
Steven Cooremancd640932021-03-08 12:00:27 +0100654 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
655 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100656 }
657 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100658 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100659 ( alg1_len <= alg2_len ) )
660 {
661 return( alg2 );
662 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100663 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100664 ( alg2_len <= alg1_len ) )
665 {
666 return( alg1 );
667 }
668 }
669 /* If the policies are from the same MAC family, check whether one
670 * of them is a minimum-MAC-length policy. Calculate the most
671 * restrictive tag length. */
672 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
673 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
674 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
675 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100676 /* Validate the combination of key type and algorithm. Since the base
677 * algorithm of alg1 and alg2 are the same, we only need this once. */
678 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100679 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100680
Steven Cooreman31a876d2021-03-03 20:47:40 +0100681 /* Get the (exact or at-least) output lengths for both sides of the
682 * requested intersection. None of the currently supported algorithms
683 * have an output length dependent on the actual key size, so setting it
684 * to a bogus value of 0 is currently OK.
685 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100686 * Note that for at-least-this-length wildcard algorithms, the output
687 * length is set to the shortest allowed length, which allows us to
688 * calculate the most restrictive tag length for the intersection. */
689 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
690 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100691 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100692
Steven Cooremana1d83222021-02-25 10:20:29 +0100693 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100694 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
695 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100696 {
Steven Cooremancd640932021-03-08 12:00:27 +0100697 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100698 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100699
700 /* If only one is an at-least-this-length policy, the intersection would
701 * be the other (fixed-length) policy as long as said fixed length is
702 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100703 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100704 {
Steven Cooremancd640932021-03-08 12:00:27 +0100705 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100706 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100707 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100708 {
Steven Cooremancd640932021-03-08 12:00:27 +0100709 return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100710 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100711
Steven Cooremancd640932021-03-08 12:00:27 +0100712 /* If none of them are wildcards, check whether they define the same tag
713 * length. This is still possible here when one is default-length and
714 * the other specific-length. Ensure to always return the
715 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100716 if( alg1_len == alg2_len )
717 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100718 }
719 /* If the policies are incompatible, allow nothing. */
720 return( 0 );
721}
722
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100723static int psa_key_algorithm_permits( psa_key_type_t key_type,
724 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200725 psa_algorithm_t requested_alg )
726{
Gilles Peskine549ea862019-05-22 11:45:59 +0200727 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200728 if( requested_alg == policy_alg )
729 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100730 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
731 * and requested_alg is the same hash-and-sign family with any hash,
732 * then requested_alg is compliant with policy_alg. */
Gilles Peskinef7b41372021-09-22 16:15:05 +0200733 if( PSA_ALG_IS_SIGN_HASH( requested_alg ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100734 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200735 {
Steven Cooreman03488022021-02-18 12:07:20 +0100736 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
737 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
738 }
739 /* If policy_alg is a wildcard AEAD algorithm of the same base as
740 * the requested algorithm, check the requested tag length to be
741 * equal-length or longer than the wildcard-specified length. */
742 if( PSA_ALG_IS_AEAD( policy_alg ) &&
743 PSA_ALG_IS_AEAD( requested_alg ) &&
744 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
745 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100746 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100747 {
748 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
749 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
750 }
Steven Cooremancd640932021-03-08 12:00:27 +0100751 /* If policy_alg is a MAC algorithm of the same base as the requested
752 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100753 if( PSA_ALG_IS_MAC( policy_alg ) &&
754 PSA_ALG_IS_MAC( requested_alg ) &&
755 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100756 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100757 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100758 /* Validate the combination of key type and algorithm. Since the policy
759 * and requested algorithms are the same, we only need this once. */
760 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100761 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100762
Steven Cooreman31a876d2021-03-03 20:47:40 +0100763 /* Get both the requested output length for the algorithm which is to be
764 * verified, and the default output length for the base algorithm.
765 * Note that none of the currently supported algorithms have an output
766 * length dependent on actual key size, so setting it to a bogus value
767 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100768 size_t requested_output_length = PSA_MAC_LENGTH(
769 key_type, 0, requested_alg );
770 size_t default_output_length = PSA_MAC_LENGTH(
771 key_type, 0,
772 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100773
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100774 /* If the policy is default-length, only allow an algorithm with
775 * a declared exact-length matching the default. */
776 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100777 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100778
779 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100780 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100781 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
782 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
783 {
784 return( 1 );
785 }
786
Steven Cooremancd640932021-03-08 12:00:27 +0100787 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
788 * check for the requested MAC length to be equal to or longer than the
789 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100790 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
791 {
792 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100793 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100794 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200795 }
Steven Cooremance48e852020-10-05 16:02:45 +0200796 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200797 * a key derivation with that key agreement should also be allowed. This
798 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200799 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
800 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
801 {
802 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
803 policy_alg );
804 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100805 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200806 return( 0 );
807}
808
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100809/** Test whether a policy permits an algorithm.
810 *
811 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100812 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100813 * \note This function requires providing the key type for which the policy is
814 * being validated, since some algorithm policy definitions (e.g. MAC)
815 * have different properties depending on what kind of cipher it is
816 * combined with.
817 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100818 * \retval PSA_SUCCESS When \p alg is a specific algorithm
819 * allowed by the \p policy.
820 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
821 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
822 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100823 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100824static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100825 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100826 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100827{
Steven Cooremand788fab2021-02-25 11:29:17 +0100828 /* '0' is not a valid algorithm */
829 if( alg == 0 )
830 return( PSA_ERROR_INVALID_ARGUMENT );
831
Steven Cooreman7e39f052021-02-23 14:18:32 +0100832 /* A requested algorithm cannot be a wildcard. */
833 if( PSA_ALG_IS_WILDCARD( alg ) )
834 return( PSA_ERROR_INVALID_ARGUMENT );
835
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100836 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
837 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100838 return( PSA_SUCCESS );
839 else
840 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841}
842
Gilles Peskinef603c712019-01-19 13:40:11 +0100843/** Restrict a key policy based on a constraint.
844 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100845 * \note This function requires providing the key type for which the policy is
846 * being restricted, since some algorithm policy definitions (e.g. MAC)
847 * have different properties depending on what kind of cipher it is
848 * combined with.
849 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100850 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100851 * \param[in,out] policy The policy to restrict.
852 * \param[in] constraint The policy constraint to apply.
853 *
854 * \retval #PSA_SUCCESS
855 * \c *policy contains the intersection of the original value of
856 * \c *policy and \c *constraint.
857 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100858 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100859 * \c *policy is unchanged.
860 */
861static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100862 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100863 psa_key_policy_t *policy,
864 const psa_key_policy_t *constraint )
865{
866 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100867 psa_key_policy_algorithm_intersection( key_type, policy->alg,
868 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200869 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100870 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
871 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100872 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
873 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200874 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
875 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100876 policy->usage &= constraint->usage;
877 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200878 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 return( PSA_SUCCESS );
880}
881
Ronald Cron5c522922020-11-14 16:35:34 +0100882/** Get the description of a key given its identifier and policy constraints
883 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200884 *
Ronald Cron5c522922020-11-14 16:35:34 +0100885 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100886 * nonzero, the key must allow operations with this algorithm. If \p alg is
887 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100888 *
Ronald Cron5c522922020-11-14 16:35:34 +0100889 * In case of a persistent key, the function loads the description of the key
890 * into a key slot if not already done.
891 *
892 * On success, the returned key slot is locked. It is the responsibility of
893 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200894 */
Ronald Cron5c522922020-11-14 16:35:34 +0100895static psa_status_t psa_get_and_lock_key_slot_with_policy(
896 mbedtls_svc_key_id_t key,
897 psa_key_slot_t **p_slot,
898 psa_key_usage_t usage,
899 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100900{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200901 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
902 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100903
Ronald Cron5c522922020-11-14 16:35:34 +0100904 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100905 if( status != PSA_SUCCESS )
906 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200907 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100908
909 /* Enforce that usage policy for the key slot contains all the flags
910 * required by the usage parameter. There is one exception: public
911 * keys can always be exported, so we treat public key objects as
912 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200913 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100914 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200915
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100917 {
918 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200919 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100920 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100921
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800922 /* Enforce that the usage policy permits the requested algorithm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100923 if( alg != 0 )
924 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100925 status = psa_key_policy_permits( &slot->attr.policy,
926 slot->attr.type,
927 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +0100928 if( status != PSA_SUCCESS )
929 goto error;
930 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100931
Darryl Green06fd18d2018-07-16 11:21:11 +0100932 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200933
934error:
935 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100936 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200937
938 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100939}
Darryl Green940d72c2018-07-13 13:18:51 +0100940
Ronald Cron5c522922020-11-14 16:35:34 +0100941/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200942 *
943 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200944 * available, as opposed to a key in a secure element and/or to be used
945 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200946 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200947 * This is a temporary function that may be used instead of
948 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
949 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200950 *
Ronald Cron5c522922020-11-14 16:35:34 +0100951 * On success, the returned key slot is locked. It is the responsibility of the
952 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200953 */
Ronald Cron5c522922020-11-14 16:35:34 +0100954static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
955 mbedtls_svc_key_id_t key,
956 psa_key_slot_t **p_slot,
957 psa_key_usage_t usage,
958 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200959{
Ronald Cron5c522922020-11-14 16:35:34 +0100960 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
961 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200962 if( status != PSA_SUCCESS )
963 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200964
Ronald Cronddae0f52021-08-24 15:39:44 +0200965 if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200966 {
Ronald Cron5c522922020-11-14 16:35:34 +0100967 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200968 *p_slot = NULL;
969 return( PSA_ERROR_NOT_SUPPORTED );
970 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200971
Gilles Peskine28f8f302019-07-24 13:30:31 +0200972 return( PSA_SUCCESS );
973}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200974
Steven Cooreman7ddee7f2021-04-07 18:08:30 +0200975psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000976{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100977 /* Data pointer will always be either a valid pointer or NULL in an
978 * initialized slot, so we can just free it. */
979 if( slot->key.data != NULL )
980 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
981
982 mbedtls_free( slot->key.data );
983 slot->key.data = NULL;
984 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000985
986 return( PSA_SUCCESS );
987}
988
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100989/** Completely wipe a slot in memory, including its policy.
990 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100991psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100992{
993 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +0100994
TRodziewicz18cddc02021-07-13 12:19:15 +0200995 /*
996 * As the return error code may not be handled in case of multiple errors,
TRodziewiczc9890e92021-07-14 10:16:26 +0200997 * do our best to report an unexpected lock counter. Assert with
998 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
999 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1000 * function is called as part of the execution of a test suite, the
1001 * execution of the test suite is stopped in error if the assertion fails.
TRodziewicz18cddc02021-07-13 12:19:15 +02001002 */
Ronald Cron5c522922020-11-14 16:35:34 +01001003 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001004 {
TRodziewicz7871c2e2021-07-07 17:29:43 +02001005 MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001006 status = PSA_ERROR_CORRUPTION_DETECTED;
1007 }
1008
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001009 /* Multipart operations may still be using the key. This is safe
1010 * because all multipart operation objects are independent from
1011 * the key slot: if they need to access the key after the setup
1012 * phase, they have a copy of the key. Note that this means that
1013 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001014 /* At this point, key material and other type-specific content has
1015 * been wiped. Clear remaining metadata. We can call memset and not
1016 * zeroize because the metadata is not particularly sensitive. */
1017 memset( slot, 0, sizeof( *slot ) );
1018 return( status );
1019}
1020
Ronald Croncf56a0a2020-08-04 09:51:30 +02001021psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001023 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001024 psa_status_t status; /* status of the last operation */
1025 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001026#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1027 psa_se_drv_table_entry_t *driver;
1028#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001029
Ronald Croncf56a0a2020-08-04 09:51:30 +02001030 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001031 return( PSA_SUCCESS );
1032
Ronald Cronf2911112020-10-29 17:51:10 +01001033 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001034 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001035 * key, this will load the key description from persistent memory if not
1036 * done yet. We cannot avoid this loading as without it we don't know if
1037 * the key is operated by an SE or not and this information is needed by
1038 * the current implementation.
1039 */
Ronald Cron5c522922020-11-14 16:35:34 +01001040 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001041 if( status != PSA_SUCCESS )
1042 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001043
Ronald Cronf2911112020-10-29 17:51:10 +01001044 /*
1045 * If the key slot containing the key description is under access by the
1046 * library (apart from the present access), the key cannot be destroyed
1047 * yet. For the time being, just return in error. Eventually (to be
1048 * implemented), the key should be destroyed when all accesses have
1049 * stopped.
1050 */
Ronald Cron5c522922020-11-14 16:35:34 +01001051 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001052 {
Ronald Cron5c522922020-11-14 16:35:34 +01001053 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001054 return( PSA_ERROR_GENERIC_ERROR );
1055 }
1056
Gilles Peskine6687cd02021-04-21 22:32:05 +02001057 if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) )
1058 {
1059 /* Refuse the destruction of a read-only key (which may or may not work
1060 * if we attempt it, depending on whether the key is merely read-only
1061 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001062 * Just do the best we can, which is to wipe the copy in memory
1063 * (done in this function's cleanup code). */
1064 overall_status = PSA_ERROR_NOT_PERMITTED;
1065 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001066 }
1067
Gilles Peskine354f7672019-07-12 23:46:38 +02001068#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001069 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001070 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001071 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001072 /* For a key in a secure element, we need to do three things:
1073 * remove the key file in internal storage, destroy the
1074 * key inside the secure element, and update the driver's
1075 * persistent data. Start a transaction that will encompass these
1076 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001077 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001078 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001079 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001080 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001081 status = psa_crypto_save_transaction( );
1082 if( status != PSA_SUCCESS )
1083 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001084 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001085 /* We should still try to destroy the key in the secure
1086 * element and the key metadata in storage. This is especially
1087 * important if the error is that the storage is full.
1088 * But how to do it exactly without risking an inconsistent
1089 * state after a reset?
1090 * https://github.com/ARMmbed/mbed-crypto/issues/215
1091 */
1092 overall_status = status;
1093 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001094 }
1095
Ronald Cronea0f8a62020-11-25 17:52:23 +01001096 status = psa_destroy_se_key( driver,
1097 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001098 if( overall_status == PSA_SUCCESS )
1099 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001100 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001101#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1102
Darryl Greend49a4992018-06-18 17:27:26 +01001103#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001104 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001105 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001106 status = psa_destroy_persistent_key( slot->attr.id );
1107 if( overall_status == PSA_SUCCESS )
1108 overall_status = status;
1109
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001110 /* TODO: other slots may have a copy of the same key. We should
1111 * invalidate them.
1112 * https://github.com/ARMmbed/mbed-crypto/issues/214
1113 */
Darryl Greend49a4992018-06-18 17:27:26 +01001114 }
1115#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001116
Gilles Peskinefc762652019-07-22 19:30:34 +02001117#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1118 if( driver != NULL )
1119 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001120 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001121 if( overall_status == PSA_SUCCESS )
1122 overall_status = status;
1123 status = psa_crypto_stop_transaction( );
1124 if( overall_status == PSA_SUCCESS )
1125 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001126 }
1127#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1128
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001129exit:
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001130 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001131 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001132 if( status != PSA_SUCCESS )
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001133 overall_status = status;
1134 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001135}
1136
John Durkop07cc04a2020-11-16 22:08:34 -08001137#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001138 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001139static psa_status_t psa_get_rsa_public_exponent(
1140 const mbedtls_rsa_context *rsa,
1141 psa_key_attributes_t *attributes )
1142{
1143 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001145 uint8_t *buffer = NULL;
1146 size_t buflen;
1147 mbedtls_mpi_init( &mpi );
1148
1149 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1150 if( ret != 0 )
1151 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001152 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1153 {
1154 /* It's the default value, which is reported as an empty string,
1155 * so there's nothing to do. */
1156 goto exit;
1157 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001158
1159 buflen = mbedtls_mpi_size( &mpi );
1160 buffer = mbedtls_calloc( 1, buflen );
1161 if( buffer == NULL )
1162 {
1163 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1164 goto exit;
1165 }
1166 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1167 if( ret != 0 )
1168 goto exit;
1169 attributes->domain_parameters = buffer;
1170 attributes->domain_parameters_size = buflen;
1171
1172exit:
1173 mbedtls_mpi_free( &mpi );
1174 if( ret != 0 )
1175 mbedtls_free( buffer );
1176 return( mbedtls_to_psa_error( ret ) );
1177}
John Durkop07cc04a2020-11-16 22:08:34 -08001178#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001179 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001180
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001181/** Retrieve all the publicly-accessible attributes of a key.
1182 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001183psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001184 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001185{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001186 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001187 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001188 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001189
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001190 psa_reset_key_attributes( attributes );
1191
Ronald Cron5c522922020-11-14 16:35:34 +01001192 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001193 if( status != PSA_SUCCESS )
1194 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001195
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001196 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001197 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1198 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1199
1200#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron512ad812021-08-24 15:50:05 +02001201 if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001202 psa_set_key_slot_number( attributes,
1203 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001204#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001205
Gilles Peskine8e338702019-07-30 20:06:31 +02001206 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001207 {
John Durkop07cc04a2020-11-16 22:08:34 -08001208#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001209 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001210 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001211 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001212 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001213 * is not yet implemented.
1214 * https://github.com/ARMmbed/mbed-crypto/issues/216
1215 */
Ronald Cron9b8b69c2021-08-24 16:00:51 +02001216 if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02001217 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001218 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001219
Ronald Cron90857082020-11-25 14:58:33 +01001220 status = mbedtls_psa_rsa_load_representation(
1221 slot->attr.type,
1222 slot->key.data,
1223 slot->key.bytes,
1224 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001225 if( status != PSA_SUCCESS )
1226 break;
1227
Steven Cooremana2371e52020-07-28 14:30:39 +02001228 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001229 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001230 mbedtls_rsa_free( rsa );
1231 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001232 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001233 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001234#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001235 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001236 default:
1237 /* Nothing else to do. */
1238 break;
1239 }
1240
1241 if( status != PSA_SUCCESS )
1242 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001243
Ronald Cron5c522922020-11-14 16:35:34 +01001244 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001245
Ronald Cron5c522922020-11-14 16:35:34 +01001246 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001247}
1248
Gilles Peskinec8000c02019-08-02 20:15:51 +02001249#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1250psa_status_t psa_get_key_slot_number(
1251 const psa_key_attributes_t *attributes,
1252 psa_key_slot_number_t *slot_number )
1253{
1254 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1255 {
1256 *slot_number = attributes->slot_number;
1257 return( PSA_SUCCESS );
1258 }
1259 else
1260 return( PSA_ERROR_INVALID_ARGUMENT );
1261}
1262#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1263
Ronald Crond18b5f82020-11-25 16:46:05 +01001264static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1265 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001266 uint8_t *data,
1267 size_t data_size,
1268 size_t *data_length )
1269{
Ronald Crond18b5f82020-11-25 16:46:05 +01001270 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001271 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001272 memcpy( data, key_buffer, key_buffer_size );
1273 memset( data + key_buffer_size, 0,
1274 data_size - key_buffer_size );
1275 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001276 return( PSA_SUCCESS );
1277}
1278
Ronald Cron7285cda2020-11-26 14:46:37 +01001279psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001280 const psa_key_attributes_t *attributes,
1281 const uint8_t *key_buffer, size_t key_buffer_size,
1282 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001283{
Ronald Crond18b5f82020-11-25 16:46:05 +01001284 psa_key_type_t type = attributes->core.type;
1285
Ronald Crond18b5f82020-11-25 16:46:05 +01001286 if( key_type_is_raw_bytes( type ) ||
1287 PSA_KEY_TYPE_IS_RSA( type ) ||
1288 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001289 {
1290 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001291 key_buffer, key_buffer_size,
1292 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001293 }
1294 else
1295 {
1296 /* This shouldn't happen in the reference implementation, but
1297 it is valid for a special-purpose implementation to omit
1298 support for exporting certain key types. */
1299 return( PSA_ERROR_NOT_SUPPORTED );
1300 }
1301}
1302
1303psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1304 uint8_t *data,
1305 size_t data_size,
1306 size_t *data_length )
1307{
1308 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1309 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1310 psa_key_slot_t *slot;
1311
Ronald Crone907e552021-01-18 13:22:38 +01001312 /* Reject a zero-length output buffer now, since this can never be a
1313 * valid key representation. This way we know that data must be a valid
1314 * pointer and we can do things like memset(data, ..., data_size). */
1315 if( data_size == 0 )
1316 return( PSA_ERROR_BUFFER_TOO_SMALL );
1317
Ronald Cron9486f9d2020-11-26 13:36:23 +01001318 /* Set the key to empty now, so that even when there are errors, we always
1319 * set data_length to a value between 0 and data_size. On error, setting
1320 * the key to empty is a good choice because an empty key representation is
1321 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001322 *data_length = 0;
1323
Ronald Cron9486f9d2020-11-26 13:36:23 +01001324 /* Export requires the EXPORT flag. There is an exception for public keys,
1325 * which don't require any flag, but
1326 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1327 */
1328 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1329 PSA_KEY_USAGE_EXPORT, 0 );
1330 if( status != PSA_SUCCESS )
1331 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001332
Ronald Crond18b5f82020-11-25 16:46:05 +01001333 psa_key_attributes_t attributes = {
1334 .core = slot->attr
1335 };
Ronald Cron67227982020-11-26 15:16:05 +01001336 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001337 slot->key.data, slot->key.bytes,
1338 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001339
Ronald Cron9486f9d2020-11-26 13:36:23 +01001340 unlock_status = psa_unlock_key_slot( slot );
1341
1342 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1343}
1344
Ronald Cron7285cda2020-11-26 14:46:37 +01001345psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001346 const psa_key_attributes_t *attributes,
1347 const uint8_t *key_buffer,
1348 size_t key_buffer_size,
1349 uint8_t *data,
1350 size_t data_size,
1351 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001352{
Ronald Crond18b5f82020-11-25 16:46:05 +01001353 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001354
Ronald Crond18b5f82020-11-25 16:46:05 +01001355 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001356 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001357 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001358 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001359 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001360 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001361 key_buffer, key_buffer_size,
1362 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001363 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001364
Ronald Crond18b5f82020-11-25 16:46:05 +01001365 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001366 {
John Durkop0e005192020-10-31 22:06:54 -07001367#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1368 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001369 return( mbedtls_psa_rsa_export_public_key( attributes,
1370 key_buffer,
1371 key_buffer_size,
1372 data,
1373 data_size,
1374 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001375#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001376 /* We don't know how to convert a private RSA key to public. */
1377 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001378#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1379 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001380 }
1381 else
Moran Pekera998bc62018-04-16 18:16:20 +03001382 {
John Durkop6ba40d12020-11-10 08:50:04 -08001383#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1384 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001385 return( mbedtls_psa_ecp_export_public_key( attributes,
1386 key_buffer,
1387 key_buffer_size,
1388 data,
1389 data_size,
1390 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001391#else
1392 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001393 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001394#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1395 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001396 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001397 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001398 else
1399 {
1400 /* This shouldn't happen in the reference implementation, but
1401 it is valid for a special-purpose implementation to omit
1402 support for exporting certain key types. */
1403 return( PSA_ERROR_NOT_SUPPORTED );
1404 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001405}
Ronald Crond18b5f82020-11-25 16:46:05 +01001406
Ronald Croncf56a0a2020-08-04 09:51:30 +02001407psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001408 uint8_t *data,
1409 size_t data_size,
1410 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001411{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001412 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001413 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001414 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001415
Ronald Crone907e552021-01-18 13:22:38 +01001416 /* Reject a zero-length output buffer now, since this can never be a
1417 * valid key representation. This way we know that data must be a valid
1418 * pointer and we can do things like memset(data, ..., data_size). */
1419 if( data_size == 0 )
1420 return( PSA_ERROR_BUFFER_TOO_SMALL );
1421
Darryl Greendd8fb772018-11-07 16:00:44 +00001422 /* Set the key to empty now, so that even when there are errors, we always
1423 * set data_length to a value between 0 and data_size. On error, setting
1424 * the key to empty is a good choice because an empty key representation is
1425 * unlikely to be accepted anywhere. */
1426 *data_length = 0;
1427
1428 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001429 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001430 if( status != PSA_SUCCESS )
1431 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001432
Ronald Cron9486f9d2020-11-26 13:36:23 +01001433 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1434 {
1435 status = PSA_ERROR_INVALID_ARGUMENT;
1436 goto exit;
1437 }
1438
Ronald Crond18b5f82020-11-25 16:46:05 +01001439 psa_key_attributes_t attributes = {
1440 .core = slot->attr
1441 };
Ronald Cron67227982020-11-26 15:16:05 +01001442 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001443 &attributes, slot->key.data, slot->key.bytes,
1444 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001445
1446exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001447 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001448
Ronald Cron5c522922020-11-14 16:35:34 +01001449 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001450}
1451
Gilles Peskine91e8c332019-08-02 19:19:39 +02001452#if defined(static_assert)
1453static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1454 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001455static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001456 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001457static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001458 "One or more key attribute flag is listed as both internal-only and external-only" );
1459#endif
1460
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001461/** Validate that a key policy is internally well-formed.
1462 *
1463 * This function only rejects invalid policies. It does not validate the
1464 * consistency of the policy with respect to other attributes of the key
1465 * such as the key type.
1466 */
1467static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001468{
1469 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001470 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001471 PSA_KEY_USAGE_ENCRYPT |
1472 PSA_KEY_USAGE_DECRYPT |
gabor-mezei-arm4a210192021-04-14 21:14:28 +02001473 PSA_KEY_USAGE_SIGN_MESSAGE |
1474 PSA_KEY_USAGE_VERIFY_MESSAGE |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001475 PSA_KEY_USAGE_SIGN_HASH |
1476 PSA_KEY_USAGE_VERIFY_HASH |
Manuel Pégourié-Gonnard805251b2021-05-04 09:49:59 +02001477 PSA_KEY_USAGE_VERIFY_DERIVATION |
Gilles Peskine4747d192019-04-17 15:05:45 +02001478 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1479 return( PSA_ERROR_INVALID_ARGUMENT );
1480
Gilles Peskine4747d192019-04-17 15:05:45 +02001481 return( PSA_SUCCESS );
1482}
1483
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001484/** Validate the internal consistency of key attributes.
1485 *
1486 * This function only rejects invalid attribute values. If does not
1487 * validate the consistency of the attributes with any key data that may
1488 * be involved in the creation of the key.
1489 *
1490 * Call this function early in the key creation process.
1491 *
1492 * \param[in] attributes Key attributes for the new key.
1493 * \param[out] p_drv On any return, the driver for the key, if any.
1494 * NULL for a transparent key.
1495 *
1496 */
1497static psa_status_t psa_validate_key_attributes(
1498 const psa_key_attributes_t *attributes,
1499 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001500{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001501 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001502 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001503 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001504
Ronald Cron54b90082020-10-29 15:26:43 +01001505 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001506 if( status != PSA_SUCCESS )
1507 return( status );
1508
Ronald Crond2ed4812020-07-17 16:11:30 +02001509 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001510 if( status != PSA_SUCCESS )
1511 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001512
Ronald Cron65f38a32020-10-23 17:11:13 +02001513 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1514 {
1515 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1516 return( PSA_ERROR_INVALID_ARGUMENT );
1517 }
1518 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001519 {
Ronald Cron77e412c2021-03-31 17:36:31 +02001520 if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) )
1521 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Crond2ed4812020-07-17 16:11:30 +02001522 }
1523
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001524 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001525 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001526 return( status );
1527
1528 /* Refuse to create overly large keys.
1529 * Note that this doesn't trigger on import if the attributes don't
1530 * explicitly specify a size (so psa_get_key_bits returns 0), so
1531 * psa_import_key() needs its own checks. */
1532 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1533 return( PSA_ERROR_NOT_SUPPORTED );
1534
Gilles Peskine91e8c332019-08-02 19:19:39 +02001535 /* Reject invalid flags. These should not be reachable through the API. */
1536 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1537 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1538 return( PSA_ERROR_INVALID_ARGUMENT );
1539
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001540 return( PSA_SUCCESS );
1541}
1542
Gilles Peskine4747d192019-04-17 15:05:45 +02001543/** Prepare a key slot to receive key material.
1544 *
1545 * This function allocates a key slot and sets its metadata.
1546 *
1547 * If this function fails, call psa_fail_key_creation().
1548 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001549 * This function is intended to be used as follows:
1550 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001551 * it with the specified attributes, and in case of a volatile key assign it
1552 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001553 * -# Populate the slot with the key material.
1554 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1555 * In case of failure at any step, stop the sequence and call
1556 * psa_fail_key_creation().
1557 *
Ronald Cron5c522922020-11-14 16:35:34 +01001558 * On success, the key slot is locked. It is the responsibility of the caller
1559 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001560 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001561 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001562 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001563 * \param[out] p_slot On success, a pointer to the prepared slot.
1564 * \param[out] p_drv On any return, the driver for the key, if any.
1565 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001566 *
1567 * \retval #PSA_SUCCESS
1568 * The key slot is ready to receive key material.
1569 * \return If this function fails, the key slot is an invalid state.
1570 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001571 */
1572static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001573 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001574 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001575 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001576 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001577{
1578 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001579 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001580 psa_key_slot_t *slot;
1581
Gilles Peskinedf179142019-07-15 22:02:14 +02001582 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001583 *p_drv = NULL;
1584
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001585 status = psa_validate_key_attributes( attributes, p_drv );
1586 if( status != PSA_SUCCESS )
1587 return( status );
1588
Ronald Cronc4d1b512020-07-31 11:26:37 +02001589 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001590 if( status != PSA_SUCCESS )
1591 return( status );
1592 slot = *p_slot;
1593
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001594 /* We're storing the declared bit-size of the key. It's up to each
1595 * creation mechanism to verify that this information is correct.
1596 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001597 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001598 * is optional (import, copy). In case of a volatile key, assign it the
1599 * volatile key identifier associated to the slot returned to contain its
1600 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001601
1602 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001603 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1604 {
1605#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1606 slot->attr.id = volatile_key_id;
1607#else
1608 slot->attr.id.key_id = volatile_key_id;
1609#endif
1610 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001611
Gilles Peskine91e8c332019-08-02 19:19:39 +02001612 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001613 * external-only flags, query `attributes`. Thanks to the check
1614 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001615 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001616 * may have set. */
1617 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001618
Gilles Peskinecbaff462019-07-12 23:46:04 +02001619#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001620 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001621 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001622 * create the key file in internal storage, create the
1623 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001624 * persistent data. This is done by starting a transaction that will
1625 * encompass these three actions.
1626 * For registering a volatile key, we just need to find an appropriate
1627 * slot number inside the SE. Since the key is designated volatile, creating
1628 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001629 /* The first thing to do is to find a slot number for the new key.
1630 * We save the slot number in persistent storage as part of the
1631 * transaction data. It will be needed to recover if the power
1632 * fails during the key creation process, to clean up on the secure
1633 * element side after restarting. Obtaining a slot number from the
1634 * secure element driver updates its persistent state, but we do not yet
1635 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001636 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001637 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001638 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001639 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001640 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001641 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001642 if( status != PSA_SUCCESS )
1643 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001644
1645 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001646 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001647 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1648 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001649 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001650 psa_crypto_transaction.key.id = slot->attr.id;
1651 status = psa_crypto_save_transaction( );
1652 if( status != PSA_SUCCESS )
1653 {
1654 (void) psa_crypto_stop_transaction( );
1655 return( status );
1656 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001657 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001658
1659 status = psa_copy_key_material_into_slot(
1660 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001661 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001662
1663 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1664 {
1665 /* Key registration only makes sense with a secure element. */
1666 return( PSA_ERROR_INVALID_ARGUMENT );
1667 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001668#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1669
Ronald Cronc4d1b512020-07-31 11:26:37 +02001670 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001671}
Gilles Peskine4747d192019-04-17 15:05:45 +02001672
1673/** Finalize the creation of a key once its key material has been set.
1674 *
1675 * This entails writing the key to persistent storage.
1676 *
1677 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001678 * See the documentation of psa_start_key_creation() for the intended use
1679 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001680 *
Ronald Cron5c522922020-11-14 16:35:34 +01001681 * If the finalization succeeds, the function unlocks the key slot (it was
1682 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1683 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001684 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001685 * \param[in,out] slot Pointer to the slot with key material.
1686 * \param[in] driver The secure element driver for the key,
1687 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001688 * \param[out] key On success, identifier of the key. Note that the
1689 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001690 *
1691 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001692 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001693 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1694 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1695 * \retval #PSA_ERROR_ALREADY_EXISTS
1696 * \retval #PSA_ERROR_DATA_INVALID
1697 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001698 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001699 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001700 * \return If this function fails, the key slot is an invalid state.
1701 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001702 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001703static psa_status_t psa_finish_key_creation(
1704 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001705 psa_se_drv_table_entry_t *driver,
1706 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001707{
1708 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001709 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001710 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001711
1712#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001713 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001714 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001715#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1716 if( driver != NULL )
1717 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001718 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001719 psa_key_slot_number_t slot_number =
1720 psa_key_slot_get_slot_number( slot ) ;
1721
Gilles Peskineb46bef22019-07-30 21:32:04 +02001722#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001723 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001724 sizeof( data.slot_number ),
1725 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001726#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001727 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001728 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001729 (uint8_t*) &data,
1730 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001731 }
1732 else
1733#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1734 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001735 /* Key material is saved in export representation in the slot, so
1736 * just pass the slot buffer for storage. */
1737 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001738 slot->key.data,
1739 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001740 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001741 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001742#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1743
Gilles Peskinecbaff462019-07-12 23:46:04 +02001744#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001745 /* Finish the transaction for a key creation. This does not
1746 * happen when registering an existing key. Detect this case
1747 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001748 * creation of a persistent key in a secure element requires a transaction,
1749 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001750 if( driver != NULL &&
1751 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001752 {
1753 status = psa_save_se_persistent_data( driver );
1754 if( status != PSA_SUCCESS )
1755 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001756 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001757 return( status );
1758 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001759 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001760 }
1761#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1762
Ronald Cron50972942020-11-14 11:28:25 +01001763 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001764 {
1765 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001766 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001767 if( status != PSA_SUCCESS )
1768 *key = MBEDTLS_SVC_KEY_ID_INIT;
1769 }
Ronald Cron50972942020-11-14 11:28:25 +01001770
Gilles Peskine4747d192019-04-17 15:05:45 +02001771 return( status );
1772}
1773
1774/** Abort the creation of a key.
1775 *
1776 * You may call this function after calling psa_start_key_creation(),
1777 * or after psa_finish_key_creation() fails. In other circumstances, this
1778 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001779 * See the documentation of psa_start_key_creation() for the intended use
1780 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001781 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001782 * \param[in,out] slot Pointer to the slot with key material.
1783 * \param[in] driver The secure element driver for the key,
1784 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001785 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001786static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001787 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001788{
Gilles Peskine011e4282019-06-26 18:34:38 +02001789 (void) driver;
1790
Gilles Peskine4747d192019-04-17 15:05:45 +02001791 if( slot == NULL )
1792 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001793
1794#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001795 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001796 * element, and the failure happened later (when saving metadata
1797 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001798 * element.
1799 * https://github.com/ARMmbed/mbed-crypto/issues/217
1800 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001801
Gilles Peskined7729582019-08-05 15:55:54 +02001802 /* Abort the ongoing transaction if any (there may not be one if
1803 * the creation process failed before starting one, or if the
1804 * key creation is a registration of a key in a secure element).
1805 * Earlier functions must already have done what it takes to undo any
1806 * partial creation. All that's left is to update the transaction data
1807 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001808 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001809#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1810
Gilles Peskine4747d192019-04-17 15:05:45 +02001811 psa_wipe_key_slot( slot );
1812}
1813
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001814/** Validate optional attributes during key creation.
1815 *
1816 * Some key attributes are optional during key creation. If they are
1817 * specified in the attributes structure, check that they are consistent
1818 * with the data in the slot.
1819 *
1820 * This function should be called near the end of key creation, after
1821 * the slot in memory is fully populated but before saving persistent data.
1822 */
1823static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001824 const psa_key_slot_t *slot,
1825 const psa_key_attributes_t *attributes )
1826{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001827 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001828 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001829 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001830 return( PSA_ERROR_INVALID_ARGUMENT );
1831 }
1832
1833 if( attributes->domain_parameters_size != 0 )
1834 {
John Durkop0e005192020-10-31 22:06:54 -07001835#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1836 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001837 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001838 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001839 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001840 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001842
Ronald Cron90857082020-11-25 14:58:33 +01001843 psa_status_t status = mbedtls_psa_rsa_load_representation(
1844 slot->attr.type,
1845 slot->key.data,
1846 slot->key.bytes,
1847 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001848 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001849 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001850
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851 mbedtls_mpi_init( &actual );
1852 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001853 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001854 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001855 mbedtls_rsa_free( rsa );
1856 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001857 if( ret != 0 )
1858 goto rsa_exit;
1859 ret = mbedtls_mpi_read_binary( &required,
1860 attributes->domain_parameters,
1861 attributes->domain_parameters_size );
1862 if( ret != 0 )
1863 goto rsa_exit;
1864 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1865 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1866 rsa_exit:
1867 mbedtls_mpi_free( &actual );
1868 mbedtls_mpi_free( &required );
1869 if( ret != 0)
1870 return( mbedtls_to_psa_error( ret ) );
1871 }
1872 else
John Durkop9814fa22020-11-04 12:28:15 -08001873#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1874 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001875 {
1876 return( PSA_ERROR_INVALID_ARGUMENT );
1877 }
1878 }
1879
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001880 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001881 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001882 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001883 return( PSA_ERROR_INVALID_ARGUMENT );
1884 }
1885
1886 return( PSA_SUCCESS );
1887}
1888
Gilles Peskine4747d192019-04-17 15:05:45 +02001889psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001890 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001891 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001892 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001893{
1894 psa_status_t status;
1895 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001896 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001897 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301898 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899
Ronald Cron81709fc2020-11-14 12:10:32 +01001900 *key = MBEDTLS_SVC_KEY_ID_INIT;
1901
Gilles Peskine0f84d622019-09-12 19:03:13 +02001902 /* Reject zero-length symmetric keys (including raw data key objects).
1903 * This also rejects any key which might be encoded as an empty string,
1904 * which is never valid. */
1905 if( data_length == 0 )
1906 return( PSA_ERROR_INVALID_ARGUMENT );
1907
Archana449608b2021-09-08 15:36:05 +05301908 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Archana6ed4bda2021-08-04 10:47:15 +05301909 if( data_length > SIZE_MAX / 8 )
1910 return( PSA_ERROR_NOT_SUPPORTED );
1911
Gilles Peskinedf179142019-07-15 22:02:14 +02001912 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001913 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001914 if( status != PSA_SUCCESS )
1915 goto exit;
1916
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001917 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301918 * storage ( thus not in the case of importing a key in a secure element
1919 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1920 * buffer to hold the imported key material. */
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001921 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001922 {
Archanad8a83dc2021-06-14 10:04:16 +05301923 if( psa_key_lifetime_is_external( attributes->core.lifetime ) )
1924 {
Archana449608b2021-09-08 15:36:05 +05301925 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
1926 attributes, data, data_length, &storage_size );
Archanad8a83dc2021-06-14 10:04:16 +05301927 if( status != PSA_SUCCESS )
1928 goto exit;
1929 }
1930 status = psa_allocate_buffer_to_slot( slot, storage_size );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001931 if( status != PSA_SUCCESS )
1932 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001933 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001934
1935 bits = slot->attr.bits;
1936 status = psa_driver_wrapper_import_key( attributes,
1937 data, data_length,
1938 slot->key.data,
1939 slot->key.bytes,
1940 &slot->key.bytes, &bits );
1941 if( status != PSA_SUCCESS )
1942 goto exit;
1943
1944 if( slot->attr.bits == 0 )
1945 slot->attr.bits = (psa_key_bits_t) bits;
1946 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001947 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001948 status = PSA_ERROR_INVALID_ARGUMENT;
1949 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001950 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001951
Archana6ed4bda2021-08-04 10:47:15 +05301952 /* Enforce a size limit, and in particular ensure that the bit
1953 * size fits in its representation type.*/
1954 if( bits > PSA_MAX_KEY_BITS )
1955 {
1956 status = PSA_ERROR_NOT_SUPPORTED;
1957 goto exit;
1958 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001959 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001960 if( status != PSA_SUCCESS )
1961 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001962
Ronald Cron81709fc2020-11-14 12:10:32 +01001963 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001964exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001965 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001966 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001967
Gilles Peskine4747d192019-04-17 15:05:45 +02001968 return( status );
1969}
1970
Gilles Peskined7729582019-08-05 15:55:54 +02001971#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1972psa_status_t mbedtls_psa_register_se_key(
1973 const psa_key_attributes_t *attributes )
1974{
1975 psa_status_t status;
1976 psa_key_slot_t *slot = NULL;
1977 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001978 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001979
1980 /* Leaving attributes unspecified is not currently supported.
1981 * It could make sense to query the key type and size from the
1982 * secure element, but not all secure elements support this
1983 * and the driver HAL doesn't currently support it. */
1984 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1985 return( PSA_ERROR_NOT_SUPPORTED );
1986 if( psa_get_key_bits( attributes ) == 0 )
1987 return( PSA_ERROR_NOT_SUPPORTED );
1988
1989 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001990 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02001991 if( status != PSA_SUCCESS )
1992 goto exit;
1993
Ronald Cron81709fc2020-11-14 12:10:32 +01001994 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02001995
1996exit:
1997 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02001998 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001999
Gilles Peskined7729582019-08-05 15:55:54 +02002000 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002001 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002002 return( status );
2003}
2004#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2005
Ronald Croncf56a0a2020-08-04 09:51:30 +02002006psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002007 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002008 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002009{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002011 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002012 psa_key_slot_t *source_slot = NULL;
2013 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002014 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002015 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302016 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002017
Ronald Cron81709fc2020-11-14 12:10:32 +01002018 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2019
Archana8a180362021-07-05 02:18:48 +05302020 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01002021 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002022 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002023 goto exit;
2024
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002025 status = psa_validate_optional_attributes( source_slot,
2026 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002027 if( status != PSA_SUCCESS )
2028 goto exit;
2029
Archana9d17bf42021-09-10 06:22:44 +05302030 /* The target key type and number of bits have been validated by
2031 * psa_validate_optional_attributes() to be either equal to zero or
2032 * equal to the ones of the source key. So it is safe to inherit
2033 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302034 * */
Archana9d17bf42021-09-10 06:22:44 +05302035 actual_attributes.core.bits = source_slot->attr.bits;
2036 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302037
2038
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002039 status = psa_restrict_key_policy( source_slot->attr.type,
2040 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002041 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002042 if( status != PSA_SUCCESS )
2043 goto exit;
2044
Ronald Cron81709fc2020-11-14 12:10:32 +01002045 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2046 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002047 if( status != PSA_SUCCESS )
2048 goto exit;
Archana8a180362021-07-05 02:18:48 +05302049 if( PSA_KEY_LIFETIME_GET_LOCATION( target_slot->attr.lifetime ) !=
2050 PSA_KEY_LIFETIME_GET_LOCATION( source_slot->attr.lifetime ) )
Gilles Peskinef603c712019-01-19 13:40:11 +01002051 {
Archana8a180362021-07-05 02:18:48 +05302052 /*
Archana9d17bf42021-09-10 06:22:44 +05302053 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302054 * the source key would need to be exported as plaintext and re-imported
2055 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302056 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302057 * appropriate API invocations from the application, if needed.
2058 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002059 status = PSA_ERROR_NOT_SUPPORTED;
2060 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002061 }
Archana8a180362021-07-05 02:18:48 +05302062 /*
2063 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302064 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302065 * - For opaque keys this translates to an invocation of the drivers'
2066 * copy_key entry point through the dispatch layer.
2067 * */
Ronald Cron6cc66312021-04-02 12:27:47 +02002068 if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) )
2069 {
Archana8a180362021-07-05 02:18:48 +05302070 status = psa_driver_wrapper_get_key_buffer_size( &actual_attributes,
Archana449608b2021-09-08 15:36:05 +05302071 &storage_size );
Archana8a180362021-07-05 02:18:48 +05302072 if( status != PSA_SUCCESS )
2073 goto exit;
Archana374fe5b2021-09-08 15:50:28 +05302074
Archana8a180362021-07-05 02:18:48 +05302075 status = psa_allocate_buffer_to_slot( target_slot, storage_size );
2076 if( status != PSA_SUCCESS )
2077 goto exit;
Archana374fe5b2021-09-08 15:50:28 +05302078
Archana8a180362021-07-05 02:18:48 +05302079 status = psa_driver_wrapper_copy_key( &actual_attributes,
2080 source_slot->key.data,
2081 source_slot->key.bytes,
2082 target_slot->key.data,
2083 target_slot->key.bytes,
2084 &target_slot->key.bytes );
2085 if( status != PSA_SUCCESS )
2086 goto exit;
Ronald Cron6cc66312021-04-02 12:27:47 +02002087 }
Archana8a180362021-07-05 02:18:48 +05302088 else
2089 {
Archana9d17bf42021-09-10 06:22:44 +05302090 status = psa_copy_key_material_into_slot( target_slot,
2091 source_slot->key.data,
2092 source_slot->key.bytes );
Archana8a180362021-07-05 02:18:48 +05302093 if( status != PSA_SUCCESS )
2094 goto exit;
2095 }
Ronald Cron81709fc2020-11-14 12:10:32 +01002096 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002097exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002098 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002099 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002100
Ronald Cron5c522922020-11-14 16:35:34 +01002101 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002102
Ronald Cron5c522922020-11-14 16:35:34 +01002103 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002104}
2105
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002106
2107
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002108/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002109/* Message digests */
2110/****************************************************************/
2111
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002112psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2113{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002114 /* Aborting a non-active operation is allowed */
2115 if( operation->id == 0 )
2116 return( PSA_SUCCESS );
2117
2118 psa_status_t status = psa_driver_wrapper_hash_abort( operation );
2119 operation->id = 0;
2120
2121 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002122}
2123
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002124psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002125 psa_algorithm_t alg )
2126{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002127 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002128
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002129 /* A context must be freshly initialized before it can be set up. */
2130 if( operation->id != 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002131 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002132 status = PSA_ERROR_BAD_STATE;
2133 goto exit;
2134 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002135
2136 if( !PSA_ALG_IS_HASH( alg ) )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002137 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002138 status = PSA_ERROR_INVALID_ARGUMENT;
2139 goto exit;
2140 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002141
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002142 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2143 * directly zeroes the int-sized dummy member of the context union. */
Steven Cooreman893232f2021-03-15 12:23:37 +01002144 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
2145
Dave Rodgman685b6a72021-06-24 11:49:14 +01002146 status = psa_driver_wrapper_hash_setup( operation, alg );
2147
2148exit:
2149 if( status != PSA_SUCCESS )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002150 psa_hash_abort( operation );
Dave Rodgman685b6a72021-06-24 11:49:14 +01002151
2152 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002153}
2154
2155psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2156 const uint8_t *input,
2157 size_t input_length )
2158{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002159 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2160
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002161 if( operation->id == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002162 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002163 status = PSA_ERROR_BAD_STATE;
2164 goto exit;
2165 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002166
Steven Cooremanc8288352021-03-04 14:02:19 +01002167 /* Don't require hash implementations to behave correctly on a
2168 * zero-length input, which may have an invalid pointer. */
2169 if( input_length == 0 )
2170 return( PSA_SUCCESS );
2171
Dave Rodgman685b6a72021-06-24 11:49:14 +01002172 status = psa_driver_wrapper_hash_update( operation, input, input_length );
2173
2174exit:
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002175 if( status != PSA_SUCCESS )
2176 psa_hash_abort( operation );
2177
2178 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002179}
2180
2181psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2182 uint8_t *hash,
2183 size_t hash_size,
2184 size_t *hash_length )
2185{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002186 *hash_length = 0;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002187 if( operation->id == 0 )
2188 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189
Steven Cooreman1e582352021-02-18 17:24:37 +01002190 psa_status_t status = psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002191 operation, hash, hash_size, hash_length );
Steven Cooreman0e307642021-02-18 16:18:32 +01002192 psa_hash_abort( operation );
2193 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194}
2195
Gilles Peskine2d277862018-06-18 15:41:12 +02002196psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2197 const uint8_t *hash,
2198 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002199{
Ronald Cron69a63422021-10-18 09:47:58 +02002200 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002201 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002202 psa_status_t status = psa_hash_finish(
2203 operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01002204 actual_hash, sizeof( actual_hash ),
2205 &actual_hash_length );
Dave Rodgman685b6a72021-06-24 11:49:14 +01002206
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002207 if( status != PSA_SUCCESS )
Dave Rodgman685b6a72021-06-24 11:49:14 +01002208 goto exit;
2209
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002210 if( actual_hash_length != hash_length )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002211 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002212 status = PSA_ERROR_INVALID_SIGNATURE;
2213 goto exit;
2214 }
2215
Steven Cooreman36876a02021-04-29 16:37:59 +02002216 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Dave Rodgman685b6a72021-06-24 11:49:14 +01002217 status = PSA_ERROR_INVALID_SIGNATURE;
2218
2219exit:
Gilles Peskine60aebec2021-12-13 12:33:18 +01002220 mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) );
Dave Rodgman685b6a72021-06-24 11:49:14 +01002221 if( status != PSA_SUCCESS )
2222 psa_hash_abort(operation);
2223
2224 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002225}
2226
Gilles Peskine0a749c82019-11-28 19:33:58 +01002227psa_status_t psa_hash_compute( psa_algorithm_t alg,
2228 const uint8_t *input, size_t input_length,
2229 uint8_t *hash, size_t hash_size,
2230 size_t *hash_length )
2231{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002232 *hash_length = 0;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002233 if( !PSA_ALG_IS_HASH( alg ) )
2234 return( PSA_ERROR_INVALID_ARGUMENT );
2235
Steven Cooreman1e582352021-02-18 17:24:37 +01002236 return( psa_driver_wrapper_hash_compute( alg, input, input_length,
2237 hash, hash_size, hash_length ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002238}
2239
2240psa_status_t psa_hash_compare( psa_algorithm_t alg,
2241 const uint8_t *input, size_t input_length,
2242 const uint8_t *hash, size_t hash_length )
2243{
Ronald Cron69a63422021-10-18 09:47:58 +02002244 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002245 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002246
2247 if( !PSA_ALG_IS_HASH( alg ) )
2248 return( PSA_ERROR_INVALID_ARGUMENT );
2249
Steven Cooreman1e582352021-02-18 17:24:37 +01002250 psa_status_t status = psa_driver_wrapper_hash_compute(
2251 alg, input, input_length,
2252 actual_hash, sizeof(actual_hash),
2253 &actual_hash_length );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002254 if( status != PSA_SUCCESS )
Gilles Peskine60aebec2021-12-13 12:33:18 +01002255 goto exit;
Steven Cooreman84d670d2021-02-18 16:22:53 +01002256 if( actual_hash_length != hash_length )
Gilles Peskine60aebec2021-12-13 12:33:18 +01002257 {
2258 status = PSA_ERROR_INVALID_SIGNATURE;
2259 goto exit;
2260 }
Steven Cooreman36876a02021-04-29 16:37:59 +02002261 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Gilles Peskine60aebec2021-12-13 12:33:18 +01002262 status = PSA_ERROR_INVALID_SIGNATURE;
2263
2264exit:
2265 mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) );
2266 return( status );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002267}
2268
Gilles Peskineeb35d782019-01-22 17:56:16 +01002269psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2270 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002271{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002272 if( source_operation->id == 0 ||
2273 target_operation->id != 0 )
2274 {
2275 return( PSA_ERROR_BAD_STATE );
2276 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002277
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002278 psa_status_t status = psa_driver_wrapper_hash_clone( source_operation,
2279 target_operation );
2280 if( status != PSA_SUCCESS )
2281 psa_hash_abort( target_operation );
2282
2283 return( status );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002284}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002285
2286
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002287/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002288/* MAC */
2289/****************************************************************/
2290
Steven Cooremane6804192021-03-19 18:28:56 +01002291psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002292{
Steven Cooremane6804192021-03-19 18:28:56 +01002293 /* Aborting a non-active operation is allowed */
2294 if( operation->id == 0 )
2295 return( PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002296
Steven Cooremane6804192021-03-19 18:28:56 +01002297 psa_status_t status = psa_driver_wrapper_mac_abort( operation );
Steven Cooreman72f736a2021-05-07 14:14:37 +02002298 operation->mac_size = 0;
2299 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002300 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002301
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002302 return( status );
2303}
2304
Ronald Cron2dff3b22021-06-17 16:33:22 +02002305static psa_status_t psa_mac_finalize_alg_and_key_validation(
2306 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002307 const psa_key_attributes_t *attributes,
2308 uint8_t *mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002309{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002310 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron79bdd822021-06-17 16:46:44 +02002311 psa_key_type_t key_type = psa_get_key_type( attributes );
2312 size_t key_bits = psa_get_key_bits( attributes );
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002313
Ronald Cron28ea0502021-06-17 16:10:24 +02002314 if( ! PSA_ALG_IS_MAC( alg ) )
Ronald Cron79bdd822021-06-17 16:46:44 +02002315 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremane6804192021-03-19 18:28:56 +01002316
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002317 /* Validate the combination of key type and algorithm */
Ronald Cron79bdd822021-06-17 16:46:44 +02002318 status = psa_mac_key_can_do( alg, key_type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002319 if( status != PSA_SUCCESS )
Ronald Cron79bdd822021-06-17 16:46:44 +02002320 return( status );
Steven Cooreman15472f82021-03-02 16:16:22 +01002321
Steven Cooremand1a68f12021-05-10 11:13:41 +02002322 /* Get the output length for the algorithm and key combination */
Ronald Cron79bdd822021-06-17 16:46:44 +02002323 *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002324
Ronald Cron79bdd822021-06-17 16:46:44 +02002325 if( *mac_size < 4 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002326 {
2327 /* A very short MAC is too short for security since it can be
2328 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2329 * so we make this our minimum, even though 32 bits is still
2330 * too small for security. */
Ronald Cron79bdd822021-06-17 16:46:44 +02002331 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman15472f82021-03-02 16:16:22 +01002332 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002333
Ronald Cron79bdd822021-06-17 16:46:44 +02002334 if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits,
2335 PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002336 {
2337 /* It's impossible to "truncate" to a larger length than the full length
2338 * of the algorithm. */
Ronald Cron79bdd822021-06-17 16:46:44 +02002339 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002340 }
2341
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002342 if( *mac_size > PSA_MAC_MAX_SIZE )
2343 {
2344 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2345 * that is disabled in the compile-time configuration. The result can
2346 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2347 * configuration into account. In this case, force a return of
2348 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2349 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2350 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2351 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2352 * systematically generated tests. */
2353 return( PSA_ERROR_NOT_SUPPORTED );
2354 }
2355
Ronald Cron79bdd822021-06-17 16:46:44 +02002356 return( PSA_SUCCESS );
Ronald Cron2dff3b22021-06-17 16:33:22 +02002357}
2358
Gilles Peskinee59236f2018-01-27 23:32:46 +01002359static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
2360 mbedtls_svc_key_id_t key,
2361 psa_algorithm_t alg,
2362 int is_sign )
2363{
2364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2365 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002366 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002367
2368 /* A context must be freshly initialized before it can be set up. */
2369 if( operation->id != 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002370 {
Dave Rodgman54648242021-06-24 11:49:45 +01002371 status = PSA_ERROR_BAD_STATE;
2372 goto exit;
2373 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374
2375 status = psa_get_and_lock_key_slot_with_policy(
2376 key,
2377 &slot,
Mateusz Starzykce0e6a92021-08-17 15:24:32 +02002378 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Gilles Peskine8c9def32018-02-08 10:02:12 +01002379 alg );
2380 if( status != PSA_SUCCESS )
Dave Rodgman54648242021-06-24 11:49:45 +01002381 goto exit;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002382
2383 psa_key_attributes_t attributes = {
2384 .core = slot->attr
2385 };
2386
Ronald Cron79bdd822021-06-17 16:46:44 +02002387 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2388 &operation->mac_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002389 if( status != PSA_SUCCESS )
2390 goto exit;
2391
2392 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002393 /* Dispatch the MAC setup call with validated input */
2394 if( is_sign )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002395 {
Steven Cooremane6804192021-03-19 18:28:56 +01002396 status = psa_driver_wrapper_mac_sign_setup( operation,
2397 &attributes,
2398 slot->key.data,
2399 slot->key.bytes,
2400 alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002401 }
2402 else
Gilles Peskinefbfac682018-07-08 20:51:54 +02002403 {
Steven Cooremane6804192021-03-19 18:28:56 +01002404 status = psa_driver_wrapper_mac_verify_setup( operation,
2405 &attributes,
2406 slot->key.data,
2407 slot->key.bytes,
2408 alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002409 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002410
Gilles Peskinefbfac682018-07-08 20:51:54 +02002411exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002412 if( status != PSA_SUCCESS )
Steven Cooremane6804192021-03-19 18:28:56 +01002413 psa_mac_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002414
Ronald Cron5c522922020-11-14 16:35:34 +01002415 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002416
Ronald Cron5c522922020-11-14 16:35:34 +01002417 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418}
2419
Gilles Peskine89167cb2018-07-08 20:12:23 +02002420psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002421 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002422 psa_algorithm_t alg )
2423{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002424 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002425}
2426
2427psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002428 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002429 psa_algorithm_t alg )
2430{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002431 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002432}
2433
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002434psa_status_t psa_mac_update( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01002435 const uint8_t *input,
2436 size_t input_length )
2437{
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002438 if( operation->id == 0 )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002439 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002440
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002441 /* Don't require hash implementations to behave correctly on a
2442 * zero-length input, which may have an invalid pointer. */
2443 if( input_length == 0 )
2444 return( PSA_SUCCESS );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002445
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002446 psa_status_t status = psa_driver_wrapper_mac_update( operation,
2447 input, input_length );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002448 if( status != PSA_SUCCESS )
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002449 psa_mac_abort( operation );
2450
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002451 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002452}
2453
Steven Cooremana5b860a2021-03-19 19:04:39 +01002454psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002455 uint8_t *mac,
2456 size_t mac_size,
2457 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002458{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002459 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2460 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002461
Steven Cooremana5b860a2021-03-19 19:04:39 +01002462 if( operation->id == 0 )
Dave Rodgman8036bdd2021-06-24 16:19:08 +01002463 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002464 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002465 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002466 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002467
Steven Cooreman72f736a2021-05-07 14:14:37 +02002468 if( ! operation->is_sign )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002469 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002470 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002471 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002472 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002473
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002474 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2475 * once all the error checks are done. */
2476 if( operation->mac_size == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002477 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002478 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002479 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002480 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002481
2482 if( mac_size < operation->mac_size )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002483 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002484 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002485 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002486 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002487
Steven Cooremana5b860a2021-03-19 19:04:39 +01002488 status = psa_driver_wrapper_mac_sign_finish( operation,
Steven Cooreman72f736a2021-05-07 14:14:37 +02002489 mac, operation->mac_size,
2490 mac_length );
2491
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002492exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002493 /* In case of success, set the potential excess room in the output buffer
2494 * to an invalid value, to avoid potentially leaking a longer MAC.
2495 * In case of error, set the output length and content to a safe default,
2496 * such that in case the caller misses an error check, the output would be
2497 * an unachievable MAC.
2498 */
2499 if( status != PSA_SUCCESS )
Steven Cooreman72f736a2021-05-07 14:14:37 +02002500 {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002501 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002502 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002503 }
mohammad16036df908f2018-04-02 08:34:15 -07002504
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002505 if( mac_size > operation->mac_size )
2506 memset( &mac[operation->mac_size], '!',
2507 mac_size - operation->mac_size );
2508
Steven Cooremana5b860a2021-03-19 19:04:39 +01002509 abort_status = psa_mac_abort( operation );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002510
Steven Cooremana5b860a2021-03-19 19:04:39 +01002511 return( status == PSA_SUCCESS ? abort_status : status );
mohammad16036df908f2018-04-02 08:34:15 -07002512}
2513
Steven Cooremana5b860a2021-03-19 19:04:39 +01002514psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002515 const uint8_t *mac,
2516 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002517{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002518 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2519 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002520
Steven Cooremana5b860a2021-03-19 19:04:39 +01002521 if( operation->id == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002522 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002523 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002524 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002525 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002526
Steven Cooreman72f736a2021-05-07 14:14:37 +02002527 if( operation->is_sign )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002528 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002529 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002530 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002531 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002532
2533 if( operation->mac_size != mac_length )
2534 {
2535 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002536 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002537 }
2538
Steven Cooremana5b860a2021-03-19 19:04:39 +01002539 status = psa_driver_wrapper_mac_verify_finish( operation,
2540 mac, mac_length );
Steven Cooreman72f736a2021-05-07 14:14:37 +02002541
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002542exit:
Steven Cooremana5b860a2021-03-19 19:04:39 +01002543 abort_status = psa_mac_abort( operation );
mohammad16036df908f2018-04-02 08:34:15 -07002544
Steven Cooremana5b860a2021-03-19 19:04:39 +01002545 return( status == PSA_SUCCESS ? abort_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546}
2547
Ronald Croncd989b52021-06-18 14:23:33 +02002548static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key,
2549 psa_algorithm_t alg,
2550 const uint8_t *input,
2551 size_t input_length,
2552 uint8_t *mac,
2553 size_t mac_size,
2554 size_t *mac_length,
2555 int is_sign )
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002556{
2557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002558 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2559 psa_key_slot_t *slot;
2560 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561
Ronald Cron51131b52021-06-17 17:17:20 +02002562 status = psa_get_and_lock_key_slot_with_policy(
Mateusz Starzykce0e6a92021-08-17 15:24:32 +02002563 key,
2564 &slot,
2565 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Ronald Croncd989b52021-06-18 14:23:33 +02002566 alg );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002567 if( status != PSA_SUCCESS )
2568 goto exit;
2569
Ronald Cron51131b52021-06-17 17:17:20 +02002570 psa_key_attributes_t attributes = {
2571 .core = slot->attr
2572 };
2573
2574 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2575 &operation_mac_size );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002576 if( status != PSA_SUCCESS )
2577 goto exit;
2578
Ronald Cron51131b52021-06-17 17:17:20 +02002579 if( mac_size < operation_mac_size )
2580 {
2581 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002582 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002583 }
2584
2585 status = psa_driver_wrapper_mac_compute(
2586 &attributes,
2587 slot->key.data, slot->key.bytes,
2588 alg,
2589 input, input_length,
2590 mac, operation_mac_size, mac_length );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002591
2592exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002593 /* In case of success, set the potential excess room in the output buffer
2594 * to an invalid value, to avoid potentially leaking a longer MAC.
2595 * In case of error, set the output length and content to a safe default,
2596 * such that in case the caller misses an error check, the output would be
2597 * an unachievable MAC.
2598 */
2599 if( status != PSA_SUCCESS )
Ronald Cron51131b52021-06-17 17:17:20 +02002600 {
Ronald Cron51131b52021-06-17 17:17:20 +02002601 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002602 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002603 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002604 if( mac_size > operation_mac_size )
2605 memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002606
Ronald Cron51131b52021-06-17 17:17:20 +02002607 unlock_status = psa_unlock_key_slot( slot );
2608
2609 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002610}
2611
Ronald Croncd989b52021-06-18 14:23:33 +02002612psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key,
2613 psa_algorithm_t alg,
2614 const uint8_t *input,
2615 size_t input_length,
2616 uint8_t *mac,
2617 size_t mac_size,
2618 size_t *mac_length)
2619{
2620 return( psa_mac_compute_internal( key, alg,
2621 input, input_length,
2622 mac, mac_size, mac_length, 1 ) );
2623}
2624
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002625psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key,
2626 psa_algorithm_t alg,
2627 const uint8_t *input,
2628 size_t input_length,
2629 const uint8_t *mac,
2630 size_t mac_length)
2631{
2632 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002633 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2634 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002635
Ronald Crona587cbc2021-06-18 14:51:29 +02002636 status = psa_mac_compute_internal( key, alg,
2637 input, input_length,
2638 actual_mac, sizeof( actual_mac ),
2639 &actual_mac_length, 0 );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002640 if( status != PSA_SUCCESS )
2641 goto exit;
2642
Ronald Crona587cbc2021-06-18 14:51:29 +02002643 if( mac_length != actual_mac_length )
2644 {
2645 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002646 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002647 }
2648 if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
2649 {
2650 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002651 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002652 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002653
2654exit:
Ronald Crona587cbc2021-06-18 14:51:29 +02002655 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002656
2657 return ( status );
2658}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002659
Gilles Peskinee59236f2018-01-27 23:32:46 +01002660/****************************************************************/
2661/* Asymmetric cryptography */
2662/****************************************************************/
2663
gabor-mezei-armf0486182021-05-12 11:03:09 +02002664static psa_status_t psa_sign_verify_check_alg( int input_is_message,
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002665 psa_algorithm_t alg )
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002666{
gabor-mezei-armf0486182021-05-12 11:03:09 +02002667 if( input_is_message )
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002668 {
2669 if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) )
2670 return( PSA_ERROR_INVALID_ARGUMENT );
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002671
Gilles Peskinef7b41372021-09-22 16:15:05 +02002672 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002673 {
2674 if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) )
2675 return( PSA_ERROR_INVALID_ARGUMENT );
2676 }
2677 }
2678 else
2679 {
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02002680 if( ! PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002681 return( PSA_ERROR_INVALID_ARGUMENT );
2682 }
2683
2684 return( PSA_SUCCESS );
2685}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002686
2687static psa_status_t psa_sign_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002688 int input_is_message,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002689 psa_algorithm_t alg,
2690 const uint8_t * input,
2691 size_t input_length,
2692 uint8_t * signature,
2693 size_t signature_size,
2694 size_t * signature_length )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002695{
2696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2697 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2698 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002699
2700 *signature_length = 0;
2701
gabor-mezei-armf0486182021-05-12 11:03:09 +02002702 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002703 if( status != PSA_SUCCESS )
2704 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002705
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002706 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002707 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002708 * buffer can in principle be empty since it doesn't actually have
2709 * to be a hash.) */
2710 if( signature_size == 0 )
2711 return( PSA_ERROR_BUFFER_TOO_SMALL );
2712
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002713 status = psa_get_and_lock_key_slot_with_policy(
2714 key, &slot,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002715 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2716 PSA_KEY_USAGE_SIGN_HASH,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002717 alg );
2718
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002719 if( status != PSA_SUCCESS )
2720 goto exit;
2721
2722 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
2723 {
2724 status = PSA_ERROR_INVALID_ARGUMENT;
2725 goto exit;
2726 }
2727
2728 psa_key_attributes_t attributes = {
2729 .core = slot->attr
2730 };
2731
gabor-mezei-armf0486182021-05-12 11:03:09 +02002732 if( input_is_message )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002733 {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002734 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002735 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002736 alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002737 signature, signature_size, signature_length );
2738 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002739 else
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002740 {
2741
2742 status = psa_driver_wrapper_sign_hash(
2743 &attributes, slot->key.data, slot->key.bytes,
2744 alg, input, input_length,
2745 signature, signature_size, signature_length );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002746 }
2747
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002748
2749exit:
2750 /* Fill the unused part of the output buffer (the whole buffer on error,
2751 * the trailing part on success) with something that isn't a valid signature
2752 * (barring an attack on the signature and deliberately-crafted input),
2753 * in case the caller doesn't check the return status properly. */
2754 if( status == PSA_SUCCESS )
2755 memset( signature + *signature_length, '!',
2756 signature_size - *signature_length );
2757 else
2758 memset( signature, '!', signature_size );
2759 /* If signature_size is 0 then we have nothing to do. We must not call
2760 * memset because signature may be NULL in this case. */
2761
2762 unlock_status = psa_unlock_key_slot( slot );
2763
2764 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
2765}
2766
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002767static psa_status_t psa_verify_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002768 int input_is_message,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002769 psa_algorithm_t alg,
2770 const uint8_t * input,
2771 size_t input_length,
2772 const uint8_t * signature,
2773 size_t signature_length )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002774{
2775 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2776 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2777 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002778
gabor-mezei-armf0486182021-05-12 11:03:09 +02002779 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002780 if( status != PSA_SUCCESS )
2781 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002782
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002783 status = psa_get_and_lock_key_slot_with_policy(
2784 key, &slot,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002785 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2786 PSA_KEY_USAGE_VERIFY_HASH,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002787 alg );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002788
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002789 if( status != PSA_SUCCESS )
2790 return( status );
2791
2792 psa_key_attributes_t attributes = {
2793 .core = slot->attr
2794 };
2795
gabor-mezei-armf0486182021-05-12 11:03:09 +02002796 if( input_is_message )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002797 {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002798 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002799 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002800 alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002801 signature, signature_length );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002802 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002803 else
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002804 {
2805 status = psa_driver_wrapper_verify_hash(
2806 &attributes, slot->key.data, slot->key.bytes,
2807 alg, input, input_length,
2808 signature, signature_length );
2809 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002810
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002811 unlock_status = psa_unlock_key_slot( slot );
2812
2813 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002814
2815}
2816
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002817psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002818 const psa_key_attributes_t *attributes,
2819 const uint8_t *key_buffer,
2820 size_t key_buffer_size,
2821 psa_algorithm_t alg,
2822 const uint8_t *input,
2823 size_t input_length,
2824 uint8_t *signature,
2825 size_t signature_size,
2826 size_t *signature_length )
2827{
2828 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002829
Gilles Peskinef7b41372021-09-22 16:15:05 +02002830 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002831 {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002832 size_t hash_length;
2833 uint8_t hash[PSA_HASH_MAX_SIZE];
2834
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002835 status = psa_driver_wrapper_hash_compute(
2836 PSA_ALG_SIGN_GET_HASH( alg ),
2837 input, input_length,
2838 hash, sizeof( hash ), &hash_length );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002839
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002840 if( status != PSA_SUCCESS )
2841 return status;
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002842
2843 return psa_driver_wrapper_sign_hash(
2844 attributes, key_buffer, key_buffer_size,
2845 alg, hash, hash_length,
2846 signature, signature_size, signature_length );
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002847 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002848
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002849 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002850}
2851
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002852psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
2853 psa_algorithm_t alg,
2854 const uint8_t * input,
2855 size_t input_length,
2856 uint8_t * signature,
2857 size_t signature_size,
2858 size_t * signature_length )
2859{
2860 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002861 key, 1, alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002862 signature, signature_size, signature_length );
2863}
2864
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002865psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002866 const psa_key_attributes_t *attributes,
2867 const uint8_t *key_buffer,
2868 size_t key_buffer_size,
2869 psa_algorithm_t alg,
2870 const uint8_t *input,
2871 size_t input_length,
2872 const uint8_t *signature,
2873 size_t signature_length )
2874{
2875 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002876
Gilles Peskinef7b41372021-09-22 16:15:05 +02002877 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002878 {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002879 size_t hash_length;
2880 uint8_t hash[PSA_HASH_MAX_SIZE];
2881
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002882 status = psa_driver_wrapper_hash_compute(
2883 PSA_ALG_SIGN_GET_HASH( alg ),
2884 input, input_length,
2885 hash, sizeof( hash ), &hash_length );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002886
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002887 if( status != PSA_SUCCESS )
2888 return status;
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002889
2890 return psa_driver_wrapper_verify_hash(
2891 attributes, key_buffer, key_buffer_size,
2892 alg, hash, hash_length,
2893 signature, signature_length );
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002894 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002895
gabor-mezei-arm474a35f2021-05-05 13:59:01 +02002896 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002897}
2898
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002899psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
2900 psa_algorithm_t alg,
2901 const uint8_t * input,
2902 size_t input_length,
2903 const uint8_t * signature,
2904 size_t signature_length )
2905{
2906 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002907 key, 1, alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002908 signature, signature_length );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002909}
2910
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002911psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002912 const psa_key_attributes_t *attributes,
2913 const uint8_t *key_buffer, size_t key_buffer_size,
2914 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2915 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002916{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002917 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002918 {
Ronald Cron4501c982021-03-19 20:09:32 +01002919 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2920 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002921 {
Ronald Cron4501c982021-03-19 20:09:32 +01002922#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2923 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2924 return( mbedtls_psa_rsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002925 attributes,
2926 key_buffer, key_buffer_size,
2927 alg, hash, hash_length,
2928 signature, signature_size, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002929#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2930 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002931 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002932 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002933 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002934 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002935 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002936 }
Ronald Cron81ca97e2021-04-09 15:32:03 +02002937 else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Ronald Cron4501c982021-03-19 20:09:32 +01002938 {
2939 if( PSA_ALG_IS_ECDSA( alg ) )
2940 {
2941#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2942 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2943 return( mbedtls_psa_ecdsa_sign_hash(
2944 attributes,
2945 key_buffer, key_buffer_size,
2946 alg, hash, hash_length,
2947 signature, signature_size, signature_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002948#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2949 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron4501c982021-03-19 20:09:32 +01002950 }
2951 else
2952 {
2953 return( PSA_ERROR_INVALID_ARGUMENT );
2954 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002955 }
Ronald Cron4501c982021-03-19 20:09:32 +01002956
2957 (void)key_buffer;
2958 (void)key_buffer_size;
2959 (void)hash;
2960 (void)hash_length;
2961 (void)signature;
2962 (void)signature_size;
2963 (void)signature_length;
2964
2965 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01002966}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002967
2968psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
2969 psa_algorithm_t alg,
2970 const uint8_t *hash,
2971 size_t hash_length,
2972 uint8_t *signature,
2973 size_t signature_size,
2974 size_t *signature_length )
2975{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002976 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002977 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01002978 signature, signature_size, signature_length );
itayzafrir5c753392018-05-08 11:18:38 +03002979}
2980
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002981psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002982 const psa_key_attributes_t *attributes,
2983 const uint8_t *key_buffer, size_t key_buffer_size,
2984 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2985 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002986{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002987 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002988 {
Ronald Cron4501c982021-03-19 20:09:32 +01002989 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2990 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002991 {
Ronald Cron4501c982021-03-19 20:09:32 +01002992#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2993 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2994 return( mbedtls_psa_rsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002995 attributes,
2996 key_buffer, key_buffer_size,
2997 alg, hash, hash_length,
2998 signature, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002999#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3000 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02003001 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003002 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003003 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003004 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003005 }
itayzafrir5c753392018-05-08 11:18:38 +03003006 }
Ronald Cron81ca97e2021-04-09 15:32:03 +02003007 else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003008 {
Ronald Cron4501c982021-03-19 20:09:32 +01003009 if( PSA_ALG_IS_ECDSA( alg ) )
3010 {
3011#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3012 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3013 return( mbedtls_psa_ecdsa_verify_hash(
3014 attributes,
3015 key_buffer, key_buffer_size,
3016 alg, hash, hash_length,
3017 signature, signature_length ) );
3018#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3019 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3020 }
3021 else
3022 {
3023 return( PSA_ERROR_INVALID_ARGUMENT );
3024 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003025 }
Ronald Cron4501c982021-03-19 20:09:32 +01003026
3027 (void)key_buffer;
3028 (void)key_buffer_size;
3029 (void)hash;
3030 (void)hash_length;
3031 (void)signature;
3032 (void)signature_length;
3033
3034 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01003035}
3036
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003037psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
3038 psa_algorithm_t alg,
3039 const uint8_t *hash,
3040 size_t hash_length,
3041 const uint8_t *signature,
3042 size_t signature_length )
3043{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003044 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003045 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01003046 signature, signature_length );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003047}
3048
Ronald Croncf56a0a2020-08-04 09:51:30 +02003049psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003050 psa_algorithm_t alg,
3051 const uint8_t *input,
3052 size_t input_length,
3053 const uint8_t *salt,
3054 size_t salt_length,
3055 uint8_t *output,
3056 size_t output_size,
3057 size_t *output_length )
3058{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003059 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003060 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003061 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003062
Darryl Green5cc689a2018-07-24 15:34:10 +01003063 (void) input;
3064 (void) input_length;
3065 (void) salt;
3066 (void) output;
3067 (void) output_size;
3068
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003069 *output_length = 0;
3070
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003071 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3072 return( PSA_ERROR_INVALID_ARGUMENT );
3073
Ronald Cron5c522922020-11-14 16:35:34 +01003074 status = psa_get_and_lock_transparent_key_slot_with_policy(
3075 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003076 if( status != PSA_SUCCESS )
3077 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003078 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3079 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003080 {
3081 status = PSA_ERROR_INVALID_ARGUMENT;
3082 goto exit;
3083 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003084
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003085 psa_key_attributes_t attributes = {
3086 .core = slot->attr
3087 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003088
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003089 status = psa_driver_wrapper_asymmetric_encrypt(
3090 &attributes, slot->key.data, slot->key.bytes,
3091 alg, input, input_length, salt, salt_length,
3092 output, output_size, output_length );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003093exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003094 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003095
Ronald Cron5c522922020-11-14 16:35:34 +01003096 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003097}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003098
Ronald Croncf56a0a2020-08-04 09:51:30 +02003099psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003100 psa_algorithm_t alg,
3101 const uint8_t *input,
3102 size_t input_length,
3103 const uint8_t *salt,
3104 size_t salt_length,
3105 uint8_t *output,
3106 size_t output_size,
3107 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003108{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003110 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003111 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003112
Darryl Green5cc689a2018-07-24 15:34:10 +01003113 (void) input;
3114 (void) input_length;
3115 (void) salt;
3116 (void) output;
3117 (void) output_size;
3118
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003119 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003120
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003121 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3122 return( PSA_ERROR_INVALID_ARGUMENT );
3123
Ronald Cron5c522922020-11-14 16:35:34 +01003124 status = psa_get_and_lock_transparent_key_slot_with_policy(
3125 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003126 if( status != PSA_SUCCESS )
3127 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003128 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003129 {
3130 status = PSA_ERROR_INVALID_ARGUMENT;
3131 goto exit;
3132 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003133
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003134 psa_key_attributes_t attributes = {
3135 .core = slot->attr
3136 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003137
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003138 status = psa_driver_wrapper_asymmetric_decrypt(
3139 &attributes, slot->key.data, slot->key.bytes,
3140 alg, input, input_length, salt, salt_length,
3141 output, output_size, output_length );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003142
3143exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003144 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003145
Ronald Cron5c522922020-11-14 16:35:34 +01003146 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003147}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003148
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003149
3150
mohammad1603503973b2018-03-12 15:59:30 +02003151/****************************************************************/
3152/* Symmetric cryptography */
3153/****************************************************************/
3154
Ronald Cronab99ac22020-10-01 16:38:28 +02003155static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
3156 mbedtls_svc_key_id_t key,
3157 psa_algorithm_t alg,
3158 mbedtls_operation_t cipher_operation )
3159{
3160 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3161 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003162 psa_key_slot_t *slot = NULL;
Ronald Cronab99ac22020-10-01 16:38:28 +02003163 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3164 PSA_KEY_USAGE_ENCRYPT :
3165 PSA_KEY_USAGE_DECRYPT );
3166
3167 /* A context must be freshly initialized before it can be set up. */
Ronald Cron49fafa92021-03-10 08:34:23 +01003168 if( operation->id != 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003169 {
Dave Rodgman54648242021-06-24 11:49:45 +01003170 status = PSA_ERROR_BAD_STATE;
3171 goto exit;
3172 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003173
Ronald Cronab99ac22020-10-01 16:38:28 +02003174 if( ! PSA_ALG_IS_CIPHER( alg ) )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003175 {
Dave Rodgman54648242021-06-24 11:49:45 +01003176 status = PSA_ERROR_INVALID_ARGUMENT;
3177 goto exit;
3178 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003179
Ronald Cronab99ac22020-10-01 16:38:28 +02003180 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
3181 if( status != PSA_SUCCESS )
3182 goto exit;
3183
Ronald Cron49fafa92021-03-10 08:34:23 +01003184 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003185 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003186 * so we only set it (in the driver wrapper) after resources have been
3187 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003188 operation->iv_set = 0;
Ronald Cronab99ac22020-10-01 16:38:28 +02003189 if( alg == PSA_ALG_ECB_NO_PADDING )
3190 operation->iv_required = 0;
3191 else
3192 operation->iv_required = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003193 operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
Ronald Cronab99ac22020-10-01 16:38:28 +02003194
Ronald Crona4af55f2020-12-14 14:36:06 +01003195 psa_key_attributes_t attributes = {
3196 .core = slot->attr
3197 };
3198
Ronald Cronab99ac22020-10-01 16:38:28 +02003199 /* Try doing the operation through a driver before using software fallback. */
3200 if( cipher_operation == MBEDTLS_ENCRYPT )
Ronald Crona4af55f2020-12-14 14:36:06 +01003201 status = psa_driver_wrapper_cipher_encrypt_setup( operation,
3202 &attributes,
3203 slot->key.data,
3204 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003205 alg );
3206 else
Ronald Crona4af55f2020-12-14 14:36:06 +01003207 status = psa_driver_wrapper_cipher_decrypt_setup( operation,
3208 &attributes,
3209 slot->key.data,
3210 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003211 alg );
3212
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003213exit:
Ronald Cron06aa4422021-03-09 17:32:57 +01003214 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003215 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003216
Ronald Cron5c522922020-11-14 16:35:34 +01003217 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003218
Ronald Cron5c522922020-11-14 16:35:34 +01003219 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003220}
3221
Gilles Peskinefe119512018-07-08 21:39:34 +02003222psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003223 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003224 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003225{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003226 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003227}
3228
Gilles Peskinefe119512018-07-08 21:39:34 +02003229psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003230 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003231 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003232{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003233 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003234}
3235
Gilles Peskinefe119512018-07-08 21:39:34 +02003236psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003237 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003238 size_t iv_size,
3239 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003240{
Ronald Cron6d051732020-10-01 14:10:20 +02003241 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003242 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3243 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01003244
Ronald Cron49fafa92021-03-10 08:34:23 +01003245 if( operation->id == 0 )
Ronald Cronc45b4af2020-09-29 16:18:05 +02003246 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003247 status = PSA_ERROR_BAD_STATE;
3248 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003249 }
3250
Steven Cooreman7df02922020-09-09 15:28:49 +02003251 if( operation->iv_set || ! operation->iv_required )
3252 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003253 status = PSA_ERROR_BAD_STATE;
3254 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003255 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003256
Ronald Cron2fb90522021-07-05 12:31:44 +02003257 default_iv_length = operation->default_iv_length;
3258 if( iv_size < default_iv_length )
Ronald Cron5618a392021-03-26 09:52:26 +01003259 {
3260 status = PSA_ERROR_BUFFER_TOO_SMALL;
3261 goto exit;
3262 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003263
Ronald Cron2fb90522021-07-05 12:31:44 +02003264 if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE )
3265 {
3266 status = PSA_ERROR_GENERIC_ERROR;
3267 goto exit;
3268 }
3269
3270 status = psa_generate_random( local_iv, default_iv_length );
Ronald Cron5618a392021-03-26 09:52:26 +01003271 if( status != PSA_SUCCESS )
3272 goto exit;
3273
3274 status = psa_driver_wrapper_cipher_set_iv( operation,
Ronald Cron2fb90522021-07-05 12:31:44 +02003275 local_iv, default_iv_length );
Ronald Cron5618a392021-03-26 09:52:26 +01003276
3277exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003278 if( status == PSA_SUCCESS )
Ronald Cron5618a392021-03-26 09:52:26 +01003279 {
Ronald Cron2fb90522021-07-05 12:31:44 +02003280 memcpy( iv, local_iv, default_iv_length );
3281 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003282 operation->iv_set = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003283 }
Steven Cooreman7df02922020-09-09 15:28:49 +02003284 else
Ronald Cron2fb90522021-07-05 12:31:44 +02003285 {
3286 *iv_length = 0;
Moran Peker395db872018-05-31 14:07:14 +03003287 psa_cipher_abort( operation );
Ronald Cron2fb90522021-07-05 12:31:44 +02003288 }
Ronald Cron6d051732020-10-01 14:10:20 +02003289
itayzafrir534bd7c2018-08-02 13:56:32 +03003290 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003291}
3292
Gilles Peskinefe119512018-07-08 21:39:34 +02003293psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003294 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003295 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003296{
Ronald Cron6d051732020-10-01 14:10:20 +02003297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003298
Ronald Cron49fafa92021-03-10 08:34:23 +01003299 if( operation->id == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003300 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003301 status = PSA_ERROR_BAD_STATE;
3302 goto exit;
3303 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003304
Steven Cooreman7df02922020-09-09 15:28:49 +02003305 if( operation->iv_set || ! operation->iv_required )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003306 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003307 status = PSA_ERROR_BAD_STATE;
3308 goto exit;
3309 }
Ronald Crona0d68172021-03-26 10:15:08 +01003310
3311 if( iv_length > PSA_CIPHER_IV_MAX_SIZE )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003312 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003313 status = PSA_ERROR_INVALID_ARGUMENT;
3314 goto exit;
3315 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003316
Ronald Crondd24c9b2020-12-15 14:10:01 +01003317 status = psa_driver_wrapper_cipher_set_iv( operation,
3318 iv,
3319 iv_length );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003320
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003321exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003322 if( status == PSA_SUCCESS )
3323 operation->iv_set = 1;
3324 else
mohammad1603503973b2018-03-12 15:59:30 +02003325 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003326 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003327}
3328
Gilles Peskinee553c652018-06-04 16:22:46 +02003329psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3330 const uint8_t *input,
3331 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003332 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003333 size_t output_size,
3334 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003335{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003336 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003337
Ronald Cron49fafa92021-03-10 08:34:23 +01003338 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003339 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003340 status = PSA_ERROR_BAD_STATE;
3341 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003342 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003343
Steven Cooreman7df02922020-09-09 15:28:49 +02003344 if( operation->iv_required && ! operation->iv_set )
3345 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003346 status = PSA_ERROR_BAD_STATE;
3347 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003348 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003349
Ronald Crondd24c9b2020-12-15 14:10:01 +01003350 status = psa_driver_wrapper_cipher_update( operation,
3351 input,
3352 input_length,
3353 output,
3354 output_size,
3355 output_length );
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003356
3357exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003358 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003359 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003360
itayzafrir534bd7c2018-08-02 13:56:32 +03003361 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003362}
3363
Gilles Peskinee553c652018-06-04 16:22:46 +02003364psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3365 uint8_t *output,
3366 size_t output_size,
3367 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003368{
David Saadab4ecc272019-02-14 13:48:10 +02003369 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003370
Ronald Cron49fafa92021-03-10 08:34:23 +01003371 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003372 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003373 status = PSA_ERROR_BAD_STATE;
3374 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003375 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003376
Steven Cooreman7df02922020-09-09 15:28:49 +02003377 if( operation->iv_required && ! operation->iv_set )
3378 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003379 status = PSA_ERROR_BAD_STATE;
3380 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003381 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003382
Ronald Crondd24c9b2020-12-15 14:10:01 +01003383 status = psa_driver_wrapper_cipher_finish( operation,
3384 output,
3385 output_size,
3386 output_length );
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003387
3388exit:
Steven Cooremanef8575e2020-09-11 11:44:50 +02003389 if( status == PSA_SUCCESS )
3390 return( psa_cipher_abort( operation ) );
3391 else
3392 {
3393 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003394 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01003395
Steven Cooremanef8575e2020-09-11 11:44:50 +02003396 return( status );
3397 }
mohammad1603503973b2018-03-12 15:59:30 +02003398}
3399
Gilles Peskinee553c652018-06-04 16:22:46 +02003400psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3401{
Ronald Cron49fafa92021-03-10 08:34:23 +01003402 if( operation->id == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003403 {
Steven Cooremana07b9972020-09-10 14:54:14 +02003404 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003405 * in use. It's ok to call abort on such an object, and there's
3406 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003407 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003408 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003409
Ronald Crondd24c9b2020-12-15 14:10:01 +01003410 psa_driver_wrapper_cipher_abort( operation );
Gilles Peskinee553c652018-06-04 16:22:46 +02003411
Ronald Cron49fafa92021-03-10 08:34:23 +01003412 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003413 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003414 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003415
Moran Peker395db872018-05-31 14:07:14 +03003416 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003417}
3418
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003419psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key,
3420 psa_algorithm_t alg,
3421 const uint8_t *input,
3422 size_t input_length,
3423 uint8_t *output,
3424 size_t output_size,
3425 size_t *output_length )
3426{
3427 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003428 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02003429 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02003430 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3431 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02003432
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003433 if( ! PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron23919522021-07-08 19:00:07 +02003434 {
3435 status = PSA_ERROR_INVALID_ARGUMENT;
3436 goto exit;
3437 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003438
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003439 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3440 PSA_KEY_USAGE_ENCRYPT,
3441 alg );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003442 if( status != PSA_SUCCESS )
Ronald Cron23919522021-07-08 19:00:07 +02003443 goto exit;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003444
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003445 psa_key_attributes_t attributes = {
3446 .core = slot->attr
3447 };
3448
Ronald Cronc6e6f502021-07-09 18:44:58 +02003449 default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
3450 if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE )
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003451 {
Ronald Cronc6e6f502021-07-09 18:44:58 +02003452 status = PSA_ERROR_GENERIC_ERROR;
3453 goto exit;
3454 }
3455
3456 if( default_iv_length > 0 )
3457 {
3458 if( output_size < default_iv_length )
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003459 {
3460 status = PSA_ERROR_BUFFER_TOO_SMALL;
3461 goto exit;
3462 }
3463
Ronald Cronc6e6f502021-07-09 18:44:58 +02003464 status = psa_generate_random( local_iv, default_iv_length );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003465 if( status != PSA_SUCCESS )
3466 goto exit;
3467 }
3468
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003469 status = psa_driver_wrapper_cipher_encrypt(
3470 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02003471 alg, local_iv, default_iv_length, input, input_length,
3472 output + default_iv_length, output_size - default_iv_length,
3473 output_length );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003474
3475exit:
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003476 unlock_status = psa_unlock_key_slot( slot );
Ronald Cron23919522021-07-08 19:00:07 +02003477 if( status == PSA_SUCCESS )
3478 status = unlock_status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003479
Ronald Cron9b674282021-07-09 09:19:35 +02003480 if( status == PSA_SUCCESS )
Ronald Cronc6e6f502021-07-09 18:44:58 +02003481 {
3482 if( default_iv_length > 0 )
3483 memcpy( output, local_iv, default_iv_length );
3484 *output_length += default_iv_length;
3485 }
Ronald Cron9b674282021-07-09 09:19:35 +02003486 else
Ronald Cron23919522021-07-08 19:00:07 +02003487 *output_length = 0;
3488
3489 return( status );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003490}
3491
3492psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key,
3493 psa_algorithm_t alg,
3494 const uint8_t *input,
3495 size_t input_length,
3496 uint8_t *output,
3497 size_t output_size,
3498 size_t *output_length )
3499{
3500 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003501 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02003502 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02003503
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003504 if( ! PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron23919522021-07-08 19:00:07 +02003505 {
3506 status = PSA_ERROR_INVALID_ARGUMENT;
3507 goto exit;
3508 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003509
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003510 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3511 PSA_KEY_USAGE_DECRYPT,
3512 alg );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003513 if( status != PSA_SUCCESS )
Ronald Cron23919522021-07-08 19:00:07 +02003514 goto exit;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003515
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003516 psa_key_attributes_t attributes = {
3517 .core = slot->attr
3518 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003519
Mateusz Starzyk594215b2021-10-14 12:23:06 +02003520 if( alg == PSA_ALG_CCM_STAR_NO_TAG && input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) )
3521 {
3522 status = PSA_ERROR_INVALID_ARGUMENT;
3523 goto exit;
3524 }
3525 else if ( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) )
gabor-mezei-arm258ae072021-06-25 15:25:38 +02003526 {
3527 status = PSA_ERROR_INVALID_ARGUMENT;
3528 goto exit;
3529 }
3530
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003531 status = psa_driver_wrapper_cipher_decrypt(
3532 &attributes, slot->key.data, slot->key.bytes,
3533 alg, input, input_length,
3534 output, output_size, output_length );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003535
gabor-mezei-arm258ae072021-06-25 15:25:38 +02003536exit:
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003537 unlock_status = psa_unlock_key_slot( slot );
Ronald Cron23919522021-07-08 19:00:07 +02003538 if( status == PSA_SUCCESS )
3539 status = unlock_status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003540
Ronald Cron23919522021-07-08 19:00:07 +02003541 if( status != PSA_SUCCESS )
3542 *output_length = 0;
3543
3544 return( status );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003545}
3546
3547
mohammad16035955c982018-04-26 00:53:03 +03003548/****************************************************************/
3549/* AEAD */
3550/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003551
Paul Elliottbaff51c2021-09-28 17:44:45 +01003552/* Helper function to get the base algorithm from its variants. */
3553static psa_algorithm_t psa_aead_get_base_algorithm( psa_algorithm_t alg )
3554{
3555 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg );
3556}
3557
3558/* Helper function to perform common nonce length checks. */
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003559static psa_status_t psa_aead_check_nonce_length( psa_algorithm_t alg,
3560 size_t nonce_length )
3561{
Paul Elliottbaff51c2021-09-28 17:44:45 +01003562 psa_algorithm_t base_alg = psa_aead_get_base_algorithm( alg );
3563
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003564 switch(base_alg)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003565 {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003566#if defined(PSA_WANT_ALG_GCM)
3567 case PSA_ALG_GCM:
3568 /* Not checking max nonce size here as GCM spec allows almost
3569 * arbitrarily large nonces. Please note that we do not generally
3570 * recommend the usage of nonces of greater length than
3571 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
3572 * size, which can then lead to collisions if you encrypt a very
3573 * large number of messages.*/
3574 if( nonce_length != 0 )
3575 return( PSA_SUCCESS );
3576 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003577#endif /* PSA_WANT_ALG_GCM */
3578#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003579 case PSA_ALG_CCM:
3580 if( nonce_length >= 7 && nonce_length <= 13 )
3581 return( PSA_SUCCESS );
3582 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003583#endif /* PSA_WANT_ALG_CCM */
3584#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003585 case PSA_ALG_CHACHA20_POLY1305:
3586 if( nonce_length == 12 )
3587 return( PSA_SUCCESS );
Bence Szépkúti6d48e202021-11-15 20:04:15 +01003588 else if( nonce_length == 8 )
3589 return( PSA_ERROR_NOT_SUPPORTED );
3590 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003591#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003592 default:
Bence Szépkúti357b78e2021-11-09 13:17:17 +01003593 return( PSA_ERROR_NOT_SUPPORTED );
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003594 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003595
Bence Szépkúti357b78e2021-11-09 13:17:17 +01003596 return( PSA_ERROR_INVALID_ARGUMENT );
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003597}
3598
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01003599static psa_status_t psa_aead_check_algorithm( psa_algorithm_t alg )
3600{
Bence Szépkúti08f34652021-12-08 21:07:13 +01003601 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3602 return( PSA_ERROR_INVALID_ARGUMENT );
3603
3604 return( PSA_SUCCESS );
3605}
3606
Ronald Croncf56a0a2020-08-04 09:51:30 +02003607psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003608 psa_algorithm_t alg,
3609 const uint8_t *nonce,
3610 size_t nonce_length,
3611 const uint8_t *additional_data,
3612 size_t additional_data_length,
3613 const uint8_t *plaintext,
3614 size_t plaintext_length,
3615 uint8_t *ciphertext,
3616 size_t ciphertext_size,
3617 size_t *ciphertext_length )
3618{
Ronald Cron215633c2021-03-16 17:15:37 +01003619 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3620 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003621
mohammad1603f08a5502018-06-03 15:05:47 +03003622 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003623
Bence Szépkúti39fb9d12022-01-13 14:33:45 +01003624 status = psa_aead_check_algorithm( alg );
Bence Szépkúti08f34652021-12-08 21:07:13 +01003625 if( status != PSA_SUCCESS )
3626 return( status );
Steven Cooremanea7ab132021-03-17 16:28:00 +01003627
Ronald Cron9a986162021-03-26 12:40:07 +01003628 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003629 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003630 if( status != PSA_SUCCESS )
3631 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003632
Ronald Cron215633c2021-03-16 17:15:37 +01003633 psa_key_attributes_t attributes = {
3634 .core = slot->attr
3635 };
mohammad16035955c982018-04-26 00:53:03 +03003636
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003637 status = psa_aead_check_nonce_length( alg, nonce_length );
3638 if( status != PSA_SUCCESS )
3639 goto exit;
3640
Ronald Cronde822812021-03-17 16:08:20 +01003641 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003642 &attributes, slot->key.data, slot->key.bytes,
3643 alg,
3644 nonce, nonce_length,
3645 additional_data, additional_data_length,
3646 plaintext, plaintext_length,
3647 ciphertext, ciphertext_size, ciphertext_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003648
Gilles Peskineedf9a652018-08-17 18:11:56 +02003649 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3650 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003651
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003652exit:
Ronald Cron9f310172021-03-16 16:36:37 +01003653 psa_unlock_key_slot( slot );
mohammad16035955c982018-04-26 00:53:03 +03003654
mohammad16035955c982018-04-26 00:53:03 +03003655 return( status );
Gilles Peskineee652a32018-06-01 19:23:52 +02003656}
3657
Ronald Croncf56a0a2020-08-04 09:51:30 +02003658psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003659 psa_algorithm_t alg,
3660 const uint8_t *nonce,
3661 size_t nonce_length,
3662 const uint8_t *additional_data,
3663 size_t additional_data_length,
3664 const uint8_t *ciphertext,
3665 size_t ciphertext_length,
3666 uint8_t *plaintext,
3667 size_t plaintext_size,
3668 size_t *plaintext_length )
3669{
Ronald Cron215633c2021-03-16 17:15:37 +01003670 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3671 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003672
Gilles Peskineee652a32018-06-01 19:23:52 +02003673 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003674
Bence Szépkúti39fb9d12022-01-13 14:33:45 +01003675 status = psa_aead_check_algorithm( alg );
Bence Szépkúti08f34652021-12-08 21:07:13 +01003676 if( status != PSA_SUCCESS )
3677 return( status );
Steven Cooremanea7ab132021-03-17 16:28:00 +01003678
Ronald Cron9a986162021-03-26 12:40:07 +01003679 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003680 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003681 if( status != PSA_SUCCESS )
3682 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003683
Ronald Cron215633c2021-03-16 17:15:37 +01003684 psa_key_attributes_t attributes = {
3685 .core = slot->attr
3686 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003687
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003688 status = psa_aead_check_nonce_length( alg, nonce_length );
3689 if( status != PSA_SUCCESS )
3690 goto exit;
3691
Ronald Cronde822812021-03-17 16:08:20 +01003692 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003693 &attributes, slot->key.data, slot->key.bytes,
3694 alg,
3695 nonce, nonce_length,
3696 additional_data, additional_data_length,
3697 ciphertext, ciphertext_length,
3698 plaintext, plaintext_size, plaintext_length );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003699
Gilles Peskineedf9a652018-08-17 18:11:56 +02003700 if( status != PSA_SUCCESS && plaintext_size != 0 )
3701 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003702
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003703exit:
Ronald Cron215633c2021-03-16 17:15:37 +01003704 psa_unlock_key_slot( slot );
3705
Gilles Peskineedf9a652018-08-17 18:11:56 +02003706 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003707}
3708
Andrzej Kurekf8816012021-12-19 17:00:12 +01003709static psa_status_t psa_validate_tag_length( psa_aead_operation_t *operation,
3710 psa_algorithm_t alg ) {
3711 uint8_t tag_len = 0;
3712 if( psa_driver_get_tag_len( operation, &tag_len ) != PSA_SUCCESS )
3713 {
3714 return( PSA_ERROR_INVALID_ARGUMENT );
3715 }
3716
3717 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
3718 {
3719#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
3720 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
3721 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
3722 if( tag_len < 4 || tag_len > 16 || tag_len % 2 )
3723 return( PSA_ERROR_INVALID_ARGUMENT );
3724 break;
3725#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
3726
3727#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
3728 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
3729 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
3730 if( tag_len != 4 && tag_len != 8 && ( tag_len < 12 || tag_len > 16 ) )
3731 return( PSA_ERROR_INVALID_ARGUMENT );
3732 break;
3733#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
3734
3735#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
3736 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
3737 /* We only support the default tag length. */
3738 if( tag_len != 16 )
3739 return( PSA_ERROR_INVALID_ARGUMENT );
3740 break;
3741#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
3742
3743 default:
3744 (void) tag_len;
3745 return( PSA_ERROR_NOT_SUPPORTED );
3746 }
3747 return( PSA_SUCCESS );
3748}
3749
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003750/* Set the key for a multipart authenticated operation. */
3751static psa_status_t psa_aead_setup( psa_aead_operation_t *operation,
Paul Elliottd9343f22021-08-23 18:59:49 +01003752 int is_encrypt,
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003753 mbedtls_svc_key_id_t key,
3754 psa_algorithm_t alg )
Paul Elliottc2b71442021-06-24 18:17:52 +01003755{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003756 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3757 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3758 psa_key_slot_t *slot = NULL;
3759 psa_key_usage_t key_usage = 0;
3760
Bence Szépkúti39fb9d12022-01-13 14:33:45 +01003761 status = psa_aead_check_algorithm( alg );
Bence Szépkúti08f34652021-12-08 21:07:13 +01003762 if( status != PSA_SUCCESS )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003763 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003764
3765 if( operation->id != 0 )
3766 {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003767 status = PSA_ERROR_BAD_STATE;
3768 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003769 }
3770
3771 if( operation->nonce_set || operation->lengths_set ||
3772 operation->ad_started || operation->body_started )
3773 {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003774 status = PSA_ERROR_BAD_STATE;
3775 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003776 }
3777
Paul Elliottd9343f22021-08-23 18:59:49 +01003778 if( is_encrypt )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003779 key_usage = PSA_KEY_USAGE_ENCRYPT;
3780 else
3781 key_usage = PSA_KEY_USAGE_DECRYPT;
3782
3783 status = psa_get_and_lock_key_slot_with_policy( key, &slot, key_usage,
3784 alg );
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003785 if( status != PSA_SUCCESS )
3786 goto exit;
3787
3788 psa_key_attributes_t attributes = {
3789 .core = slot->attr
3790 };
3791
Paul Elliottd9343f22021-08-23 18:59:49 +01003792 if( is_encrypt )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003793 status = psa_driver_wrapper_aead_encrypt_setup( operation,
3794 &attributes,
3795 slot->key.data,
3796 slot->key.bytes,
3797 alg );
3798 else
3799 status = psa_driver_wrapper_aead_decrypt_setup( operation,
3800 &attributes,
3801 slot->key.data,
3802 slot->key.bytes,
3803 alg );
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003804 if( status != PSA_SUCCESS )
3805 goto exit;
3806
Andrzej Kurekf8816012021-12-19 17:00:12 +01003807 if( ( status = psa_validate_tag_length( operation, alg ) ) != PSA_SUCCESS )
3808 goto exit;
3809
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003810 operation->key_type = psa_get_key_type( &attributes );
3811
3812exit:
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003813 unlock_status = psa_unlock_key_slot( slot );
3814
3815 if( status == PSA_SUCCESS )
3816 {
3817 status = unlock_status;
3818 operation->alg = psa_aead_get_base_algorithm( alg );
Paul Elliottd9343f22021-08-23 18:59:49 +01003819 operation->is_encrypt = is_encrypt;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003820 }
3821 else
3822 psa_aead_abort( operation );
3823
3824 return( status );
Paul Elliottc2b71442021-06-24 18:17:52 +01003825}
3826
Paul Elliott302ff6b2021-04-20 18:10:30 +01003827/* Set the key for a multipart authenticated encryption operation. */
3828psa_status_t psa_aead_encrypt_setup( psa_aead_operation_t *operation,
3829 mbedtls_svc_key_id_t key,
3830 psa_algorithm_t alg )
3831{
Paul Elliottd9343f22021-08-23 18:59:49 +01003832 return( psa_aead_setup( operation, 1, key, alg ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003833}
3834
3835/* Set the key for a multipart authenticated decryption operation. */
3836psa_status_t psa_aead_decrypt_setup( psa_aead_operation_t *operation,
3837 mbedtls_svc_key_id_t key,
3838 psa_algorithm_t alg )
3839{
Paul Elliottd9343f22021-08-23 18:59:49 +01003840 return( psa_aead_setup( operation, 0, key, alg ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003841}
3842
3843/* Generate a random nonce / IV for multipart AEAD operation */
3844psa_status_t psa_aead_generate_nonce( psa_aead_operation_t *operation,
3845 uint8_t *nonce,
3846 size_t nonce_size,
3847 size_t *nonce_length )
3848{
3849 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01003850 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01003851 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003852
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003853 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003854
Paul Elliottcee785c2021-05-20 14:29:20 +01003855 if( operation->id == 0 )
3856 {
3857 status = PSA_ERROR_BAD_STATE;
3858 goto exit;
3859 }
3860
Paul Elliottdaf5c892021-08-25 16:24:58 +01003861 if( operation->nonce_set || !operation->is_encrypt )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003862 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003863 status = PSA_ERROR_BAD_STATE;
3864 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003865 }
3866
Paul Elliotte193ea82021-10-01 13:00:16 +01003867 /* For CCM, this size may not be correct according to the PSA
3868 * specification. The PSA Crypto 1.0.1 specification states:
3869 *
3870 * CCM encodes the plaintext length pLen in L octets, with L the smallest
3871 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
3872 *
3873 * However this restriction that L has to be the smallest integer is not
3874 * applied in practice, and it is not implementable here since the
3875 * plaintext length may or may not be known at this time. */
Paul Elliottbbe90b52021-05-11 22:22:42 +01003876 required_nonce_size = PSA_AEAD_NONCE_LENGTH( operation->key_type,
Paul Elliott3242f6c2021-08-25 16:33:47 +01003877 operation->alg );
Paul Elliott39dc6b82021-05-11 19:16:09 +01003878 if( nonce_size < required_nonce_size )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003879 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003880 status = PSA_ERROR_BUFFER_TOO_SMALL;
3881 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003882 }
3883
Ronald Croncae59092021-11-26 18:53:58 +01003884 status = psa_generate_random( local_nonce, required_nonce_size );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003885 if( status != PSA_SUCCESS )
Paul Elliott39dc6b82021-05-11 19:16:09 +01003886 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003887
Ronald Croncae59092021-11-26 18:53:58 +01003888 status = psa_aead_set_nonce( operation, local_nonce, required_nonce_size );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003889
Paul Elliott39dc6b82021-05-11 19:16:09 +01003890exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01003891 if( status == PSA_SUCCESS )
Ronald Croncae59092021-11-26 18:53:58 +01003892 {
3893 memcpy( nonce, local_nonce, required_nonce_size );
Paul Elliott39dc6b82021-05-11 19:16:09 +01003894 *nonce_length = required_nonce_size;
Ronald Croncae59092021-11-26 18:53:58 +01003895 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01003896 else
3897 psa_aead_abort( operation );
3898
3899 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003900}
3901
3902/* Set the nonce for a multipart authenticated encryption or decryption
3903 operation.*/
3904psa_status_t psa_aead_set_nonce( psa_aead_operation_t *operation,
3905 const uint8_t *nonce,
3906 size_t nonce_length )
3907{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003908 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3909
Paul Elliottcee785c2021-05-20 14:29:20 +01003910 if( operation->id == 0 )
3911 {
3912 status = PSA_ERROR_BAD_STATE;
3913 goto exit;
3914 }
3915
Paul Elliott3d7d52c2021-09-01 10:33:14 +01003916 if( operation->nonce_set )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003917 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003918 status = PSA_ERROR_BAD_STATE;
3919 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003920 }
3921
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003922 status = psa_aead_check_nonce_length( operation->alg, nonce_length );
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003923 if( status != PSA_SUCCESS )
Paul Elliott4ed1ed12021-09-27 18:09:28 +01003924 {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003925 status = PSA_ERROR_INVALID_ARGUMENT;
3926 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01003927 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01003928
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003929 status = psa_driver_wrapper_aead_set_nonce( operation, nonce,
3930 nonce_length );
3931
Paul Elliott39dc6b82021-05-11 19:16:09 +01003932exit:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003933 if( status == PSA_SUCCESS )
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003934 operation->nonce_set = 1;
Paul Elliott39dc6b82021-05-11 19:16:09 +01003935 else
3936 psa_aead_abort( operation );
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003937
3938 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003939}
3940
3941/* Declare the lengths of the message and additional data for multipart AEAD. */
3942psa_status_t psa_aead_set_lengths( psa_aead_operation_t *operation,
3943 size_t ad_length,
3944 size_t plaintext_length )
3945{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003946 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3947
Paul Elliottcee785c2021-05-20 14:29:20 +01003948 if( operation->id == 0 )
3949 {
3950 status = PSA_ERROR_BAD_STATE;
3951 goto exit;
3952 }
3953
Paul Elliott6eb95982021-05-21 17:41:41 +01003954 if( operation->lengths_set || operation->ad_started ||
Paul Elliott7f429b72021-06-24 18:08:54 +01003955 operation->body_started )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003956 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003957 status = PSA_ERROR_BAD_STATE;
3958 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003959 }
3960
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003961 switch(operation->alg)
Paul Elliott325d3742021-09-27 17:56:28 +01003962 {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003963#if defined(PSA_WANT_ALG_GCM)
3964 case PSA_ALG_GCM:
3965 /* Lengths can only be too large for GCM if size_t is bigger than 32
3966 * bits. Without the guard this code will generate warnings on 32bit
3967 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01003968#if SIZE_MAX > UINT32_MAX
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003969 if( (( uint64_t ) ad_length ) >> 61 != 0 ||
3970 (( uint64_t ) plaintext_length ) > 0xFFFFFFFE0ull )
3971 {
3972 status = PSA_ERROR_INVALID_ARGUMENT;
3973 goto exit;
3974 }
Paul Elliott325d3742021-09-27 17:56:28 +01003975#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003976 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003977#endif /* PSA_WANT_ALG_GCM */
3978#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003979 case PSA_ALG_CCM:
3980 if( ad_length > 0xFF00 )
3981 {
3982 status = PSA_ERROR_INVALID_ARGUMENT;
3983 goto exit;
3984 }
3985 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003986#endif /* PSA_WANT_ALG_CCM */
3987#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003988 case PSA_ALG_CHACHA20_POLY1305:
3989 /* No length restrictions for ChaChaPoly. */
3990 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003991#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003992 default:
3993 break;
3994 }
Paul Elliott325d3742021-09-27 17:56:28 +01003995
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003996 status = psa_driver_wrapper_aead_set_lengths( operation, ad_length,
3997 plaintext_length );
3998
Paul Elliott39dc6b82021-05-11 19:16:09 +01003999exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004000 if( status == PSA_SUCCESS )
Paul Elliottee4ffe02021-05-20 17:25:06 +01004001 {
4002 operation->ad_remaining = ad_length;
4003 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004004 operation->lengths_set = 1;
Paul Elliottee4ffe02021-05-20 17:25:06 +01004005 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004006 else
4007 psa_aead_abort( operation );
4008
4009 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004010}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004011
4012/* Pass additional data to an active multipart AEAD operation. */
Paul Elliott302ff6b2021-04-20 18:10:30 +01004013psa_status_t psa_aead_update_ad( psa_aead_operation_t *operation,
4014 const uint8_t *input,
4015 size_t input_length )
4016{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4018
Paul Elliottcee785c2021-05-20 14:29:20 +01004019 if( operation->id == 0 )
4020 {
4021 status = PSA_ERROR_BAD_STATE;
4022 goto exit;
4023 }
4024
Paul Elliott6eb95982021-05-21 17:41:41 +01004025 if( !operation->nonce_set || operation->body_started )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004026 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004027 status = PSA_ERROR_BAD_STATE;
4028 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004029 }
4030
Paul Elliottee4ffe02021-05-20 17:25:06 +01004031 if( operation->lengths_set )
4032 {
Paul Elliott6eb95982021-05-21 17:41:41 +01004033 if( operation->ad_remaining < input_length )
Paul Elliottee4ffe02021-05-20 17:25:06 +01004034 {
4035 status = PSA_ERROR_INVALID_ARGUMENT;
4036 goto exit;
4037 }
4038
4039 operation->ad_remaining -= input_length;
4040 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004041#if defined(PSA_WANT_ALG_CCM)
4042 else if( operation->alg == PSA_ALG_CCM )
4043 {
4044 status = PSA_ERROR_BAD_STATE;
4045 goto exit;
4046 }
4047#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004048
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004049 status = psa_driver_wrapper_aead_update_ad( operation, input,
4050 input_length );
4051
Paul Elliott39dc6b82021-05-11 19:16:09 +01004052exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004053 if( status == PSA_SUCCESS )
4054 operation->ad_started = 1;
4055 else
4056 psa_aead_abort( operation );
4057
4058 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004059}
4060
4061/* Encrypt or decrypt a message fragment in an active multipart AEAD
4062 operation.*/
4063psa_status_t psa_aead_update( psa_aead_operation_t *operation,
4064 const uint8_t *input,
4065 size_t input_length,
4066 uint8_t *output,
4067 size_t output_size,
4068 size_t *output_length )
4069{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004070 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004071
4072 *output_length = 0;
4073
Paul Elliottcee785c2021-05-20 14:29:20 +01004074 if( operation->id == 0 )
4075 {
4076 status = PSA_ERROR_BAD_STATE;
4077 goto exit;
4078 }
4079
Paul Elliott6eb95982021-05-21 17:41:41 +01004080 if( !operation->nonce_set )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004081 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004082 status = PSA_ERROR_BAD_STATE;
4083 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004084 }
4085
Paul Elliottee4ffe02021-05-20 17:25:06 +01004086 if( operation->lengths_set )
4087 {
4088 /* Additional data length was supplied, but not all the additional
4089 data was supplied.*/
4090 if( operation->ad_remaining != 0 )
4091 {
4092 status = PSA_ERROR_INVALID_ARGUMENT;
4093 goto exit;
4094 }
4095
4096 /* Too much data provided. */
4097 if( operation->body_remaining < input_length )
4098 {
4099 status = PSA_ERROR_INVALID_ARGUMENT;
4100 goto exit;
4101 }
4102
4103 operation->body_remaining -= input_length;
4104 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004105#if defined(PSA_WANT_ALG_CCM)
4106 else if( operation->alg == PSA_ALG_CCM )
4107 {
4108 status = PSA_ERROR_BAD_STATE;
4109 goto exit;
4110 }
4111#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004112
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004113 status = psa_driver_wrapper_aead_update( operation, input, input_length,
4114 output, output_size,
4115 output_length );
4116
Paul Elliott39dc6b82021-05-11 19:16:09 +01004117exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004118 if( status == PSA_SUCCESS )
4119 operation->body_started = 1;
4120 else
4121 psa_aead_abort( operation );
4122
4123 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004124}
4125
Paul Elliott69bf5fc2021-09-19 18:26:37 +01004126static psa_status_t psa_aead_final_checks( const psa_aead_operation_t *operation )
Paul Elliottad53dcc2021-06-23 08:50:14 +01004127{
Paul Elliott7f429b72021-06-24 18:08:54 +01004128 if( operation->id == 0 || !operation->nonce_set )
Paul Elliottad53dcc2021-06-23 08:50:14 +01004129 return( PSA_ERROR_BAD_STATE );
4130
Paul Elliotta5614442021-07-14 14:54:11 +01004131 if( operation->lengths_set && ( operation->ad_remaining != 0 ||
Paul Elliottad53dcc2021-06-23 08:50:14 +01004132 operation->body_remaining != 0 ) )
4133 return( PSA_ERROR_INVALID_ARGUMENT );
4134
4135 return( PSA_SUCCESS );
4136}
4137
Paul Elliott302ff6b2021-04-20 18:10:30 +01004138/* Finish encrypting a message in a multipart AEAD operation. */
4139psa_status_t psa_aead_finish( psa_aead_operation_t *operation,
4140 uint8_t *ciphertext,
4141 size_t ciphertext_size,
4142 size_t *ciphertext_length,
4143 uint8_t *tag,
4144 size_t tag_size,
4145 size_t *tag_length )
4146{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004147 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4148
Paul Elliott302ff6b2021-04-20 18:10:30 +01004149 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004150 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004151
Paul Elliott814fffb2021-08-16 18:20:36 +01004152 status = psa_aead_final_checks( operation );
Paul Elliottad53dcc2021-06-23 08:50:14 +01004153 if( status != PSA_SUCCESS )
4154 goto exit;
4155
Paul Elliott7f429b72021-06-24 18:08:54 +01004156 if( !operation->is_encrypt )
Paul Elliottcee785c2021-05-20 14:29:20 +01004157 {
4158 status = PSA_ERROR_BAD_STATE;
4159 goto exit;
4160 }
4161
Paul Elliott39dc6b82021-05-11 19:16:09 +01004162 status = psa_driver_wrapper_aead_finish( operation, ciphertext,
4163 ciphertext_size,
4164 ciphertext_length,
4165 tag, tag_size, tag_length );
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004166
Paul Elliott39dc6b82021-05-11 19:16:09 +01004167exit:
Paul Elliottf47b0952021-05-21 18:02:33 +01004168 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004169 * the zero tag size, make sure the tag is set to something implausible.
4170 * Even if the operation succeeds, make sure we clear the rest of the
4171 * buffer to prevent potential leakage of anything previously placed in
4172 * the same buffer.*/
Paul Elliott90fdc112021-09-22 17:15:48 +01004173 if( tag != NULL )
Paul Elliottec95cc92021-09-19 22:33:09 +01004174 {
4175 if( status != PSA_SUCCESS )
4176 memset( tag, '!', tag_size );
4177 else if( *tag_length < tag_size )
4178 memset( tag + *tag_length, '!', ( tag_size - *tag_length ) );
4179 }
Paul Elliottf47b0952021-05-21 18:02:33 +01004180
Paul Elliott39dc6b82021-05-11 19:16:09 +01004181 psa_aead_abort( operation );
4182
4183 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004184}
4185
4186/* Finish authenticating and decrypting a message in a multipart AEAD
4187 operation.*/
4188psa_status_t psa_aead_verify( psa_aead_operation_t *operation,
4189 uint8_t *plaintext,
4190 size_t plaintext_size,
4191 size_t *plaintext_length,
4192 const uint8_t *tag,
4193 size_t tag_length )
4194{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004195 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4196
Paul Elliott302ff6b2021-04-20 18:10:30 +01004197 *plaintext_length = 0;
4198
Paul Elliott814fffb2021-08-16 18:20:36 +01004199 status = psa_aead_final_checks( operation );
Paul Elliottad53dcc2021-06-23 08:50:14 +01004200 if( status != PSA_SUCCESS )
4201 goto exit;
4202
Paul Elliott7f429b72021-06-24 18:08:54 +01004203 if( operation->is_encrypt )
Paul Elliottcee785c2021-05-20 14:29:20 +01004204 {
4205 status = PSA_ERROR_BAD_STATE;
4206 goto exit;
4207 }
4208
Paul Elliott39dc6b82021-05-11 19:16:09 +01004209 status = psa_driver_wrapper_aead_verify( operation, plaintext,
4210 plaintext_size,
4211 plaintext_length,
4212 tag, tag_length );
4213
4214exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004215 psa_aead_abort( operation );
4216
4217 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004218}
4219
4220/* Abort an AEAD operation. */
Paul Elliottbbe90b52021-05-11 22:22:42 +01004221psa_status_t psa_aead_abort( psa_aead_operation_t *operation )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004222{
4223 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4224
4225 if( operation->id == 0 )
4226 {
4227 /* The object has (apparently) been initialized but it is not (yet)
4228 * in use. It's ok to call abort on such an object, and there's
4229 * nothing to do. */
4230 return( PSA_SUCCESS );
4231 }
4232
4233 status = psa_driver_wrapper_aead_abort( operation );
4234
Paul Elliott70618b22021-09-22 17:12:16 +01004235 memset( operation, 0, sizeof( *operation ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004236
4237 return( status );
4238}
4239
Gilles Peskinee59236f2018-01-27 23:32:46 +01004240/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004241/* Generators */
4242/****************************************************************/
4243
Przemek Stekiel69c46792022-06-10 12:59:51 +02004244#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004245 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4246 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4247#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004248#endif /* At least one builtin KDF */
4249
Przemek Stekiel69c46792022-06-10 12:59:51 +02004250#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004251 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4252 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4253static psa_status_t psa_key_derivation_start_hmac(
4254 psa_mac_operation_t *operation,
4255 psa_algorithm_t hash_alg,
4256 const uint8_t *hmac_key,
4257 size_t hmac_key_length )
4258{
4259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4260 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4261 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
4262 psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) );
4263 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
4264
Steven Cooreman72f736a2021-05-07 14:14:37 +02004265 operation->is_sign = 1;
4266 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
4267
Steven Cooreman094a77e2021-05-06 17:58:36 +02004268 status = psa_driver_wrapper_mac_sign_setup( operation,
4269 &attributes,
4270 hmac_key, hmac_key_length,
4271 PSA_ALG_HMAC( hash_alg ) );
4272
4273 psa_reset_key_attributes( &attributes );
4274 return( status );
4275}
4276#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004277
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004278#define HKDF_STATE_INIT 0 /* no input yet */
4279#define HKDF_STATE_STARTED 1 /* got salt */
4280#define HKDF_STATE_KEYED 2 /* got key */
4281#define HKDF_STATE_OUTPUT 3 /* output started */
4282
Gilles Peskinecbe66502019-05-16 16:59:18 +02004283static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004284 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004285{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004286 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4287 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004288 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004289 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004290}
4291
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004292psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004293{
4294 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004295 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004296 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004297 {
4298 /* The object has (apparently) been initialized but it is not
4299 * in use. It's ok to call abort on such an object, and there's
4300 * nothing to do. */
4301 }
4302 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004303#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiela29b4882022-06-02 11:37:03 +02004304 if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004305 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004306 mbedtls_free( operation->ctx.hkdf.info );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004307 status = psa_mac_abort( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004308 }
John Durkop07cc04a2020-11-16 22:08:34 -08004309 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004310#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004311#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4312 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4313 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004314 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004315 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004316 {
Steven Cooremana6df6042021-04-29 19:32:25 +02004317 if( operation->ctx.tls12_prf.secret != NULL )
4318 {
4319 mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret,
4320 operation->ctx.tls12_prf.secret_length );
4321 mbedtls_free( operation->ctx.tls12_prf.secret );
4322 }
4323
Janos Follath6a1d2622019-06-11 10:37:28 +01004324 if( operation->ctx.tls12_prf.seed != NULL )
4325 {
4326 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4327 operation->ctx.tls12_prf.seed_length );
4328 mbedtls_free( operation->ctx.tls12_prf.seed );
4329 }
4330
4331 if( operation->ctx.tls12_prf.label != NULL )
4332 {
4333 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4334 operation->ctx.tls12_prf.label_length );
4335 mbedtls_free( operation->ctx.tls12_prf.label );
4336 }
4337
Przemek Stekiele3ee2212022-04-07 14:29:56 +02004338 if( operation->ctx.tls12_prf.other_secret != NULL )
4339 {
4340 mbedtls_platform_zeroize( operation->ctx.tls12_prf.other_secret,
4341 operation->ctx.tls12_prf.other_secret_length );
4342 mbedtls_free( operation->ctx.tls12_prf.other_secret );
4343 }
4344
Steven Cooremana6df6042021-04-29 19:32:25 +02004345 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004346
4347 /* We leave the fields Ai and output_block to be erased safely by the
4348 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004349 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004350 else
John Durkop07cc04a2020-11-16 22:08:34 -08004351#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4352 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004353 {
4354 status = PSA_ERROR_BAD_STATE;
4355 }
Janos Follath083036a2019-06-11 10:22:26 +01004356 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004357 return( status );
4358}
4359
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004360psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004361 size_t *capacity)
4362{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004364 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004365 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004366 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004367 }
4368
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004369 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004370 return( PSA_SUCCESS );
4371}
4372
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004374 size_t capacity )
4375{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004376 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004377 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004378 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004379 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004380 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004381 return( PSA_SUCCESS );
4382}
4383
Przemek Stekiel69c46792022-06-10 12:59:51 +02004384#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004385/* Read some bytes from an HKDF-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004386static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004387 psa_algorithm_t kdf_alg,
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004388 uint8_t *output,
4389 size_t output_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004390{
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004391 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004392 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004393 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004394 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004395#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004396 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004397#else
4398 const uint8_t last_block = 0xff;
4399#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004400
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004401 if( hkdf->state < HKDF_STATE_KEYED ||
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004402 ( !hkdf->info_set
4403#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
4404 && !PSA_ALG_IS_HKDF_EXTRACT( kdf_alg )
4405#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
4406 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004407 return( PSA_ERROR_BAD_STATE );
4408 hkdf->state = HKDF_STATE_OUTPUT;
4409
Gilles Peskinebef7f142018-07-12 17:22:21 +02004410 while( output_length != 0 )
4411 {
4412 /* Copy what remains of the current block */
4413 uint8_t n = hash_length - hkdf->offset_in_block;
4414 if( n > output_length )
4415 n = (uint8_t) output_length;
4416 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4417 output += n;
4418 output_length -= n;
4419 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004420 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004421 break;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004422 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004423 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004425 * object was corrupted or if this function is called directly
4426 * inside the library. */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004427 if( hkdf->block_number == last_block )
Gilles Peskined54931c2018-07-17 21:06:59 +02004428 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004429
4430 /* We need a new block */
4431 ++hkdf->block_number;
4432 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004433
Steven Cooreman094a77e2021-05-06 17:58:36 +02004434 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4435 hash_alg,
4436 hkdf->prk,
4437 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004438 if( status != PSA_SUCCESS )
4439 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004440
Gilles Peskinebef7f142018-07-12 17:22:21 +02004441 if( hkdf->block_number != 1 )
4442 {
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004443 status = psa_mac_update( &hkdf->hmac,
4444 hkdf->output_block,
4445 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004446 if( status != PSA_SUCCESS )
4447 return( status );
4448 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004449 status = psa_mac_update( &hkdf->hmac,
4450 hkdf->info,
4451 hkdf->info_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004452 if( status != PSA_SUCCESS )
4453 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004454 status = psa_mac_update( &hkdf->hmac,
4455 &hkdf->block_number, 1 );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004456 if( status != PSA_SUCCESS )
4457 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004458 status = psa_mac_sign_finish( &hkdf->hmac,
4459 hkdf->output_block,
4460 sizeof( hkdf->output_block ),
4461 &hmac_output_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004462 if( status != PSA_SUCCESS )
4463 return( status );
4464 }
4465
4466 return( PSA_SUCCESS );
4467}
Przemek Stekiel69c46792022-06-10 12:59:51 +02004468#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004469
John Durkop07cc04a2020-11-16 22:08:34 -08004470#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4471 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004472static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4473 psa_tls12_prf_key_derivation_t *tls12_prf,
4474 psa_algorithm_t alg )
4475{
4476 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004477 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremana6df6042021-04-29 19:32:25 +02004478 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4479 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004480 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004481
Janos Follath7742fee2019-06-17 12:58:10 +01004482 /* We can't be wanting more output after block 0xff, otherwise
4483 * the capacity check in psa_key_derivation_output_bytes() would have
4484 * prevented this call. It could happen only if the operation
4485 * object was corrupted or if this function is called directly
4486 * inside the library. */
4487 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004488 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004489
4490 /* We need a new block */
4491 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004492 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004493
4494 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4495 *
4496 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4497 *
4498 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4499 * HMAC_hash(secret, A(2) + seed) +
4500 * HMAC_hash(secret, A(3) + seed) + ...
4501 *
4502 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004503 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004504 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004505 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004506 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004507 * is currently extracted as `output_block` and where i is
4508 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004509 */
4510
Steven Cooreman094a77e2021-05-06 17:58:36 +02004511 status = psa_key_derivation_start_hmac( &hmac,
4512 hash_alg,
4513 tls12_prf->secret,
4514 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004515 if( status != PSA_SUCCESS )
4516 goto cleanup;
4517
4518 /* Calculate A(i) where i = tls12_prf->block_number. */
4519 if( tls12_prf->block_number == 1 )
4520 {
4521 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4522 * the variable seed and in this instance means it in the context of the
4523 * P_hash function, where seed = label + seed.) */
Steven Cooremana6df6042021-04-29 19:32:25 +02004524 status = psa_mac_update( &hmac,
4525 tls12_prf->label,
4526 tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004527 if( status != PSA_SUCCESS )
4528 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004529 status = psa_mac_update( &hmac,
4530 tls12_prf->seed,
4531 tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004532 if( status != PSA_SUCCESS )
4533 goto cleanup;
4534 }
4535 else
4536 {
4537 /* A(i) = HMAC_hash(secret, A(i-1)) */
Steven Cooremana6df6042021-04-29 19:32:25 +02004538 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004539 if( status != PSA_SUCCESS )
4540 goto cleanup;
4541 }
4542
Steven Cooremana6df6042021-04-29 19:32:25 +02004543 status = psa_mac_sign_finish( &hmac,
4544 tls12_prf->Ai, hash_length,
4545 &hmac_output_length );
4546 if( hmac_output_length != hash_length )
4547 status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follathea29bfb2019-06-19 12:21:20 +01004548 if( status != PSA_SUCCESS )
4549 goto cleanup;
4550
4551 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Steven Cooreman094a77e2021-05-06 17:58:36 +02004552 status = psa_key_derivation_start_hmac( &hmac,
4553 hash_alg,
4554 tls12_prf->secret,
4555 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004556 if( status != PSA_SUCCESS )
4557 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004558 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004559 if( status != PSA_SUCCESS )
4560 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004561 status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004562 if( status != PSA_SUCCESS )
4563 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004564 status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004565 if( status != PSA_SUCCESS )
4566 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004567 status = psa_mac_sign_finish( &hmac,
4568 tls12_prf->output_block, hash_length,
4569 &hmac_output_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004570 if( status != PSA_SUCCESS )
4571 goto cleanup;
4572
Janos Follath7742fee2019-06-17 12:58:10 +01004573
4574cleanup:
Steven Cooremana6df6042021-04-29 19:32:25 +02004575 cleanup_status = psa_mac_abort( &hmac );
Janos Follathea29bfb2019-06-19 12:21:20 +01004576 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4577 status = cleanup_status;
4578
4579 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004580}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004581
Janos Follath844eb0e2019-06-19 12:10:49 +01004582static psa_status_t psa_key_derivation_tls12_prf_read(
4583 psa_tls12_prf_key_derivation_t *tls12_prf,
4584 psa_algorithm_t alg,
4585 uint8_t *output,
4586 size_t output_length )
4587{
4588 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004589 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01004590 psa_status_t status;
4591 uint8_t offset, length;
4592
Gilles Peskineb1edaec2021-06-11 22:41:46 +02004593 switch( tls12_prf->state )
4594 {
4595 case PSA_TLS12_PRF_STATE_LABEL_SET:
4596 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4597 break;
4598 case PSA_TLS12_PRF_STATE_OUTPUT:
4599 break;
4600 default:
4601 return( PSA_ERROR_BAD_STATE );
4602 }
4603
Janos Follath844eb0e2019-06-19 12:10:49 +01004604 while( output_length != 0 )
4605 {
4606 /* Check if we have fully processed the current block. */
4607 if( tls12_prf->left_in_block == 0 )
4608 {
4609 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4610 alg );
4611 if( status != PSA_SUCCESS )
4612 return( status );
4613
4614 continue;
4615 }
4616
4617 if( tls12_prf->left_in_block > output_length )
4618 length = (uint8_t) output_length;
4619 else
4620 length = tls12_prf->left_in_block;
4621
4622 offset = hash_length - tls12_prf->left_in_block;
4623 memcpy( output, tls12_prf->output_block + offset, length );
4624 output += length;
4625 output_length -= length;
4626 tls12_prf->left_in_block -= length;
4627 }
4628
4629 return( PSA_SUCCESS );
4630}
John Durkop07cc04a2020-11-16 22:08:34 -08004631#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4632 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004633
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004634psa_status_t psa_key_derivation_output_bytes(
4635 psa_key_derivation_operation_t *operation,
4636 uint8_t *output,
4637 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004638{
4639 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004640 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004641
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004642 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004643 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004644 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004645 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004646 }
4647
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004649 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004650 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004651 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004652 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004653 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004654 goto exit;
4655 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004656 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004657 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004658 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004659 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004660 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4661 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004663 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004664 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004665 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004666 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004667
Przemek Stekiel69c46792022-06-10 12:59:51 +02004668#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiela29b4882022-06-02 11:37:03 +02004669 if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004670 {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004671 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, kdf_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004672 output, output_length );
4673 }
Janos Follathadbec812019-06-14 11:05:39 +01004674 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004675#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004676#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4677 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01004678 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08004679 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004680 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Steven Cooremana6df6042021-04-29 19:32:25 +02004682 kdf_alg, output,
4683 output_length );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004684 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004685 else
John Durkop07cc04a2020-11-16 22:08:34 -08004686#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4687 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004688 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004689 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004690 return( PSA_ERROR_BAD_STATE );
4691 }
4692
4693exit:
4694 if( status != PSA_SUCCESS )
4695 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004696 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004697 * This allows us to differentiate between exhausted operations and
4698 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4699 * operations. */
4700 psa_algorithm_t alg = operation->alg;
4701 psa_key_derivation_abort( operation );
4702 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004703 memset( output, '!', output_length );
4704 }
4705 return( status );
4706}
4707
David Brown7807bf72021-02-09 16:28:23 -07004708#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02004709static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4710{
4711 if( data_size >= 8 )
4712 mbedtls_des_key_set_parity( data );
4713 if( data_size >= 16 )
4714 mbedtls_des_key_set_parity( data + 8 );
4715 if( data_size >= 24 )
4716 mbedtls_des_key_set_parity( data + 16 );
4717}
David Brown7807bf72021-02-09 16:28:23 -07004718#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004719
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004720/*
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +01004721* ECC keys on a Weierstrass elliptic curve require the generation
4722* of a private key which is an integer
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004723* in the range [1, N - 1], where N is the boundary of the private key domain:
4724* N is the prime p for Diffie-Hellman, or the order of the
4725* curve’s base point for ECC.
4726*
4727* Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
4728* This function generates the private key using the following process:
4729*
4730* 1. Draw a byte string of length ceiling(m/8) bytes.
4731* 2. If m is not a multiple of 8, set the most significant
4732* (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
4733* 3. Convert the string to integer k by decoding it as a big-endian byte string.
4734* 4. If k > N - 2, discard the result and return to step 1.
4735* 5. Output k + 1 as the private key.
4736*
4737* This method allows compliance to NIST standards, specifically the methods titled
4738* Key-Pair Generation by Testing Candidates in the following publications:
4739* - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
4740* Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
4741* Diffie-Hellman keys.
4742*
4743* - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
4744* Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004745*
4746* Note: Function allocates memory for *data buffer, so given *data should be
4747* always NULL.
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004748*/
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004749#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4750 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4751 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4752 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
4753 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004754static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004755 psa_key_slot_t *slot,
4756 size_t bits,
4757 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004758 uint8_t **data
4759 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004760{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004761 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004762 mbedtls_mpi k;
4763 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004765 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004766
4767 mbedtls_mpi_init( &k );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004768 mbedtls_mpi_init( &diff_N_2 );
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004769
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004770 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
4771 slot->attr.type );
4772 mbedtls_ecp_group_id grp_id =
4773 mbedtls_ecc_group_of_psa( curve, bits, 0 );
4774
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01004775 if( grp_id == MBEDTLS_ECP_DP_NONE )
4776 {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004777 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01004778 goto cleanup;
4779 }
4780
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004781 mbedtls_ecp_group ecp_group;
Przemyslaw Stekiel65348162021-11-18 11:57:07 +01004782 mbedtls_ecp_group_init( &ecp_group );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004783
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004784 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ecp_group, grp_id ) );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004785
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004786 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004787 /* Let m be the bit size of N. */
4788 size_t m = ecp_group.nbits;
4789
4790 size_t m_bytes = PSA_BITS_TO_BYTES( m );
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004791
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004792 /* Calculate N - 2 - it will be needed later. */
4793 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &diff_N_2, &ecp_group.N, 2 ) );
4794
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004795 /* Note: This function is always called with *data == NULL and it
4796 * allocates memory for the data buffer. */
4797 *data = mbedtls_calloc( 1, m_bytes );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004798 if( *data == NULL )
4799 {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004800 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004801 goto cleanup;
4802 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004803
Przemek Stekielc85f0912022-03-08 11:37:54 +01004804 while( key_out_of_range )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004805 {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004806 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Przemek Stekielc85f0912022-03-08 11:37:54 +01004807 if( ( status = psa_key_derivation_output_bytes( operation, *data, m_bytes ) ) != 0 )
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004808 goto cleanup;
4809
4810 /* 2. If m is not a multiple of 8 */
Przemek Stekielc85f0912022-03-08 11:37:54 +01004811 if( m % 8 != 0 )
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004812 {
4813 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004814 * (8 * ceiling(m/8) - m) bits of the first byte in
4815 * the string to zero.
4816 */
4817 uint8_t clear_bit_mask = ( 1 << ( m % 8 ) ) - 1;
4818 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004819 }
4820
4821 /* 3. Convert the string to integer k by decoding it as a
4822 * big-endian byte string.
4823 */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004824 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &k, *data, m_bytes ) );
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004825
4826 /* 4. If k > N - 2, discard the result and return to step 1.
4827 * Result of comparison is returned. When it indicates error
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004828 * then this function is called again.
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004829 */
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004830 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( &diff_N_2, &k, &key_out_of_range ) );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004831 }
4832
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004833 /* 5. Output k + 1 as the private key. */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004834 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &k, &k, 1 ) );
4835 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &k, *data, m_bytes ) );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004836cleanup:
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004837 if( ret != 0 )
Przemyslaw Stekieldc8d7d92021-12-02 09:15:20 +01004838 status = mbedtls_to_psa_error( ret );
Przemek Stekielc85f0912022-03-08 11:37:54 +01004839 if( status != PSA_SUCCESS ) {
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004840 mbedtls_free( *data );
4841 *data = NULL;
4842 }
4843 mbedtls_mpi_free( &k );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004844 mbedtls_mpi_free( &diff_N_2 );
Przemyslaw Stekieldc8d7d92021-12-02 09:15:20 +01004845 return( status );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004846}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004847
4848/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
4849 * is determined by the curve, and sets the mandatory bits accordingly. That is:
4850 *
4851 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
4852 * draw a 32-byte string and process it as specified in
4853 * Elliptic Curves for Security [RFC7748] §5.
4854 *
4855 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
4856 * draw a 56-byte string and process it as specified in [RFC7748] §5.
4857 *
4858 * Note: Function allocates memory for *data buffer, so given *data should be
4859 * always NULL.
4860 */
4861
4862static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
4863 size_t bits,
4864 psa_key_derivation_operation_t *operation,
4865 uint8_t **data
4866 )
4867{
4868 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004869 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004870
4871 switch( bits )
4872 {
4873 case 255:
4874 output_length = 32;
4875 break;
4876 case 448:
4877 output_length = 56;
4878 break;
4879 default:
4880 return( PSA_ERROR_INVALID_ARGUMENT );
4881 break;
4882 }
4883
4884 *data = mbedtls_calloc( 1, output_length );
4885
4886 if( *data == NULL )
4887 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4888
4889 status = psa_key_derivation_output_bytes( operation, *data, output_length );
4890
4891 if( status != PSA_SUCCESS )
4892 return status;
4893
4894 switch( bits )
4895 {
4896 case 255:
4897 (*data)[0] &= 248;
4898 (*data)[31] &= 127;
4899 (*data)[31] |= 64;
4900 break;
4901 case 448:
4902 (*data)[0] &= 252;
4903 (*data)[55] |= 128;
4904 break;
4905 default:
Przemek Stekiela81aed22022-03-01 15:13:30 +01004906 return( PSA_ERROR_CORRUPTION_DETECTED );
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004907 break;
4908 }
4909
4910 return status;
4911}
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004912#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
4913 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
4914 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4915 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
4916 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004917
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004918static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004919 psa_key_slot_t *slot,
4920 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004921 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004922{
4923 uint8_t *data = NULL;
4924 size_t bytes = PSA_BITS_TO_BYTES( bits );
Archanad8a83dc2021-06-14 10:04:16 +05304925 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004927
Przemek Stekieldcab6cc2022-03-01 14:22:29 +01004928 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004929 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004930
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004931#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4932 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4933 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4934 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
4935 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemek Stekielc85f0912022-03-08 11:37:54 +01004936 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004937 {
Przemyslaw Stekiel92481592022-01-04 10:09:46 +01004938 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type );
Przemek Stekielc85f0912022-03-08 11:37:54 +01004939 if( PSA_ECC_FAMILY_IS_WEIERSTRASS( curve ) )
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004940 {
4941 /* Weierstrass elliptic curve */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004942 status = psa_generate_derived_ecc_key_weierstrass_helper( slot, bits, operation, &data );
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004943 if( status != PSA_SUCCESS )
4944 goto exit;
Przemyslaw Stekiel50fcc532021-11-26 10:54:52 +01004945 }
4946 else
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004947 {
4948 /* Montgomery elliptic curve */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004949 status = psa_generate_derived_ecc_key_montgomery_helper( bits, operation, &data );
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004950 if( status != PSA_SUCCESS )
4951 goto exit;
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004952 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004953 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004954#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
4955 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
4956 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4957 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
4958 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004959 if( key_type_is_raw_bytes( slot->attr.type ) )
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004960 {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004961 if( bits % 8 != 0 )
4962 return( PSA_ERROR_INVALID_ARGUMENT );
4963 data = mbedtls_calloc( 1, bytes );
4964 if( data == NULL )
4965 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4966
4967 status = psa_key_derivation_output_bytes( operation, data, bytes );
4968 if( status != PSA_SUCCESS )
4969 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07004970#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004971 if( slot->attr.type == PSA_KEY_TYPE_DES )
4972 psa_des_set_key_parity( data, bytes );
Przemek Stekielf110dc02022-03-01 14:48:05 +01004973#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004974 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004975 else
Przemek Stekieldcab6cc2022-03-01 14:22:29 +01004976 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Crondd04d422020-11-28 15:14:42 +01004977
Ronald Cronbf33c932020-11-28 18:06:53 +01004978 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004979 psa_key_attributes_t attributes = {
4980 .core = slot->attr
4981 };
4982
Archanad8a83dc2021-06-14 10:04:16 +05304983 if( psa_key_lifetime_is_external( attributes.core.lifetime ) )
4984 {
Archana449608b2021-09-08 15:36:05 +05304985 status = psa_driver_wrapper_get_key_buffer_size( &attributes,
4986 &storage_size );
Archanad8a83dc2021-06-14 10:04:16 +05304987 if( status != PSA_SUCCESS )
4988 goto exit;
4989 }
4990 status = psa_allocate_buffer_to_slot( slot, storage_size );
4991 if( status != PSA_SUCCESS )
4992 goto exit;
4993
Ronald Cronbf33c932020-11-28 18:06:53 +01004994 status = psa_driver_wrapper_import_key( &attributes,
4995 data, bytes,
4996 slot->key.data,
4997 slot->key.bytes,
4998 &slot->key.bytes, &bits );
4999 if( bits != slot->attr.bits )
5000 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005001
5002exit:
5003 mbedtls_free( data );
5004 return( status );
5005}
5006
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005007psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005008 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005009 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005010{
5011 psa_status_t status;
5012 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005013 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005014
Ronald Cron81709fc2020-11-14 12:10:32 +01005015 *key = MBEDTLS_SVC_KEY_ID_INIT;
5016
Gilles Peskine0f84d622019-09-12 19:03:13 +02005017 /* Reject any attempt to create a zero-length key so that we don't
5018 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5019 if( psa_get_key_bits( attributes ) == 0 )
5020 return( PSA_ERROR_INVALID_ARGUMENT );
5021
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005022 if( operation->alg == PSA_ALG_NONE )
5023 return( PSA_ERROR_BAD_STATE );
5024
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005025 if( ! operation->can_output_key )
5026 return( PSA_ERROR_NOT_PERMITTED );
5027
Ronald Cron81709fc2020-11-14 12:10:32 +01005028 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5029 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005030#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5031 if( driver != NULL )
5032 {
5033 /* Deriving a key in a secure element is not implemented yet. */
5034 status = PSA_ERROR_NOT_SUPPORTED;
5035 }
5036#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005037 if( status == PSA_SUCCESS )
5038 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005039 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005040 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005041 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005042 }
5043 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005044 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005045 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005046 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005047
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005048 return( status );
5049}
5050
Gilles Peskineeab56e42018-07-12 17:12:33 +02005051
5052
5053/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005054/* Key derivation */
5055/****************************************************************/
5056
Steven Cooreman932ffb72021-02-15 12:14:32 +01005057#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005058static int is_kdf_alg_supported( psa_algorithm_t kdf_alg )
5059{
5060#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
5061 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5062 return( 1 );
5063#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005064#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
5065 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5066 return( 1 );
5067#endif
5068#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
5069 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) )
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005070 return( 1 );
5071#endif
5072#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5073 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5074 return( 1 );
5075#endif
5076#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5077 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5078 return( 1 );
5079#endif
5080 return( 0 );
5081}
5082
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005083static psa_status_t psa_hash_try_support( psa_algorithm_t alg )
5084{
5085 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
5086 psa_status_t status = psa_hash_setup( &operation, alg );
5087 psa_hash_abort( &operation );
5088 return( status );
5089}
5090
Gilles Peskine969c5d62019-01-16 15:53:06 +01005091static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005092 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005093 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005094{
Janos Follath5fe19732019-06-20 15:09:30 +01005095 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5096 * initialisers for this union leave some bytes unspecified.) */
5097 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5098
Gilles Peskine969c5d62019-01-16 15:53:06 +01005099 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005100 if( ! is_kdf_alg_supported( kdf_alg ) )
5101 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop07cc04a2020-11-16 22:08:34 -08005102
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005103 /* All currently supported key derivation algorithms are based on a
5104 * hash algorithm. */
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005105 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
5106 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
5107 if( hash_size == 0 )
5108 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005109
5110 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5111 * we might fail later, which is somewhat unfriendly and potentially
5112 * risk-prone. */
5113 psa_status_t status = psa_hash_try_support( hash_alg );
5114 if( status != PSA_SUCCESS )
5115 return( status );
5116
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005117 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5118 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
5119 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005120 {
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005121 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005122 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005123#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005124 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5125 operation->capacity = hash_size;
5126 else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005127#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005128 operation->capacity = 255 * hash_size;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005129 return( PSA_SUCCESS );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005130}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005131
5132static psa_status_t psa_key_agreement_try_support( psa_algorithm_t alg )
5133{
5134#if defined(PSA_WANT_ALG_ECDH)
5135 if( alg == PSA_ALG_ECDH )
5136 return( PSA_SUCCESS );
5137#endif
5138 (void) alg;
5139 return( PSA_ERROR_NOT_SUPPORTED );
5140}
John Durkop07cc04a2020-11-16 22:08:34 -08005141#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005142
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005143psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005144 psa_algorithm_t alg )
5145{
5146 psa_status_t status;
5147
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005149 return( PSA_ERROR_BAD_STATE );
5150
Gilles Peskine6843c292019-01-18 16:44:49 +01005151 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5152 return( PSA_ERROR_INVALID_ARGUMENT );
5153 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005154 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005155#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005156 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005157 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( alg );
5158 status = psa_key_agreement_try_support( ka_alg );
5159 if( status != PSA_SUCCESS )
5160 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005161 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005162#else
5163 return( PSA_ERROR_NOT_SUPPORTED );
5164#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005165 }
5166 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5167 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005168#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005169 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005170#else
5171 return( PSA_ERROR_NOT_SUPPORTED );
5172#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005173 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005174 else
5175 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005176
5177 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005178 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005179 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180}
5181
Przemek Stekiel69c46792022-06-10 12:59:51 +02005182#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005183static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005184 psa_algorithm_t kdf_alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005185 psa_key_derivation_step_t step,
5186 const uint8_t *data,
5187 size_t data_length )
5188{
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005189 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005190 psa_status_t status;
5191 switch( step )
5192 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005193 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005194#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005195 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) )
5196 return( PSA_ERROR_INVALID_ARGUMENT );
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005197#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine2b522db2019-04-12 15:11:49 +02005198 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005199 return( PSA_ERROR_BAD_STATE );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005200 else
5201 {
Steven Cooreman094a77e2021-05-06 17:58:36 +02005202 status = psa_key_derivation_start_hmac( &hkdf->hmac,
5203 hash_alg,
5204 data, data_length );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005205 if( status != PSA_SUCCESS )
5206 return( status );
5207 hkdf->state = HKDF_STATE_STARTED;
5208 return( PSA_SUCCESS );
5209 }
Gilles Peskine03410b52019-05-16 16:05:19 +02005210 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005211#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005212 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005213 {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02005214 /* We shouldn't be in different state as HKDF_EXPAND only allows
5215 * two inputs: SECRET (this case) and INFO which does not modify
5216 * the state. It could happen only if the hkdf
5217 * object was corrupted. */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005218 if( hkdf->state != HKDF_STATE_INIT )
5219 return( PSA_ERROR_BAD_STATE );
5220
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02005221 /* Allow only input that fits expected prk size */
5222 if( data_length != PSA_HASH_LENGTH( hash_alg ) )
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005223 return( PSA_ERROR_INVALID_ARGUMENT );
5224
5225 memcpy( hkdf->prk, data, data_length );
5226 }
5227 else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005228#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005229 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005230 /* HKDF: If no salt was provided, use an empty salt.
5231 * HKDF-EXTRACT: salt is mandatory. */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005232 if( hkdf->state == HKDF_STATE_INIT )
5233 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005234#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
5235 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5236 return( PSA_ERROR_BAD_STATE );
5237#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005238 status = psa_key_derivation_start_hmac( &hkdf->hmac,
5239 hash_alg,
5240 NULL, 0 );
5241 if( status != PSA_SUCCESS )
5242 return( status );
5243 hkdf->state = HKDF_STATE_STARTED;
5244 }
5245 if( hkdf->state != HKDF_STATE_STARTED )
5246 return( PSA_ERROR_BAD_STATE );
5247 status = psa_mac_update( &hkdf->hmac,
5248 data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005249 if( status != PSA_SUCCESS )
5250 return( status );
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005251 status = psa_mac_sign_finish( &hkdf->hmac,
5252 hkdf->prk,
5253 sizeof( hkdf->prk ),
5254 &data_length );
5255 if( status != PSA_SUCCESS )
5256 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005257 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005258
Gilles Peskine2b522db2019-04-12 15:11:49 +02005259 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005260 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005261#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005262 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5263 {
5264 /* The only block of output is the PRK. */
5265 memcpy( hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH( hash_alg ) );
5266 hkdf->offset_in_block = 0;
5267 }
5268 else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005269#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005270 {
5271 /* Block 0 is empty, and the next block will be
5272 * generated by psa_key_derivation_hkdf_read(). */
5273 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
5274 }
5275
Gilles Peskine2b522db2019-04-12 15:11:49 +02005276 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005277 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005278#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005279 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5280 return( PSA_ERROR_INVALID_ARGUMENT );
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005281#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02005282#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
5283 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) &&
5284 hkdf->state == HKDF_STATE_INIT )
5285 return( PSA_ERROR_BAD_STATE );
5286#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005287 if( hkdf->state == HKDF_STATE_OUTPUT )
5288 return( PSA_ERROR_BAD_STATE );
5289 if( hkdf->info_set )
5290 return( PSA_ERROR_BAD_STATE );
5291 hkdf->info_length = data_length;
5292 if( data_length != 0 )
5293 {
5294 hkdf->info = mbedtls_calloc( 1, data_length );
5295 if( hkdf->info == NULL )
5296 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5297 memcpy( hkdf->info, data, data_length );
5298 }
5299 hkdf->info_set = 1;
5300 return( PSA_SUCCESS );
5301 default:
5302 return( PSA_ERROR_INVALID_ARGUMENT );
5303 }
5304}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005305#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005306
John Durkop07cc04a2020-11-16 22:08:34 -08005307#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5308 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005309static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5310 const uint8_t *data,
5311 size_t data_length )
5312{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005313 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005314 return( PSA_ERROR_BAD_STATE );
5315
Janos Follathd6dce9f2019-07-04 09:11:38 +01005316 if( data_length != 0 )
5317 {
5318 prf->seed = mbedtls_calloc( 1, data_length );
5319 if( prf->seed == NULL )
5320 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005321
Janos Follathd6dce9f2019-07-04 09:11:38 +01005322 memcpy( prf->seed, data, data_length );
5323 prf->seed_length = data_length;
5324 }
Janos Follathf08e2652019-06-13 09:05:41 +01005325
Gilles Peskine43f958b2020-12-13 14:55:14 +01005326 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005327
5328 return( PSA_SUCCESS );
5329}
5330
Janos Follath81550542019-06-13 14:26:34 +01005331static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
Janos Follath81550542019-06-13 14:26:34 +01005332 const uint8_t *data,
5333 size_t data_length )
5334{
Przemek Stekield7a28642022-04-07 14:58:33 +02005335 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
5336 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET )
Janos Follath81550542019-06-13 14:26:34 +01005337 return( PSA_ERROR_BAD_STATE );
5338
Steven Cooremana6df6042021-04-29 19:32:25 +02005339 if( data_length != 0 )
5340 {
5341 prf->secret = mbedtls_calloc( 1, data_length );
5342 if( prf->secret == NULL )
5343 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5344
5345 memcpy( prf->secret, data, data_length );
5346 prf->secret_length = data_length;
5347 }
Janos Follath81550542019-06-13 14:26:34 +01005348
Gilles Peskine43f958b2020-12-13 14:55:14 +01005349 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005350
5351 return( PSA_SUCCESS );
5352}
5353
Janos Follath63028dd2019-06-13 09:15:47 +01005354static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5355 const uint8_t *data,
5356 size_t data_length )
5357{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005358 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005359 return( PSA_ERROR_BAD_STATE );
5360
Janos Follathd6dce9f2019-07-04 09:11:38 +01005361 if( data_length != 0 )
5362 {
5363 prf->label = mbedtls_calloc( 1, data_length );
5364 if( prf->label == NULL )
5365 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005366
Janos Follathd6dce9f2019-07-04 09:11:38 +01005367 memcpy( prf->label, data, data_length );
5368 prf->label_length = data_length;
5369 }
Janos Follath63028dd2019-06-13 09:15:47 +01005370
Gilles Peskine43f958b2020-12-13 14:55:14 +01005371 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005372
5373 return( PSA_SUCCESS );
5374}
5375
Janos Follathb03233e2019-06-11 15:30:30 +01005376static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
Janos Follathb03233e2019-06-11 15:30:30 +01005377 psa_key_derivation_step_t step,
5378 const uint8_t *data,
5379 size_t data_length )
5380{
Janos Follathb03233e2019-06-11 15:30:30 +01005381 switch( step )
5382 {
Janos Follathf08e2652019-06-13 09:05:41 +01005383 case PSA_KEY_DERIVATION_INPUT_SEED:
5384 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005385 case PSA_KEY_DERIVATION_INPUT_SECRET:
Steven Cooremana6df6042021-04-29 19:32:25 +02005386 return( psa_tls12_prf_set_key( prf, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005387 case PSA_KEY_DERIVATION_INPUT_LABEL:
5388 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005389 default:
5390 return( PSA_ERROR_INVALID_ARGUMENT );
5391 }
5392}
John Durkop07cc04a2020-11-16 22:08:34 -08005393#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5394 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5395
5396#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5397static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5398 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005399 const uint8_t *data,
5400 size_t data_length )
5401{
5402 psa_status_t status;
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005403 const size_t pms_len = ( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
5404 4 + data_length + prf->other_secret_length :
5405 4 + 2 * data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08005406
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005407 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005408 return( PSA_ERROR_INVALID_ARGUMENT );
5409
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005410 uint8_t *pms = mbedtls_calloc( 1, pms_len );
Przemek Stekiel937b90f2022-04-20 08:33:13 +02005411 if( pms == NULL )
5412 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005413 uint8_t *cur = pms;
5414
5415 /* pure-PSK:
5416 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08005417 *
5418 * The premaster secret is formed as follows: if the PSK is N octets
5419 * long, concatenate a uint16 with the value N, N zero octets, a second
5420 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005421 *
5422 * mixed-PSK:
5423 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
5424 * follows: concatenate a uint16 with the length of the other secret,
5425 * the other secret itself, uint16 with the length of PSK, and the
5426 * PSK itself.
5427 * For details please check:
5428 * - RFC 4279, Section 4 for the definition of RSA-PSK,
5429 * - RFC 4279, Section 3 for the definition of DHE-PSK,
5430 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08005431 */
5432
Przemek Stekiel4e47a912022-04-21 11:40:18 +02005433 if( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET )
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005434 {
5435 *cur++ = MBEDTLS_BYTE_1( prf->other_secret_length );
5436 *cur++ = MBEDTLS_BYTE_0( prf->other_secret_length );
Przemek Stekiel4e47a912022-04-21 11:40:18 +02005437 if( prf->other_secret_length != 0 )
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02005438 {
5439 memcpy( cur, prf->other_secret, prf->other_secret_length );
Przemek Stekiel03faf5d22022-04-20 08:37:43 +02005440 mbedtls_platform_zeroize( prf->other_secret, prf->other_secret_length );
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02005441 cur += prf->other_secret_length;
5442 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005443 }
5444 else
5445 {
5446 *cur++ = MBEDTLS_BYTE_1( data_length );
5447 *cur++ = MBEDTLS_BYTE_0( data_length );
5448 memset( cur, 0, data_length );
5449 cur += data_length;
5450 }
5451
Joe Subbiani5ecac212021-06-24 13:00:03 +01005452 *cur++ = MBEDTLS_BYTE_1( data_length );
5453 *cur++ = MBEDTLS_BYTE_0( data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08005454 memcpy( cur, data, data_length );
5455 cur += data_length;
5456
Steven Cooremana6df6042021-04-29 19:32:25 +02005457 status = psa_tls12_prf_set_key( prf, pms, cur - pms );
John Durkop07cc04a2020-11-16 22:08:34 -08005458
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005459 mbedtls_platform_zeroize( pms, pms_len );
5460 mbedtls_free( pms );
John Durkop07cc04a2020-11-16 22:08:34 -08005461 return( status );
5462}
Janos Follath6660f0e2019-06-17 08:44:03 +01005463
Przemek Stekiele47201b2022-04-19 13:53:28 +02005464static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
5465 psa_tls12_prf_key_derivation_t *prf,
5466 const uint8_t *data,
5467 size_t data_length )
5468{
5469 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
5470 return( PSA_ERROR_BAD_STATE );
5471
5472 if( data_length != 0 )
5473 {
5474 prf->other_secret = mbedtls_calloc( 1, data_length );
5475 if( prf->other_secret == NULL )
5476 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5477
5478 memcpy( prf->other_secret, data, data_length );
5479 prf->other_secret_length = data_length;
5480 }
5481 else
5482 {
5483 prf->other_secret_length = 0;
5484 }
5485
5486 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
5487
5488 return( PSA_SUCCESS );
5489}
5490
Janos Follath6660f0e2019-06-17 08:44:03 +01005491static psa_status_t psa_tls12_prf_psk_to_ms_input(
5492 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005493 psa_key_derivation_step_t step,
5494 const uint8_t *data,
5495 size_t data_length )
5496{
Przemek Stekiele47201b2022-04-19 13:53:28 +02005497 switch( step )
Janos Follath0c1ed842019-06-28 13:35:36 +01005498 {
Przemek Stekiele47201b2022-04-19 13:53:28 +02005499 case PSA_KEY_DERIVATION_INPUT_SECRET:
5500 return( psa_tls12_prf_psk_to_ms_set_key( prf,
5501 data, data_length ) );
5502 break;
5503 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
5504 return( psa_tls12_prf_psk_to_ms_set_other_key( prf,
5505 data,
5506 data_length ) );
5507 break;
5508 default:
5509 return( psa_tls12_prf_input( prf, step, data, data_length ) );
5510 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01005511
Przemek Stekiele47201b2022-04-19 13:53:28 +02005512 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005513}
John Durkop07cc04a2020-11-16 22:08:34 -08005514#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005515
Gilles Peskineb8965192019-09-24 16:21:10 +02005516/** Check whether the given key type is acceptable for the given
5517 * input step of a key derivation.
5518 *
5519 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5520 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5521 * Both secret and non-secret inputs can alternatively have the type
5522 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5523 * that the input was passed as a buffer rather than via a key object.
5524 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005525static int psa_key_derivation_check_input_type(
5526 psa_key_derivation_step_t step,
5527 psa_key_type_t key_type )
5528{
5529 switch( step )
5530 {
5531 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005532 if( key_type == PSA_KEY_TYPE_DERIVE )
5533 return( PSA_SUCCESS );
5534 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005535 return( PSA_SUCCESS );
5536 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02005537 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
5538 if( key_type == PSA_KEY_TYPE_DERIVE )
5539 return( PSA_SUCCESS );
5540 if( key_type == PSA_KEY_TYPE_NONE )
5541 return( PSA_SUCCESS );
5542 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005543 case PSA_KEY_DERIVATION_INPUT_LABEL:
5544 case PSA_KEY_DERIVATION_INPUT_SALT:
5545 case PSA_KEY_DERIVATION_INPUT_INFO:
5546 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005547 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5548 return( PSA_SUCCESS );
5549 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005550 return( PSA_SUCCESS );
5551 break;
5552 }
5553 return( PSA_ERROR_INVALID_ARGUMENT );
5554}
5555
Janos Follathb80a94e2019-06-12 15:54:46 +01005556static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005557 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005558 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005559 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005560 const uint8_t *data,
5561 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005562{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005563 psa_status_t status;
5564 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5565
5566 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005567 if( status != PSA_SUCCESS )
5568 goto exit;
5569
Przemek Stekiel69c46792022-06-10 12:59:51 +02005570#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiela29b4882022-06-02 11:37:03 +02005571 if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005572 {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005573 status = psa_hkdf_input( &operation->ctx.hkdf, kdf_alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005574 step, data, data_length );
5575 }
John Durkop07cc04a2020-11-16 22:08:34 -08005576 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005577#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005578#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5579 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005580 {
Janos Follathb03233e2019-06-11 15:30:30 +01005581 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
Janos Follathb03233e2019-06-11 15:30:30 +01005582 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005583 }
John Durkop07cc04a2020-11-16 22:08:34 -08005584 else
5585#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5586#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5587 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005588 {
5589 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005590 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005591 }
5592 else
John Durkop07cc04a2020-11-16 22:08:34 -08005593#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005594 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005595 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005596 (void) data;
5597 (void) data_length;
5598 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005599 return( PSA_ERROR_BAD_STATE );
5600 }
5601
Gilles Peskine224b0d62019-09-23 18:13:17 +02005602exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005603 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005604 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005605 return( status );
5606}
5607
Janos Follath51f4a0f2019-06-14 11:35:55 +01005608psa_status_t psa_key_derivation_input_bytes(
5609 psa_key_derivation_operation_t *operation,
5610 psa_key_derivation_step_t step,
5611 const uint8_t *data,
5612 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005613{
Gilles Peskineb8965192019-09-24 16:21:10 +02005614 return( psa_key_derivation_input_internal( operation, step,
5615 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005616 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005617}
5618
Janos Follath51f4a0f2019-06-14 11:35:55 +01005619psa_status_t psa_key_derivation_input_key(
5620 psa_key_derivation_operation_t *operation,
5621 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005622 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005623{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005624 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005625 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005626 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005627
Ronald Cron5c522922020-11-14 16:35:34 +01005628 status = psa_get_and_lock_transparent_key_slot_with_policy(
5629 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005630 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005631 {
5632 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005633 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005634 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005635
5636 /* Passing a key object as a SECRET input unlocks the permission
5637 * to output to a key object. */
5638 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5639 operation->can_output_key = 1;
5640
Ronald Cronf95a2b12020-10-22 15:24:49 +02005641 status = psa_key_derivation_input_internal( operation,
5642 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005643 slot->key.data,
5644 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005645
Ronald Cron5c522922020-11-14 16:35:34 +01005646 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005647
Ronald Cron5c522922020-11-14 16:35:34 +01005648 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005649}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005650
5651
5652
5653/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005654/* Key agreement */
5655/****************************************************************/
5656
John Durkop6ba40d12020-11-10 08:50:04 -08005657#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005658static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5659 size_t peer_key_length,
5660 const mbedtls_ecp_keypair *our_key,
5661 uint8_t *shared_secret,
5662 size_t shared_secret_size,
5663 size_t *shared_secret_length )
5664{
Steven Cooremand4867872020-08-05 16:31:39 +02005665 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005666 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005667 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005668 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005669 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005670 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005671
Ronald Cron90857082020-11-25 14:58:33 +01005672 status = mbedtls_psa_ecp_load_representation(
5673 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005674 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005675 peer_key,
5676 peer_key_length,
5677 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005678 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005679 goto exit;
5680
Jaeden Amero97271b32019-01-10 19:38:51 +00005681 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005682 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005683 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005684 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005685 status = mbedtls_to_psa_error(
5686 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5687 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005688 goto exit;
5689
Jaeden Amero97271b32019-01-10 19:38:51 +00005690 status = mbedtls_to_psa_error(
5691 mbedtls_ecdh_calc_secret( &ecdh,
5692 shared_secret_length,
5693 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005694 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005695 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005696 if( status != PSA_SUCCESS )
5697 goto exit;
5698 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5699 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005700
5701exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005702 if( status != PSA_SUCCESS )
5703 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005704 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005705 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005706 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005707
Jaeden Amero97271b32019-01-10 19:38:51 +00005708 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005709}
John Durkop6ba40d12020-11-10 08:50:04 -08005710#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005711
Gilles Peskine01d718c2018-09-18 12:01:02 +02005712#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5713
Gilles Peskine0216fe12019-04-11 21:23:21 +02005714static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5715 psa_key_slot_t *private_key,
5716 const uint8_t *peer_key,
5717 size_t peer_key_length,
5718 uint8_t *shared_secret,
5719 size_t shared_secret_size,
5720 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005721{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005722 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005723 {
John Durkop6ba40d12020-11-10 08:50:04 -08005724#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005725 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005726 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005727 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005728 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005729 psa_status_t status = mbedtls_psa_ecp_load_representation(
5730 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005731 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005732 private_key->key.data,
5733 private_key->key.bytes,
5734 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005735 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005736 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005737 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005738 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005739 shared_secret, shared_secret_size,
5740 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005741 mbedtls_ecp_keypair_free( ecp );
5742 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005743 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005744#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005745 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005746 (void) private_key;
5747 (void) peer_key;
5748 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005749 (void) shared_secret;
5750 (void) shared_secret_size;
5751 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005752 return( PSA_ERROR_NOT_SUPPORTED );
5753 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005754}
5755
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005756/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005757 * to potentially free embedded data structures and wipe confidential data.
5758 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005759static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005760 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005761 psa_key_slot_t *private_key,
5762 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005763 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005764{
5765 psa_status_t status;
5766 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5767 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005768 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005769
5770 /* Step 1: run the secret agreement algorithm to generate the shared
5771 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005772 status = psa_key_agreement_raw_internal( ka_alg,
5773 private_key,
5774 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005775 shared_secret,
5776 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005777 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005778 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005779 goto exit;
5780
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005781 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005782 * the shared secret. A shared secret is permitted wherever a key
5783 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005784 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005785 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005786 shared_secret,
5787 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005788exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005789 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005790 return( status );
5791}
5792
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005793psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005794 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005795 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005796 const uint8_t *peer_key,
5797 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005798{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005799 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005800 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005801 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005802
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005803 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005804 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005805 status = psa_get_and_lock_transparent_key_slot_with_policy(
5806 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005807 if( status != PSA_SUCCESS )
5808 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005809 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005810 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005811 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005812 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005813 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005814 else
5815 {
5816 /* If a private key has been added as SECRET, we allow the derived
5817 * key material to be used as a key in PSA Crypto. */
5818 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5819 operation->can_output_key = 1;
5820 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005821
Ronald Cron5c522922020-11-14 16:35:34 +01005822 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005823
Ronald Cron5c522922020-11-14 16:35:34 +01005824 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005825}
5826
Gilles Peskinebe697d82019-05-16 18:00:41 +02005827psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005828 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005829 const uint8_t *peer_key,
5830 size_t peer_key_length,
5831 uint8_t *output,
5832 size_t output_size,
5833 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005834{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005835 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005836 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005837 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005838
5839 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5840 {
5841 status = PSA_ERROR_INVALID_ARGUMENT;
5842 goto exit;
5843 }
Ronald Cron5c522922020-11-14 16:35:34 +01005844 status = psa_get_and_lock_transparent_key_slot_with_policy(
5845 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005846 if( status != PSA_SUCCESS )
5847 goto exit;
5848
Gilles Peskine3e561302022-04-14 00:17:15 +02005849 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5850 * for the output size. The PSA specification only guarantees that this
5851 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5852 * but it might be nice to allow smaller buffers if the output fits.
5853 * At the time of writing this comment, with only ECDH implemented,
5854 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5855 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5856 * be exact for it as well. */
5857 size_t expected_length =
5858 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( slot->attr.type, slot->attr.bits );
5859 if( output_size < expected_length )
5860 {
5861 status = PSA_ERROR_BUFFER_TOO_SMALL;
5862 goto exit;
5863 }
5864
Gilles Peskine0216fe12019-04-11 21:23:21 +02005865 status = psa_key_agreement_raw_internal( alg, slot,
5866 peer_key, peer_key_length,
5867 output, output_size,
5868 output_length );
5869
5870exit:
5871 if( status != PSA_SUCCESS )
5872 {
5873 /* If an error happens and is not handled properly, the output
5874 * may be used as a key to protect sensitive data. Arrange for such
5875 * a key to be random, which is likely to result in decryption or
5876 * verification errors. This is better than filling the buffer with
5877 * some constant data such as zeros, which would result in the data
5878 * being protected with a reproducible, easily knowable key.
5879 */
5880 psa_generate_random( output, output_size );
5881 *output_length = output_size;
5882 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005883
Ronald Cron5c522922020-11-14 16:35:34 +01005884 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005885
Ronald Cron5c522922020-11-14 16:35:34 +01005886 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005887}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005888
5889
Gilles Peskine30524eb2020-11-13 17:02:26 +01005890
Gilles Peskine86a440b2018-11-12 18:39:40 +01005891/****************************************************************/
5892/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005893/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005894
Gilles Peskine30524eb2020-11-13 17:02:26 +01005895/** Initialize the PSA random generator.
5896 */
5897static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5898{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005899#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5900 memset( rng, 0, sizeof( *rng ) );
5901#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5902
Gilles Peskine30524eb2020-11-13 17:02:26 +01005903 /* Set default configuration if
5904 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5905 if( rng->entropy_init == NULL )
5906 rng->entropy_init = mbedtls_entropy_init;
5907 if( rng->entropy_free == NULL )
5908 rng->entropy_free = mbedtls_entropy_free;
5909
5910 rng->entropy_init( &rng->entropy );
5911#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5912 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5913 /* The PSA entropy injection feature depends on using NV seed as an entropy
5914 * source. Add NV seed as an entropy source for PSA entropy injection. */
5915 mbedtls_entropy_add_source( &rng->entropy,
5916 mbedtls_nv_seed_poll, NULL,
5917 MBEDTLS_ENTROPY_BLOCK_SIZE,
5918 MBEDTLS_ENTROPY_SOURCE_STRONG );
5919#endif
5920
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005921 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005922#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005923}
5924
5925/** Deinitialize the PSA random generator.
5926 */
5927static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5928{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005929#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5930 memset( rng, 0, sizeof( *rng ) );
5931#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005932 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005933 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005934#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005935}
5936
5937/** Seed the PSA random generator.
5938 */
5939static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5940{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005941#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5942 /* Do nothing: the external RNG seeds itself. */
5943 (void) rng;
5944 return( PSA_SUCCESS );
5945#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005946 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005947 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5948 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005949 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005950#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005951}
5952
Gilles Peskinee59236f2018-01-27 23:32:46 +01005953psa_status_t psa_generate_random( uint8_t *output,
5954 size_t output_size )
5955{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005956 GUARD_MODULE_INITIALIZED;
5957
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005958#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5959
5960 size_t output_length = 0;
5961 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5962 output, output_size,
5963 &output_length );
5964 if( status != PSA_SUCCESS )
5965 return( status );
5966 /* Breaking up a request into smaller chunks is currently not supported
5967 * for the extrernal RNG interface. */
5968 if( output_length != output_size )
5969 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
5970 return( PSA_SUCCESS );
5971
5972#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5973
Gilles Peskine71ddab92021-01-04 21:01:07 +01005974 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02005975 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005976 size_t request_size =
5977 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5978 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5979 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005980 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
5981 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02005982 if( ret != 0 )
5983 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01005984 output_size -= request_size;
5985 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005986 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005987 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005988#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005989}
5990
Gilles Peskine8814fc42020-12-14 15:33:44 +01005991/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005992 *
5993 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5994 * `psa_generate_random(...)`. The state parameter is ignored since the
5995 * PSA API doesn't support passing an explicit state.
5996 *
5997 * In the non-external case, psa_generate_random() calls an
5998 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5999 * and semantics as mbedtls_psa_get_random(). As an optimization,
6000 * instead of doing this back-and-forth between the PSA API and the
6001 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6002 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006003 */
6004#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6005int mbedtls_psa_get_random( void *p_rng,
6006 unsigned char *output,
6007 size_t output_size )
6008{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006009 /* This function takes a pointer to the RNG state because that's what
6010 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6011 * its own state internally and doesn't let the caller access that state.
6012 * So we just ignore the state parameter, and in practice we'll pass
6013 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006014 (void) p_rng;
6015 psa_status_t status = psa_generate_random( output, output_size );
6016 if( status == PSA_SUCCESS )
6017 return( 0 );
6018 else
6019 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6020}
6021#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6022
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006023#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006024#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006025
Gilles Peskine7228da22019-07-15 11:06:15 +02006026psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006027 size_t seed_size )
6028{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006029 if( global_data.initialized )
6030 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006031
6032 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6033 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6034 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6035 return( PSA_ERROR_INVALID_ARGUMENT );
6036
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006037 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006038}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006039#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006040
Ronald Cron3772afe2021-02-08 16:10:05 +01006041/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006042 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006043 * \param type The key type
6044 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006045 *
6046 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006047 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006048 * \retval #PSA_ERROR_INVALID_ARGUMENT
6049 * The size in bits of the key is not valid.
6050 * \retval #PSA_ERROR_NOT_SUPPORTED
6051 * The type and/or the size in bits of the key or the combination of
6052 * the two is not supported.
6053 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006054static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6055 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006056{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006057 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006058
Gilles Peskinee59236f2018-01-27 23:32:46 +01006059 if( key_type_is_raw_bytes( type ) )
6060 {
Archana4d7ae1d2021-07-07 02:50:22 +05306061 status = psa_validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006062 if( status != PSA_SUCCESS )
6063 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006064 }
6065 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006066#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006067 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6068 {
6069 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6070 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006071
Ronald Cron01b2aba2020-10-05 09:42:02 +02006072 /* Accept only byte-aligned keys, for the same reasons as
6073 * in psa_import_rsa_key(). */
6074 if( bits % 8 != 0 )
6075 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006076 }
6077 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006078#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006079
Ronald Cron5c4d3862020-12-07 11:07:24 +01006080#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006081 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6082 {
Ronald Crond81ab562021-02-16 09:01:16 +01006083 /* To avoid empty block, return successfully here. */
6084 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006085 }
6086 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006087#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006088 {
6089 return( PSA_ERROR_NOT_SUPPORTED );
6090 }
6091
6092 return( PSA_SUCCESS );
6093}
6094
Ronald Cron55ed0592020-10-05 10:30:40 +02006095psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006096 const psa_key_attributes_t *attributes,
6097 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006098{
6099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006100 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006101
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006102 if( ( attributes->domain_parameters == NULL ) &&
6103 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006104 return( PSA_ERROR_INVALID_ARGUMENT );
6105
Ronald Cron01b2aba2020-10-05 09:42:02 +02006106 if( key_type_is_raw_bytes( type ) )
6107 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006108 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006109 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006110 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006111
David Brown12ca5032021-02-11 11:02:00 -07006112#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006113 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006114 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006115#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006116 }
6117 else
6118
Jaeden Amero424fa932021-05-14 08:34:32 +01006119#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6120 defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006121 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006122 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006123 return( mbedtls_psa_rsa_generate_key( attributes,
6124 key_buffer,
6125 key_buffer_size,
6126 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006127 }
6128 else
Jaeden Amero424fa932021-05-14 08:34:32 +01006129#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6130 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006131
John Durkop9814fa22020-11-04 12:28:15 -08006132#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006133 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006134 {
Ronald Cron7023db52020-11-20 18:17:42 +01006135 return( mbedtls_psa_ecp_generate_key( attributes,
6136 key_buffer,
6137 key_buffer_size,
6138 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006139 }
6140 else
John Durkop9814fa22020-11-04 12:28:15 -08006141#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006142 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006143 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006144 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006145 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006146
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006147 return( PSA_SUCCESS );
6148}
Darryl Green0c6575a2018-11-07 16:05:30 +00006149
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006150psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006151 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006152{
6153 psa_status_t status;
6154 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006155 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006156 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006157
Ronald Cron81709fc2020-11-14 12:10:32 +01006158 *key = MBEDTLS_SVC_KEY_ID_INIT;
6159
Gilles Peskine0f84d622019-09-12 19:03:13 +02006160 /* Reject any attempt to create a zero-length key so that we don't
6161 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6162 if( psa_get_key_bits( attributes ) == 0 )
6163 return( PSA_ERROR_INVALID_ARGUMENT );
6164
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006165 /* Reject any attempt to create a public key. */
6166 if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) )
6167 return( PSA_ERROR_INVALID_ARGUMENT );
6168
Ronald Cron81709fc2020-11-14 12:10:32 +01006169 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6170 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006171 if( status != PSA_SUCCESS )
6172 goto exit;
6173
Ronald Cron977c2472020-10-13 08:32:21 +02006174 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05306175 * storage ( thus not in the case of generating a key in a secure element
6176 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
6177 * buffer to hold the generated key material. */
Ronald Cron977c2472020-10-13 08:32:21 +02006178 if( slot->key.data == NULL )
6179 {
6180 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6181 PSA_KEY_LOCATION_LOCAL_STORAGE )
6182 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006183 status = psa_validate_key_type_and_size_for_key_generation(
6184 attributes->core.type, attributes->core.bits );
6185 if( status != PSA_SUCCESS )
6186 goto exit;
6187
6188 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6189 attributes->core.type,
6190 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006191 }
6192 else
6193 {
6194 status = psa_driver_wrapper_get_key_buffer_size(
6195 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006196 if( status != PSA_SUCCESS )
6197 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006198 }
Ronald Cron977c2472020-10-13 08:32:21 +02006199
6200 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6201 if( status != PSA_SUCCESS )
6202 goto exit;
6203 }
6204
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006205 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006206 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006207
Ronald Cron2b56bc82020-10-05 10:02:26 +02006208 if( status != PSA_SUCCESS )
6209 psa_remove_key_data_from_memory( slot );
6210
Gilles Peskine11792082019-08-06 18:36:36 +02006211exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006212 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006213 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006214 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006215 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006216
Darryl Green0c6575a2018-11-07 16:05:30 +00006217 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006218}
6219
Gilles Peskinee59236f2018-01-27 23:32:46 +01006220/****************************************************************/
6221/* Module setup */
6222/****************************************************************/
6223
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006224#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006225psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6226 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6227 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6228{
6229 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6230 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006231 global_data.rng.entropy_init = entropy_init;
6232 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006233 return( PSA_SUCCESS );
6234}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006235#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006236
Gilles Peskinee59236f2018-01-27 23:32:46 +01006237void mbedtls_psa_crypto_free( void )
6238{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006239 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006240 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6241 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006242 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006243 }
6244 /* Wipe all remaining data, including configuration.
6245 * In particular, this sets all state indicator to the value
6246 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006247 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Ronald Cron9ba76912021-04-10 16:57:30 +02006248
6249 /* Terminate drivers */
6250 psa_driver_wrapper_free( );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006251}
6252
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006253#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6254/** Recover a transaction that was interrupted by a power failure.
6255 *
6256 * This function is called during initialization, before psa_crypto_init()
6257 * returns. If this function returns a failure status, the initialization
6258 * fails.
6259 */
6260static psa_status_t psa_crypto_recover_transaction(
6261 const psa_crypto_transaction_t *transaction )
6262{
6263 switch( transaction->unknown.type )
6264 {
6265 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6266 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006267 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006268 * is implemented.
6269 * https://github.com/ARMmbed/mbed-crypto/issues/218
6270 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006271 default:
6272 /* We found an unsupported transaction in the storage.
6273 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006274 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006275 }
6276}
6277#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6278
Gilles Peskinee59236f2018-01-27 23:32:46 +01006279psa_status_t psa_crypto_init( void )
6280{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006281 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006282
Gilles Peskinec6b69072018-11-20 21:42:52 +01006283 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006284 if( global_data.initialized != 0 )
6285 return( PSA_SUCCESS );
6286
Gilles Peskine30524eb2020-11-13 17:02:26 +01006287 /* Initialize and seed the random generator. */
6288 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006289 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006290 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006291 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006292 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006293 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006294
Gilles Peskine66fb1262018-12-10 16:29:04 +01006295 status = psa_initialize_key_slots( );
6296 if( status != PSA_SUCCESS )
6297 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006298
Ronald Cron9ba76912021-04-10 16:57:30 +02006299 /* Init drivers */
6300 status = psa_driver_wrapper_init( );
Gilles Peskined9348f22019-10-01 15:22:29 +02006301 if( status != PSA_SUCCESS )
6302 goto exit;
Gilles Peskined9348f22019-10-01 15:22:29 +02006303
Gilles Peskine4b734222019-07-24 15:56:31 +02006304#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006305 status = psa_crypto_load_transaction( );
6306 if( status == PSA_SUCCESS )
6307 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006308 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6309 if( status != PSA_SUCCESS )
6310 goto exit;
6311 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006312 }
6313 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6314 {
6315 /* There's no transaction to complete. It's all good. */
6316 status = PSA_SUCCESS;
6317 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006318#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006319
Gilles Peskinec6b69072018-11-20 21:42:52 +01006320 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006321 global_data.initialized = 1;
6322
6323exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006324 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006325 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006326 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006327}
6328
6329#endif /* MBEDTLS_PSA_CRYPTO_C */