blob: 817fb95751bb141243b9e8cc865c4fa354a1cd4e [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
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"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010038#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010039#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020040#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020041#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020042#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010043#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010044/* Include internal declarations that are useful for implementing persistently
45 * stored keys. */
46#include "psa_crypto_storage.h"
47
Gilles Peskineb2b64d32020-12-14 16:43:58 +010048#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010049
Gilles Peskineb46bef22019-07-30 21:32:04 +020050#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include <stdlib.h>
52#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020054#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055#define mbedtls_calloc calloc
56#define mbedtls_free free
57#endif
58
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010059#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020061#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000062#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020063#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/blowfish.h"
65#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020066#include "mbedtls/chacha20.h"
67#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/cipher.h"
69#include "mbedtls/ccm.h"
70#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020072#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010073#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010074#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/error.h"
76#include "mbedtls/gcm.h"
77#include "mbedtls/md2.h"
78#include "mbedtls/md4.h"
79#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010080#include "mbedtls/md.h"
81#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010082#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010083#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010084#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000085#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010087#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088#include "mbedtls/sha1.h"
89#include "mbedtls/sha256.h"
90#include "mbedtls/sha512.h"
91#include "mbedtls/xtea.h"
92
Gilles Peskine996deb12018-08-01 15:45:45 +020093#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
94
Gilles Peskine9ef733f2018-02-07 21:05:37 +010095/* constant-time buffer comparison */
96static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
97{
98 size_t i;
99 unsigned char diff = 0;
100
101 for( i = 0; i < n; i++ )
102 diff |= a[i] ^ b[i];
103
104 return( diff );
105}
106
107
108
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100109/****************************************************************/
110/* Global data, support functions and library management */
111/****************************************************************/
112
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200113static int key_type_is_raw_bytes( psa_key_type_t type )
114{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200115 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200116}
117
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100118/* Values for psa_global_data_t::rng_state */
119#define RNG_NOT_INITIALIZED 0
120#define RNG_INITIALIZED 1
121#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100122
Gilles Peskine2d277862018-06-18 15:41:12 +0200123typedef struct
124{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100125 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100126 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100127 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100128} psa_global_data_t;
129
130static psa_global_data_t global_data;
131
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100132#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
133mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
134 &global_data.rng.drbg;
135#endif
136
itayzafrir0adf0fc2018-09-06 16:24:41 +0300137#define GUARD_MODULE_INITIALIZED \
138 if( global_data.initialized == 0 ) \
139 return( PSA_ERROR_BAD_STATE );
140
Steven Cooreman01164162020-07-20 15:31:37 +0200141psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100142{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100143 /* Mbed TLS error codes can combine a high-level error code and a
144 * low-level error code. The low-level error usually reflects the
145 * root cause better, so dispatch on that preferably. */
146 int low_level_ret = - ( -ret & 0x007f );
147 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100148 {
149 case 0:
150 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100151
152 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
153 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
154 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
155 return( PSA_ERROR_NOT_SUPPORTED );
156 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
157 return( PSA_ERROR_HARDWARE_FAILURE );
158
Gilles Peskine9a944802018-06-21 09:35:35 +0200159 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
160 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
161 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
162 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
163 case MBEDTLS_ERR_ASN1_INVALID_DATA:
164 return( PSA_ERROR_INVALID_ARGUMENT );
165 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
166 return( PSA_ERROR_INSUFFICIENT_MEMORY );
167 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
168 return( PSA_ERROR_BUFFER_TOO_SMALL );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
173 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
Jaeden Amero93e21112019-02-20 13:57:28 +0000180#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000181 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000182#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
183 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
184#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
186 return( PSA_ERROR_NOT_SUPPORTED );
187 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
188 return( PSA_ERROR_HARDWARE_FAILURE );
189
190 case MBEDTLS_ERR_CCM_BAD_INPUT:
191 return( PSA_ERROR_INVALID_ARGUMENT );
192 case MBEDTLS_ERR_CCM_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
195 return( PSA_ERROR_HARDWARE_FAILURE );
196
Gilles Peskine26869f22019-05-06 15:25:00 +0200197 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199
200 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
201 return( PSA_ERROR_BAD_STATE );
202 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
203 return( PSA_ERROR_INVALID_SIGNATURE );
204
Gilles Peskinea5905292018-02-07 20:59:33 +0100205 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
206 return( PSA_ERROR_NOT_SUPPORTED );
207 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
208 return( PSA_ERROR_INVALID_ARGUMENT );
209 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
210 return( PSA_ERROR_INSUFFICIENT_MEMORY );
211 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
212 return( PSA_ERROR_INVALID_PADDING );
213 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200214 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
216 return( PSA_ERROR_INVALID_SIGNATURE );
217 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200218 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100219 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
220 return( PSA_ERROR_HARDWARE_FAILURE );
221
222 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
223 return( PSA_ERROR_HARDWARE_FAILURE );
224
Gilles Peskine82e57d12020-11-13 21:31:17 +0100225#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
226 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100227 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
228 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
231 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
232 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
233 return( PSA_ERROR_NOT_SUPPORTED );
234 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
235 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100236#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100237
238 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
239 return( PSA_ERROR_NOT_SUPPORTED );
240 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
Gilles Peskinee59236f2018-01-27 23:32:46 +0100243 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
244 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
245 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
246 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100247
248 case MBEDTLS_ERR_GCM_AUTH_FAILED:
249 return( PSA_ERROR_INVALID_SIGNATURE );
250 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200251 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100252 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskine82e57d12020-11-13 21:31:17 +0100255#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
256 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100257 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
258 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100259 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
260 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
261 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
262 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
263 return( PSA_ERROR_NOT_SUPPORTED );
264 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
265 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
266#endif
267
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
269 return( PSA_ERROR_NOT_SUPPORTED );
270 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_MD_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
275 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100276
Gilles Peskinef76aa772018-10-29 19:24:33 +0100277 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
278 return( PSA_ERROR_STORAGE_FAILURE );
279 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
284 return( PSA_ERROR_BUFFER_TOO_SMALL );
285 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
292 return( PSA_ERROR_INSUFFICIENT_MEMORY );
293
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100294 case MBEDTLS_ERR_PK_ALLOC_FAILED:
295 return( PSA_ERROR_INSUFFICIENT_MEMORY );
296 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
297 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
298 return( PSA_ERROR_INVALID_ARGUMENT );
299 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100300 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
302 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
305 return( PSA_ERROR_NOT_SUPPORTED );
306 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
307 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
308 return( PSA_ERROR_NOT_PERMITTED );
309 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
310 return( PSA_ERROR_INVALID_ARGUMENT );
311 case MBEDTLS_ERR_PK_INVALID_ALG:
312 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
313 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
314 return( PSA_ERROR_NOT_SUPPORTED );
315 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
316 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
318 return( PSA_ERROR_HARDWARE_FAILURE );
319
Gilles Peskineff2d2002019-05-06 15:26:23 +0200320 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
322 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
323 return( PSA_ERROR_NOT_SUPPORTED );
324
Gilles Peskinea5905292018-02-07 20:59:33 +0100325 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
326 return( PSA_ERROR_INVALID_ARGUMENT );
327 case MBEDTLS_ERR_RSA_INVALID_PADDING:
328 return( PSA_ERROR_INVALID_PADDING );
329 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
332 return( PSA_ERROR_INVALID_ARGUMENT );
333 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
334 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200335 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100336 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
337 return( PSA_ERROR_INVALID_SIGNATURE );
338 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
339 return( PSA_ERROR_BUFFER_TOO_SMALL );
340 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100341 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100342 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
343 return( PSA_ERROR_NOT_SUPPORTED );
344 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
345 return( PSA_ERROR_HARDWARE_FAILURE );
346
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
348 return( PSA_ERROR_INVALID_ARGUMENT );
349 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
350 return( PSA_ERROR_HARDWARE_FAILURE );
351
itayzafrir5c753392018-05-08 11:18:38 +0300352 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300353 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300354 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300355 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
356 return( PSA_ERROR_BUFFER_TOO_SMALL );
357 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
358 return( PSA_ERROR_NOT_SUPPORTED );
359 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
360 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
361 return( PSA_ERROR_INVALID_SIGNATURE );
362 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
363 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100364 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
365 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300366 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
367 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100368
Janos Follatha13b9052019-11-22 12:48:59 +0000369 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
370 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300371
Gilles Peskinee59236f2018-01-27 23:32:46 +0100372 default:
David Saadab4ecc272019-02-14 13:48:10 +0200373 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100374 }
375}
376
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200377
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200378
379
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100380/****************************************************************/
381/* Key management */
382/****************************************************************/
383
Gilles Peskine73167e12019-07-12 23:44:37 +0200384#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
385static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
386{
Gilles Peskine8e338702019-07-30 20:06:31 +0200387 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200388}
389#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
390
John Durkop07cc04a2020-11-16 22:08:34 -0800391/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
392 * current test driver in key_management.c is using this function
393 * when accelerators are used for ECC key pair and public key.
394 * Once that dependency is resolved these guards can be removed.
395 */
John Durkop9814fa22020-11-04 12:28:15 -0800396#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800397 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
398 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
399 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100400mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100401 size_t bits,
402 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200403{
404 switch( curve )
405 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100406 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100407 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100408 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100409#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100410 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100411 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100412#endif
413#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100414 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100415 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100416#endif
417#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100418 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100419 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100420#endif
421#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100422 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100423 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100424#endif
425#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100426 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100427 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100428 case 528:
429 if( bits_is_sloppy )
430 return( MBEDTLS_ECP_DP_SECP521R1 );
431 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100432#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100433 }
434 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100435
Paul Elliott8ff510a2020-06-02 17:19:28 +0100436 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100437 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100438 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100439#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100440 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100441 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100442#endif
443#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100444 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100445 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100446#endif
447#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100448 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100449 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100451 }
452 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100453
Paul Elliott8ff510a2020-06-02 17:19:28 +0100454 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100456 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100458 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100459 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100460 case 256:
461 if( bits_is_sloppy )
462 return( MBEDTLS_ECP_DP_CURVE25519 );
463 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100464#endif
465#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100466 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100467 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100468#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100469 }
470 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100471
Paul Elliott8ff510a2020-06-02 17:19:28 +0100472 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100474 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100475#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100476 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100477 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100478#endif
479#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100480 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100481 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100482#endif
483#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100484 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100485 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100486#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100487 }
488 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200489 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100490
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100491 (void) bits_is_sloppy;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100492 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200493}
John Durkop9814fa22020-11-04 12:28:15 -0800494#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800495 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
496 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
497 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200498
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200499static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
500 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501{
502 /* Check that the bit size is acceptable for the key type */
503 switch( type )
504 {
505 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200507 case PSA_KEY_TYPE_DERIVE:
508 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100509#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200510 case PSA_KEY_TYPE_AES:
511 if( bits != 128 && bits != 192 && bits != 256 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
513 break;
514#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100515#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200516 case PSA_KEY_TYPE_CAMELLIA:
517 if( bits != 128 && bits != 192 && bits != 256 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100521#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 case PSA_KEY_TYPE_DES:
523 if( bits != 64 && bits != 128 && bits != 192 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100527#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528 case PSA_KEY_TYPE_ARC4:
529 if( bits < 8 || bits > 2048 )
530 return( PSA_ERROR_INVALID_ARGUMENT );
531 break;
532#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100533#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200534 case PSA_KEY_TYPE_CHACHA20:
535 if( bits != 256 )
536 return( PSA_ERROR_INVALID_ARGUMENT );
537 break;
538#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539 default:
540 return( PSA_ERROR_NOT_SUPPORTED );
541 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200542 if( bits % 8 != 0 )
543 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200545 return( PSA_SUCCESS );
546}
547
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100548/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 *
Steven Cooremancd640932021-03-08 12:00:27 +0100550 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
551 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100552 *
553 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 * \param[in] key_type The key type of the key to be used with the
555 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100556 *
557 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100558 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100559 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100560 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100561 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100562MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100563 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100564 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100565{
Steven Cooremancd640932021-03-08 12:00:27 +0100566 if( PSA_ALG_IS_HMAC( algorithm ) )
567 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100568 if( key_type == PSA_KEY_TYPE_HMAC )
569 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100570 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100571
572 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100573 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100574 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
575 * key. */
576 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
577 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
578 {
579 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
580 * the block length (larger than 1) for block ciphers. */
581 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
582 return( PSA_SUCCESS );
583 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100584 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100585
586 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100587}
588
Steven Cooreman75b74362020-07-28 14:30:13 +0200589/** Try to allocate a buffer to an empty key slot.
590 *
591 * \param[in,out] slot Key slot to attach buffer to.
592 * \param[in] buffer_length Requested size of the buffer.
593 *
594 * \retval #PSA_SUCCESS
595 * The buffer has been successfully allocated.
596 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
597 * Not enough memory was available for allocation.
598 * \retval #PSA_ERROR_ALREADY_EXISTS
599 * Trying to allocate a buffer to a non-empty key slot.
600 */
601static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
602 size_t buffer_length )
603{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100604 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200605 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200606
Ronald Cronea0f8a62020-11-25 17:52:23 +0100607 slot->key.data = mbedtls_calloc( 1, buffer_length );
608 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200609 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200610
Ronald Cronea0f8a62020-11-25 17:52:23 +0100611 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200612 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200613}
614
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200615psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
616 const uint8_t* data,
617 size_t data_length )
618{
619 psa_status_t status = psa_allocate_buffer_to_slot( slot,
620 data_length );
621 if( status != PSA_SUCCESS )
622 return( status );
623
Ronald Cronea0f8a62020-11-25 17:52:23 +0100624 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200625 return( PSA_SUCCESS );
626}
627
Ronald Crona0fe59f2020-11-29 11:14:53 +0100628psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100629 const psa_key_attributes_t *attributes,
630 const uint8_t *data, size_t data_length,
631 uint8_t *key_buffer, size_t key_buffer_size,
632 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100633{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100634 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
635 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100636
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200637 /* zero-length keys are never supported. */
638 if( data_length == 0 )
639 return( PSA_ERROR_NOT_SUPPORTED );
640
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100641 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100642 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100643 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200644
Steven Cooreman75b74362020-07-28 14:30:13 +0200645 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
646 if( data_length > SIZE_MAX / 8 )
647 return( PSA_ERROR_NOT_SUPPORTED );
648
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200649 /* Enforce a size limit, and in particular ensure that the bit
650 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100651 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200652 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200653
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100654 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200655 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200656 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200657
Ronald Crondd04d422020-11-28 15:14:42 +0100658 /* Copy the key material. */
659 memcpy( key_buffer, data, data_length );
660 *key_buffer_length = data_length;
661 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200662
Steven Cooreman40120f62020-10-29 11:42:22 +0100663 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100664 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100665 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200666 {
John Durkop9814fa22020-11-04 12:28:15 -0800667#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
668 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100669 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200670 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100671 return( mbedtls_psa_ecp_import_key( attributes,
672 data, data_length,
673 key_buffer, key_buffer_size,
674 key_buffer_length,
675 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100676 }
John Durkop9814fa22020-11-04 12:28:15 -0800677#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
678 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700679#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
680 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100681 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200682 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100683 return( mbedtls_psa_rsa_import_key( attributes,
684 data, data_length,
685 key_buffer, key_buffer_size,
686 key_buffer_length,
687 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200688 }
John Durkop9814fa22020-11-04 12:28:15 -0800689#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
690 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100691 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100692
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100693 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100694}
695
Gilles Peskinef603c712019-01-19 13:40:11 +0100696/** Calculate the intersection of two algorithm usage policies.
697 *
698 * Return 0 (which allows no operation) on incompatibility.
699 */
700static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100701 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100702 psa_algorithm_t alg1,
703 psa_algorithm_t alg2 )
704{
Gilles Peskine549ea862019-05-22 11:45:59 +0200705 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100706 if( alg1 == alg2 )
707 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100708 /* If the policies are from the same hash-and-sign family, check
709 * if one is a wildcard. If so the other has the specific algorithm. */
710 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
711 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
712 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100713 {
Steven Cooreman03488022021-02-18 12:07:20 +0100714 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
715 return( alg2 );
716 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
717 return( alg1 );
718 }
719 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100720 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100721 * restrictive tag length. */
722 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
723 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
724 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
725 {
726 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
727 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100728 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100729
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100730 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100731 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
732 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100733 {
Steven Cooremancd640932021-03-08 12:00:27 +0100734 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
735 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100736 }
737 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100738 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100739 ( alg1_len <= alg2_len ) )
740 {
741 return( alg2 );
742 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100743 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100744 ( alg2_len <= alg1_len ) )
745 {
746 return( alg1 );
747 }
748 }
749 /* If the policies are from the same MAC family, check whether one
750 * of them is a minimum-MAC-length policy. Calculate the most
751 * restrictive tag length. */
752 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
753 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
754 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
755 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100756 /* Validate the combination of key type and algorithm. Since the base
757 * algorithm of alg1 and alg2 are the same, we only need this once. */
758 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100759 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100760
Steven Cooreman31a876d2021-03-03 20:47:40 +0100761 /* Get the (exact or at-least) output lengths for both sides of the
762 * requested intersection. None of the currently supported algorithms
763 * have an output length dependent on the actual key size, so setting it
764 * to a bogus value of 0 is currently OK.
765 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100766 * Note that for at-least-this-length wildcard algorithms, the output
767 * length is set to the shortest allowed length, which allows us to
768 * calculate the most restrictive tag length for the intersection. */
769 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
770 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100771 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100772
Steven Cooremana1d83222021-02-25 10:20:29 +0100773 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100774 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
775 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100776 {
Steven Cooremancd640932021-03-08 12:00:27 +0100777 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100778 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100779
780 /* If only one is an at-least-this-length policy, the intersection would
781 * be the other (fixed-length) policy as long as said fixed length is
782 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100783 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100784 {
Steven Cooremancd640932021-03-08 12:00:27 +0100785 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100786 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100787 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100788 {
Steven Cooremancd640932021-03-08 12:00:27 +0100789 return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100790 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100791
Steven Cooremancd640932021-03-08 12:00:27 +0100792 /* If none of them are wildcards, check whether they define the same tag
793 * length. This is still possible here when one is default-length and
794 * the other specific-length. Ensure to always return the
795 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100796 if( alg1_len == alg2_len )
797 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100798 }
799 /* If the policies are incompatible, allow nothing. */
800 return( 0 );
801}
802
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100803static int psa_key_algorithm_permits( psa_key_type_t key_type,
804 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200805 psa_algorithm_t requested_alg )
806{
Gilles Peskine549ea862019-05-22 11:45:59 +0200807 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200808 if( requested_alg == policy_alg )
809 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100810 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
811 * and requested_alg is the same hash-and-sign family with any hash,
812 * then requested_alg is compliant with policy_alg. */
813 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
814 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200815 {
Steven Cooreman03488022021-02-18 12:07:20 +0100816 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
817 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
818 }
819 /* If policy_alg is a wildcard AEAD algorithm of the same base as
820 * the requested algorithm, check the requested tag length to be
821 * equal-length or longer than the wildcard-specified length. */
822 if( PSA_ALG_IS_AEAD( policy_alg ) &&
823 PSA_ALG_IS_AEAD( requested_alg ) &&
824 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
825 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100826 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100827 {
828 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
829 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
830 }
Steven Cooremancd640932021-03-08 12:00:27 +0100831 /* If policy_alg is a MAC algorithm of the same base as the requested
832 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100833 if( PSA_ALG_IS_MAC( policy_alg ) &&
834 PSA_ALG_IS_MAC( requested_alg ) &&
835 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100836 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100837 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100838 /* Validate the combination of key type and algorithm. Since the policy
839 * and requested algorithms are the same, we only need this once. */
840 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100841 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100842
Steven Cooreman31a876d2021-03-03 20:47:40 +0100843 /* Get both the requested output length for the algorithm which is to be
844 * verified, and the default output length for the base algorithm.
845 * Note that none of the currently supported algorithms have an output
846 * length dependent on actual key size, so setting it to a bogus value
847 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100848 size_t requested_output_length = PSA_MAC_LENGTH(
849 key_type, 0, requested_alg );
850 size_t default_output_length = PSA_MAC_LENGTH(
851 key_type, 0,
852 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100853
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100854 /* If the policy is default-length, only allow an algorithm with
855 * a declared exact-length matching the default. */
856 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100857 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100858
859 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100860 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100861 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
862 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
863 {
864 return( 1 );
865 }
866
Steven Cooremancd640932021-03-08 12:00:27 +0100867 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
868 * check for the requested MAC length to be equal to or longer than the
869 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100870 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
871 {
872 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100873 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100874 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200875 }
Steven Cooremance48e852020-10-05 16:02:45 +0200876 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200877 * a key derivation with that key agreement should also be allowed. This
878 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200879 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
880 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
881 {
882 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
883 policy_alg );
884 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100885 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200886 return( 0 );
887}
888
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100889/** Test whether a policy permits an algorithm.
890 *
891 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100892 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100893 * \note This function requires providing the key type for which the policy is
894 * being validated, since some algorithm policy definitions (e.g. MAC)
895 * have different properties depending on what kind of cipher it is
896 * combined with.
897 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100898 * \retval PSA_SUCCESS When \p alg is a specific algorithm
899 * allowed by the \p policy.
900 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
901 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
902 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100903 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100904static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100905 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100906 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100907{
Steven Cooremand788fab2021-02-25 11:29:17 +0100908 /* '0' is not a valid algorithm */
909 if( alg == 0 )
910 return( PSA_ERROR_INVALID_ARGUMENT );
911
Steven Cooreman7e39f052021-02-23 14:18:32 +0100912 /* A requested algorithm cannot be a wildcard. */
913 if( PSA_ALG_IS_WILDCARD( alg ) )
914 return( PSA_ERROR_INVALID_ARGUMENT );
915
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100916 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
917 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100918 return( PSA_SUCCESS );
919 else
920 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100921}
922
Gilles Peskinef603c712019-01-19 13:40:11 +0100923/** Restrict a key policy based on a constraint.
924 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100925 * \note This function requires providing the key type for which the policy is
926 * being restricted, since some algorithm policy definitions (e.g. MAC)
927 * have different properties depending on what kind of cipher it is
928 * combined with.
929 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100930 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100931 * \param[in,out] policy The policy to restrict.
932 * \param[in] constraint The policy constraint to apply.
933 *
934 * \retval #PSA_SUCCESS
935 * \c *policy contains the intersection of the original value of
936 * \c *policy and \c *constraint.
937 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100938 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100939 * \c *policy is unchanged.
940 */
941static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100942 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100943 psa_key_policy_t *policy,
944 const psa_key_policy_t *constraint )
945{
946 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100947 psa_key_policy_algorithm_intersection( key_type, policy->alg,
948 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200949 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100950 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
951 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100952 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
953 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200954 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
955 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100956 policy->usage &= constraint->usage;
957 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200958 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100959 return( PSA_SUCCESS );
960}
961
Ronald Cron5c522922020-11-14 16:35:34 +0100962/** Get the description of a key given its identifier and policy constraints
963 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200964 *
Ronald Cron5c522922020-11-14 16:35:34 +0100965 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100966 * nonzero, the key must allow operations with this algorithm. If \p alg is
967 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100968 *
Ronald Cron5c522922020-11-14 16:35:34 +0100969 * In case of a persistent key, the function loads the description of the key
970 * into a key slot if not already done.
971 *
972 * On success, the returned key slot is locked. It is the responsibility of
973 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200974 */
Ronald Cron5c522922020-11-14 16:35:34 +0100975static psa_status_t psa_get_and_lock_key_slot_with_policy(
976 mbedtls_svc_key_id_t key,
977 psa_key_slot_t **p_slot,
978 psa_key_usage_t usage,
979 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100980{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200981 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
982 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100983
Ronald Cron5c522922020-11-14 16:35:34 +0100984 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100985 if( status != PSA_SUCCESS )
986 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200987 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100988
989 /* Enforce that usage policy for the key slot contains all the flags
990 * required by the usage parameter. There is one exception: public
991 * keys can always be exported, so we treat public key objects as
992 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200993 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100994 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200995
Gilles Peskine8e338702019-07-30 20:06:31 +0200996 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100997 {
998 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200999 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001000 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001001
1002 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +01001003 if( alg != 0 )
1004 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001005 status = psa_key_policy_permits( &slot->attr.policy,
1006 slot->attr.type,
1007 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +01001008 if( status != PSA_SUCCESS )
1009 goto error;
1010 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001011
Darryl Green06fd18d2018-07-16 11:21:11 +01001012 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001013
1014error:
1015 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001016 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001017
1018 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001019}
Darryl Green940d72c2018-07-13 13:18:51 +01001020
Ronald Cron5c522922020-11-14 16:35:34 +01001021/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001022 *
1023 * A transparent key is a key for which the key material is directly
1024 * available, as opposed to a key in a secure element.
1025 *
Ronald Cron5c522922020-11-14 16:35:34 +01001026 * This is a temporary function to use instead of
1027 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1028 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001029 *
Ronald Cron5c522922020-11-14 16:35:34 +01001030 * On success, the returned key slot is locked. It is the responsibility of the
1031 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001032 */
1033#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001034static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1035 mbedtls_svc_key_id_t key,
1036 psa_key_slot_t **p_slot,
1037 psa_key_usage_t usage,
1038 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001039{
Ronald Cron5c522922020-11-14 16:35:34 +01001040 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1041 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001042 if( status != PSA_SUCCESS )
1043 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001044
Gilles Peskineadad8132019-07-25 11:31:23 +02001045 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001046 {
Ronald Cron5c522922020-11-14 16:35:34 +01001047 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001048 *p_slot = NULL;
1049 return( PSA_ERROR_NOT_SUPPORTED );
1050 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001051
Gilles Peskine28f8f302019-07-24 13:30:31 +02001052 return( PSA_SUCCESS );
1053}
1054#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1055/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001056#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1057 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001058#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1059
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001060/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001061static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001062{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001063 /* Data pointer will always be either a valid pointer or NULL in an
1064 * initialized slot, so we can just free it. */
1065 if( slot->key.data != NULL )
1066 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1067
1068 mbedtls_free( slot->key.data );
1069 slot->key.data = NULL;
1070 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001071
1072 return( PSA_SUCCESS );
1073}
1074
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001075/** Completely wipe a slot in memory, including its policy.
1076 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001077psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001078{
1079 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001080
1081 /*
1082 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001083 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001084 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1085 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001086 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001087 */
Ronald Cron5c522922020-11-14 16:35:34 +01001088 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001089 {
1090#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001091 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001092#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001093 status = PSA_ERROR_CORRUPTION_DETECTED;
1094 }
1095
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001096 /* Multipart operations may still be using the key. This is safe
1097 * because all multipart operation objects are independent from
1098 * the key slot: if they need to access the key after the setup
1099 * phase, they have a copy of the key. Note that this means that
1100 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001101 /* At this point, key material and other type-specific content has
1102 * been wiped. Clear remaining metadata. We can call memset and not
1103 * zeroize because the metadata is not particularly sensitive. */
1104 memset( slot, 0, sizeof( *slot ) );
1105 return( status );
1106}
1107
Ronald Croncf56a0a2020-08-04 09:51:30 +02001108psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001109{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001110 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001111 psa_status_t status; /* status of the last operation */
1112 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001113#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1114 psa_se_drv_table_entry_t *driver;
1115#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001116
Ronald Croncf56a0a2020-08-04 09:51:30 +02001117 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001118 return( PSA_SUCCESS );
1119
Ronald Cronf2911112020-10-29 17:51:10 +01001120 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001121 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001122 * key, this will load the key description from persistent memory if not
1123 * done yet. We cannot avoid this loading as without it we don't know if
1124 * the key is operated by an SE or not and this information is needed by
1125 * the current implementation.
1126 */
Ronald Cron5c522922020-11-14 16:35:34 +01001127 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001128 if( status != PSA_SUCCESS )
1129 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001130
Ronald Cronf2911112020-10-29 17:51:10 +01001131 /*
1132 * If the key slot containing the key description is under access by the
1133 * library (apart from the present access), the key cannot be destroyed
1134 * yet. For the time being, just return in error. Eventually (to be
1135 * implemented), the key should be destroyed when all accesses have
1136 * stopped.
1137 */
Ronald Cron5c522922020-11-14 16:35:34 +01001138 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001139 {
Ronald Cron5c522922020-11-14 16:35:34 +01001140 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001141 return( PSA_ERROR_GENERIC_ERROR );
1142 }
1143
Gilles Peskine354f7672019-07-12 23:46:38 +02001144#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001145 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001146 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001147 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001148 /* For a key in a secure element, we need to do three things:
1149 * remove the key file in internal storage, destroy the
1150 * key inside the secure element, and update the driver's
1151 * persistent data. Start a transaction that will encompass these
1152 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001153 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001154 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001155 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001156 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001157 status = psa_crypto_save_transaction( );
1158 if( status != PSA_SUCCESS )
1159 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001160 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001161 /* We should still try to destroy the key in the secure
1162 * element and the key metadata in storage. This is especially
1163 * important if the error is that the storage is full.
1164 * But how to do it exactly without risking an inconsistent
1165 * state after a reset?
1166 * https://github.com/ARMmbed/mbed-crypto/issues/215
1167 */
1168 overall_status = status;
1169 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001170 }
1171
Ronald Cronea0f8a62020-11-25 17:52:23 +01001172 status = psa_destroy_se_key( driver,
1173 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001174 if( overall_status == PSA_SUCCESS )
1175 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001176 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001177#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1178
Darryl Greend49a4992018-06-18 17:27:26 +01001179#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001180 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001181 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001182 status = psa_destroy_persistent_key( slot->attr.id );
1183 if( overall_status == PSA_SUCCESS )
1184 overall_status = status;
1185
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001186 /* TODO: other slots may have a copy of the same key. We should
1187 * invalidate them.
1188 * https://github.com/ARMmbed/mbed-crypto/issues/214
1189 */
Darryl Greend49a4992018-06-18 17:27:26 +01001190 }
1191#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001192
Gilles Peskinefc762652019-07-22 19:30:34 +02001193#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1194 if( driver != NULL )
1195 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001196 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001197 if( overall_status == PSA_SUCCESS )
1198 overall_status = status;
1199 status = psa_crypto_stop_transaction( );
1200 if( overall_status == PSA_SUCCESS )
1201 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001202 }
1203#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1204
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001205#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1206exit:
1207#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001208 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001209 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1210 if( overall_status == PSA_SUCCESS )
1211 overall_status = status;
1212 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001213}
1214
John Durkop07cc04a2020-11-16 22:08:34 -08001215#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001216 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001217static psa_status_t psa_get_rsa_public_exponent(
1218 const mbedtls_rsa_context *rsa,
1219 psa_key_attributes_t *attributes )
1220{
1221 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 uint8_t *buffer = NULL;
1224 size_t buflen;
1225 mbedtls_mpi_init( &mpi );
1226
1227 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1228 if( ret != 0 )
1229 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001230 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1231 {
1232 /* It's the default value, which is reported as an empty string,
1233 * so there's nothing to do. */
1234 goto exit;
1235 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001236
1237 buflen = mbedtls_mpi_size( &mpi );
1238 buffer = mbedtls_calloc( 1, buflen );
1239 if( buffer == NULL )
1240 {
1241 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1242 goto exit;
1243 }
1244 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1245 if( ret != 0 )
1246 goto exit;
1247 attributes->domain_parameters = buffer;
1248 attributes->domain_parameters_size = buflen;
1249
1250exit:
1251 mbedtls_mpi_free( &mpi );
1252 if( ret != 0 )
1253 mbedtls_free( buffer );
1254 return( mbedtls_to_psa_error( ret ) );
1255}
John Durkop07cc04a2020-11-16 22:08:34 -08001256#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001257 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001258
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001259/** Retrieve all the publicly-accessible attributes of a key.
1260 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001261psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001262 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001263{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001264 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001265 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001266 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001267
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001268 psa_reset_key_attributes( attributes );
1269
Ronald Cron5c522922020-11-14 16:35:34 +01001270 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001271 if( status != PSA_SUCCESS )
1272 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001273
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001274 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001275 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1276 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1277
1278#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1279 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001280 psa_set_key_slot_number( attributes,
1281 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001282#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001283
Gilles Peskine8e338702019-07-30 20:06:31 +02001284 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001285 {
John Durkop07cc04a2020-11-16 22:08:34 -08001286#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001287 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001288 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001289 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001290#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001291 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001292 * is not yet implemented.
1293 * https://github.com/ARMmbed/mbed-crypto/issues/216
1294 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001295 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001296 break;
1297#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001298 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001299 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001300
Ronald Cron90857082020-11-25 14:58:33 +01001301 status = mbedtls_psa_rsa_load_representation(
1302 slot->attr.type,
1303 slot->key.data,
1304 slot->key.bytes,
1305 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001306 if( status != PSA_SUCCESS )
1307 break;
1308
Steven Cooremana2371e52020-07-28 14:30:39 +02001309 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001310 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001311 mbedtls_rsa_free( rsa );
1312 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001313 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001314 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001315#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001316 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001317 default:
1318 /* Nothing else to do. */
1319 break;
1320 }
1321
1322 if( status != PSA_SUCCESS )
1323 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001324
Ronald Cron5c522922020-11-14 16:35:34 +01001325 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001326
Ronald Cron5c522922020-11-14 16:35:34 +01001327 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001328}
1329
Gilles Peskinec8000c02019-08-02 20:15:51 +02001330#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1331psa_status_t psa_get_key_slot_number(
1332 const psa_key_attributes_t *attributes,
1333 psa_key_slot_number_t *slot_number )
1334{
1335 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1336 {
1337 *slot_number = attributes->slot_number;
1338 return( PSA_SUCCESS );
1339 }
1340 else
1341 return( PSA_ERROR_INVALID_ARGUMENT );
1342}
1343#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1344
Ronald Crond18b5f82020-11-25 16:46:05 +01001345static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1346 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001347 uint8_t *data,
1348 size_t data_size,
1349 size_t *data_length )
1350{
Ronald Crond18b5f82020-11-25 16:46:05 +01001351 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001352 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001353 memcpy( data, key_buffer, key_buffer_size );
1354 memset( data + key_buffer_size, 0,
1355 data_size - key_buffer_size );
1356 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001357 return( PSA_SUCCESS );
1358}
1359
Ronald Cron7285cda2020-11-26 14:46:37 +01001360psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001361 const psa_key_attributes_t *attributes,
1362 const uint8_t *key_buffer, size_t key_buffer_size,
1363 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001364{
Ronald Crond18b5f82020-11-25 16:46:05 +01001365 psa_key_type_t type = attributes->core.type;
1366
Ronald Crond18b5f82020-11-25 16:46:05 +01001367 if( key_type_is_raw_bytes( type ) ||
1368 PSA_KEY_TYPE_IS_RSA( type ) ||
1369 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001370 {
1371 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001372 key_buffer, key_buffer_size,
1373 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001374 }
1375 else
1376 {
1377 /* This shouldn't happen in the reference implementation, but
1378 it is valid for a special-purpose implementation to omit
1379 support for exporting certain key types. */
1380 return( PSA_ERROR_NOT_SUPPORTED );
1381 }
1382}
1383
1384psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1385 uint8_t *data,
1386 size_t data_size,
1387 size_t *data_length )
1388{
1389 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1390 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1391 psa_key_slot_t *slot;
1392
Ronald Crone907e552021-01-18 13:22:38 +01001393 /* Reject a zero-length output buffer now, since this can never be a
1394 * valid key representation. This way we know that data must be a valid
1395 * pointer and we can do things like memset(data, ..., data_size). */
1396 if( data_size == 0 )
1397 return( PSA_ERROR_BUFFER_TOO_SMALL );
1398
Ronald Cron9486f9d2020-11-26 13:36:23 +01001399 /* Set the key to empty now, so that even when there are errors, we always
1400 * set data_length to a value between 0 and data_size. On error, setting
1401 * the key to empty is a good choice because an empty key representation is
1402 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001403 *data_length = 0;
1404
Ronald Cron9486f9d2020-11-26 13:36:23 +01001405 /* Export requires the EXPORT flag. There is an exception for public keys,
1406 * which don't require any flag, but
1407 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1408 */
1409 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1410 PSA_KEY_USAGE_EXPORT, 0 );
1411 if( status != PSA_SUCCESS )
1412 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001413
Ronald Crond18b5f82020-11-25 16:46:05 +01001414 psa_key_attributes_t attributes = {
1415 .core = slot->attr
1416 };
Ronald Cron67227982020-11-26 15:16:05 +01001417 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001418 slot->key.data, slot->key.bytes,
1419 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001420
Ronald Cron9486f9d2020-11-26 13:36:23 +01001421 unlock_status = psa_unlock_key_slot( slot );
1422
1423 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1424}
1425
Ronald Cron7285cda2020-11-26 14:46:37 +01001426psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001427 const psa_key_attributes_t *attributes,
1428 const uint8_t *key_buffer,
1429 size_t key_buffer_size,
1430 uint8_t *data,
1431 size_t data_size,
1432 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001433{
Ronald Crond18b5f82020-11-25 16:46:05 +01001434 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001435
Ronald Crond18b5f82020-11-25 16:46:05 +01001436 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001437 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001438 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001439 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001440 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001441 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001442 key_buffer, key_buffer_size,
1443 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001444 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001445
Ronald Crond18b5f82020-11-25 16:46:05 +01001446 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001447 {
John Durkop0e005192020-10-31 22:06:54 -07001448#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1449 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001450 return( mbedtls_psa_rsa_export_public_key( attributes,
1451 key_buffer,
1452 key_buffer_size,
1453 data,
1454 data_size,
1455 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001456#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001457 /* We don't know how to convert a private RSA key to public. */
1458 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001459#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1460 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001461 }
1462 else
Moran Pekera998bc62018-04-16 18:16:20 +03001463 {
John Durkop6ba40d12020-11-10 08:50:04 -08001464#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1465 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001466 return( mbedtls_psa_ecp_export_public_key( attributes,
1467 key_buffer,
1468 key_buffer_size,
1469 data,
1470 data_size,
1471 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001472#else
1473 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001474 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001475#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1476 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001477 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001478 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001479 else
1480 {
1481 /* This shouldn't happen in the reference implementation, but
1482 it is valid for a special-purpose implementation to omit
1483 support for exporting certain key types. */
1484 return( PSA_ERROR_NOT_SUPPORTED );
1485 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001486}
Ronald Crond18b5f82020-11-25 16:46:05 +01001487
Ronald Croncf56a0a2020-08-04 09:51:30 +02001488psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001489 uint8_t *data,
1490 size_t data_size,
1491 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001492{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001493 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001494 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001495 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001496
Ronald Crone907e552021-01-18 13:22:38 +01001497 /* Reject a zero-length output buffer now, since this can never be a
1498 * valid key representation. This way we know that data must be a valid
1499 * pointer and we can do things like memset(data, ..., data_size). */
1500 if( data_size == 0 )
1501 return( PSA_ERROR_BUFFER_TOO_SMALL );
1502
Darryl Greendd8fb772018-11-07 16:00:44 +00001503 /* Set the key to empty now, so that even when there are errors, we always
1504 * set data_length to a value between 0 and data_size. On error, setting
1505 * the key to empty is a good choice because an empty key representation is
1506 * unlikely to be accepted anywhere. */
1507 *data_length = 0;
1508
1509 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001510 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001511 if( status != PSA_SUCCESS )
1512 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001513
Ronald Cron9486f9d2020-11-26 13:36:23 +01001514 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1515 {
1516 status = PSA_ERROR_INVALID_ARGUMENT;
1517 goto exit;
1518 }
1519
Ronald Crond18b5f82020-11-25 16:46:05 +01001520 psa_key_attributes_t attributes = {
1521 .core = slot->attr
1522 };
Ronald Cron67227982020-11-26 15:16:05 +01001523 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001524 &attributes, slot->key.data, slot->key.bytes,
1525 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001526
1527exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001528 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001529
Ronald Cron5c522922020-11-14 16:35:34 +01001530 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001531}
1532
Gilles Peskine91e8c332019-08-02 19:19:39 +02001533#if defined(static_assert)
1534static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1535 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001536static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001537 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001538static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001539 "One or more key attribute flag is listed as both internal-only and external-only" );
1540#endif
1541
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001542/** Validate that a key policy is internally well-formed.
1543 *
1544 * This function only rejects invalid policies. It does not validate the
1545 * consistency of the policy with respect to other attributes of the key
1546 * such as the key type.
1547 */
1548static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001549{
1550 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001551 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001552 PSA_KEY_USAGE_ENCRYPT |
1553 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001554 PSA_KEY_USAGE_SIGN_HASH |
1555 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001556 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1557 return( PSA_ERROR_INVALID_ARGUMENT );
1558
Gilles Peskine4747d192019-04-17 15:05:45 +02001559 return( PSA_SUCCESS );
1560}
1561
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001562/** Validate the internal consistency of key attributes.
1563 *
1564 * This function only rejects invalid attribute values. If does not
1565 * validate the consistency of the attributes with any key data that may
1566 * be involved in the creation of the key.
1567 *
1568 * Call this function early in the key creation process.
1569 *
1570 * \param[in] attributes Key attributes for the new key.
1571 * \param[out] p_drv On any return, the driver for the key, if any.
1572 * NULL for a transparent key.
1573 *
1574 */
1575static psa_status_t psa_validate_key_attributes(
1576 const psa_key_attributes_t *attributes,
1577 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001578{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001579 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001580 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001581 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001582
Ronald Cron54b90082020-10-29 15:26:43 +01001583 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001584 if( status != PSA_SUCCESS )
1585 return( status );
1586
Ronald Crond2ed4812020-07-17 16:11:30 +02001587 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001588 if( status != PSA_SUCCESS )
1589 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001590
Ronald Cron65f38a32020-10-23 17:11:13 +02001591 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1592 {
1593 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1594 return( PSA_ERROR_INVALID_ARGUMENT );
1595 }
1596 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001597 {
Ronald Cron77e412c2021-03-31 17:36:31 +02001598 if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) )
1599 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Crond2ed4812020-07-17 16:11:30 +02001600 }
1601
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001602 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001603 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001604 return( status );
1605
1606 /* Refuse to create overly large keys.
1607 * Note that this doesn't trigger on import if the attributes don't
1608 * explicitly specify a size (so psa_get_key_bits returns 0), so
1609 * psa_import_key() needs its own checks. */
1610 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1611 return( PSA_ERROR_NOT_SUPPORTED );
1612
Gilles Peskine91e8c332019-08-02 19:19:39 +02001613 /* Reject invalid flags. These should not be reachable through the API. */
1614 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1615 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1616 return( PSA_ERROR_INVALID_ARGUMENT );
1617
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001618 return( PSA_SUCCESS );
1619}
1620
Gilles Peskine4747d192019-04-17 15:05:45 +02001621/** Prepare a key slot to receive key material.
1622 *
1623 * This function allocates a key slot and sets its metadata.
1624 *
1625 * If this function fails, call psa_fail_key_creation().
1626 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001627 * This function is intended to be used as follows:
1628 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001629 * it with the specified attributes, and in case of a volatile key assign it
1630 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001631 * -# Populate the slot with the key material.
1632 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1633 * In case of failure at any step, stop the sequence and call
1634 * psa_fail_key_creation().
1635 *
Ronald Cron5c522922020-11-14 16:35:34 +01001636 * On success, the key slot is locked. It is the responsibility of the caller
1637 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001638 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001639 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001640 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001641 * \param[out] p_slot On success, a pointer to the prepared slot.
1642 * \param[out] p_drv On any return, the driver for the key, if any.
1643 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001644 *
1645 * \retval #PSA_SUCCESS
1646 * The key slot is ready to receive key material.
1647 * \return If this function fails, the key slot is an invalid state.
1648 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001649 */
1650static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001651 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001653 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001654 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001655{
1656 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001657 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001658 psa_key_slot_t *slot;
1659
Gilles Peskinedf179142019-07-15 22:02:14 +02001660 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001661 *p_drv = NULL;
1662
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001663 status = psa_validate_key_attributes( attributes, p_drv );
1664 if( status != PSA_SUCCESS )
1665 return( status );
1666
Ronald Cronc4d1b512020-07-31 11:26:37 +02001667 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001668 if( status != PSA_SUCCESS )
1669 return( status );
1670 slot = *p_slot;
1671
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001672 /* We're storing the declared bit-size of the key. It's up to each
1673 * creation mechanism to verify that this information is correct.
1674 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001675 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001676 * is optional (import, copy). In case of a volatile key, assign it the
1677 * volatile key identifier associated to the slot returned to contain its
1678 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001679
1680 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001681 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1682 {
1683#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1684 slot->attr.id = volatile_key_id;
1685#else
1686 slot->attr.id.key_id = volatile_key_id;
1687#endif
1688 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001689
Gilles Peskine91e8c332019-08-02 19:19:39 +02001690 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001691 * external-only flags, query `attributes`. Thanks to the check
1692 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001693 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001694 * may have set. */
1695 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001696
Gilles Peskinecbaff462019-07-12 23:46:04 +02001697#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001698 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001699 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001700 * create the key file in internal storage, create the
1701 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001702 * persistent data. This is done by starting a transaction that will
1703 * encompass these three actions.
1704 * For registering a volatile key, we just need to find an appropriate
1705 * slot number inside the SE. Since the key is designated volatile, creating
1706 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001707 /* The first thing to do is to find a slot number for the new key.
1708 * We save the slot number in persistent storage as part of the
1709 * transaction data. It will be needed to recover if the power
1710 * fails during the key creation process, to clean up on the secure
1711 * element side after restarting. Obtaining a slot number from the
1712 * secure element driver updates its persistent state, but we do not yet
1713 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001714 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001715 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001716 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001717 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001718 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001719 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001720 if( status != PSA_SUCCESS )
1721 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001722
1723 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001724 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001725 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1726 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001727 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001728 psa_crypto_transaction.key.id = slot->attr.id;
1729 status = psa_crypto_save_transaction( );
1730 if( status != PSA_SUCCESS )
1731 {
1732 (void) psa_crypto_stop_transaction( );
1733 return( status );
1734 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001735 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001736
1737 status = psa_copy_key_material_into_slot(
1738 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001739 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001740
1741 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1742 {
1743 /* Key registration only makes sense with a secure element. */
1744 return( PSA_ERROR_INVALID_ARGUMENT );
1745 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001746#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1747
Ronald Cronc4d1b512020-07-31 11:26:37 +02001748 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001749}
Gilles Peskine4747d192019-04-17 15:05:45 +02001750
1751/** Finalize the creation of a key once its key material has been set.
1752 *
1753 * This entails writing the key to persistent storage.
1754 *
1755 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001756 * See the documentation of psa_start_key_creation() for the intended use
1757 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 *
Ronald Cron5c522922020-11-14 16:35:34 +01001759 * If the finalization succeeds, the function unlocks the key slot (it was
1760 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1761 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001762 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001763 * \param[in,out] slot Pointer to the slot with key material.
1764 * \param[in] driver The secure element driver for the key,
1765 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001766 * \param[out] key On success, identifier of the key. Note that the
1767 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001768 *
1769 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001770 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001771 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1772 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1773 * \retval #PSA_ERROR_ALREADY_EXISTS
1774 * \retval #PSA_ERROR_DATA_INVALID
1775 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001776 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001777 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001778 * \return If this function fails, the key slot is an invalid state.
1779 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001780 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001781static psa_status_t psa_finish_key_creation(
1782 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001783 psa_se_drv_table_entry_t *driver,
1784 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001785{
1786 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001787 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001788 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001789
1790#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001791 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001792 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001793#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1794 if( driver != NULL )
1795 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001796 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001797 psa_key_slot_number_t slot_number =
1798 psa_key_slot_get_slot_number( slot ) ;
1799
Gilles Peskineb46bef22019-07-30 21:32:04 +02001800#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001801 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001802 sizeof( data.slot_number ),
1803 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001804#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001805 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001806 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001807 (uint8_t*) &data,
1808 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001809 }
1810 else
1811#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1812 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001813 /* Key material is saved in export representation in the slot, so
1814 * just pass the slot buffer for storage. */
1815 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001816 slot->key.data,
1817 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001818 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001819 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001820#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1821
Gilles Peskinecbaff462019-07-12 23:46:04 +02001822#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001823 /* Finish the transaction for a key creation. This does not
1824 * happen when registering an existing key. Detect this case
1825 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001826 * creation of a persistent key in a secure element requires a transaction,
1827 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001828 if( driver != NULL &&
1829 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001830 {
1831 status = psa_save_se_persistent_data( driver );
1832 if( status != PSA_SUCCESS )
1833 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001834 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001835 return( status );
1836 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001837 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001838 }
1839#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1840
Ronald Cron50972942020-11-14 11:28:25 +01001841 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001842 {
1843 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001844 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001845 if( status != PSA_SUCCESS )
1846 *key = MBEDTLS_SVC_KEY_ID_INIT;
1847 }
Ronald Cron50972942020-11-14 11:28:25 +01001848
Gilles Peskine4747d192019-04-17 15:05:45 +02001849 return( status );
1850}
1851
1852/** Abort the creation of a key.
1853 *
1854 * You may call this function after calling psa_start_key_creation(),
1855 * or after psa_finish_key_creation() fails. In other circumstances, this
1856 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001857 * See the documentation of psa_start_key_creation() for the intended use
1858 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001859 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001860 * \param[in,out] slot Pointer to the slot with key material.
1861 * \param[in] driver The secure element driver for the key,
1862 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001863 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001864static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001865 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001866{
Gilles Peskine011e4282019-06-26 18:34:38 +02001867 (void) driver;
1868
Gilles Peskine4747d192019-04-17 15:05:45 +02001869 if( slot == NULL )
1870 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001871
1872#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001873 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001874 * element, and the failure happened later (when saving metadata
1875 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001876 * element.
1877 * https://github.com/ARMmbed/mbed-crypto/issues/217
1878 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001879
Gilles Peskined7729582019-08-05 15:55:54 +02001880 /* Abort the ongoing transaction if any (there may not be one if
1881 * the creation process failed before starting one, or if the
1882 * key creation is a registration of a key in a secure element).
1883 * Earlier functions must already have done what it takes to undo any
1884 * partial creation. All that's left is to update the transaction data
1885 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001886 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001887#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1888
Gilles Peskine4747d192019-04-17 15:05:45 +02001889 psa_wipe_key_slot( slot );
1890}
1891
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001892/** Validate optional attributes during key creation.
1893 *
1894 * Some key attributes are optional during key creation. If they are
1895 * specified in the attributes structure, check that they are consistent
1896 * with the data in the slot.
1897 *
1898 * This function should be called near the end of key creation, after
1899 * the slot in memory is fully populated but before saving persistent data.
1900 */
1901static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001902 const psa_key_slot_t *slot,
1903 const psa_key_attributes_t *attributes )
1904{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001905 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001906 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001907 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001908 return( PSA_ERROR_INVALID_ARGUMENT );
1909 }
1910
1911 if( attributes->domain_parameters_size != 0 )
1912 {
John Durkop0e005192020-10-31 22:06:54 -07001913#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1914 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001915 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001916 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001917 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001918 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001919 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001920
Ronald Cron90857082020-11-25 14:58:33 +01001921 psa_status_t status = mbedtls_psa_rsa_load_representation(
1922 slot->attr.type,
1923 slot->key.data,
1924 slot->key.bytes,
1925 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001926 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001927 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001928
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001929 mbedtls_mpi_init( &actual );
1930 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001931 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001932 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001933 mbedtls_rsa_free( rsa );
1934 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001935 if( ret != 0 )
1936 goto rsa_exit;
1937 ret = mbedtls_mpi_read_binary( &required,
1938 attributes->domain_parameters,
1939 attributes->domain_parameters_size );
1940 if( ret != 0 )
1941 goto rsa_exit;
1942 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1943 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1944 rsa_exit:
1945 mbedtls_mpi_free( &actual );
1946 mbedtls_mpi_free( &required );
1947 if( ret != 0)
1948 return( mbedtls_to_psa_error( ret ) );
1949 }
1950 else
John Durkop9814fa22020-11-04 12:28:15 -08001951#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1952 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953 {
1954 return( PSA_ERROR_INVALID_ARGUMENT );
1955 }
1956 }
1957
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001958 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001959 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001960 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001961 return( PSA_ERROR_INVALID_ARGUMENT );
1962 }
1963
1964 return( PSA_SUCCESS );
1965}
1966
Gilles Peskine4747d192019-04-17 15:05:45 +02001967psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001968 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001969 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001970 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001971{
1972 psa_status_t status;
1973 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001974 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001975 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001976
Ronald Cron81709fc2020-11-14 12:10:32 +01001977 *key = MBEDTLS_SVC_KEY_ID_INIT;
1978
Gilles Peskine0f84d622019-09-12 19:03:13 +02001979 /* Reject zero-length symmetric keys (including raw data key objects).
1980 * This also rejects any key which might be encoded as an empty string,
1981 * which is never valid. */
1982 if( data_length == 0 )
1983 return( PSA_ERROR_INVALID_ARGUMENT );
1984
Gilles Peskinedf179142019-07-15 22:02:14 +02001985 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001986 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001987 if( status != PSA_SUCCESS )
1988 goto exit;
1989
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001990 /* In the case of a transparent key or an opaque key stored in local
1991 * storage (thus not in the case of generating a key in a secure element
1992 * or cryptoprocessor with storage), we have to allocate a buffer to
1993 * hold the generated key material. */
1994 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001995 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001996 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001997 if( status != PSA_SUCCESS )
1998 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001999 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002000
2001 bits = slot->attr.bits;
2002 status = psa_driver_wrapper_import_key( attributes,
2003 data, data_length,
2004 slot->key.data,
2005 slot->key.bytes,
2006 &slot->key.bytes, &bits );
2007 if( status != PSA_SUCCESS )
2008 goto exit;
2009
2010 if( slot->attr.bits == 0 )
2011 slot->attr.bits = (psa_key_bits_t) bits;
2012 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01002013 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002014 status = PSA_ERROR_INVALID_ARGUMENT;
2015 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002016 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002017
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002018 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002019 if( status != PSA_SUCCESS )
2020 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002021
Ronald Cron81709fc2020-11-14 12:10:32 +01002022 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002023exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002024 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002025 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002026
Gilles Peskine4747d192019-04-17 15:05:45 +02002027 return( status );
2028}
2029
Gilles Peskined7729582019-08-05 15:55:54 +02002030#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2031psa_status_t mbedtls_psa_register_se_key(
2032 const psa_key_attributes_t *attributes )
2033{
2034 psa_status_t status;
2035 psa_key_slot_t *slot = NULL;
2036 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002037 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002038
2039 /* Leaving attributes unspecified is not currently supported.
2040 * It could make sense to query the key type and size from the
2041 * secure element, but not all secure elements support this
2042 * and the driver HAL doesn't currently support it. */
2043 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2044 return( PSA_ERROR_NOT_SUPPORTED );
2045 if( psa_get_key_bits( attributes ) == 0 )
2046 return( PSA_ERROR_NOT_SUPPORTED );
2047
2048 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002049 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002050 if( status != PSA_SUCCESS )
2051 goto exit;
2052
Ronald Cron81709fc2020-11-14 12:10:32 +01002053 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002054
2055exit:
2056 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002057 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002058
Gilles Peskined7729582019-08-05 15:55:54 +02002059 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002060 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002061 return( status );
2062}
2063#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2064
Gilles Peskinef603c712019-01-19 13:40:11 +01002065static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002066 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002067{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002068 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002069 source->key.data,
2070 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002071 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002072 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002073
Steven Cooreman398aee52020-10-13 14:35:45 +02002074 target->attr.type = source->attr.type;
2075 target->attr.bits = source->attr.bits;
2076
2077 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002078}
2079
Ronald Croncf56a0a2020-08-04 09:51:30 +02002080psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002081 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002082 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002083{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002084 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002085 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002086 psa_key_slot_t *source_slot = NULL;
2087 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002088 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002089 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002090
Ronald Cron81709fc2020-11-14 12:10:32 +01002091 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2092
Ronald Cron5c522922020-11-14 16:35:34 +01002093 status = psa_get_and_lock_transparent_key_slot_with_policy(
2094 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002095 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002096 goto exit;
2097
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002098 status = psa_validate_optional_attributes( source_slot,
2099 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002100 if( status != PSA_SUCCESS )
2101 goto exit;
2102
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002103 status = psa_restrict_key_policy( source_slot->attr.type,
2104 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002105 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002106 if( status != PSA_SUCCESS )
2107 goto exit;
2108
Ronald Cron81709fc2020-11-14 12:10:32 +01002109 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2110 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002111 if( status != PSA_SUCCESS )
2112 goto exit;
2113
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002114#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2115 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002116 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002117 /* Copying to a secure element is not implemented yet. */
2118 status = PSA_ERROR_NOT_SUPPORTED;
2119 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002120 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002121#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002122
Ronald Cron6cc66312021-04-02 12:27:47 +02002123 if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) )
2124 {
2125 /*
2126 * Copying through an opaque driver is not implemented yet, consider
2127 * a lifetime with an external location as an invalid parameter for
2128 * now.
2129 */
2130 status = PSA_ERROR_INVALID_ARGUMENT;
2131 goto exit;
2132 }
2133
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002134 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002135 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002136 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002137
Ronald Cron81709fc2020-11-14 12:10:32 +01002138 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002139exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002140 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002141 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002142
Ronald Cron5c522922020-11-14 16:35:34 +01002143 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002144
Ronald Cron5c522922020-11-14 16:35:34 +01002145 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002146}
2147
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002148
2149
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002150/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002151/* Message digests */
2152/****************************************************************/
2153
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002154psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2155{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002156 /* Aborting a non-active operation is allowed */
2157 if( operation->id == 0 )
2158 return( PSA_SUCCESS );
2159
2160 psa_status_t status = psa_driver_wrapper_hash_abort( operation );
2161 operation->id = 0;
2162
2163 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002164}
2165
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002166psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002167 psa_algorithm_t alg )
2168{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002169 /* A context must be freshly initialized before it can be set up. */
2170 if( operation->id != 0 )
2171 return( PSA_ERROR_BAD_STATE );
2172
2173 if( !PSA_ALG_IS_HASH( alg ) )
Steven Cooreman0e307642021-02-18 16:18:32 +01002174 return( PSA_ERROR_INVALID_ARGUMENT );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002175
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002176 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2177 * directly zeroes the int-sized dummy member of the context union. */
Steven Cooreman893232f2021-03-15 12:23:37 +01002178 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
2179
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002180 return( psa_driver_wrapper_hash_setup( operation, alg ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002181}
2182
2183psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2184 const uint8_t *input,
2185 size_t input_length )
2186{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002187 if( operation->id == 0 )
2188 return( PSA_ERROR_BAD_STATE );
Gilles Peskine94e44542018-07-12 16:58:43 +02002189
Steven Cooremanc8288352021-03-04 14:02:19 +01002190 /* Don't require hash implementations to behave correctly on a
2191 * zero-length input, which may have an invalid pointer. */
2192 if( input_length == 0 )
2193 return( PSA_SUCCESS );
2194
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002195 psa_status_t status = psa_driver_wrapper_hash_update( operation,
2196 input, input_length );
2197 if( status != PSA_SUCCESS )
2198 psa_hash_abort( operation );
2199
2200 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002201}
2202
2203psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2204 uint8_t *hash,
2205 size_t hash_size,
2206 size_t *hash_length )
2207{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002208 *hash_length = 0;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002209 if( operation->id == 0 )
2210 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002211
Steven Cooreman1e582352021-02-18 17:24:37 +01002212 psa_status_t status = psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002213 operation, hash, hash_size, hash_length );
Steven Cooreman0e307642021-02-18 16:18:32 +01002214 psa_hash_abort( operation );
2215 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216}
2217
Gilles Peskine2d277862018-06-18 15:41:12 +02002218psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2219 const uint8_t *hash,
2220 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221{
2222 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2223 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002224 psa_status_t status = psa_hash_finish(
2225 operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01002226 actual_hash, sizeof( actual_hash ),
2227 &actual_hash_length );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002228 if( status != PSA_SUCCESS )
2229 return( status );
2230 if( actual_hash_length != hash_length )
2231 return( PSA_ERROR_INVALID_SIGNATURE );
2232 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2233 return( PSA_ERROR_INVALID_SIGNATURE );
2234 return( PSA_SUCCESS );
2235}
2236
Gilles Peskine0a749c82019-11-28 19:33:58 +01002237psa_status_t psa_hash_compute( psa_algorithm_t alg,
2238 const uint8_t *input, size_t input_length,
2239 uint8_t *hash, size_t hash_size,
2240 size_t *hash_length )
2241{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002242 *hash_length = 0;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002243 if( !PSA_ALG_IS_HASH( alg ) )
2244 return( PSA_ERROR_INVALID_ARGUMENT );
2245
Steven Cooreman1e582352021-02-18 17:24:37 +01002246 return( psa_driver_wrapper_hash_compute( alg, input, input_length,
2247 hash, hash_size, hash_length ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002248}
2249
2250psa_status_t psa_hash_compare( psa_algorithm_t alg,
2251 const uint8_t *input, size_t input_length,
2252 const uint8_t *hash, size_t hash_length )
2253{
Steven Cooreman84d670d2021-02-18 16:22:53 +01002254 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2255 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002256
2257 if( !PSA_ALG_IS_HASH( alg ) )
2258 return( PSA_ERROR_INVALID_ARGUMENT );
2259
Steven Cooreman1e582352021-02-18 17:24:37 +01002260 psa_status_t status = psa_driver_wrapper_hash_compute(
2261 alg, input, input_length,
2262 actual_hash, sizeof(actual_hash),
2263 &actual_hash_length );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002264 if( status != PSA_SUCCESS )
Steven Cooreman84d670d2021-02-18 16:22:53 +01002265 return( status );
2266 if( actual_hash_length != hash_length )
2267 return( PSA_ERROR_INVALID_SIGNATURE );
2268 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2269 return( PSA_ERROR_INVALID_SIGNATURE );
2270 return( PSA_SUCCESS );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002271}
2272
Gilles Peskineeb35d782019-01-22 17:56:16 +01002273psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2274 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002275{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002276 if( source_operation->id == 0 ||
2277 target_operation->id != 0 )
2278 {
2279 return( PSA_ERROR_BAD_STATE );
2280 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002281
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002282 psa_status_t status = psa_driver_wrapper_hash_clone( source_operation,
2283 target_operation );
2284 if( status != PSA_SUCCESS )
2285 psa_hash_abort( target_operation );
2286
2287 return( status );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002288}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002289
2290
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002291/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002292/* MAC */
2293/****************************************************************/
2294
John Durkop6ba40d12020-11-10 08:50:04 -08002295#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002296static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002297{
Gilles Peskine2d277862018-06-18 15:41:12 +02002298 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002299 {
2300 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002301 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002302 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002303 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002304 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002305 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002306 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002307 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002308 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002309 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002310 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002311 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002312 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002313 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002314 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002315 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002316 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002317 return( 128 );
2318 default:
2319 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002320 }
2321}
John Durkop07cc04a2020-11-16 22:08:34 -08002322#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002323
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002324/* Initialize the MAC operation structure. Once this function has been
2325 * called, psa_mac_abort can run and will do the right thing. */
2326static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2327 psa_algorithm_t alg )
2328{
2329 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2330
Steven Cooreman15472f82021-03-02 16:16:22 +01002331 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002332 operation->key_set = 0;
2333 operation->iv_set = 0;
2334 operation->iv_required = 0;
2335 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002336 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002337
Ronald Cron3d471812021-03-18 13:40:31 +01002338#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01002339 if( operation->alg == PSA_ALG_CMAC )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340 {
2341 operation->iv_required = 0;
2342 mbedtls_cipher_init( &operation->ctx.cmac );
2343 status = PSA_SUCCESS;
2344 }
2345 else
Ronald Cron3d471812021-03-18 13:40:31 +01002346#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
John Durkopd0321952020-10-29 21:37:36 -07002347#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002348 if( PSA_ALG_IS_HMAC( operation->alg ) )
2349 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002350 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
Steven Cooreman0e307642021-02-18 16:18:32 +01002351 operation->ctx.hmac.alg = 0;
Gilles Peskineff94abd2018-07-12 17:07:52 +02002352 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002353 }
2354 else
John Durkopd0321952020-10-29 21:37:36 -07002355#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002356 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002357 if( ! PSA_ALG_IS_MAC( alg ) )
2358 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002359 }
2360
2361 if( status != PSA_SUCCESS )
2362 memset( operation, 0, sizeof( *operation ) );
2363 return( status );
2364}
2365
John Durkop6ba40d12020-11-10 08:50:04 -08002366#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002367static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2368{
Gilles Peskine3f108122018-12-07 18:14:53 +01002369 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002370 return( psa_hash_abort( &hmac->hash_ctx ) );
2371}
John Durkop6ba40d12020-11-10 08:50:04 -08002372#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002373
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2375{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002376 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002377 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002378 /* The object has (apparently) been initialized but it is not
2379 * in use. It's ok to call abort on such an object, and there's
2380 * nothing to do. */
2381 return( PSA_SUCCESS );
2382 }
2383 else
Ronald Cron3d471812021-03-18 13:40:31 +01002384#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002385 if( operation->alg == PSA_ALG_CMAC )
2386 {
2387 mbedtls_cipher_free( &operation->ctx.cmac );
2388 }
2389 else
Ronald Cron3d471812021-03-18 13:40:31 +01002390#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
John Durkopd0321952020-10-29 21:37:36 -07002391#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002392 if( PSA_ALG_IS_HMAC( operation->alg ) )
2393 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002394 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002395 }
2396 else
John Durkopd0321952020-10-29 21:37:36 -07002397#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002398 {
2399 /* Sanity check (shouldn't happen: operation->alg should
2400 * always have been initialized to a valid value). */
2401 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002402 }
Moran Peker41deec42018-04-04 15:43:05 +03002403
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404 operation->alg = 0;
2405 operation->key_set = 0;
2406 operation->iv_set = 0;
2407 operation->iv_required = 0;
2408 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002409 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002410
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002412
2413bad_state:
2414 /* If abort is called on an uninitialized object, we can't trust
2415 * anything. Wipe the object in case it contains confidential data.
2416 * This may result in a memory leak if a pointer gets overwritten,
2417 * but it's too late to do anything about this. */
2418 memset( operation, 0, sizeof( *operation ) );
2419 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002420}
2421
Ronald Cron3d471812021-03-18 13:40:31 +01002422#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01002423static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation,
2424 psa_key_slot_t *slot )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002425{
Janos Follath24eed8d2019-11-22 13:21:35 +00002426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002427 const mbedtls_cipher_info_t *cipher_info =
2428 mbedtls_cipher_info_from_psa( PSA_ALG_CMAC,
2429 slot->attr.type, slot->attr.bits,
2430 NULL );
2431 if( cipher_info == NULL )
2432 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002433
2434 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2435 if( ret != 0 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002436 goto exit;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002437
2438 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002439 slot->key.data,
Steven Cooreman15472f82021-03-02 16:16:22 +01002440 slot->attr.bits );
2441exit:
2442 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002443}
Ronald Cron3d471812021-03-18 13:40:31 +01002444#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002445
John Durkop6ba40d12020-11-10 08:50:04 -08002446#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002447static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2448 const uint8_t *key,
2449 size_t key_length,
2450 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002451{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002452 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002453 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002454 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002455 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002456 psa_status_t status;
2457
Steven Cooreman0e307642021-02-18 16:18:32 +01002458 hmac->alg = hash_alg;
2459
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002460 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2461 * overflow below. This should never trigger if the hash algorithm
2462 * is implemented correctly. */
2463 /* The size checks against the ipad and opad buffers cannot be written
2464 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2465 * because that triggers -Wlogical-op on GCC 7.3. */
2466 if( block_size > sizeof( ipad ) )
2467 return( PSA_ERROR_NOT_SUPPORTED );
2468 if( block_size > sizeof( hmac->opad ) )
2469 return( PSA_ERROR_NOT_SUPPORTED );
2470 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002471 return( PSA_ERROR_NOT_SUPPORTED );
2472
Gilles Peskined223b522018-06-11 18:12:58 +02002473 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002474 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002475 status = psa_hash_compute( hash_alg, key, key_length,
2476 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002477 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002478 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002479 }
Gilles Peskine96889972018-07-12 17:07:03 +02002480 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2481 * but it is permitted. It is common when HMAC is used in HKDF, for
2482 * example. Don't call `memcpy` in the 0-length because `key` could be
2483 * an invalid pointer which would make the behavior undefined. */
2484 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002485 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002486
Gilles Peskined223b522018-06-11 18:12:58 +02002487 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2488 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002489 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002490 ipad[i] ^= 0x36;
2491 memset( ipad + key_length, 0x36, block_size - key_length );
2492
2493 /* Copy the key material from ipad to opad, flipping the requisite bits,
2494 * and filling the rest of opad with the requisite constant. */
2495 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002496 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2497 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002498
Gilles Peskine01126fa2018-07-12 17:04:55 +02002499 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002500 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002501 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002502
Gilles Peskine01126fa2018-07-12 17:04:55 +02002503 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002504
2505cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002506 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002507
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002508 return( status );
2509}
John Durkop6ba40d12020-11-10 08:50:04 -08002510#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002511
Gilles Peskine89167cb2018-07-08 20:12:23 +02002512static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002513 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002514 psa_algorithm_t alg,
2515 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002516{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002517 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002518 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002519 psa_key_slot_t *slot;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002520 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002521 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002522
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002523 /* A context must be freshly initialized before it can be set up. */
2524 if( operation->alg != 0 )
2525 {
2526 return( PSA_ERROR_BAD_STATE );
2527 }
2528
Steven Cooreman15472f82021-03-02 16:16:22 +01002529 status = psa_mac_init( operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002530 if( status != PSA_SUCCESS )
2531 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002532 if( is_sign )
2533 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534
Ronald Cron5c522922020-11-14 16:35:34 +01002535 status = psa_get_and_lock_transparent_key_slot_with_policy(
2536 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002537 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002538 goto exit;
Steven Cooreman15472f82021-03-02 16:16:22 +01002539
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002540 /* Validate the combination of key type and algorithm */
2541 status = psa_mac_key_can_do( alg, slot->attr.type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002542 if( status != PSA_SUCCESS )
2543 goto exit;
2544
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002545 /* Get the output length for the algorithm and key combination. None of the
2546 * currently supported algorithms have an output length dependent on actual
2547 * key size, so setting it to a bogus value is currently OK. */
2548 operation->mac_size = PSA_MAC_LENGTH( slot->attr.type, 0, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002549
2550 if( operation->mac_size < 4 )
2551 {
2552 /* A very short MAC is too short for security since it can be
2553 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2554 * so we make this our minimum, even though 32 bits is still
2555 * too small for security. */
2556 status = PSA_ERROR_NOT_SUPPORTED;
2557 goto exit;
2558 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002559
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002560 if( operation->mac_size >
2561 PSA_MAC_LENGTH( slot->attr.type, 0, PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
2562 {
2563 /* It's impossible to "truncate" to a larger length than the full length
2564 * of the algorithm. */
2565 status = PSA_ERROR_INVALID_ARGUMENT;
2566 goto exit;
2567 }
2568
Ronald Cron3d471812021-03-18 13:40:31 +01002569#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01002570 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002571 {
Steven Cooreman15472f82021-03-02 16:16:22 +01002572 status = psa_cmac_setup( operation, slot );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002573 }
2574 else
Ronald Cron3d471812021-03-18 13:40:31 +01002575#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
John Durkopd0321952020-10-29 21:37:36 -07002576#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01002577 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002578 {
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002579 /* Sanity check. This shouldn't fail on a valid configuration. */
Steven Cooreman1fb691a2021-03-08 12:01:55 +01002580 if( operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002581 {
2582 status = PSA_ERROR_NOT_SUPPORTED;
2583 goto exit;
2584 }
2585
Gilles Peskine8e338702019-07-30 20:06:31 +02002586 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002587 {
2588 status = PSA_ERROR_INVALID_ARGUMENT;
2589 goto exit;
2590 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002591
Gilles Peskine01126fa2018-07-12 17:04:55 +02002592 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002593 slot->key.data,
2594 slot->key.bytes,
Steven Cooreman15472f82021-03-02 16:16:22 +01002595 PSA_ALG_HMAC_GET_HASH( alg ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002596 }
2597 else
John Durkopd0321952020-10-29 21:37:36 -07002598#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002599 {
2600 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002602
Gilles Peskinefbfac682018-07-08 20:51:54 +02002603exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002604 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002606 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002608 else
2609 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002610 operation->key_set = 1;
2611 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002612
Ronald Cron5c522922020-11-14 16:35:34 +01002613 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002614
Ronald Cron5c522922020-11-14 16:35:34 +01002615 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616}
2617
Gilles Peskine89167cb2018-07-08 20:12:23 +02002618psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002619 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002620 psa_algorithm_t alg )
2621{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002622 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002623}
2624
2625psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002626 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002627 psa_algorithm_t alg )
2628{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002629 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002630}
2631
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2633 const uint8_t *input,
2634 size_t input_length )
2635{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002636 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002637 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002638 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002640 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002641 operation->has_input = 1;
2642
Ronald Cron3d471812021-03-18 13:40:31 +01002643#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002644 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002645 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002646 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2647 input, input_length );
2648 status = mbedtls_to_psa_error( ret );
2649 }
2650 else
Ronald Cron3d471812021-03-18 13:40:31 +01002651#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
John Durkopd0321952020-10-29 21:37:36 -07002652#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002653 if( PSA_ALG_IS_HMAC( operation->alg ) )
2654 {
2655 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2656 input_length );
2657 }
2658 else
John Durkopd0321952020-10-29 21:37:36 -07002659#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002660 {
2661 /* This shouldn't happen if `operation` was initialized by
2662 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002663 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002664 }
2665
Gilles Peskinefbfac682018-07-08 20:51:54 +02002666 if( status != PSA_SUCCESS )
2667 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002668 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002669}
2670
John Durkop6ba40d12020-11-10 08:50:04 -08002671#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002672static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2673 uint8_t *mac,
2674 size_t mac_size )
2675{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002676 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Steven Cooreman0e307642021-02-18 16:18:32 +01002677 psa_algorithm_t hash_alg = hmac->alg;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002678 size_t hash_size = 0;
2679 size_t block_size = psa_get_hash_block_size( hash_alg );
2680 psa_status_t status;
2681
Gilles Peskine01126fa2018-07-12 17:04:55 +02002682 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2683 if( status != PSA_SUCCESS )
2684 return( status );
2685 /* From here on, tmp needs to be wiped. */
2686
2687 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2688 if( status != PSA_SUCCESS )
2689 goto exit;
2690
2691 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2692 if( status != PSA_SUCCESS )
2693 goto exit;
2694
2695 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2696 if( status != PSA_SUCCESS )
2697 goto exit;
2698
Gilles Peskined911eb72018-08-14 15:18:45 +02002699 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2700 if( status != PSA_SUCCESS )
2701 goto exit;
2702
2703 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002704
2705exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002706 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002707 return( status );
2708}
John Durkop6ba40d12020-11-10 08:50:04 -08002709#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002710
mohammad16036df908f2018-04-02 08:34:15 -07002711static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002712 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002713 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002714{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002715 if( ! operation->key_set )
2716 return( PSA_ERROR_BAD_STATE );
2717 if( operation->iv_required && ! operation->iv_set )
2718 return( PSA_ERROR_BAD_STATE );
2719
Gilles Peskine8c9def32018-02-08 10:02:12 +01002720 if( mac_size < operation->mac_size )
2721 return( PSA_ERROR_BUFFER_TOO_SMALL );
2722
Ronald Cron3d471812021-03-18 13:40:31 +01002723#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002724 if( operation->alg == PSA_ALG_CMAC )
2725 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002726 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02002727 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2728 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002729 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002730 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002731 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002732 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002733 else
Ronald Cron3d471812021-03-18 13:40:31 +01002734#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
John Durkopd0321952020-10-29 21:37:36 -07002735#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002736 if( PSA_ALG_IS_HMAC( operation->alg ) )
2737 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002738 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002739 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002740 }
2741 else
John Durkopd0321952020-10-29 21:37:36 -07002742#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002743 {
2744 /* This shouldn't happen if `operation` was initialized by
2745 * a setup function. */
2746 return( PSA_ERROR_BAD_STATE );
2747 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748}
2749
Gilles Peskineacd4be32018-07-08 19:56:25 +02002750psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2751 uint8_t *mac,
2752 size_t mac_size,
2753 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002754{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002755 psa_status_t status;
2756
Jaeden Amero252ef282019-02-15 14:05:35 +00002757 if( operation->alg == 0 )
2758 {
2759 return( PSA_ERROR_BAD_STATE );
2760 }
2761
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002762 /* Fill the output buffer with something that isn't a valid mac
2763 * (barring an attack on the mac and deliberately-crafted input),
2764 * in case the caller doesn't check the return status properly. */
2765 *mac_length = mac_size;
2766 /* If mac_size is 0 then mac may be NULL and then the
2767 * call to memset would have undefined behavior. */
2768 if( mac_size != 0 )
2769 memset( mac, '!', mac_size );
2770
Gilles Peskine89167cb2018-07-08 20:12:23 +02002771 if( ! operation->is_sign )
2772 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002773 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002774 }
mohammad16036df908f2018-04-02 08:34:15 -07002775
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002776 status = psa_mac_finish_internal( operation, mac, mac_size );
2777
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002778 if( status == PSA_SUCCESS )
2779 {
2780 status = psa_mac_abort( operation );
2781 if( status == PSA_SUCCESS )
2782 *mac_length = operation->mac_size;
2783 else
2784 memset( mac, '!', mac_size );
2785 }
2786 else
2787 psa_mac_abort( operation );
2788 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002789}
2790
Gilles Peskineacd4be32018-07-08 19:56:25 +02002791psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2792 const uint8_t *mac,
2793 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002794{
Gilles Peskine828ed142018-06-18 23:25:51 +02002795 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002796 psa_status_t status;
2797
Jaeden Amero252ef282019-02-15 14:05:35 +00002798 if( operation->alg == 0 )
2799 {
2800 return( PSA_ERROR_BAD_STATE );
2801 }
2802
Gilles Peskine89167cb2018-07-08 20:12:23 +02002803 if( operation->is_sign )
2804 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002805 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002806 }
2807 if( operation->mac_size != mac_length )
2808 {
2809 status = PSA_ERROR_INVALID_SIGNATURE;
2810 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002811 }
mohammad16036df908f2018-04-02 08:34:15 -07002812
2813 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002814 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01002815 if( status != PSA_SUCCESS )
2816 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002817
2818 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2819 status = PSA_ERROR_INVALID_SIGNATURE;
2820
2821cleanup:
2822 if( status == PSA_SUCCESS )
2823 status = psa_mac_abort( operation );
2824 else
2825 psa_mac_abort( operation );
2826
Gilles Peskine3f108122018-12-07 18:14:53 +01002827 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002828
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002829 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002830}
2831
2832
Gilles Peskine20035e32018-02-03 22:44:14 +01002833
Gilles Peskine20035e32018-02-03 22:44:14 +01002834/****************************************************************/
2835/* Asymmetric cryptography */
2836/****************************************************************/
2837
Ronald Cron67b1eb32020-12-08 17:37:27 +01002838psa_status_t psa_sign_hash_internal(
Ronald Cron18659932020-12-08 16:32:23 +01002839 const psa_key_attributes_t *attributes,
2840 const uint8_t *key_buffer, size_t key_buffer_size,
2841 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2842 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002843{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002844 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002845 {
Ronald Cron4501c982021-03-19 20:09:32 +01002846 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2847 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002848 {
Ronald Cron4501c982021-03-19 20:09:32 +01002849#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2850 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2851 return( mbedtls_psa_rsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002852 attributes,
2853 key_buffer, key_buffer_size,
2854 alg, hash, hash_length,
2855 signature, signature_size, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002856#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2857 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002858 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002859 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002860 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002861 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002862 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002863 }
2864 else
Ronald Cron4501c982021-03-19 20:09:32 +01002865 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
2866 {
2867 if( PSA_ALG_IS_ECDSA( alg ) )
2868 {
2869#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2870 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2871 return( mbedtls_psa_ecdsa_sign_hash(
2872 attributes,
2873 key_buffer, key_buffer_size,
2874 alg, hash, hash_length,
2875 signature, signature_size, signature_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002876#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2877 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron4501c982021-03-19 20:09:32 +01002878 }
2879 else
2880 {
2881 return( PSA_ERROR_INVALID_ARGUMENT );
2882 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002883 }
Ronald Cron4501c982021-03-19 20:09:32 +01002884
2885 (void)key_buffer;
2886 (void)key_buffer_size;
2887 (void)hash;
2888 (void)hash_length;
2889 (void)signature;
2890 (void)signature_size;
2891 (void)signature_length;
2892
2893 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01002894}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002895
2896psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
2897 psa_algorithm_t alg,
2898 const uint8_t *hash,
2899 size_t hash_length,
2900 uint8_t *signature,
2901 size_t signature_size,
2902 size_t *signature_length )
2903{
2904 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2905 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2906 psa_key_slot_t *slot;
2907
2908 *signature_length = signature_size;
2909 /* Immediately reject a zero-length signature buffer. This guarantees
2910 * that signature must be a valid pointer. (On the other hand, the hash
2911 * buffer can in principle be empty since it doesn't actually have
2912 * to be a hash.) */
2913 if( signature_size == 0 )
2914 return( PSA_ERROR_BUFFER_TOO_SMALL );
2915
2916 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
2917 PSA_KEY_USAGE_SIGN_HASH,
2918 alg );
2919 if( status != PSA_SUCCESS )
2920 goto exit;
2921 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
2922 {
2923 status = PSA_ERROR_INVALID_ARGUMENT;
2924 goto exit;
2925 }
2926
Ronald Cron9f17aa42020-12-08 17:07:25 +01002927 psa_key_attributes_t attributes = {
2928 .core = slot->attr
2929 };
Gilles Peskinee59236f2018-01-27 23:32:46 +01002930
Ronald Cron9f17aa42020-12-08 17:07:25 +01002931 status = psa_driver_wrapper_sign_hash(
2932 &attributes, slot->key.data, slot->key.bytes,
2933 alg, hash, hash_length,
2934 signature, signature_size, signature_length );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002935
2936exit:
2937 /* Fill the unused part of the output buffer (the whole buffer on error,
2938 * the trailing part on success) with something that isn't a valid mac
2939 * (barring an attack on the mac and deliberately-crafted input),
2940 * in case the caller doesn't check the return status properly. */
2941 if( status == PSA_SUCCESS )
2942 memset( signature + *signature_length, '!',
2943 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02002944 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002945 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002946 /* If signature_size is 0 then we have nothing to do. We must not call
2947 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02002948
Ronald Cron5c522922020-11-14 16:35:34 +01002949 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002950
Ronald Cron5c522922020-11-14 16:35:34 +01002951 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03002952}
2953
Ronald Cron67b1eb32020-12-08 17:37:27 +01002954psa_status_t psa_verify_hash_internal(
Ronald Cron18659932020-12-08 16:32:23 +01002955 const psa_key_attributes_t *attributes,
2956 const uint8_t *key_buffer, size_t key_buffer_size,
2957 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2958 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002959{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002960 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002961 {
Ronald Cron4501c982021-03-19 20:09:32 +01002962 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2963 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002964 {
Ronald Cron4501c982021-03-19 20:09:32 +01002965#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2966 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2967 return( mbedtls_psa_rsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002968 attributes,
2969 key_buffer, key_buffer_size,
2970 alg, hash, hash_length,
2971 signature, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002972#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2973 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002974 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002975 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002976 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002977 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002978 }
itayzafrir5c753392018-05-08 11:18:38 +03002979 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002980 else
Ronald Cron4501c982021-03-19 20:09:32 +01002981 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002982 {
Ronald Cron4501c982021-03-19 20:09:32 +01002983 if( PSA_ALG_IS_ECDSA( alg ) )
2984 {
2985#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2986 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2987 return( mbedtls_psa_ecdsa_verify_hash(
2988 attributes,
2989 key_buffer, key_buffer_size,
2990 alg, hash, hash_length,
2991 signature, signature_length ) );
2992#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2993 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
2994 }
2995 else
2996 {
2997 return( PSA_ERROR_INVALID_ARGUMENT );
2998 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002999 }
Ronald Cron4501c982021-03-19 20:09:32 +01003000
3001 (void)key_buffer;
3002 (void)key_buffer_size;
3003 (void)hash;
3004 (void)hash_length;
3005 (void)signature;
3006 (void)signature_length;
3007
3008 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01003009}
3010
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003011psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
3012 psa_algorithm_t alg,
3013 const uint8_t *hash,
3014 size_t hash_length,
3015 const uint8_t *signature,
3016 size_t signature_length )
3017{
3018 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3019 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003020 psa_key_slot_t *slot;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003021
3022 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003023 PSA_KEY_USAGE_VERIFY_HASH,
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003024 alg );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003025 if( status != PSA_SUCCESS )
3026 return( status );
3027
Ronald Cron9f17aa42020-12-08 17:07:25 +01003028 psa_key_attributes_t attributes = {
3029 .core = slot->attr
3030 };
Gilles Peskinee59236f2018-01-27 23:32:46 +01003031
Ronald Cron9f17aa42020-12-08 17:07:25 +01003032 status = psa_driver_wrapper_verify_hash(
3033 &attributes, slot->key.data, slot->key.bytes,
3034 alg, hash, hash_length,
3035 signature, signature_length );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003036
Ronald Cron5c522922020-11-14 16:35:34 +01003037 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003038
Ronald Cron5c522922020-11-14 16:35:34 +01003039 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003040}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003041
John Durkop0e005192020-10-31 22:06:54 -07003042#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003043static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3044 mbedtls_rsa_context *rsa )
3045{
3046 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3047 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3048 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3049 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3050}
John Durkop0e005192020-10-31 22:06:54 -07003051#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003052
Ronald Croncf56a0a2020-08-04 09:51:30 +02003053psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003054 psa_algorithm_t alg,
3055 const uint8_t *input,
3056 size_t input_length,
3057 const uint8_t *salt,
3058 size_t salt_length,
3059 uint8_t *output,
3060 size_t output_size,
3061 size_t *output_length )
3062{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003063 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003064 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003065 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003066
Darryl Green5cc689a2018-07-24 15:34:10 +01003067 (void) input;
3068 (void) input_length;
3069 (void) salt;
3070 (void) output;
3071 (void) output_size;
3072
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003073 *output_length = 0;
3074
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003075 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3076 return( PSA_ERROR_INVALID_ARGUMENT );
3077
Ronald Cron5c522922020-11-14 16:35:34 +01003078 status = psa_get_and_lock_transparent_key_slot_with_policy(
3079 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003080 if( status != PSA_SUCCESS )
3081 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003082 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3083 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003084 {
3085 status = PSA_ERROR_INVALID_ARGUMENT;
3086 goto exit;
3087 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003088
John Durkop6ba40d12020-11-10 08:50:04 -08003089#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3090 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003091 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003092 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003093 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003094 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3095 slot->key.data,
3096 slot->key.bytes,
3097 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003098 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003099 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003100
3101 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003102 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003103 status = PSA_ERROR_BUFFER_TOO_SMALL;
3104 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003105 }
John Durkop0e005192020-10-31 22:06:54 -07003106#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003107 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
3108 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003109 status = mbedtls_to_psa_error(
3110 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003111 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003112 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003113 MBEDTLS_RSA_PUBLIC,
3114 input_length,
3115 input,
3116 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003117 }
3118 else
John Durkop0e005192020-10-31 22:06:54 -07003119#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3120#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003121 if( PSA_ALG_IS_RSA_OAEP( alg ) )
3122 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003123 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003124 status = mbedtls_to_psa_error(
3125 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003126 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003127 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003128 MBEDTLS_RSA_PUBLIC,
3129 salt, salt_length,
3130 input_length,
3131 input,
3132 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003133 }
3134 else
John Durkop0e005192020-10-31 22:06:54 -07003135#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003136 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003137 status = PSA_ERROR_INVALID_ARGUMENT;
3138 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003139 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003140rsa_exit:
3141 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003142 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003143
Steven Cooremana2371e52020-07-28 14:30:39 +02003144 mbedtls_rsa_free( rsa );
3145 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003146 }
3147 else
John Durkop9814fa22020-11-04 12:28:15 -08003148#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003149 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003150 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003151 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003152 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003153
3154exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003155 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003156
Ronald Cron5c522922020-11-14 16:35:34 +01003157 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003158}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003159
Ronald Croncf56a0a2020-08-04 09:51:30 +02003160psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003161 psa_algorithm_t alg,
3162 const uint8_t *input,
3163 size_t input_length,
3164 const uint8_t *salt,
3165 size_t salt_length,
3166 uint8_t *output,
3167 size_t output_size,
3168 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003169{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003171 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003172 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003173
Darryl Green5cc689a2018-07-24 15:34:10 +01003174 (void) input;
3175 (void) input_length;
3176 (void) salt;
3177 (void) output;
3178 (void) output_size;
3179
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003180 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003181
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003182 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3183 return( PSA_ERROR_INVALID_ARGUMENT );
3184
Ronald Cron5c522922020-11-14 16:35:34 +01003185 status = psa_get_and_lock_transparent_key_slot_with_policy(
3186 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003187 if( status != PSA_SUCCESS )
3188 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003189 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003190 {
3191 status = PSA_ERROR_INVALID_ARGUMENT;
3192 goto exit;
3193 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194
John Durkop6ba40d12020-11-10 08:50:04 -08003195#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3196 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003197 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003198 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003199 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003200 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3201 slot->key.data,
3202 slot->key.bytes,
3203 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003204 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003205 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003206
Steven Cooremana2371e52020-07-28 14:30:39 +02003207 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003208 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003209 status = PSA_ERROR_INVALID_ARGUMENT;
3210 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003211 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212
John Durkop0e005192020-10-31 22:06:54 -07003213#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003214 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003215 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003216 status = mbedtls_to_psa_error(
3217 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003218 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003219 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003220 MBEDTLS_RSA_PRIVATE,
3221 output_length,
3222 input,
3223 output,
3224 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003225 }
3226 else
John Durkop0e005192020-10-31 22:06:54 -07003227#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3228#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003229 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003230 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003231 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003232 status = mbedtls_to_psa_error(
3233 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003234 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003235 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003236 MBEDTLS_RSA_PRIVATE,
3237 salt, salt_length,
3238 output_length,
3239 input,
3240 output,
3241 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242 }
3243 else
John Durkop0e005192020-10-31 22:06:54 -07003244#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003245 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003246 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003248
Steven Cooreman4fed4552020-08-03 14:46:03 +02003249rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003250 mbedtls_rsa_free( rsa );
3251 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253 else
John Durkop6ba40d12020-11-10 08:50:04 -08003254#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3255 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003256 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003257 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003258 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003259
3260exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003261 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003262
Ronald Cron5c522922020-11-14 16:35:34 +01003263 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003264}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003265
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003266
3267
mohammad1603503973b2018-03-12 15:59:30 +02003268/****************************************************************/
3269/* Symmetric cryptography */
3270/****************************************************************/
3271
Ronald Cronab99ac22020-10-01 16:38:28 +02003272static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
3273 mbedtls_svc_key_id_t key,
3274 psa_algorithm_t alg,
3275 mbedtls_operation_t cipher_operation )
3276{
3277 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3278 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3279 psa_key_slot_t *slot;
3280 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3281 PSA_KEY_USAGE_ENCRYPT :
3282 PSA_KEY_USAGE_DECRYPT );
3283
3284 /* A context must be freshly initialized before it can be set up. */
Ronald Cron49fafa92021-03-10 08:34:23 +01003285 if( operation->id != 0 )
Ronald Cronab99ac22020-10-01 16:38:28 +02003286 return( PSA_ERROR_BAD_STATE );
3287
3288 /* The requested algorithm must be one that can be processed by cipher. */
3289 if( ! PSA_ALG_IS_CIPHER( alg ) )
3290 return( PSA_ERROR_INVALID_ARGUMENT );
3291
3292 /* Fetch key material from key storage. */
3293 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
3294 if( status != PSA_SUCCESS )
3295 goto exit;
3296
Ronald Cron49fafa92021-03-10 08:34:23 +01003297 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003298 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003299 * so we only set it (in the driver wrapper) after resources have been
3300 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003301 operation->iv_set = 0;
Ronald Cronab99ac22020-10-01 16:38:28 +02003302 if( alg == PSA_ALG_ECB_NO_PADDING )
3303 operation->iv_required = 0;
3304 else
3305 operation->iv_required = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003306 operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
Ronald Cronab99ac22020-10-01 16:38:28 +02003307
Ronald Crona4af55f2020-12-14 14:36:06 +01003308 psa_key_attributes_t attributes = {
3309 .core = slot->attr
3310 };
3311
Ronald Cronab99ac22020-10-01 16:38:28 +02003312 /* Try doing the operation through a driver before using software fallback. */
3313 if( cipher_operation == MBEDTLS_ENCRYPT )
Ronald Crona4af55f2020-12-14 14:36:06 +01003314 status = psa_driver_wrapper_cipher_encrypt_setup( operation,
3315 &attributes,
3316 slot->key.data,
3317 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003318 alg );
3319 else
Ronald Crona4af55f2020-12-14 14:36:06 +01003320 status = psa_driver_wrapper_cipher_decrypt_setup( operation,
3321 &attributes,
3322 slot->key.data,
3323 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003324 alg );
3325
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003326exit:
Ronald Cron06aa4422021-03-09 17:32:57 +01003327 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003328 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003329
Ronald Cron5c522922020-11-14 16:35:34 +01003330 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003331
Ronald Cron5c522922020-11-14 16:35:34 +01003332 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003333}
3334
Gilles Peskinefe119512018-07-08 21:39:34 +02003335psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003336 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003337 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003338{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003339 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003340}
3341
Gilles Peskinefe119512018-07-08 21:39:34 +02003342psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003343 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003344 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003345{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003346 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003347}
3348
Gilles Peskinefe119512018-07-08 21:39:34 +02003349psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003350 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003351 size_t iv_size,
3352 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003353{
Ronald Cron6d051732020-10-01 14:10:20 +02003354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003355
Ronald Cron5618a392021-03-26 09:52:26 +01003356 *iv_length = 0;
3357
Ronald Cron49fafa92021-03-10 08:34:23 +01003358 if( operation->id == 0 )
Ronald Cronc45b4af2020-09-29 16:18:05 +02003359 {
3360 return( PSA_ERROR_BAD_STATE );
3361 }
3362
Steven Cooreman7df02922020-09-09 15:28:49 +02003363 if( operation->iv_set || ! operation->iv_required )
3364 {
3365 return( PSA_ERROR_BAD_STATE );
3366 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003367
Ronald Cron5618a392021-03-26 09:52:26 +01003368 if( iv_size < operation->default_iv_length )
3369 {
3370 status = PSA_ERROR_BUFFER_TOO_SMALL;
3371 goto exit;
3372 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003373
Ronald Cron5618a392021-03-26 09:52:26 +01003374 status = psa_generate_random( iv, operation->default_iv_length );
3375 if( status != PSA_SUCCESS )
3376 goto exit;
3377
3378 status = psa_driver_wrapper_cipher_set_iv( operation,
3379 iv,
3380 operation->default_iv_length );
3381
3382exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003383 if( status == PSA_SUCCESS )
Ronald Cron5618a392021-03-26 09:52:26 +01003384 {
Steven Cooreman7df02922020-09-09 15:28:49 +02003385 operation->iv_set = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003386 *iv_length = operation->default_iv_length;
3387 }
Steven Cooreman7df02922020-09-09 15:28:49 +02003388 else
Moran Peker395db872018-05-31 14:07:14 +03003389 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003390
itayzafrir534bd7c2018-08-02 13:56:32 +03003391 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003392}
3393
Gilles Peskinefe119512018-07-08 21:39:34 +02003394psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003395 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003396 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003397{
Ronald Cron6d051732020-10-01 14:10:20 +02003398 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003399
Ronald Cron49fafa92021-03-10 08:34:23 +01003400 if( operation->id == 0 )
Ronald Cronc45b4af2020-09-29 16:18:05 +02003401 return( PSA_ERROR_BAD_STATE );
Ronald Cronc45b4af2020-09-29 16:18:05 +02003402
Steven Cooreman7df02922020-09-09 15:28:49 +02003403 if( operation->iv_set || ! operation->iv_required )
Steven Cooreman7df02922020-09-09 15:28:49 +02003404 return( PSA_ERROR_BAD_STATE );
Ronald Crona0d68172021-03-26 10:15:08 +01003405
3406 if( iv_length > PSA_CIPHER_IV_MAX_SIZE )
3407 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003408
Ronald Crondd24c9b2020-12-15 14:10:01 +01003409 status = psa_driver_wrapper_cipher_set_iv( operation,
3410 iv,
3411 iv_length );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003412
itayzafrir534bd7c2018-08-02 13:56:32 +03003413 if( status == PSA_SUCCESS )
3414 operation->iv_set = 1;
3415 else
mohammad1603503973b2018-03-12 15:59:30 +02003416 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003417 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003418}
3419
Gilles Peskinee553c652018-06-04 16:22:46 +02003420psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3421 const uint8_t *input,
3422 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003423 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003424 size_t output_size,
3425 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003426{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003427 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003428
Ronald Cron49fafa92021-03-10 08:34:23 +01003429 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003430 {
3431 return( PSA_ERROR_BAD_STATE );
3432 }
3433 if( operation->iv_required && ! operation->iv_set )
3434 {
3435 return( PSA_ERROR_BAD_STATE );
3436 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003437
Ronald Crondd24c9b2020-12-15 14:10:01 +01003438 status = psa_driver_wrapper_cipher_update( operation,
3439 input,
3440 input_length,
3441 output,
3442 output_size,
3443 output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003444 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003445 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003446
itayzafrir534bd7c2018-08-02 13:56:32 +03003447 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003448}
3449
Gilles Peskinee553c652018-06-04 16:22:46 +02003450psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3451 uint8_t *output,
3452 size_t output_size,
3453 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003454{
David Saadab4ecc272019-02-14 13:48:10 +02003455 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003456
Ronald Cron49fafa92021-03-10 08:34:23 +01003457 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003458 {
3459 return( PSA_ERROR_BAD_STATE );
3460 }
3461 if( operation->iv_required && ! operation->iv_set )
3462 {
3463 return( PSA_ERROR_BAD_STATE );
3464 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003465
Ronald Crondd24c9b2020-12-15 14:10:01 +01003466 status = psa_driver_wrapper_cipher_finish( operation,
3467 output,
3468 output_size,
3469 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003470 if( status == PSA_SUCCESS )
3471 return( psa_cipher_abort( operation ) );
3472 else
3473 {
3474 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003475 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01003476
Steven Cooremanef8575e2020-09-11 11:44:50 +02003477 return( status );
3478 }
mohammad1603503973b2018-03-12 15:59:30 +02003479}
3480
Gilles Peskinee553c652018-06-04 16:22:46 +02003481psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3482{
Ronald Cron49fafa92021-03-10 08:34:23 +01003483 if( operation->id == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003484 {
Steven Cooremana07b9972020-09-10 14:54:14 +02003485 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003486 * in use. It's ok to call abort on such an object, and there's
3487 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003488 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003489 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003490
Ronald Crondd24c9b2020-12-15 14:10:01 +01003491 psa_driver_wrapper_cipher_abort( operation );
Gilles Peskinee553c652018-06-04 16:22:46 +02003492
Ronald Cron49fafa92021-03-10 08:34:23 +01003493 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003494 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003495 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003496
Moran Peker395db872018-05-31 14:07:14 +03003497 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003498}
3499
mohammad16035955c982018-04-26 00:53:03 +03003500/****************************************************************/
3501/* AEAD */
3502/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003503
Ronald Croncf56a0a2020-08-04 09:51:30 +02003504psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003505 psa_algorithm_t alg,
3506 const uint8_t *nonce,
3507 size_t nonce_length,
3508 const uint8_t *additional_data,
3509 size_t additional_data_length,
3510 const uint8_t *plaintext,
3511 size_t plaintext_length,
3512 uint8_t *ciphertext,
3513 size_t ciphertext_size,
3514 size_t *ciphertext_length )
3515{
Ronald Cron215633c2021-03-16 17:15:37 +01003516 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3517 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003518
mohammad1603f08a5502018-06-03 15:05:47 +03003519 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003520
Steven Cooremanea7ab132021-03-17 16:28:00 +01003521 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3522 return( PSA_ERROR_NOT_SUPPORTED );
3523
Ronald Cron9a986162021-03-26 12:40:07 +01003524 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003525 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003526 if( status != PSA_SUCCESS )
3527 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003528
Ronald Cron215633c2021-03-16 17:15:37 +01003529 psa_key_attributes_t attributes = {
3530 .core = slot->attr
3531 };
mohammad16035955c982018-04-26 00:53:03 +03003532
Ronald Cronde822812021-03-17 16:08:20 +01003533 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003534 &attributes, slot->key.data, slot->key.bytes,
3535 alg,
3536 nonce, nonce_length,
3537 additional_data, additional_data_length,
3538 plaintext, plaintext_length,
3539 ciphertext, ciphertext_size, ciphertext_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003540
Gilles Peskineedf9a652018-08-17 18:11:56 +02003541 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3542 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003543
Ronald Cron9f310172021-03-16 16:36:37 +01003544 psa_unlock_key_slot( slot );
mohammad16035955c982018-04-26 00:53:03 +03003545
mohammad16035955c982018-04-26 00:53:03 +03003546 return( status );
Gilles Peskineee652a32018-06-01 19:23:52 +02003547}
3548
Ronald Croncf56a0a2020-08-04 09:51:30 +02003549psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003550 psa_algorithm_t alg,
3551 const uint8_t *nonce,
3552 size_t nonce_length,
3553 const uint8_t *additional_data,
3554 size_t additional_data_length,
3555 const uint8_t *ciphertext,
3556 size_t ciphertext_length,
3557 uint8_t *plaintext,
3558 size_t plaintext_size,
3559 size_t *plaintext_length )
3560{
Ronald Cron215633c2021-03-16 17:15:37 +01003561 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3562 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003563
Gilles Peskineee652a32018-06-01 19:23:52 +02003564 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003565
Steven Cooremanea7ab132021-03-17 16:28:00 +01003566 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3567 return( PSA_ERROR_NOT_SUPPORTED );
3568
Ronald Cron9a986162021-03-26 12:40:07 +01003569 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003570 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003571 if( status != PSA_SUCCESS )
3572 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003573
Ronald Cron215633c2021-03-16 17:15:37 +01003574 psa_key_attributes_t attributes = {
3575 .core = slot->attr
3576 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003577
Ronald Cronde822812021-03-17 16:08:20 +01003578 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003579 &attributes, slot->key.data, slot->key.bytes,
3580 alg,
3581 nonce, nonce_length,
3582 additional_data, additional_data_length,
3583 ciphertext, ciphertext_length,
3584 plaintext, plaintext_size, plaintext_length );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003585
Gilles Peskineedf9a652018-08-17 18:11:56 +02003586 if( status != PSA_SUCCESS && plaintext_size != 0 )
3587 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003588
Ronald Cron215633c2021-03-16 17:15:37 +01003589 psa_unlock_key_slot( slot );
3590
Gilles Peskineedf9a652018-08-17 18:11:56 +02003591 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003592}
3593
Gilles Peskinee59236f2018-01-27 23:32:46 +01003594/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003595/* Generators */
3596/****************************************************************/
3597
John Durkop07cc04a2020-11-16 22:08:34 -08003598#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3599 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3600 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3601#define AT_LEAST_ONE_BUILTIN_KDF
3602#endif
3603
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003604#define HKDF_STATE_INIT 0 /* no input yet */
3605#define HKDF_STATE_STARTED 1 /* got salt */
3606#define HKDF_STATE_KEYED 2 /* got key */
3607#define HKDF_STATE_OUTPUT 3 /* output started */
3608
Gilles Peskinecbe66502019-05-16 16:59:18 +02003609static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003610 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003611{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003612 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3613 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003614 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003615 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003616}
3617
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003618psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003619{
3620 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003621 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003622 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003623 {
3624 /* The object has (apparently) been initialized but it is not
3625 * in use. It's ok to call abort on such an object, and there's
3626 * nothing to do. */
3627 }
3628 else
John Durkopd0321952020-10-29 21:37:36 -07003629#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003630 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003631 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003632 mbedtls_free( operation->ctx.hkdf.info );
3633 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003634 }
John Durkop07cc04a2020-11-16 22:08:34 -08003635 else
3636#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3637#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3638 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3639 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003640 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003641 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003642 {
Janos Follath6a1d2622019-06-11 10:37:28 +01003643 if( operation->ctx.tls12_prf.seed != NULL )
3644 {
3645 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
3646 operation->ctx.tls12_prf.seed_length );
3647 mbedtls_free( operation->ctx.tls12_prf.seed );
3648 }
3649
3650 if( operation->ctx.tls12_prf.label != NULL )
3651 {
3652 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
3653 operation->ctx.tls12_prf.label_length );
3654 mbedtls_free( operation->ctx.tls12_prf.label );
3655 }
3656
3657 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
3658
3659 /* We leave the fields Ai and output_block to be erased safely by the
3660 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003661 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003662 else
John Durkop07cc04a2020-11-16 22:08:34 -08003663#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3664 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003665 {
3666 status = PSA_ERROR_BAD_STATE;
3667 }
Janos Follath083036a2019-06-11 10:22:26 +01003668 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003669 return( status );
3670}
3671
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003672psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003673 size_t *capacity)
3674{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003675 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003676 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003677 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02003678 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003679 }
3680
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003681 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003682 return( PSA_SUCCESS );
3683}
3684
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003685psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003686 size_t capacity )
3687{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003688 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003689 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003690 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003691 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003692 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003693 return( PSA_SUCCESS );
3694}
3695
John Durkopd0321952020-10-29 21:37:36 -07003696#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003697/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003698 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003699static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003700 psa_algorithm_t hash_alg,
3701 uint8_t *output,
3702 size_t output_length )
3703{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003704 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003705 psa_status_t status;
3706
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003707 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3708 return( PSA_ERROR_BAD_STATE );
3709 hkdf->state = HKDF_STATE_OUTPUT;
3710
Gilles Peskinebef7f142018-07-12 17:22:21 +02003711 while( output_length != 0 )
3712 {
3713 /* Copy what remains of the current block */
3714 uint8_t n = hash_length - hkdf->offset_in_block;
3715 if( n > output_length )
3716 n = (uint8_t) output_length;
3717 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3718 output += n;
3719 output_length -= n;
3720 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003721 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003722 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003723 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003724 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003725 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003726 * object was corrupted or if this function is called directly
3727 * inside the library. */
3728 if( hkdf->block_number == 0xff )
3729 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003730
3731 /* We need a new block */
3732 ++hkdf->block_number;
3733 hkdf->offset_in_block = 0;
3734 status = psa_hmac_setup_internal( &hkdf->hmac,
3735 hkdf->prk, hash_length,
3736 hash_alg );
3737 if( status != PSA_SUCCESS )
3738 return( status );
3739 if( hkdf->block_number != 1 )
3740 {
3741 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3742 hkdf->output_block,
3743 hash_length );
3744 if( status != PSA_SUCCESS )
3745 return( status );
3746 }
3747 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3748 hkdf->info,
3749 hkdf->info_length );
3750 if( status != PSA_SUCCESS )
3751 return( status );
3752 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3753 &hkdf->block_number, 1 );
3754 if( status != PSA_SUCCESS )
3755 return( status );
3756 status = psa_hmac_finish_internal( &hkdf->hmac,
3757 hkdf->output_block,
3758 sizeof( hkdf->output_block ) );
3759 if( status != PSA_SUCCESS )
3760 return( status );
3761 }
3762
3763 return( PSA_SUCCESS );
3764}
John Durkop07cc04a2020-11-16 22:08:34 -08003765#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003766
John Durkop07cc04a2020-11-16 22:08:34 -08003767#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3768 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01003769static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
3770 psa_tls12_prf_key_derivation_t *tls12_prf,
3771 psa_algorithm_t alg )
3772{
3773 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003774 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01003775 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
3776 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003777
Janos Follath7742fee2019-06-17 12:58:10 +01003778 /* We can't be wanting more output after block 0xff, otherwise
3779 * the capacity check in psa_key_derivation_output_bytes() would have
3780 * prevented this call. It could happen only if the operation
3781 * object was corrupted or if this function is called directly
3782 * inside the library. */
3783 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01003784 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01003785
3786 /* We need a new block */
3787 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01003788 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01003789
3790 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3791 *
3792 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3793 *
3794 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3795 * HMAC_hash(secret, A(2) + seed) +
3796 * HMAC_hash(secret, A(3) + seed) + ...
3797 *
3798 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01003799 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01003800 *
Janos Follathea29bfb2019-06-19 12:21:20 +01003801 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01003802 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01003803 * is currently extracted as `output_block` and where i is
3804 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01003805 */
3806
Janos Follathea29bfb2019-06-19 12:21:20 +01003807 /* Save the hash context before using it, to preserve the hash state with
3808 * only the inner padding in it. We need this, because inner padding depends
3809 * on the key (secret in the RFC's terminology). */
3810 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
3811 if( status != PSA_SUCCESS )
3812 goto cleanup;
3813
3814 /* Calculate A(i) where i = tls12_prf->block_number. */
3815 if( tls12_prf->block_number == 1 )
3816 {
3817 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
3818 * the variable seed and in this instance means it in the context of the
3819 * P_hash function, where seed = label + seed.) */
3820 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
3821 tls12_prf->label, tls12_prf->label_length );
3822 if( status != PSA_SUCCESS )
3823 goto cleanup;
3824 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
3825 tls12_prf->seed, tls12_prf->seed_length );
3826 if( status != PSA_SUCCESS )
3827 goto cleanup;
3828 }
3829 else
3830 {
3831 /* A(i) = HMAC_hash(secret, A(i-1)) */
3832 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
3833 tls12_prf->Ai, hash_length );
3834 if( status != PSA_SUCCESS )
3835 goto cleanup;
3836 }
3837
3838 status = psa_hmac_finish_internal( &tls12_prf->hmac,
3839 tls12_prf->Ai, hash_length );
3840 if( status != PSA_SUCCESS )
3841 goto cleanup;
3842 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
3843 if( status != PSA_SUCCESS )
3844 goto cleanup;
3845
3846 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
3847 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
3848 tls12_prf->Ai, hash_length );
3849 if( status != PSA_SUCCESS )
3850 goto cleanup;
3851 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
3852 tls12_prf->label, tls12_prf->label_length );
3853 if( status != PSA_SUCCESS )
3854 goto cleanup;
3855 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
3856 tls12_prf->seed, tls12_prf->seed_length );
3857 if( status != PSA_SUCCESS )
3858 goto cleanup;
3859 status = psa_hmac_finish_internal( &tls12_prf->hmac,
3860 tls12_prf->output_block, hash_length );
3861 if( status != PSA_SUCCESS )
3862 goto cleanup;
3863 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
3864 if( status != PSA_SUCCESS )
3865 goto cleanup;
3866
Janos Follath7742fee2019-06-17 12:58:10 +01003867
3868cleanup:
3869
Janos Follathea29bfb2019-06-19 12:21:20 +01003870 cleanup_status = psa_hash_abort( &backup );
3871 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3872 status = cleanup_status;
3873
3874 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01003875}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003876
Janos Follath844eb0e2019-06-19 12:10:49 +01003877static psa_status_t psa_key_derivation_tls12_prf_read(
3878 psa_tls12_prf_key_derivation_t *tls12_prf,
3879 psa_algorithm_t alg,
3880 uint8_t *output,
3881 size_t output_length )
3882{
3883 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003884 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01003885 psa_status_t status;
3886 uint8_t offset, length;
3887
3888 while( output_length != 0 )
3889 {
3890 /* Check if we have fully processed the current block. */
3891 if( tls12_prf->left_in_block == 0 )
3892 {
3893 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
3894 alg );
3895 if( status != PSA_SUCCESS )
3896 return( status );
3897
3898 continue;
3899 }
3900
3901 if( tls12_prf->left_in_block > output_length )
3902 length = (uint8_t) output_length;
3903 else
3904 length = tls12_prf->left_in_block;
3905
3906 offset = hash_length - tls12_prf->left_in_block;
3907 memcpy( output, tls12_prf->output_block + offset, length );
3908 output += length;
3909 output_length -= length;
3910 tls12_prf->left_in_block -= length;
3911 }
3912
3913 return( PSA_SUCCESS );
3914}
John Durkop07cc04a2020-11-16 22:08:34 -08003915#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
3916 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003917
Janos Follath6c6c8fc2019-06-17 12:38:20 +01003918psa_status_t psa_key_derivation_output_bytes(
3919 psa_key_derivation_operation_t *operation,
3920 uint8_t *output,
3921 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003922{
3923 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003924 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003925
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003926 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003927 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003928 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02003929 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003930 }
3931
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003932 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003933 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003934 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003935 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003936 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02003937 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003938 goto exit;
3939 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003940 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003941 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003942 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003943 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02003944 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3945 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003946 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02003947 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02003948 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003949 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003950 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003951
John Durkopd0321952020-10-29 21:37:36 -07003952#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003953 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003954 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01003955 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003956 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003957 output, output_length );
3958 }
Janos Follathadbec812019-06-14 11:05:39 +01003959 else
John Durkop07cc04a2020-11-16 22:08:34 -08003960#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3961#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3962 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01003963 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08003964 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003965 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003966 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003967 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003968 output_length );
3969 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003970 else
John Durkop07cc04a2020-11-16 22:08:34 -08003971#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
3972 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003973 {
Steven Cooreman74afe472021-02-10 17:19:22 +01003974 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003975 return( PSA_ERROR_BAD_STATE );
3976 }
3977
3978exit:
3979 if( status != PSA_SUCCESS )
3980 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003981 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003982 * This allows us to differentiate between exhausted operations and
3983 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
3984 * operations. */
3985 psa_algorithm_t alg = operation->alg;
3986 psa_key_derivation_abort( operation );
3987 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003988 memset( output, '!', output_length );
3989 }
3990 return( status );
3991}
3992
David Brown7807bf72021-02-09 16:28:23 -07003993#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02003994static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3995{
3996 if( data_size >= 8 )
3997 mbedtls_des_key_set_parity( data );
3998 if( data_size >= 16 )
3999 mbedtls_des_key_set_parity( data + 8 );
4000 if( data_size >= 24 )
4001 mbedtls_des_key_set_parity( data + 16 );
4002}
David Brown7807bf72021-02-09 16:28:23 -07004003#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004004
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004005static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004006 psa_key_slot_t *slot,
4007 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004008 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004009{
4010 uint8_t *data = NULL;
4011 size_t bytes = PSA_BITS_TO_BYTES( bits );
4012 psa_status_t status;
4013
Gilles Peskine8e338702019-07-30 20:06:31 +02004014 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004015 return( PSA_ERROR_INVALID_ARGUMENT );
4016 if( bits % 8 != 0 )
4017 return( PSA_ERROR_INVALID_ARGUMENT );
4018 data = mbedtls_calloc( 1, bytes );
4019 if( data == NULL )
4020 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4021
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004022 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004023 if( status != PSA_SUCCESS )
4024 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07004025#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004026 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004027 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07004028#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004029
4030 status = psa_allocate_buffer_to_slot( slot, bytes );
4031 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00004032 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01004033
Ronald Cronbf33c932020-11-28 18:06:53 +01004034 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004035 psa_key_attributes_t attributes = {
4036 .core = slot->attr
4037 };
4038
Ronald Cronbf33c932020-11-28 18:06:53 +01004039 status = psa_driver_wrapper_import_key( &attributes,
4040 data, bytes,
4041 slot->key.data,
4042 slot->key.bytes,
4043 &slot->key.bytes, &bits );
4044 if( bits != slot->attr.bits )
4045 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004046
4047exit:
4048 mbedtls_free( data );
4049 return( status );
4050}
4051
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004052psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004053 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004054 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004055{
4056 psa_status_t status;
4057 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004058 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004059
Ronald Cron81709fc2020-11-14 12:10:32 +01004060 *key = MBEDTLS_SVC_KEY_ID_INIT;
4061
Gilles Peskine0f84d622019-09-12 19:03:13 +02004062 /* Reject any attempt to create a zero-length key so that we don't
4063 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4064 if( psa_get_key_bits( attributes ) == 0 )
4065 return( PSA_ERROR_INVALID_ARGUMENT );
4066
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004067 if( ! operation->can_output_key )
4068 return( PSA_ERROR_NOT_PERMITTED );
4069
Ronald Cron81709fc2020-11-14 12:10:32 +01004070 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
4071 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004072#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4073 if( driver != NULL )
4074 {
4075 /* Deriving a key in a secure element is not implemented yet. */
4076 status = PSA_ERROR_NOT_SUPPORTED;
4077 }
4078#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004079 if( status == PSA_SUCCESS )
4080 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004081 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004082 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004083 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004084 }
4085 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01004086 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004087 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004088 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004089
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004090 return( status );
4091}
4092
Gilles Peskineeab56e42018-07-12 17:12:33 +02004093
4094
4095/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004096/* Key derivation */
4097/****************************************************************/
4098
Steven Cooreman932ffb72021-02-15 12:14:32 +01004099#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004100static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004101 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004102 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004103{
John Durkop07cc04a2020-11-16 22:08:34 -08004104 int is_kdf_alg_supported;
4105
Janos Follath5fe19732019-06-20 15:09:30 +01004106 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4107 * initialisers for this union leave some bytes unspecified.) */
4108 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4109
Gilles Peskine969c5d62019-01-16 15:53:06 +01004110 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07004111#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08004112 if( PSA_ALG_IS_HKDF( kdf_alg ) )
4113 is_kdf_alg_supported = 1;
4114 else
4115#endif
4116#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4117 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
4118 is_kdf_alg_supported = 1;
4119 else
4120#endif
4121#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4122 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
4123 is_kdf_alg_supported = 1;
4124 else
4125#endif
4126 is_kdf_alg_supported = 0;
4127
4128 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004129 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004130 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004131 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004132 if( hash_size == 0 )
4133 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004134 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4135 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004136 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004137 {
4138 return( PSA_ERROR_NOT_SUPPORTED );
4139 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004140 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004141 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004142 }
John Durkop07cc04a2020-11-16 22:08:34 -08004143
4144 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004145}
John Durkop07cc04a2020-11-16 22:08:34 -08004146#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004147
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004148psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004149 psa_algorithm_t alg )
4150{
4151 psa_status_t status;
4152
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004153 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004154 return( PSA_ERROR_BAD_STATE );
4155
Gilles Peskine6843c292019-01-18 16:44:49 +01004156 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4157 return( PSA_ERROR_INVALID_ARGUMENT );
4158 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004159 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004160#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004161 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004162 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004163#else
4164 return( PSA_ERROR_NOT_SUPPORTED );
4165#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004166 }
4167 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4168 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004169#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004170 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004171#else
4172 return( PSA_ERROR_NOT_SUPPORTED );
4173#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004174 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004175 else
4176 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004177
4178 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004179 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004180 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004181}
4182
John Durkopd0321952020-10-29 21:37:36 -07004183#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004184static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004185 psa_algorithm_t hash_alg,
4186 psa_key_derivation_step_t step,
4187 const uint8_t *data,
4188 size_t data_length )
4189{
4190 psa_status_t status;
4191 switch( step )
4192 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004193 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004194 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004195 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004196 status = psa_hmac_setup_internal( &hkdf->hmac,
4197 data, data_length,
4198 hash_alg );
4199 if( status != PSA_SUCCESS )
4200 return( status );
4201 hkdf->state = HKDF_STATE_STARTED;
4202 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004203 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004204 /* If no salt was provided, use an empty salt. */
4205 if( hkdf->state == HKDF_STATE_INIT )
4206 {
4207 status = psa_hmac_setup_internal( &hkdf->hmac,
4208 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004209 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004210 if( status != PSA_SUCCESS )
4211 return( status );
4212 hkdf->state = HKDF_STATE_STARTED;
4213 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004214 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004215 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004216 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4217 data, data_length );
4218 if( status != PSA_SUCCESS )
4219 return( status );
4220 status = psa_hmac_finish_internal( &hkdf->hmac,
4221 hkdf->prk,
4222 sizeof( hkdf->prk ) );
4223 if( status != PSA_SUCCESS )
4224 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004225 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004226 hkdf->block_number = 0;
4227 hkdf->state = HKDF_STATE_KEYED;
4228 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004229 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004230 if( hkdf->state == HKDF_STATE_OUTPUT )
4231 return( PSA_ERROR_BAD_STATE );
4232 if( hkdf->info_set )
4233 return( PSA_ERROR_BAD_STATE );
4234 hkdf->info_length = data_length;
4235 if( data_length != 0 )
4236 {
4237 hkdf->info = mbedtls_calloc( 1, data_length );
4238 if( hkdf->info == NULL )
4239 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4240 memcpy( hkdf->info, data, data_length );
4241 }
4242 hkdf->info_set = 1;
4243 return( PSA_SUCCESS );
4244 default:
4245 return( PSA_ERROR_INVALID_ARGUMENT );
4246 }
4247}
John Durkop07cc04a2020-11-16 22:08:34 -08004248#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004249
John Durkop07cc04a2020-11-16 22:08:34 -08004250#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4251 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01004252static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4253 const uint8_t *data,
4254 size_t data_length )
4255{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004256 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01004257 return( PSA_ERROR_BAD_STATE );
4258
Janos Follathd6dce9f2019-07-04 09:11:38 +01004259 if( data_length != 0 )
4260 {
4261 prf->seed = mbedtls_calloc( 1, data_length );
4262 if( prf->seed == NULL )
4263 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004264
Janos Follathd6dce9f2019-07-04 09:11:38 +01004265 memcpy( prf->seed, data, data_length );
4266 prf->seed_length = data_length;
4267 }
Janos Follathf08e2652019-06-13 09:05:41 +01004268
Gilles Peskine43f958b2020-12-13 14:55:14 +01004269 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004270
4271 return( PSA_SUCCESS );
4272}
4273
Janos Follath81550542019-06-13 14:26:34 +01004274static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4275 psa_algorithm_t hash_alg,
4276 const uint8_t *data,
4277 size_t data_length )
4278{
4279 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01004280 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01004281 return( PSA_ERROR_BAD_STATE );
4282
4283 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4284 if( status != PSA_SUCCESS )
4285 return( status );
4286
Gilles Peskine43f958b2020-12-13 14:55:14 +01004287 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004288
4289 return( PSA_SUCCESS );
4290}
4291
Janos Follath63028dd2019-06-13 09:15:47 +01004292static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
4293 const uint8_t *data,
4294 size_t data_length )
4295{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004296 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01004297 return( PSA_ERROR_BAD_STATE );
4298
Janos Follathd6dce9f2019-07-04 09:11:38 +01004299 if( data_length != 0 )
4300 {
4301 prf->label = mbedtls_calloc( 1, data_length );
4302 if( prf->label == NULL )
4303 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01004304
Janos Follathd6dce9f2019-07-04 09:11:38 +01004305 memcpy( prf->label, data, data_length );
4306 prf->label_length = data_length;
4307 }
Janos Follath63028dd2019-06-13 09:15:47 +01004308
Gilles Peskine43f958b2020-12-13 14:55:14 +01004309 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004310
4311 return( PSA_SUCCESS );
4312}
4313
Janos Follathb03233e2019-06-11 15:30:30 +01004314static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
4315 psa_algorithm_t hash_alg,
4316 psa_key_derivation_step_t step,
4317 const uint8_t *data,
4318 size_t data_length )
4319{
Janos Follathb03233e2019-06-11 15:30:30 +01004320 switch( step )
4321 {
Janos Follathf08e2652019-06-13 09:05:41 +01004322 case PSA_KEY_DERIVATION_INPUT_SEED:
4323 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01004324 case PSA_KEY_DERIVATION_INPUT_SECRET:
4325 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01004326 case PSA_KEY_DERIVATION_INPUT_LABEL:
4327 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01004328 default:
4329 return( PSA_ERROR_INVALID_ARGUMENT );
4330 }
4331}
John Durkop07cc04a2020-11-16 22:08:34 -08004332#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4333 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4334
4335#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4336static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4337 psa_tls12_prf_key_derivation_t *prf,
4338 psa_algorithm_t hash_alg,
4339 const uint8_t *data,
4340 size_t data_length )
4341{
4342 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004343 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08004344 uint8_t *cur = pms;
4345
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004346 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08004347 return( PSA_ERROR_INVALID_ARGUMENT );
4348
4349 /* Quoting RFC 4279, Section 2:
4350 *
4351 * The premaster secret is formed as follows: if the PSK is N octets
4352 * long, concatenate a uint16 with the value N, N zero octets, a second
4353 * uint16 with the value N, and the PSK itself.
4354 */
4355
4356 *cur++ = ( data_length >> 8 ) & 0xff;
4357 *cur++ = ( data_length >> 0 ) & 0xff;
4358 memset( cur, 0, data_length );
4359 cur += data_length;
4360 *cur++ = pms[0];
4361 *cur++ = pms[1];
4362 memcpy( cur, data, data_length );
4363 cur += data_length;
4364
4365 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
4366
4367 mbedtls_platform_zeroize( pms, sizeof( pms ) );
4368 return( status );
4369}
Janos Follath6660f0e2019-06-17 08:44:03 +01004370
4371static psa_status_t psa_tls12_prf_psk_to_ms_input(
4372 psa_tls12_prf_key_derivation_t *prf,
4373 psa_algorithm_t hash_alg,
4374 psa_key_derivation_step_t step,
4375 const uint8_t *data,
4376 size_t data_length )
4377{
4378 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01004379 {
Janos Follath6660f0e2019-06-17 08:44:03 +01004380 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
4381 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01004382 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004383
4384 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
4385}
John Durkop07cc04a2020-11-16 22:08:34 -08004386#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004387
Gilles Peskineb8965192019-09-24 16:21:10 +02004388/** Check whether the given key type is acceptable for the given
4389 * input step of a key derivation.
4390 *
4391 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4392 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4393 * Both secret and non-secret inputs can alternatively have the type
4394 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4395 * that the input was passed as a buffer rather than via a key object.
4396 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004397static int psa_key_derivation_check_input_type(
4398 psa_key_derivation_step_t step,
4399 psa_key_type_t key_type )
4400{
4401 switch( step )
4402 {
4403 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02004404 if( key_type == PSA_KEY_TYPE_DERIVE )
4405 return( PSA_SUCCESS );
4406 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02004407 return( PSA_SUCCESS );
4408 break;
4409 case PSA_KEY_DERIVATION_INPUT_LABEL:
4410 case PSA_KEY_DERIVATION_INPUT_SALT:
4411 case PSA_KEY_DERIVATION_INPUT_INFO:
4412 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02004413 if( key_type == PSA_KEY_TYPE_RAW_DATA )
4414 return( PSA_SUCCESS );
4415 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02004416 return( PSA_SUCCESS );
4417 break;
4418 }
4419 return( PSA_ERROR_INVALID_ARGUMENT );
4420}
4421
Janos Follathb80a94e2019-06-12 15:54:46 +01004422static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004424 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004425 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004426 const uint8_t *data,
4427 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004428{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004429 psa_status_t status;
4430 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
4431
4432 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02004433 if( status != PSA_SUCCESS )
4434 goto exit;
4435
John Durkopd0321952020-10-29 21:37:36 -07004436#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004437 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004438 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004441 step, data, data_length );
4442 }
John Durkop07cc04a2020-11-16 22:08:34 -08004443 else
4444#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4445#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4446 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004447 {
Janos Follathb03233e2019-06-11 15:30:30 +01004448 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
4449 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
4450 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01004451 }
John Durkop07cc04a2020-11-16 22:08:34 -08004452 else
4453#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4454#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4455 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01004456 {
4457 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
4458 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
4459 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004460 }
4461 else
John Durkop07cc04a2020-11-16 22:08:34 -08004462#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004463 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004464 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004465 (void) data;
4466 (void) data_length;
4467 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004468 return( PSA_ERROR_BAD_STATE );
4469 }
4470
Gilles Peskine224b0d62019-09-23 18:13:17 +02004471exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004472 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004473 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004474 return( status );
4475}
4476
Janos Follath51f4a0f2019-06-14 11:35:55 +01004477psa_status_t psa_key_derivation_input_bytes(
4478 psa_key_derivation_operation_t *operation,
4479 psa_key_derivation_step_t step,
4480 const uint8_t *data,
4481 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004482{
Gilles Peskineb8965192019-09-24 16:21:10 +02004483 return( psa_key_derivation_input_internal( operation, step,
4484 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01004485 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004486}
4487
Janos Follath51f4a0f2019-06-14 11:35:55 +01004488psa_status_t psa_key_derivation_input_key(
4489 psa_key_derivation_operation_t *operation,
4490 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004491 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004492{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004493 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004494 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004495 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004496
Ronald Cron5c522922020-11-14 16:35:34 +01004497 status = psa_get_and_lock_transparent_key_slot_with_policy(
4498 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004499 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02004500 {
4501 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004502 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02004503 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004504
4505 /* Passing a key object as a SECRET input unlocks the permission
4506 * to output to a key object. */
4507 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
4508 operation->can_output_key = 1;
4509
Ronald Cronf95a2b12020-10-22 15:24:49 +02004510 status = psa_key_derivation_input_internal( operation,
4511 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004512 slot->key.data,
4513 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004514
Ronald Cron5c522922020-11-14 16:35:34 +01004515 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004516
Ronald Cron5c522922020-11-14 16:35:34 +01004517 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004518}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004519
4520
4521
4522/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004523/* Key agreement */
4524/****************************************************************/
4525
John Durkop6ba40d12020-11-10 08:50:04 -08004526#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004527static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4528 size_t peer_key_length,
4529 const mbedtls_ecp_keypair *our_key,
4530 uint8_t *shared_secret,
4531 size_t shared_secret_size,
4532 size_t *shared_secret_length )
4533{
Steven Cooremand4867872020-08-05 16:31:39 +02004534 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004535 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004536 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004537 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01004538 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004539 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004540
Ronald Cron90857082020-11-25 14:58:33 +01004541 status = mbedtls_psa_ecp_load_representation(
4542 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01004543 bits,
Ronald Cron90857082020-11-25 14:58:33 +01004544 peer_key,
4545 peer_key_length,
4546 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004547 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004548 goto exit;
4549
Jaeden Amero97271b32019-01-10 19:38:51 +00004550 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02004551 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00004552 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004553 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004554 status = mbedtls_to_psa_error(
4555 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4556 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004557 goto exit;
4558
Jaeden Amero97271b32019-01-10 19:38:51 +00004559 status = mbedtls_to_psa_error(
4560 mbedtls_ecdh_calc_secret( &ecdh,
4561 shared_secret_length,
4562 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004563 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004564 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004565 if( status != PSA_SUCCESS )
4566 goto exit;
4567 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
4568 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004569
4570exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01004571 if( status != PSA_SUCCESS )
4572 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004573 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02004574 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02004575 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02004576
Jaeden Amero97271b32019-01-10 19:38:51 +00004577 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004578}
John Durkop6ba40d12020-11-10 08:50:04 -08004579#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004580
Gilles Peskine01d718c2018-09-18 12:01:02 +02004581#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4582
Gilles Peskine0216fe12019-04-11 21:23:21 +02004583static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4584 psa_key_slot_t *private_key,
4585 const uint8_t *peer_key,
4586 size_t peer_key_length,
4587 uint8_t *shared_secret,
4588 size_t shared_secret_size,
4589 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004590{
Gilles Peskine0216fe12019-04-11 21:23:21 +02004591 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004592 {
John Durkop6ba40d12020-11-10 08:50:04 -08004593#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004594 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02004595 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004596 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02004597 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004598 psa_status_t status = mbedtls_psa_ecp_load_representation(
4599 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01004600 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01004601 private_key->key.data,
4602 private_key->key.bytes,
4603 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004604 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004605 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02004606 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02004607 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004608 shared_secret, shared_secret_size,
4609 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004610 mbedtls_ecp_keypair_free( ecp );
4611 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02004612 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08004613#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004614 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004615 (void) private_key;
4616 (void) peer_key;
4617 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004618 (void) shared_secret;
4619 (void) shared_secret_size;
4620 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004621 return( PSA_ERROR_NOT_SUPPORTED );
4622 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02004623}
4624
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004625/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02004626 * to potentially free embedded data structures and wipe confidential data.
4627 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004628static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004629 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004630 psa_key_slot_t *private_key,
4631 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004632 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004633{
4634 psa_status_t status;
4635 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4636 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004637 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004638
4639 /* Step 1: run the secret agreement algorithm to generate the shared
4640 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004641 status = psa_key_agreement_raw_internal( ka_alg,
4642 private_key,
4643 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03004644 shared_secret,
4645 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02004646 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004647 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004648 goto exit;
4649
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004650 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02004651 * the shared secret. A shared secret is permitted wherever a key
4652 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01004653 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004654 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01004655 shared_secret,
4656 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004657exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004658 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004659 return( status );
4660}
4661
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004663 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004664 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004665 const uint8_t *peer_key,
4666 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004667{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004668 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004669 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004670 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004671
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004672 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004673 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01004674 status = psa_get_and_lock_transparent_key_slot_with_policy(
4675 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004676 if( status != PSA_SUCCESS )
4677 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004678 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004679 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004680 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004681 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004682 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004683 else
4684 {
4685 /* If a private key has been added as SECRET, we allow the derived
4686 * key material to be used as a key in PSA Crypto. */
4687 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
4688 operation->can_output_key = 1;
4689 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004690
Ronald Cron5c522922020-11-14 16:35:34 +01004691 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004692
Ronald Cron5c522922020-11-14 16:35:34 +01004693 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004694}
4695
Gilles Peskinebe697d82019-05-16 18:00:41 +02004696psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004697 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02004698 const uint8_t *peer_key,
4699 size_t peer_key_length,
4700 uint8_t *output,
4701 size_t output_size,
4702 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02004703{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004705 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004706 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004707
4708 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4709 {
4710 status = PSA_ERROR_INVALID_ARGUMENT;
4711 goto exit;
4712 }
Ronald Cron5c522922020-11-14 16:35:34 +01004713 status = psa_get_and_lock_transparent_key_slot_with_policy(
4714 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004715 if( status != PSA_SUCCESS )
4716 goto exit;
4717
4718 status = psa_key_agreement_raw_internal( alg, slot,
4719 peer_key, peer_key_length,
4720 output, output_size,
4721 output_length );
4722
4723exit:
4724 if( status != PSA_SUCCESS )
4725 {
4726 /* If an error happens and is not handled properly, the output
4727 * may be used as a key to protect sensitive data. Arrange for such
4728 * a key to be random, which is likely to result in decryption or
4729 * verification errors. This is better than filling the buffer with
4730 * some constant data such as zeros, which would result in the data
4731 * being protected with a reproducible, easily knowable key.
4732 */
4733 psa_generate_random( output, output_size );
4734 *output_length = output_size;
4735 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004736
Ronald Cron5c522922020-11-14 16:35:34 +01004737 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004738
Ronald Cron5c522922020-11-14 16:35:34 +01004739 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004740}
Gilles Peskine86a440b2018-11-12 18:39:40 +01004741
4742
Gilles Peskine30524eb2020-11-13 17:02:26 +01004743
Gilles Peskine86a440b2018-11-12 18:39:40 +01004744/****************************************************************/
4745/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02004746/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02004747
Gilles Peskine30524eb2020-11-13 17:02:26 +01004748/** Initialize the PSA random generator.
4749 */
4750static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
4751{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004752#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4753 memset( rng, 0, sizeof( *rng ) );
4754#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4755
Gilles Peskine30524eb2020-11-13 17:02:26 +01004756 /* Set default configuration if
4757 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4758 if( rng->entropy_init == NULL )
4759 rng->entropy_init = mbedtls_entropy_init;
4760 if( rng->entropy_free == NULL )
4761 rng->entropy_free = mbedtls_entropy_free;
4762
4763 rng->entropy_init( &rng->entropy );
4764#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
4765 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
4766 /* The PSA entropy injection feature depends on using NV seed as an entropy
4767 * source. Add NV seed as an entropy source for PSA entropy injection. */
4768 mbedtls_entropy_add_source( &rng->entropy,
4769 mbedtls_nv_seed_poll, NULL,
4770 MBEDTLS_ENTROPY_BLOCK_SIZE,
4771 MBEDTLS_ENTROPY_SOURCE_STRONG );
4772#endif
4773
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004774 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004775#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01004776}
4777
4778/** Deinitialize the PSA random generator.
4779 */
4780static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
4781{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004782#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4783 memset( rng, 0, sizeof( *rng ) );
4784#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004785 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01004786 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004787#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01004788}
4789
4790/** Seed the PSA random generator.
4791 */
4792static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
4793{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004794#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4795 /* Do nothing: the external RNG seeds itself. */
4796 (void) rng;
4797 return( PSA_SUCCESS );
4798#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01004799 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004800 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
4801 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01004802 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004803#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01004804}
4805
Gilles Peskinee59236f2018-01-27 23:32:46 +01004806psa_status_t psa_generate_random( uint8_t *output,
4807 size_t output_size )
4808{
Gilles Peskinee59236f2018-01-27 23:32:46 +01004809 GUARD_MODULE_INITIALIZED;
4810
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004811#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4812
4813 size_t output_length = 0;
4814 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
4815 output, output_size,
4816 &output_length );
4817 if( status != PSA_SUCCESS )
4818 return( status );
4819 /* Breaking up a request into smaller chunks is currently not supported
4820 * for the extrernal RNG interface. */
4821 if( output_length != output_size )
4822 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
4823 return( PSA_SUCCESS );
4824
4825#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4826
Gilles Peskine71ddab92021-01-04 21:01:07 +01004827 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02004828 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01004829 size_t request_size =
4830 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
4831 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
4832 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01004833 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
4834 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02004835 if( ret != 0 )
4836 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01004837 output_size -= request_size;
4838 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02004839 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01004840 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004841#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004842}
4843
Gilles Peskine8814fc42020-12-14 15:33:44 +01004844/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01004845 *
4846 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
4847 * `psa_generate_random(...)`. The state parameter is ignored since the
4848 * PSA API doesn't support passing an explicit state.
4849 *
4850 * In the non-external case, psa_generate_random() calls an
4851 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
4852 * and semantics as mbedtls_psa_get_random(). As an optimization,
4853 * instead of doing this back-and-forth between the PSA API and the
4854 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
4855 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01004856 */
4857#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4858int mbedtls_psa_get_random( void *p_rng,
4859 unsigned char *output,
4860 size_t output_size )
4861{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01004862 /* This function takes a pointer to the RNG state because that's what
4863 * classic mbedtls functions using an RNG expect. The PSA RNG manages
4864 * its own state internally and doesn't let the caller access that state.
4865 * So we just ignore the state parameter, and in practice we'll pass
4866 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01004867 (void) p_rng;
4868 psa_status_t status = psa_generate_random( output, output_size );
4869 if( status == PSA_SUCCESS )
4870 return( 0 );
4871 else
4872 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
4873}
4874#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4875
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004876#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
4877#include "mbedtls/entropy_poll.h"
4878
Gilles Peskine7228da22019-07-15 11:06:15 +02004879psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004880 size_t seed_size )
4881{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004882 if( global_data.initialized )
4883 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004884
4885 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4886 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4887 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4888 return( PSA_ERROR_INVALID_ARGUMENT );
4889
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004890 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004891}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004892#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004893
Ronald Cron3772afe2021-02-08 16:10:05 +01004894/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02004895 *
Ronald Cron3772afe2021-02-08 16:10:05 +01004896 * \param type The key type
4897 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02004898 *
4899 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01004900 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02004901 * \retval #PSA_ERROR_INVALID_ARGUMENT
4902 * The size in bits of the key is not valid.
4903 * \retval #PSA_ERROR_NOT_SUPPORTED
4904 * The type and/or the size in bits of the key or the combination of
4905 * the two is not supported.
4906 */
Ronald Cron3772afe2021-02-08 16:10:05 +01004907static psa_status_t psa_validate_key_type_and_size_for_key_generation(
4908 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004909{
Ronald Cronf3bb7612020-10-02 20:11:59 +02004910 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004911
Gilles Peskinee59236f2018-01-27 23:32:46 +01004912 if( key_type_is_raw_bytes( type ) )
4913 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02004914 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004915 if( status != PSA_SUCCESS )
4916 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02004917 }
4918 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01004919#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02004920 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
4921 {
4922 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4923 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02004924
Ronald Cron01b2aba2020-10-05 09:42:02 +02004925 /* Accept only byte-aligned keys, for the same reasons as
4926 * in psa_import_rsa_key(). */
4927 if( bits % 8 != 0 )
4928 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02004929 }
4930 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01004931#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02004932
Ronald Cron5c4d3862020-12-07 11:07:24 +01004933#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02004934 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
4935 {
Ronald Crond81ab562021-02-16 09:01:16 +01004936 /* To avoid empty block, return successfully here. */
4937 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02004938 }
4939 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01004940#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02004941 {
4942 return( PSA_ERROR_NOT_SUPPORTED );
4943 }
4944
4945 return( PSA_SUCCESS );
4946}
4947
Ronald Cron55ed0592020-10-05 10:30:40 +02004948psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02004949 const psa_key_attributes_t *attributes,
4950 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02004951{
4952 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02004953 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02004954
Ronald Cron2a38a6b2020-10-02 20:02:04 +02004955 if( ( attributes->domain_parameters == NULL ) &&
4956 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02004957 return( PSA_ERROR_INVALID_ARGUMENT );
4958
Ronald Cron01b2aba2020-10-05 09:42:02 +02004959 if( key_type_is_raw_bytes( type ) )
4960 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02004961 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004962 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004963 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02004964
David Brown12ca5032021-02-11 11:02:00 -07004965#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01004966 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02004967 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07004968#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004969 }
4970 else
4971
John Durkop07cc04a2020-11-16 22:08:34 -08004972#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004973 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004974 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01004975 return( mbedtls_psa_rsa_generate_key( attributes,
4976 key_buffer,
4977 key_buffer_size,
4978 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004979 }
4980 else
John Durkop07cc04a2020-11-16 22:08:34 -08004981#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004982
John Durkop9814fa22020-11-04 12:28:15 -08004983#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004984 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004985 {
Ronald Cron7023db52020-11-20 18:17:42 +01004986 return( mbedtls_psa_ecp_generate_key( attributes,
4987 key_buffer,
4988 key_buffer_size,
4989 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004990 }
4991 else
John Durkop9814fa22020-11-04 12:28:15 -08004992#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02004993 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02004994 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004995 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02004996 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01004997
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004998 return( PSA_SUCCESS );
4999}
Darryl Green0c6575a2018-11-07 16:05:30 +00005000
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005001psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005002 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005003{
5004 psa_status_t status;
5005 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005006 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005007 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005008
Ronald Cron81709fc2020-11-14 12:10:32 +01005009 *key = MBEDTLS_SVC_KEY_ID_INIT;
5010
Gilles Peskine0f84d622019-09-12 19:03:13 +02005011 /* Reject any attempt to create a zero-length key so that we don't
5012 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5013 if( psa_get_key_bits( attributes ) == 0 )
5014 return( PSA_ERROR_INVALID_ARGUMENT );
5015
Ronald Cron81709fc2020-11-14 12:10:32 +01005016 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
5017 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005018 if( status != PSA_SUCCESS )
5019 goto exit;
5020
Ronald Cron977c2472020-10-13 08:32:21 +02005021 /* In the case of a transparent key or an opaque key stored in local
5022 * storage (thus not in the case of generating a key in a secure element
5023 * or cryptoprocessor with storage), we have to allocate a buffer to
5024 * hold the generated key material. */
5025 if( slot->key.data == NULL )
5026 {
5027 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
5028 PSA_KEY_LOCATION_LOCAL_STORAGE )
5029 {
Ronald Cron3772afe2021-02-08 16:10:05 +01005030 status = psa_validate_key_type_and_size_for_key_generation(
5031 attributes->core.type, attributes->core.bits );
5032 if( status != PSA_SUCCESS )
5033 goto exit;
5034
5035 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
5036 attributes->core.type,
5037 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02005038 }
5039 else
5040 {
5041 status = psa_driver_wrapper_get_key_buffer_size(
5042 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01005043 if( status != PSA_SUCCESS )
5044 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02005045 }
Ronald Cron977c2472020-10-13 08:32:21 +02005046
5047 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
5048 if( status != PSA_SUCCESS )
5049 goto exit;
5050 }
5051
Steven Cooreman2a1664c2020-07-20 15:33:08 +02005052 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02005053 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02005054
Ronald Cron2b56bc82020-10-05 10:02:26 +02005055 if( status != PSA_SUCCESS )
5056 psa_remove_key_data_from_memory( slot );
5057
Gilles Peskine11792082019-08-06 18:36:36 +02005058exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005059 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005060 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005061 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005062 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005063
Darryl Green0c6575a2018-11-07 16:05:30 +00005064 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005065}
5066
Gilles Peskinee59236f2018-01-27 23:32:46 +01005067/****************************************************************/
5068/* Module setup */
5069/****************************************************************/
5070
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005071#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005072psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5073 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5074 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5075{
5076 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5077 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005078 global_data.rng.entropy_init = entropy_init;
5079 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01005080 return( PSA_SUCCESS );
5081}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005082#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005083
Gilles Peskinee59236f2018-01-27 23:32:46 +01005084void mbedtls_psa_crypto_free( void )
5085{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005086 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005087 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5088 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005089 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005090 }
5091 /* Wipe all remaining data, including configuration.
5092 * In particular, this sets all state indicator to the value
5093 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005094 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005095#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005096 /* Unregister all secure element drivers, so that we restart from
5097 * a pristine state. */
5098 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005099#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005100}
5101
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005102#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5103/** Recover a transaction that was interrupted by a power failure.
5104 *
5105 * This function is called during initialization, before psa_crypto_init()
5106 * returns. If this function returns a failure status, the initialization
5107 * fails.
5108 */
5109static psa_status_t psa_crypto_recover_transaction(
5110 const psa_crypto_transaction_t *transaction )
5111{
5112 switch( transaction->unknown.type )
5113 {
5114 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5115 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005116 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005117 * is implemented.
5118 * https://github.com/ARMmbed/mbed-crypto/issues/218
5119 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005120 default:
5121 /* We found an unsupported transaction in the storage.
5122 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01005123 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005124 }
5125}
5126#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5127
Gilles Peskinee59236f2018-01-27 23:32:46 +01005128psa_status_t psa_crypto_init( void )
5129{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005130 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005131
Gilles Peskinec6b69072018-11-20 21:42:52 +01005132 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005133 if( global_data.initialized != 0 )
5134 return( PSA_SUCCESS );
5135
Gilles Peskine30524eb2020-11-13 17:02:26 +01005136 /* Initialize and seed the random generator. */
5137 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005138 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01005139 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005140 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005141 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005142 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005143
Gilles Peskine66fb1262018-12-10 16:29:04 +01005144 status = psa_initialize_key_slots( );
5145 if( status != PSA_SUCCESS )
5146 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005147
Gilles Peskined9348f22019-10-01 15:22:29 +02005148#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5149 status = psa_init_all_se_drivers( );
5150 if( status != PSA_SUCCESS )
5151 goto exit;
5152#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5153
Gilles Peskine4b734222019-07-24 15:56:31 +02005154#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005155 status = psa_crypto_load_transaction( );
5156 if( status == PSA_SUCCESS )
5157 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005158 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5159 if( status != PSA_SUCCESS )
5160 goto exit;
5161 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005162 }
5163 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5164 {
5165 /* There's no transaction to complete. It's all good. */
5166 status = PSA_SUCCESS;
5167 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005168#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005169
Gilles Peskinec6b69072018-11-20 21:42:52 +01005170 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005171 global_data.initialized = 1;
5172
5173exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005174 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005175 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005176 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005177}
5178
5179#endif /* MBEDTLS_PSA_CRYPTO_C */