blob: 2ce5e4320d13d35047327084cacd6f444b5766cf [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
Przemek Stekiel86679c72022-10-06 17:06:56 +02003708static psa_status_t psa_validate_tag_length( psa_algorithm_t alg ) {
Przemek Stekiel8a05a642022-10-06 17:01:58 +02003709 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg );
Andrzej Kurekf8816012021-12-19 17:00:12 +01003710
3711 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
3712 {
Przemek Stekiel86679c72022-10-06 17:06:56 +02003713#if defined(PSA_WANT_ALG_CCM)
Andrzej Kurekf8816012021-12-19 17:00:12 +01003714 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
3715 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
3716 if( tag_len < 4 || tag_len > 16 || tag_len % 2 )
3717 return( PSA_ERROR_INVALID_ARGUMENT );
3718 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02003719#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01003720
Przemek Stekiel86679c72022-10-06 17:06:56 +02003721#if defined(PSA_WANT_ALG_GCM)
Andrzej Kurekf8816012021-12-19 17:00:12 +01003722 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
3723 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
3724 if( tag_len != 4 && tag_len != 8 && ( tag_len < 12 || tag_len > 16 ) )
3725 return( PSA_ERROR_INVALID_ARGUMENT );
3726 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02003727#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01003728
Przemek Stekiel86679c72022-10-06 17:06:56 +02003729#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Andrzej Kurekf8816012021-12-19 17:00:12 +01003730 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
3731 /* We only support the default tag length. */
3732 if( tag_len != 16 )
3733 return( PSA_ERROR_INVALID_ARGUMENT );
3734 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02003735#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01003736
3737 default:
3738 (void) tag_len;
3739 return( PSA_ERROR_NOT_SUPPORTED );
3740 }
3741 return( PSA_SUCCESS );
3742}
3743
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003744/* Set the key for a multipart authenticated operation. */
3745static psa_status_t psa_aead_setup( psa_aead_operation_t *operation,
Paul Elliottd9343f22021-08-23 18:59:49 +01003746 int is_encrypt,
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003747 mbedtls_svc_key_id_t key,
3748 psa_algorithm_t alg )
Paul Elliottc2b71442021-06-24 18:17:52 +01003749{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003750 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3751 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3752 psa_key_slot_t *slot = NULL;
3753 psa_key_usage_t key_usage = 0;
3754
Bence Szépkúti39fb9d12022-01-13 14:33:45 +01003755 status = psa_aead_check_algorithm( alg );
Bence Szépkúti08f34652021-12-08 21:07:13 +01003756 if( status != PSA_SUCCESS )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003757 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003758
3759 if( operation->id != 0 )
3760 {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003761 status = PSA_ERROR_BAD_STATE;
3762 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003763 }
3764
3765 if( operation->nonce_set || operation->lengths_set ||
3766 operation->ad_started || operation->body_started )
3767 {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003768 status = PSA_ERROR_BAD_STATE;
3769 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003770 }
3771
Paul Elliottd9343f22021-08-23 18:59:49 +01003772 if( is_encrypt )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003773 key_usage = PSA_KEY_USAGE_ENCRYPT;
3774 else
3775 key_usage = PSA_KEY_USAGE_DECRYPT;
3776
3777 status = psa_get_and_lock_key_slot_with_policy( key, &slot, key_usage,
3778 alg );
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003779 if( status != PSA_SUCCESS )
3780 goto exit;
3781
3782 psa_key_attributes_t attributes = {
3783 .core = slot->attr
3784 };
3785
Przemek Stekiel6ab50762022-10-08 17:54:30 +02003786 if( ( status = psa_validate_tag_length( alg ) ) != PSA_SUCCESS )
3787 goto exit;
3788
Paul Elliottd9343f22021-08-23 18:59:49 +01003789 if( is_encrypt )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003790 status = psa_driver_wrapper_aead_encrypt_setup( operation,
3791 &attributes,
3792 slot->key.data,
3793 slot->key.bytes,
3794 alg );
3795 else
3796 status = psa_driver_wrapper_aead_decrypt_setup( operation,
3797 &attributes,
3798 slot->key.data,
3799 slot->key.bytes,
3800 alg );
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003801 if( status != PSA_SUCCESS )
3802 goto exit;
3803
3804 operation->key_type = psa_get_key_type( &attributes );
3805
3806exit:
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003807 unlock_status = psa_unlock_key_slot( slot );
3808
3809 if( status == PSA_SUCCESS )
3810 {
3811 status = unlock_status;
3812 operation->alg = psa_aead_get_base_algorithm( alg );
Paul Elliottd9343f22021-08-23 18:59:49 +01003813 operation->is_encrypt = is_encrypt;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003814 }
3815 else
3816 psa_aead_abort( operation );
3817
3818 return( status );
Paul Elliottc2b71442021-06-24 18:17:52 +01003819}
3820
Paul Elliott302ff6b2021-04-20 18:10:30 +01003821/* Set the key for a multipart authenticated encryption operation. */
3822psa_status_t psa_aead_encrypt_setup( psa_aead_operation_t *operation,
3823 mbedtls_svc_key_id_t key,
3824 psa_algorithm_t alg )
3825{
Paul Elliottd9343f22021-08-23 18:59:49 +01003826 return( psa_aead_setup( operation, 1, key, alg ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003827}
3828
3829/* Set the key for a multipart authenticated decryption operation. */
3830psa_status_t psa_aead_decrypt_setup( psa_aead_operation_t *operation,
3831 mbedtls_svc_key_id_t key,
3832 psa_algorithm_t alg )
3833{
Paul Elliottd9343f22021-08-23 18:59:49 +01003834 return( psa_aead_setup( operation, 0, key, alg ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003835}
3836
3837/* Generate a random nonce / IV for multipart AEAD operation */
3838psa_status_t psa_aead_generate_nonce( psa_aead_operation_t *operation,
3839 uint8_t *nonce,
3840 size_t nonce_size,
3841 size_t *nonce_length )
3842{
3843 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01003844 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01003845 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003846
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003847 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003848
Paul Elliottcee785c2021-05-20 14:29:20 +01003849 if( operation->id == 0 )
3850 {
3851 status = PSA_ERROR_BAD_STATE;
3852 goto exit;
3853 }
3854
Paul Elliottdaf5c892021-08-25 16:24:58 +01003855 if( operation->nonce_set || !operation->is_encrypt )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003856 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003857 status = PSA_ERROR_BAD_STATE;
3858 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003859 }
3860
Paul Elliotte193ea82021-10-01 13:00:16 +01003861 /* For CCM, this size may not be correct according to the PSA
3862 * specification. The PSA Crypto 1.0.1 specification states:
3863 *
3864 * CCM encodes the plaintext length pLen in L octets, with L the smallest
3865 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
3866 *
3867 * However this restriction that L has to be the smallest integer is not
3868 * applied in practice, and it is not implementable here since the
3869 * plaintext length may or may not be known at this time. */
Paul Elliottbbe90b52021-05-11 22:22:42 +01003870 required_nonce_size = PSA_AEAD_NONCE_LENGTH( operation->key_type,
Paul Elliott3242f6c2021-08-25 16:33:47 +01003871 operation->alg );
Paul Elliott39dc6b82021-05-11 19:16:09 +01003872 if( nonce_size < required_nonce_size )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003873 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003874 status = PSA_ERROR_BUFFER_TOO_SMALL;
3875 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003876 }
3877
Ronald Croncae59092021-11-26 18:53:58 +01003878 status = psa_generate_random( local_nonce, required_nonce_size );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003879 if( status != PSA_SUCCESS )
Paul Elliott39dc6b82021-05-11 19:16:09 +01003880 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003881
Ronald Croncae59092021-11-26 18:53:58 +01003882 status = psa_aead_set_nonce( operation, local_nonce, required_nonce_size );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003883
Paul Elliott39dc6b82021-05-11 19:16:09 +01003884exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01003885 if( status == PSA_SUCCESS )
Ronald Croncae59092021-11-26 18:53:58 +01003886 {
3887 memcpy( nonce, local_nonce, required_nonce_size );
Paul Elliott39dc6b82021-05-11 19:16:09 +01003888 *nonce_length = required_nonce_size;
Ronald Croncae59092021-11-26 18:53:58 +01003889 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01003890 else
3891 psa_aead_abort( operation );
3892
3893 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003894}
3895
3896/* Set the nonce for a multipart authenticated encryption or decryption
3897 operation.*/
3898psa_status_t psa_aead_set_nonce( psa_aead_operation_t *operation,
3899 const uint8_t *nonce,
3900 size_t nonce_length )
3901{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003902 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3903
Paul Elliottcee785c2021-05-20 14:29:20 +01003904 if( operation->id == 0 )
3905 {
3906 status = PSA_ERROR_BAD_STATE;
3907 goto exit;
3908 }
3909
Paul Elliott3d7d52c2021-09-01 10:33:14 +01003910 if( operation->nonce_set )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003911 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003912 status = PSA_ERROR_BAD_STATE;
3913 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003914 }
3915
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003916 status = psa_aead_check_nonce_length( operation->alg, nonce_length );
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003917 if( status != PSA_SUCCESS )
Paul Elliott4ed1ed12021-09-27 18:09:28 +01003918 {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003919 status = PSA_ERROR_INVALID_ARGUMENT;
3920 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01003921 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01003922
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003923 status = psa_driver_wrapper_aead_set_nonce( operation, nonce,
3924 nonce_length );
3925
Paul Elliott39dc6b82021-05-11 19:16:09 +01003926exit:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003927 if( status == PSA_SUCCESS )
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003928 operation->nonce_set = 1;
Paul Elliott39dc6b82021-05-11 19:16:09 +01003929 else
3930 psa_aead_abort( operation );
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003931
3932 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003933}
3934
3935/* Declare the lengths of the message and additional data for multipart AEAD. */
3936psa_status_t psa_aead_set_lengths( psa_aead_operation_t *operation,
3937 size_t ad_length,
3938 size_t plaintext_length )
3939{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003940 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3941
Paul Elliottcee785c2021-05-20 14:29:20 +01003942 if( operation->id == 0 )
3943 {
3944 status = PSA_ERROR_BAD_STATE;
3945 goto exit;
3946 }
3947
Paul Elliott6eb95982021-05-21 17:41:41 +01003948 if( operation->lengths_set || operation->ad_started ||
Paul Elliott7f429b72021-06-24 18:08:54 +01003949 operation->body_started )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003950 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003951 status = PSA_ERROR_BAD_STATE;
3952 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003953 }
3954
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003955 switch(operation->alg)
Paul Elliott325d3742021-09-27 17:56:28 +01003956 {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003957#if defined(PSA_WANT_ALG_GCM)
3958 case PSA_ALG_GCM:
3959 /* Lengths can only be too large for GCM if size_t is bigger than 32
3960 * bits. Without the guard this code will generate warnings on 32bit
3961 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01003962#if SIZE_MAX > UINT32_MAX
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003963 if( (( uint64_t ) ad_length ) >> 61 != 0 ||
3964 (( uint64_t ) plaintext_length ) > 0xFFFFFFFE0ull )
3965 {
3966 status = PSA_ERROR_INVALID_ARGUMENT;
3967 goto exit;
3968 }
Paul Elliott325d3742021-09-27 17:56:28 +01003969#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003970 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003971#endif /* PSA_WANT_ALG_GCM */
3972#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003973 case PSA_ALG_CCM:
3974 if( ad_length > 0xFF00 )
3975 {
3976 status = PSA_ERROR_INVALID_ARGUMENT;
3977 goto exit;
3978 }
3979 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003980#endif /* PSA_WANT_ALG_CCM */
3981#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003982 case PSA_ALG_CHACHA20_POLY1305:
3983 /* No length restrictions for ChaChaPoly. */
3984 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003985#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003986 default:
3987 break;
3988 }
Paul Elliott325d3742021-09-27 17:56:28 +01003989
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003990 status = psa_driver_wrapper_aead_set_lengths( operation, ad_length,
3991 plaintext_length );
3992
Paul Elliott39dc6b82021-05-11 19:16:09 +01003993exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01003994 if( status == PSA_SUCCESS )
Paul Elliottee4ffe02021-05-20 17:25:06 +01003995 {
3996 operation->ad_remaining = ad_length;
3997 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01003998 operation->lengths_set = 1;
Paul Elliottee4ffe02021-05-20 17:25:06 +01003999 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004000 else
4001 psa_aead_abort( operation );
4002
4003 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004004}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004005
4006/* Pass additional data to an active multipart AEAD operation. */
Paul Elliott302ff6b2021-04-20 18:10:30 +01004007psa_status_t psa_aead_update_ad( psa_aead_operation_t *operation,
4008 const uint8_t *input,
4009 size_t input_length )
4010{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004011 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4012
Paul Elliottcee785c2021-05-20 14:29:20 +01004013 if( operation->id == 0 )
4014 {
4015 status = PSA_ERROR_BAD_STATE;
4016 goto exit;
4017 }
4018
Paul Elliott6eb95982021-05-21 17:41:41 +01004019 if( !operation->nonce_set || operation->body_started )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004020 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004021 status = PSA_ERROR_BAD_STATE;
4022 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004023 }
4024
Paul Elliottee4ffe02021-05-20 17:25:06 +01004025 if( operation->lengths_set )
4026 {
Paul Elliott6eb95982021-05-21 17:41:41 +01004027 if( operation->ad_remaining < input_length )
Paul Elliottee4ffe02021-05-20 17:25:06 +01004028 {
4029 status = PSA_ERROR_INVALID_ARGUMENT;
4030 goto exit;
4031 }
4032
4033 operation->ad_remaining -= input_length;
4034 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004035#if defined(PSA_WANT_ALG_CCM)
4036 else if( operation->alg == PSA_ALG_CCM )
4037 {
4038 status = PSA_ERROR_BAD_STATE;
4039 goto exit;
4040 }
4041#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004042
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004043 status = psa_driver_wrapper_aead_update_ad( operation, input,
4044 input_length );
4045
Paul Elliott39dc6b82021-05-11 19:16:09 +01004046exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004047 if( status == PSA_SUCCESS )
4048 operation->ad_started = 1;
4049 else
4050 psa_aead_abort( operation );
4051
4052 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004053}
4054
4055/* Encrypt or decrypt a message fragment in an active multipart AEAD
4056 operation.*/
4057psa_status_t psa_aead_update( psa_aead_operation_t *operation,
4058 const uint8_t *input,
4059 size_t input_length,
4060 uint8_t *output,
4061 size_t output_size,
4062 size_t *output_length )
4063{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004064 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004065
4066 *output_length = 0;
4067
Paul Elliottcee785c2021-05-20 14:29:20 +01004068 if( operation->id == 0 )
4069 {
4070 status = PSA_ERROR_BAD_STATE;
4071 goto exit;
4072 }
4073
Paul Elliott6eb95982021-05-21 17:41:41 +01004074 if( !operation->nonce_set )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004075 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004076 status = PSA_ERROR_BAD_STATE;
4077 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004078 }
4079
Paul Elliottee4ffe02021-05-20 17:25:06 +01004080 if( operation->lengths_set )
4081 {
4082 /* Additional data length was supplied, but not all the additional
4083 data was supplied.*/
4084 if( operation->ad_remaining != 0 )
4085 {
4086 status = PSA_ERROR_INVALID_ARGUMENT;
4087 goto exit;
4088 }
4089
4090 /* Too much data provided. */
4091 if( operation->body_remaining < input_length )
4092 {
4093 status = PSA_ERROR_INVALID_ARGUMENT;
4094 goto exit;
4095 }
4096
4097 operation->body_remaining -= input_length;
4098 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004099#if defined(PSA_WANT_ALG_CCM)
4100 else if( operation->alg == PSA_ALG_CCM )
4101 {
4102 status = PSA_ERROR_BAD_STATE;
4103 goto exit;
4104 }
4105#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004106
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004107 status = psa_driver_wrapper_aead_update( operation, input, input_length,
4108 output, output_size,
4109 output_length );
4110
Paul Elliott39dc6b82021-05-11 19:16:09 +01004111exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004112 if( status == PSA_SUCCESS )
4113 operation->body_started = 1;
4114 else
4115 psa_aead_abort( operation );
4116
4117 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004118}
4119
Paul Elliott69bf5fc2021-09-19 18:26:37 +01004120static psa_status_t psa_aead_final_checks( const psa_aead_operation_t *operation )
Paul Elliottad53dcc2021-06-23 08:50:14 +01004121{
Paul Elliott7f429b72021-06-24 18:08:54 +01004122 if( operation->id == 0 || !operation->nonce_set )
Paul Elliottad53dcc2021-06-23 08:50:14 +01004123 return( PSA_ERROR_BAD_STATE );
4124
Paul Elliotta5614442021-07-14 14:54:11 +01004125 if( operation->lengths_set && ( operation->ad_remaining != 0 ||
Paul Elliottad53dcc2021-06-23 08:50:14 +01004126 operation->body_remaining != 0 ) )
4127 return( PSA_ERROR_INVALID_ARGUMENT );
4128
4129 return( PSA_SUCCESS );
4130}
4131
Paul Elliott302ff6b2021-04-20 18:10:30 +01004132/* Finish encrypting a message in a multipart AEAD operation. */
4133psa_status_t psa_aead_finish( psa_aead_operation_t *operation,
4134 uint8_t *ciphertext,
4135 size_t ciphertext_size,
4136 size_t *ciphertext_length,
4137 uint8_t *tag,
4138 size_t tag_size,
4139 size_t *tag_length )
4140{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4142
Paul Elliott302ff6b2021-04-20 18:10:30 +01004143 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004144 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004145
Paul Elliott814fffb2021-08-16 18:20:36 +01004146 status = psa_aead_final_checks( operation );
Paul Elliottad53dcc2021-06-23 08:50:14 +01004147 if( status != PSA_SUCCESS )
4148 goto exit;
4149
Paul Elliott7f429b72021-06-24 18:08:54 +01004150 if( !operation->is_encrypt )
Paul Elliottcee785c2021-05-20 14:29:20 +01004151 {
4152 status = PSA_ERROR_BAD_STATE;
4153 goto exit;
4154 }
4155
Paul Elliott39dc6b82021-05-11 19:16:09 +01004156 status = psa_driver_wrapper_aead_finish( operation, ciphertext,
4157 ciphertext_size,
4158 ciphertext_length,
4159 tag, tag_size, tag_length );
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004160
Paul Elliott39dc6b82021-05-11 19:16:09 +01004161exit:
Paul Elliottf47b0952021-05-21 18:02:33 +01004162 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004163 * the zero tag size, make sure the tag is set to something implausible.
4164 * Even if the operation succeeds, make sure we clear the rest of the
4165 * buffer to prevent potential leakage of anything previously placed in
4166 * the same buffer.*/
Paul Elliott90fdc112021-09-22 17:15:48 +01004167 if( tag != NULL )
Paul Elliottec95cc92021-09-19 22:33:09 +01004168 {
4169 if( status != PSA_SUCCESS )
4170 memset( tag, '!', tag_size );
4171 else if( *tag_length < tag_size )
4172 memset( tag + *tag_length, '!', ( tag_size - *tag_length ) );
4173 }
Paul Elliottf47b0952021-05-21 18:02:33 +01004174
Paul Elliott39dc6b82021-05-11 19:16:09 +01004175 psa_aead_abort( operation );
4176
4177 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004178}
4179
4180/* Finish authenticating and decrypting a message in a multipart AEAD
4181 operation.*/
4182psa_status_t psa_aead_verify( psa_aead_operation_t *operation,
4183 uint8_t *plaintext,
4184 size_t plaintext_size,
4185 size_t *plaintext_length,
4186 const uint8_t *tag,
4187 size_t tag_length )
4188{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004189 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4190
Paul Elliott302ff6b2021-04-20 18:10:30 +01004191 *plaintext_length = 0;
4192
Paul Elliott814fffb2021-08-16 18:20:36 +01004193 status = psa_aead_final_checks( operation );
Paul Elliottad53dcc2021-06-23 08:50:14 +01004194 if( status != PSA_SUCCESS )
4195 goto exit;
4196
Paul Elliott7f429b72021-06-24 18:08:54 +01004197 if( operation->is_encrypt )
Paul Elliottcee785c2021-05-20 14:29:20 +01004198 {
4199 status = PSA_ERROR_BAD_STATE;
4200 goto exit;
4201 }
4202
Paul Elliott39dc6b82021-05-11 19:16:09 +01004203 status = psa_driver_wrapper_aead_verify( operation, plaintext,
4204 plaintext_size,
4205 plaintext_length,
4206 tag, tag_length );
4207
4208exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004209 psa_aead_abort( operation );
4210
4211 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004212}
4213
4214/* Abort an AEAD operation. */
Paul Elliottbbe90b52021-05-11 22:22:42 +01004215psa_status_t psa_aead_abort( psa_aead_operation_t *operation )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004216{
4217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4218
4219 if( operation->id == 0 )
4220 {
4221 /* The object has (apparently) been initialized but it is not (yet)
4222 * in use. It's ok to call abort on such an object, and there's
4223 * nothing to do. */
4224 return( PSA_SUCCESS );
4225 }
4226
4227 status = psa_driver_wrapper_aead_abort( operation );
4228
Paul Elliott70618b22021-09-22 17:12:16 +01004229 memset( operation, 0, sizeof( *operation ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004230
4231 return( status );
4232}
4233
Gilles Peskinee59236f2018-01-27 23:32:46 +01004234/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004235/* Generators */
4236/****************************************************************/
4237
Przemek Stekiel69c46792022-06-10 12:59:51 +02004238#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004239 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004240 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4241 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004242#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004243#endif /* At least one builtin KDF */
4244
Przemek Stekiel69c46792022-06-10 12:59:51 +02004245#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004246 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4247 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4248static psa_status_t psa_key_derivation_start_hmac(
4249 psa_mac_operation_t *operation,
4250 psa_algorithm_t hash_alg,
4251 const uint8_t *hmac_key,
4252 size_t hmac_key_length )
4253{
4254 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4256 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
4257 psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) );
4258 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
4259
Steven Cooreman72f736a2021-05-07 14:14:37 +02004260 operation->is_sign = 1;
4261 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
4262
Steven Cooreman094a77e2021-05-06 17:58:36 +02004263 status = psa_driver_wrapper_mac_sign_setup( operation,
4264 &attributes,
4265 hmac_key, hmac_key_length,
4266 PSA_ALG_HMAC( hash_alg ) );
4267
4268 psa_reset_key_attributes( &attributes );
4269 return( status );
4270}
4271#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004272
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004273#define HKDF_STATE_INIT 0 /* no input yet */
4274#define HKDF_STATE_STARTED 1 /* got salt */
4275#define HKDF_STATE_KEYED 2 /* got key */
4276#define HKDF_STATE_OUTPUT 3 /* output started */
4277
Gilles Peskinecbe66502019-05-16 16:59:18 +02004278static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004279 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004280{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004281 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4282 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004283 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004284 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004285}
4286
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004287psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004288{
4289 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004290 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004291 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004292 {
4293 /* The object has (apparently) been initialized but it is not
4294 * in use. It's ok to call abort on such an object, and there's
4295 * nothing to do. */
4296 }
4297 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004298#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiela29b4882022-06-02 11:37:03 +02004299 if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004300 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004301 mbedtls_free( operation->ctx.hkdf.info );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004302 status = psa_mac_abort( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004303 }
John Durkop07cc04a2020-11-16 22:08:34 -08004304 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004305#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004306#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4307 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4308 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004309 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004310 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004311 {
Steven Cooremana6df6042021-04-29 19:32:25 +02004312 if( operation->ctx.tls12_prf.secret != NULL )
4313 {
4314 mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret,
4315 operation->ctx.tls12_prf.secret_length );
4316 mbedtls_free( operation->ctx.tls12_prf.secret );
4317 }
4318
Janos Follath6a1d2622019-06-11 10:37:28 +01004319 if( operation->ctx.tls12_prf.seed != NULL )
4320 {
4321 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4322 operation->ctx.tls12_prf.seed_length );
4323 mbedtls_free( operation->ctx.tls12_prf.seed );
4324 }
4325
4326 if( operation->ctx.tls12_prf.label != NULL )
4327 {
4328 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4329 operation->ctx.tls12_prf.label_length );
4330 mbedtls_free( operation->ctx.tls12_prf.label );
4331 }
4332
Przemek Stekiele3ee2212022-04-07 14:29:56 +02004333 if( operation->ctx.tls12_prf.other_secret != NULL )
4334 {
4335 mbedtls_platform_zeroize( operation->ctx.tls12_prf.other_secret,
4336 operation->ctx.tls12_prf.other_secret_length );
4337 mbedtls_free( operation->ctx.tls12_prf.other_secret );
4338 }
4339
Steven Cooremana6df6042021-04-29 19:32:25 +02004340 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004341
4342 /* We leave the fields Ai and output_block to be erased safely by the
4343 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004344 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004345 else
John Durkop07cc04a2020-11-16 22:08:34 -08004346#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4347 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004348#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04004349 if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004350 {
4351 mbedtls_platform_zeroize( operation->ctx.tls12_ecjpake_to_pms.data,
Andrzej Kurek5603efd2022-09-26 10:49:16 -04004352 sizeof( operation->ctx.tls12_ecjpake_to_pms.data ) );
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004353 }
4354 else
4355#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004356 {
4357 status = PSA_ERROR_BAD_STATE;
4358 }
Janos Follath083036a2019-06-11 10:22:26 +01004359 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004360 return( status );
4361}
4362
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004364 size_t *capacity)
4365{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004366 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004367 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004368 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004369 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004370 }
4371
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004372 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004373 return( PSA_SUCCESS );
4374}
4375
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004376psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004377 size_t capacity )
4378{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004379 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004380 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004381 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004382 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004383 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004384 return( PSA_SUCCESS );
4385}
4386
Przemek Stekiel69c46792022-06-10 12:59:51 +02004387#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004388/* Read some bytes from an HKDF-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004389static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004390 psa_algorithm_t kdf_alg,
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004391 uint8_t *output,
4392 size_t output_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004393{
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004394 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004395 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004396 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004397 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004398#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004399 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004400#else
4401 const uint8_t last_block = 0xff;
4402#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004403
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004404 if( hkdf->state < HKDF_STATE_KEYED ||
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004405 ( !hkdf->info_set
4406#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
4407 && !PSA_ALG_IS_HKDF_EXTRACT( kdf_alg )
4408#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
4409 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004410 return( PSA_ERROR_BAD_STATE );
4411 hkdf->state = HKDF_STATE_OUTPUT;
4412
Gilles Peskinebef7f142018-07-12 17:22:21 +02004413 while( output_length != 0 )
4414 {
4415 /* Copy what remains of the current block */
4416 uint8_t n = hash_length - hkdf->offset_in_block;
4417 if( n > output_length )
4418 n = (uint8_t) output_length;
4419 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4420 output += n;
4421 output_length -= n;
4422 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004423 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004424 break;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004425 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004426 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004427 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004428 * object was corrupted or if this function is called directly
4429 * inside the library. */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004430 if( hkdf->block_number == last_block )
Gilles Peskined54931c2018-07-17 21:06:59 +02004431 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004432
4433 /* We need a new block */
4434 ++hkdf->block_number;
4435 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004436
Steven Cooreman094a77e2021-05-06 17:58:36 +02004437 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4438 hash_alg,
4439 hkdf->prk,
4440 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004441 if( status != PSA_SUCCESS )
4442 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004443
Gilles Peskinebef7f142018-07-12 17:22:21 +02004444 if( hkdf->block_number != 1 )
4445 {
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004446 status = psa_mac_update( &hkdf->hmac,
4447 hkdf->output_block,
4448 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004449 if( status != PSA_SUCCESS )
4450 return( status );
4451 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004452 status = psa_mac_update( &hkdf->hmac,
4453 hkdf->info,
4454 hkdf->info_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004455 if( status != PSA_SUCCESS )
4456 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004457 status = psa_mac_update( &hkdf->hmac,
4458 &hkdf->block_number, 1 );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004459 if( status != PSA_SUCCESS )
4460 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004461 status = psa_mac_sign_finish( &hkdf->hmac,
4462 hkdf->output_block,
4463 sizeof( hkdf->output_block ),
4464 &hmac_output_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004465 if( status != PSA_SUCCESS )
4466 return( status );
4467 }
4468
4469 return( PSA_SUCCESS );
4470}
Przemek Stekiel69c46792022-06-10 12:59:51 +02004471#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004472
John Durkop07cc04a2020-11-16 22:08:34 -08004473#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4474 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004475static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4476 psa_tls12_prf_key_derivation_t *tls12_prf,
4477 psa_algorithm_t alg )
4478{
4479 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004480 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremana6df6042021-04-29 19:32:25 +02004481 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4482 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004483 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004484
Janos Follath7742fee2019-06-17 12:58:10 +01004485 /* We can't be wanting more output after block 0xff, otherwise
4486 * the capacity check in psa_key_derivation_output_bytes() would have
4487 * prevented this call. It could happen only if the operation
4488 * object was corrupted or if this function is called directly
4489 * inside the library. */
4490 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004491 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004492
4493 /* We need a new block */
4494 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004495 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004496
4497 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4498 *
4499 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4500 *
4501 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4502 * HMAC_hash(secret, A(2) + seed) +
4503 * HMAC_hash(secret, A(3) + seed) + ...
4504 *
4505 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004506 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004507 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004508 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004509 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004510 * is currently extracted as `output_block` and where i is
4511 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004512 */
4513
Steven Cooreman094a77e2021-05-06 17:58:36 +02004514 status = psa_key_derivation_start_hmac( &hmac,
4515 hash_alg,
4516 tls12_prf->secret,
4517 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004518 if( status != PSA_SUCCESS )
4519 goto cleanup;
4520
4521 /* Calculate A(i) where i = tls12_prf->block_number. */
4522 if( tls12_prf->block_number == 1 )
4523 {
4524 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4525 * the variable seed and in this instance means it in the context of the
4526 * P_hash function, where seed = label + seed.) */
Steven Cooremana6df6042021-04-29 19:32:25 +02004527 status = psa_mac_update( &hmac,
4528 tls12_prf->label,
4529 tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004530 if( status != PSA_SUCCESS )
4531 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004532 status = psa_mac_update( &hmac,
4533 tls12_prf->seed,
4534 tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004535 if( status != PSA_SUCCESS )
4536 goto cleanup;
4537 }
4538 else
4539 {
4540 /* A(i) = HMAC_hash(secret, A(i-1)) */
Steven Cooremana6df6042021-04-29 19:32:25 +02004541 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004542 if( status != PSA_SUCCESS )
4543 goto cleanup;
4544 }
4545
Steven Cooremana6df6042021-04-29 19:32:25 +02004546 status = psa_mac_sign_finish( &hmac,
4547 tls12_prf->Ai, hash_length,
4548 &hmac_output_length );
4549 if( hmac_output_length != hash_length )
4550 status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follathea29bfb2019-06-19 12:21:20 +01004551 if( status != PSA_SUCCESS )
4552 goto cleanup;
4553
4554 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Steven Cooreman094a77e2021-05-06 17:58:36 +02004555 status = psa_key_derivation_start_hmac( &hmac,
4556 hash_alg,
4557 tls12_prf->secret,
4558 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004559 if( status != PSA_SUCCESS )
4560 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004561 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004562 if( status != PSA_SUCCESS )
4563 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004564 status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004565 if( status != PSA_SUCCESS )
4566 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004567 status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004568 if( status != PSA_SUCCESS )
4569 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004570 status = psa_mac_sign_finish( &hmac,
4571 tls12_prf->output_block, hash_length,
4572 &hmac_output_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004573 if( status != PSA_SUCCESS )
4574 goto cleanup;
4575
Janos Follath7742fee2019-06-17 12:58:10 +01004576
4577cleanup:
Steven Cooremana6df6042021-04-29 19:32:25 +02004578 cleanup_status = psa_mac_abort( &hmac );
Janos Follathea29bfb2019-06-19 12:21:20 +01004579 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4580 status = cleanup_status;
4581
4582 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004583}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004584
Janos Follath844eb0e2019-06-19 12:10:49 +01004585static psa_status_t psa_key_derivation_tls12_prf_read(
4586 psa_tls12_prf_key_derivation_t *tls12_prf,
4587 psa_algorithm_t alg,
4588 uint8_t *output,
4589 size_t output_length )
4590{
4591 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004592 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01004593 psa_status_t status;
4594 uint8_t offset, length;
4595
Gilles Peskineb1edaec2021-06-11 22:41:46 +02004596 switch( tls12_prf->state )
4597 {
4598 case PSA_TLS12_PRF_STATE_LABEL_SET:
4599 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4600 break;
4601 case PSA_TLS12_PRF_STATE_OUTPUT:
4602 break;
4603 default:
4604 return( PSA_ERROR_BAD_STATE );
4605 }
4606
Janos Follath844eb0e2019-06-19 12:10:49 +01004607 while( output_length != 0 )
4608 {
4609 /* Check if we have fully processed the current block. */
4610 if( tls12_prf->left_in_block == 0 )
4611 {
4612 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4613 alg );
4614 if( status != PSA_SUCCESS )
4615 return( status );
4616
4617 continue;
4618 }
4619
4620 if( tls12_prf->left_in_block > output_length )
4621 length = (uint8_t) output_length;
4622 else
4623 length = tls12_prf->left_in_block;
4624
4625 offset = hash_length - tls12_prf->left_in_block;
4626 memcpy( output, tls12_prf->output_block + offset, length );
4627 output += length;
4628 output_length -= length;
4629 tls12_prf->left_in_block -= length;
4630 }
4631
4632 return( PSA_SUCCESS );
4633}
John Durkop07cc04a2020-11-16 22:08:34 -08004634#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4635 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004636
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004637#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
4638static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
4639 psa_tls12_ecjpake_to_pms_t *ecjpake,
4640 uint8_t *output,
4641 size_t output_length )
4642{
4643 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04004644 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004645
4646 if( output_length != 32 )
4647 return ( PSA_ERROR_INVALID_ARGUMENT );
4648
4649 status = psa_hash_compute( PSA_ALG_SHA_256, ecjpake->data,
4650 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
4651 &output_size );
4652 if( status != PSA_SUCCESS )
4653 return ( status );
4654
4655 if( output_size != output_length )
4656 return ( PSA_ERROR_GENERIC_ERROR );
4657
4658 return ( PSA_SUCCESS );
4659}
4660#endif
4661
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004662psa_status_t psa_key_derivation_output_bytes(
4663 psa_key_derivation_operation_t *operation,
4664 uint8_t *output,
4665 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004666{
4667 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004669
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004670 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004671 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004672 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004673 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004674 }
4675
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004676 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004677 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004678 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004679 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004680 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004681 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004682 goto exit;
4683 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004684 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004685 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004686 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004687 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004688 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4689 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004690 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004691 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004692 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004693 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004694 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004695
Przemek Stekiel69c46792022-06-10 12:59:51 +02004696#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiela29b4882022-06-02 11:37:03 +02004697 if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004698 {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004699 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, kdf_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004700 output, output_length );
4701 }
Janos Follathadbec812019-06-14 11:05:39 +01004702 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004703#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004704#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4705 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01004706 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08004707 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004708 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004709 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Steven Cooremana6df6042021-04-29 19:32:25 +02004710 kdf_alg, output,
4711 output_length );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004712 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004713 else
John Durkop07cc04a2020-11-16 22:08:34 -08004714#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4715 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004716#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04004717 if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004718 {
4719 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
4720 &operation->ctx.tls12_ecjpake_to_pms, output, output_length );
4721 }
4722 else
4723#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
4724
Gilles Peskineeab56e42018-07-12 17:12:33 +02004725 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004726 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004727 return( PSA_ERROR_BAD_STATE );
4728 }
4729
4730exit:
4731 if( status != PSA_SUCCESS )
4732 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004733 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 * This allows us to differentiate between exhausted operations and
4735 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4736 * operations. */
4737 psa_algorithm_t alg = operation->alg;
4738 psa_key_derivation_abort( operation );
4739 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004740 memset( output, '!', output_length );
4741 }
4742 return( status );
4743}
4744
David Brown7807bf72021-02-09 16:28:23 -07004745#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02004746static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4747{
4748 if( data_size >= 8 )
4749 mbedtls_des_key_set_parity( data );
4750 if( data_size >= 16 )
4751 mbedtls_des_key_set_parity( data + 8 );
4752 if( data_size >= 24 )
4753 mbedtls_des_key_set_parity( data + 16 );
4754}
David Brown7807bf72021-02-09 16:28:23 -07004755#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004756
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004757/*
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +01004758* ECC keys on a Weierstrass elliptic curve require the generation
4759* of a private key which is an integer
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004760* in the range [1, N - 1], where N is the boundary of the private key domain:
4761* N is the prime p for Diffie-Hellman, or the order of the
4762* curve’s base point for ECC.
4763*
4764* Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
4765* This function generates the private key using the following process:
4766*
4767* 1. Draw a byte string of length ceiling(m/8) bytes.
4768* 2. If m is not a multiple of 8, set the most significant
4769* (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
4770* 3. Convert the string to integer k by decoding it as a big-endian byte string.
4771* 4. If k > N - 2, discard the result and return to step 1.
4772* 5. Output k + 1 as the private key.
4773*
4774* This method allows compliance to NIST standards, specifically the methods titled
4775* Key-Pair Generation by Testing Candidates in the following publications:
4776* - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
4777* Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
4778* Diffie-Hellman keys.
4779*
4780* - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
4781* Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004782*
4783* Note: Function allocates memory for *data buffer, so given *data should be
4784* always NULL.
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004785*/
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004786#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4787 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4788 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4789 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
4790 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004791static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004792 psa_key_slot_t *slot,
4793 size_t bits,
4794 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004795 uint8_t **data
4796 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004797{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004798 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004799 mbedtls_mpi k;
4800 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004802 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004803
4804 mbedtls_mpi_init( &k );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004805 mbedtls_mpi_init( &diff_N_2 );
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004806
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004807 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
4808 slot->attr.type );
4809 mbedtls_ecp_group_id grp_id =
4810 mbedtls_ecc_group_of_psa( curve, bits, 0 );
4811
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01004812 if( grp_id == MBEDTLS_ECP_DP_NONE )
4813 {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004814 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01004815 goto cleanup;
4816 }
4817
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004818 mbedtls_ecp_group ecp_group;
Przemyslaw Stekiel65348162021-11-18 11:57:07 +01004819 mbedtls_ecp_group_init( &ecp_group );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004820
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004821 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ecp_group, grp_id ) );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004822
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004823 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004824 /* Let m be the bit size of N. */
4825 size_t m = ecp_group.nbits;
4826
4827 size_t m_bytes = PSA_BITS_TO_BYTES( m );
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004828
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004829 /* Calculate N - 2 - it will be needed later. */
4830 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &diff_N_2, &ecp_group.N, 2 ) );
4831
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004832 /* Note: This function is always called with *data == NULL and it
4833 * allocates memory for the data buffer. */
4834 *data = mbedtls_calloc( 1, m_bytes );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004835 if( *data == NULL )
4836 {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004837 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004838 goto cleanup;
4839 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004840
Przemek Stekielc85f0912022-03-08 11:37:54 +01004841 while( key_out_of_range )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004842 {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004843 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Przemek Stekielc85f0912022-03-08 11:37:54 +01004844 if( ( status = psa_key_derivation_output_bytes( operation, *data, m_bytes ) ) != 0 )
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004845 goto cleanup;
4846
4847 /* 2. If m is not a multiple of 8 */
Przemek Stekielc85f0912022-03-08 11:37:54 +01004848 if( m % 8 != 0 )
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004849 {
4850 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004851 * (8 * ceiling(m/8) - m) bits of the first byte in
4852 * the string to zero.
4853 */
4854 uint8_t clear_bit_mask = ( 1 << ( m % 8 ) ) - 1;
4855 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004856 }
4857
4858 /* 3. Convert the string to integer k by decoding it as a
4859 * big-endian byte string.
4860 */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004861 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &k, *data, m_bytes ) );
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004862
4863 /* 4. If k > N - 2, discard the result and return to step 1.
4864 * Result of comparison is returned. When it indicates error
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004865 * then this function is called again.
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004866 */
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004867 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( &diff_N_2, &k, &key_out_of_range ) );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004868 }
4869
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004870 /* 5. Output k + 1 as the private key. */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004871 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &k, &k, 1 ) );
4872 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &k, *data, m_bytes ) );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004873cleanup:
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004874 if( ret != 0 )
Przemyslaw Stekieldc8d7d92021-12-02 09:15:20 +01004875 status = mbedtls_to_psa_error( ret );
Przemek Stekielc85f0912022-03-08 11:37:54 +01004876 if( status != PSA_SUCCESS ) {
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004877 mbedtls_free( *data );
4878 *data = NULL;
4879 }
4880 mbedtls_mpi_free( &k );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004881 mbedtls_mpi_free( &diff_N_2 );
Przemyslaw Stekieldc8d7d92021-12-02 09:15:20 +01004882 return( status );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004883}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004884
4885/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
4886 * is determined by the curve, and sets the mandatory bits accordingly. That is:
4887 *
4888 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
4889 * draw a 32-byte string and process it as specified in
4890 * Elliptic Curves for Security [RFC7748] §5.
4891 *
4892 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
4893 * draw a 56-byte string and process it as specified in [RFC7748] §5.
4894 *
4895 * Note: Function allocates memory for *data buffer, so given *data should be
4896 * always NULL.
4897 */
4898
4899static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
4900 size_t bits,
4901 psa_key_derivation_operation_t *operation,
4902 uint8_t **data
4903 )
4904{
4905 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004907
4908 switch( bits )
4909 {
4910 case 255:
4911 output_length = 32;
4912 break;
4913 case 448:
4914 output_length = 56;
4915 break;
4916 default:
4917 return( PSA_ERROR_INVALID_ARGUMENT );
4918 break;
4919 }
4920
4921 *data = mbedtls_calloc( 1, output_length );
4922
4923 if( *data == NULL )
4924 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4925
4926 status = psa_key_derivation_output_bytes( operation, *data, output_length );
4927
4928 if( status != PSA_SUCCESS )
4929 return status;
4930
4931 switch( bits )
4932 {
4933 case 255:
4934 (*data)[0] &= 248;
4935 (*data)[31] &= 127;
4936 (*data)[31] |= 64;
4937 break;
4938 case 448:
4939 (*data)[0] &= 252;
4940 (*data)[55] |= 128;
4941 break;
4942 default:
Przemek Stekiela81aed22022-03-01 15:13:30 +01004943 return( PSA_ERROR_CORRUPTION_DETECTED );
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004944 break;
4945 }
4946
4947 return status;
4948}
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004949#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
4950 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
4951 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4952 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
4953 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004954
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004955static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004956 psa_key_slot_t *slot,
4957 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004959{
4960 uint8_t *data = NULL;
4961 size_t bytes = PSA_BITS_TO_BYTES( bits );
Archanad8a83dc2021-06-14 10:04:16 +05304962 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004963 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004964
Przemek Stekieldcab6cc2022-03-01 14:22:29 +01004965 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004966 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004967
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004968#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4969 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4970 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4971 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
4972 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemek Stekielc85f0912022-03-08 11:37:54 +01004973 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004974 {
Przemyslaw Stekiel92481592022-01-04 10:09:46 +01004975 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type );
Przemek Stekielc85f0912022-03-08 11:37:54 +01004976 if( PSA_ECC_FAMILY_IS_WEIERSTRASS( curve ) )
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004977 {
4978 /* Weierstrass elliptic curve */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004979 status = psa_generate_derived_ecc_key_weierstrass_helper( slot, bits, operation, &data );
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004980 if( status != PSA_SUCCESS )
4981 goto exit;
Przemyslaw Stekiel50fcc532021-11-26 10:54:52 +01004982 }
4983 else
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004984 {
4985 /* Montgomery elliptic curve */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004986 status = psa_generate_derived_ecc_key_montgomery_helper( bits, operation, &data );
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004987 if( status != PSA_SUCCESS )
4988 goto exit;
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004989 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004990 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004991#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
4992 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
4993 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4994 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
4995 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004996 if( key_type_is_raw_bytes( slot->attr.type ) )
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004997 {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004998 if( bits % 8 != 0 )
4999 return( PSA_ERROR_INVALID_ARGUMENT );
5000 data = mbedtls_calloc( 1, bytes );
5001 if( data == NULL )
5002 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5003
5004 status = psa_key_derivation_output_bytes( operation, data, bytes );
5005 if( status != PSA_SUCCESS )
5006 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07005007#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005008 if( slot->attr.type == PSA_KEY_TYPE_DES )
5009 psa_des_set_key_parity( data, bytes );
Przemek Stekielf110dc02022-03-01 14:48:05 +01005010#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005011 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005012 else
Przemek Stekieldcab6cc2022-03-01 14:22:29 +01005013 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Crondd04d422020-11-28 15:14:42 +01005014
Ronald Cronbf33c932020-11-28 18:06:53 +01005015 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005016 psa_key_attributes_t attributes = {
5017 .core = slot->attr
5018 };
5019
Archanad8a83dc2021-06-14 10:04:16 +05305020 if( psa_key_lifetime_is_external( attributes.core.lifetime ) )
5021 {
Archana449608b2021-09-08 15:36:05 +05305022 status = psa_driver_wrapper_get_key_buffer_size( &attributes,
5023 &storage_size );
Archanad8a83dc2021-06-14 10:04:16 +05305024 if( status != PSA_SUCCESS )
5025 goto exit;
5026 }
5027 status = psa_allocate_buffer_to_slot( slot, storage_size );
5028 if( status != PSA_SUCCESS )
5029 goto exit;
5030
Ronald Cronbf33c932020-11-28 18:06:53 +01005031 status = psa_driver_wrapper_import_key( &attributes,
5032 data, bytes,
5033 slot->key.data,
5034 slot->key.bytes,
5035 &slot->key.bytes, &bits );
5036 if( bits != slot->attr.bits )
5037 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005038
5039exit:
5040 mbedtls_free( data );
5041 return( status );
5042}
5043
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005044psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005046 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005047{
5048 psa_status_t status;
5049 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005050 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005051
Ronald Cron81709fc2020-11-14 12:10:32 +01005052 *key = MBEDTLS_SVC_KEY_ID_INIT;
5053
Gilles Peskine0f84d622019-09-12 19:03:13 +02005054 /* Reject any attempt to create a zero-length key so that we don't
5055 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5056 if( psa_get_key_bits( attributes ) == 0 )
5057 return( PSA_ERROR_INVALID_ARGUMENT );
5058
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005059 if( operation->alg == PSA_ALG_NONE )
5060 return( PSA_ERROR_BAD_STATE );
5061
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005062 if( ! operation->can_output_key )
5063 return( PSA_ERROR_NOT_PERMITTED );
5064
Ronald Cron81709fc2020-11-14 12:10:32 +01005065 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5066 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005067#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5068 if( driver != NULL )
5069 {
5070 /* Deriving a key in a secure element is not implemented yet. */
5071 status = PSA_ERROR_NOT_SUPPORTED;
5072 }
5073#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005074 if( status == PSA_SUCCESS )
5075 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005076 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005077 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005078 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005079 }
5080 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005081 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005082 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005083 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005084
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005085 return( status );
5086}
5087
Gilles Peskineeab56e42018-07-12 17:12:33 +02005088
5089
5090/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005091/* Key derivation */
5092/****************************************************************/
5093
Steven Cooreman932ffb72021-02-15 12:14:32 +01005094#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005095static int is_kdf_alg_supported( psa_algorithm_t kdf_alg )
5096{
5097#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
5098 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5099 return( 1 );
5100#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005101#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
5102 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5103 return( 1 );
5104#endif
5105#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
5106 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) )
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005107 return( 1 );
5108#endif
5109#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5110 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5111 return( 1 );
5112#endif
5113#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5114 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5115 return( 1 );
5116#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005117#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04005118 if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005119 return( 1 );
5120#endif
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005121 return( 0 );
5122}
5123
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005124static psa_status_t psa_hash_try_support( psa_algorithm_t alg )
5125{
5126 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
5127 psa_status_t status = psa_hash_setup( &operation, alg );
5128 psa_hash_abort( &operation );
5129 return( status );
5130}
5131
Gilles Peskine969c5d62019-01-16 15:53:06 +01005132static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005133 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005134 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005135{
Janos Follath5fe19732019-06-20 15:09:30 +01005136 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5137 * initialisers for this union leave some bytes unspecified.) */
5138 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5139
Gilles Peskine969c5d62019-01-16 15:53:06 +01005140 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005141 if( ! is_kdf_alg_supported( kdf_alg ) )
5142 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop07cc04a2020-11-16 22:08:34 -08005143
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005144 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005145 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005146 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
5147 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04005148 if( kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005149 {
5150 if( hash_size == 0 )
5151 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005152
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005153 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5154 * we might fail later, which is somewhat unfriendly and potentially
5155 * risk-prone. */
5156 psa_status_t status = psa_hash_try_support( hash_alg );
5157 if( status != PSA_SUCCESS )
5158 return( status );
5159 }
5160 else
5161 {
5162 hash_size = PSA_HASH_LENGTH( PSA_ALG_SHA_256 );
5163 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005164
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005165 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5166 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
5167 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005168 {
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005169 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005170 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005171#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005172 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurekb0936502022-09-16 07:13:00 -04005173 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ||
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04005174 ( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) )
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005175 operation->capacity = hash_size;
5176 else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005177#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5178 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005179 operation->capacity = 255 * hash_size;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005180 return( PSA_SUCCESS );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005181}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005182
5183static psa_status_t psa_key_agreement_try_support( psa_algorithm_t alg )
5184{
5185#if defined(PSA_WANT_ALG_ECDH)
5186 if( alg == PSA_ALG_ECDH )
5187 return( PSA_SUCCESS );
5188#endif
5189 (void) alg;
5190 return( PSA_ERROR_NOT_SUPPORTED );
5191}
John Durkop07cc04a2020-11-16 22:08:34 -08005192#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005193
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005194psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005195 psa_algorithm_t alg )
5196{
5197 psa_status_t status;
5198
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005200 return( PSA_ERROR_BAD_STATE );
5201
Gilles Peskine6843c292019-01-18 16:44:49 +01005202 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5203 return( PSA_ERROR_INVALID_ARGUMENT );
5204 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005205 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005206#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005207 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005208 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( alg );
5209 status = psa_key_agreement_try_support( ka_alg );
5210 if( status != PSA_SUCCESS )
5211 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005213#else
5214 return( PSA_ERROR_NOT_SUPPORTED );
5215#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005216 }
5217 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5218 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005219#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005220 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005221#else
5222 return( PSA_ERROR_NOT_SUPPORTED );
5223#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005224 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005225 else
5226 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005227
5228 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005229 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005230 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005231}
5232
Przemek Stekiel69c46792022-06-10 12:59:51 +02005233#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005234static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005235 psa_algorithm_t kdf_alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005236 psa_key_derivation_step_t step,
5237 const uint8_t *data,
5238 size_t data_length )
5239{
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005240 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005241 psa_status_t status;
5242 switch( step )
5243 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005244 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005245#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005246 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) )
5247 return( PSA_ERROR_INVALID_ARGUMENT );
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005248#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine2b522db2019-04-12 15:11:49 +02005249 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005250 return( PSA_ERROR_BAD_STATE );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005251 else
5252 {
Steven Cooreman094a77e2021-05-06 17:58:36 +02005253 status = psa_key_derivation_start_hmac( &hkdf->hmac,
5254 hash_alg,
5255 data, data_length );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005256 if( status != PSA_SUCCESS )
5257 return( status );
5258 hkdf->state = HKDF_STATE_STARTED;
5259 return( PSA_SUCCESS );
5260 }
Gilles Peskine03410b52019-05-16 16:05:19 +02005261 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005262#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005263 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005264 {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02005265 /* We shouldn't be in different state as HKDF_EXPAND only allows
5266 * two inputs: SECRET (this case) and INFO which does not modify
5267 * the state. It could happen only if the hkdf
5268 * object was corrupted. */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005269 if( hkdf->state != HKDF_STATE_INIT )
5270 return( PSA_ERROR_BAD_STATE );
5271
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02005272 /* Allow only input that fits expected prk size */
5273 if( data_length != PSA_HASH_LENGTH( hash_alg ) )
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005274 return( PSA_ERROR_INVALID_ARGUMENT );
5275
5276 memcpy( hkdf->prk, data, data_length );
5277 }
5278 else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005279#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005280 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005281 /* HKDF: If no salt was provided, use an empty salt.
5282 * HKDF-EXTRACT: salt is mandatory. */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005283 if( hkdf->state == HKDF_STATE_INIT )
5284 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005285#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
5286 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5287 return( PSA_ERROR_BAD_STATE );
5288#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005289 status = psa_key_derivation_start_hmac( &hkdf->hmac,
5290 hash_alg,
5291 NULL, 0 );
5292 if( status != PSA_SUCCESS )
5293 return( status );
5294 hkdf->state = HKDF_STATE_STARTED;
5295 }
5296 if( hkdf->state != HKDF_STATE_STARTED )
5297 return( PSA_ERROR_BAD_STATE );
5298 status = psa_mac_update( &hkdf->hmac,
5299 data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005300 if( status != PSA_SUCCESS )
5301 return( status );
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005302 status = psa_mac_sign_finish( &hkdf->hmac,
5303 hkdf->prk,
5304 sizeof( hkdf->prk ),
5305 &data_length );
5306 if( status != PSA_SUCCESS )
5307 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005308 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005309
Gilles Peskine2b522db2019-04-12 15:11:49 +02005310 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005311 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005312#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005313 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5314 {
5315 /* The only block of output is the PRK. */
5316 memcpy( hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH( hash_alg ) );
5317 hkdf->offset_in_block = 0;
5318 }
5319 else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005320#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005321 {
5322 /* Block 0 is empty, and the next block will be
5323 * generated by psa_key_derivation_hkdf_read(). */
5324 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
5325 }
5326
Gilles Peskine2b522db2019-04-12 15:11:49 +02005327 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005328 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005329#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005330 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5331 return( PSA_ERROR_INVALID_ARGUMENT );
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005332#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02005333#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
5334 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) &&
5335 hkdf->state == HKDF_STATE_INIT )
5336 return( PSA_ERROR_BAD_STATE );
5337#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005338 if( hkdf->state == HKDF_STATE_OUTPUT )
5339 return( PSA_ERROR_BAD_STATE );
5340 if( hkdf->info_set )
5341 return( PSA_ERROR_BAD_STATE );
5342 hkdf->info_length = data_length;
5343 if( data_length != 0 )
5344 {
5345 hkdf->info = mbedtls_calloc( 1, data_length );
5346 if( hkdf->info == NULL )
5347 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5348 memcpy( hkdf->info, data, data_length );
5349 }
5350 hkdf->info_set = 1;
5351 return( PSA_SUCCESS );
5352 default:
5353 return( PSA_ERROR_INVALID_ARGUMENT );
5354 }
5355}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005356#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005357
John Durkop07cc04a2020-11-16 22:08:34 -08005358#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5359 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005360static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5361 const uint8_t *data,
5362 size_t data_length )
5363{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005364 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005365 return( PSA_ERROR_BAD_STATE );
5366
Janos Follathd6dce9f2019-07-04 09:11:38 +01005367 if( data_length != 0 )
5368 {
5369 prf->seed = mbedtls_calloc( 1, data_length );
5370 if( prf->seed == NULL )
5371 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005372
Janos Follathd6dce9f2019-07-04 09:11:38 +01005373 memcpy( prf->seed, data, data_length );
5374 prf->seed_length = data_length;
5375 }
Janos Follathf08e2652019-06-13 09:05:41 +01005376
Gilles Peskine43f958b2020-12-13 14:55:14 +01005377 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005378
5379 return( PSA_SUCCESS );
5380}
5381
Janos Follath81550542019-06-13 14:26:34 +01005382static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
Janos Follath81550542019-06-13 14:26:34 +01005383 const uint8_t *data,
5384 size_t data_length )
5385{
Przemek Stekield7a28642022-04-07 14:58:33 +02005386 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
5387 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET )
Janos Follath81550542019-06-13 14:26:34 +01005388 return( PSA_ERROR_BAD_STATE );
5389
Steven Cooremana6df6042021-04-29 19:32:25 +02005390 if( data_length != 0 )
5391 {
5392 prf->secret = mbedtls_calloc( 1, data_length );
5393 if( prf->secret == NULL )
5394 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5395
5396 memcpy( prf->secret, data, data_length );
5397 prf->secret_length = data_length;
5398 }
Janos Follath81550542019-06-13 14:26:34 +01005399
Gilles Peskine43f958b2020-12-13 14:55:14 +01005400 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005401
5402 return( PSA_SUCCESS );
5403}
5404
Janos Follath63028dd2019-06-13 09:15:47 +01005405static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5406 const uint8_t *data,
5407 size_t data_length )
5408{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005409 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005410 return( PSA_ERROR_BAD_STATE );
5411
Janos Follathd6dce9f2019-07-04 09:11:38 +01005412 if( data_length != 0 )
5413 {
5414 prf->label = mbedtls_calloc( 1, data_length );
5415 if( prf->label == NULL )
5416 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005417
Janos Follathd6dce9f2019-07-04 09:11:38 +01005418 memcpy( prf->label, data, data_length );
5419 prf->label_length = data_length;
5420 }
Janos Follath63028dd2019-06-13 09:15:47 +01005421
Gilles Peskine43f958b2020-12-13 14:55:14 +01005422 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005423
5424 return( PSA_SUCCESS );
5425}
5426
Janos Follathb03233e2019-06-11 15:30:30 +01005427static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
Janos Follathb03233e2019-06-11 15:30:30 +01005428 psa_key_derivation_step_t step,
5429 const uint8_t *data,
5430 size_t data_length )
5431{
Janos Follathb03233e2019-06-11 15:30:30 +01005432 switch( step )
5433 {
Janos Follathf08e2652019-06-13 09:05:41 +01005434 case PSA_KEY_DERIVATION_INPUT_SEED:
5435 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005436 case PSA_KEY_DERIVATION_INPUT_SECRET:
Steven Cooremana6df6042021-04-29 19:32:25 +02005437 return( psa_tls12_prf_set_key( prf, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005438 case PSA_KEY_DERIVATION_INPUT_LABEL:
5439 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005440 default:
5441 return( PSA_ERROR_INVALID_ARGUMENT );
5442 }
5443}
John Durkop07cc04a2020-11-16 22:08:34 -08005444#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5445 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5446
5447#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5448static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5449 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005450 const uint8_t *data,
5451 size_t data_length )
5452{
5453 psa_status_t status;
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005454 const size_t pms_len = ( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
5455 4 + data_length + prf->other_secret_length :
5456 4 + 2 * data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08005457
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005458 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005459 return( PSA_ERROR_INVALID_ARGUMENT );
5460
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005461 uint8_t *pms = mbedtls_calloc( 1, pms_len );
Przemek Stekiel937b90f2022-04-20 08:33:13 +02005462 if( pms == NULL )
5463 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005464 uint8_t *cur = pms;
5465
5466 /* pure-PSK:
5467 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08005468 *
5469 * The premaster secret is formed as follows: if the PSK is N octets
5470 * long, concatenate a uint16 with the value N, N zero octets, a second
5471 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005472 *
5473 * mixed-PSK:
5474 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
5475 * follows: concatenate a uint16 with the length of the other secret,
5476 * the other secret itself, uint16 with the length of PSK, and the
5477 * PSK itself.
5478 * For details please check:
5479 * - RFC 4279, Section 4 for the definition of RSA-PSK,
5480 * - RFC 4279, Section 3 for the definition of DHE-PSK,
5481 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08005482 */
5483
Przemek Stekiel4e47a912022-04-21 11:40:18 +02005484 if( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET )
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005485 {
5486 *cur++ = MBEDTLS_BYTE_1( prf->other_secret_length );
5487 *cur++ = MBEDTLS_BYTE_0( prf->other_secret_length );
Przemek Stekiel4e47a912022-04-21 11:40:18 +02005488 if( prf->other_secret_length != 0 )
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02005489 {
5490 memcpy( cur, prf->other_secret, prf->other_secret_length );
Przemek Stekiel03faf5d22022-04-20 08:37:43 +02005491 mbedtls_platform_zeroize( prf->other_secret, prf->other_secret_length );
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02005492 cur += prf->other_secret_length;
5493 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005494 }
5495 else
5496 {
5497 *cur++ = MBEDTLS_BYTE_1( data_length );
5498 *cur++ = MBEDTLS_BYTE_0( data_length );
5499 memset( cur, 0, data_length );
5500 cur += data_length;
5501 }
5502
Joe Subbiani5ecac212021-06-24 13:00:03 +01005503 *cur++ = MBEDTLS_BYTE_1( data_length );
5504 *cur++ = MBEDTLS_BYTE_0( data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08005505 memcpy( cur, data, data_length );
5506 cur += data_length;
5507
Steven Cooremana6df6042021-04-29 19:32:25 +02005508 status = psa_tls12_prf_set_key( prf, pms, cur - pms );
John Durkop07cc04a2020-11-16 22:08:34 -08005509
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005510 mbedtls_platform_zeroize( pms, pms_len );
5511 mbedtls_free( pms );
John Durkop07cc04a2020-11-16 22:08:34 -08005512 return( status );
5513}
Janos Follath6660f0e2019-06-17 08:44:03 +01005514
Przemek Stekiele47201b2022-04-19 13:53:28 +02005515static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
5516 psa_tls12_prf_key_derivation_t *prf,
5517 const uint8_t *data,
5518 size_t data_length )
5519{
5520 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
5521 return( PSA_ERROR_BAD_STATE );
5522
5523 if( data_length != 0 )
5524 {
5525 prf->other_secret = mbedtls_calloc( 1, data_length );
5526 if( prf->other_secret == NULL )
5527 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5528
5529 memcpy( prf->other_secret, data, data_length );
5530 prf->other_secret_length = data_length;
5531 }
5532 else
5533 {
5534 prf->other_secret_length = 0;
5535 }
5536
5537 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
5538
5539 return( PSA_SUCCESS );
5540}
5541
Janos Follath6660f0e2019-06-17 08:44:03 +01005542static psa_status_t psa_tls12_prf_psk_to_ms_input(
5543 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005544 psa_key_derivation_step_t step,
5545 const uint8_t *data,
5546 size_t data_length )
5547{
Przemek Stekiele47201b2022-04-19 13:53:28 +02005548 switch( step )
Janos Follath0c1ed842019-06-28 13:35:36 +01005549 {
Przemek Stekiele47201b2022-04-19 13:53:28 +02005550 case PSA_KEY_DERIVATION_INPUT_SECRET:
5551 return( psa_tls12_prf_psk_to_ms_set_key( prf,
5552 data, data_length ) );
5553 break;
5554 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
5555 return( psa_tls12_prf_psk_to_ms_set_other_key( prf,
5556 data,
5557 data_length ) );
5558 break;
5559 default:
5560 return( psa_tls12_prf_input( prf, step, data, data_length ) );
5561 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01005562
Przemek Stekiele47201b2022-04-19 13:53:28 +02005563 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005564}
John Durkop07cc04a2020-11-16 22:08:34 -08005565#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005566
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005567#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5568static psa_status_t psa_tls12_ecjpake_to_pms_input(
5569 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04005570 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005571 const uint8_t *data,
5572 size_t data_length )
5573{
Andrzej Kurek702776f2022-09-16 06:22:44 -04005574 if( data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
5575 step != PSA_KEY_DERIVATION_INPUT_SECRET )
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005576 {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005577 return( PSA_ERROR_INVALID_ARGUMENT );
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005578 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005579
5580 /* Check if the passed point is in an uncompressed form */
5581 if( data[0] != 0x04 )
5582 return( PSA_ERROR_INVALID_ARGUMENT );
5583
5584 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
5585 memcpy( ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
5586
5587 return( PSA_SUCCESS );
5588}
5589#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02005590/** Check whether the given key type is acceptable for the given
5591 * input step of a key derivation.
5592 *
5593 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5594 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5595 * Both secret and non-secret inputs can alternatively have the type
5596 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5597 * that the input was passed as a buffer rather than via a key object.
5598 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005599static int psa_key_derivation_check_input_type(
5600 psa_key_derivation_step_t step,
5601 psa_key_type_t key_type )
5602{
5603 switch( step )
5604 {
5605 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005606 if( key_type == PSA_KEY_TYPE_DERIVE )
5607 return( PSA_SUCCESS );
5608 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005609 return( PSA_SUCCESS );
5610 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02005611 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
5612 if( key_type == PSA_KEY_TYPE_DERIVE )
5613 return( PSA_SUCCESS );
5614 if( key_type == PSA_KEY_TYPE_NONE )
5615 return( PSA_SUCCESS );
5616 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005617 case PSA_KEY_DERIVATION_INPUT_LABEL:
5618 case PSA_KEY_DERIVATION_INPUT_SALT:
5619 case PSA_KEY_DERIVATION_INPUT_INFO:
5620 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005621 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5622 return( PSA_SUCCESS );
5623 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005624 return( PSA_SUCCESS );
5625 break;
5626 }
5627 return( PSA_ERROR_INVALID_ARGUMENT );
5628}
5629
Janos Follathb80a94e2019-06-12 15:54:46 +01005630static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005631 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005632 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005633 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005634 const uint8_t *data,
5635 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005636{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005637 psa_status_t status;
5638 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5639
5640 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005641 if( status != PSA_SUCCESS )
5642 goto exit;
5643
Przemek Stekiel69c46792022-06-10 12:59:51 +02005644#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiela29b4882022-06-02 11:37:03 +02005645 if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005646 {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005647 status = psa_hkdf_input( &operation->ctx.hkdf, kdf_alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005648 step, data, data_length );
5649 }
John Durkop07cc04a2020-11-16 22:08:34 -08005650 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005651#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005652#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5653 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005654 {
Janos Follathb03233e2019-06-11 15:30:30 +01005655 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
Janos Follathb03233e2019-06-11 15:30:30 +01005656 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005657 }
John Durkop07cc04a2020-11-16 22:08:34 -08005658 else
5659#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5660#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5661 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005662 {
5663 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005664 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005665 }
5666 else
John Durkop07cc04a2020-11-16 22:08:34 -08005667#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005668#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04005669 if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005670 {
5671 status = psa_tls12_ecjpake_to_pms_input(
Andrzej Kurek702776f2022-09-16 06:22:44 -04005672 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length );
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005673 }
5674 else
5675#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005676 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005677 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005678 (void) data;
5679 (void) data_length;
5680 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005681 return( PSA_ERROR_BAD_STATE );
5682 }
5683
Gilles Peskine224b0d62019-09-23 18:13:17 +02005684exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005685 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005686 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005687 return( status );
5688}
5689
Janos Follath51f4a0f2019-06-14 11:35:55 +01005690psa_status_t psa_key_derivation_input_bytes(
5691 psa_key_derivation_operation_t *operation,
5692 psa_key_derivation_step_t step,
5693 const uint8_t *data,
5694 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005695{
Gilles Peskineb8965192019-09-24 16:21:10 +02005696 return( psa_key_derivation_input_internal( operation, step,
5697 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005698 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005699}
5700
Janos Follath51f4a0f2019-06-14 11:35:55 +01005701psa_status_t psa_key_derivation_input_key(
5702 psa_key_derivation_operation_t *operation,
5703 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005704 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005705{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005707 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005708 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005709
Ronald Cron5c522922020-11-14 16:35:34 +01005710 status = psa_get_and_lock_transparent_key_slot_with_policy(
5711 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005712 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005713 {
5714 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005715 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005716 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005717
5718 /* Passing a key object as a SECRET input unlocks the permission
5719 * to output to a key object. */
5720 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5721 operation->can_output_key = 1;
5722
Ronald Cronf95a2b12020-10-22 15:24:49 +02005723 status = psa_key_derivation_input_internal( operation,
5724 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005725 slot->key.data,
5726 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005727
Ronald Cron5c522922020-11-14 16:35:34 +01005728 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005729
Ronald Cron5c522922020-11-14 16:35:34 +01005730 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005731}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005732
5733
5734
5735/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005736/* Key agreement */
5737/****************************************************************/
5738
John Durkop6ba40d12020-11-10 08:50:04 -08005739#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005740static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5741 size_t peer_key_length,
5742 const mbedtls_ecp_keypair *our_key,
5743 uint8_t *shared_secret,
5744 size_t shared_secret_size,
5745 size_t *shared_secret_length )
5746{
Steven Cooremand4867872020-08-05 16:31:39 +02005747 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005748 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005749 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005750 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005751 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005752 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005753
Ronald Cron90857082020-11-25 14:58:33 +01005754 status = mbedtls_psa_ecp_load_representation(
5755 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005756 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005757 peer_key,
5758 peer_key_length,
5759 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005760 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005761 goto exit;
5762
Jaeden Amero97271b32019-01-10 19:38:51 +00005763 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005764 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005765 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005766 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005767 status = mbedtls_to_psa_error(
5768 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5769 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005770 goto exit;
5771
Jaeden Amero97271b32019-01-10 19:38:51 +00005772 status = mbedtls_to_psa_error(
5773 mbedtls_ecdh_calc_secret( &ecdh,
5774 shared_secret_length,
5775 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005776 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005777 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005778 if( status != PSA_SUCCESS )
5779 goto exit;
5780 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5781 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005782
5783exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005784 if( status != PSA_SUCCESS )
5785 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005786 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005787 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005788 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005789
Jaeden Amero97271b32019-01-10 19:38:51 +00005790 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005791}
John Durkop6ba40d12020-11-10 08:50:04 -08005792#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005793
Gilles Peskine01d718c2018-09-18 12:01:02 +02005794#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5795
Gilles Peskine0216fe12019-04-11 21:23:21 +02005796static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5797 psa_key_slot_t *private_key,
5798 const uint8_t *peer_key,
5799 size_t peer_key_length,
5800 uint8_t *shared_secret,
5801 size_t shared_secret_size,
5802 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005803{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005804 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005805 {
John Durkop6ba40d12020-11-10 08:50:04 -08005806#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005807 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005808 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005809 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005810 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005811 psa_status_t status = mbedtls_psa_ecp_load_representation(
5812 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005813 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005814 private_key->key.data,
5815 private_key->key.bytes,
5816 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005817 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005818 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005819 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005820 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005821 shared_secret, shared_secret_size,
5822 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005823 mbedtls_ecp_keypair_free( ecp );
5824 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005825 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005826#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005827 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005828 (void) private_key;
5829 (void) peer_key;
5830 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005831 (void) shared_secret;
5832 (void) shared_secret_size;
5833 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005834 return( PSA_ERROR_NOT_SUPPORTED );
5835 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005836}
5837
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005838/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005839 * to potentially free embedded data structures and wipe confidential data.
5840 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005841static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005842 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005843 psa_key_slot_t *private_key,
5844 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005845 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005846{
5847 psa_status_t status;
5848 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5849 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005850 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005851
5852 /* Step 1: run the secret agreement algorithm to generate the shared
5853 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005854 status = psa_key_agreement_raw_internal( ka_alg,
5855 private_key,
5856 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005857 shared_secret,
5858 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005859 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005860 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005861 goto exit;
5862
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005863 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005864 * the shared secret. A shared secret is permitted wherever a key
5865 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005866 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005867 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005868 shared_secret,
5869 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005870exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005871 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005872 return( status );
5873}
5874
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005875psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005876 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005877 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005878 const uint8_t *peer_key,
5879 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005880{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005882 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005883 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005884
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005885 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005886 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005887 status = psa_get_and_lock_transparent_key_slot_with_policy(
5888 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005889 if( status != PSA_SUCCESS )
5890 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005891 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005892 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005893 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005894 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005895 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005896 else
5897 {
5898 /* If a private key has been added as SECRET, we allow the derived
5899 * key material to be used as a key in PSA Crypto. */
5900 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5901 operation->can_output_key = 1;
5902 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005903
Ronald Cron5c522922020-11-14 16:35:34 +01005904 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005905
Ronald Cron5c522922020-11-14 16:35:34 +01005906 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005907}
5908
Gilles Peskinebe697d82019-05-16 18:00:41 +02005909psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005910 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005911 const uint8_t *peer_key,
5912 size_t peer_key_length,
5913 uint8_t *output,
5914 size_t output_size,
5915 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005916{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005917 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005918 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005919 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005920
5921 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5922 {
5923 status = PSA_ERROR_INVALID_ARGUMENT;
5924 goto exit;
5925 }
Ronald Cron5c522922020-11-14 16:35:34 +01005926 status = psa_get_and_lock_transparent_key_slot_with_policy(
5927 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005928 if( status != PSA_SUCCESS )
5929 goto exit;
5930
Gilles Peskine3e561302022-04-14 00:17:15 +02005931 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5932 * for the output size. The PSA specification only guarantees that this
5933 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5934 * but it might be nice to allow smaller buffers if the output fits.
5935 * At the time of writing this comment, with only ECDH implemented,
5936 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5937 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5938 * be exact for it as well. */
5939 size_t expected_length =
5940 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( slot->attr.type, slot->attr.bits );
5941 if( output_size < expected_length )
5942 {
5943 status = PSA_ERROR_BUFFER_TOO_SMALL;
5944 goto exit;
5945 }
5946
Gilles Peskine0216fe12019-04-11 21:23:21 +02005947 status = psa_key_agreement_raw_internal( alg, slot,
5948 peer_key, peer_key_length,
5949 output, output_size,
5950 output_length );
5951
5952exit:
5953 if( status != PSA_SUCCESS )
5954 {
5955 /* If an error happens and is not handled properly, the output
5956 * may be used as a key to protect sensitive data. Arrange for such
5957 * a key to be random, which is likely to result in decryption or
5958 * verification errors. This is better than filling the buffer with
5959 * some constant data such as zeros, which would result in the data
5960 * being protected with a reproducible, easily knowable key.
5961 */
5962 psa_generate_random( output, output_size );
5963 *output_length = output_size;
5964 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005965
Ronald Cron5c522922020-11-14 16:35:34 +01005966 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005967
Ronald Cron5c522922020-11-14 16:35:34 +01005968 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005969}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005970
5971
Gilles Peskine30524eb2020-11-13 17:02:26 +01005972
Gilles Peskine86a440b2018-11-12 18:39:40 +01005973/****************************************************************/
5974/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005975/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005976
Gilles Peskine30524eb2020-11-13 17:02:26 +01005977/** Initialize the PSA random generator.
5978 */
5979static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5980{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005981#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5982 memset( rng, 0, sizeof( *rng ) );
5983#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5984
Gilles Peskine30524eb2020-11-13 17:02:26 +01005985 /* Set default configuration if
5986 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5987 if( rng->entropy_init == NULL )
5988 rng->entropy_init = mbedtls_entropy_init;
5989 if( rng->entropy_free == NULL )
5990 rng->entropy_free = mbedtls_entropy_free;
5991
5992 rng->entropy_init( &rng->entropy );
5993#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5994 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5995 /* The PSA entropy injection feature depends on using NV seed as an entropy
5996 * source. Add NV seed as an entropy source for PSA entropy injection. */
5997 mbedtls_entropy_add_source( &rng->entropy,
5998 mbedtls_nv_seed_poll, NULL,
5999 MBEDTLS_ENTROPY_BLOCK_SIZE,
6000 MBEDTLS_ENTROPY_SOURCE_STRONG );
6001#endif
6002
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006003 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006004#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006005}
6006
6007/** Deinitialize the PSA random generator.
6008 */
6009static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6010{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006011#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6012 memset( rng, 0, sizeof( *rng ) );
6013#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006014 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006015 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006016#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006017}
6018
6019/** Seed the PSA random generator.
6020 */
6021static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6022{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006023#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6024 /* Do nothing: the external RNG seeds itself. */
6025 (void) rng;
6026 return( PSA_SUCCESS );
6027#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006028 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006029 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6030 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006031 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006032#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006033}
6034
Gilles Peskinee59236f2018-01-27 23:32:46 +01006035psa_status_t psa_generate_random( uint8_t *output,
6036 size_t output_size )
6037{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006038 GUARD_MODULE_INITIALIZED;
6039
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006040#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6041
6042 size_t output_length = 0;
6043 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6044 output, output_size,
6045 &output_length );
6046 if( status != PSA_SUCCESS )
6047 return( status );
6048 /* Breaking up a request into smaller chunks is currently not supported
6049 * for the extrernal RNG interface. */
6050 if( output_length != output_size )
6051 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6052 return( PSA_SUCCESS );
6053
6054#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6055
Gilles Peskine71ddab92021-01-04 21:01:07 +01006056 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006057 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006058 size_t request_size =
6059 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6060 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6061 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006062 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6063 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006064 if( ret != 0 )
6065 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006066 output_size -= request_size;
6067 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006068 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006069 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006070#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006071}
6072
Gilles Peskine8814fc42020-12-14 15:33:44 +01006073/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006074 *
6075 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6076 * `psa_generate_random(...)`. The state parameter is ignored since the
6077 * PSA API doesn't support passing an explicit state.
6078 *
6079 * In the non-external case, psa_generate_random() calls an
6080 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6081 * and semantics as mbedtls_psa_get_random(). As an optimization,
6082 * instead of doing this back-and-forth between the PSA API and the
6083 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6084 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006085 */
6086#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6087int mbedtls_psa_get_random( void *p_rng,
6088 unsigned char *output,
6089 size_t output_size )
6090{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006091 /* This function takes a pointer to the RNG state because that's what
6092 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6093 * its own state internally and doesn't let the caller access that state.
6094 * So we just ignore the state parameter, and in practice we'll pass
6095 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006096 (void) p_rng;
6097 psa_status_t status = psa_generate_random( output, output_size );
6098 if( status == PSA_SUCCESS )
6099 return( 0 );
6100 else
6101 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6102}
6103#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6104
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006105#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006106#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006107
Gilles Peskine7228da22019-07-15 11:06:15 +02006108psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006109 size_t seed_size )
6110{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006111 if( global_data.initialized )
6112 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006113
6114 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6115 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6116 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6117 return( PSA_ERROR_INVALID_ARGUMENT );
6118
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006119 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006120}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006121#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006122
Ronald Cron3772afe2021-02-08 16:10:05 +01006123/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006124 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006125 * \param type The key type
6126 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006127 *
6128 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006129 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006130 * \retval #PSA_ERROR_INVALID_ARGUMENT
6131 * The size in bits of the key is not valid.
6132 * \retval #PSA_ERROR_NOT_SUPPORTED
6133 * The type and/or the size in bits of the key or the combination of
6134 * the two is not supported.
6135 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006136static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6137 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006138{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006139 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006140
Gilles Peskinee59236f2018-01-27 23:32:46 +01006141 if( key_type_is_raw_bytes( type ) )
6142 {
Archana4d7ae1d2021-07-07 02:50:22 +05306143 status = psa_validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006144 if( status != PSA_SUCCESS )
6145 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006146 }
6147 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006148#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006149 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6150 {
6151 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6152 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006153
Ronald Cron01b2aba2020-10-05 09:42:02 +02006154 /* Accept only byte-aligned keys, for the same reasons as
6155 * in psa_import_rsa_key(). */
6156 if( bits % 8 != 0 )
6157 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006158 }
6159 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006160#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006161
Ronald Cron5c4d3862020-12-07 11:07:24 +01006162#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006163 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6164 {
Ronald Crond81ab562021-02-16 09:01:16 +01006165 /* To avoid empty block, return successfully here. */
6166 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006167 }
6168 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006169#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006170 {
6171 return( PSA_ERROR_NOT_SUPPORTED );
6172 }
6173
6174 return( PSA_SUCCESS );
6175}
6176
Ronald Cron55ed0592020-10-05 10:30:40 +02006177psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006178 const psa_key_attributes_t *attributes,
6179 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006180{
6181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006182 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006183
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006184 if( ( attributes->domain_parameters == NULL ) &&
6185 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006186 return( PSA_ERROR_INVALID_ARGUMENT );
6187
Ronald Cron01b2aba2020-10-05 09:42:02 +02006188 if( key_type_is_raw_bytes( type ) )
6189 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006190 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006191 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006192 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006193
David Brown12ca5032021-02-11 11:02:00 -07006194#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006195 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006196 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006197#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006198 }
6199 else
6200
Jaeden Amero424fa932021-05-14 08:34:32 +01006201#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6202 defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006203 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006204 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006205 return( mbedtls_psa_rsa_generate_key( attributes,
6206 key_buffer,
6207 key_buffer_size,
6208 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006209 }
6210 else
Jaeden Amero424fa932021-05-14 08:34:32 +01006211#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6212 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006213
John Durkop9814fa22020-11-04 12:28:15 -08006214#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006215 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006216 {
Ronald Cron7023db52020-11-20 18:17:42 +01006217 return( mbedtls_psa_ecp_generate_key( attributes,
6218 key_buffer,
6219 key_buffer_size,
6220 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006221 }
6222 else
John Durkop9814fa22020-11-04 12:28:15 -08006223#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006224 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006225 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006226 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006227 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006228
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006229 return( PSA_SUCCESS );
6230}
Darryl Green0c6575a2018-11-07 16:05:30 +00006231
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006232psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006233 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006234{
6235 psa_status_t status;
6236 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006237 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006238 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006239
Ronald Cron81709fc2020-11-14 12:10:32 +01006240 *key = MBEDTLS_SVC_KEY_ID_INIT;
6241
Gilles Peskine0f84d622019-09-12 19:03:13 +02006242 /* Reject any attempt to create a zero-length key so that we don't
6243 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6244 if( psa_get_key_bits( attributes ) == 0 )
6245 return( PSA_ERROR_INVALID_ARGUMENT );
6246
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006247 /* Reject any attempt to create a public key. */
6248 if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) )
6249 return( PSA_ERROR_INVALID_ARGUMENT );
6250
Ronald Cron81709fc2020-11-14 12:10:32 +01006251 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6252 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006253 if( status != PSA_SUCCESS )
6254 goto exit;
6255
Ronald Cron977c2472020-10-13 08:32:21 +02006256 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05306257 * storage ( thus not in the case of generating a key in a secure element
6258 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
6259 * buffer to hold the generated key material. */
Ronald Cron977c2472020-10-13 08:32:21 +02006260 if( slot->key.data == NULL )
6261 {
6262 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6263 PSA_KEY_LOCATION_LOCAL_STORAGE )
6264 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006265 status = psa_validate_key_type_and_size_for_key_generation(
6266 attributes->core.type, attributes->core.bits );
6267 if( status != PSA_SUCCESS )
6268 goto exit;
6269
6270 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6271 attributes->core.type,
6272 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006273 }
6274 else
6275 {
6276 status = psa_driver_wrapper_get_key_buffer_size(
6277 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006278 if( status != PSA_SUCCESS )
6279 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006280 }
Ronald Cron977c2472020-10-13 08:32:21 +02006281
6282 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6283 if( status != PSA_SUCCESS )
6284 goto exit;
6285 }
6286
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006287 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006288 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006289
Ronald Cron2b56bc82020-10-05 10:02:26 +02006290 if( status != PSA_SUCCESS )
6291 psa_remove_key_data_from_memory( slot );
6292
Gilles Peskine11792082019-08-06 18:36:36 +02006293exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006294 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006295 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006296 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006297 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006298
Darryl Green0c6575a2018-11-07 16:05:30 +00006299 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006300}
6301
Gilles Peskinee59236f2018-01-27 23:32:46 +01006302/****************************************************************/
6303/* Module setup */
6304/****************************************************************/
6305
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006306#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006307psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6308 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6309 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6310{
6311 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6312 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006313 global_data.rng.entropy_init = entropy_init;
6314 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006315 return( PSA_SUCCESS );
6316}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006317#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006318
Gilles Peskinee59236f2018-01-27 23:32:46 +01006319void mbedtls_psa_crypto_free( void )
6320{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006321 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006322 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6323 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006324 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006325 }
6326 /* Wipe all remaining data, including configuration.
6327 * In particular, this sets all state indicator to the value
6328 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006329 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Ronald Cron9ba76912021-04-10 16:57:30 +02006330
6331 /* Terminate drivers */
6332 psa_driver_wrapper_free( );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006333}
6334
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006335#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6336/** Recover a transaction that was interrupted by a power failure.
6337 *
6338 * This function is called during initialization, before psa_crypto_init()
6339 * returns. If this function returns a failure status, the initialization
6340 * fails.
6341 */
6342static psa_status_t psa_crypto_recover_transaction(
6343 const psa_crypto_transaction_t *transaction )
6344{
6345 switch( transaction->unknown.type )
6346 {
6347 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6348 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006349 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006350 * is implemented.
6351 * https://github.com/ARMmbed/mbed-crypto/issues/218
6352 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006353 default:
6354 /* We found an unsupported transaction in the storage.
6355 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006356 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006357 }
6358}
6359#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6360
Gilles Peskinee59236f2018-01-27 23:32:46 +01006361psa_status_t psa_crypto_init( void )
6362{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006363 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006364
Gilles Peskinec6b69072018-11-20 21:42:52 +01006365 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006366 if( global_data.initialized != 0 )
6367 return( PSA_SUCCESS );
6368
Gilles Peskine30524eb2020-11-13 17:02:26 +01006369 /* Initialize and seed the random generator. */
6370 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006371 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006372 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006373 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006374 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006375 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006376
Gilles Peskine66fb1262018-12-10 16:29:04 +01006377 status = psa_initialize_key_slots( );
6378 if( status != PSA_SUCCESS )
6379 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006380
Ronald Cron9ba76912021-04-10 16:57:30 +02006381 /* Init drivers */
6382 status = psa_driver_wrapper_init( );
Gilles Peskined9348f22019-10-01 15:22:29 +02006383 if( status != PSA_SUCCESS )
6384 goto exit;
Gilles Peskined9348f22019-10-01 15:22:29 +02006385
Gilles Peskine4b734222019-07-24 15:56:31 +02006386#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006387 status = psa_crypto_load_transaction( );
6388 if( status == PSA_SUCCESS )
6389 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006390 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6391 if( status != PSA_SUCCESS )
6392 goto exit;
6393 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006394 }
6395 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6396 {
6397 /* There's no transaction to complete. It's all good. */
6398 status = PSA_SUCCESS;
6399 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006400#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006401
Gilles Peskinec6b69072018-11-20 21:42:52 +01006402 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006403 global_data.initialized = 1;
6404
6405exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006406 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006407 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006408 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006409}
6410
6411#endif /* MBEDTLS_PSA_CRYPTO_C */