blob: 7e2b686d8364715b560d75ad693de36ff0e86326 [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 Peskine2f9c4dc2018-01-28 13:16:24 +010055
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010056#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020057#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000058#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020059#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000074#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000076#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
88 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020090#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020091#endif
92
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010093/****************************************************************/
94/* Global data, support functions and library management */
95/****************************************************************/
96
Gilles Peskine48c0ea12018-06-21 14:15:31 +020097static int key_type_is_raw_bytes( psa_key_type_t type )
98{
Gilles Peskine78b3bb62018-08-10 16:03:41 +020099 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100}
101
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100102/* Values for psa_global_data_t::rng_state */
103#define RNG_NOT_INITIALIZED 0
104#define RNG_INITIALIZED 1
105#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100106
Gilles Peskine2d277862018-06-18 15:41:12 +0200107typedef struct
108{
Gilles Peskinec6b69072018-11-20 21:42:52 +0100109 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110 unsigned rng_state : 2;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100111 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112} psa_global_data_t;
113
114static psa_global_data_t global_data;
115
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100116#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
117mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
118 &global_data.rng.drbg;
119#endif
120
itayzafrir0adf0fc2018-09-06 16:24:41 +0300121#define GUARD_MODULE_INITIALIZED \
122 if( global_data.initialized == 0 ) \
123 return( PSA_ERROR_BAD_STATE );
124
Steven Cooreman01164162020-07-20 15:31:37 +0200125psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100127 /* Mbed TLS error codes can combine a high-level error code and a
128 * low-level error code. The low-level error usually reflects the
129 * root cause better, so dispatch on that preferably. */
130 int low_level_ret = - ( -ret & 0x007f );
131 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132 {
133 case 0:
134 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100135
136 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
137 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskinea5905292018-02-07 20:59:33 +0100138 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9a944802018-06-21 09:35:35 +0200139 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
140 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
141 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
142 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
143 case MBEDTLS_ERR_ASN1_INVALID_DATA:
144 return( PSA_ERROR_INVALID_ARGUMENT );
145 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
146 return( PSA_ERROR_INSUFFICIENT_MEMORY );
147 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
148 return( PSA_ERROR_BUFFER_TOO_SMALL );
149
Jaeden Amero93e21112019-02-20 13:57:28 +0000150#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000151 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000152#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100153 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
154 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100155
156 case MBEDTLS_ERR_CCM_BAD_INPUT:
157 return( PSA_ERROR_INVALID_ARGUMENT );
158 case MBEDTLS_ERR_CCM_AUTH_FAILED:
159 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100160
Gilles Peskine26869f22019-05-06 15:25:00 +0200161 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
162 return( PSA_ERROR_INVALID_ARGUMENT );
163
164 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
165 return( PSA_ERROR_BAD_STATE );
166 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
167 return( PSA_ERROR_INVALID_SIGNATURE );
168
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
170 return( PSA_ERROR_NOT_SUPPORTED );
171 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
172 return( PSA_ERROR_INVALID_ARGUMENT );
173 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
174 return( PSA_ERROR_INSUFFICIENT_MEMORY );
175 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
176 return( PSA_ERROR_INVALID_PADDING );
177 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200178 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100179 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
180 return( PSA_ERROR_INVALID_SIGNATURE );
181 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200182 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100183
Gilles Peskine82e57d12020-11-13 21:31:17 +0100184#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
185 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100186 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
187 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
189 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
190 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
191 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
192 return( PSA_ERROR_NOT_SUPPORTED );
193 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
194 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100195#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100196
197 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
198 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100199
Gilles Peskinee59236f2018-01-27 23:32:46 +0100200 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
201 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
202 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
203 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100204
205 case MBEDTLS_ERR_GCM_AUTH_FAILED:
206 return( PSA_ERROR_INVALID_SIGNATURE );
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200207 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
208 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200210 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100211
Gilles Peskine82e57d12020-11-13 21:31:17 +0100212#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
213 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100214 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
215 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100216 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223#endif
224
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
228 return( PSA_ERROR_INVALID_ARGUMENT );
229 case MBEDTLS_ERR_MD_ALLOC_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_MEMORY );
231 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
232 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
Gilles Peskinef76aa772018-10-29 19:24:33 +0100234 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
235 return( PSA_ERROR_STORAGE_FAILURE );
236 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
237 return( PSA_ERROR_INVALID_ARGUMENT );
238 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
239 return( PSA_ERROR_INVALID_ARGUMENT );
240 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
241 return( PSA_ERROR_BUFFER_TOO_SMALL );
242 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
243 return( PSA_ERROR_INVALID_ARGUMENT );
244 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
245 return( PSA_ERROR_INVALID_ARGUMENT );
246 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
250
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100251 case MBEDTLS_ERR_PK_ALLOC_FAILED:
252 return( PSA_ERROR_INSUFFICIENT_MEMORY );
253 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
254 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
255 return( PSA_ERROR_INVALID_ARGUMENT );
256 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100258 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
259 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
262 return( PSA_ERROR_NOT_SUPPORTED );
263 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
264 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
265 return( PSA_ERROR_NOT_PERMITTED );
266 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_PK_INVALID_ALG:
269 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
270 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
271 return( PSA_ERROR_NOT_SUPPORTED );
272 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
273 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200274 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
275 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea5905292018-02-07 20:59:33 +0100276
Gilles Peskineff2d2002019-05-06 15:26:23 +0200277 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
278 return( PSA_ERROR_HARDWARE_FAILURE );
279 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
280 return( PSA_ERROR_NOT_SUPPORTED );
281
Gilles Peskinea5905292018-02-07 20:59:33 +0100282 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_RSA_INVALID_PADDING:
285 return( PSA_ERROR_INVALID_PADDING );
286 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
287 return( PSA_ERROR_HARDWARE_FAILURE );
288 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
291 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200292 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
294 return( PSA_ERROR_INVALID_SIGNATURE );
295 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
296 return( PSA_ERROR_BUFFER_TOO_SMALL );
297 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100298 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200299
itayzafrir5c753392018-05-08 11:18:38 +0300300 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300301 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300302 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300303 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
304 return( PSA_ERROR_BUFFER_TOO_SMALL );
305 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
306 return( PSA_ERROR_NOT_SUPPORTED );
307 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
308 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
309 return( PSA_ERROR_INVALID_SIGNATURE );
310 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
311 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100312 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
313 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100314
Janos Follatha13b9052019-11-22 12:48:59 +0000315 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
316 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300317
Gilles Peskinee59236f2018-01-27 23:32:46 +0100318 default:
David Saadab4ecc272019-02-14 13:48:10 +0200319 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100320 }
321}
322
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200323
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200324
325
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100326/****************************************************************/
327/* Key management */
328/****************************************************************/
329
John Durkop9814fa22020-11-04 12:28:15 -0800330#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800331 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100332 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
333 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
334 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100335mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100336 size_t bits,
337 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200338{
339 switch( curve )
340 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100341 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100342 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100343 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100344#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100345 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100346 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100347#endif
348#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100349 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100350 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100351#endif
352#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100353 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100354 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100355#endif
356#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100357 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100358 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100359#endif
360#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100361 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100362 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100363 case 528:
364 if( bits_is_sloppy )
365 return( MBEDTLS_ECP_DP_SECP521R1 );
366 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100367#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100368 }
369 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100370
Paul Elliott8ff510a2020-06-02 17:19:28 +0100371 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100372 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100373 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100374#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100375 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100376 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100377#endif
378#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100379 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100381#endif
382#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100383 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100384 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100385#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100386 }
387 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100388
Paul Elliott8ff510a2020-06-02 17:19:28 +0100389 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100390 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100391 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100392#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100393 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100394 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100395 case 256:
396 if( bits_is_sloppy )
397 return( MBEDTLS_ECP_DP_CURVE25519 );
398 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100399#endif
400#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100401 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100402 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100403#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100404 }
405 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100406
Paul Elliott8ff510a2020-06-02 17:19:28 +0100407 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100408 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100409 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100410#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100411 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100413#endif
414#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100415 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100416 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100417#endif
418#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100419 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100420 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100421#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100422 }
423 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200424 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100426 (void) bits_is_sloppy;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200428}
John Durkop9814fa22020-11-04 12:28:15 -0800429#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100430 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
431 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
432 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
433 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200434
Archana4d7ae1d2021-07-07 02:50:22 +0530435psa_status_t psa_validate_unstructured_key_bit_size( psa_key_type_t type,
Archana449608b2021-09-08 15:36:05 +0530436 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200437{
438 /* Check that the bit size is acceptable for the key type */
439 switch( type )
440 {
441 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200442 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200443 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200444 case PSA_KEY_TYPE_PASSWORD:
445 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200446 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100447#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200448 case PSA_KEY_TYPE_AES:
449 if( bits != 128 && bits != 192 && bits != 256 )
450 return( PSA_ERROR_INVALID_ARGUMENT );
451 break;
452#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200453#if defined(PSA_WANT_KEY_TYPE_ARIA)
454 case PSA_KEY_TYPE_ARIA:
455 if( bits != 128 && bits != 192 && bits != 256 )
456 return( PSA_ERROR_INVALID_ARGUMENT );
457 break;
458#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100459#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460 case PSA_KEY_TYPE_CAMELLIA:
461 if( bits != 128 && bits != 192 && bits != 256 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100465#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466 case PSA_KEY_TYPE_DES:
467 if( bits != 64 && bits != 128 && bits != 192 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100471#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200472 case PSA_KEY_TYPE_CHACHA20:
473 if( bits != 256 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477 default:
478 return( PSA_ERROR_NOT_SUPPORTED );
479 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200480 if( bits % 8 != 0 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200483 return( PSA_SUCCESS );
484}
485
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100486/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100487 *
Steven Cooremancd640932021-03-08 12:00:27 +0100488 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
489 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100490 *
491 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100492 * \param[in] key_type The key type of the key to be used with the
493 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100494 *
495 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100496 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100497 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100498 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100499 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100500MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100501 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100502 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100503{
Steven Cooremancd640932021-03-08 12:00:27 +0100504 if( PSA_ALG_IS_HMAC( algorithm ) )
505 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100506 if( key_type == PSA_KEY_TYPE_HMAC )
507 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100508 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100509
510 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100511 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100512 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
513 * key. */
514 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
515 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
516 {
517 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
518 * the block length (larger than 1) for block ciphers. */
519 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
520 return( PSA_SUCCESS );
521 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100522 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100523
524 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100525}
526
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200527psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
528 size_t buffer_length )
Steven Cooreman75b74362020-07-28 14:30:13 +0200529{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100530 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200531 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200532
Ronald Cronea0f8a62020-11-25 17:52:23 +0100533 slot->key.data = mbedtls_calloc( 1, buffer_length );
534 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200535 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200536
Ronald Cronea0f8a62020-11-25 17:52:23 +0100537 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200538 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200539}
540
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200541psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
542 const uint8_t* data,
543 size_t data_length )
544{
545 psa_status_t status = psa_allocate_buffer_to_slot( slot,
546 data_length );
547 if( status != PSA_SUCCESS )
548 return( status );
549
Ronald Cronea0f8a62020-11-25 17:52:23 +0100550 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200551 return( PSA_SUCCESS );
552}
553
Ronald Crona0fe59f2020-11-29 11:14:53 +0100554psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100555 const psa_key_attributes_t *attributes,
556 const uint8_t *data, size_t data_length,
557 uint8_t *key_buffer, size_t key_buffer_size,
558 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100560 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
561 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200563 /* zero-length keys are never supported. */
564 if( data_length == 0 )
565 return( PSA_ERROR_NOT_SUPPORTED );
566
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100567 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100569 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200570
Archana449608b2021-09-08 15:36:05 +0530571 status = psa_validate_unstructured_key_bit_size( attributes->core.type,
572 *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200573 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200574 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200575
Ronald Crondd04d422020-11-28 15:14:42 +0100576 /* Copy the key material. */
577 memcpy( key_buffer, data, data_length );
578 *key_buffer_length = data_length;
579 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200580
Steven Cooreman40120f62020-10-29 11:42:22 +0100581 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100582 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100583 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200584 {
John Durkop9814fa22020-11-04 12:28:15 -0800585#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
586 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100587 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200588 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100589 return( mbedtls_psa_ecp_import_key( attributes,
590 data, data_length,
591 key_buffer, key_buffer_size,
592 key_buffer_length,
593 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100594 }
John Durkop9814fa22020-11-04 12:28:15 -0800595#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
596 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700597#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
598 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100599 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200600 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100601 return( mbedtls_psa_rsa_import_key( attributes,
602 data, data_length,
603 key_buffer, key_buffer_size,
604 key_buffer_length,
605 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200606 }
John Durkop9814fa22020-11-04 12:28:15 -0800607#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
608 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100609 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100610
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100611 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100612}
613
Gilles Peskinef603c712019-01-19 13:40:11 +0100614/** Calculate the intersection of two algorithm usage policies.
615 *
616 * Return 0 (which allows no operation) on incompatibility.
617 */
618static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100619 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100620 psa_algorithm_t alg1,
621 psa_algorithm_t alg2 )
622{
Gilles Peskine549ea862019-05-22 11:45:59 +0200623 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100624 if( alg1 == alg2 )
625 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100626 /* If the policies are from the same hash-and-sign family, check
627 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskinef7b41372021-09-22 16:15:05 +0200628 if( PSA_ALG_IS_SIGN_HASH( alg1 ) &&
629 PSA_ALG_IS_SIGN_HASH( alg2 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100630 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100631 {
Steven Cooreman03488022021-02-18 12:07:20 +0100632 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
633 return( alg2 );
634 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
635 return( alg1 );
636 }
637 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100638 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100639 * restrictive tag length. */
640 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
641 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
642 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
643 {
644 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
645 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100646 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100647
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100648 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100649 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
650 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100651 {
Steven Cooremancd640932021-03-08 12:00:27 +0100652 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
653 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100654 }
655 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100656 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100657 ( alg1_len <= alg2_len ) )
658 {
659 return( alg2 );
660 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100661 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100662 ( alg2_len <= alg1_len ) )
663 {
664 return( alg1 );
665 }
666 }
667 /* If the policies are from the same MAC family, check whether one
668 * of them is a minimum-MAC-length policy. Calculate the most
669 * restrictive tag length. */
670 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
671 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
672 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
673 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100674 /* Validate the combination of key type and algorithm. Since the base
675 * algorithm of alg1 and alg2 are the same, we only need this once. */
676 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100677 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100678
Steven Cooreman31a876d2021-03-03 20:47:40 +0100679 /* Get the (exact or at-least) output lengths for both sides of the
680 * requested intersection. None of the currently supported algorithms
681 * have an output length dependent on the actual key size, so setting it
682 * to a bogus value of 0 is currently OK.
683 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100684 * Note that for at-least-this-length wildcard algorithms, the output
685 * length is set to the shortest allowed length, which allows us to
686 * calculate the most restrictive tag length for the intersection. */
687 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
688 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100689 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100690
Steven Cooremana1d83222021-02-25 10:20:29 +0100691 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100692 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
693 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100694 {
Steven Cooremancd640932021-03-08 12:00:27 +0100695 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100696 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100697
698 /* If only one is an at-least-this-length policy, the intersection would
699 * be the other (fixed-length) policy as long as said fixed length is
700 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100701 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100702 {
Steven Cooremancd640932021-03-08 12:00:27 +0100703 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100704 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100705 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100706 {
Steven Cooremancd640932021-03-08 12:00:27 +0100707 return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100708 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100709
Steven Cooremancd640932021-03-08 12:00:27 +0100710 /* If none of them are wildcards, check whether they define the same tag
711 * length. This is still possible here when one is default-length and
712 * the other specific-length. Ensure to always return the
713 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100714 if( alg1_len == alg2_len )
715 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100716 }
717 /* If the policies are incompatible, allow nothing. */
718 return( 0 );
719}
720
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100721static int psa_key_algorithm_permits( psa_key_type_t key_type,
722 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200723 psa_algorithm_t requested_alg )
724{
Gilles Peskine549ea862019-05-22 11:45:59 +0200725 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200726 if( requested_alg == policy_alg )
727 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100728 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
729 * and requested_alg is the same hash-and-sign family with any hash,
730 * then requested_alg is compliant with policy_alg. */
Gilles Peskinef7b41372021-09-22 16:15:05 +0200731 if( PSA_ALG_IS_SIGN_HASH( requested_alg ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100732 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200733 {
Steven Cooreman03488022021-02-18 12:07:20 +0100734 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
735 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
736 }
737 /* If policy_alg is a wildcard AEAD algorithm of the same base as
738 * the requested algorithm, check the requested tag length to be
739 * equal-length or longer than the wildcard-specified length. */
740 if( PSA_ALG_IS_AEAD( policy_alg ) &&
741 PSA_ALG_IS_AEAD( requested_alg ) &&
742 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
743 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100744 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100745 {
746 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
747 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
748 }
Steven Cooremancd640932021-03-08 12:00:27 +0100749 /* If policy_alg is a MAC algorithm of the same base as the requested
750 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100751 if( PSA_ALG_IS_MAC( policy_alg ) &&
752 PSA_ALG_IS_MAC( requested_alg ) &&
753 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100754 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100755 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100756 /* Validate the combination of key type and algorithm. Since the policy
757 * and requested algorithms are the same, we only need this once. */
758 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100759 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100760
Steven Cooreman31a876d2021-03-03 20:47:40 +0100761 /* Get both the requested output length for the algorithm which is to be
762 * verified, and the default output length for the base algorithm.
763 * Note that none of the currently supported algorithms have an output
764 * length dependent on actual key size, so setting it to a bogus value
765 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100766 size_t requested_output_length = PSA_MAC_LENGTH(
767 key_type, 0, requested_alg );
768 size_t default_output_length = PSA_MAC_LENGTH(
769 key_type, 0,
770 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100771
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100772 /* If the policy is default-length, only allow an algorithm with
773 * a declared exact-length matching the default. */
774 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100775 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100776
777 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100778 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100779 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
780 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
781 {
782 return( 1 );
783 }
784
Steven Cooremancd640932021-03-08 12:00:27 +0100785 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
786 * check for the requested MAC length to be equal to or longer than the
787 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100788 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
789 {
790 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100791 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100792 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200793 }
Steven Cooremance48e852020-10-05 16:02:45 +0200794 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200795 * a key derivation with that key agreement should also be allowed. This
796 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200797 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
798 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
799 {
800 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
801 policy_alg );
802 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100803 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200804 return( 0 );
805}
806
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100807/** Test whether a policy permits an algorithm.
808 *
809 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100810 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100811 * \note This function requires providing the key type for which the policy is
812 * being validated, since some algorithm policy definitions (e.g. MAC)
813 * have different properties depending on what kind of cipher it is
814 * combined with.
815 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100816 * \retval PSA_SUCCESS When \p alg is a specific algorithm
817 * allowed by the \p policy.
818 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
819 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
820 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100821 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100822static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100823 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100824 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100825{
Steven Cooremand788fab2021-02-25 11:29:17 +0100826 /* '0' is not a valid algorithm */
827 if( alg == 0 )
828 return( PSA_ERROR_INVALID_ARGUMENT );
829
Steven Cooreman7e39f052021-02-23 14:18:32 +0100830 /* A requested algorithm cannot be a wildcard. */
831 if( PSA_ALG_IS_WILDCARD( alg ) )
832 return( PSA_ERROR_INVALID_ARGUMENT );
833
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100834 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
835 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100836 return( PSA_SUCCESS );
837 else
838 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100839}
840
Gilles Peskinef603c712019-01-19 13:40:11 +0100841/** Restrict a key policy based on a constraint.
842 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100843 * \note This function requires providing the key type for which the policy is
844 * being restricted, since some algorithm policy definitions (e.g. MAC)
845 * have different properties depending on what kind of cipher it is
846 * combined with.
847 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100848 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100849 * \param[in,out] policy The policy to restrict.
850 * \param[in] constraint The policy constraint to apply.
851 *
852 * \retval #PSA_SUCCESS
853 * \c *policy contains the intersection of the original value of
854 * \c *policy and \c *constraint.
855 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100856 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100857 * \c *policy is unchanged.
858 */
859static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100860 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100861 psa_key_policy_t *policy,
862 const psa_key_policy_t *constraint )
863{
864 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100865 psa_key_policy_algorithm_intersection( key_type, policy->alg,
866 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200867 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100868 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
869 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100870 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
871 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200872 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
873 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100874 policy->usage &= constraint->usage;
875 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200876 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100877 return( PSA_SUCCESS );
878}
879
Ronald Cron5c522922020-11-14 16:35:34 +0100880/** Get the description of a key given its identifier and policy constraints
881 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200882 *
Ronald Cron5c522922020-11-14 16:35:34 +0100883 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100884 * nonzero, the key must allow operations with this algorithm. If \p alg is
885 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100886 *
Ronald Cron5c522922020-11-14 16:35:34 +0100887 * In case of a persistent key, the function loads the description of the key
888 * into a key slot if not already done.
889 *
890 * On success, the returned key slot is locked. It is the responsibility of
891 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200892 */
Ronald Cron5c522922020-11-14 16:35:34 +0100893static psa_status_t psa_get_and_lock_key_slot_with_policy(
894 mbedtls_svc_key_id_t key,
895 psa_key_slot_t **p_slot,
896 psa_key_usage_t usage,
897 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100898{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200899 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
900 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100901
Ronald Cron5c522922020-11-14 16:35:34 +0100902 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100903 if( status != PSA_SUCCESS )
904 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200905 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100906
907 /* Enforce that usage policy for the key slot contains all the flags
908 * required by the usage parameter. There is one exception: public
909 * keys can always be exported, so we treat public key objects as
910 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200911 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100912 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200913
Gilles Peskine8e338702019-07-30 20:06:31 +0200914 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100915 {
916 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200917 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100918 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100919
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800920 /* Enforce that the usage policy permits the requested algorithm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100921 if( alg != 0 )
922 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100923 status = psa_key_policy_permits( &slot->attr.policy,
924 slot->attr.type,
925 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +0100926 if( status != PSA_SUCCESS )
927 goto error;
928 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100929
Darryl Green06fd18d2018-07-16 11:21:11 +0100930 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200931
932error:
933 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100934 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200935
936 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100937}
Darryl Green940d72c2018-07-13 13:18:51 +0100938
Ronald Cron5c522922020-11-14 16:35:34 +0100939/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200940 *
941 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200942 * available, as opposed to a key in a secure element and/or to be used
943 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200944 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200945 * This is a temporary function that may be used instead of
946 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
947 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200948 *
Ronald Cron5c522922020-11-14 16:35:34 +0100949 * On success, the returned key slot is locked. It is the responsibility of the
950 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200951 */
Ronald Cron5c522922020-11-14 16:35:34 +0100952static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
953 mbedtls_svc_key_id_t key,
954 psa_key_slot_t **p_slot,
955 psa_key_usage_t usage,
956 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200957{
Ronald Cron5c522922020-11-14 16:35:34 +0100958 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
959 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200960 if( status != PSA_SUCCESS )
961 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200962
Ronald Cronddae0f52021-08-24 15:39:44 +0200963 if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200964 {
Ronald Cron5c522922020-11-14 16:35:34 +0100965 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200966 *p_slot = NULL;
967 return( PSA_ERROR_NOT_SUPPORTED );
968 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200969
Gilles Peskine28f8f302019-07-24 13:30:31 +0200970 return( PSA_SUCCESS );
971}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200972
Steven Cooreman7ddee7f2021-04-07 18:08:30 +0200973psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000974{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100975 /* Data pointer will always be either a valid pointer or NULL in an
976 * initialized slot, so we can just free it. */
977 if( slot->key.data != NULL )
978 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
979
980 mbedtls_free( slot->key.data );
981 slot->key.data = NULL;
982 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000983
984 return( PSA_SUCCESS );
985}
986
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100987/** Completely wipe a slot in memory, including its policy.
988 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100989psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100990{
991 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +0100992
TRodziewicz18cddc02021-07-13 12:19:15 +0200993 /*
994 * As the return error code may not be handled in case of multiple errors,
TRodziewiczc9890e92021-07-14 10:16:26 +0200995 * do our best to report an unexpected lock counter. Assert with
996 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
997 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
998 * function is called as part of the execution of a test suite, the
999 * execution of the test suite is stopped in error if the assertion fails.
TRodziewicz18cddc02021-07-13 12:19:15 +02001000 */
Ronald Cron5c522922020-11-14 16:35:34 +01001001 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001002 {
TRodziewicz7871c2e2021-07-07 17:29:43 +02001003 MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001004 status = PSA_ERROR_CORRUPTION_DETECTED;
1005 }
1006
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001007 /* Multipart operations may still be using the key. This is safe
1008 * because all multipart operation objects are independent from
1009 * the key slot: if they need to access the key after the setup
1010 * phase, they have a copy of the key. Note that this means that
1011 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001012 /* At this point, key material and other type-specific content has
1013 * been wiped. Clear remaining metadata. We can call memset and not
1014 * zeroize because the metadata is not particularly sensitive. */
1015 memset( slot, 0, sizeof( *slot ) );
1016 return( status );
1017}
1018
Ronald Croncf56a0a2020-08-04 09:51:30 +02001019psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001021 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001022 psa_status_t status; /* status of the last operation */
1023 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001024#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1025 psa_se_drv_table_entry_t *driver;
1026#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001027
Ronald Croncf56a0a2020-08-04 09:51:30 +02001028 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001029 return( PSA_SUCCESS );
1030
Ronald Cronf2911112020-10-29 17:51:10 +01001031 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001032 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001033 * key, this will load the key description from persistent memory if not
1034 * done yet. We cannot avoid this loading as without it we don't know if
1035 * the key is operated by an SE or not and this information is needed by
1036 * the current implementation.
1037 */
Ronald Cron5c522922020-11-14 16:35:34 +01001038 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001039 if( status != PSA_SUCCESS )
1040 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001041
Ronald Cronf2911112020-10-29 17:51:10 +01001042 /*
1043 * If the key slot containing the key description is under access by the
1044 * library (apart from the present access), the key cannot be destroyed
1045 * yet. For the time being, just return in error. Eventually (to be
1046 * implemented), the key should be destroyed when all accesses have
1047 * stopped.
1048 */
Ronald Cron5c522922020-11-14 16:35:34 +01001049 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001050 {
Ronald Cron5c522922020-11-14 16:35:34 +01001051 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001052 return( PSA_ERROR_GENERIC_ERROR );
1053 }
1054
Gilles Peskine6687cd02021-04-21 22:32:05 +02001055 if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) )
1056 {
1057 /* Refuse the destruction of a read-only key (which may or may not work
1058 * if we attempt it, depending on whether the key is merely read-only
1059 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001060 * Just do the best we can, which is to wipe the copy in memory
1061 * (done in this function's cleanup code). */
1062 overall_status = PSA_ERROR_NOT_PERMITTED;
1063 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001064 }
1065
Gilles Peskine354f7672019-07-12 23:46:38 +02001066#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001067 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001068 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001069 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001070 /* For a key in a secure element, we need to do three things:
1071 * remove the key file in internal storage, destroy the
1072 * key inside the secure element, and update the driver's
1073 * persistent data. Start a transaction that will encompass these
1074 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001075 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001076 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001077 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001078 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001079 status = psa_crypto_save_transaction( );
1080 if( status != PSA_SUCCESS )
1081 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001082 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001083 /* We should still try to destroy the key in the secure
1084 * element and the key metadata in storage. This is especially
1085 * important if the error is that the storage is full.
1086 * But how to do it exactly without risking an inconsistent
1087 * state after a reset?
1088 * https://github.com/ARMmbed/mbed-crypto/issues/215
1089 */
1090 overall_status = status;
1091 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001092 }
1093
Ronald Cronea0f8a62020-11-25 17:52:23 +01001094 status = psa_destroy_se_key( driver,
1095 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001096 if( overall_status == PSA_SUCCESS )
1097 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001098 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001099#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1100
Darryl Greend49a4992018-06-18 17:27:26 +01001101#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001102 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001103 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001104 status = psa_destroy_persistent_key( slot->attr.id );
1105 if( overall_status == PSA_SUCCESS )
1106 overall_status = status;
1107
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001108 /* TODO: other slots may have a copy of the same key. We should
1109 * invalidate them.
1110 * https://github.com/ARMmbed/mbed-crypto/issues/214
1111 */
Darryl Greend49a4992018-06-18 17:27:26 +01001112 }
1113#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001114
Gilles Peskinefc762652019-07-22 19:30:34 +02001115#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1116 if( driver != NULL )
1117 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001118 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001119 if( overall_status == PSA_SUCCESS )
1120 overall_status = status;
1121 status = psa_crypto_stop_transaction( );
1122 if( overall_status == PSA_SUCCESS )
1123 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001124 }
1125#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1126
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001127exit:
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001128 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001129 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001130 if( status != PSA_SUCCESS )
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001131 overall_status = status;
1132 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001133}
1134
John Durkop07cc04a2020-11-16 22:08:34 -08001135#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001136 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001137static psa_status_t psa_get_rsa_public_exponent(
1138 const mbedtls_rsa_context *rsa,
1139 psa_key_attributes_t *attributes )
1140{
1141 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001142 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001143 uint8_t *buffer = NULL;
1144 size_t buflen;
1145 mbedtls_mpi_init( &mpi );
1146
1147 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1148 if( ret != 0 )
1149 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001150 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1151 {
1152 /* It's the default value, which is reported as an empty string,
1153 * so there's nothing to do. */
1154 goto exit;
1155 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001156
1157 buflen = mbedtls_mpi_size( &mpi );
1158 buffer = mbedtls_calloc( 1, buflen );
1159 if( buffer == NULL )
1160 {
1161 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1162 goto exit;
1163 }
1164 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1165 if( ret != 0 )
1166 goto exit;
1167 attributes->domain_parameters = buffer;
1168 attributes->domain_parameters_size = buflen;
1169
1170exit:
1171 mbedtls_mpi_free( &mpi );
1172 if( ret != 0 )
1173 mbedtls_free( buffer );
1174 return( mbedtls_to_psa_error( ret ) );
1175}
John Durkop07cc04a2020-11-16 22:08:34 -08001176#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001177 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001178
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001179/** Retrieve all the publicly-accessible attributes of a key.
1180 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001181psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001182 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001183{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001184 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001185 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001186 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001187
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001188 psa_reset_key_attributes( attributes );
1189
Ronald Cron5c522922020-11-14 16:35:34 +01001190 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001191 if( status != PSA_SUCCESS )
1192 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001193
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001194 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001195 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1196 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1197
1198#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron512ad812021-08-24 15:50:05 +02001199 if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001200 psa_set_key_slot_number( attributes,
1201 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001202#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001203
Gilles Peskine8e338702019-07-30 20:06:31 +02001204 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 {
John Durkop07cc04a2020-11-16 22:08:34 -08001206#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001207 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001208 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001209 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001210 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001211 * is not yet implemented.
1212 * https://github.com/ARMmbed/mbed-crypto/issues/216
1213 */
Ronald Cron9b8b69c2021-08-24 16:00:51 +02001214 if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02001215 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001216 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001217
Ronald Cron90857082020-11-25 14:58:33 +01001218 status = mbedtls_psa_rsa_load_representation(
1219 slot->attr.type,
1220 slot->key.data,
1221 slot->key.bytes,
1222 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001223 if( status != PSA_SUCCESS )
1224 break;
1225
Steven Cooremana2371e52020-07-28 14:30:39 +02001226 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001227 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001228 mbedtls_rsa_free( rsa );
1229 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001230 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001231 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001232#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001233 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001234 default:
1235 /* Nothing else to do. */
1236 break;
1237 }
1238
1239 if( status != PSA_SUCCESS )
1240 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001241
Ronald Cron5c522922020-11-14 16:35:34 +01001242 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001243
Ronald Cron5c522922020-11-14 16:35:34 +01001244 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001245}
1246
Gilles Peskinec8000c02019-08-02 20:15:51 +02001247#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1248psa_status_t psa_get_key_slot_number(
1249 const psa_key_attributes_t *attributes,
1250 psa_key_slot_number_t *slot_number )
1251{
1252 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1253 {
1254 *slot_number = attributes->slot_number;
1255 return( PSA_SUCCESS );
1256 }
1257 else
1258 return( PSA_ERROR_INVALID_ARGUMENT );
1259}
1260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1261
Ronald Crond18b5f82020-11-25 16:46:05 +01001262static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1263 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001264 uint8_t *data,
1265 size_t data_size,
1266 size_t *data_length )
1267{
Ronald Crond18b5f82020-11-25 16:46:05 +01001268 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001269 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001270 memcpy( data, key_buffer, key_buffer_size );
1271 memset( data + key_buffer_size, 0,
1272 data_size - key_buffer_size );
1273 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001274 return( PSA_SUCCESS );
1275}
1276
Ronald Cron7285cda2020-11-26 14:46:37 +01001277psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001278 const psa_key_attributes_t *attributes,
1279 const uint8_t *key_buffer, size_t key_buffer_size,
1280 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001281{
Ronald Crond18b5f82020-11-25 16:46:05 +01001282 psa_key_type_t type = attributes->core.type;
1283
Ronald Crond18b5f82020-11-25 16:46:05 +01001284 if( key_type_is_raw_bytes( type ) ||
1285 PSA_KEY_TYPE_IS_RSA( type ) ||
1286 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001287 {
1288 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001289 key_buffer, key_buffer_size,
1290 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001291 }
1292 else
1293 {
1294 /* This shouldn't happen in the reference implementation, but
1295 it is valid for a special-purpose implementation to omit
1296 support for exporting certain key types. */
1297 return( PSA_ERROR_NOT_SUPPORTED );
1298 }
1299}
1300
1301psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1302 uint8_t *data,
1303 size_t data_size,
1304 size_t *data_length )
1305{
1306 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1307 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1308 psa_key_slot_t *slot;
1309
Ronald Crone907e552021-01-18 13:22:38 +01001310 /* Reject a zero-length output buffer now, since this can never be a
1311 * valid key representation. This way we know that data must be a valid
1312 * pointer and we can do things like memset(data, ..., data_size). */
1313 if( data_size == 0 )
1314 return( PSA_ERROR_BUFFER_TOO_SMALL );
1315
Ronald Cron9486f9d2020-11-26 13:36:23 +01001316 /* Set the key to empty now, so that even when there are errors, we always
1317 * set data_length to a value between 0 and data_size. On error, setting
1318 * the key to empty is a good choice because an empty key representation is
1319 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001320 *data_length = 0;
1321
Ronald Cron9486f9d2020-11-26 13:36:23 +01001322 /* Export requires the EXPORT flag. There is an exception for public keys,
1323 * which don't require any flag, but
1324 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1325 */
1326 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1327 PSA_KEY_USAGE_EXPORT, 0 );
1328 if( status != PSA_SUCCESS )
1329 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001330
Ronald Crond18b5f82020-11-25 16:46:05 +01001331 psa_key_attributes_t attributes = {
1332 .core = slot->attr
1333 };
Ronald Cron67227982020-11-26 15:16:05 +01001334 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001335 slot->key.data, slot->key.bytes,
1336 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001337
Ronald Cron9486f9d2020-11-26 13:36:23 +01001338 unlock_status = psa_unlock_key_slot( slot );
1339
1340 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1341}
1342
Ronald Cron7285cda2020-11-26 14:46:37 +01001343psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001344 const psa_key_attributes_t *attributes,
1345 const uint8_t *key_buffer,
1346 size_t key_buffer_size,
1347 uint8_t *data,
1348 size_t data_size,
1349 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001350{
Ronald Crond18b5f82020-11-25 16:46:05 +01001351 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001352
Ronald Crond18b5f82020-11-25 16:46:05 +01001353 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001354 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001355 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001356 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001357 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001358 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001359 key_buffer, key_buffer_size,
1360 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001361 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001362
Ronald Crond18b5f82020-11-25 16:46:05 +01001363 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001364 {
John Durkop0e005192020-10-31 22:06:54 -07001365#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1366 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001367 return( mbedtls_psa_rsa_export_public_key( attributes,
1368 key_buffer,
1369 key_buffer_size,
1370 data,
1371 data_size,
1372 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001373#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001374 /* We don't know how to convert a private RSA key to public. */
1375 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001376#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1377 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001378 }
1379 else
Moran Pekera998bc62018-04-16 18:16:20 +03001380 {
John Durkop6ba40d12020-11-10 08:50:04 -08001381#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1382 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001383 return( mbedtls_psa_ecp_export_public_key( attributes,
1384 key_buffer,
1385 key_buffer_size,
1386 data,
1387 data_size,
1388 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001389#else
1390 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001391 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001392#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1393 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001394 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001395 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001396 else
1397 {
1398 /* This shouldn't happen in the reference implementation, but
1399 it is valid for a special-purpose implementation to omit
1400 support for exporting certain key types. */
1401 return( PSA_ERROR_NOT_SUPPORTED );
1402 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001403}
Ronald Crond18b5f82020-11-25 16:46:05 +01001404
Ronald Croncf56a0a2020-08-04 09:51:30 +02001405psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001406 uint8_t *data,
1407 size_t data_size,
1408 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001409{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001410 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001411 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001412 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001413
Ronald Crone907e552021-01-18 13:22:38 +01001414 /* Reject a zero-length output buffer now, since this can never be a
1415 * valid key representation. This way we know that data must be a valid
1416 * pointer and we can do things like memset(data, ..., data_size). */
1417 if( data_size == 0 )
1418 return( PSA_ERROR_BUFFER_TOO_SMALL );
1419
Darryl Greendd8fb772018-11-07 16:00:44 +00001420 /* Set the key to empty now, so that even when there are errors, we always
1421 * set data_length to a value between 0 and data_size. On error, setting
1422 * the key to empty is a good choice because an empty key representation is
1423 * unlikely to be accepted anywhere. */
1424 *data_length = 0;
1425
1426 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001427 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001428 if( status != PSA_SUCCESS )
1429 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001430
Ronald Cron9486f9d2020-11-26 13:36:23 +01001431 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1432 {
1433 status = PSA_ERROR_INVALID_ARGUMENT;
1434 goto exit;
1435 }
1436
Ronald Crond18b5f82020-11-25 16:46:05 +01001437 psa_key_attributes_t attributes = {
1438 .core = slot->attr
1439 };
Ronald Cron67227982020-11-26 15:16:05 +01001440 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001441 &attributes, slot->key.data, slot->key.bytes,
1442 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001443
1444exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001445 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001446
Ronald Cron5c522922020-11-14 16:35:34 +01001447 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001448}
1449
Gilles Peskine91e8c332019-08-02 19:19:39 +02001450#if defined(static_assert)
1451static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1452 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001453static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001454 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001455static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001456 "One or more key attribute flag is listed as both internal-only and external-only" );
1457#endif
1458
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001459/** Validate that a key policy is internally well-formed.
1460 *
1461 * This function only rejects invalid policies. It does not validate the
1462 * consistency of the policy with respect to other attributes of the key
1463 * such as the key type.
1464 */
1465static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001466{
1467 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001468 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001469 PSA_KEY_USAGE_ENCRYPT |
1470 PSA_KEY_USAGE_DECRYPT |
gabor-mezei-arm4a210192021-04-14 21:14:28 +02001471 PSA_KEY_USAGE_SIGN_MESSAGE |
1472 PSA_KEY_USAGE_VERIFY_MESSAGE |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001473 PSA_KEY_USAGE_SIGN_HASH |
1474 PSA_KEY_USAGE_VERIFY_HASH |
Manuel Pégourié-Gonnard805251b2021-05-04 09:49:59 +02001475 PSA_KEY_USAGE_VERIFY_DERIVATION |
Gilles Peskine4747d192019-04-17 15:05:45 +02001476 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1477 return( PSA_ERROR_INVALID_ARGUMENT );
1478
Gilles Peskine4747d192019-04-17 15:05:45 +02001479 return( PSA_SUCCESS );
1480}
1481
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001482/** Validate the internal consistency of key attributes.
1483 *
1484 * This function only rejects invalid attribute values. If does not
1485 * validate the consistency of the attributes with any key data that may
1486 * be involved in the creation of the key.
1487 *
1488 * Call this function early in the key creation process.
1489 *
1490 * \param[in] attributes Key attributes for the new key.
1491 * \param[out] p_drv On any return, the driver for the key, if any.
1492 * NULL for a transparent key.
1493 *
1494 */
1495static psa_status_t psa_validate_key_attributes(
1496 const psa_key_attributes_t *attributes,
1497 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001498{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001499 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001500 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001501 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001502
Ronald Cron54b90082020-10-29 15:26:43 +01001503 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001504 if( status != PSA_SUCCESS )
1505 return( status );
1506
Ronald Crond2ed4812020-07-17 16:11:30 +02001507 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001508 if( status != PSA_SUCCESS )
1509 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001510
Ronald Cron65f38a32020-10-23 17:11:13 +02001511 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1512 {
1513 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1514 return( PSA_ERROR_INVALID_ARGUMENT );
1515 }
1516 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001517 {
Ronald Cron77e412c2021-03-31 17:36:31 +02001518 if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) )
1519 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Crond2ed4812020-07-17 16:11:30 +02001520 }
1521
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001522 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001523 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001524 return( status );
1525
1526 /* Refuse to create overly large keys.
1527 * Note that this doesn't trigger on import if the attributes don't
1528 * explicitly specify a size (so psa_get_key_bits returns 0), so
1529 * psa_import_key() needs its own checks. */
1530 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1531 return( PSA_ERROR_NOT_SUPPORTED );
1532
Gilles Peskine91e8c332019-08-02 19:19:39 +02001533 /* Reject invalid flags. These should not be reachable through the API. */
1534 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1535 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1536 return( PSA_ERROR_INVALID_ARGUMENT );
1537
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001538 return( PSA_SUCCESS );
1539}
1540
Gilles Peskine4747d192019-04-17 15:05:45 +02001541/** Prepare a key slot to receive key material.
1542 *
1543 * This function allocates a key slot and sets its metadata.
1544 *
1545 * If this function fails, call psa_fail_key_creation().
1546 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001547 * This function is intended to be used as follows:
1548 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001549 * it with the specified attributes, and in case of a volatile key assign it
1550 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001551 * -# Populate the slot with the key material.
1552 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1553 * In case of failure at any step, stop the sequence and call
1554 * psa_fail_key_creation().
1555 *
Ronald Cron5c522922020-11-14 16:35:34 +01001556 * On success, the key slot is locked. It is the responsibility of the caller
1557 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001558 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001559 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001560 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001561 * \param[out] p_slot On success, a pointer to the prepared slot.
1562 * \param[out] p_drv On any return, the driver for the key, if any.
1563 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001564 *
1565 * \retval #PSA_SUCCESS
1566 * The key slot is ready to receive key material.
1567 * \return If this function fails, the key slot is an invalid state.
1568 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001569 */
1570static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001571 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001572 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001573 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001574 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001575{
1576 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001577 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001578 psa_key_slot_t *slot;
1579
Gilles Peskinedf179142019-07-15 22:02:14 +02001580 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001581 *p_drv = NULL;
1582
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001583 status = psa_validate_key_attributes( attributes, p_drv );
1584 if( status != PSA_SUCCESS )
1585 return( status );
1586
Ronald Cronc4d1b512020-07-31 11:26:37 +02001587 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001588 if( status != PSA_SUCCESS )
1589 return( status );
1590 slot = *p_slot;
1591
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001592 /* We're storing the declared bit-size of the key. It's up to each
1593 * creation mechanism to verify that this information is correct.
1594 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001595 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001596 * is optional (import, copy). In case of a volatile key, assign it the
1597 * volatile key identifier associated to the slot returned to contain its
1598 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001599
1600 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001601 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1602 {
1603#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1604 slot->attr.id = volatile_key_id;
1605#else
1606 slot->attr.id.key_id = volatile_key_id;
1607#endif
1608 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001609
Gilles Peskine91e8c332019-08-02 19:19:39 +02001610 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001611 * external-only flags, query `attributes`. Thanks to the check
1612 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001613 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001614 * may have set. */
1615 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001616
Gilles Peskinecbaff462019-07-12 23:46:04 +02001617#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001618 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001619 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001620 * create the key file in internal storage, create the
1621 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001622 * persistent data. This is done by starting a transaction that will
1623 * encompass these three actions.
1624 * For registering a volatile key, we just need to find an appropriate
1625 * slot number inside the SE. Since the key is designated volatile, creating
1626 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001627 /* The first thing to do is to find a slot number for the new key.
1628 * We save the slot number in persistent storage as part of the
1629 * transaction data. It will be needed to recover if the power
1630 * fails during the key creation process, to clean up on the secure
1631 * element side after restarting. Obtaining a slot number from the
1632 * secure element driver updates its persistent state, but we do not yet
1633 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001634 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001635 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001636 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001637 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001638 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001639 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001640 if( status != PSA_SUCCESS )
1641 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001642
1643 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001644 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001645 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1646 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001647 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001648 psa_crypto_transaction.key.id = slot->attr.id;
1649 status = psa_crypto_save_transaction( );
1650 if( status != PSA_SUCCESS )
1651 {
1652 (void) psa_crypto_stop_transaction( );
1653 return( status );
1654 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001655 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001656
1657 status = psa_copy_key_material_into_slot(
1658 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001659 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001660
1661 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1662 {
1663 /* Key registration only makes sense with a secure element. */
1664 return( PSA_ERROR_INVALID_ARGUMENT );
1665 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001666#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1667
Ronald Cronc4d1b512020-07-31 11:26:37 +02001668 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001669}
Gilles Peskine4747d192019-04-17 15:05:45 +02001670
1671/** Finalize the creation of a key once its key material has been set.
1672 *
1673 * This entails writing the key to persistent storage.
1674 *
1675 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001676 * See the documentation of psa_start_key_creation() for the intended use
1677 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001678 *
Ronald Cron5c522922020-11-14 16:35:34 +01001679 * If the finalization succeeds, the function unlocks the key slot (it was
1680 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1681 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001682 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001683 * \param[in,out] slot Pointer to the slot with key material.
1684 * \param[in] driver The secure element driver for the key,
1685 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001686 * \param[out] key On success, identifier of the key. Note that the
1687 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001688 *
1689 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001690 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001691 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1692 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1693 * \retval #PSA_ERROR_ALREADY_EXISTS
1694 * \retval #PSA_ERROR_DATA_INVALID
1695 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001696 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001697 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001698 * \return If this function fails, the key slot is an invalid state.
1699 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001700 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001701static psa_status_t psa_finish_key_creation(
1702 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001703 psa_se_drv_table_entry_t *driver,
1704 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001705{
1706 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001707 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001708 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001709
1710#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001711 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001712 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001713#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1714 if( driver != NULL )
1715 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001716 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001717 psa_key_slot_number_t slot_number =
1718 psa_key_slot_get_slot_number( slot ) ;
1719
Gilles Peskineb46bef22019-07-30 21:32:04 +02001720#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001721 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001722 sizeof( data.slot_number ),
1723 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001724#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001725 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001726 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001727 (uint8_t*) &data,
1728 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001729 }
1730 else
1731#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1732 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001733 /* Key material is saved in export representation in the slot, so
1734 * just pass the slot buffer for storage. */
1735 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001736 slot->key.data,
1737 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001738 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001739 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001740#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1741
Gilles Peskinecbaff462019-07-12 23:46:04 +02001742#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001743 /* Finish the transaction for a key creation. This does not
1744 * happen when registering an existing key. Detect this case
1745 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001746 * creation of a persistent key in a secure element requires a transaction,
1747 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001748 if( driver != NULL &&
1749 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001750 {
1751 status = psa_save_se_persistent_data( driver );
1752 if( status != PSA_SUCCESS )
1753 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001754 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001755 return( status );
1756 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001757 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001758 }
1759#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1760
Ronald Cron50972942020-11-14 11:28:25 +01001761 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001762 {
1763 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001764 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001765 if( status != PSA_SUCCESS )
1766 *key = MBEDTLS_SVC_KEY_ID_INIT;
1767 }
Ronald Cron50972942020-11-14 11:28:25 +01001768
Gilles Peskine4747d192019-04-17 15:05:45 +02001769 return( status );
1770}
1771
1772/** Abort the creation of a key.
1773 *
1774 * You may call this function after calling psa_start_key_creation(),
1775 * or after psa_finish_key_creation() fails. In other circumstances, this
1776 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001777 * See the documentation of psa_start_key_creation() for the intended use
1778 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001779 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001780 * \param[in,out] slot Pointer to the slot with key material.
1781 * \param[in] driver The secure element driver for the key,
1782 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001783 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001784static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001785 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001786{
Gilles Peskine011e4282019-06-26 18:34:38 +02001787 (void) driver;
1788
Gilles Peskine4747d192019-04-17 15:05:45 +02001789 if( slot == NULL )
1790 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001791
1792#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001793 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001794 * element, and the failure happened later (when saving metadata
1795 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001796 * element.
1797 * https://github.com/ARMmbed/mbed-crypto/issues/217
1798 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001799
Gilles Peskined7729582019-08-05 15:55:54 +02001800 /* Abort the ongoing transaction if any (there may not be one if
1801 * the creation process failed before starting one, or if the
1802 * key creation is a registration of a key in a secure element).
1803 * Earlier functions must already have done what it takes to undo any
1804 * partial creation. All that's left is to update the transaction data
1805 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001806 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001807#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1808
Gilles Peskine4747d192019-04-17 15:05:45 +02001809 psa_wipe_key_slot( slot );
1810}
1811
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001812/** Validate optional attributes during key creation.
1813 *
1814 * Some key attributes are optional during key creation. If they are
1815 * specified in the attributes structure, check that they are consistent
1816 * with the data in the slot.
1817 *
1818 * This function should be called near the end of key creation, after
1819 * the slot in memory is fully populated but before saving persistent data.
1820 */
1821static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001822 const psa_key_slot_t *slot,
1823 const psa_key_attributes_t *attributes )
1824{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001825 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001826 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001827 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001828 return( PSA_ERROR_INVALID_ARGUMENT );
1829 }
1830
1831 if( attributes->domain_parameters_size != 0 )
1832 {
John Durkop0e005192020-10-31 22:06:54 -07001833#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1834 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001835 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001836 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001837 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001838 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001840
Ronald Cron90857082020-11-25 14:58:33 +01001841 psa_status_t status = mbedtls_psa_rsa_load_representation(
1842 slot->attr.type,
1843 slot->key.data,
1844 slot->key.bytes,
1845 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001846 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001847 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001848
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001849 mbedtls_mpi_init( &actual );
1850 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001851 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001852 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001853 mbedtls_rsa_free( rsa );
1854 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001855 if( ret != 0 )
1856 goto rsa_exit;
1857 ret = mbedtls_mpi_read_binary( &required,
1858 attributes->domain_parameters,
1859 attributes->domain_parameters_size );
1860 if( ret != 0 )
1861 goto rsa_exit;
1862 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1863 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1864 rsa_exit:
1865 mbedtls_mpi_free( &actual );
1866 mbedtls_mpi_free( &required );
1867 if( ret != 0)
1868 return( mbedtls_to_psa_error( ret ) );
1869 }
1870 else
John Durkop9814fa22020-11-04 12:28:15 -08001871#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1872 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001873 {
1874 return( PSA_ERROR_INVALID_ARGUMENT );
1875 }
1876 }
1877
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001878 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001879 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001880 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001881 return( PSA_ERROR_INVALID_ARGUMENT );
1882 }
1883
1884 return( PSA_SUCCESS );
1885}
1886
Gilles Peskine4747d192019-04-17 15:05:45 +02001887psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001888 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001889 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001890 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001891{
1892 psa_status_t status;
1893 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001894 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001895 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301896 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001897
Ronald Cron81709fc2020-11-14 12:10:32 +01001898 *key = MBEDTLS_SVC_KEY_ID_INIT;
1899
Gilles Peskine0f84d622019-09-12 19:03:13 +02001900 /* Reject zero-length symmetric keys (including raw data key objects).
1901 * This also rejects any key which might be encoded as an empty string,
1902 * which is never valid. */
1903 if( data_length == 0 )
1904 return( PSA_ERROR_INVALID_ARGUMENT );
1905
Archana449608b2021-09-08 15:36:05 +05301906 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Archana6ed4bda2021-08-04 10:47:15 +05301907 if( data_length > SIZE_MAX / 8 )
1908 return( PSA_ERROR_NOT_SUPPORTED );
1909
Gilles Peskinedf179142019-07-15 22:02:14 +02001910 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001911 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001912 if( status != PSA_SUCCESS )
1913 goto exit;
1914
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001915 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301916 * storage ( thus not in the case of importing a key in a secure element
1917 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1918 * buffer to hold the imported key material. */
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001919 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001920 {
Archanad8a83dc2021-06-14 10:04:16 +05301921 if( psa_key_lifetime_is_external( attributes->core.lifetime ) )
1922 {
Archana449608b2021-09-08 15:36:05 +05301923 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
1924 attributes, data, data_length, &storage_size );
Archanad8a83dc2021-06-14 10:04:16 +05301925 if( status != PSA_SUCCESS )
1926 goto exit;
1927 }
1928 status = psa_allocate_buffer_to_slot( slot, storage_size );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001929 if( status != PSA_SUCCESS )
1930 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001931 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001932
1933 bits = slot->attr.bits;
1934 status = psa_driver_wrapper_import_key( attributes,
1935 data, data_length,
1936 slot->key.data,
1937 slot->key.bytes,
1938 &slot->key.bytes, &bits );
1939 if( status != PSA_SUCCESS )
1940 goto exit;
1941
1942 if( slot->attr.bits == 0 )
1943 slot->attr.bits = (psa_key_bits_t) bits;
1944 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001945 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001946 status = PSA_ERROR_INVALID_ARGUMENT;
1947 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001948 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001949
Archana6ed4bda2021-08-04 10:47:15 +05301950 /* Enforce a size limit, and in particular ensure that the bit
1951 * size fits in its representation type.*/
1952 if( bits > PSA_MAX_KEY_BITS )
1953 {
1954 status = PSA_ERROR_NOT_SUPPORTED;
1955 goto exit;
1956 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001957 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001958 if( status != PSA_SUCCESS )
1959 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001960
Ronald Cron81709fc2020-11-14 12:10:32 +01001961 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001962exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001963 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001964 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001965
Gilles Peskine4747d192019-04-17 15:05:45 +02001966 return( status );
1967}
1968
Gilles Peskined7729582019-08-05 15:55:54 +02001969#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1970psa_status_t mbedtls_psa_register_se_key(
1971 const psa_key_attributes_t *attributes )
1972{
1973 psa_status_t status;
1974 psa_key_slot_t *slot = NULL;
1975 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001976 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001977
1978 /* Leaving attributes unspecified is not currently supported.
1979 * It could make sense to query the key type and size from the
1980 * secure element, but not all secure elements support this
1981 * and the driver HAL doesn't currently support it. */
1982 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1983 return( PSA_ERROR_NOT_SUPPORTED );
1984 if( psa_get_key_bits( attributes ) == 0 )
1985 return( PSA_ERROR_NOT_SUPPORTED );
1986
1987 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001988 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02001989 if( status != PSA_SUCCESS )
1990 goto exit;
1991
Ronald Cron81709fc2020-11-14 12:10:32 +01001992 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02001993
1994exit:
1995 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02001996 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001997
Gilles Peskined7729582019-08-05 15:55:54 +02001998 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001999 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002000 return( status );
2001}
2002#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2003
Ronald Croncf56a0a2020-08-04 09:51:30 +02002004psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002005 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002006 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002007{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002008 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002009 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002010 psa_key_slot_t *source_slot = NULL;
2011 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002012 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002013 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302014 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002015
Ronald Cron81709fc2020-11-14 12:10:32 +01002016 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2017
Archana8a180362021-07-05 02:18:48 +05302018 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01002019 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002020 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002021 goto exit;
2022
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002023 status = psa_validate_optional_attributes( source_slot,
2024 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002025 if( status != PSA_SUCCESS )
2026 goto exit;
2027
Archana9d17bf42021-09-10 06:22:44 +05302028 /* The target key type and number of bits have been validated by
2029 * psa_validate_optional_attributes() to be either equal to zero or
2030 * equal to the ones of the source key. So it is safe to inherit
2031 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302032 * */
Archana9d17bf42021-09-10 06:22:44 +05302033 actual_attributes.core.bits = source_slot->attr.bits;
2034 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302035
2036
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002037 status = psa_restrict_key_policy( source_slot->attr.type,
2038 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002039 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002040 if( status != PSA_SUCCESS )
2041 goto exit;
2042
Ronald Cron81709fc2020-11-14 12:10:32 +01002043 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2044 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002045 if( status != PSA_SUCCESS )
2046 goto exit;
Archana8a180362021-07-05 02:18:48 +05302047 if( PSA_KEY_LIFETIME_GET_LOCATION( target_slot->attr.lifetime ) !=
2048 PSA_KEY_LIFETIME_GET_LOCATION( source_slot->attr.lifetime ) )
Gilles Peskinef603c712019-01-19 13:40:11 +01002049 {
Archana8a180362021-07-05 02:18:48 +05302050 /*
Archana9d17bf42021-09-10 06:22:44 +05302051 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302052 * the source key would need to be exported as plaintext and re-imported
2053 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302054 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302055 * appropriate API invocations from the application, if needed.
2056 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002057 status = PSA_ERROR_NOT_SUPPORTED;
2058 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002059 }
Archana8a180362021-07-05 02:18:48 +05302060 /*
2061 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302062 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302063 * - For opaque keys this translates to an invocation of the drivers'
2064 * copy_key entry point through the dispatch layer.
2065 * */
Ronald Cron6cc66312021-04-02 12:27:47 +02002066 if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) )
2067 {
Archana8a180362021-07-05 02:18:48 +05302068 status = psa_driver_wrapper_get_key_buffer_size( &actual_attributes,
Archana449608b2021-09-08 15:36:05 +05302069 &storage_size );
Archana8a180362021-07-05 02:18:48 +05302070 if( status != PSA_SUCCESS )
2071 goto exit;
Archana374fe5b2021-09-08 15:50:28 +05302072
Archana8a180362021-07-05 02:18:48 +05302073 status = psa_allocate_buffer_to_slot( target_slot, storage_size );
2074 if( status != PSA_SUCCESS )
2075 goto exit;
Archana374fe5b2021-09-08 15:50:28 +05302076
Archana8a180362021-07-05 02:18:48 +05302077 status = psa_driver_wrapper_copy_key( &actual_attributes,
2078 source_slot->key.data,
2079 source_slot->key.bytes,
2080 target_slot->key.data,
2081 target_slot->key.bytes,
2082 &target_slot->key.bytes );
2083 if( status != PSA_SUCCESS )
2084 goto exit;
Ronald Cron6cc66312021-04-02 12:27:47 +02002085 }
Archana8a180362021-07-05 02:18:48 +05302086 else
2087 {
Archana9d17bf42021-09-10 06:22:44 +05302088 status = psa_copy_key_material_into_slot( target_slot,
2089 source_slot->key.data,
2090 source_slot->key.bytes );
Archana8a180362021-07-05 02:18:48 +05302091 if( status != PSA_SUCCESS )
2092 goto exit;
2093 }
Ronald Cron81709fc2020-11-14 12:10:32 +01002094 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002095exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002096 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002097 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002098
Ronald Cron5c522922020-11-14 16:35:34 +01002099 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002100
Ronald Cron5c522922020-11-14 16:35:34 +01002101 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002102}
2103
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002104
2105
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002106/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002107/* Message digests */
2108/****************************************************************/
2109
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002110psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2111{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002112 /* Aborting a non-active operation is allowed */
2113 if( operation->id == 0 )
2114 return( PSA_SUCCESS );
2115
2116 psa_status_t status = psa_driver_wrapper_hash_abort( operation );
2117 operation->id = 0;
2118
2119 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002120}
2121
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002122psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002123 psa_algorithm_t alg )
2124{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002125 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002126
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002127 /* A context must be freshly initialized before it can be set up. */
2128 if( operation->id != 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002129 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002130 status = PSA_ERROR_BAD_STATE;
2131 goto exit;
2132 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002133
2134 if( !PSA_ALG_IS_HASH( alg ) )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002135 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002136 status = PSA_ERROR_INVALID_ARGUMENT;
2137 goto exit;
2138 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002139
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002140 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2141 * directly zeroes the int-sized dummy member of the context union. */
Steven Cooreman893232f2021-03-15 12:23:37 +01002142 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
2143
Dave Rodgman685b6a72021-06-24 11:49:14 +01002144 status = psa_driver_wrapper_hash_setup( operation, alg );
2145
2146exit:
2147 if( status != PSA_SUCCESS )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002148 psa_hash_abort( operation );
Dave Rodgman685b6a72021-06-24 11:49:14 +01002149
2150 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002151}
2152
2153psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2154 const uint8_t *input,
2155 size_t input_length )
2156{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002157 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2158
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002159 if( operation->id == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002160 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002161 status = PSA_ERROR_BAD_STATE;
2162 goto exit;
2163 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002164
Steven Cooremanc8288352021-03-04 14:02:19 +01002165 /* Don't require hash implementations to behave correctly on a
2166 * zero-length input, which may have an invalid pointer. */
2167 if( input_length == 0 )
2168 return( PSA_SUCCESS );
2169
Dave Rodgman685b6a72021-06-24 11:49:14 +01002170 status = psa_driver_wrapper_hash_update( operation, input, input_length );
2171
2172exit:
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002173 if( status != PSA_SUCCESS )
2174 psa_hash_abort( operation );
2175
2176 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002177}
2178
2179psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2180 uint8_t *hash,
2181 size_t hash_size,
2182 size_t *hash_length )
2183{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002184 *hash_length = 0;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002185 if( operation->id == 0 )
2186 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002187
Steven Cooreman1e582352021-02-18 17:24:37 +01002188 psa_status_t status = psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002189 operation, hash, hash_size, hash_length );
Steven Cooreman0e307642021-02-18 16:18:32 +01002190 psa_hash_abort( operation );
2191 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002192}
2193
Gilles Peskine2d277862018-06-18 15:41:12 +02002194psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2195 const uint8_t *hash,
2196 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002197{
Ronald Cron69a63422021-10-18 09:47:58 +02002198 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002199 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002200 psa_status_t status = psa_hash_finish(
2201 operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01002202 actual_hash, sizeof( actual_hash ),
2203 &actual_hash_length );
Dave Rodgman685b6a72021-06-24 11:49:14 +01002204
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002205 if( status != PSA_SUCCESS )
Dave Rodgman685b6a72021-06-24 11:49:14 +01002206 goto exit;
2207
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002208 if( actual_hash_length != hash_length )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002209 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002210 status = PSA_ERROR_INVALID_SIGNATURE;
2211 goto exit;
2212 }
2213
Steven Cooreman36876a02021-04-29 16:37:59 +02002214 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Dave Rodgman685b6a72021-06-24 11:49:14 +01002215 status = PSA_ERROR_INVALID_SIGNATURE;
2216
2217exit:
Gilles Peskine60aebec2021-12-13 12:33:18 +01002218 mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) );
Dave Rodgman685b6a72021-06-24 11:49:14 +01002219 if( status != PSA_SUCCESS )
2220 psa_hash_abort(operation);
2221
2222 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002223}
2224
Gilles Peskine0a749c82019-11-28 19:33:58 +01002225psa_status_t psa_hash_compute( psa_algorithm_t alg,
2226 const uint8_t *input, size_t input_length,
2227 uint8_t *hash, size_t hash_size,
2228 size_t *hash_length )
2229{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002230 *hash_length = 0;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002231 if( !PSA_ALG_IS_HASH( alg ) )
2232 return( PSA_ERROR_INVALID_ARGUMENT );
2233
Steven Cooreman1e582352021-02-18 17:24:37 +01002234 return( psa_driver_wrapper_hash_compute( alg, input, input_length,
2235 hash, hash_size, hash_length ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002236}
2237
2238psa_status_t psa_hash_compare( psa_algorithm_t alg,
2239 const uint8_t *input, size_t input_length,
2240 const uint8_t *hash, size_t hash_length )
2241{
Ronald Cron69a63422021-10-18 09:47:58 +02002242 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002243 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002244
2245 if( !PSA_ALG_IS_HASH( alg ) )
2246 return( PSA_ERROR_INVALID_ARGUMENT );
2247
Steven Cooreman1e582352021-02-18 17:24:37 +01002248 psa_status_t status = psa_driver_wrapper_hash_compute(
2249 alg, input, input_length,
2250 actual_hash, sizeof(actual_hash),
2251 &actual_hash_length );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002252 if( status != PSA_SUCCESS )
Gilles Peskine60aebec2021-12-13 12:33:18 +01002253 goto exit;
Steven Cooreman84d670d2021-02-18 16:22:53 +01002254 if( actual_hash_length != hash_length )
Gilles Peskine60aebec2021-12-13 12:33:18 +01002255 {
2256 status = PSA_ERROR_INVALID_SIGNATURE;
2257 goto exit;
2258 }
Steven Cooreman36876a02021-04-29 16:37:59 +02002259 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Gilles Peskine60aebec2021-12-13 12:33:18 +01002260 status = PSA_ERROR_INVALID_SIGNATURE;
2261
2262exit:
2263 mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) );
2264 return( status );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002265}
2266
Gilles Peskineeb35d782019-01-22 17:56:16 +01002267psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2268 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002269{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002270 if( source_operation->id == 0 ||
2271 target_operation->id != 0 )
2272 {
2273 return( PSA_ERROR_BAD_STATE );
2274 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002275
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002276 psa_status_t status = psa_driver_wrapper_hash_clone( source_operation,
2277 target_operation );
2278 if( status != PSA_SUCCESS )
2279 psa_hash_abort( target_operation );
2280
2281 return( status );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002282}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002283
2284
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002285/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002286/* MAC */
2287/****************************************************************/
2288
Steven Cooremane6804192021-03-19 18:28:56 +01002289psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002290{
Steven Cooremane6804192021-03-19 18:28:56 +01002291 /* Aborting a non-active operation is allowed */
2292 if( operation->id == 0 )
2293 return( PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002294
Steven Cooremane6804192021-03-19 18:28:56 +01002295 psa_status_t status = psa_driver_wrapper_mac_abort( operation );
Steven Cooreman72f736a2021-05-07 14:14:37 +02002296 operation->mac_size = 0;
2297 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002298 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002299
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002300 return( status );
2301}
2302
Ronald Cron2dff3b22021-06-17 16:33:22 +02002303static psa_status_t psa_mac_finalize_alg_and_key_validation(
2304 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002305 const psa_key_attributes_t *attributes,
2306 uint8_t *mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002307{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002308 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron79bdd822021-06-17 16:46:44 +02002309 psa_key_type_t key_type = psa_get_key_type( attributes );
2310 size_t key_bits = psa_get_key_bits( attributes );
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002311
Ronald Cron28ea0502021-06-17 16:10:24 +02002312 if( ! PSA_ALG_IS_MAC( alg ) )
Ronald Cron79bdd822021-06-17 16:46:44 +02002313 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremane6804192021-03-19 18:28:56 +01002314
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002315 /* Validate the combination of key type and algorithm */
Ronald Cron79bdd822021-06-17 16:46:44 +02002316 status = psa_mac_key_can_do( alg, key_type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002317 if( status != PSA_SUCCESS )
Ronald Cron79bdd822021-06-17 16:46:44 +02002318 return( status );
Steven Cooreman15472f82021-03-02 16:16:22 +01002319
Steven Cooremand1a68f12021-05-10 11:13:41 +02002320 /* Get the output length for the algorithm and key combination */
Ronald Cron79bdd822021-06-17 16:46:44 +02002321 *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002322
Ronald Cron79bdd822021-06-17 16:46:44 +02002323 if( *mac_size < 4 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002324 {
2325 /* A very short MAC is too short for security since it can be
2326 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2327 * so we make this our minimum, even though 32 bits is still
2328 * too small for security. */
Ronald Cron79bdd822021-06-17 16:46:44 +02002329 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman15472f82021-03-02 16:16:22 +01002330 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002331
Ronald Cron79bdd822021-06-17 16:46:44 +02002332 if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits,
2333 PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002334 {
2335 /* It's impossible to "truncate" to a larger length than the full length
2336 * of the algorithm. */
Ronald Cron79bdd822021-06-17 16:46:44 +02002337 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002338 }
2339
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002340 if( *mac_size > PSA_MAC_MAX_SIZE )
2341 {
2342 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2343 * that is disabled in the compile-time configuration. The result can
2344 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2345 * configuration into account. In this case, force a return of
2346 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2347 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2348 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2349 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2350 * systematically generated tests. */
2351 return( PSA_ERROR_NOT_SUPPORTED );
2352 }
2353
Ronald Cron79bdd822021-06-17 16:46:44 +02002354 return( PSA_SUCCESS );
Ronald Cron2dff3b22021-06-17 16:33:22 +02002355}
2356
Gilles Peskinee59236f2018-01-27 23:32:46 +01002357static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
2358 mbedtls_svc_key_id_t key,
2359 psa_algorithm_t alg,
2360 int is_sign )
2361{
2362 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2363 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002364 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002365
2366 /* A context must be freshly initialized before it can be set up. */
2367 if( operation->id != 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002368 {
Dave Rodgman54648242021-06-24 11:49:45 +01002369 status = PSA_ERROR_BAD_STATE;
2370 goto exit;
2371 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002372
2373 status = psa_get_and_lock_key_slot_with_policy(
2374 key,
2375 &slot,
Mateusz Starzykce0e6a92021-08-17 15:24:32 +02002376 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Gilles Peskine8c9def32018-02-08 10:02:12 +01002377 alg );
2378 if( status != PSA_SUCCESS )
Dave Rodgman54648242021-06-24 11:49:45 +01002379 goto exit;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002380
2381 psa_key_attributes_t attributes = {
2382 .core = slot->attr
2383 };
2384
Ronald Cron79bdd822021-06-17 16:46:44 +02002385 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2386 &operation->mac_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002387 if( status != PSA_SUCCESS )
2388 goto exit;
2389
2390 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002391 /* Dispatch the MAC setup call with validated input */
2392 if( is_sign )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002393 {
Steven Cooremane6804192021-03-19 18:28:56 +01002394 status = psa_driver_wrapper_mac_sign_setup( operation,
2395 &attributes,
2396 slot->key.data,
2397 slot->key.bytes,
2398 alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002399 }
2400 else
Gilles Peskinefbfac682018-07-08 20:51:54 +02002401 {
Steven Cooremane6804192021-03-19 18:28:56 +01002402 status = psa_driver_wrapper_mac_verify_setup( operation,
2403 &attributes,
2404 slot->key.data,
2405 slot->key.bytes,
2406 alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002407 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002408
Gilles Peskinefbfac682018-07-08 20:51:54 +02002409exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002410 if( status != PSA_SUCCESS )
Steven Cooremane6804192021-03-19 18:28:56 +01002411 psa_mac_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002412
Ronald Cron5c522922020-11-14 16:35:34 +01002413 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002414
Ronald Cron5c522922020-11-14 16:35:34 +01002415 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002416}
2417
Gilles Peskine89167cb2018-07-08 20:12:23 +02002418psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002419 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002420 psa_algorithm_t alg )
2421{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002422 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002423}
2424
2425psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002426 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002427 psa_algorithm_t alg )
2428{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002429 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002430}
2431
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002432psa_status_t psa_mac_update( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433 const uint8_t *input,
2434 size_t input_length )
2435{
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002436 if( operation->id == 0 )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002437 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002438
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002439 /* Don't require hash implementations to behave correctly on a
2440 * zero-length input, which may have an invalid pointer. */
2441 if( input_length == 0 )
2442 return( PSA_SUCCESS );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002443
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002444 psa_status_t status = psa_driver_wrapper_mac_update( operation,
2445 input, input_length );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002446 if( status != PSA_SUCCESS )
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002447 psa_mac_abort( operation );
2448
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002449 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450}
2451
Steven Cooremana5b860a2021-03-19 19:04:39 +01002452psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002453 uint8_t *mac,
2454 size_t mac_size,
2455 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002456{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002457 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2458 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002459
Steven Cooremana5b860a2021-03-19 19:04:39 +01002460 if( operation->id == 0 )
Dave Rodgman8036bdd2021-06-24 16:19:08 +01002461 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002462 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002463 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002464 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002465
Steven Cooreman72f736a2021-05-07 14:14:37 +02002466 if( ! operation->is_sign )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002467 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002468 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002469 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002470 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002471
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002472 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2473 * once all the error checks are done. */
2474 if( operation->mac_size == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002475 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002476 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002477 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002478 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002479
2480 if( mac_size < operation->mac_size )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002481 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002482 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002483 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002484 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002485
Steven Cooremana5b860a2021-03-19 19:04:39 +01002486 status = psa_driver_wrapper_mac_sign_finish( operation,
Steven Cooreman72f736a2021-05-07 14:14:37 +02002487 mac, operation->mac_size,
2488 mac_length );
2489
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002490exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002491 /* In case of success, set the potential excess room in the output buffer
2492 * to an invalid value, to avoid potentially leaking a longer MAC.
2493 * In case of error, set the output length and content to a safe default,
2494 * such that in case the caller misses an error check, the output would be
2495 * an unachievable MAC.
2496 */
2497 if( status != PSA_SUCCESS )
Steven Cooreman72f736a2021-05-07 14:14:37 +02002498 {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002499 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002500 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002501 }
mohammad16036df908f2018-04-02 08:34:15 -07002502
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002503 if( mac_size > operation->mac_size )
2504 memset( &mac[operation->mac_size], '!',
2505 mac_size - operation->mac_size );
2506
Steven Cooremana5b860a2021-03-19 19:04:39 +01002507 abort_status = psa_mac_abort( operation );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002508
Steven Cooremana5b860a2021-03-19 19:04:39 +01002509 return( status == PSA_SUCCESS ? abort_status : status );
mohammad16036df908f2018-04-02 08:34:15 -07002510}
2511
Steven Cooremana5b860a2021-03-19 19:04:39 +01002512psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002513 const uint8_t *mac,
2514 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002515{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002516 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2517 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002518
Steven Cooremana5b860a2021-03-19 19:04:39 +01002519 if( operation->id == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002520 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002521 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002522 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002523 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002524
Steven Cooreman72f736a2021-05-07 14:14:37 +02002525 if( operation->is_sign )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002526 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002527 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002528 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002529 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002530
2531 if( operation->mac_size != mac_length )
2532 {
2533 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002534 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002535 }
2536
Steven Cooremana5b860a2021-03-19 19:04:39 +01002537 status = psa_driver_wrapper_mac_verify_finish( operation,
2538 mac, mac_length );
Steven Cooreman72f736a2021-05-07 14:14:37 +02002539
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002540exit:
Steven Cooremana5b860a2021-03-19 19:04:39 +01002541 abort_status = psa_mac_abort( operation );
mohammad16036df908f2018-04-02 08:34:15 -07002542
Steven Cooremana5b860a2021-03-19 19:04:39 +01002543 return( status == PSA_SUCCESS ? abort_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002544}
2545
Ronald Croncd989b52021-06-18 14:23:33 +02002546static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key,
2547 psa_algorithm_t alg,
2548 const uint8_t *input,
2549 size_t input_length,
2550 uint8_t *mac,
2551 size_t mac_size,
2552 size_t *mac_length,
2553 int is_sign )
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002554{
2555 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002556 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2557 psa_key_slot_t *slot;
2558 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559
Ronald Cron51131b52021-06-17 17:17:20 +02002560 status = psa_get_and_lock_key_slot_with_policy(
Mateusz Starzykce0e6a92021-08-17 15:24:32 +02002561 key,
2562 &slot,
2563 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Ronald Croncd989b52021-06-18 14:23:33 +02002564 alg );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002565 if( status != PSA_SUCCESS )
2566 goto exit;
2567
Ronald Cron51131b52021-06-17 17:17:20 +02002568 psa_key_attributes_t attributes = {
2569 .core = slot->attr
2570 };
2571
2572 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2573 &operation_mac_size );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002574 if( status != PSA_SUCCESS )
2575 goto exit;
2576
Ronald Cron51131b52021-06-17 17:17:20 +02002577 if( mac_size < operation_mac_size )
2578 {
2579 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002580 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002581 }
2582
2583 status = psa_driver_wrapper_mac_compute(
2584 &attributes,
2585 slot->key.data, slot->key.bytes,
2586 alg,
2587 input, input_length,
2588 mac, operation_mac_size, mac_length );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002589
2590exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002591 /* In case of success, set the potential excess room in the output buffer
2592 * to an invalid value, to avoid potentially leaking a longer MAC.
2593 * In case of error, set the output length and content to a safe default,
2594 * such that in case the caller misses an error check, the output would be
2595 * an unachievable MAC.
2596 */
2597 if( status != PSA_SUCCESS )
Ronald Cron51131b52021-06-17 17:17:20 +02002598 {
Ronald Cron51131b52021-06-17 17:17:20 +02002599 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002600 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002601 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002602 if( mac_size > operation_mac_size )
2603 memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002604
Ronald Cron51131b52021-06-17 17:17:20 +02002605 unlock_status = psa_unlock_key_slot( slot );
2606
2607 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002608}
2609
Ronald Croncd989b52021-06-18 14:23:33 +02002610psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key,
2611 psa_algorithm_t alg,
2612 const uint8_t *input,
2613 size_t input_length,
2614 uint8_t *mac,
2615 size_t mac_size,
2616 size_t *mac_length)
2617{
2618 return( psa_mac_compute_internal( key, alg,
2619 input, input_length,
2620 mac, mac_size, mac_length, 1 ) );
2621}
2622
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002623psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key,
2624 psa_algorithm_t alg,
2625 const uint8_t *input,
2626 size_t input_length,
2627 const uint8_t *mac,
2628 size_t mac_length)
2629{
2630 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002631 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2632 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002633
Ronald Crona587cbc2021-06-18 14:51:29 +02002634 status = psa_mac_compute_internal( key, alg,
2635 input, input_length,
2636 actual_mac, sizeof( actual_mac ),
2637 &actual_mac_length, 0 );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002638 if( status != PSA_SUCCESS )
2639 goto exit;
2640
Ronald Crona587cbc2021-06-18 14:51:29 +02002641 if( mac_length != actual_mac_length )
2642 {
2643 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002644 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002645 }
2646 if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
2647 {
2648 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002649 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002650 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002651
2652exit:
Ronald Crona587cbc2021-06-18 14:51:29 +02002653 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002654
2655 return ( status );
2656}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002657
Gilles Peskinee59236f2018-01-27 23:32:46 +01002658/****************************************************************/
2659/* Asymmetric cryptography */
2660/****************************************************************/
2661
gabor-mezei-armf0486182021-05-12 11:03:09 +02002662static psa_status_t psa_sign_verify_check_alg( int input_is_message,
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002663 psa_algorithm_t alg )
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002664{
gabor-mezei-armf0486182021-05-12 11:03:09 +02002665 if( input_is_message )
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002666 {
2667 if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) )
2668 return( PSA_ERROR_INVALID_ARGUMENT );
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002669
Gilles Peskinef7b41372021-09-22 16:15:05 +02002670 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002671 {
2672 if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) )
2673 return( PSA_ERROR_INVALID_ARGUMENT );
2674 }
2675 }
2676 else
2677 {
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02002678 if( ! PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002679 return( PSA_ERROR_INVALID_ARGUMENT );
2680 }
2681
2682 return( PSA_SUCCESS );
2683}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002684
2685static psa_status_t psa_sign_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002686 int input_is_message,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002687 psa_algorithm_t alg,
2688 const uint8_t * input,
2689 size_t input_length,
2690 uint8_t * signature,
2691 size_t signature_size,
2692 size_t * signature_length )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002693{
2694 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2695 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2696 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002697
2698 *signature_length = 0;
2699
gabor-mezei-armf0486182021-05-12 11:03:09 +02002700 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002701 if( status != PSA_SUCCESS )
2702 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002703
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002704 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002705 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002706 * buffer can in principle be empty since it doesn't actually have
2707 * to be a hash.) */
2708 if( signature_size == 0 )
2709 return( PSA_ERROR_BUFFER_TOO_SMALL );
2710
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002711 status = psa_get_and_lock_key_slot_with_policy(
2712 key, &slot,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002713 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2714 PSA_KEY_USAGE_SIGN_HASH,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002715 alg );
2716
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002717 if( status != PSA_SUCCESS )
2718 goto exit;
2719
2720 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
2721 {
2722 status = PSA_ERROR_INVALID_ARGUMENT;
2723 goto exit;
2724 }
2725
2726 psa_key_attributes_t attributes = {
2727 .core = slot->attr
2728 };
2729
gabor-mezei-armf0486182021-05-12 11:03:09 +02002730 if( input_is_message )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002731 {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002732 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002733 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002734 alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002735 signature, signature_size, signature_length );
2736 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002737 else
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002738 {
2739
2740 status = psa_driver_wrapper_sign_hash(
2741 &attributes, slot->key.data, slot->key.bytes,
2742 alg, input, input_length,
2743 signature, signature_size, signature_length );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002744 }
2745
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002746
2747exit:
2748 /* Fill the unused part of the output buffer (the whole buffer on error,
2749 * the trailing part on success) with something that isn't a valid signature
2750 * (barring an attack on the signature and deliberately-crafted input),
2751 * in case the caller doesn't check the return status properly. */
2752 if( status == PSA_SUCCESS )
2753 memset( signature + *signature_length, '!',
2754 signature_size - *signature_length );
2755 else
2756 memset( signature, '!', signature_size );
2757 /* If signature_size is 0 then we have nothing to do. We must not call
2758 * memset because signature may be NULL in this case. */
2759
2760 unlock_status = psa_unlock_key_slot( slot );
2761
2762 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
2763}
2764
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002765static psa_status_t psa_verify_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002766 int input_is_message,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002767 psa_algorithm_t alg,
2768 const uint8_t * input,
2769 size_t input_length,
2770 const uint8_t * signature,
2771 size_t signature_length )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002772{
2773 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2774 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2775 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002776
gabor-mezei-armf0486182021-05-12 11:03:09 +02002777 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002778 if( status != PSA_SUCCESS )
2779 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002780
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002781 status = psa_get_and_lock_key_slot_with_policy(
2782 key, &slot,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002783 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2784 PSA_KEY_USAGE_VERIFY_HASH,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002785 alg );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002786
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002787 if( status != PSA_SUCCESS )
2788 return( status );
2789
2790 psa_key_attributes_t attributes = {
2791 .core = slot->attr
2792 };
2793
gabor-mezei-armf0486182021-05-12 11:03:09 +02002794 if( input_is_message )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002795 {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002796 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002797 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002798 alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002799 signature, signature_length );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002801 else
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002802 {
2803 status = psa_driver_wrapper_verify_hash(
2804 &attributes, slot->key.data, slot->key.bytes,
2805 alg, input, input_length,
2806 signature, signature_length );
2807 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002808
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002809 unlock_status = psa_unlock_key_slot( slot );
2810
2811 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002812
2813}
2814
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002815psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002816 const psa_key_attributes_t *attributes,
2817 const uint8_t *key_buffer,
2818 size_t key_buffer_size,
2819 psa_algorithm_t alg,
2820 const uint8_t *input,
2821 size_t input_length,
2822 uint8_t *signature,
2823 size_t signature_size,
2824 size_t *signature_length )
2825{
2826 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002827
Gilles Peskinef7b41372021-09-22 16:15:05 +02002828 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002829 {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002830 size_t hash_length;
2831 uint8_t hash[PSA_HASH_MAX_SIZE];
2832
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002833 status = psa_driver_wrapper_hash_compute(
2834 PSA_ALG_SIGN_GET_HASH( alg ),
2835 input, input_length,
2836 hash, sizeof( hash ), &hash_length );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002837
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002838 if( status != PSA_SUCCESS )
2839 return status;
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002840
2841 return psa_driver_wrapper_sign_hash(
2842 attributes, key_buffer, key_buffer_size,
2843 alg, hash, hash_length,
2844 signature, signature_size, signature_length );
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002845 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002846
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002847 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002848}
2849
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002850psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
2851 psa_algorithm_t alg,
2852 const uint8_t * input,
2853 size_t input_length,
2854 uint8_t * signature,
2855 size_t signature_size,
2856 size_t * signature_length )
2857{
2858 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002859 key, 1, alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002860 signature, signature_size, signature_length );
2861}
2862
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002863psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002864 const psa_key_attributes_t *attributes,
2865 const uint8_t *key_buffer,
2866 size_t key_buffer_size,
2867 psa_algorithm_t alg,
2868 const uint8_t *input,
2869 size_t input_length,
2870 const uint8_t *signature,
2871 size_t signature_length )
2872{
2873 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002874
Gilles Peskinef7b41372021-09-22 16:15:05 +02002875 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002876 {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002877 size_t hash_length;
2878 uint8_t hash[PSA_HASH_MAX_SIZE];
2879
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002880 status = psa_driver_wrapper_hash_compute(
2881 PSA_ALG_SIGN_GET_HASH( alg ),
2882 input, input_length,
2883 hash, sizeof( hash ), &hash_length );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002884
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002885 if( status != PSA_SUCCESS )
2886 return status;
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002887
2888 return psa_driver_wrapper_verify_hash(
2889 attributes, key_buffer, key_buffer_size,
2890 alg, hash, hash_length,
2891 signature, signature_length );
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002892 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002893
gabor-mezei-arm474a35f2021-05-05 13:59:01 +02002894 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002895}
2896
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002897psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
2898 psa_algorithm_t alg,
2899 const uint8_t * input,
2900 size_t input_length,
2901 const uint8_t * signature,
2902 size_t signature_length )
2903{
2904 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002905 key, 1, alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002906 signature, signature_length );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002907}
2908
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002909psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002910 const psa_key_attributes_t *attributes,
2911 const uint8_t *key_buffer, size_t key_buffer_size,
2912 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2913 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002914{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002915 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002916 {
Ronald Cron4501c982021-03-19 20:09:32 +01002917 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2918 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002919 {
Ronald Cron4501c982021-03-19 20:09:32 +01002920#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2921 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2922 return( mbedtls_psa_rsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002923 attributes,
2924 key_buffer, key_buffer_size,
2925 alg, hash, hash_length,
2926 signature, signature_size, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002927#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2928 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002929 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002930 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002931 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002932 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002933 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002934 }
Ronald Cron81ca97e2021-04-09 15:32:03 +02002935 else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Ronald Cron4501c982021-03-19 20:09:32 +01002936 {
2937 if( PSA_ALG_IS_ECDSA( alg ) )
2938 {
2939#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2940 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2941 return( mbedtls_psa_ecdsa_sign_hash(
2942 attributes,
2943 key_buffer, key_buffer_size,
2944 alg, hash, hash_length,
2945 signature, signature_size, signature_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002946#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2947 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron4501c982021-03-19 20:09:32 +01002948 }
2949 else
2950 {
2951 return( PSA_ERROR_INVALID_ARGUMENT );
2952 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002953 }
Ronald Cron4501c982021-03-19 20:09:32 +01002954
2955 (void)key_buffer;
2956 (void)key_buffer_size;
2957 (void)hash;
2958 (void)hash_length;
2959 (void)signature;
2960 (void)signature_size;
2961 (void)signature_length;
2962
2963 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01002964}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002965
2966psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
2967 psa_algorithm_t alg,
2968 const uint8_t *hash,
2969 size_t hash_length,
2970 uint8_t *signature,
2971 size_t signature_size,
2972 size_t *signature_length )
2973{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002974 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002975 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01002976 signature, signature_size, signature_length );
itayzafrir5c753392018-05-08 11:18:38 +03002977}
2978
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002979psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002980 const psa_key_attributes_t *attributes,
2981 const uint8_t *key_buffer, size_t key_buffer_size,
2982 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2983 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002984{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002985 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002986 {
Ronald Cron4501c982021-03-19 20:09:32 +01002987 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2988 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002989 {
Ronald Cron4501c982021-03-19 20:09:32 +01002990#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2991 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2992 return( mbedtls_psa_rsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002993 attributes,
2994 key_buffer, key_buffer_size,
2995 alg, hash, hash_length,
2996 signature, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002997#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2998 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002999 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003000 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003001 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003002 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003003 }
itayzafrir5c753392018-05-08 11:18:38 +03003004 }
Ronald Cron81ca97e2021-04-09 15:32:03 +02003005 else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003006 {
Ronald Cron4501c982021-03-19 20:09:32 +01003007 if( PSA_ALG_IS_ECDSA( alg ) )
3008 {
3009#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3010 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3011 return( mbedtls_psa_ecdsa_verify_hash(
3012 attributes,
3013 key_buffer, key_buffer_size,
3014 alg, hash, hash_length,
3015 signature, signature_length ) );
3016#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3017 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3018 }
3019 else
3020 {
3021 return( PSA_ERROR_INVALID_ARGUMENT );
3022 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003023 }
Ronald Cron4501c982021-03-19 20:09:32 +01003024
3025 (void)key_buffer;
3026 (void)key_buffer_size;
3027 (void)hash;
3028 (void)hash_length;
3029 (void)signature;
3030 (void)signature_length;
3031
3032 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01003033}
3034
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003035psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
3036 psa_algorithm_t alg,
3037 const uint8_t *hash,
3038 size_t hash_length,
3039 const uint8_t *signature,
3040 size_t signature_length )
3041{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003042 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003043 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01003044 signature, signature_length );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003045}
3046
Ronald Croncf56a0a2020-08-04 09:51:30 +02003047psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003048 psa_algorithm_t alg,
3049 const uint8_t *input,
3050 size_t input_length,
3051 const uint8_t *salt,
3052 size_t salt_length,
3053 uint8_t *output,
3054 size_t output_size,
3055 size_t *output_length )
3056{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003057 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003058 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003059 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003060
Darryl Green5cc689a2018-07-24 15:34:10 +01003061 (void) input;
3062 (void) input_length;
3063 (void) salt;
3064 (void) output;
3065 (void) output_size;
3066
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003067 *output_length = 0;
3068
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003069 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3070 return( PSA_ERROR_INVALID_ARGUMENT );
3071
Ronald Cron5c522922020-11-14 16:35:34 +01003072 status = psa_get_and_lock_transparent_key_slot_with_policy(
3073 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003074 if( status != PSA_SUCCESS )
3075 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003076 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3077 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003078 {
3079 status = PSA_ERROR_INVALID_ARGUMENT;
3080 goto exit;
3081 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003082
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003083 psa_key_attributes_t attributes = {
3084 .core = slot->attr
3085 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003086
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003087 status = psa_driver_wrapper_asymmetric_encrypt(
3088 &attributes, slot->key.data, slot->key.bytes,
3089 alg, input, input_length, salt, salt_length,
3090 output, output_size, output_length );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003091exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003092 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003093
Ronald Cron5c522922020-11-14 16:35:34 +01003094 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003095}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003096
Ronald Croncf56a0a2020-08-04 09:51:30 +02003097psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003098 psa_algorithm_t alg,
3099 const uint8_t *input,
3100 size_t input_length,
3101 const uint8_t *salt,
3102 size_t salt_length,
3103 uint8_t *output,
3104 size_t output_size,
3105 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003106{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003107 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003108 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003109 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003110
Darryl Green5cc689a2018-07-24 15:34:10 +01003111 (void) input;
3112 (void) input_length;
3113 (void) salt;
3114 (void) output;
3115 (void) output_size;
3116
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003117 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003118
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003119 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3120 return( PSA_ERROR_INVALID_ARGUMENT );
3121
Ronald Cron5c522922020-11-14 16:35:34 +01003122 status = psa_get_and_lock_transparent_key_slot_with_policy(
3123 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003124 if( status != PSA_SUCCESS )
3125 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003126 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003127 {
3128 status = PSA_ERROR_INVALID_ARGUMENT;
3129 goto exit;
3130 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003131
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003132 psa_key_attributes_t attributes = {
3133 .core = slot->attr
3134 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003135
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003136 status = psa_driver_wrapper_asymmetric_decrypt(
3137 &attributes, slot->key.data, slot->key.bytes,
3138 alg, input, input_length, salt, salt_length,
3139 output, output_size, output_length );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003140
3141exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003142 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003143
Ronald Cron5c522922020-11-14 16:35:34 +01003144 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003145}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003146
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003147
3148
mohammad1603503973b2018-03-12 15:59:30 +02003149/****************************************************************/
3150/* Symmetric cryptography */
3151/****************************************************************/
3152
Ronald Cronab99ac22020-10-01 16:38:28 +02003153static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
3154 mbedtls_svc_key_id_t key,
3155 psa_algorithm_t alg,
3156 mbedtls_operation_t cipher_operation )
3157{
3158 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3159 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003160 psa_key_slot_t *slot = NULL;
Ronald Cronab99ac22020-10-01 16:38:28 +02003161 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3162 PSA_KEY_USAGE_ENCRYPT :
3163 PSA_KEY_USAGE_DECRYPT );
3164
3165 /* A context must be freshly initialized before it can be set up. */
Ronald Cron49fafa92021-03-10 08:34:23 +01003166 if( operation->id != 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003167 {
Dave Rodgman54648242021-06-24 11:49:45 +01003168 status = PSA_ERROR_BAD_STATE;
3169 goto exit;
3170 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003171
Ronald Cronab99ac22020-10-01 16:38:28 +02003172 if( ! PSA_ALG_IS_CIPHER( alg ) )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003173 {
Dave Rodgman54648242021-06-24 11:49:45 +01003174 status = PSA_ERROR_INVALID_ARGUMENT;
3175 goto exit;
3176 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003177
Ronald Cronab99ac22020-10-01 16:38:28 +02003178 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
3179 if( status != PSA_SUCCESS )
3180 goto exit;
3181
Ronald Cron49fafa92021-03-10 08:34:23 +01003182 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003183 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003184 * so we only set it (in the driver wrapper) after resources have been
3185 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003186 operation->iv_set = 0;
Ronald Cronab99ac22020-10-01 16:38:28 +02003187 if( alg == PSA_ALG_ECB_NO_PADDING )
3188 operation->iv_required = 0;
3189 else
3190 operation->iv_required = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003191 operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
Ronald Cronab99ac22020-10-01 16:38:28 +02003192
Ronald Crona4af55f2020-12-14 14:36:06 +01003193 psa_key_attributes_t attributes = {
3194 .core = slot->attr
3195 };
3196
Ronald Cronab99ac22020-10-01 16:38:28 +02003197 /* Try doing the operation through a driver before using software fallback. */
3198 if( cipher_operation == MBEDTLS_ENCRYPT )
Ronald Crona4af55f2020-12-14 14:36:06 +01003199 status = psa_driver_wrapper_cipher_encrypt_setup( operation,
3200 &attributes,
3201 slot->key.data,
3202 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003203 alg );
3204 else
Ronald Crona4af55f2020-12-14 14:36:06 +01003205 status = psa_driver_wrapper_cipher_decrypt_setup( operation,
3206 &attributes,
3207 slot->key.data,
3208 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003209 alg );
3210
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003211exit:
Ronald Cron06aa4422021-03-09 17:32:57 +01003212 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003213 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003214
Ronald Cron5c522922020-11-14 16:35:34 +01003215 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003216
Ronald Cron5c522922020-11-14 16:35:34 +01003217 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003218}
3219
Gilles Peskinefe119512018-07-08 21:39:34 +02003220psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003221 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003222 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003223{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003224 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003225}
3226
Gilles Peskinefe119512018-07-08 21:39:34 +02003227psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003228 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003229 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003230{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003231 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003232}
3233
Gilles Peskinefe119512018-07-08 21:39:34 +02003234psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003235 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003236 size_t iv_size,
3237 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003238{
Ronald Cron6d051732020-10-01 14:10:20 +02003239 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003240 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3241 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01003242
Ronald Cron49fafa92021-03-10 08:34:23 +01003243 if( operation->id == 0 )
Ronald Cronc45b4af2020-09-29 16:18:05 +02003244 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003245 status = PSA_ERROR_BAD_STATE;
3246 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003247 }
3248
Steven Cooreman7df02922020-09-09 15:28:49 +02003249 if( operation->iv_set || ! operation->iv_required )
3250 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003251 status = PSA_ERROR_BAD_STATE;
3252 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003253 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003254
Ronald Cron2fb90522021-07-05 12:31:44 +02003255 default_iv_length = operation->default_iv_length;
3256 if( iv_size < default_iv_length )
Ronald Cron5618a392021-03-26 09:52:26 +01003257 {
3258 status = PSA_ERROR_BUFFER_TOO_SMALL;
3259 goto exit;
3260 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003261
Ronald Cron2fb90522021-07-05 12:31:44 +02003262 if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE )
3263 {
3264 status = PSA_ERROR_GENERIC_ERROR;
3265 goto exit;
3266 }
3267
3268 status = psa_generate_random( local_iv, default_iv_length );
Ronald Cron5618a392021-03-26 09:52:26 +01003269 if( status != PSA_SUCCESS )
3270 goto exit;
3271
3272 status = psa_driver_wrapper_cipher_set_iv( operation,
Ronald Cron2fb90522021-07-05 12:31:44 +02003273 local_iv, default_iv_length );
Ronald Cron5618a392021-03-26 09:52:26 +01003274
3275exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003276 if( status == PSA_SUCCESS )
Ronald Cron5618a392021-03-26 09:52:26 +01003277 {
Ronald Cron2fb90522021-07-05 12:31:44 +02003278 memcpy( iv, local_iv, default_iv_length );
3279 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003280 operation->iv_set = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003281 }
Steven Cooreman7df02922020-09-09 15:28:49 +02003282 else
Ronald Cron2fb90522021-07-05 12:31:44 +02003283 {
3284 *iv_length = 0;
Moran Peker395db872018-05-31 14:07:14 +03003285 psa_cipher_abort( operation );
Ronald Cron2fb90522021-07-05 12:31:44 +02003286 }
Ronald Cron6d051732020-10-01 14:10:20 +02003287
itayzafrir534bd7c2018-08-02 13:56:32 +03003288 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003289}
3290
Gilles Peskinefe119512018-07-08 21:39:34 +02003291psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003292 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003293 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003294{
Ronald Cron6d051732020-10-01 14:10:20 +02003295 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003296
Ronald Cron49fafa92021-03-10 08:34:23 +01003297 if( operation->id == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003298 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003299 status = PSA_ERROR_BAD_STATE;
3300 goto exit;
3301 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003302
Steven Cooreman7df02922020-09-09 15:28:49 +02003303 if( operation->iv_set || ! operation->iv_required )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003304 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003305 status = PSA_ERROR_BAD_STATE;
3306 goto exit;
3307 }
Ronald Crona0d68172021-03-26 10:15:08 +01003308
3309 if( iv_length > PSA_CIPHER_IV_MAX_SIZE )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003310 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003311 status = PSA_ERROR_INVALID_ARGUMENT;
3312 goto exit;
3313 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003314
Ronald Crondd24c9b2020-12-15 14:10:01 +01003315 status = psa_driver_wrapper_cipher_set_iv( operation,
3316 iv,
3317 iv_length );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003318
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003319exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003320 if( status == PSA_SUCCESS )
3321 operation->iv_set = 1;
3322 else
mohammad1603503973b2018-03-12 15:59:30 +02003323 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003324 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003325}
3326
Gilles Peskinee553c652018-06-04 16:22:46 +02003327psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3328 const uint8_t *input,
3329 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003330 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003331 size_t output_size,
3332 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003333{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003334 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003335
Ronald Cron49fafa92021-03-10 08:34:23 +01003336 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003337 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003338 status = PSA_ERROR_BAD_STATE;
3339 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003340 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003341
Steven Cooreman7df02922020-09-09 15:28:49 +02003342 if( operation->iv_required && ! operation->iv_set )
3343 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003344 status = PSA_ERROR_BAD_STATE;
3345 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003346 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003347
Ronald Crondd24c9b2020-12-15 14:10:01 +01003348 status = psa_driver_wrapper_cipher_update( operation,
3349 input,
3350 input_length,
3351 output,
3352 output_size,
3353 output_length );
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003354
3355exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003356 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003357 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003358
itayzafrir534bd7c2018-08-02 13:56:32 +03003359 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003360}
3361
Gilles Peskinee553c652018-06-04 16:22:46 +02003362psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3363 uint8_t *output,
3364 size_t output_size,
3365 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003366{
David Saadab4ecc272019-02-14 13:48:10 +02003367 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003368
Ronald Cron49fafa92021-03-10 08:34:23 +01003369 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003370 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003371 status = PSA_ERROR_BAD_STATE;
3372 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003373 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003374
Steven Cooreman7df02922020-09-09 15:28:49 +02003375 if( operation->iv_required && ! operation->iv_set )
3376 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003377 status = PSA_ERROR_BAD_STATE;
3378 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003379 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003380
Ronald Crondd24c9b2020-12-15 14:10:01 +01003381 status = psa_driver_wrapper_cipher_finish( operation,
3382 output,
3383 output_size,
3384 output_length );
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003385
3386exit:
Steven Cooremanef8575e2020-09-11 11:44:50 +02003387 if( status == PSA_SUCCESS )
3388 return( psa_cipher_abort( operation ) );
3389 else
3390 {
3391 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003392 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01003393
Steven Cooremanef8575e2020-09-11 11:44:50 +02003394 return( status );
3395 }
mohammad1603503973b2018-03-12 15:59:30 +02003396}
3397
Gilles Peskinee553c652018-06-04 16:22:46 +02003398psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3399{
Ronald Cron49fafa92021-03-10 08:34:23 +01003400 if( operation->id == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003401 {
Steven Cooremana07b9972020-09-10 14:54:14 +02003402 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003403 * in use. It's ok to call abort on such an object, and there's
3404 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003405 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003406 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003407
Ronald Crondd24c9b2020-12-15 14:10:01 +01003408 psa_driver_wrapper_cipher_abort( operation );
Gilles Peskinee553c652018-06-04 16:22:46 +02003409
Ronald Cron49fafa92021-03-10 08:34:23 +01003410 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003411 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003412 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003413
Moran Peker395db872018-05-31 14:07:14 +03003414 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003415}
3416
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003417psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key,
3418 psa_algorithm_t alg,
3419 const uint8_t *input,
3420 size_t input_length,
3421 uint8_t *output,
3422 size_t output_size,
3423 size_t *output_length )
3424{
3425 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003426 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02003427 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02003428 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3429 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02003430
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003431 if( ! PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron23919522021-07-08 19:00:07 +02003432 {
3433 status = PSA_ERROR_INVALID_ARGUMENT;
3434 goto exit;
3435 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003436
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003437 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3438 PSA_KEY_USAGE_ENCRYPT,
3439 alg );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003440 if( status != PSA_SUCCESS )
Ronald Cron23919522021-07-08 19:00:07 +02003441 goto exit;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003442
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003443 psa_key_attributes_t attributes = {
3444 .core = slot->attr
3445 };
3446
Ronald Cronc6e6f502021-07-09 18:44:58 +02003447 default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
3448 if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE )
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003449 {
Ronald Cronc6e6f502021-07-09 18:44:58 +02003450 status = PSA_ERROR_GENERIC_ERROR;
3451 goto exit;
3452 }
3453
3454 if( default_iv_length > 0 )
3455 {
3456 if( output_size < default_iv_length )
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003457 {
3458 status = PSA_ERROR_BUFFER_TOO_SMALL;
3459 goto exit;
3460 }
3461
Ronald Cronc6e6f502021-07-09 18:44:58 +02003462 status = psa_generate_random( local_iv, default_iv_length );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003463 if( status != PSA_SUCCESS )
3464 goto exit;
3465 }
3466
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003467 status = psa_driver_wrapper_cipher_encrypt(
3468 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02003469 alg, local_iv, default_iv_length, input, input_length,
3470 output + default_iv_length, output_size - default_iv_length,
3471 output_length );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003472
3473exit:
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003474 unlock_status = psa_unlock_key_slot( slot );
Ronald Cron23919522021-07-08 19:00:07 +02003475 if( status == PSA_SUCCESS )
3476 status = unlock_status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003477
Ronald Cron9b674282021-07-09 09:19:35 +02003478 if( status == PSA_SUCCESS )
Ronald Cronc6e6f502021-07-09 18:44:58 +02003479 {
3480 if( default_iv_length > 0 )
3481 memcpy( output, local_iv, default_iv_length );
3482 *output_length += default_iv_length;
3483 }
Ronald Cron9b674282021-07-09 09:19:35 +02003484 else
Ronald Cron23919522021-07-08 19:00:07 +02003485 *output_length = 0;
3486
3487 return( status );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003488}
3489
3490psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key,
3491 psa_algorithm_t alg,
3492 const uint8_t *input,
3493 size_t input_length,
3494 uint8_t *output,
3495 size_t output_size,
3496 size_t *output_length )
3497{
3498 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003499 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02003500 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02003501
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003502 if( ! PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron23919522021-07-08 19:00:07 +02003503 {
3504 status = PSA_ERROR_INVALID_ARGUMENT;
3505 goto exit;
3506 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003507
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003508 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3509 PSA_KEY_USAGE_DECRYPT,
3510 alg );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003511 if( status != PSA_SUCCESS )
Ronald Cron23919522021-07-08 19:00:07 +02003512 goto exit;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003513
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003514 psa_key_attributes_t attributes = {
3515 .core = slot->attr
3516 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003517
Mateusz Starzyk594215b2021-10-14 12:23:06 +02003518 if( alg == PSA_ALG_CCM_STAR_NO_TAG && input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) )
3519 {
3520 status = PSA_ERROR_INVALID_ARGUMENT;
3521 goto exit;
3522 }
3523 else if ( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) )
gabor-mezei-arm258ae072021-06-25 15:25:38 +02003524 {
3525 status = PSA_ERROR_INVALID_ARGUMENT;
3526 goto exit;
3527 }
3528
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003529 status = psa_driver_wrapper_cipher_decrypt(
3530 &attributes, slot->key.data, slot->key.bytes,
3531 alg, input, input_length,
3532 output, output_size, output_length );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003533
gabor-mezei-arm258ae072021-06-25 15:25:38 +02003534exit:
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003535 unlock_status = psa_unlock_key_slot( slot );
Ronald Cron23919522021-07-08 19:00:07 +02003536 if( status == PSA_SUCCESS )
3537 status = unlock_status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003538
Ronald Cron23919522021-07-08 19:00:07 +02003539 if( status != PSA_SUCCESS )
3540 *output_length = 0;
3541
3542 return( status );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003543}
3544
3545
mohammad16035955c982018-04-26 00:53:03 +03003546/****************************************************************/
3547/* AEAD */
3548/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003549
Paul Elliottbaff51c2021-09-28 17:44:45 +01003550/* Helper function to get the base algorithm from its variants. */
3551static psa_algorithm_t psa_aead_get_base_algorithm( psa_algorithm_t alg )
3552{
3553 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg );
3554}
3555
3556/* Helper function to perform common nonce length checks. */
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003557static psa_status_t psa_aead_check_nonce_length( psa_algorithm_t alg,
3558 size_t nonce_length )
3559{
Paul Elliottbaff51c2021-09-28 17:44:45 +01003560 psa_algorithm_t base_alg = psa_aead_get_base_algorithm( alg );
3561
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003562 switch(base_alg)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003563 {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003564#if defined(PSA_WANT_ALG_GCM)
3565 case PSA_ALG_GCM:
3566 /* Not checking max nonce size here as GCM spec allows almost
3567 * arbitrarily large nonces. Please note that we do not generally
3568 * recommend the usage of nonces of greater length than
3569 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
3570 * size, which can then lead to collisions if you encrypt a very
3571 * large number of messages.*/
3572 if( nonce_length != 0 )
3573 return( PSA_SUCCESS );
3574 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003575#endif /* PSA_WANT_ALG_GCM */
3576#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003577 case PSA_ALG_CCM:
3578 if( nonce_length >= 7 && nonce_length <= 13 )
3579 return( PSA_SUCCESS );
3580 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003581#endif /* PSA_WANT_ALG_CCM */
3582#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003583 case PSA_ALG_CHACHA20_POLY1305:
3584 if( nonce_length == 12 )
3585 return( PSA_SUCCESS );
Bence Szépkúti6d48e202021-11-15 20:04:15 +01003586 else if( nonce_length == 8 )
3587 return( PSA_ERROR_NOT_SUPPORTED );
3588 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003589#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003590 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02003591 (void) nonce_length;
Bence Szépkúti357b78e2021-11-09 13:17:17 +01003592 return( PSA_ERROR_NOT_SUPPORTED );
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003593 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003594
Bence Szépkúti357b78e2021-11-09 13:17:17 +01003595 return( PSA_ERROR_INVALID_ARGUMENT );
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003596}
3597
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01003598static psa_status_t psa_aead_check_algorithm( psa_algorithm_t alg )
3599{
Bence Szépkúti08f34652021-12-08 21:07:13 +01003600 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3601 return( PSA_ERROR_INVALID_ARGUMENT );
3602
3603 return( PSA_SUCCESS );
3604}
3605
Ronald Croncf56a0a2020-08-04 09:51:30 +02003606psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003607 psa_algorithm_t alg,
3608 const uint8_t *nonce,
3609 size_t nonce_length,
3610 const uint8_t *additional_data,
3611 size_t additional_data_length,
3612 const uint8_t *plaintext,
3613 size_t plaintext_length,
3614 uint8_t *ciphertext,
3615 size_t ciphertext_size,
3616 size_t *ciphertext_length )
3617{
Ronald Cron215633c2021-03-16 17:15:37 +01003618 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3619 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003620
mohammad1603f08a5502018-06-03 15:05:47 +03003621 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003622
Bence Szépkúti39fb9d12022-01-13 14:33:45 +01003623 status = psa_aead_check_algorithm( alg );
Bence Szépkúti08f34652021-12-08 21:07:13 +01003624 if( status != PSA_SUCCESS )
3625 return( status );
Steven Cooremanea7ab132021-03-17 16:28:00 +01003626
Ronald Cron9a986162021-03-26 12:40:07 +01003627 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003628 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003629 if( status != PSA_SUCCESS )
3630 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003631
Ronald Cron215633c2021-03-16 17:15:37 +01003632 psa_key_attributes_t attributes = {
3633 .core = slot->attr
3634 };
mohammad16035955c982018-04-26 00:53:03 +03003635
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003636 status = psa_aead_check_nonce_length( alg, nonce_length );
3637 if( status != PSA_SUCCESS )
3638 goto exit;
3639
Ronald Cronde822812021-03-17 16:08:20 +01003640 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003641 &attributes, slot->key.data, slot->key.bytes,
3642 alg,
3643 nonce, nonce_length,
3644 additional_data, additional_data_length,
3645 plaintext, plaintext_length,
3646 ciphertext, ciphertext_size, ciphertext_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003647
Gilles Peskineedf9a652018-08-17 18:11:56 +02003648 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3649 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003650
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003651exit:
Ronald Cron9f310172021-03-16 16:36:37 +01003652 psa_unlock_key_slot( slot );
mohammad16035955c982018-04-26 00:53:03 +03003653
mohammad16035955c982018-04-26 00:53:03 +03003654 return( status );
Gilles Peskineee652a32018-06-01 19:23:52 +02003655}
3656
Ronald Croncf56a0a2020-08-04 09:51:30 +02003657psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003658 psa_algorithm_t alg,
3659 const uint8_t *nonce,
3660 size_t nonce_length,
3661 const uint8_t *additional_data,
3662 size_t additional_data_length,
3663 const uint8_t *ciphertext,
3664 size_t ciphertext_length,
3665 uint8_t *plaintext,
3666 size_t plaintext_size,
3667 size_t *plaintext_length )
3668{
Ronald Cron215633c2021-03-16 17:15:37 +01003669 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3670 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003671
Gilles Peskineee652a32018-06-01 19:23:52 +02003672 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003673
Bence Szépkúti39fb9d12022-01-13 14:33:45 +01003674 status = psa_aead_check_algorithm( alg );
Bence Szépkúti08f34652021-12-08 21:07:13 +01003675 if( status != PSA_SUCCESS )
3676 return( status );
Steven Cooremanea7ab132021-03-17 16:28:00 +01003677
Ronald Cron9a986162021-03-26 12:40:07 +01003678 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003679 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003680 if( status != PSA_SUCCESS )
3681 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003682
Ronald Cron215633c2021-03-16 17:15:37 +01003683 psa_key_attributes_t attributes = {
3684 .core = slot->attr
3685 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003686
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003687 status = psa_aead_check_nonce_length( alg, nonce_length );
3688 if( status != PSA_SUCCESS )
3689 goto exit;
3690
Ronald Cronde822812021-03-17 16:08:20 +01003691 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003692 &attributes, slot->key.data, slot->key.bytes,
3693 alg,
3694 nonce, nonce_length,
3695 additional_data, additional_data_length,
3696 ciphertext, ciphertext_length,
3697 plaintext, plaintext_size, plaintext_length );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003698
Gilles Peskineedf9a652018-08-17 18:11:56 +02003699 if( status != PSA_SUCCESS && plaintext_size != 0 )
3700 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003701
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003702exit:
Ronald Cron215633c2021-03-16 17:15:37 +01003703 psa_unlock_key_slot( slot );
3704
Gilles Peskineedf9a652018-08-17 18:11:56 +02003705 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003706}
3707
Andrzej Kurekf8816012021-12-19 17:00:12 +01003708static psa_status_t psa_validate_tag_length( psa_aead_operation_t *operation,
3709 psa_algorithm_t alg ) {
3710 uint8_t tag_len = 0;
3711 if( psa_driver_get_tag_len( operation, &tag_len ) != PSA_SUCCESS )
3712 {
3713 return( PSA_ERROR_INVALID_ARGUMENT );
3714 }
3715
3716 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
3717 {
3718#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
3719 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
3720 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
3721 if( tag_len < 4 || tag_len > 16 || tag_len % 2 )
3722 return( PSA_ERROR_INVALID_ARGUMENT );
3723 break;
3724#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
3725
3726#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
3727 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
3728 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
3729 if( tag_len != 4 && tag_len != 8 && ( tag_len < 12 || tag_len > 16 ) )
3730 return( PSA_ERROR_INVALID_ARGUMENT );
3731 break;
3732#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
3733
3734#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
3735 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
3736 /* We only support the default tag length. */
3737 if( tag_len != 16 )
3738 return( PSA_ERROR_INVALID_ARGUMENT );
3739 break;
3740#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
3741
3742 default:
3743 (void) tag_len;
3744 return( PSA_ERROR_NOT_SUPPORTED );
3745 }
3746 return( PSA_SUCCESS );
3747}
3748
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003749/* Set the key for a multipart authenticated operation. */
3750static psa_status_t psa_aead_setup( psa_aead_operation_t *operation,
Paul Elliottd9343f22021-08-23 18:59:49 +01003751 int is_encrypt,
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003752 mbedtls_svc_key_id_t key,
3753 psa_algorithm_t alg )
Paul Elliottc2b71442021-06-24 18:17:52 +01003754{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003755 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3756 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3757 psa_key_slot_t *slot = NULL;
3758 psa_key_usage_t key_usage = 0;
3759
Bence Szépkúti39fb9d12022-01-13 14:33:45 +01003760 status = psa_aead_check_algorithm( alg );
Bence Szépkúti08f34652021-12-08 21:07:13 +01003761 if( status != PSA_SUCCESS )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003762 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003763
3764 if( operation->id != 0 )
3765 {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003766 status = PSA_ERROR_BAD_STATE;
3767 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003768 }
3769
3770 if( operation->nonce_set || operation->lengths_set ||
3771 operation->ad_started || operation->body_started )
3772 {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003773 status = PSA_ERROR_BAD_STATE;
3774 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003775 }
3776
Paul Elliottd9343f22021-08-23 18:59:49 +01003777 if( is_encrypt )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003778 key_usage = PSA_KEY_USAGE_ENCRYPT;
3779 else
3780 key_usage = PSA_KEY_USAGE_DECRYPT;
3781
3782 status = psa_get_and_lock_key_slot_with_policy( key, &slot, key_usage,
3783 alg );
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003784 if( status != PSA_SUCCESS )
3785 goto exit;
3786
3787 psa_key_attributes_t attributes = {
3788 .core = slot->attr
3789 };
3790
Paul Elliottd9343f22021-08-23 18:59:49 +01003791 if( is_encrypt )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003792 status = psa_driver_wrapper_aead_encrypt_setup( operation,
3793 &attributes,
3794 slot->key.data,
3795 slot->key.bytes,
3796 alg );
3797 else
3798 status = psa_driver_wrapper_aead_decrypt_setup( operation,
3799 &attributes,
3800 slot->key.data,
3801 slot->key.bytes,
3802 alg );
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003803 if( status != PSA_SUCCESS )
3804 goto exit;
3805
Andrzej Kurekf8816012021-12-19 17:00:12 +01003806 if( ( status = psa_validate_tag_length( operation, alg ) ) != PSA_SUCCESS )
3807 goto exit;
3808
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003809 operation->key_type = psa_get_key_type( &attributes );
3810
3811exit:
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003812 unlock_status = psa_unlock_key_slot( slot );
3813
3814 if( status == PSA_SUCCESS )
3815 {
3816 status = unlock_status;
3817 operation->alg = psa_aead_get_base_algorithm( alg );
Paul Elliottd9343f22021-08-23 18:59:49 +01003818 operation->is_encrypt = is_encrypt;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003819 }
3820 else
3821 psa_aead_abort( operation );
3822
3823 return( status );
Paul Elliottc2b71442021-06-24 18:17:52 +01003824}
3825
Paul Elliott302ff6b2021-04-20 18:10:30 +01003826/* Set the key for a multipart authenticated encryption operation. */
3827psa_status_t psa_aead_encrypt_setup( psa_aead_operation_t *operation,
3828 mbedtls_svc_key_id_t key,
3829 psa_algorithm_t alg )
3830{
Paul Elliottd9343f22021-08-23 18:59:49 +01003831 return( psa_aead_setup( operation, 1, key, alg ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003832}
3833
3834/* Set the key for a multipart authenticated decryption operation. */
3835psa_status_t psa_aead_decrypt_setup( psa_aead_operation_t *operation,
3836 mbedtls_svc_key_id_t key,
3837 psa_algorithm_t alg )
3838{
Paul Elliottd9343f22021-08-23 18:59:49 +01003839 return( psa_aead_setup( operation, 0, key, alg ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003840}
3841
3842/* Generate a random nonce / IV for multipart AEAD operation */
3843psa_status_t psa_aead_generate_nonce( psa_aead_operation_t *operation,
3844 uint8_t *nonce,
3845 size_t nonce_size,
3846 size_t *nonce_length )
3847{
3848 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01003849 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01003850 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003851
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003852 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003853
Paul Elliottcee785c2021-05-20 14:29:20 +01003854 if( operation->id == 0 )
3855 {
3856 status = PSA_ERROR_BAD_STATE;
3857 goto exit;
3858 }
3859
Paul Elliottdaf5c892021-08-25 16:24:58 +01003860 if( operation->nonce_set || !operation->is_encrypt )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003861 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003862 status = PSA_ERROR_BAD_STATE;
3863 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003864 }
3865
Paul Elliotte193ea82021-10-01 13:00:16 +01003866 /* For CCM, this size may not be correct according to the PSA
3867 * specification. The PSA Crypto 1.0.1 specification states:
3868 *
3869 * CCM encodes the plaintext length pLen in L octets, with L the smallest
3870 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
3871 *
3872 * However this restriction that L has to be the smallest integer is not
3873 * applied in practice, and it is not implementable here since the
3874 * plaintext length may or may not be known at this time. */
Paul Elliottbbe90b52021-05-11 22:22:42 +01003875 required_nonce_size = PSA_AEAD_NONCE_LENGTH( operation->key_type,
Paul Elliott3242f6c2021-08-25 16:33:47 +01003876 operation->alg );
Paul Elliott39dc6b82021-05-11 19:16:09 +01003877 if( nonce_size < required_nonce_size )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003878 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003879 status = PSA_ERROR_BUFFER_TOO_SMALL;
3880 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003881 }
3882
Ronald Croncae59092021-11-26 18:53:58 +01003883 status = psa_generate_random( local_nonce, required_nonce_size );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003884 if( status != PSA_SUCCESS )
Paul Elliott39dc6b82021-05-11 19:16:09 +01003885 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003886
Ronald Croncae59092021-11-26 18:53:58 +01003887 status = psa_aead_set_nonce( operation, local_nonce, required_nonce_size );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003888
Paul Elliott39dc6b82021-05-11 19:16:09 +01003889exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01003890 if( status == PSA_SUCCESS )
Ronald Croncae59092021-11-26 18:53:58 +01003891 {
3892 memcpy( nonce, local_nonce, required_nonce_size );
Paul Elliott39dc6b82021-05-11 19:16:09 +01003893 *nonce_length = required_nonce_size;
Ronald Croncae59092021-11-26 18:53:58 +01003894 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01003895 else
3896 psa_aead_abort( operation );
3897
3898 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003899}
3900
3901/* Set the nonce for a multipart authenticated encryption or decryption
3902 operation.*/
3903psa_status_t psa_aead_set_nonce( psa_aead_operation_t *operation,
3904 const uint8_t *nonce,
3905 size_t nonce_length )
3906{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003907 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3908
Paul Elliottcee785c2021-05-20 14:29:20 +01003909 if( operation->id == 0 )
3910 {
3911 status = PSA_ERROR_BAD_STATE;
3912 goto exit;
3913 }
3914
Paul Elliott3d7d52c2021-09-01 10:33:14 +01003915 if( operation->nonce_set )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003916 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003917 status = PSA_ERROR_BAD_STATE;
3918 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003919 }
3920
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003921 status = psa_aead_check_nonce_length( operation->alg, nonce_length );
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003922 if( status != PSA_SUCCESS )
Paul Elliott4ed1ed12021-09-27 18:09:28 +01003923 {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003924 status = PSA_ERROR_INVALID_ARGUMENT;
3925 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01003926 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01003927
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003928 status = psa_driver_wrapper_aead_set_nonce( operation, nonce,
3929 nonce_length );
3930
Paul Elliott39dc6b82021-05-11 19:16:09 +01003931exit:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003932 if( status == PSA_SUCCESS )
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003933 operation->nonce_set = 1;
Paul Elliott39dc6b82021-05-11 19:16:09 +01003934 else
3935 psa_aead_abort( operation );
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003936
3937 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003938}
3939
3940/* Declare the lengths of the message and additional data for multipart AEAD. */
3941psa_status_t psa_aead_set_lengths( psa_aead_operation_t *operation,
3942 size_t ad_length,
3943 size_t plaintext_length )
3944{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003945 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3946
Paul Elliottcee785c2021-05-20 14:29:20 +01003947 if( operation->id == 0 )
3948 {
3949 status = PSA_ERROR_BAD_STATE;
3950 goto exit;
3951 }
3952
Paul Elliott6eb95982021-05-21 17:41:41 +01003953 if( operation->lengths_set || operation->ad_started ||
Paul Elliott7f429b72021-06-24 18:08:54 +01003954 operation->body_started )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003955 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003956 status = PSA_ERROR_BAD_STATE;
3957 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003958 }
3959
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003960 switch(operation->alg)
Paul Elliott325d3742021-09-27 17:56:28 +01003961 {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003962#if defined(PSA_WANT_ALG_GCM)
3963 case PSA_ALG_GCM:
3964 /* Lengths can only be too large for GCM if size_t is bigger than 32
3965 * bits. Without the guard this code will generate warnings on 32bit
3966 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01003967#if SIZE_MAX > UINT32_MAX
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003968 if( (( uint64_t ) ad_length ) >> 61 != 0 ||
3969 (( uint64_t ) plaintext_length ) > 0xFFFFFFFE0ull )
3970 {
3971 status = PSA_ERROR_INVALID_ARGUMENT;
3972 goto exit;
3973 }
Paul Elliott325d3742021-09-27 17:56:28 +01003974#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003975 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003976#endif /* PSA_WANT_ALG_GCM */
3977#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003978 case PSA_ALG_CCM:
3979 if( ad_length > 0xFF00 )
3980 {
3981 status = PSA_ERROR_INVALID_ARGUMENT;
3982 goto exit;
3983 }
3984 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003985#endif /* PSA_WANT_ALG_CCM */
3986#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003987 case PSA_ALG_CHACHA20_POLY1305:
3988 /* No length restrictions for ChaChaPoly. */
3989 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003990#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003991 default:
3992 break;
3993 }
Paul Elliott325d3742021-09-27 17:56:28 +01003994
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003995 status = psa_driver_wrapper_aead_set_lengths( operation, ad_length,
3996 plaintext_length );
3997
Paul Elliott39dc6b82021-05-11 19:16:09 +01003998exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01003999 if( status == PSA_SUCCESS )
Paul Elliottee4ffe02021-05-20 17:25:06 +01004000 {
4001 operation->ad_remaining = ad_length;
4002 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004003 operation->lengths_set = 1;
Paul Elliottee4ffe02021-05-20 17:25:06 +01004004 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004005 else
4006 psa_aead_abort( operation );
4007
4008 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004009}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004010
4011/* Pass additional data to an active multipart AEAD operation. */
Paul Elliott302ff6b2021-04-20 18:10:30 +01004012psa_status_t psa_aead_update_ad( psa_aead_operation_t *operation,
4013 const uint8_t *input,
4014 size_t input_length )
4015{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4017
Paul Elliottcee785c2021-05-20 14:29:20 +01004018 if( operation->id == 0 )
4019 {
4020 status = PSA_ERROR_BAD_STATE;
4021 goto exit;
4022 }
4023
Paul Elliott6eb95982021-05-21 17:41:41 +01004024 if( !operation->nonce_set || operation->body_started )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004025 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004026 status = PSA_ERROR_BAD_STATE;
4027 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004028 }
4029
Paul Elliottee4ffe02021-05-20 17:25:06 +01004030 if( operation->lengths_set )
4031 {
Paul Elliott6eb95982021-05-21 17:41:41 +01004032 if( operation->ad_remaining < input_length )
Paul Elliottee4ffe02021-05-20 17:25:06 +01004033 {
4034 status = PSA_ERROR_INVALID_ARGUMENT;
4035 goto exit;
4036 }
4037
4038 operation->ad_remaining -= input_length;
4039 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004040#if defined(PSA_WANT_ALG_CCM)
4041 else if( operation->alg == PSA_ALG_CCM )
4042 {
4043 status = PSA_ERROR_BAD_STATE;
4044 goto exit;
4045 }
4046#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004047
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004048 status = psa_driver_wrapper_aead_update_ad( operation, input,
4049 input_length );
4050
Paul Elliott39dc6b82021-05-11 19:16:09 +01004051exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004052 if( status == PSA_SUCCESS )
4053 operation->ad_started = 1;
4054 else
4055 psa_aead_abort( operation );
4056
4057 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004058}
4059
4060/* Encrypt or decrypt a message fragment in an active multipart AEAD
4061 operation.*/
4062psa_status_t psa_aead_update( psa_aead_operation_t *operation,
4063 const uint8_t *input,
4064 size_t input_length,
4065 uint8_t *output,
4066 size_t output_size,
4067 size_t *output_length )
4068{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004069 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004070
4071 *output_length = 0;
4072
Paul Elliottcee785c2021-05-20 14:29:20 +01004073 if( operation->id == 0 )
4074 {
4075 status = PSA_ERROR_BAD_STATE;
4076 goto exit;
4077 }
4078
Paul Elliott6eb95982021-05-21 17:41:41 +01004079 if( !operation->nonce_set )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004080 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004081 status = PSA_ERROR_BAD_STATE;
4082 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004083 }
4084
Paul Elliottee4ffe02021-05-20 17:25:06 +01004085 if( operation->lengths_set )
4086 {
4087 /* Additional data length was supplied, but not all the additional
4088 data was supplied.*/
4089 if( operation->ad_remaining != 0 )
4090 {
4091 status = PSA_ERROR_INVALID_ARGUMENT;
4092 goto exit;
4093 }
4094
4095 /* Too much data provided. */
4096 if( operation->body_remaining < input_length )
4097 {
4098 status = PSA_ERROR_INVALID_ARGUMENT;
4099 goto exit;
4100 }
4101
4102 operation->body_remaining -= input_length;
4103 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004104#if defined(PSA_WANT_ALG_CCM)
4105 else if( operation->alg == PSA_ALG_CCM )
4106 {
4107 status = PSA_ERROR_BAD_STATE;
4108 goto exit;
4109 }
4110#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004111
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004112 status = psa_driver_wrapper_aead_update( operation, input, input_length,
4113 output, output_size,
4114 output_length );
4115
Paul Elliott39dc6b82021-05-11 19:16:09 +01004116exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004117 if( status == PSA_SUCCESS )
4118 operation->body_started = 1;
4119 else
4120 psa_aead_abort( operation );
4121
4122 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004123}
4124
Paul Elliott69bf5fc2021-09-19 18:26:37 +01004125static psa_status_t psa_aead_final_checks( const psa_aead_operation_t *operation )
Paul Elliottad53dcc2021-06-23 08:50:14 +01004126{
Paul Elliott7f429b72021-06-24 18:08:54 +01004127 if( operation->id == 0 || !operation->nonce_set )
Paul Elliottad53dcc2021-06-23 08:50:14 +01004128 return( PSA_ERROR_BAD_STATE );
4129
Paul Elliotta5614442021-07-14 14:54:11 +01004130 if( operation->lengths_set && ( operation->ad_remaining != 0 ||
Paul Elliottad53dcc2021-06-23 08:50:14 +01004131 operation->body_remaining != 0 ) )
4132 return( PSA_ERROR_INVALID_ARGUMENT );
4133
4134 return( PSA_SUCCESS );
4135}
4136
Paul Elliott302ff6b2021-04-20 18:10:30 +01004137/* Finish encrypting a message in a multipart AEAD operation. */
4138psa_status_t psa_aead_finish( psa_aead_operation_t *operation,
4139 uint8_t *ciphertext,
4140 size_t ciphertext_size,
4141 size_t *ciphertext_length,
4142 uint8_t *tag,
4143 size_t tag_size,
4144 size_t *tag_length )
4145{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004146 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4147
Paul Elliott302ff6b2021-04-20 18:10:30 +01004148 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004149 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004150
Paul Elliott814fffb2021-08-16 18:20:36 +01004151 status = psa_aead_final_checks( operation );
Paul Elliottad53dcc2021-06-23 08:50:14 +01004152 if( status != PSA_SUCCESS )
4153 goto exit;
4154
Paul Elliott7f429b72021-06-24 18:08:54 +01004155 if( !operation->is_encrypt )
Paul Elliottcee785c2021-05-20 14:29:20 +01004156 {
4157 status = PSA_ERROR_BAD_STATE;
4158 goto exit;
4159 }
4160
Paul Elliott39dc6b82021-05-11 19:16:09 +01004161 status = psa_driver_wrapper_aead_finish( operation, ciphertext,
4162 ciphertext_size,
4163 ciphertext_length,
4164 tag, tag_size, tag_length );
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004165
Paul Elliott39dc6b82021-05-11 19:16:09 +01004166exit:
Paul Elliottf47b0952021-05-21 18:02:33 +01004167 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004168 * the zero tag size, make sure the tag is set to something implausible.
4169 * Even if the operation succeeds, make sure we clear the rest of the
4170 * buffer to prevent potential leakage of anything previously placed in
4171 * the same buffer.*/
Paul Elliott90fdc112021-09-22 17:15:48 +01004172 if( tag != NULL )
Paul Elliottec95cc92021-09-19 22:33:09 +01004173 {
4174 if( status != PSA_SUCCESS )
4175 memset( tag, '!', tag_size );
4176 else if( *tag_length < tag_size )
4177 memset( tag + *tag_length, '!', ( tag_size - *tag_length ) );
4178 }
Paul Elliottf47b0952021-05-21 18:02:33 +01004179
Paul Elliott39dc6b82021-05-11 19:16:09 +01004180 psa_aead_abort( operation );
4181
4182 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004183}
4184
4185/* Finish authenticating and decrypting a message in a multipart AEAD
4186 operation.*/
4187psa_status_t psa_aead_verify( psa_aead_operation_t *operation,
4188 uint8_t *plaintext,
4189 size_t plaintext_size,
4190 size_t *plaintext_length,
4191 const uint8_t *tag,
4192 size_t tag_length )
4193{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004194 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4195
Paul Elliott302ff6b2021-04-20 18:10:30 +01004196 *plaintext_length = 0;
4197
Paul Elliott814fffb2021-08-16 18:20:36 +01004198 status = psa_aead_final_checks( operation );
Paul Elliottad53dcc2021-06-23 08:50:14 +01004199 if( status != PSA_SUCCESS )
4200 goto exit;
4201
Paul Elliott7f429b72021-06-24 18:08:54 +01004202 if( operation->is_encrypt )
Paul Elliottcee785c2021-05-20 14:29:20 +01004203 {
4204 status = PSA_ERROR_BAD_STATE;
4205 goto exit;
4206 }
4207
Paul Elliott39dc6b82021-05-11 19:16:09 +01004208 status = psa_driver_wrapper_aead_verify( operation, plaintext,
4209 plaintext_size,
4210 plaintext_length,
4211 tag, tag_length );
4212
4213exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004214 psa_aead_abort( operation );
4215
4216 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004217}
4218
4219/* Abort an AEAD operation. */
Paul Elliottbbe90b52021-05-11 22:22:42 +01004220psa_status_t psa_aead_abort( psa_aead_operation_t *operation )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004221{
4222 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4223
4224 if( operation->id == 0 )
4225 {
4226 /* The object has (apparently) been initialized but it is not (yet)
4227 * in use. It's ok to call abort on such an object, and there's
4228 * nothing to do. */
4229 return( PSA_SUCCESS );
4230 }
4231
4232 status = psa_driver_wrapper_aead_abort( operation );
4233
Paul Elliott70618b22021-09-22 17:12:16 +01004234 memset( operation, 0, sizeof( *operation ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004235
4236 return( status );
4237}
4238
Gilles Peskinee59236f2018-01-27 23:32:46 +01004239/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004240/* Generators */
4241/****************************************************************/
4242
Przemek Stekiel69c46792022-06-10 12:59:51 +02004243#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004244 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004245 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4246 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004247#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) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004353#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04004354 if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004355 {
4356 mbedtls_platform_zeroize( operation->ctx.tls12_ecjpake_to_pms.data,
Andrzej Kurek5603efd2022-09-26 10:49:16 -04004357 sizeof( operation->ctx.tls12_ecjpake_to_pms.data ) );
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004358 }
4359 else
4360#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004361 {
4362 status = PSA_ERROR_BAD_STATE;
4363 }
Janos Follath083036a2019-06-11 10:22:26 +01004364 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004365 return( status );
4366}
4367
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004368psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004369 size_t *capacity)
4370{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004371 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004372 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004374 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004375 }
4376
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004377 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004378 return( PSA_SUCCESS );
4379}
4380
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004381psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004382 size_t capacity )
4383{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004384 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004385 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004386 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004387 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004388 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004389 return( PSA_SUCCESS );
4390}
4391
Przemek Stekiel69c46792022-06-10 12:59:51 +02004392#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004393/* Read some bytes from an HKDF-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004394static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004395 psa_algorithm_t kdf_alg,
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004396 uint8_t *output,
4397 size_t output_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004398{
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004399 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004400 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004401 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004402 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004403#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004404 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004405#else
4406 const uint8_t last_block = 0xff;
4407#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004408
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004409 if( hkdf->state < HKDF_STATE_KEYED ||
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004410 ( !hkdf->info_set
4411#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
4412 && !PSA_ALG_IS_HKDF_EXTRACT( kdf_alg )
4413#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
4414 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004415 return( PSA_ERROR_BAD_STATE );
4416 hkdf->state = HKDF_STATE_OUTPUT;
4417
Gilles Peskinebef7f142018-07-12 17:22:21 +02004418 while( output_length != 0 )
4419 {
4420 /* Copy what remains of the current block */
4421 uint8_t n = hash_length - hkdf->offset_in_block;
4422 if( n > output_length )
4423 n = (uint8_t) output_length;
4424 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4425 output += n;
4426 output_length -= n;
4427 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004428 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004429 break;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004430 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004431 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004432 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004433 * object was corrupted or if this function is called directly
4434 * inside the library. */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004435 if( hkdf->block_number == last_block )
Gilles Peskined54931c2018-07-17 21:06:59 +02004436 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004437
4438 /* We need a new block */
4439 ++hkdf->block_number;
4440 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004441
Steven Cooreman094a77e2021-05-06 17:58:36 +02004442 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4443 hash_alg,
4444 hkdf->prk,
4445 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004446 if( status != PSA_SUCCESS )
4447 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004448
Gilles Peskinebef7f142018-07-12 17:22:21 +02004449 if( hkdf->block_number != 1 )
4450 {
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004451 status = psa_mac_update( &hkdf->hmac,
4452 hkdf->output_block,
4453 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004454 if( status != PSA_SUCCESS )
4455 return( status );
4456 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004457 status = psa_mac_update( &hkdf->hmac,
4458 hkdf->info,
4459 hkdf->info_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004460 if( status != PSA_SUCCESS )
4461 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004462 status = psa_mac_update( &hkdf->hmac,
4463 &hkdf->block_number, 1 );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004464 if( status != PSA_SUCCESS )
4465 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004466 status = psa_mac_sign_finish( &hkdf->hmac,
4467 hkdf->output_block,
4468 sizeof( hkdf->output_block ),
4469 &hmac_output_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004470 if( status != PSA_SUCCESS )
4471 return( status );
4472 }
4473
4474 return( PSA_SUCCESS );
4475}
Przemek Stekiel69c46792022-06-10 12:59:51 +02004476#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004477
John Durkop07cc04a2020-11-16 22:08:34 -08004478#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4479 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004480static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4481 psa_tls12_prf_key_derivation_t *tls12_prf,
4482 psa_algorithm_t alg )
4483{
4484 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004485 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremana6df6042021-04-29 19:32:25 +02004486 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4487 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004488 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004489
Janos Follath7742fee2019-06-17 12:58:10 +01004490 /* We can't be wanting more output after block 0xff, otherwise
4491 * the capacity check in psa_key_derivation_output_bytes() would have
4492 * prevented this call. It could happen only if the operation
4493 * object was corrupted or if this function is called directly
4494 * inside the library. */
4495 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004496 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004497
4498 /* We need a new block */
4499 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004500 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004501
4502 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4503 *
4504 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4505 *
4506 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4507 * HMAC_hash(secret, A(2) + seed) +
4508 * HMAC_hash(secret, A(3) + seed) + ...
4509 *
4510 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004511 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004512 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004513 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004514 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004515 * is currently extracted as `output_block` and where i is
4516 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004517 */
4518
Steven Cooreman094a77e2021-05-06 17:58:36 +02004519 status = psa_key_derivation_start_hmac( &hmac,
4520 hash_alg,
4521 tls12_prf->secret,
4522 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004523 if( status != PSA_SUCCESS )
4524 goto cleanup;
4525
4526 /* Calculate A(i) where i = tls12_prf->block_number. */
4527 if( tls12_prf->block_number == 1 )
4528 {
4529 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4530 * the variable seed and in this instance means it in the context of the
4531 * P_hash function, where seed = label + seed.) */
Steven Cooremana6df6042021-04-29 19:32:25 +02004532 status = psa_mac_update( &hmac,
4533 tls12_prf->label,
4534 tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004535 if( status != PSA_SUCCESS )
4536 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004537 status = psa_mac_update( &hmac,
4538 tls12_prf->seed,
4539 tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004540 if( status != PSA_SUCCESS )
4541 goto cleanup;
4542 }
4543 else
4544 {
4545 /* A(i) = HMAC_hash(secret, A(i-1)) */
Steven Cooremana6df6042021-04-29 19:32:25 +02004546 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004547 if( status != PSA_SUCCESS )
4548 goto cleanup;
4549 }
4550
Steven Cooremana6df6042021-04-29 19:32:25 +02004551 status = psa_mac_sign_finish( &hmac,
4552 tls12_prf->Ai, hash_length,
4553 &hmac_output_length );
4554 if( hmac_output_length != hash_length )
4555 status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follathea29bfb2019-06-19 12:21:20 +01004556 if( status != PSA_SUCCESS )
4557 goto cleanup;
4558
4559 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Steven Cooreman094a77e2021-05-06 17:58:36 +02004560 status = psa_key_derivation_start_hmac( &hmac,
4561 hash_alg,
4562 tls12_prf->secret,
4563 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004564 if( status != PSA_SUCCESS )
4565 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004566 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004567 if( status != PSA_SUCCESS )
4568 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004569 status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004570 if( status != PSA_SUCCESS )
4571 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004572 status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004573 if( status != PSA_SUCCESS )
4574 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004575 status = psa_mac_sign_finish( &hmac,
4576 tls12_prf->output_block, hash_length,
4577 &hmac_output_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004578 if( status != PSA_SUCCESS )
4579 goto cleanup;
4580
Janos Follath7742fee2019-06-17 12:58:10 +01004581
4582cleanup:
Steven Cooremana6df6042021-04-29 19:32:25 +02004583 cleanup_status = psa_mac_abort( &hmac );
Janos Follathea29bfb2019-06-19 12:21:20 +01004584 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4585 status = cleanup_status;
4586
4587 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004588}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004589
Janos Follath844eb0e2019-06-19 12:10:49 +01004590static psa_status_t psa_key_derivation_tls12_prf_read(
4591 psa_tls12_prf_key_derivation_t *tls12_prf,
4592 psa_algorithm_t alg,
4593 uint8_t *output,
4594 size_t output_length )
4595{
4596 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004597 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01004598 psa_status_t status;
4599 uint8_t offset, length;
4600
Gilles Peskineb1edaec2021-06-11 22:41:46 +02004601 switch( tls12_prf->state )
4602 {
4603 case PSA_TLS12_PRF_STATE_LABEL_SET:
4604 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4605 break;
4606 case PSA_TLS12_PRF_STATE_OUTPUT:
4607 break;
4608 default:
4609 return( PSA_ERROR_BAD_STATE );
4610 }
4611
Janos Follath844eb0e2019-06-19 12:10:49 +01004612 while( output_length != 0 )
4613 {
4614 /* Check if we have fully processed the current block. */
4615 if( tls12_prf->left_in_block == 0 )
4616 {
4617 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4618 alg );
4619 if( status != PSA_SUCCESS )
4620 return( status );
4621
4622 continue;
4623 }
4624
4625 if( tls12_prf->left_in_block > output_length )
4626 length = (uint8_t) output_length;
4627 else
4628 length = tls12_prf->left_in_block;
4629
4630 offset = hash_length - tls12_prf->left_in_block;
4631 memcpy( output, tls12_prf->output_block + offset, length );
4632 output += length;
4633 output_length -= length;
4634 tls12_prf->left_in_block -= length;
4635 }
4636
4637 return( PSA_SUCCESS );
4638}
John Durkop07cc04a2020-11-16 22:08:34 -08004639#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4640 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004641
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004642#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
4643static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
4644 psa_tls12_ecjpake_to_pms_t *ecjpake,
4645 uint8_t *output,
4646 size_t output_length )
4647{
4648 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04004649 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004650
4651 if( output_length != 32 )
4652 return ( PSA_ERROR_INVALID_ARGUMENT );
4653
4654 status = psa_hash_compute( PSA_ALG_SHA_256, ecjpake->data,
4655 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
4656 &output_size );
4657 if( status != PSA_SUCCESS )
4658 return ( status );
4659
4660 if( output_size != output_length )
4661 return ( PSA_ERROR_GENERIC_ERROR );
4662
4663 return ( PSA_SUCCESS );
4664}
4665#endif
4666
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004667psa_status_t psa_key_derivation_output_bytes(
4668 psa_key_derivation_operation_t *operation,
4669 uint8_t *output,
4670 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004671{
4672 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004673 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004674
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004676 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004677 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004678 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004679 }
4680
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004682 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004683 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004684 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004685 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004686 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004687 goto exit;
4688 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004689 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004690 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004691 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004692 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004693 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4694 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004695 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004696 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004697 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004698 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004699 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004700
Przemek Stekiel69c46792022-06-10 12:59:51 +02004701#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiela29b4882022-06-02 11:37:03 +02004702 if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004703 {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004704 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, kdf_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004705 output, output_length );
4706 }
Janos Follathadbec812019-06-14 11:05:39 +01004707 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004708#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004709#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4710 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01004711 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08004712 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004713 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004714 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Steven Cooremana6df6042021-04-29 19:32:25 +02004715 kdf_alg, output,
4716 output_length );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004717 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004718 else
John Durkop07cc04a2020-11-16 22:08:34 -08004719#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4720 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004721#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04004722 if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004723 {
4724 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
4725 &operation->ctx.tls12_ecjpake_to_pms, output, output_length );
4726 }
4727 else
4728#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
4729
Gilles Peskineeab56e42018-07-12 17:12:33 +02004730 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004731 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004732 return( PSA_ERROR_BAD_STATE );
4733 }
4734
4735exit:
4736 if( status != PSA_SUCCESS )
4737 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004738 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739 * This allows us to differentiate between exhausted operations and
4740 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4741 * operations. */
4742 psa_algorithm_t alg = operation->alg;
4743 psa_key_derivation_abort( operation );
4744 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745 memset( output, '!', output_length );
4746 }
4747 return( status );
4748}
4749
David Brown7807bf72021-02-09 16:28:23 -07004750#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02004751static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4752{
4753 if( data_size >= 8 )
4754 mbedtls_des_key_set_parity( data );
4755 if( data_size >= 16 )
4756 mbedtls_des_key_set_parity( data + 8 );
4757 if( data_size >= 24 )
4758 mbedtls_des_key_set_parity( data + 16 );
4759}
David Brown7807bf72021-02-09 16:28:23 -07004760#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004761
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004762/*
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +01004763* ECC keys on a Weierstrass elliptic curve require the generation
4764* of a private key which is an integer
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004765* in the range [1, N - 1], where N is the boundary of the private key domain:
4766* N is the prime p for Diffie-Hellman, or the order of the
4767* curve’s base point for ECC.
4768*
4769* Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
4770* This function generates the private key using the following process:
4771*
4772* 1. Draw a byte string of length ceiling(m/8) bytes.
4773* 2. If m is not a multiple of 8, set the most significant
4774* (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
4775* 3. Convert the string to integer k by decoding it as a big-endian byte string.
4776* 4. If k > N - 2, discard the result and return to step 1.
4777* 5. Output k + 1 as the private key.
4778*
4779* This method allows compliance to NIST standards, specifically the methods titled
4780* Key-Pair Generation by Testing Candidates in the following publications:
4781* - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
4782* Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
4783* Diffie-Hellman keys.
4784*
4785* - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
4786* Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004787*
4788* Note: Function allocates memory for *data buffer, so given *data should be
4789* always NULL.
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004790*/
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004791#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4792 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4793 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4794 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
4795 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004796static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004797 psa_key_slot_t *slot,
4798 size_t bits,
4799 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004800 uint8_t **data
4801 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004802{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004803 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004804 mbedtls_mpi k;
4805 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004806 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004807 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004808
4809 mbedtls_mpi_init( &k );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004810 mbedtls_mpi_init( &diff_N_2 );
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004811
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004812 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
4813 slot->attr.type );
4814 mbedtls_ecp_group_id grp_id =
4815 mbedtls_ecc_group_of_psa( curve, bits, 0 );
4816
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01004817 if( grp_id == MBEDTLS_ECP_DP_NONE )
4818 {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004819 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01004820 goto cleanup;
4821 }
4822
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004823 mbedtls_ecp_group ecp_group;
Przemyslaw Stekiel65348162021-11-18 11:57:07 +01004824 mbedtls_ecp_group_init( &ecp_group );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004825
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004826 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ecp_group, grp_id ) );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004827
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004828 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004829 /* Let m be the bit size of N. */
4830 size_t m = ecp_group.nbits;
4831
4832 size_t m_bytes = PSA_BITS_TO_BYTES( m );
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004833
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004834 /* Calculate N - 2 - it will be needed later. */
4835 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &diff_N_2, &ecp_group.N, 2 ) );
4836
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004837 /* Note: This function is always called with *data == NULL and it
4838 * allocates memory for the data buffer. */
4839 *data = mbedtls_calloc( 1, m_bytes );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004840 if( *data == NULL )
4841 {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004842 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004843 goto cleanup;
4844 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004845
Przemek Stekielc85f0912022-03-08 11:37:54 +01004846 while( key_out_of_range )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004847 {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004848 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Przemek Stekielc85f0912022-03-08 11:37:54 +01004849 if( ( status = psa_key_derivation_output_bytes( operation, *data, m_bytes ) ) != 0 )
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004850 goto cleanup;
4851
4852 /* 2. If m is not a multiple of 8 */
Przemek Stekielc85f0912022-03-08 11:37:54 +01004853 if( m % 8 != 0 )
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004854 {
4855 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004856 * (8 * ceiling(m/8) - m) bits of the first byte in
4857 * the string to zero.
4858 */
4859 uint8_t clear_bit_mask = ( 1 << ( m % 8 ) ) - 1;
4860 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004861 }
4862
4863 /* 3. Convert the string to integer k by decoding it as a
4864 * big-endian byte string.
4865 */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004866 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &k, *data, m_bytes ) );
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004867
4868 /* 4. If k > N - 2, discard the result and return to step 1.
4869 * Result of comparison is returned. When it indicates error
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004870 * then this function is called again.
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004871 */
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004872 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( &diff_N_2, &k, &key_out_of_range ) );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004873 }
4874
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004875 /* 5. Output k + 1 as the private key. */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004876 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &k, &k, 1 ) );
4877 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &k, *data, m_bytes ) );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004878cleanup:
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004879 if( ret != 0 )
Przemyslaw Stekieldc8d7d92021-12-02 09:15:20 +01004880 status = mbedtls_to_psa_error( ret );
Przemek Stekielc85f0912022-03-08 11:37:54 +01004881 if( status != PSA_SUCCESS ) {
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004882 mbedtls_free( *data );
4883 *data = NULL;
4884 }
4885 mbedtls_mpi_free( &k );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004886 mbedtls_mpi_free( &diff_N_2 );
Przemyslaw Stekieldc8d7d92021-12-02 09:15:20 +01004887 return( status );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004888}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004889
4890/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
4891 * is determined by the curve, and sets the mandatory bits accordingly. That is:
4892 *
4893 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
4894 * draw a 32-byte string and process it as specified in
4895 * Elliptic Curves for Security [RFC7748] §5.
4896 *
4897 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
4898 * draw a 56-byte string and process it as specified in [RFC7748] §5.
4899 *
4900 * Note: Function allocates memory for *data buffer, so given *data should be
4901 * always NULL.
4902 */
4903
4904static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
4905 size_t bits,
4906 psa_key_derivation_operation_t *operation,
4907 uint8_t **data
4908 )
4909{
4910 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004911 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004912
4913 switch( bits )
4914 {
4915 case 255:
4916 output_length = 32;
4917 break;
4918 case 448:
4919 output_length = 56;
4920 break;
4921 default:
4922 return( PSA_ERROR_INVALID_ARGUMENT );
4923 break;
4924 }
4925
4926 *data = mbedtls_calloc( 1, output_length );
4927
4928 if( *data == NULL )
4929 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4930
4931 status = psa_key_derivation_output_bytes( operation, *data, output_length );
4932
4933 if( status != PSA_SUCCESS )
4934 return status;
4935
4936 switch( bits )
4937 {
4938 case 255:
4939 (*data)[0] &= 248;
4940 (*data)[31] &= 127;
4941 (*data)[31] |= 64;
4942 break;
4943 case 448:
4944 (*data)[0] &= 252;
4945 (*data)[55] |= 128;
4946 break;
4947 default:
Przemek Stekiela81aed22022-03-01 15:13:30 +01004948 return( PSA_ERROR_CORRUPTION_DETECTED );
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004949 break;
4950 }
4951
4952 return status;
4953}
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 Stekield8cdcba2021-11-15 12:38:53 +01004959
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004960static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004961 psa_key_slot_t *slot,
4962 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004964{
4965 uint8_t *data = NULL;
4966 size_t bytes = PSA_BITS_TO_BYTES( bits );
Archanad8a83dc2021-06-14 10:04:16 +05304967 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004969
Przemek Stekieldcab6cc2022-03-01 14:22:29 +01004970 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004971 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004972
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004973#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4974 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4975 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4976 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
4977 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemek Stekielc85f0912022-03-08 11:37:54 +01004978 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004979 {
Przemyslaw Stekiel92481592022-01-04 10:09:46 +01004980 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type );
Przemek Stekielc85f0912022-03-08 11:37:54 +01004981 if( PSA_ECC_FAMILY_IS_WEIERSTRASS( curve ) )
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004982 {
4983 /* Weierstrass elliptic curve */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004984 status = psa_generate_derived_ecc_key_weierstrass_helper( slot, bits, operation, &data );
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004985 if( status != PSA_SUCCESS )
4986 goto exit;
Przemyslaw Stekiel50fcc532021-11-26 10:54:52 +01004987 }
4988 else
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004989 {
4990 /* Montgomery elliptic curve */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004991 status = psa_generate_derived_ecc_key_montgomery_helper( bits, operation, &data );
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004992 if( status != PSA_SUCCESS )
4993 goto exit;
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004994 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004995 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004996#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
4997 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
4998 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4999 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5000 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005001 if( key_type_is_raw_bytes( slot->attr.type ) )
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005002 {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005003 if( bits % 8 != 0 )
5004 return( PSA_ERROR_INVALID_ARGUMENT );
5005 data = mbedtls_calloc( 1, bytes );
5006 if( data == NULL )
5007 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5008
5009 status = psa_key_derivation_output_bytes( operation, data, bytes );
5010 if( status != PSA_SUCCESS )
5011 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07005012#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005013 if( slot->attr.type == PSA_KEY_TYPE_DES )
5014 psa_des_set_key_parity( data, bytes );
Przemek Stekielf110dc02022-03-01 14:48:05 +01005015#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005016 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005017 else
Przemek Stekieldcab6cc2022-03-01 14:22:29 +01005018 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Crondd04d422020-11-28 15:14:42 +01005019
Ronald Cronbf33c932020-11-28 18:06:53 +01005020 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005021 psa_key_attributes_t attributes = {
5022 .core = slot->attr
5023 };
5024
Archanad8a83dc2021-06-14 10:04:16 +05305025 if( psa_key_lifetime_is_external( attributes.core.lifetime ) )
5026 {
Archana449608b2021-09-08 15:36:05 +05305027 status = psa_driver_wrapper_get_key_buffer_size( &attributes,
5028 &storage_size );
Archanad8a83dc2021-06-14 10:04:16 +05305029 if( status != PSA_SUCCESS )
5030 goto exit;
5031 }
5032 status = psa_allocate_buffer_to_slot( slot, storage_size );
5033 if( status != PSA_SUCCESS )
5034 goto exit;
5035
Ronald Cronbf33c932020-11-28 18:06:53 +01005036 status = psa_driver_wrapper_import_key( &attributes,
5037 data, bytes,
5038 slot->key.data,
5039 slot->key.bytes,
5040 &slot->key.bytes, &bits );
5041 if( bits != slot->attr.bits )
5042 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005043
5044exit:
5045 mbedtls_free( data );
5046 return( status );
5047}
5048
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005049psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005050 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005051 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005052{
5053 psa_status_t status;
5054 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005055 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005056
Ronald Cron81709fc2020-11-14 12:10:32 +01005057 *key = MBEDTLS_SVC_KEY_ID_INIT;
5058
Gilles Peskine0f84d622019-09-12 19:03:13 +02005059 /* Reject any attempt to create a zero-length key so that we don't
5060 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5061 if( psa_get_key_bits( attributes ) == 0 )
5062 return( PSA_ERROR_INVALID_ARGUMENT );
5063
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005064 if( operation->alg == PSA_ALG_NONE )
5065 return( PSA_ERROR_BAD_STATE );
5066
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005067 if( ! operation->can_output_key )
5068 return( PSA_ERROR_NOT_PERMITTED );
5069
Ronald Cron81709fc2020-11-14 12:10:32 +01005070 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5071 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005072#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5073 if( driver != NULL )
5074 {
5075 /* Deriving a key in a secure element is not implemented yet. */
5076 status = PSA_ERROR_NOT_SUPPORTED;
5077 }
5078#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005079 if( status == PSA_SUCCESS )
5080 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005081 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005082 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005083 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005084 }
5085 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005086 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005087 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005088 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005089
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005090 return( status );
5091}
5092
Gilles Peskineeab56e42018-07-12 17:12:33 +02005093
5094
5095/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005096/* Key derivation */
5097/****************************************************************/
5098
Steven Cooreman932ffb72021-02-15 12:14:32 +01005099#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005100static int is_kdf_alg_supported( psa_algorithm_t kdf_alg )
5101{
5102#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
5103 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5104 return( 1 );
5105#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005106#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
5107 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5108 return( 1 );
5109#endif
5110#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
5111 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) )
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005112 return( 1 );
5113#endif
5114#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5115 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5116 return( 1 );
5117#endif
5118#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5119 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5120 return( 1 );
5121#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005122#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04005123 if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005124 return( 1 );
5125#endif
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005126 return( 0 );
5127}
5128
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005129static psa_status_t psa_hash_try_support( psa_algorithm_t alg )
5130{
5131 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
5132 psa_status_t status = psa_hash_setup( &operation, alg );
5133 psa_hash_abort( &operation );
5134 return( status );
5135}
5136
Gilles Peskine969c5d62019-01-16 15:53:06 +01005137static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005138 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005139 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005140{
Janos Follath5fe19732019-06-20 15:09:30 +01005141 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5142 * initialisers for this union leave some bytes unspecified.) */
5143 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5144
Gilles Peskine969c5d62019-01-16 15:53:06 +01005145 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005146 if( ! is_kdf_alg_supported( kdf_alg ) )
5147 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop07cc04a2020-11-16 22:08:34 -08005148
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005149 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005150 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005151 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
5152 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04005153 if( kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005154 {
5155 if( hash_size == 0 )
5156 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005157
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005158 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5159 * we might fail later, which is somewhat unfriendly and potentially
5160 * risk-prone. */
5161 psa_status_t status = psa_hash_try_support( hash_alg );
5162 if( status != PSA_SUCCESS )
5163 return( status );
5164 }
5165 else
5166 {
5167 hash_size = PSA_HASH_LENGTH( PSA_ALG_SHA_256 );
5168 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005169
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005170 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5171 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
5172 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005173 {
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005174 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005175 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005176#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005177 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurekb0936502022-09-16 07:13:00 -04005178 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ||
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04005179 ( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) )
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005180 operation->capacity = hash_size;
5181 else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005182#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5183 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005184 operation->capacity = 255 * hash_size;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005185 return( PSA_SUCCESS );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005186}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005187
5188static psa_status_t psa_key_agreement_try_support( psa_algorithm_t alg )
5189{
5190#if defined(PSA_WANT_ALG_ECDH)
5191 if( alg == PSA_ALG_ECDH )
5192 return( PSA_SUCCESS );
5193#endif
5194 (void) alg;
5195 return( PSA_ERROR_NOT_SUPPORTED );
5196}
John Durkop07cc04a2020-11-16 22:08:34 -08005197#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005198
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005200 psa_algorithm_t alg )
5201{
5202 psa_status_t status;
5203
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005204 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005205 return( PSA_ERROR_BAD_STATE );
5206
Gilles Peskine6843c292019-01-18 16:44:49 +01005207 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5208 return( PSA_ERROR_INVALID_ARGUMENT );
5209 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005210 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005211#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005212 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005213 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( alg );
5214 status = psa_key_agreement_try_support( ka_alg );
5215 if( status != PSA_SUCCESS )
5216 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005218#else
5219 return( PSA_ERROR_NOT_SUPPORTED );
5220#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005221 }
5222 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5223 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005224#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005225 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005226#else
5227 return( PSA_ERROR_NOT_SUPPORTED );
5228#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005229 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005230 else
5231 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005232
5233 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005234 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005235 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005236}
5237
Przemek Stekiel69c46792022-06-10 12:59:51 +02005238#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005239static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005240 psa_algorithm_t kdf_alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005241 psa_key_derivation_step_t step,
5242 const uint8_t *data,
5243 size_t data_length )
5244{
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005245 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005246 psa_status_t status;
5247 switch( step )
5248 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005249 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005250#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005251 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) )
5252 return( PSA_ERROR_INVALID_ARGUMENT );
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005253#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine2b522db2019-04-12 15:11:49 +02005254 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005255 return( PSA_ERROR_BAD_STATE );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005256 else
5257 {
Steven Cooreman094a77e2021-05-06 17:58:36 +02005258 status = psa_key_derivation_start_hmac( &hkdf->hmac,
5259 hash_alg,
5260 data, data_length );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005261 if( status != PSA_SUCCESS )
5262 return( status );
5263 hkdf->state = HKDF_STATE_STARTED;
5264 return( PSA_SUCCESS );
5265 }
Gilles Peskine03410b52019-05-16 16:05:19 +02005266 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005267#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005268 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005269 {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02005270 /* We shouldn't be in different state as HKDF_EXPAND only allows
5271 * two inputs: SECRET (this case) and INFO which does not modify
5272 * the state. It could happen only if the hkdf
5273 * object was corrupted. */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005274 if( hkdf->state != HKDF_STATE_INIT )
5275 return( PSA_ERROR_BAD_STATE );
5276
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02005277 /* Allow only input that fits expected prk size */
5278 if( data_length != PSA_HASH_LENGTH( hash_alg ) )
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005279 return( PSA_ERROR_INVALID_ARGUMENT );
5280
5281 memcpy( hkdf->prk, data, data_length );
5282 }
5283 else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005284#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005285 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005286 /* HKDF: If no salt was provided, use an empty salt.
5287 * HKDF-EXTRACT: salt is mandatory. */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005288 if( hkdf->state == HKDF_STATE_INIT )
5289 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005290#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
5291 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5292 return( PSA_ERROR_BAD_STATE );
5293#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005294 status = psa_key_derivation_start_hmac( &hkdf->hmac,
5295 hash_alg,
5296 NULL, 0 );
5297 if( status != PSA_SUCCESS )
5298 return( status );
5299 hkdf->state = HKDF_STATE_STARTED;
5300 }
5301 if( hkdf->state != HKDF_STATE_STARTED )
5302 return( PSA_ERROR_BAD_STATE );
5303 status = psa_mac_update( &hkdf->hmac,
5304 data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005305 if( status != PSA_SUCCESS )
5306 return( status );
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005307 status = psa_mac_sign_finish( &hkdf->hmac,
5308 hkdf->prk,
5309 sizeof( hkdf->prk ),
5310 &data_length );
5311 if( status != PSA_SUCCESS )
5312 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005313 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005314
Gilles Peskine2b522db2019-04-12 15:11:49 +02005315 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005316 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005317#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005318 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5319 {
5320 /* The only block of output is the PRK. */
5321 memcpy( hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH( hash_alg ) );
5322 hkdf->offset_in_block = 0;
5323 }
5324 else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005325#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005326 {
5327 /* Block 0 is empty, and the next block will be
5328 * generated by psa_key_derivation_hkdf_read(). */
5329 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
5330 }
5331
Gilles Peskine2b522db2019-04-12 15:11:49 +02005332 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005333 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005334#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005335 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5336 return( PSA_ERROR_INVALID_ARGUMENT );
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005337#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02005338#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
5339 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) &&
5340 hkdf->state == HKDF_STATE_INIT )
5341 return( PSA_ERROR_BAD_STATE );
5342#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005343 if( hkdf->state == HKDF_STATE_OUTPUT )
5344 return( PSA_ERROR_BAD_STATE );
5345 if( hkdf->info_set )
5346 return( PSA_ERROR_BAD_STATE );
5347 hkdf->info_length = data_length;
5348 if( data_length != 0 )
5349 {
5350 hkdf->info = mbedtls_calloc( 1, data_length );
5351 if( hkdf->info == NULL )
5352 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5353 memcpy( hkdf->info, data, data_length );
5354 }
5355 hkdf->info_set = 1;
5356 return( PSA_SUCCESS );
5357 default:
5358 return( PSA_ERROR_INVALID_ARGUMENT );
5359 }
5360}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005361#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005362
John Durkop07cc04a2020-11-16 22:08:34 -08005363#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5364 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005365static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5366 const uint8_t *data,
5367 size_t data_length )
5368{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005369 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005370 return( PSA_ERROR_BAD_STATE );
5371
Janos Follathd6dce9f2019-07-04 09:11:38 +01005372 if( data_length != 0 )
5373 {
5374 prf->seed = mbedtls_calloc( 1, data_length );
5375 if( prf->seed == NULL )
5376 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005377
Janos Follathd6dce9f2019-07-04 09:11:38 +01005378 memcpy( prf->seed, data, data_length );
5379 prf->seed_length = data_length;
5380 }
Janos Follathf08e2652019-06-13 09:05:41 +01005381
Gilles Peskine43f958b2020-12-13 14:55:14 +01005382 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005383
5384 return( PSA_SUCCESS );
5385}
5386
Janos Follath81550542019-06-13 14:26:34 +01005387static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
Janos Follath81550542019-06-13 14:26:34 +01005388 const uint8_t *data,
5389 size_t data_length )
5390{
Przemek Stekield7a28642022-04-07 14:58:33 +02005391 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
5392 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET )
Janos Follath81550542019-06-13 14:26:34 +01005393 return( PSA_ERROR_BAD_STATE );
5394
Steven Cooremana6df6042021-04-29 19:32:25 +02005395 if( data_length != 0 )
5396 {
5397 prf->secret = mbedtls_calloc( 1, data_length );
5398 if( prf->secret == NULL )
5399 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5400
5401 memcpy( prf->secret, data, data_length );
5402 prf->secret_length = data_length;
5403 }
Janos Follath81550542019-06-13 14:26:34 +01005404
Gilles Peskine43f958b2020-12-13 14:55:14 +01005405 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005406
5407 return( PSA_SUCCESS );
5408}
5409
Janos Follath63028dd2019-06-13 09:15:47 +01005410static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5411 const uint8_t *data,
5412 size_t data_length )
5413{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005414 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005415 return( PSA_ERROR_BAD_STATE );
5416
Janos Follathd6dce9f2019-07-04 09:11:38 +01005417 if( data_length != 0 )
5418 {
5419 prf->label = mbedtls_calloc( 1, data_length );
5420 if( prf->label == NULL )
5421 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005422
Janos Follathd6dce9f2019-07-04 09:11:38 +01005423 memcpy( prf->label, data, data_length );
5424 prf->label_length = data_length;
5425 }
Janos Follath63028dd2019-06-13 09:15:47 +01005426
Gilles Peskine43f958b2020-12-13 14:55:14 +01005427 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005428
5429 return( PSA_SUCCESS );
5430}
5431
Janos Follathb03233e2019-06-11 15:30:30 +01005432static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
Janos Follathb03233e2019-06-11 15:30:30 +01005433 psa_key_derivation_step_t step,
5434 const uint8_t *data,
5435 size_t data_length )
5436{
Janos Follathb03233e2019-06-11 15:30:30 +01005437 switch( step )
5438 {
Janos Follathf08e2652019-06-13 09:05:41 +01005439 case PSA_KEY_DERIVATION_INPUT_SEED:
5440 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005441 case PSA_KEY_DERIVATION_INPUT_SECRET:
Steven Cooremana6df6042021-04-29 19:32:25 +02005442 return( psa_tls12_prf_set_key( prf, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005443 case PSA_KEY_DERIVATION_INPUT_LABEL:
5444 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005445 default:
5446 return( PSA_ERROR_INVALID_ARGUMENT );
5447 }
5448}
John Durkop07cc04a2020-11-16 22:08:34 -08005449#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5450 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5451
5452#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5453static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5454 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005455 const uint8_t *data,
5456 size_t data_length )
5457{
5458 psa_status_t status;
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005459 const size_t pms_len = ( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
5460 4 + data_length + prf->other_secret_length :
5461 4 + 2 * data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08005462
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005463 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005464 return( PSA_ERROR_INVALID_ARGUMENT );
5465
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005466 uint8_t *pms = mbedtls_calloc( 1, pms_len );
Przemek Stekiel937b90f2022-04-20 08:33:13 +02005467 if( pms == NULL )
5468 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005469 uint8_t *cur = pms;
5470
5471 /* pure-PSK:
5472 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08005473 *
5474 * The premaster secret is formed as follows: if the PSK is N octets
5475 * long, concatenate a uint16 with the value N, N zero octets, a second
5476 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005477 *
5478 * mixed-PSK:
5479 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
5480 * follows: concatenate a uint16 with the length of the other secret,
5481 * the other secret itself, uint16 with the length of PSK, and the
5482 * PSK itself.
5483 * For details please check:
5484 * - RFC 4279, Section 4 for the definition of RSA-PSK,
5485 * - RFC 4279, Section 3 for the definition of DHE-PSK,
5486 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08005487 */
5488
Przemek Stekiel4e47a912022-04-21 11:40:18 +02005489 if( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET )
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005490 {
5491 *cur++ = MBEDTLS_BYTE_1( prf->other_secret_length );
5492 *cur++ = MBEDTLS_BYTE_0( prf->other_secret_length );
Przemek Stekiel4e47a912022-04-21 11:40:18 +02005493 if( prf->other_secret_length != 0 )
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02005494 {
5495 memcpy( cur, prf->other_secret, prf->other_secret_length );
Przemek Stekiel03faf5d22022-04-20 08:37:43 +02005496 mbedtls_platform_zeroize( prf->other_secret, prf->other_secret_length );
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02005497 cur += prf->other_secret_length;
5498 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005499 }
5500 else
5501 {
5502 *cur++ = MBEDTLS_BYTE_1( data_length );
5503 *cur++ = MBEDTLS_BYTE_0( data_length );
5504 memset( cur, 0, data_length );
5505 cur += data_length;
5506 }
5507
Joe Subbiani5ecac212021-06-24 13:00:03 +01005508 *cur++ = MBEDTLS_BYTE_1( data_length );
5509 *cur++ = MBEDTLS_BYTE_0( data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08005510 memcpy( cur, data, data_length );
5511 cur += data_length;
5512
Steven Cooremana6df6042021-04-29 19:32:25 +02005513 status = psa_tls12_prf_set_key( prf, pms, cur - pms );
John Durkop07cc04a2020-11-16 22:08:34 -08005514
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005515 mbedtls_platform_zeroize( pms, pms_len );
5516 mbedtls_free( pms );
John Durkop07cc04a2020-11-16 22:08:34 -08005517 return( status );
5518}
Janos Follath6660f0e2019-06-17 08:44:03 +01005519
Przemek Stekiele47201b2022-04-19 13:53:28 +02005520static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
5521 psa_tls12_prf_key_derivation_t *prf,
5522 const uint8_t *data,
5523 size_t data_length )
5524{
5525 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
5526 return( PSA_ERROR_BAD_STATE );
5527
5528 if( data_length != 0 )
5529 {
5530 prf->other_secret = mbedtls_calloc( 1, data_length );
5531 if( prf->other_secret == NULL )
5532 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5533
5534 memcpy( prf->other_secret, data, data_length );
5535 prf->other_secret_length = data_length;
5536 }
5537 else
5538 {
5539 prf->other_secret_length = 0;
5540 }
5541
5542 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
5543
5544 return( PSA_SUCCESS );
5545}
5546
Janos Follath6660f0e2019-06-17 08:44:03 +01005547static psa_status_t psa_tls12_prf_psk_to_ms_input(
5548 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005549 psa_key_derivation_step_t step,
5550 const uint8_t *data,
5551 size_t data_length )
5552{
Przemek Stekiele47201b2022-04-19 13:53:28 +02005553 switch( step )
Janos Follath0c1ed842019-06-28 13:35:36 +01005554 {
Przemek Stekiele47201b2022-04-19 13:53:28 +02005555 case PSA_KEY_DERIVATION_INPUT_SECRET:
5556 return( psa_tls12_prf_psk_to_ms_set_key( prf,
5557 data, data_length ) );
5558 break;
5559 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
5560 return( psa_tls12_prf_psk_to_ms_set_other_key( prf,
5561 data,
5562 data_length ) );
5563 break;
5564 default:
5565 return( psa_tls12_prf_input( prf, step, data, data_length ) );
5566 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01005567
Przemek Stekiele47201b2022-04-19 13:53:28 +02005568 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005569}
John Durkop07cc04a2020-11-16 22:08:34 -08005570#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005571
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005572#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5573static psa_status_t psa_tls12_ecjpake_to_pms_input(
5574 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04005575 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005576 const uint8_t *data,
5577 size_t data_length )
5578{
Andrzej Kurek702776f2022-09-16 06:22:44 -04005579 if( data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
5580 step != PSA_KEY_DERIVATION_INPUT_SECRET )
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005581 {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005582 return( PSA_ERROR_INVALID_ARGUMENT );
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005583 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005584
5585 /* Check if the passed point is in an uncompressed form */
5586 if( data[0] != 0x04 )
5587 return( PSA_ERROR_INVALID_ARGUMENT );
5588
5589 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
5590 memcpy( ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
5591
5592 return( PSA_SUCCESS );
5593}
5594#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02005595/** Check whether the given key type is acceptable for the given
5596 * input step of a key derivation.
5597 *
5598 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5599 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5600 * Both secret and non-secret inputs can alternatively have the type
5601 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5602 * that the input was passed as a buffer rather than via a key object.
5603 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005604static int psa_key_derivation_check_input_type(
5605 psa_key_derivation_step_t step,
5606 psa_key_type_t key_type )
5607{
5608 switch( step )
5609 {
5610 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005611 if( key_type == PSA_KEY_TYPE_DERIVE )
5612 return( PSA_SUCCESS );
5613 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005614 return( PSA_SUCCESS );
5615 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02005616 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
5617 if( key_type == PSA_KEY_TYPE_DERIVE )
5618 return( PSA_SUCCESS );
5619 if( key_type == PSA_KEY_TYPE_NONE )
5620 return( PSA_SUCCESS );
5621 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005622 case PSA_KEY_DERIVATION_INPUT_LABEL:
5623 case PSA_KEY_DERIVATION_INPUT_SALT:
5624 case PSA_KEY_DERIVATION_INPUT_INFO:
5625 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005626 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5627 return( PSA_SUCCESS );
5628 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005629 return( PSA_SUCCESS );
5630 break;
5631 }
5632 return( PSA_ERROR_INVALID_ARGUMENT );
5633}
5634
Janos Follathb80a94e2019-06-12 15:54:46 +01005635static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005636 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005637 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005638 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005639 const uint8_t *data,
5640 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005641{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005642 psa_status_t status;
5643 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5644
5645 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005646 if( status != PSA_SUCCESS )
5647 goto exit;
5648
Przemek Stekiel69c46792022-06-10 12:59:51 +02005649#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiela29b4882022-06-02 11:37:03 +02005650 if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005651 {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005652 status = psa_hkdf_input( &operation->ctx.hkdf, kdf_alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005653 step, data, data_length );
5654 }
John Durkop07cc04a2020-11-16 22:08:34 -08005655 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005656#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005657#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5658 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005659 {
Janos Follathb03233e2019-06-11 15:30:30 +01005660 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
Janos Follathb03233e2019-06-11 15:30:30 +01005661 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005662 }
John Durkop07cc04a2020-11-16 22:08:34 -08005663 else
5664#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5665#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5666 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005667 {
5668 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005669 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005670 }
5671 else
John Durkop07cc04a2020-11-16 22:08:34 -08005672#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005673#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04005674 if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005675 {
5676 status = psa_tls12_ecjpake_to_pms_input(
Andrzej Kurek702776f2022-09-16 06:22:44 -04005677 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length );
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005678 }
5679 else
5680#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005681 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005682 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005683 (void) data;
5684 (void) data_length;
5685 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005686 return( PSA_ERROR_BAD_STATE );
5687 }
5688
Gilles Peskine224b0d62019-09-23 18:13:17 +02005689exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005690 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005691 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005692 return( status );
5693}
5694
Janos Follath51f4a0f2019-06-14 11:35:55 +01005695psa_status_t psa_key_derivation_input_bytes(
5696 psa_key_derivation_operation_t *operation,
5697 psa_key_derivation_step_t step,
5698 const uint8_t *data,
5699 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005700{
Gilles Peskineb8965192019-09-24 16:21:10 +02005701 return( psa_key_derivation_input_internal( operation, step,
5702 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005703 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005704}
5705
Janos Follath51f4a0f2019-06-14 11:35:55 +01005706psa_status_t psa_key_derivation_input_key(
5707 psa_key_derivation_operation_t *operation,
5708 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005709 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005710{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005711 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005712 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005713 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005714
Ronald Cron5c522922020-11-14 16:35:34 +01005715 status = psa_get_and_lock_transparent_key_slot_with_policy(
5716 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005717 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005718 {
5719 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005720 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005721 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005722
5723 /* Passing a key object as a SECRET input unlocks the permission
5724 * to output to a key object. */
5725 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5726 operation->can_output_key = 1;
5727
Ronald Cronf95a2b12020-10-22 15:24:49 +02005728 status = psa_key_derivation_input_internal( operation,
5729 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005730 slot->key.data,
5731 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005732
Ronald Cron5c522922020-11-14 16:35:34 +01005733 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005734
Ronald Cron5c522922020-11-14 16:35:34 +01005735 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005736}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005737
5738
5739
5740/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005741/* Key agreement */
5742/****************************************************************/
5743
John Durkop6ba40d12020-11-10 08:50:04 -08005744#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005745static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5746 size_t peer_key_length,
5747 const mbedtls_ecp_keypair *our_key,
5748 uint8_t *shared_secret,
5749 size_t shared_secret_size,
5750 size_t *shared_secret_length )
5751{
Steven Cooremand4867872020-08-05 16:31:39 +02005752 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005753 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005754 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005755 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005756 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005757 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005758
Ronald Cron90857082020-11-25 14:58:33 +01005759 status = mbedtls_psa_ecp_load_representation(
5760 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005761 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005762 peer_key,
5763 peer_key_length,
5764 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005765 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005766 goto exit;
5767
Jaeden Amero97271b32019-01-10 19:38:51 +00005768 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005769 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005770 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005771 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005772 status = mbedtls_to_psa_error(
5773 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5774 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005775 goto exit;
5776
Jaeden Amero97271b32019-01-10 19:38:51 +00005777 status = mbedtls_to_psa_error(
5778 mbedtls_ecdh_calc_secret( &ecdh,
5779 shared_secret_length,
5780 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005781 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005782 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005783 if( status != PSA_SUCCESS )
5784 goto exit;
5785 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5786 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005787
5788exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005789 if( status != PSA_SUCCESS )
5790 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005791 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005792 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005793 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005794
Jaeden Amero97271b32019-01-10 19:38:51 +00005795 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005796}
John Durkop6ba40d12020-11-10 08:50:04 -08005797#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005798
Gilles Peskine01d718c2018-09-18 12:01:02 +02005799#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5800
Gilles Peskine0216fe12019-04-11 21:23:21 +02005801static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5802 psa_key_slot_t *private_key,
5803 const uint8_t *peer_key,
5804 size_t peer_key_length,
5805 uint8_t *shared_secret,
5806 size_t shared_secret_size,
5807 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005808{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005809 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005810 {
John Durkop6ba40d12020-11-10 08:50:04 -08005811#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005812 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005813 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005814 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005815 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005816 psa_status_t status = mbedtls_psa_ecp_load_representation(
5817 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005818 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005819 private_key->key.data,
5820 private_key->key.bytes,
5821 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005822 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005823 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005824 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005825 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005826 shared_secret, shared_secret_size,
5827 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005828 mbedtls_ecp_keypair_free( ecp );
5829 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005830 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005831#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005832 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005833 (void) private_key;
5834 (void) peer_key;
5835 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005836 (void) shared_secret;
5837 (void) shared_secret_size;
5838 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005839 return( PSA_ERROR_NOT_SUPPORTED );
5840 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005841}
5842
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005843/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005844 * to potentially free embedded data structures and wipe confidential data.
5845 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005846static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005847 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005848 psa_key_slot_t *private_key,
5849 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005850 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005851{
5852 psa_status_t status;
5853 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5854 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005855 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005856
5857 /* Step 1: run the secret agreement algorithm to generate the shared
5858 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005859 status = psa_key_agreement_raw_internal( ka_alg,
5860 private_key,
5861 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005862 shared_secret,
5863 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005864 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005865 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005866 goto exit;
5867
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005868 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005869 * the shared secret. A shared secret is permitted wherever a key
5870 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005871 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005872 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005873 shared_secret,
5874 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005875exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005876 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005877 return( status );
5878}
5879
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005880psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005881 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005882 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005883 const uint8_t *peer_key,
5884 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005885{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005886 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005887 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005888 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005889
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005890 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005891 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005892 status = psa_get_and_lock_transparent_key_slot_with_policy(
5893 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005894 if( status != PSA_SUCCESS )
5895 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005896 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005897 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005898 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005899 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005900 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005901 else
5902 {
5903 /* If a private key has been added as SECRET, we allow the derived
5904 * key material to be used as a key in PSA Crypto. */
5905 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5906 operation->can_output_key = 1;
5907 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005908
Ronald Cron5c522922020-11-14 16:35:34 +01005909 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005910
Ronald Cron5c522922020-11-14 16:35:34 +01005911 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005912}
5913
Gilles Peskinebe697d82019-05-16 18:00:41 +02005914psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005915 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005916 const uint8_t *peer_key,
5917 size_t peer_key_length,
5918 uint8_t *output,
5919 size_t output_size,
5920 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005921{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005922 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005923 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005924 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005925
5926 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5927 {
5928 status = PSA_ERROR_INVALID_ARGUMENT;
5929 goto exit;
5930 }
Ronald Cron5c522922020-11-14 16:35:34 +01005931 status = psa_get_and_lock_transparent_key_slot_with_policy(
5932 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005933 if( status != PSA_SUCCESS )
5934 goto exit;
5935
Gilles Peskine3e561302022-04-14 00:17:15 +02005936 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5937 * for the output size. The PSA specification only guarantees that this
5938 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5939 * but it might be nice to allow smaller buffers if the output fits.
5940 * At the time of writing this comment, with only ECDH implemented,
5941 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5942 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5943 * be exact for it as well. */
5944 size_t expected_length =
5945 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( slot->attr.type, slot->attr.bits );
5946 if( output_size < expected_length )
5947 {
5948 status = PSA_ERROR_BUFFER_TOO_SMALL;
5949 goto exit;
5950 }
5951
Gilles Peskine0216fe12019-04-11 21:23:21 +02005952 status = psa_key_agreement_raw_internal( alg, slot,
5953 peer_key, peer_key_length,
5954 output, output_size,
5955 output_length );
5956
5957exit:
5958 if( status != PSA_SUCCESS )
5959 {
5960 /* If an error happens and is not handled properly, the output
5961 * may be used as a key to protect sensitive data. Arrange for such
5962 * a key to be random, which is likely to result in decryption or
5963 * verification errors. This is better than filling the buffer with
5964 * some constant data such as zeros, which would result in the data
5965 * being protected with a reproducible, easily knowable key.
5966 */
5967 psa_generate_random( output, output_size );
5968 *output_length = output_size;
5969 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005970
Ronald Cron5c522922020-11-14 16:35:34 +01005971 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005972
Ronald Cron5c522922020-11-14 16:35:34 +01005973 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005974}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005975
5976
Gilles Peskine30524eb2020-11-13 17:02:26 +01005977
Gilles Peskine86a440b2018-11-12 18:39:40 +01005978/****************************************************************/
5979/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005980/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005981
Gilles Peskine30524eb2020-11-13 17:02:26 +01005982/** Initialize the PSA random generator.
5983 */
5984static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5985{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005986#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5987 memset( rng, 0, sizeof( *rng ) );
5988#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5989
Gilles Peskine30524eb2020-11-13 17:02:26 +01005990 /* Set default configuration if
5991 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5992 if( rng->entropy_init == NULL )
5993 rng->entropy_init = mbedtls_entropy_init;
5994 if( rng->entropy_free == NULL )
5995 rng->entropy_free = mbedtls_entropy_free;
5996
5997 rng->entropy_init( &rng->entropy );
5998#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5999 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6000 /* The PSA entropy injection feature depends on using NV seed as an entropy
6001 * source. Add NV seed as an entropy source for PSA entropy injection. */
6002 mbedtls_entropy_add_source( &rng->entropy,
6003 mbedtls_nv_seed_poll, NULL,
6004 MBEDTLS_ENTROPY_BLOCK_SIZE,
6005 MBEDTLS_ENTROPY_SOURCE_STRONG );
6006#endif
6007
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006008 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006009#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006010}
6011
6012/** Deinitialize the PSA random generator.
6013 */
6014static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6015{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006016#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6017 memset( rng, 0, sizeof( *rng ) );
6018#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006019 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006020 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006021#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006022}
6023
6024/** Seed the PSA random generator.
6025 */
6026static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6027{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006028#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6029 /* Do nothing: the external RNG seeds itself. */
6030 (void) rng;
6031 return( PSA_SUCCESS );
6032#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006033 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006034 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6035 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006036 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006037#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006038}
6039
Gilles Peskinee59236f2018-01-27 23:32:46 +01006040psa_status_t psa_generate_random( uint8_t *output,
6041 size_t output_size )
6042{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006043 GUARD_MODULE_INITIALIZED;
6044
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006045#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6046
6047 size_t output_length = 0;
6048 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6049 output, output_size,
6050 &output_length );
6051 if( status != PSA_SUCCESS )
6052 return( status );
6053 /* Breaking up a request into smaller chunks is currently not supported
6054 * for the extrernal RNG interface. */
6055 if( output_length != output_size )
6056 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6057 return( PSA_SUCCESS );
6058
6059#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6060
Gilles Peskine71ddab92021-01-04 21:01:07 +01006061 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006062 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006063 size_t request_size =
6064 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6065 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6066 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006067 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6068 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006069 if( ret != 0 )
6070 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006071 output_size -= request_size;
6072 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006073 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006074 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006075#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006076}
6077
Gilles Peskine8814fc42020-12-14 15:33:44 +01006078/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006079 *
6080 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6081 * `psa_generate_random(...)`. The state parameter is ignored since the
6082 * PSA API doesn't support passing an explicit state.
6083 *
6084 * In the non-external case, psa_generate_random() calls an
6085 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6086 * and semantics as mbedtls_psa_get_random(). As an optimization,
6087 * instead of doing this back-and-forth between the PSA API and the
6088 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6089 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006090 */
6091#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6092int mbedtls_psa_get_random( void *p_rng,
6093 unsigned char *output,
6094 size_t output_size )
6095{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006096 /* This function takes a pointer to the RNG state because that's what
6097 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6098 * its own state internally and doesn't let the caller access that state.
6099 * So we just ignore the state parameter, and in practice we'll pass
6100 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006101 (void) p_rng;
6102 psa_status_t status = psa_generate_random( output, output_size );
6103 if( status == PSA_SUCCESS )
6104 return( 0 );
6105 else
6106 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6107}
6108#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6109
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006110#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006111#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006112
Gilles Peskine7228da22019-07-15 11:06:15 +02006113psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006114 size_t seed_size )
6115{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006116 if( global_data.initialized )
6117 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006118
6119 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6120 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6121 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6122 return( PSA_ERROR_INVALID_ARGUMENT );
6123
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006124 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006125}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006126#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006127
Ronald Cron3772afe2021-02-08 16:10:05 +01006128/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006129 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006130 * \param type The key type
6131 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006132 *
6133 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006134 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006135 * \retval #PSA_ERROR_INVALID_ARGUMENT
6136 * The size in bits of the key is not valid.
6137 * \retval #PSA_ERROR_NOT_SUPPORTED
6138 * The type and/or the size in bits of the key or the combination of
6139 * the two is not supported.
6140 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006141static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6142 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006143{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006144 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006145
Gilles Peskinee59236f2018-01-27 23:32:46 +01006146 if( key_type_is_raw_bytes( type ) )
6147 {
Archana4d7ae1d2021-07-07 02:50:22 +05306148 status = psa_validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006149 if( status != PSA_SUCCESS )
6150 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006151 }
6152 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006153#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006154 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6155 {
6156 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6157 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006158
Ronald Cron01b2aba2020-10-05 09:42:02 +02006159 /* Accept only byte-aligned keys, for the same reasons as
6160 * in psa_import_rsa_key(). */
6161 if( bits % 8 != 0 )
6162 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006163 }
6164 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006165#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006166
Ronald Cron5c4d3862020-12-07 11:07:24 +01006167#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006168 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6169 {
Ronald Crond81ab562021-02-16 09:01:16 +01006170 /* To avoid empty block, return successfully here. */
6171 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006172 }
6173 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006174#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006175 {
6176 return( PSA_ERROR_NOT_SUPPORTED );
6177 }
6178
6179 return( PSA_SUCCESS );
6180}
6181
Ronald Cron55ed0592020-10-05 10:30:40 +02006182psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006183 const psa_key_attributes_t *attributes,
6184 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006185{
6186 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006187 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006188
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006189 if( ( attributes->domain_parameters == NULL ) &&
6190 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006191 return( PSA_ERROR_INVALID_ARGUMENT );
6192
Ronald Cron01b2aba2020-10-05 09:42:02 +02006193 if( key_type_is_raw_bytes( type ) )
6194 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006195 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006196 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006197 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006198
David Brown12ca5032021-02-11 11:02:00 -07006199#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006200 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006201 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006202#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006203 }
6204 else
6205
Jaeden Amero424fa932021-05-14 08:34:32 +01006206#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6207 defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006208 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006209 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006210 return( mbedtls_psa_rsa_generate_key( attributes,
6211 key_buffer,
6212 key_buffer_size,
6213 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006214 }
6215 else
Jaeden Amero424fa932021-05-14 08:34:32 +01006216#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6217 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006218
John Durkop9814fa22020-11-04 12:28:15 -08006219#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006220 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006221 {
Ronald Cron7023db52020-11-20 18:17:42 +01006222 return( mbedtls_psa_ecp_generate_key( attributes,
6223 key_buffer,
6224 key_buffer_size,
6225 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006226 }
6227 else
John Durkop9814fa22020-11-04 12:28:15 -08006228#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006229 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006230 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006231 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006232 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006233
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006234 return( PSA_SUCCESS );
6235}
Darryl Green0c6575a2018-11-07 16:05:30 +00006236
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006237psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006238 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006239{
6240 psa_status_t status;
6241 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006242 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006243 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006244
Ronald Cron81709fc2020-11-14 12:10:32 +01006245 *key = MBEDTLS_SVC_KEY_ID_INIT;
6246
Gilles Peskine0f84d622019-09-12 19:03:13 +02006247 /* Reject any attempt to create a zero-length key so that we don't
6248 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6249 if( psa_get_key_bits( attributes ) == 0 )
6250 return( PSA_ERROR_INVALID_ARGUMENT );
6251
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006252 /* Reject any attempt to create a public key. */
6253 if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) )
6254 return( PSA_ERROR_INVALID_ARGUMENT );
6255
Ronald Cron81709fc2020-11-14 12:10:32 +01006256 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6257 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006258 if( status != PSA_SUCCESS )
6259 goto exit;
6260
Ronald Cron977c2472020-10-13 08:32:21 +02006261 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05306262 * storage ( thus not in the case of generating a key in a secure element
6263 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
6264 * buffer to hold the generated key material. */
Ronald Cron977c2472020-10-13 08:32:21 +02006265 if( slot->key.data == NULL )
6266 {
6267 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6268 PSA_KEY_LOCATION_LOCAL_STORAGE )
6269 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006270 status = psa_validate_key_type_and_size_for_key_generation(
6271 attributes->core.type, attributes->core.bits );
6272 if( status != PSA_SUCCESS )
6273 goto exit;
6274
6275 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6276 attributes->core.type,
6277 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006278 }
6279 else
6280 {
6281 status = psa_driver_wrapper_get_key_buffer_size(
6282 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006283 if( status != PSA_SUCCESS )
6284 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006285 }
Ronald Cron977c2472020-10-13 08:32:21 +02006286
6287 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6288 if( status != PSA_SUCCESS )
6289 goto exit;
6290 }
6291
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006292 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006293 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006294
Ronald Cron2b56bc82020-10-05 10:02:26 +02006295 if( status != PSA_SUCCESS )
6296 psa_remove_key_data_from_memory( slot );
6297
Gilles Peskine11792082019-08-06 18:36:36 +02006298exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006299 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006300 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006301 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006302 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006303
Darryl Green0c6575a2018-11-07 16:05:30 +00006304 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006305}
6306
Gilles Peskinee59236f2018-01-27 23:32:46 +01006307/****************************************************************/
6308/* Module setup */
6309/****************************************************************/
6310
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006311#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006312psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6313 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6314 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6315{
6316 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6317 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006318 global_data.rng.entropy_init = entropy_init;
6319 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006320 return( PSA_SUCCESS );
6321}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006322#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006323
Gilles Peskinee59236f2018-01-27 23:32:46 +01006324void mbedtls_psa_crypto_free( void )
6325{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006326 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006327 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6328 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006329 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006330 }
6331 /* Wipe all remaining data, including configuration.
6332 * In particular, this sets all state indicator to the value
6333 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006334 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Ronald Cron9ba76912021-04-10 16:57:30 +02006335
6336 /* Terminate drivers */
6337 psa_driver_wrapper_free( );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006338}
6339
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006340#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6341/** Recover a transaction that was interrupted by a power failure.
6342 *
6343 * This function is called during initialization, before psa_crypto_init()
6344 * returns. If this function returns a failure status, the initialization
6345 * fails.
6346 */
6347static psa_status_t psa_crypto_recover_transaction(
6348 const psa_crypto_transaction_t *transaction )
6349{
6350 switch( transaction->unknown.type )
6351 {
6352 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6353 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006354 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006355 * is implemented.
6356 * https://github.com/ARMmbed/mbed-crypto/issues/218
6357 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006358 default:
6359 /* We found an unsupported transaction in the storage.
6360 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006361 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006362 }
6363}
6364#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6365
Gilles Peskinee59236f2018-01-27 23:32:46 +01006366psa_status_t psa_crypto_init( void )
6367{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006368 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006369
Gilles Peskinec6b69072018-11-20 21:42:52 +01006370 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006371 if( global_data.initialized != 0 )
6372 return( PSA_SUCCESS );
6373
Gilles Peskine30524eb2020-11-13 17:02:26 +01006374 /* Initialize and seed the random generator. */
6375 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006376 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006377 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006378 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006379 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006380 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006381
Gilles Peskine66fb1262018-12-10 16:29:04 +01006382 status = psa_initialize_key_slots( );
6383 if( status != PSA_SUCCESS )
6384 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006385
Ronald Cron9ba76912021-04-10 16:57:30 +02006386 /* Init drivers */
6387 status = psa_driver_wrapper_init( );
Gilles Peskined9348f22019-10-01 15:22:29 +02006388 if( status != PSA_SUCCESS )
6389 goto exit;
Gilles Peskined9348f22019-10-01 15:22:29 +02006390
Gilles Peskine4b734222019-07-24 15:56:31 +02006391#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006392 status = psa_crypto_load_transaction( );
6393 if( status == PSA_SUCCESS )
6394 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006395 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6396 if( status != PSA_SUCCESS )
6397 goto exit;
6398 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006399 }
6400 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6401 {
6402 /* There's no transaction to complete. It's all good. */
6403 status = PSA_SUCCESS;
6404 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006405#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006406
Gilles Peskinec6b69072018-11-20 21:42:52 +01006407 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006408 global_data.initialized = 1;
6409
6410exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006411 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006412 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006413 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006414}
6415
6416#endif /* MBEDTLS_PSA_CRYPTO_C */